diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/crypt/Makefile | 2 | ||||
-rw-r--r-- | tests/crypt/run-test.c | 2 | ||||
-rw-r--r-- | tests/crypt/test.h | 1 | ||||
-rw-r--r-- | tests/crypt/test_sha256.c | 138 | ||||
-rw-r--r-- | tests/eloop-bench/Makefile | 2 | ||||
-rw-r--r-- | tests/eloop-bench/eloop-bench.c | 9 |
6 files changed, 149 insertions, 5 deletions
diff --git a/tests/crypt/Makefile b/tests/crypt/Makefile index dd676f4b2aa9..c3df1a31f5ad 100644 --- a/tests/crypt/Makefile +++ b/tests/crypt/Makefile @@ -3,7 +3,7 @@ include ${TOP}/iconfig.mk PROG= run-test SRCS= run-test.c -SRCS+= test_hmac_md5.c +SRCS+= test_hmac_md5.c test_sha256.c CFLAGS?= -O2 CSTD?= c99 diff --git a/tests/crypt/run-test.c b/tests/crypt/run-test.c index 42f52d1fce2d..91cc89830305 100644 --- a/tests/crypt/run-test.c +++ b/tests/crypt/run-test.c @@ -33,6 +33,8 @@ int main(void) if (test_hmac_md5()) r = -1; + if (test_sha256()) + r = -1; return r; } diff --git a/tests/crypt/test.h b/tests/crypt/test.h index 4f716f1220f4..3c9070a5c83d 100644 --- a/tests/crypt/test.h +++ b/tests/crypt/test.h @@ -28,5 +28,6 @@ #ifndef TEST_H int test_hmac_md5(void); +int test_sha256(void); #endif diff --git a/tests/crypt/test_sha256.c b/tests/crypt/test_sha256.c new file mode 100644 index 000000000000..986e0513c34c --- /dev/null +++ b/tests/crypt/test_sha256.c @@ -0,0 +1,138 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright (c) 2023 Tobias Heider <tobias.heider@canonical.com> + * Copyright (c) 2006-2018 Roy Marples <roy@marples.name> + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "config.h" +#include "test.h" + +#ifdef SHA2_H +#include SHA2_H +#endif + +# ifndef SHA256_DIGEST_LENGTH +# define SHA256_DIGEST_LENGTH 32 +# endif + +static void +print_md(FILE *stream, const uint8_t *md) +{ + int i; + + fprintf(stream, "digest = 0x"); + for (i = 0; i < SHA256_DIGEST_LENGTH; i++) + fprintf(stream, "%02x", *md++); + fprintf(stream, "\n"); +} + +static void +test_md(const uint8_t *md, const uint8_t *tst) +{ + print_md(stdout, md); + if (memcmp(md, tst, SHA256_DIGEST_LENGTH) == 0) + return; + fprintf(stderr, "FAILED!\nExpected\t\t\t"); + print_md(stderr, tst); + exit(EXIT_FAILURE); +} + +static void +sha256_test1(void) +{ + const uint8_t text[] = "Hi There"; + const uint8_t expect[SHA256_DIGEST_LENGTH] = { + 0xcc, 0x6d, 0x58, 0x96, 0xd7, 0x70, 0x10, 0x1e, + 0xf0, 0x28, 0x0c, 0x94, 0x3a, 0x2d, 0x3c, 0x3f, + 0x24, 0xcd ,0x5b, 0x11, 0x46, 0x4a, 0x51, 0x86, + 0xda, 0xf7, 0xa2, 0x38, 0x47, 0x71, 0x62, 0xac + }; + uint8_t digest[SHA256_DIGEST_LENGTH]; + SHA256_CTX ctx; + + printf ("SHA256 Test 1:\t\t"); + SHA256_Init(&ctx); + SHA256_Update(&ctx, text, 8); + SHA256_Final(digest, &ctx); + test_md(digest, expect); +} + +static void +sha256_test2(void) +{ + const uint8_t text[] = "what do ya want for nothing?"; + const uint8_t expect[SHA256_DIGEST_LENGTH] = { + 0xb3, 0x81, 0xe7, 0xfe, 0xc6, 0x53, 0xfc, 0x3a, + 0xb9, 0xb1, 0x78, 0x27, 0x23, 0x66, 0xb8, 0xac, + 0x87, 0xfe, 0xd8, 0xd3, 0x1c, 0xb2, 0x5e, 0xd1, + 0xd0, 0xe1, 0xf3, 0x31, 0x86, 0x44, 0xc8, 0x9c, + }; + uint8_t digest[SHA256_DIGEST_LENGTH]; + SHA256_CTX ctx; + + printf ("SHA256 Test 2:\t\t"); + SHA256_Init(&ctx); + SHA256_Update(&ctx, text, 28); + SHA256_Final(digest, &ctx); + test_md(digest, expect); +} + +static void +sha256_test3(void) +{ + const uint8_t expect[SHA256_DIGEST_LENGTH] = { + 0x5c, 0xf6, 0x18, 0xb5, 0xb6, 0xd3, 0x8b, 0xd1, + 0x6c, 0x2e, 0x55, 0x8e, 0xef, 0x4d, 0x4b, 0x6d, + 0x52, 0x82, 0x84, 0x54, 0x7f, 0xd4, 0xa0, 0x9d, + 0xa2, 0xab, 0xb6, 0xf0, 0x98, 0xec, 0x61, 0x93, + }; + uint8_t digest[SHA256_DIGEST_LENGTH]; + uint8_t text[50]; + int i; + SHA256_CTX ctx; + + printf ("SHA256 Test 3:\t\t"); + for (i = 0; i < 50; i++) + text[i] = 0xdd; + SHA256_Init(&ctx); + SHA256_Update(&ctx, text, 50); + SHA256_Final(digest, &ctx); + test_md(digest, expect); +} + +int test_sha256(void) +{ + printf ("Starting SHA256 tests...\n\n"); + sha256_test1(); + sha256_test2(); + sha256_test3(); + printf("\nAll tests pass.\n"); + return 0; +} diff --git a/tests/eloop-bench/Makefile b/tests/eloop-bench/Makefile index 2827c6070422..a0ebafd9c0f9 100644 --- a/tests/eloop-bench/Makefile +++ b/tests/eloop-bench/Makefile @@ -39,7 +39,7 @@ distclean: clean depend: ${PROG}: ${DEPEND} ${OBJS} - ${CC} ${LDFLAGS} -o $@ ${OBJS} + ${CC} ${LDFLAGS} -o $@ ${OBJS} ${LDADD} test: ${PROG} ./${PROG} diff --git a/tests/eloop-bench/eloop-bench.c b/tests/eloop-bench/eloop-bench.c index 0f90e824c3ad..fd0fb2edd3db 100644 --- a/tests/eloop-bench/eloop-bench.c +++ b/tests/eloop-bench/eloop-bench.c @@ -1,6 +1,6 @@ /* * eloop benchmark - * Copyright (c) 2006-2018 Roy Marples <roy@marples.name> + * Copyright (c) 2006-2023 Roy Marples <roy@marples.name> * All rights reserved. * Redistribution and use in source and binary forms, with or without @@ -58,11 +58,14 @@ static struct pipe *pipes; static struct eloop *e; static void -read_cb(void *arg) +read_cb(void *arg, unsigned short events) { struct pipe *p = arg; unsigned char buf[1]; + if (events != ELE_READ) + warn("%s: unexpected events 0x%04x", __func__, events); + if (read(p->fd[0], buf, 1) != 1) { warn("%s: read", __func__); bad++; @@ -156,7 +159,7 @@ main(int argc, char **argv) for (i = 0, p = pipes; i < npipes; i++, p++) { if (pipe2(p->fd, O_CLOEXEC | O_NONBLOCK) == -1) err(EXIT_FAILURE, "pipe"); - if (eloop_event_add(e, p->fd[0], read_cb, p) == -1) + if (eloop_event_add(e, p->fd[0], ELE_READ, read_cb, p) == -1) err(EXIT_FAILURE, "eloop_event_add"); } |