add cram based unit tests
authorPetr Štetiar <ynezz@true.cz>
Thu, 17 Oct 2019 20:50:06 +0000 (22:50 +0200)
committerPetr Štetiar <ynezz@true.cz>
Sat, 9 Nov 2019 13:28:09 +0000 (14:28 +0100)
For improved QA etc.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
.gitignore [new file with mode: 0644]
.gitlab-ci.yml
CMakeLists.txt
tests/CMakeLists.txt [new file with mode: 0644]
tests/artifacts/key-build.ucert [new file with mode: 0644]
tests/artifacts/metadata.json [new file with mode: 0644]
tests/cram/CMakeLists.txt [new file with mode: 0644]
tests/cram/test_crc32.t [new file with mode: 0644]
tests/cram/test_fwtool.t [new file with mode: 0644]
tests/test-crc32.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..e13f3ab
--- /dev/null
@@ -0,0 +1,3 @@
+tests/cram/*.t.err
+*.pyc
+.venv
index 56180ce3c91590b7be5bffa44ad2c7a76baaa83b..a6995285b3e2eff12e34b5b935b935289af91a2c 100644 (file)
@@ -1,3 +1,6 @@
+variables:
+  CI_ENABLE_UNIT_TESTING: 1
+
 include:
   - remote: https://gitlab.com/ynezz/openwrt-ci/raw/master/openwrt-ci/gitlab/main.yml
   - remote: https://gitlab.com/ynezz/openwrt-ci/raw/master/openwrt-ci/gitlab/pipeline.yml
index 8c449f9be6acb45034c232c261e1aa210301b70a..dfe6045fb4e8f299650c2be6b962169073b5d77c 100644 (file)
@@ -5,4 +5,10 @@ INCLUDE(GNUInstallDirs)
 
 ADD_DEFINITIONS(-Wall -Werror -Wextra -Wno-unused-parameter)
 ADD_EXECUTABLE(fwtool fwtool.c)
+
+IF(UNIT_TESTING)
+       ENABLE_TESTING()
+       ADD_SUBDIRECTORY(tests)
+ENDIF()
+
 INSTALL(TARGETS fwtool RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..e07ce4d
--- /dev/null
@@ -0,0 +1,4 @@
+ADD_SUBDIRECTORY(cram)
+
+ADD_EXECUTABLE(test-crc32 test-crc32.c)
+TARGET_INCLUDE_DIRECTORIES(test-crc32 PRIVATE ${PROJECT_SOURCE_DIR})
diff --git a/tests/artifacts/key-build.ucert b/tests/artifacts/key-build.ucert
new file mode 100644 (file)
index 0000000..7228f11
Binary files /dev/null and b/tests/artifacts/key-build.ucert differ
diff --git a/tests/artifacts/metadata.json b/tests/artifacts/metadata.json
new file mode 100644 (file)
index 0000000..0c32fae
--- /dev/null
@@ -0,0 +1 @@
+{  "metadata_version": "1.0", "supported_devices":["tplink,archer-c7-v5","archer-c7-v5"], "version": { "dist": "OpenWrt", "version": "SNAPSHOT", "revision": "r11215+4-bba6646b5cba", "target": "ath79/generic", "board": "tplink_archer-c7-v5" } }
diff --git a/tests/cram/CMakeLists.txt b/tests/cram/CMakeLists.txt
new file mode 100644 (file)
index 0000000..abf7a4d
--- /dev/null
@@ -0,0 +1,22 @@
+FIND_PACKAGE(PythonInterp 3 REQUIRED)
+FILE(GLOB test_cases "test_*.t")
+
+SET(PYTHON_VENV_DIR "${CMAKE_CURRENT_BINARY_DIR}/.venv")
+SET(PYTHON_VENV_PIP "${PYTHON_VENV_DIR}/bin/pip")
+SET(PYTHON_VENV_CRAM "${PYTHON_VENV_DIR}/bin/cram")
+
+ADD_CUSTOM_COMMAND(
+       OUTPUT ${PYTHON_VENV_CRAM}
+       COMMAND ${PYTHON_EXECUTABLE} -m venv ${PYTHON_VENV_DIR}
+       COMMAND ${PYTHON_VENV_PIP} install cram
+)
+ADD_CUSTOM_TARGET(prepare-cram-venv ALL DEPENDS ${PYTHON_VENV_CRAM})
+
+ADD_TEST(
+       NAME cram
+       COMMAND ${PYTHON_VENV_CRAM} ${test_cases}
+       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "FWTOOL=$<TARGET_FILE:fwtool>")
+SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "TEST_CRC32=$<TARGET_FILE:test-crc32>")
diff --git a/tests/cram/test_crc32.t b/tests/cram/test_crc32.t
new file mode 100644 (file)
index 0000000..9102336
--- /dev/null
@@ -0,0 +1,13 @@
+set PATH for convenience:
+
+  $ [ -n "$TEST_CRC32" ] && export PATH="$(dirname "$TEST_CRC32"):$PATH"
+  $ alias test-crc32='valgrind --quiet --leak-check=full test-crc32'
+
+check that crc32 is producing expected results:
+
+  $ test-crc32
+  all_ffs=0x44f33105
+  all_7fs=0x3726dda7
+  all_ones=0xbea1dbe8
+  all_zeroes=0xa77a69f2
+  quick_fox=0x7c7180f2
diff --git a/tests/cram/test_fwtool.t b/tests/cram/test_fwtool.t
new file mode 100644 (file)
index 0000000..530cf3d
--- /dev/null
@@ -0,0 +1,91 @@
+set PATH and ARTIFACTS for convenience:
+
+  $ [ -n "$FWTOOL" ] && export PATH="$(dirname "$FWTOOL"):$PATH"
+  $ export ARTIFACTS="$TESTDIR/../artifacts"
+  $ alias fwtool='valgrind --quiet --leak-check=full fwtool'
+
+check usage:
+
+  $ fwtool
+  Usage: fwtool <options> <firmware>
+  
+  Options:
+    -S <file>:\t\tAppend signature file to firmware image (esc)
+    -I <file>:\t\tAppend metadata file to firmware image (esc)
+    -s <file>:\t\tExtract signature file from firmware image (esc)
+    -i <file>:\t\tExtract metadata file from firmware image (esc)
+    -t:\t\t\tRemove extracted chunks from firmare image (using -s, -i) (esc)
+    -T:\t\t\tOutput firmware image without extracted chunks to stdout (using -s, -i) (esc)
+    -q:\t\t\tQuiet (suppress error messages) (esc)
+  
+  [1]
+
+check that -T don't truncate ouput by 16 bytes and produces desired error output:
+
+  $ dd if=/dev/urandom of=image.bin bs=512k count=1 2> /dev/null
+  $ cp image.bin image.bin.stripped
+  $ md5sum image.bin* > md5sums
+
+  $ fwtool -T -i /dev/null image.bin > image.bin.stripped
+  Data not found
+  [1]
+
+  $ md5sum --check md5sums
+  image.bin: OK
+  image.bin.stripped: OK
+
+check metadata insertion and extraction:
+
+  $ cp "$ARTIFACTS/metadata.json" .
+  $ dd if=/dev/urandom of=image.bin bs=512k count=1 2> /dev/null
+  $ md5sum image.bin metadata.json > md5sums
+
+  $ fwtool -I metadata.json image.bin
+  $ strings image.bin | grep metadata_version > metadata.json.extracted
+  $ diff --unified "$ARTIFACTS/metadata.json" metadata.json.extracted
+  $ rm metadata.json
+
+  $ fwtool -t -i metadata.json image.bin
+  $ md5sum --check md5sums
+  image.bin: OK
+  metadata.json: OK
+
+check signature insertion and extraction:
+
+  $ cp "$ARTIFACTS/key-build.ucert" .
+  $ dd if=/dev/urandom of=image.bin bs=512k count=1 2> /dev/null
+  $ md5sum image.bin key-build.ucert > md5sums
+
+  $ fwtool -S key-build.ucert image.bin
+  $ tail --bytes 532 image.bin | head --bytes 516 > key-build.ucert.extracted
+  $ cmp --print-bytes "$ARTIFACTS/key-build.ucert" key-build.ucert.extracted
+  $ rm key-build.ucert
+
+  $ fwtool -t -s key-build.ucert image.bin
+  $ md5sum --check md5sums
+  image.bin: OK
+  key-build.ucert: OK
+
+check both signature and metadata insertion and extraction:
+
+  $ cp "$ARTIFACTS/metadata.json" "$ARTIFACTS/key-build.ucert" .
+  $ dd if=/dev/urandom of=image.bin bs=512k count=1 2> /dev/null
+  $ md5sum image.bin metadata.json key-build.ucert > md5sums
+
+  $ fwtool -I metadata.json image.bin
+  $ fwtool -S key-build.ucert image.bin
+
+  $ strings image.bin | grep metadata_version > metadata.json.extracted
+  $ diff -u "$ARTIFACTS/metadata.json" metadata.json.extracted
+  $ rm metadata.json
+
+  $ tail --bytes 532 image.bin | head --bytes 516 > key-build.ucert.extracted
+  $ cmp -b "$ARTIFACTS/key-build.ucert" key-build.ucert.extracted
+  $ rm key-build.ucert
+
+  $ fwtool -t -s key-build.ucert image.bin
+  $ fwtool -t -i metadata.json image.bin
+  $ md5sum --check md5sums
+  image.bin: OK
+  metadata.json: OK
+  key-build.ucert: OK
diff --git a/tests/test-crc32.c b/tests/test-crc32.c
new file mode 100644 (file)
index 0000000..2e0b06e
--- /dev/null
@@ -0,0 +1,26 @@
+#include <stdio.h>
+
+#include "crc32.h"
+#include "utils.h"
+
+static uint32_t crc_table[256];
+
+int main()
+{
+       uint32_t crc32 = ~0;
+       uint8_t all_ones[1<<10] = { 1 };
+       uint8_t all_ffs[1<<5] = { 0xff };
+       uint8_t all_7fs[1<<15] = { 0x7f };
+       uint8_t all_zeroes[1<<8] = { 0 };
+       uint8_t quick_fox[] = "The quick brown fox jumps over the lazy dog and then walks away.";
+
+       crc32_filltable(crc_table);
+
+       fprintf(stdout, "all_ffs=0x%x\n", cpu_to_be32(crc32_block(crc32, all_ffs, sizeof(all_ffs), crc_table)));
+       fprintf(stdout, "all_7fs=0x%x\n", cpu_to_be32(crc32_block(crc32, all_7fs, sizeof(all_7fs), crc_table)));
+       fprintf(stdout, "all_ones=0x%x\n", cpu_to_be32(crc32_block(crc32, all_ones, sizeof(all_ones), crc_table)));
+       fprintf(stdout, "all_zeroes=0x%x\n", cpu_to_be32(crc32_block(crc32, all_zeroes, sizeof(all_zeroes), crc_table)));
+       fprintf(stdout, "quick_fox=0x%x\n", cpu_to_be32(crc32_block(crc32, quick_fox, sizeof(quick_fox), crc_table)));
+
+       return 0;
+}