From 7a49bd0747e87965b80fa794142f177cab4177b5 Mon Sep 17 00:00:00 2001
From: Silke Hofstra <silke@slxh.eu>
Date: Sat, 13 Oct 2018 20:39:17 +0200
Subject: [PATCH] tests: add test for the libb2 package

---
 tests/pkg_libb2/Makefile        | 14 ++++++
 tests/pkg_libb2/main.c          | 86 +++++++++++++++++++++++++++++++++
 tests/pkg_libb2/tests/01-run.py | 18 +++++++
 3 files changed, 118 insertions(+)
 create mode 100644 tests/pkg_libb2/Makefile
 create mode 100644 tests/pkg_libb2/main.c
 create mode 100755 tests/pkg_libb2/tests/01-run.py

diff --git a/tests/pkg_libb2/Makefile b/tests/pkg_libb2/Makefile
new file mode 100644
index 0000000000..c7bd172537
--- /dev/null
+++ b/tests/pkg_libb2/Makefile
@@ -0,0 +1,14 @@
+include ../Makefile.tests_common
+
+# BLAKE2s + BLAKE2 is too big for these boards
+BOARD_INSUFFICIENT_MEMORY := arduino-duemilanove arduino-uno chronos \
+                             mega-xplained msb-430 msb-430h nucleo-f031k6 \
+                             nucleo-f042k6 nucleo-l031k6 telosb waspmote-pro \
+                             wsn430-v1_3b wsn430-v1_4 z1
+
+TEST_ON_CI_WHITELIST += all
+
+USEPKG += libb2
+USEMODULE += embunit
+
+include $(RIOTBASE)/Makefile.include
diff --git a/tests/pkg_libb2/main.c b/tests/pkg_libb2/main.c
new file mode 100644
index 0000000000..1ac0d2c432
--- /dev/null
+++ b/tests/pkg_libb2/main.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2018 Silke Hofstra
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ */
+
+/**
+ * @ingroup     tests
+ * @{
+ *
+ * @file
+ * @brief       Test the libb2 package
+ *
+ * @author      Silke Hofstra <silke@slxh.eu>
+ *
+ * @}
+ */
+
+#include <string.h>
+#include "embUnit.h"
+#include "blake2.h"
+
+const uint8_t msg[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+                        0x08, 0x09, 0x0a, 0x0b, 0x0c };
+const uint8_t key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+                        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+                        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+                        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+                        0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
+                        0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+                        0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+                        0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f };
+const uint8_t b2s[] = { 0xf5, 0xc4, 0xbe, 0xc6, 0xd6, 0x2f, 0xc6, 0x08,
+                        0xbf, 0x41, 0xcc, 0x11, 0x5f, 0x16, 0xd6, 0x1c,
+                        0x7e, 0xfd, 0x3f, 0xf6, 0xc6, 0x56, 0x92, 0xbb,
+                        0xe0, 0xaf, 0xff, 0xb1, 0xfe, 0xde, 0x74, 0x75 };
+const uint8_t b2b[] = { 0x43, 0xd4, 0x4b, 0xfa, 0x18, 0x76, 0x8c, 0x59,
+                        0x89, 0x6b, 0xf7, 0xed, 0x17, 0x65, 0xcb, 0x2d,
+                        0x14, 0xaf, 0x8c, 0x26, 0x02, 0x66, 0x03, 0x90,
+                        0x99, 0xb2, 0x5a, 0x60, 0x3e, 0x4d, 0xdc, 0x50,
+                        0x39, 0xd6, 0xef, 0x3a, 0x91, 0x84, 0x7d, 0x10,
+                        0x88, 0xd4, 0x01, 0xc0, 0xc7, 0xe8, 0x47, 0x78,
+                        0x1a, 0x8a, 0x59, 0x0d, 0x33, 0xa3, 0xc6, 0xcb,
+                        0x4d, 0xf0, 0xfa, 0xb1, 0xc2, 0xf2, 0x23, 0x55 };
+
+/* Test BLAKE2s */
+static void test_blake2s(void)
+{
+    uint8_t hash[BLAKE2S_OUTBYTES];
+    int res = blake2s(hash, msg, key,
+                      sizeof hash, sizeof msg, BLAKE2S_KEYBYTES);
+
+    TEST_ASSERT(res == 0);
+    TEST_ASSERT(memcmp(b2s, hash, sizeof hash) == 0);
+}
+
+/* Test BLAKE2b */
+static void test_blake2b(void)
+{
+    uint8_t hash[BLAKE2B_OUTBYTES];
+    int res = blake2b(hash, msg, key,
+                      sizeof hash, sizeof msg, BLAKE2B_KEYBYTES);
+
+    TEST_ASSERT(res == 0);
+    TEST_ASSERT(memcmp(b2b, hash, sizeof hash) == 0);
+}
+
+Test *tests_blake2(void)
+{
+    EMB_UNIT_TESTFIXTURES(fixtures) {
+        new_TestFixture(test_blake2s),
+        new_TestFixture(test_blake2b),
+    };
+    EMB_UNIT_TESTCALLER(blake2_tests, NULL, NULL, fixtures);
+    return (Test *)&blake2_tests;
+}
+
+int main(void)
+{
+    TESTS_START();
+    TESTS_RUN(tests_blake2());
+    TESTS_END();
+    return 0;
+}
diff --git a/tests/pkg_libb2/tests/01-run.py b/tests/pkg_libb2/tests/01-run.py
new file mode 100755
index 0000000000..36a1bc15fc
--- /dev/null
+++ b/tests/pkg_libb2/tests/01-run.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python3
+
+# Copyright (C) 2017 Freie Universität Berlin
+#
+# This file is subject to the terms and conditions of the GNU Lesser
+# General Public License v2.1. See the file LICENSE in the top level
+# directory for more details.
+
+import sys
+from testrunner import run
+
+
+def testfunc(child):
+    child.expect_exact('OK (2 tests)')
+
+
+if __name__ == "__main__":
+    sys.exit(run(testfunc))
-- 
GitLab