diff options
Diffstat (limited to 'src/supplemental')
| -rw-r--r-- | src/supplemental/base64/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/supplemental/base64/base64_test.c | 106 | ||||
| -rw-r--r-- | src/supplemental/sha1/CMakeLists.txt | 10 | ||||
| -rw-r--r-- | src/supplemental/sha1/sha1_test.c | 72 |
4 files changed, 182 insertions, 7 deletions
diff --git a/src/supplemental/base64/CMakeLists.txt b/src/supplemental/base64/CMakeLists.txt index 83740c83..b4c92e6a 100644 --- a/src/supplemental/base64/CMakeLists.txt +++ b/src/supplemental/base64/CMakeLists.txt @@ -12,5 +12,6 @@ if (NNG_SUPP_BASE64) set(_SRCS supplemental/base64/base64.c supplemental/base64/base64.h) + nng_test(base64_test) set(NNG_SRCS ${NNG_SRCS} ${_SRCS} PARENT_SCOPE) endif() diff --git a/src/supplemental/base64/base64_test.c b/src/supplemental/base64/base64_test.c new file mode 100644 index 00000000..2c79a243 --- /dev/null +++ b/src/supplemental/base64/base64_test.c @@ -0,0 +1,106 @@ +// +// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include <string.h> + +#include <nng/nng.h> + +#include "base64.h" + +#include <acutest.h> + +typedef struct { + char *decoded; + char *encoded; +} test_case; + +static test_case cases[] = { + { "", "" }, + { "f", "Zg==" }, + { "fo", "Zm8=" }, + { "foo", "Zm9v" }, + { "foob", "Zm9vYg==" }, + { "fooba", "Zm9vYmE=" }, + { "foobar", "Zm9vYmFy" }, + { NULL, NULL }, +}; + +void +test_encode(void) +{ + int i; + void *dec; + + for (i = 0; (dec = cases[i].decoded) != NULL; i++) { + char buf[1024]; + char name[8]; + int rv; + + (void) snprintf(name, sizeof(name), "%d", i); + TEST_CASE(name); + rv = nni_base64_encode(dec, strlen(dec), buf, 1024); + TEST_CHECK(rv >= 0); + TEST_CHECK(rv == (int) strlen(cases[i].encoded)); + buf[rv] = 0; + TEST_CHECK(strcmp(buf, cases[i].encoded) == 0); + } +} + +void +test_decode(void) +{ + int i; + void *enc; + + for (i = 0; (enc = cases[i].encoded) != NULL; i++) { + char buf[1024]; + char name[8]; + int rv; + + (void) snprintf(name, sizeof(name), "%d", i); + TEST_CASE(name); + + rv = nni_base64_decode(enc, strlen(enc), (void *) buf, 1024); + TEST_CHECK(rv >= 0); + TEST_CHECK(rv == (int) strlen(cases[i].decoded)); + buf[rv] = 0; + TEST_CHECK(strcmp(buf, cases[i].decoded) == 0); + } +} + +void +test_overflow(void) +{ + char tmp[1024]; + for (int i = 1; cases[i].decoded != NULL; i++) { + void *enc = cases[i].encoded; + void *dec = cases[i].decoded; + void *buf = tmp; + + char name[8]; + (void) snprintf(name, sizeof(name), "%d", i); + TEST_CASE(name); + + TEST_CHECK(nni_base64_encode( + dec, strlen(dec), buf, strlen(enc) - 1) == -1); + TEST_CHECK(nni_base64_encode(dec, strlen(dec), buf, 0) == -1); + + TEST_CHECK(nni_base64_decode( + enc, strlen(enc), buf, strlen(dec) - 1) == -1); + TEST_CHECK(nni_base64_encode(enc, strlen(enc), buf, 0) == -1); + } +} + +TEST_LIST = { + { "encode", test_encode }, + { "decode", test_decode }, + { "overflow", test_overflow }, + { NULL, NULL }, +}; diff --git a/src/supplemental/sha1/CMakeLists.txt b/src/supplemental/sha1/CMakeLists.txt index 370dc7c2..de301b08 100644 --- a/src/supplemental/sha1/CMakeLists.txt +++ b/src/supplemental/sha1/CMakeLists.txt @@ -1,6 +1,6 @@ # +# Copyright 2019 Staysail Systems, Inc. <info@staysail.tech> # Copyright 2017 Capitar IT Group BV <info@capitar.com> -# Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> # # This software is supplied under the terms of the MIT License, a # copy of which should be located in the distribution where this @@ -8,9 +8,5 @@ # found online at https://opensource.org/licenses/MIT. # -if (NNG_SUPP_SHA1) - set(_SRCS - supplemental/sha1/sha1.c - supplemental/sha1/sha1.h) - set(NNG_SRCS ${NNG_SRCS} ${_SRCS} PARENT_SCOPE) -endif() +nng_sources_if(NNG_SUPP_SHA1 sha1.c sha1.h) +nng_test(sha1_test) diff --git a/src/supplemental/sha1/sha1_test.c b/src/supplemental/sha1/sha1_test.c new file mode 100644 index 00000000..505175b0 --- /dev/null +++ b/src/supplemental/sha1/sha1_test.c @@ -0,0 +1,72 @@ +// +// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include <stdint.h> +#include <string.h> + +#include <nng/nng.h> + +#include <acutest.h> + +#include "sha1.h" + + +// The following test vectors are from RFC 3174. +#define TEST1 "abc" +#define TEST2a "abcdbcdecdefdefgefghfghighijhi" +#define TEST2b "jkijkljklmklmnlmnomnopnopq" +#define TEST2 TEST2a TEST2b +#define TEST3 "a" +#define TEST4a "01234567012345670123456701234567" +#define TEST4b "01234567012345670123456701234567" +/* an exact multiple of 512 bits */ +#define TEST4 TEST4a TEST4b + +char *testarray[4] = { TEST1, TEST2, TEST3, TEST4 }; +int repeatcount[4] = { 1, 1, 1000000, 10 }; +char *resultarray[4] = { + "A9 99 3E 36 47 06 81 6A BA 3E 25 71 78 50 C2 6C 9C D0 D8 9D", + "84 98 3E 44 1C 3B D2 6E BA AE 4A A1 F9 51 29 E5 E5 46 70 F1", + "34 AA 97 3C D4 C4 DA A4 F6 1E EB 2B DB AD 27 31 65 34 01 6F", + "DE A3 56 A2 CD DD 90 C7 A7 EC ED C5 EB B5 63 93 4F 46 04 52" +}; + +void +test_sha1(void) +{ + + for (int i = 0; i < 4; i++) { + nni_sha1_ctx ctx; + size_t slen = strlen(testarray[i]); + uint8_t digest[20]; + char strout[20 * 3 + 1]; + char name[8]; + + snprintf(name, sizeof(name), "%d", i); + TEST_CASE(name); + + memset(digest, 0, sizeof(digest)); + nni_sha1_init(&ctx); + for (int j = 0; j < repeatcount[i]; j++) { + nni_sha1_update(&ctx, (uint8_t *) testarray[i], slen); + } + nni_sha1_final(&ctx, digest); + for (int j = 0; j < 20; j++) { + snprintf(strout + j * 3, 4, "%02X ", digest[j]); + } + strout[20 * 3 - 1] = '\0'; + TEST_CHECK(strcmp(strout, resultarray[i]) == 0); + } +} + +TEST_LIST = { + { "sha1", test_sha1 }, + { NULL, NULL }, +}; |
