From ffb1ba6e47ff51e90f28ea00c8747fb46524b37b Mon Sep 17 00:00:00 2001
From: Francisco Acosta <fco.ja.ac@gmail.com>
Date: Thu, 25 Oct 2018 15:44:37 +0200
Subject: [PATCH] tests: add riotboot_hdr test

Unittest for riotboot_hdr submodule. If successful, it must
print 4 successful tests.
---
 tests/riotboot_hdr/Makefile        | 10 ++++
 tests/riotboot_hdr/main.c          | 93 ++++++++++++++++++++++++++++++
 tests/riotboot_hdr/tests/01-run.py | 18 ++++++
 3 files changed, 121 insertions(+)
 create mode 100644 tests/riotboot_hdr/Makefile
 create mode 100644 tests/riotboot_hdr/main.c
 create mode 100755 tests/riotboot_hdr/tests/01-run.py

diff --git a/tests/riotboot_hdr/Makefile b/tests/riotboot_hdr/Makefile
new file mode 100644
index 0000000000..68cbbd8c11
--- /dev/null
+++ b/tests/riotboot_hdr/Makefile
@@ -0,0 +1,10 @@
+include ../Makefile.tests_common
+
+USEMODULE += riotboot_hdr
+USEMODULE += embunit
+
+HDR_LOG_LEVEL ?= LOG_NONE
+
+CFLAGS += -DLOG_LEVEL=$(HDR_LOG_LEVEL)
+
+include $(RIOTBASE)/Makefile.include
diff --git a/tests/riotboot_hdr/main.c b/tests/riotboot_hdr/main.c
new file mode 100644
index 0000000000..109dff56bb
--- /dev/null
+++ b/tests/riotboot_hdr/main.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2018 Inria
+ *
+ * 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      Tests for module riotboot_hdr
+ *
+ * @author     Francisco Acosta <francisco.acosta@inria.fr>
+ */
+
+#include <stdio.h>
+
+#include "riotboot/hdr.h"
+#include "embUnit.h"
+
+const riotboot_hdr_t riotboot_hdr_good = {
+    .magic_number = RIOTBOOT_MAGIC,
+    .version = 0x5bd19bff,
+    .start_addr = 0x00001100,
+    .chksum = 0x02eda672
+};
+
+const riotboot_hdr_t riotboot_hdr_bad_magic = {
+    .magic_number = 0x12345678,
+    .version = 0x5bd19bff,
+    .start_addr = 0x00001100,
+    .chksum = 0x02eda672
+};
+
+const riotboot_hdr_t riotboot_hdr_bad_chksum = {
+    .magic_number = RIOTBOOT_MAGIC,
+    .version = 0x5bd19bff,
+    .start_addr = 0x00001100,
+    .chksum = 0x02000000
+};
+
+static void test_riotboot_hdr_01(void)
+{
+    int ret = riotboot_hdr_validate(&riotboot_hdr_good);
+
+    TEST_ASSERT_EQUAL_INT(0, ret);
+}
+
+static void test_riotboot_hdr_02(void)
+{
+    int ret = riotboot_hdr_validate(&riotboot_hdr_bad_magic);
+
+    TEST_ASSERT_EQUAL_INT(-1, ret);
+}
+
+static void test_riotboot_hdr_03(void)
+{
+    int ret = riotboot_hdr_validate(&riotboot_hdr_bad_chksum);
+
+    TEST_ASSERT_EQUAL_INT(-1, ret);
+}
+
+static void test_riotboot_hdr_04(void)
+{
+    uint32_t chksum = riotboot_hdr_checksum(&riotboot_hdr_good);
+
+    TEST_ASSERT_EQUAL_INT(0x02eda672, chksum);
+}
+
+Test *tests_riotboot_hdr(void)
+{
+    EMB_UNIT_TESTFIXTURES(fixtures) {
+        new_TestFixture(test_riotboot_hdr_01),
+        new_TestFixture(test_riotboot_hdr_02),
+        new_TestFixture(test_riotboot_hdr_03),
+        new_TestFixture(test_riotboot_hdr_04),
+    };
+
+    EMB_UNIT_TESTCALLER(riotboot_hdr_tests, NULL, NULL, fixtures);
+
+    return (Test *)&riotboot_hdr_tests;
+}
+
+int main(void)
+{
+    TESTS_START();
+    TESTS_RUN(tests_riotboot_hdr());
+    TESTS_END();
+    return 0;
+}
diff --git a/tests/riotboot_hdr/tests/01-run.py b/tests/riotboot_hdr/tests/01-run.py
new file mode 100755
index 0000000000..8d71a331ef
--- /dev/null
+++ b/tests/riotboot_hdr/tests/01-run.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python3
+
+# Copyright (C) 2018 Francisco Acosta <francisco.acosta@inria.fr>
+#
+# 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(r"OK \(\d+ tests\)")
+
+
+if __name__ == "__main__":
+    sys.exit(run(testfunc))
-- 
GitLab