diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..d353ef0 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,52 @@ + +name: Continuous testing + +on: + push: + pull_request: + +jobs: + tests: + name: Run test environment + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: + - ubuntu-latest + - ubuntu-18.04 + + steps: + - uses: actions/checkout@v2 + + - name: Install essential + run: | + sudo apt-get update + sudo apt-get install build-essential + + - name: generate a makefile and use it to install more packages + run: | + ./autogen.sh + ./configure + make build-dep + + - name: Run the real configure step + run: | + CFLAGS="-fprofile-arcs -ftest-coverage" LDFLAGS="--coverage" ./configure --with-zstd + + - name: Run embedded tests + run: make test + + - name: Generate a coverage report + run: | + make cover + make gcov + + - name: Upload gcovr report artifact + uses: actions/upload-artifact@v2 + with: + name: code-coverage-report + path: coverage + + - name: Upload data to codecov + uses: codecov/codecov-action@v2 diff --git a/.gitignore b/.gitignore index e9a6b16..7845108 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ example_sn_embed supernode tools/n2n-benchmark tools/n2n-decode +tools/n2n-keygen build .idea cmake-build-default @@ -27,3 +28,19 @@ packages/etc/systemd/system/supernode.service *dSYM* cmake-build-*/ + +# Binaries built to run tests +tools/tests-compress +tools/tests-elliptic +tools/tests-hashing +tools/tests-transform +tools/tests-wire + +# Files generated while running tests +tests/*.out + +# Files generated while running coverage reports +*.gcno +*.gcda +*.gcov +coverage/ diff --git a/CMakeLists.txt b/CMakeLists.txt index bd63b74..31ca8c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,6 +120,7 @@ add_library(n2n STATIC src/edge_utils.c src/sn_utils.c src/wire.c + src/hexdump.c src/minilzo.c src/tf.c src/cc20.c @@ -201,6 +202,17 @@ target_link_libraries(n2n-benchmark n2n) add_executable(n2n-keygen tools/n2n-keygen.c) target_link_libraries(n2n-keygen n2n) +add_executable(tests-compress tools/tests-compress.c) +target_link_libraries(tests-compress n2n) +add_executable(tests-elliptic tools/tests-elliptic.c) +target_link_libraries(tests-elliptic n2n) +add_executable(tests-hashing tools/tests-hashing.c) +target_link_libraries(tests-hashing n2n) +add_executable(tests-transform tools/tests-transform.c) +target_link_libraries(tests-transform n2n) +add_executable(tests-wire tools/tests-wire.c) +target_link_libraries(tests-wire n2n) + if(N2N_OPTION_USE_PCAPLIB) find_library(PCAP_LIB pcap) if(PCAP_LIB) @@ -253,4 +265,10 @@ install(FILES ${PROJECT_BINARY_DIR}/doc/supernode.1.gz DESTINATION /usr/share/man/man1) install(FILES ${PROJECT_BINARY_DIR}/doc/n2n.7.gz DESTINATION /usr/share/man/man7) + +# TODO: +# - Add the right dependancy so that the tests binaries get built first +enable_testing() +add_test(tests ${PROJECT_SOURCE_DIR}/tools/test_harness ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/tests) + endif(DEFINED UNIX) diff --git a/Makefile.in b/Makefile.in index df60bb0..af1d8a7 100644 --- a/Makefile.in +++ b/Makefile.in @@ -2,6 +2,7 @@ # NOTE: these are needed by the configure.in inside the packages folder N2N_VERSION_SHORT=@N2N_VERSION_SHORT@ GIT_COMMITS=@GIT_COMMITS@ +GIT_DESCRIBE=$(shell git describe --always --dirty) ######## @@ -65,7 +66,7 @@ APPS+=example_sn_embed DOCS=edge.8.gz supernode.1.gz n2n.7.gz -.PHONY: steps build push all clean install tools +.PHONY: steps build push all clean install tools test cover gcov build-dep all: $(APPS) $(DOCS) tools tools: $(N2N_LIB) @@ -93,9 +94,33 @@ $(N2N_LIB): $(N2N_OBJS) $(AR) rcs $(N2N_LIB) $(N2N_OBJS) # $(RANLIB) $@ +test: tools + tools/test_harness + +# To generate coverage information, run configure with +# CFLAGS="-fprofile-arcs -ftest-coverage" LDFLAGS="--coverage" +# and run the desired tests. Ensure that package gcovr is installed +# and then run "make cover" +cover: + mkdir -p coverage + gcovr -s --html --html-details --output=coverage/index.html + +# Use coverage data to generate gcov text report files. +# Unfortunately, these end up in the wrong directory due to the +# makefile layout +# The steps to use this are similar to the "make cover" above +gcov: + gcov $(N2N_OBJS) + $(MAKE) -C tools gcov + +# This is the superset of all packages that might be needed. +# It is a convinent target to use during development or from a CI/CD system +build-dep: + sudo apt install build-essential autoconf libcap-dev libzstd-dev gcovr + clean: - rm -rf $(N2N_OBJS) $(N2N_LIB) $(APPS) $(DOCS) test *.dSYM *~ - -rm src/*.gcno src/*.gcda + rm -rf $(N2N_OBJS) $(N2N_LIB) $(APPS) $(DOCS) coverage/ *.dSYM *~ + rm -f tests/*.out src/*.gcno src/*.gcda $(MAKE) -C tools clean install: edge supernode edge.8.gz supernode.1.gz n2n.7.gz diff --git a/include/hexdump.h b/include/hexdump.h new file mode 100644 index 0000000..4480319 --- /dev/null +++ b/include/hexdump.h @@ -0,0 +1,6 @@ +#ifndef HEXDUMP_H +#define HEXDUMP_H + +void fhexdump(unsigned int display_addr, void *in, int size, FILE *stream); + +#endif diff --git a/src/hexdump.c b/src/hexdump.c new file mode 100644 index 0000000..04846ba --- /dev/null +++ b/src/hexdump.c @@ -0,0 +1,43 @@ + +#include + +#include "n2n.h" +#include "hexdump.h" + +void fhexdump(unsigned int display_addr, void *in, int size, FILE *stream) { + uint8_t *p = in; + + while(size>0) { + fprintf(stream, "%03x: ", display_addr); + + for (int i = 0; i < 16; i++) { + if (i < size) { + fprintf(stream, "%02x", p[i]); + } else { + fprintf(stream, " "); + } + if (i==7) { + fprintf(stream, " "); + } else { + fprintf(stream, " "); + } + } + fprintf(stream, " |"); + + for (int i = 0; i < 16; i++) { + if (i < size) { + char ch = p[i]; + if (ch>=0x20 && ch<=0x7e) { + fprintf(stream, "%c", ch); + } else { + fprintf(stream, " "); + } + } + } + fprintf(stream, "|\n"); + + size -= 16; + display_addr += 16; + p += 16; + } +} diff --git a/tests/tests-compress.expected b/tests/tests-compress.expected new file mode 100644 index 0000000..6360547 --- /dev/null +++ b/tests/tests-compress.expected @@ -0,0 +1,44 @@ +original: input size = 0x200 +000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +010: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +020: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +030: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +040: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +050: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +060: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +070: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +080: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +090: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0a0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0b0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0c0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0d0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0e0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0f0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +100: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +110: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +120: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +130: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +140: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +150: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +160: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +170: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +180: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +190: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1a0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1b0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1c0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1d0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1e0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1f0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | + +lzo1x: output size = 0x2f +000: 0d 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e | | +010: 0f 20 00 bc 3c 00 00 02 0c 0d 0e 0f 00 01 02 03 | < | +020: 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 11 00 00 | | + +zstd: output size = 0x21 +000: 28 b5 2f fd 60 00 01 bd 00 00 80 00 01 02 03 04 |( / ` | +010: 05 06 07 08 09 0a 0b 0c 0d 0e 0f 01 00 da 47 9d | G | +020: 4b |K| + diff --git a/tests/tests-elliptic.expected b/tests/tests-elliptic.expected new file mode 100644 index 0000000..c60ce1b --- /dev/null +++ b/tests/tests-elliptic.expected @@ -0,0 +1,11 @@ +environment: input +000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | | +010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 09 | | +environment: key +000: 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 |UUUUUUUUUUUUUUUU| +010: 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 |UUUUUUUUUUUUUUUU| + +curve25519: output +000: 7f 42 1b f9 34 5a 59 84 4a 30 bc 53 64 74 fa 7c | B 4ZY J0 Sdt || +010: 15 81 77 a4 4d 34 6d 2f 8b c1 8c 05 d6 a9 44 54 | w M4m/ DT| + diff --git a/tests/tests-hashing.expected b/tests/tests-hashing.expected new file mode 100644 index 0000000..1f29fa9 --- /dev/null +++ b/tests/tests-hashing.expected @@ -0,0 +1,36 @@ +environment: input size = 0x200 +000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +010: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +020: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +030: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +040: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +050: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +060: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +070: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +080: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +090: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0a0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0b0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0c0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0d0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0e0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0f0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +100: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +110: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +120: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +130: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +140: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +150: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +160: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +170: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +180: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +190: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1a0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1b0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1c0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1d0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1e0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1f0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | + +pearson: output = 0xb2d98fa82ea108be + diff --git a/tests/tests-transform.expected b/tests/tests-transform.expected new file mode 100644 index 0000000..14276cf --- /dev/null +++ b/tests/tests-transform.expected @@ -0,0 +1,225 @@ +environment: community_name = "abc123def456" +environment: encrypt_key = "SoMEVer!S$cUREPassWORD" +environment: input size = 0x200 +000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +010: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +020: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +030: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +040: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +050: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +060: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +070: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +080: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +090: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0a0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0b0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0c0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0d0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0e0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +0f0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +100: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +110: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +120: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +130: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +140: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +150: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +160: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +170: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +180: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +190: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1a0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1b0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1c0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1d0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1e0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | +1f0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | | + +null: output size = 0x226 +000: 03 02 00 03 61 62 63 31 32 33 64 65 66 34 35 36 | abc123def456| +010: 00 00 00 00 00 00 00 00 00 01 02 03 04 05 00 01 | | +020: 02 03 04 05 00 00 00 01 02 03 04 05 06 07 08 09 | | +030: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +040: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +050: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +060: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +070: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +080: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +090: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +0a0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +0b0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +0c0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +0d0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +0e0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +0f0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +100: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +110: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +120: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +130: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +140: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +150: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +160: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +170: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +180: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +190: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +1a0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +1b0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +1c0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +1d0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +1e0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +1f0: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +200: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +210: 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07 08 09 | | +220: 0a 0b 0c 0d 0e 0f | | + +tf: output size = 0x236 +000: 03 02 00 03 61 62 63 31 32 33 64 65 66 34 35 36 | abc123def456| +010: 00 00 00 00 00 00 00 00 00 01 02 03 04 05 00 01 | | +020: 02 03 04 05 00 00 69 54 ba b7 b9 00 7e 1f 4e 43 | iT ~ NC| +030: 6b 6e c0 b7 5a bb e1 6a 7d 8b f6 41 9e fb 7e c1 |kn Z j} A ~ | +040: a8 6c 67 6b c7 27 17 32 9b 89 f2 5d 1d 67 49 de | lgk ' 2 ] gI | +050: ab 5c d4 b7 a1 97 99 76 7d 90 8d 2c 7c 0d 65 66 | \ v} ,| ef| +060: d5 7f 1a 3b bf 0c 52 b3 42 0d c8 c8 0d 62 8e 4b | ; R B b K| +070: 98 7b bc 0d 9c db bf 61 dc 9d 9f 44 a4 fc 8d 1a | { a D | +080: 70 f0 14 87 89 0e 4a b8 7e 66 72 7a 04 6e 9b 17 |p J ~frz n | +090: e1 2c 06 ce 52 04 2a a0 0f 7f 76 f4 5c c5 e2 09 | , R * v \ | +0a0: f6 35 ff ad 87 ab 72 5a 6f bc 61 78 f6 3c 48 e2 | 5 rZo ax | +1a0: a7 fc d2 83 5b 61 b1 e0 fd 28 d1 ec 65 a2 cf 6c | [a ( e l| +1b0: ef 3b ad cc 75 e3 f9 71 0f 90 71 a6 bc 1a d5 17 | ; u q q | +1c0: 65 64 3e 0c d2 c8 de bd 1f d5 af 84 fc fe aa bd |ed> | +1d0: c5 88 13 af 09 ee 8c c0 38 49 79 09 a7 7a 01 48 | 8Iy z H| +1e0: 2e 3e 9a 38 1b c6 b8 c0 a9 4e 61 0f 19 2a 95 84 |.> 8 Na * | +1f0: 3b 53 1c db 9a ec af 8f 2d af 73 d5 cc 71 bd 42 |;S - s q B| +200: 4f e2 70 ca 45 b6 44 18 54 fe 6b 23 31 ba f4 b1 |O p E D T k#1 | +210: 02 a1 26 4f f1 a9 c0 78 e6 3b 11 9e d6 3c 61 e5 | &O x ; e 9 | +150: c2 4c 3d 3e 73 a7 15 fc 28 6c a6 e5 8b 16 bd c9 | L=>s (l | +160: e2 c9 5e d9 64 8e bc f5 92 a4 e5 74 04 cb 9c 90 | ^ d t | +170: 0c 10 28 5f 30 10 61 b7 44 50 b1 f9 3e 21 a1 41 | (_0 a DP >! A| +180: c2 e2 a5 e9 f7 33 16 52 32 61 d1 a3 c5 0b 61 d4 | 3 R2a a | +190: 53 0f 65 a8 d9 e1 fb e1 9b 1b 61 16 d1 75 0a 4b |S e a u K| +1a0: 9a 9f d1 f3 4e f6 ca b2 a2 24 50 8b 10 4d 45 54 | N $P MET| +1b0: 3d e1 c2 8f 07 52 67 52 7e d1 7f 99 3c 4e de eb |= RgR~ g|[2 6. | +080: 74 50 9f a3 de 38 4e c7 ca 6b 35 7c 1b 4e 39 ea |tP 8N k5| N9 | +090: 5f e2 8a 80 d9 58 6e 14 32 bf 8e e9 a2 b0 19 8a |_ Xn 2 | +0a0: e1 1d da fa 3f 4a b7 1c 03 e0 e3 17 85 54 84 c8 | ?J T | +0b0: 3e 90 8e 85 5b e7 12 3d 73 9e b9 ef 7e 48 c0 55 |> [ =s ~H U| +0c0: 59 28 29 f4 d1 12 62 b3 b3 db 65 0a 6c 8c 44 be |Y() b e l D | +0d0: e2 76 49 e6 b6 4a 9e 7e 9c 49 c9 10 d3 dc 85 33 | vI J ~ I 3| +0e0: fe eb a5 dc 5e 18 cf dc 9a 99 da bc 5c 9f c1 ff | ^ \ | +0f0: 9c 92 3b a9 9d d6 5d 03 f4 f3 5e a9 52 21 d2 d0 | ; ] ^ R! | +100: 8b 4e 8a b6 06 af 4e 34 98 e2 bc 9b c2 f1 9d 72 | N N4 r| +110: f8 0f f3 d5 83 34 7d 47 fe bf 6c 1d c2 d3 89 a4 | 4}G l | +120: ff 5b 76 3c cb 9b 4d 09 a6 3a a2 2f 0b 8d 7a 34 | [v< M : / z4| +130: 12 d5 73 c4 bc ba 13 76 e7 69 50 6d 50 ab 76 b5 | s v iPmP v | +140: a6 fb b4 fb c7 98 3d ac ce e1 e1 98 97 9f 24 98 | = $ | +150: 90 82 61 00 38 57 b0 36 7d 55 a7 70 9c ee 51 26 | a 8W 6}U p Q&| +160: 47 02 4f b5 fe cc 2c e0 07 c6 7b 04 6d a1 89 dc |G O , { m | +170: e8 98 71 fd 27 54 d1 f1 2d 0b 3e 64 ef 02 74 71 | q 'T - >d tq| +180: db f5 b1 84 87 6d c9 37 c5 c3 3a de ea 1b 53 d3 | m 7 : S | +190: f1 ed 8f 24 2d 74 a3 77 23 20 9b 75 c3 f2 ef 4a | $-t w# u J| +1a0: 75 ec d3 86 59 11 c5 fc b0 ed 79 b0 8e a8 03 c8 |u Y y | +1b0: 3e 05 db b0 65 62 53 e5 ef e7 95 88 ce 62 89 7a |> ebS b z| +1c0: 9d 3c bc ea a4 3a f6 df 1d 10 8a a3 80 7c 3b 80 | < : |; | +1d0: be 0a 27 76 2b bc 7d 02 98 bf b7 5e 4a 4c 53 df | 'v+ } ^JLS | +1e0: 57 ff 67 7e 33 6a 00 4c ff d6 c0 ff 3f 1c 24 f7 |W g~3j L ? $ | +1f0: fb fd 3b d2 4f 18 e3 9d 62 5b 9b 15 68 13 44 d8 | ; O b[ h D | +200: 71 79 9d b1 ab 2f bd f3 44 6b 77 96 b8 44 bc 90 |qy / Dkw D | +210: 1f 74 db d2 73 7d 5d 44 f7 a6 92 4e fa 24 e3 92 | t s}]D N $ | +220: c8 34 c7 1d 16 8b f5 80 d9 15 48 24 12 16 14 76 | 4 H$ v| +230: 15 5f d0 dc 1e 9c | _ | + +speck: output size = 0x236 +000: 03 02 00 03 61 62 63 31 32 33 64 65 66 34 35 36 | abc123def456| +010: 00 00 00 00 00 00 00 00 00 01 02 03 04 05 00 01 | | +020: 02 03 04 05 00 00 69 c2 17 4a ab 55 f3 8d 44 17 | i J U D | +030: 2c 31 09 b5 b0 2f 0c 87 f2 05 13 59 34 49 72 45 |,1 / Y4IrE| +040: 2c 79 51 cf 39 f5 ce 0e b9 fc c8 41 31 08 e9 a5 |,yQ 9 A1 | +050: 54 3c aa 77 aa 29 10 41 4a 16 4b e9 89 8f 92 7b |T< w ) AJ K {| +060: 29 b3 0b 4b 84 92 2a 3a fe bd c0 50 75 fc a4 7e |) K *: Pu ~| +070: 4e 20 2e 3b 53 eb d1 37 43 35 b6 5e 55 a6 5e 0e |N .;S 7C5 ^U ^ | +080: e3 05 db 07 5f a8 74 4d 9f 41 ed 5f 44 93 98 9b | _ tM A _D | +090: fb 48 3c 9c 36 8b a6 71 ed ee f4 e6 10 21 b1 b5 | H< 6 q ! | +0a0: f4 b7 28 db d5 5d 43 3d 4b 8d 1a 33 cf c5 54 09 | ( ]C=K 3 T | +0b0: b6 9d f5 f2 27 2f e7 be dd 4c 3c 5c c8 71 ca 71 | '/ L<\ q q| +0c0: 8c 47 37 21 6c ad 3f d6 9a 99 ab 18 fc d8 1b c7 | G7!l ? | +0d0: 72 c2 7d fb 57 8c 97 4f 77 4c 6c 94 4a ab f4 94 |r } W OwLl J | +0e0: d8 e3 02 e2 b9 bf 12 47 fb 53 aa b8 4a 93 38 c6 | G S J 8 | +0f0: bb 8b 27 8b d6 db f3 e4 e3 43 7e 7f 83 cf c9 df | ' C~ | +100: e5 71 b9 80 9c ad 38 e5 10 aa 99 ad e1 0d 34 6f | q 8 4o| +110: de 7e ff 29 cc fe b5 89 49 a9 a1 b5 9e 9f be 5e | ~ ) I ^| +120: e6 d2 74 2c 57 91 86 40 02 a0 7b 6b 42 6e b0 63 | t,W @ {kBn c| +130: 0e 04 19 7c f0 e5 ff 3d db de e3 c5 fc 70 18 7d | | = p }| +140: 06 33 7e 6a bb 46 3b 94 28 85 87 51 9f 6e 95 22 | 3~j F; ( Q n "| +150: 99 b4 34 bd 29 94 3a a8 a4 ff 5b 19 53 69 cb e5 | 4 ) : [ Si | +160: a6 0c 41 c5 22 89 82 a7 a5 f3 f3 49 ed 5d ce f3 | A " I ] | +170: a7 ee 77 dd a9 aa 26 5b 85 ec b1 6e f4 33 a0 b8 | w &[ n 3 | +180: 93 2a 80 6f 3f 5f 0a ff 1b 72 14 5d 4f 1a cc 74 | * o?_ r ]O t| +190: 69 01 da 81 7d 89 4b 0f 68 fe c6 c5 ae 39 86 1d |i } K h 9 | +1a0: ab e3 c7 35 2e 5d a8 3d 56 3e 26 52 74 72 5b f2 | 5.] =V>&Rtr[ | +1b0: 41 1a 7a 04 d9 d0 65 fe 92 c4 b9 be 75 e5 9e e1 |A z e u | +1c0: 8e 52 f4 27 98 44 61 26 7f 6b 96 0c c4 6a a6 6b | R ' Da& k j k| +1d0: 36 66 81 a1 f6 dd ab 2a a7 63 e5 7f 63 67 79 08 |6f * c cgy | +1e0: ba 7b bb 11 12 9c 14 b2 a4 2b 56 66 14 c1 54 c6 | { +Vf T | +1f0: 96 f0 e4 68 8a 5c 11 b6 27 af 61 ef ab 47 9e 7f | h \ ' a G | +200: 76 0e 39 c3 fb 88 94 29 7c 9e 96 9b e5 e1 6b ae |v 9 )| k | +210: 87 03 a4 86 a2 1f 91 cf 90 1c 11 08 57 bc c7 90 | W | +220: 0b c1 51 2e 28 a6 58 96 e2 e7 f2 20 c6 ac 06 05 | Q.( X | +230: 39 75 4a 56 cf f8 |9uJV | + diff --git a/tests/tests-wire.expected b/tests/tests-wire.expected new file mode 100644 index 0000000..4624103 --- /dev/null +++ b/tests/tests-wire.expected @@ -0,0 +1,17 @@ +environment: common.ttl = 2 +environment: common.flags = 0 +environment: common.community = "abc123def456z" + +REGISTER: common.pc = 1 +REGISTER: reg.cookie = 0 +REGISTER: reg.dev_addr.net_addr = 0x20212223 +REGISTER: reg.dev_addr.net_bitlen = 25 +REGISTER: reg.dev_desc = "Dummy_Dev_Desc" + +REGISTER: output retval = 0x24 +REGISTER: output idx = 0x3d +000: 03 02 00 01 61 62 63 31 32 33 64 65 66 34 35 36 | abc123def456| +010: 7a 00 00 00 00 00 00 00 00 00 00 00 00 01 02 03 |z | +020: 04 05 10 11 12 13 14 15 20 21 22 23 19 44 75 6d | !"# Dum| +030: 6d 79 5f 44 65 76 5f 44 65 73 63 00 00 |my_Dev_Desc | + diff --git a/tools/Makefile.in b/tools/Makefile.in index 78a5d2d..e5c3164 100644 --- a/tools/Makefile.in +++ b/tools/Makefile.in @@ -27,9 +27,11 @@ N2N_LIB=../libn2n.a TOOLS=n2n-benchmark n2n-keygen TOOLS+=@ADDITIONAL_TOOLS@ -.PHONY: all clean install -all: $(TOOLS) +TESTS=tests-compress tests-elliptic tests-hashing tests-transform +TESTS+=tests-wire +.PHONY: all clean install +all: $(TOOLS) $(TESTS) n2n-benchmark.o: $(N2N_LIB) $(HEADERS) ../Makefile Makefile n2n-keygen.o: $(N2N_LIB) $(HEADERS) ../Makefile Makefile @@ -37,8 +39,14 @@ n2n-keygen.o: $(N2N_LIB) $(HEADERS) ../Makefile Makefile n2n-decode: n2n-decode.c $(N2N_LIB) $(HEADERS) ../Makefile Makefile $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDLIBS) -lpcap -o $@ +# See comments in the topdir Makefile about how to generate coverage +# data. +gcov: + gcov $(TOOLS) $(TESTS) + clean: rm -rf $(TOOLS) *.o *.dSYM *~ + rm -f $(TESTS) *.gcno *.gcda install: $(TOOLS) $(INSTALL_PROG) $(TOOLS) $(SBINDIR)/ diff --git a/tools/test_harness b/tools/test_harness new file mode 100755 index 0000000..dcacef1 --- /dev/null +++ b/tools/test_harness @@ -0,0 +1,39 @@ +#!/bin/sh +# +# This expects to find the tests in the tools dir and the expected results +# in the tests dir. + +TESTS=" + tests-compress + tests-elliptic + tests-hashing + tests-transform + tests-wire +" + +TOOLSDIR=tools +TESTDATA=tests + +# Allow both dirs be overidden +[ -n "$1" ] && TOOLSDIR="$1" +[ -n "$2" ] && TESTDATA="$2" + +# Confirm we have all the tools and data +for i in $TESTS; do + if [ ! -e $TOOLSDIR/$i ]; then + echo "Could not find test $TOOLSDIR/$i" + exit 1 + fi + if [ ! -e $TESTDATA/$i.expected ]; then + echo "Could not find testdata $TESTDATA/$i.expected" + exit 1 + fi +done + +# Actually run the tests +set -e +for i in $TESTS; do + echo "$TOOLSDIR/$i >$TESTDATA/$i.out" + $TOOLSDIR/$i >$TESTDATA/$i.out + cmp $TESTDATA/$i.expected $TESTDATA/$i.out +done diff --git a/tools/tests-compress.c b/tools/tests-compress.c new file mode 100644 index 0000000..76f310e --- /dev/null +++ b/tools/tests-compress.c @@ -0,0 +1,162 @@ +/* + * (C) 2007-21 - ntop.org and contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see + * + */ + +#include + +#include "n2n.h" +#include "hexdump.h" + +/* heap allocation for compression as per lzo example doc */ +#define HEAP_ALLOC(var,size) lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ] +static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS); + + +uint8_t PKT_CONTENT[]={ + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }; + +static void init_compression_for_benchmark(void) { + + if(lzo_init() != LZO_E_OK) { + traceEvent(TRACE_ERROR, "LZO compression init error"); + exit(1); + } + +#ifdef N2N_HAVE_ZSTD + // zstd does not require initialization. if it were required, this would be a good place +#endif +} + + +static void deinit_compression_for_benchmark(void) { + + // lzo1x does not require de-initialization. if it were required, this would be a good place + +#ifdef N2N_HAVE_ZSTD + // zstd does not require de-initialization. if it were required, this would be a good place +#endif +} + +void test_lzo1x() { + char *test_name = "lzo1x"; + uint8_t compression_buffer[N2N_PKT_BUF_SIZE]; // size allows enough of a reserve required for compression + lzo_uint compression_len = sizeof(compression_buffer); + + if (lzo1x_1_compress(PKT_CONTENT, sizeof(PKT_CONTENT), compression_buffer, &compression_len, wrkmem) != LZO_E_OK) { + fprintf(stderr, "%s: compression error\n", test_name); + exit(1); + } + + assert(compression_len == 47); + + printf("%s: output size = 0x%lx\n", test_name, compression_len); + fhexdump(0, compression_buffer, compression_len, stdout); + + uint8_t deflation_buffer[N2N_PKT_BUF_SIZE]; + lzo_uint deflated_len; + lzo1x_decompress (compression_buffer, compression_len, deflation_buffer, &deflated_len, NULL); + + assert(deflated_len == sizeof(PKT_CONTENT)); + if (memcmp(PKT_CONTENT, deflation_buffer, deflated_len)!=0) { + fprintf(stderr, "%s: round-trip buffer mismatch\n", test_name); + exit(1); + } + + fprintf(stderr, "%s: tested\n", test_name); + printf("\n"); +} + +void test_zstd() { + char *test_name = "zstd"; + +#ifdef N2N_HAVE_ZSTD + uint8_t compression_buffer[N2N_PKT_BUF_SIZE]; // size allows enough of a reserve required for compression + lzo_uint compression_len = sizeof(compression_buffer); + + compression_len = N2N_PKT_BUF_SIZE; + compression_len = ZSTD_compress(compression_buffer, compression_len, PKT_CONTENT, sizeof(PKT_CONTENT), ZSTD_COMPRESSION_LEVEL) ; + if(ZSTD_isError(compression_len)) { + fprintf(stderr, "%s: compression error\n", test_name); + exit(1); + } + + assert(compression_len == 33); + + printf("%s: output size = 0x%lx\n", test_name, compression_len); + fhexdump(0, compression_buffer, compression_len, stdout); + + uint8_t deflation_buffer[N2N_PKT_BUF_SIZE]; + int64_t deflated_len = sizeof(deflation_buffer); + deflated_len = (int32_t)ZSTD_decompress (deflation_buffer, deflated_len, compression_buffer, compression_len); + if(ZSTD_isError(deflated_len)) { + fprintf(stderr, "%s: decompression error '%s'\n", + test_name, ZSTD_getErrorName(deflated_len)); + exit(1); + } + + assert(deflated_len == sizeof(PKT_CONTENT)); + if (memcmp(PKT_CONTENT, deflation_buffer, deflated_len)!=0) { + fprintf(stderr, "%s: round-trip buffer mismatch\n", test_name); + exit(1); + } + + fprintf(stderr, "%s: tested\n", test_name); +#else + // FIXME - output dummy data to the stdout for easy comparison + printf("zstd: output size = 0x21\n"); + printf("000: 28 b5 2f fd 60 00 01 bd 00 00 80 00 01 02 03 04 |( / ` |\n"); + printf("010: 05 06 07 08 09 0a 0b 0c 0d 0e 0f 01 00 da 47 9d | G |\n"); + printf("020: 4b |K|\n"); + + fprintf(stderr, "%s: not compiled - dummy data output\n", test_name); +#endif + printf("\n"); +} + + +int main(int argc, char * argv[]) { + + /* Also for compression (init moved here for ciphers get run before in case of lzo init error) */ + init_compression_for_benchmark(); + + printf("%s: input size = 0x%lx\n", "original", sizeof(PKT_CONTENT)); + fhexdump(0, PKT_CONTENT, sizeof(PKT_CONTENT), stdout); + printf("\n"); + + test_lzo1x(); + test_zstd(); + + deinit_compression_for_benchmark(); + + return 0; +} + diff --git a/tools/tests-elliptic.c b/tools/tests-elliptic.c new file mode 100644 index 0000000..cf65efd --- /dev/null +++ b/tools/tests-elliptic.c @@ -0,0 +1,56 @@ +/* + * (C) 2007-21 - ntop.org and contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see + * + */ + +#include "n2n.h" +#include "hexdump.h" + +void test_curve25519(unsigned char *pkt_input, unsigned char *key) { + char *test_name = "curve25519"; + unsigned char pkt_output[32]; + + curve25519(pkt_output, key, pkt_input); + + printf("%s: output\n", test_name); + fhexdump(0, pkt_output, sizeof(pkt_output), stdout); + + fprintf(stderr, "%s: tested\n", test_name); + printf("\n"); +} + +int main(int argc, char * argv[]) { + char *test_name = "environment"; + + unsigned char key[32]; + unsigned char pkt_input[32]; + + memset(pkt_input, 0, 31); + pkt_input[31] = 9; + + memset(key, 0x55, 32); + + printf("%s: input\n", test_name); + fhexdump(0, pkt_input, sizeof(pkt_input), stdout); + printf("%s: key\n", test_name); + fhexdump(0, key, sizeof(key), stdout); + printf("\n"); + + test_curve25519(pkt_input, key); + + return 0; +} + diff --git a/tools/tests-hashing.c b/tools/tests-hashing.c new file mode 100644 index 0000000..366bf50 --- /dev/null +++ b/tools/tests-hashing.c @@ -0,0 +1,64 @@ +/* + * (C) 2007-21 - ntop.org and contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see + * + */ + +#include "n2n.h" +#include "hexdump.h" + + +uint8_t PKT_CONTENT[]={ + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }; + +void test_pearson(void *buf, unsigned int bufsize) { + char *test_name = "pearson"; + + uint64_t hash = pearson_hash_64(buf, bufsize); + + printf("%s: output = 0x%lx\n", test_name, hash); + + fprintf(stderr, "%s: tested\n", test_name); + printf("\n"); +} + +int main(int argc, char * argv[]) { + pearson_hash_init(); + + char *test_name = "environment"; + printf("%s: input size = 0x%lx\n", test_name, sizeof(PKT_CONTENT)); + fhexdump(0, PKT_CONTENT, sizeof(PKT_CONTENT), stdout); + printf("\n"); + + test_pearson(PKT_CONTENT, sizeof(PKT_CONTENT)); + + return 0; +} + diff --git a/tools/tests-transform.c b/tools/tests-transform.c new file mode 100644 index 0000000..af126a6 --- /dev/null +++ b/tools/tests-transform.c @@ -0,0 +1,161 @@ +/* + * (C) 2007-21 - ntop.org and contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see + * + */ + +#include "n2n.h" +#include "hexdump.h" + +#define DURATION 2.5 // test duration per algorithm +#define PACKETS_BEFORE_GETTIME 2047 // do not check time after every packet but after (2 ^ n - 1) + + +uint8_t PKT_CONTENT[]={ + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }; + +/* Prototypes */ +static ssize_t do_encode_packet( uint8_t * pktbuf, size_t bufsize, const n2n_community_t c ); +static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2n_edge_conf_t *conf, uint8_t *pktbuf); + + +int main(int argc, char * argv[]) { + uint8_t pktbuf[N2N_PKT_BUF_SIZE]; + n2n_trans_op_t transop_null, transop_tf; + n2n_trans_op_t transop_aes; + n2n_trans_op_t transop_cc20; + + n2n_trans_op_t transop_speck; + n2n_edge_conf_t conf; + + /* Init configuration */ + edge_init_conf_defaults(&conf); + strncpy((char *)conf.community_name, "abc123def456", sizeof(conf.community_name)); + conf.encrypt_key = "SoMEVer!S$cUREPassWORD"; + + char *test_name = "environment"; + printf("%s: community_name = \"%s\"\n", test_name, conf.community_name); + printf("%s: encrypt_key = \"%s\"\n", test_name, conf.encrypt_key); + printf("%s: input size = 0x%lx\n", test_name, sizeof(PKT_CONTENT)); + fhexdump(0, PKT_CONTENT, sizeof(PKT_CONTENT), stdout); + printf("\n"); + + /* Init transopts */ + n2n_transop_null_init(&conf, &transop_null); + n2n_transop_tf_init(&conf, &transop_tf); + n2n_transop_aes_init(&conf, &transop_aes); + n2n_transop_cc20_init(&conf, &transop_cc20); + n2n_transop_speck_init(&conf, &transop_speck); + + /* Run the tests */ + /* FIXME: interop tests are pretty useless without the expected encrypted buffer data */ + run_transop_benchmark("null", &transop_null, &conf, pktbuf); + run_transop_benchmark("tf", &transop_tf, &conf, pktbuf); + run_transop_benchmark("aes", &transop_aes, &conf, pktbuf); + run_transop_benchmark("cc20", &transop_cc20, &conf, pktbuf); + run_transop_benchmark("speck", &transop_speck, &conf, pktbuf); + + /* Cleanup */ + transop_null.deinit(&transop_null); + transop_tf.deinit(&transop_tf); + transop_aes.deinit(&transop_aes); + transop_cc20.deinit(&transop_cc20); + transop_speck.deinit(&transop_speck); + + return 0; +} + +// --- cipher benchmark ------------------------------------------------------------------- + +static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2n_edge_conf_t *conf, uint8_t *pktbuf) { + n2n_common_t cmn; + n2n_PACKET_t pkt; + n2n_mac_t mac_buf; + uint8_t decodebuf[N2N_PKT_BUF_SIZE]; + size_t idx; + size_t rem; + size_t nw; + + // encryption + memset(mac_buf, 0, sizeof(mac_buf)); + + nw = do_encode_packet( pktbuf, N2N_PKT_BUF_SIZE, conf->community_name); + nw += op_fn->fwd(op_fn, + pktbuf+nw, N2N_PKT_BUF_SIZE-nw, + PKT_CONTENT, sizeof(PKT_CONTENT), mac_buf); + + printf("%s: output size = 0x%lx\n", op_name, nw); + fhexdump(0, pktbuf, nw, stdout); + + // decrpytion + idx=0; + rem=nw; + decode_common( &cmn, pktbuf, &rem, &idx); + decode_PACKET( &pkt, &cmn, pktbuf, &rem, &idx ); + op_fn->rev(op_fn, decodebuf, sizeof(decodebuf), pktbuf+idx, rem, 0); + + if(memcmp(decodebuf, PKT_CONTENT, sizeof(PKT_CONTENT)) != 0) { + fprintf(stderr, "%s: round-trip buffer mismatch\n", op_name); + exit(1); + } + + fprintf(stderr, "%s: tested\n", op_name); + printf("\n"); +} + + +static ssize_t do_encode_packet( uint8_t * pktbuf, size_t bufsize, const n2n_community_t c ) +{ + // FIXME: this is a parameter of the test environment + n2n_mac_t destMac={0,1,2,3,4,5}; + + n2n_common_t cmn; + n2n_PACKET_t pkt; + size_t idx; + + + memset( &cmn, 0, sizeof(cmn) ); + cmn.ttl = N2N_DEFAULT_TTL; + cmn.pc = n2n_packet; + cmn.flags=0; /* no options, not from supernode, no socket */ + memcpy( cmn.community, c, N2N_COMMUNITY_SIZE ); + + memset( &pkt, 0, sizeof(pkt) ); + memcpy( pkt.srcMac, destMac, N2N_MAC_SIZE); + memcpy( pkt.dstMac, destMac, N2N_MAC_SIZE); + + pkt.sock.family=0; /* do not encode sock */ + + idx=0; + encode_PACKET( pktbuf, &idx, &cmn, &pkt ); + traceEvent( TRACE_DEBUG, "encoded PACKET header of size=%u", (unsigned int)idx ); + + return idx; +} diff --git a/tools/tests-wire.c b/tools/tests-wire.c new file mode 100644 index 0000000..ba02441 --- /dev/null +++ b/tools/tests-wire.c @@ -0,0 +1,82 @@ +/* + * (C) 2007-21 - ntop.org and contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see + * + */ + +#include "n2n.h" +#include "hexdump.h" + +void test_REGISTER(n2n_common_t *common) { + char *test_name = "REGISTER"; + + common->pc = n2n_register; + printf("%s: common.pc = %i\n", test_name, common->pc); + + n2n_REGISTER_t reg; + memset( ®, 0, sizeof(reg) ); + n2n_mac_t dummysrcMac={0,1,2,3,4,5}; + memcpy( reg.srcMac, dummysrcMac, sizeof(dummysrcMac)); + n2n_mac_t dummydstMac={0x10,0x11,0x12,0x13,0x14,0x15}; + memcpy( reg.dstMac, dummydstMac, sizeof(dummydstMac)); + reg.dev_addr.net_addr = 0x20212223; + reg.dev_addr.net_bitlen = 25; + strcpy( (char *)reg.dev_desc, "Dummy_Dev_Desc" ); + + printf("%s: reg.cookie = %i\n", test_name, reg.cookie); + // TODO: print reg.srcMac, reg.dstMac + // TODO: print reg.sock + printf("%s: reg.dev_addr.net_addr = 0x%08x\n", test_name, reg.dev_addr.net_addr); + printf("%s: reg.dev_addr.net_bitlen = %i\n", test_name, reg.dev_addr.net_bitlen); + printf("%s: reg.dev_desc = \"%s\"\n", test_name, reg.dev_desc); + printf("\n"); + + uint8_t pktbuf[N2N_PKT_BUF_SIZE]; + size_t idx = 0; + size_t retval = encode_REGISTER( pktbuf, &idx, common, ®); + + printf("%s: output retval = 0x%lx\n", test_name, retval); + printf("%s: output idx = 0x%lx\n", test_name, idx); + fhexdump(0, pktbuf, idx, stdout); + + // TODO: decode_REGISTER() and print + + fprintf(stderr, "%s: tested\n", test_name); + printf("\n"); +} + +int main(int argc, char * argv[]) { + char *test_name = "environment"; + + n2n_community_t c; + strncpy((char *)c, "abc123def456z", sizeof(c)); + + n2n_common_t common; + memset( &common, 0, sizeof(common) ); + common.ttl = N2N_DEFAULT_TTL; + common.flags = 0; + memcpy( common.community, c, N2N_COMMUNITY_SIZE ); + + printf("%s: common.ttl = %i\n", test_name, common.ttl); + printf("%s: common.flags = %i\n", test_name, common.flags); + printf("%s: common.community = \"%s\"\n", test_name, common.community); + printf("\n"); + + test_REGISTER(&common); + // TODO: add more wire tests + + return 0; +} +