logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: bf93db3a6eba9906740e9bc09d90741ef312cfce
parent e450d99b5382edf35a5e9e9e66fab0c8686e8cc6
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 26 Sep 2024 10:27:52 +0200

test-lib: prefix test executables with t_

This allows to avoid filename conflicts when using coverage

Diffstat:

MMakefile30+++++++++++++++---------------
Dtest-lib/sha1.c69---------------------------------------------------------------------
Dtest-lib/sha256.c71-----------------------------------------------------------------------
Dtest-lib/sha512.c82-------------------------------------------------------------------------------
Rtest-lib/mode.c -> test-lib/t_mode.c0
Atest-lib/t_sha1.c68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest-lib/t_sha256.c70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest-lib/t_sha512.c81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rtest-lib/strtodur.c -> test-lib/t_strtodur.c0
Rtest-lib/symbolize_mode.c -> test-lib/t_symbolize_mode.c0
Rtest-lib/truncation.c -> test-lib/t_truncation.c0
11 files changed, 234 insertions(+), 237 deletions(-)

diff --git a/Makefile b/Makefile @@ -30,7 +30,7 @@ selfcheck-cmds: $(EXE) $(TEST_CMDS) LDSTATIC="$(LDSTATIC)" ./check-cmds.sh .PHONY: selfcheck-libs -TEST_LIBS = test-lib/mode test-lib/strtodur test-lib/symbolize_mode test-lib/truncation test-lib/sha1 test-lib/sha256 test-lib/sha512 +TEST_LIBS = test-lib/t_mode test-lib/t_strtodur test-lib/t_symbolize_mode test-lib/t_truncation test-lib/t_sha1 test-lib/t_sha256 test-lib/t_sha512 selfcheck-libs: $(TEST_LIBS) LDSTATIC="$(LDSTATIC)" ./check-libs.sh $(TEST_LIBS) @@ -102,26 +102,26 @@ cmd/timeout: cmd/timeout.c lib/strtodur.c lib/sys_signame.h lib/sys_signame.c Ma $(RM) -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} $(CC) -std=c99 $(CFLAGS) -o $@ cmd/timeout.c lib/strtodur.c lib/sys_signame.c $(LDFLAGS) $(LDSTATIC) -test-lib/mode: test-lib/mode.c lib/mode.c Makefile - $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/mode.c lib/mode.c $(LDFLAGS) $(LDSTATIC) +test-lib/t_mode: test-lib/t_mode.c lib/mode.c Makefile + $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/t_mode.c lib/mode.c $(LDFLAGS) $(LDSTATIC) -test-lib/strtodur: test-lib/strtodur.c lib/strtodur.c Makefile - $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/strtodur.c lib/strtodur.c $(LDFLAGS) $(LDSTATIC) +test-lib/t_strtodur: test-lib/t_strtodur.c lib/strtodur.c Makefile + $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/t_strtodur.c lib/strtodur.c $(LDFLAGS) $(LDSTATIC) -test-lib/symbolize_mode: test-lib/symbolize_mode.c lib/symbolize_mode.c Makefile - $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/symbolize_mode.c lib/symbolize_mode.c $(LDFLAGS) $(LDSTATIC) +test-lib/t_symbolize_mode: test-lib/t_symbolize_mode.c lib/symbolize_mode.c Makefile + $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/t_symbolize_mode.c lib/symbolize_mode.c $(LDFLAGS) $(LDSTATIC) -test-lib/truncation: test-lib/truncation.c lib/truncation.c Makefile - $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/truncation.c lib/truncation.c $(LDFLAGS) $(LDSTATIC) +test-lib/t_truncation: test-lib/t_truncation.c lib/truncation.c Makefile + $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/t_truncation.c lib/truncation.c $(LDFLAGS) $(LDSTATIC) -test-lib/sha1: test-lib/sha1.c lib/sha1.c lib/sha1.h lib/bytes2hex.c lib/strconv.h Makefile - $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/sha1.c lib/sha1.c lib/bytes2hex.c $(LDFLAGS) $(LDSTATIC) +test-lib/t_sha1: test-lib/t_sha1.c lib/sha1.c lib/sha1.h lib/bytes2hex.c lib/strconv.h Makefile + $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/t_sha1.c lib/sha1.c lib/bytes2hex.c $(LDFLAGS) $(LDSTATIC) -test-lib/sha256: test-lib/sha256.c lib/sha256.c lib/sha256.h lib/bytes2hex.c lib/strconv.h Makefile - $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/sha256.c lib/sha256.c lib/bytes2hex.c $(LDFLAGS) $(LDSTATIC) +test-lib/t_sha256: test-lib/t_sha256.c lib/sha256.c lib/sha256.h lib/bytes2hex.c lib/strconv.h Makefile + $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/t_sha256.c lib/sha256.c lib/bytes2hex.c $(LDFLAGS) $(LDSTATIC) -test-lib/sha512: test-lib/sha512.c lib/sha512.c lib/sha512.h lib/bytes2hex.c lib/strconv.h Makefile - $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/sha512.c lib/sha512.c lib/bytes2hex.c $(LDFLAGS) $(LDSTATIC) +test-lib/t_sha512: test-lib/t_sha512.c lib/sha512.c lib/sha512.h lib/bytes2hex.c lib/strconv.h Makefile + $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/t_sha512.c lib/sha512.c lib/bytes2hex.c $(LDFLAGS) $(LDSTATIC) cmd/df: cmd/df.c lib/humanize.c Makefile diff --git a/test-lib/sha1.c b/test-lib/sha1.c @@ -1,69 +0,0 @@ -// utils-std: Collection of commonly available Unix tools -// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> -// SPDX-License-Identifier: 0BSD - -#define _POSIX_C_SOURCE 200809L -#include "../lib/sha1.h" - -#include "../lib/strconv.h" - -#include <assert.h> -#include <stdio.h> // printf -#include <string.h> // memcmp, memset - -int counter = 0; -int err = 0; - -static void -t_sha1(const char *name, - const void *data, - size_t datalen, - const char expected[SHA1_DIGEST_LENGTH * 2]) -{ - int id = ++counter; - - struct sha1 ctx; - sha1_init(&ctx); - sha1_update(&ctx, data, datalen); - - uint8_t res[SHA1_DIGEST_LENGTH] = ""; - sha1_sum(&ctx, res); - - char got[SHA1_DIGEST_LENGTH * 2] = ""; - bytes2hex(res, sizeof(res), got, sizeof(got)); - - if(memcmp(expected, got, SHA1_DIGEST_LENGTH * 2) == 0) - { - printf("ok %d - %s\n", id, name); - return; - } - - err = 1; - - fputs("# Expected: ", stdout); - fwrite(expected, SHA1_DIGEST_LENGTH * 2, 1, stdout); - fputs("\n", stdout); - - fputs("# Got: ", stdout); - fwrite(got, SHA1_DIGEST_LENGTH * 2, 1, stdout); - fputs("\n", stdout); - - printf("not ok %d - %s\n", id, name); -} - -int -main(void) -{ - int plan = 3; - printf("1..%d\n", plan); - - uint8_t million_a[1000000]; - memset(million_a, 'a', 1000000); - - t_sha1("empty", "", 0, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - t_sha1("abc", "abc", 3, "a9993e364706816aba3e25717850c26c9cd0d89d"); - t_sha1("million_a", million_a, 1000000, "34aa973cd4c4daa4f61eeb2bdbad27316534016f"); - - assert(counter == plan); - return err; -} diff --git a/test-lib/sha256.c b/test-lib/sha256.c @@ -1,71 +0,0 @@ -// utils-std: Collection of commonly available Unix tools -// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> -// SPDX-License-Identifier: 0BSD - -#define _POSIX_C_SOURCE 200809L -#include "../lib/sha256.h" - -#include "../lib/strconv.h" - -#include <assert.h> -#include <stdio.h> // printf -#include <string.h> // memcmp - -int counter = 0; -int err = 0; - -static void -t_sha256(const char *name, - const void *data, - size_t datalen, - const char expected[SHA256_DIGEST_LENGTH * 2]) -{ - int id = ++counter; - - struct sha256 ctx; - sha256_init(&ctx); - sha256_update(&ctx, data, datalen); - - uint8_t res[SHA256_DIGEST_LENGTH] = ""; - sha256_sum(&ctx, res); - - char got[SHA256_DIGEST_LENGTH * 2] = ""; - bytes2hex(res, sizeof(res), got, sizeof(got)); - - if(memcmp(expected, got, SHA256_DIGEST_LENGTH * 2) == 0) - { - printf("ok %d - %s\n", id, name); - return; - } - - err = 1; - - fputs("# Expected: ", stdout); - fwrite(expected, SHA256_DIGEST_LENGTH * 2, 1, stdout); - fputs("\n", stdout); - - fputs("# Got: ", stdout); - fwrite(got, SHA256_DIGEST_LENGTH * 2, 1, stdout); - fputs("\n", stdout); - - printf("not ok %d - %s\n", id, name); -} - -int -main(void) -{ - int plan = 3; - printf("1..%d\n", plan); - - t_sha256("empty", "", 0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); - - // https://web.archive.org/web/20130526224224/https://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf - t_sha256("abc", "abc", 3, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); - t_sha256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - 56, - "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); - - assert(counter == plan); - return err; -} diff --git a/test-lib/sha512.c b/test-lib/sha512.c @@ -1,82 +0,0 @@ -// utils-std: Collection of commonly available Unix tools -// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> -// SPDX-License-Identifier: 0BSD - -#define _POSIX_C_SOURCE 200809L -#include "../lib/sha512.h" - -#include "../lib/strconv.h" - -#include <assert.h> -#include <stdio.h> // printf -#include <string.h> // memcmp - -int counter = 0; -int err = 0; - -static void -t_sha512(const char *name, - const void *data, - size_t datalen, - const char expected[SHA512_DIGEST_LENGTH * 2]) -{ - int id = ++counter; - - struct sha512 ctx; - sha512_init(&ctx); - sha512_update(&ctx, data, datalen); - - uint8_t res[SHA512_DIGEST_LENGTH] = ""; - sha512_sum(&ctx, res); - - char got[SHA512_DIGEST_LENGTH * 2] = ""; - bytes2hex(res, sizeof(res), got, sizeof(got)); - - if(memcmp(expected, got, SHA512_DIGEST_LENGTH * 2) == 0) - { - printf("ok %d - %s\n", id, name); - return; - } - - err = 1; - - fputs("# Expected: ", stdout); - fwrite(expected, SHA512_DIGEST_LENGTH * 2, 1, stdout); - fputs("\n", stdout); - - fputs("# Got: ", stdout); - fwrite(got, SHA512_DIGEST_LENGTH * 2, 1, stdout); - fputs("\n", stdout); - - printf("not ok %d - %s\n", id, name); -} - -int -main(void) -{ - int plan = 3; - printf("1..%d\n", plan); - - t_sha512("empty", - "", - 0, - "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce" - "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - // https://web.archive.org/web/20130526224224/https://csrc.nist.gov/groups/STM/cavp/documents/shs/sha512-384-512.pdf - t_sha512("abc", - "abc", - 3, - "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" - "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); - // clang-format off - t_sha512("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - 112, - "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" - "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"); - // clang-format on - - assert(counter == plan); - return err; -} diff --git a/test-lib/mode.c b/test-lib/t_mode.c diff --git a/test-lib/t_sha1.c b/test-lib/t_sha1.c @@ -0,0 +1,68 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: 0BSD + +#define _POSIX_C_SOURCE 200809L +#include "../lib/sha1.h" +#include "../lib/strconv.h" + +#include <assert.h> +#include <stdio.h> // printf +#include <string.h> // memcmp, memset + +int counter = 0; +int err = 0; + +static void +t_sha1(const char *name, + const void *data, + size_t datalen, + const char expected[SHA1_DIGEST_LENGTH * 2]) +{ + int id = ++counter; + + struct sha1 ctx; + sha1_init(&ctx); + sha1_update(&ctx, data, datalen); + + uint8_t res[SHA1_DIGEST_LENGTH] = ""; + sha1_sum(&ctx, res); + + char got[SHA1_DIGEST_LENGTH * 2] = ""; + bytes2hex(res, sizeof(res), got, sizeof(got)); + + if(memcmp(expected, got, SHA1_DIGEST_LENGTH * 2) == 0) + { + printf("ok %d - %s\n", id, name); + return; + } + + err = 1; + + fputs("# Expected: ", stdout); + fwrite(expected, SHA1_DIGEST_LENGTH * 2, 1, stdout); + fputs("\n", stdout); + + fputs("# Got: ", stdout); + fwrite(got, SHA1_DIGEST_LENGTH * 2, 1, stdout); + fputs("\n", stdout); + + printf("not ok %d - %s\n", id, name); +} + +int +main(void) +{ + int plan = 3; + printf("1..%d\n", plan); + + uint8_t million_a[1000000]; + memset(million_a, 'a', 1000000); + + t_sha1("empty", "", 0, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); + t_sha1("abc", "abc", 3, "a9993e364706816aba3e25717850c26c9cd0d89d"); + t_sha1("million_a", million_a, 1000000, "34aa973cd4c4daa4f61eeb2bdbad27316534016f"); + + assert(counter == plan); + return err; +} diff --git a/test-lib/t_sha256.c b/test-lib/t_sha256.c @@ -0,0 +1,70 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: 0BSD + +#define _POSIX_C_SOURCE 200809L +#include "../lib/sha256.h" +#include "../lib/strconv.h" + +#include <assert.h> +#include <stdio.h> // printf +#include <string.h> // memcmp + +int counter = 0; +int err = 0; + +static void +t_sha256(const char *name, + const void *data, + size_t datalen, + const char expected[SHA256_DIGEST_LENGTH * 2]) +{ + int id = ++counter; + + struct sha256 ctx; + sha256_init(&ctx); + sha256_update(&ctx, data, datalen); + + uint8_t res[SHA256_DIGEST_LENGTH] = ""; + sha256_sum(&ctx, res); + + char got[SHA256_DIGEST_LENGTH * 2] = ""; + bytes2hex(res, sizeof(res), got, sizeof(got)); + + if(memcmp(expected, got, SHA256_DIGEST_LENGTH * 2) == 0) + { + printf("ok %d - %s\n", id, name); + return; + } + + err = 1; + + fputs("# Expected: ", stdout); + fwrite(expected, SHA256_DIGEST_LENGTH * 2, 1, stdout); + fputs("\n", stdout); + + fputs("# Got: ", stdout); + fwrite(got, SHA256_DIGEST_LENGTH * 2, 1, stdout); + fputs("\n", stdout); + + printf("not ok %d - %s\n", id, name); +} + +int +main(void) +{ + int plan = 3; + printf("1..%d\n", plan); + + t_sha256("empty", "", 0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); + + // https://web.archive.org/web/20130526224224/https://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf + t_sha256("abc", "abc", 3, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); + t_sha256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + 56, + "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); + + assert(counter == plan); + return err; +} diff --git a/test-lib/t_sha512.c b/test-lib/t_sha512.c @@ -0,0 +1,81 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: 0BSD + +#define _POSIX_C_SOURCE 200809L +#include "../lib/sha512.h" +#include "../lib/strconv.h" + +#include <assert.h> +#include <stdio.h> // printf +#include <string.h> // memcmp + +int counter = 0; +int err = 0; + +static void +t_sha512(const char *name, + const void *data, + size_t datalen, + const char expected[SHA512_DIGEST_LENGTH * 2]) +{ + int id = ++counter; + + struct sha512 ctx; + sha512_init(&ctx); + sha512_update(&ctx, data, datalen); + + uint8_t res[SHA512_DIGEST_LENGTH] = ""; + sha512_sum(&ctx, res); + + char got[SHA512_DIGEST_LENGTH * 2] = ""; + bytes2hex(res, sizeof(res), got, sizeof(got)); + + if(memcmp(expected, got, SHA512_DIGEST_LENGTH * 2) == 0) + { + printf("ok %d - %s\n", id, name); + return; + } + + err = 1; + + fputs("# Expected: ", stdout); + fwrite(expected, SHA512_DIGEST_LENGTH * 2, 1, stdout); + fputs("\n", stdout); + + fputs("# Got: ", stdout); + fwrite(got, SHA512_DIGEST_LENGTH * 2, 1, stdout); + fputs("\n", stdout); + + printf("not ok %d - %s\n", id, name); +} + +int +main(void) +{ + int plan = 3; + printf("1..%d\n", plan); + + t_sha512("empty", + "", + 0, + "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce" + "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); + + // https://web.archive.org/web/20130526224224/https://csrc.nist.gov/groups/STM/cavp/documents/shs/sha512-384-512.pdf + t_sha512("abc", + "abc", + 3, + "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); + // clang-format off + t_sha512("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + 112, + "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"); + // clang-format on + + assert(counter == plan); + return err; +} diff --git a/test-lib/strtodur.c b/test-lib/t_strtodur.c diff --git a/test-lib/symbolize_mode.c b/test-lib/t_symbolize_mode.c diff --git a/test-lib/truncation.c b/test-lib/t_truncation.c