Lack of unit tests is addressed through these patches.
>From fd8f83c7b100f841f7772e9dd83a0b7e182d3b3b Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 5 May 2018 17:38:39 +0200 Subject: [PATCH 1/4] md5 tests: Add test for md5_stream.
* tests/test-digest.h: New file. * tests/test-md5.c: Include test-digest.h. (main): Invoke test_digest_on_files on 'md5_stream'. * modules/crypto/md5-tests (Files): Add tests/test-digest.h. --- ChangeLog | 8 ++++ modules/crypto/md5-tests | 1 + tests/test-digest.h | 115 +++++++++++++++++++++++++++++++++++++++++++++++ tests/test-md5.c | 11 +++++ 4 files changed, 135 insertions(+) create mode 100644 tests/test-digest.h diff --git a/ChangeLog b/ChangeLog index 00e809f..926727d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2018-05-05 Bruno Haible <br...@clisp.org> + + md5 tests: Add test for md5_stream. + * tests/test-digest.h: New file. + * tests/test-md5.c: Include test-digest.h. + (main): Invoke test_digest_on_files on 'md5_stream'. + * modules/crypto/md5-tests (Files): Add tests/test-digest.h. + 2018-04-28 Matteo Croce <mcr...@redhat.com> md5sum: Use AF_ALG when available. diff --git a/modules/crypto/md5-tests b/modules/crypto/md5-tests index 3b4bd63..23384e8 100644 --- a/modules/crypto/md5-tests +++ b/modules/crypto/md5-tests @@ -1,5 +1,6 @@ Files: tests/test-md5.c +tests/test-digest.h Depends-on: diff --git a/tests/test-digest.h b/tests/test-digest.h new file mode 100644 index 0000000..39a1d2e --- /dev/null +++ b/tests/test-digest.h @@ -0,0 +1,115 @@ +/* Test of message digests. + Copyright (C) 2018 Free Software Foundation, Inc. + + 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 <https://www.gnu.org/licenses/>. */ + +static void +test_digest_on_files (int (*streamfunc) (FILE *, void *), + const char *streamfunc_name, + size_t digest_size, + const void *expected_for_empty_file, + const void *expected_for_small_file, + const void *expected_for_large_file) +{ + int pass; + unlink (TESTFILE); + + for (pass = 0; pass < 3; pass++) + { + { + FILE *fp = fopen (TESTFILE, "wb"); + if (fp == NULL) + { + fprintf (stderr, "Could not create file %s.\n", TESTFILE); + exit (1); + } + switch (pass) + { + case 0: + /* Nothing to do for the empty file. */ + break; + case 1: + /* Fill the small file. */ + fputs ("The quick brown fox jumps over the lazy dog.\n", fp); + break; + case 2: + /* Fill the large file (8 MiB). */ + { + unsigned int i; + for (i = 0; i < 0x400000; i++) + { + unsigned char c[2]; + unsigned int j = i * (i-1) * (i-5); + c[0] = (unsigned char)(j >> 6); + c[1] = (i % 499) + (i % 101); + fwrite (c, 1, 2, fp); + } + } + break; + } + if (ferror (fp)) + { + fprintf (stderr, "Could not write data to file %s.\n", TESTFILE); + exit (1); + } + fclose (fp); + } + { + /* Test an unaligned digest. */ + char *digest = (char *) malloc (digest_size + 1) + 1; + const void *expected; + FILE *fp; + int ret; + + switch (pass) + { + case 0: expected = expected_for_empty_file; break; + case 1: expected = expected_for_small_file; break; + case 2: expected = expected_for_large_file; break; + default: abort (); + } + + fp = fopen (TESTFILE, "rb"); + if (fp == NULL) + { + fprintf (stderr, "Could not open file %s.\n", TESTFILE); + exit (1); + } + ret = streamfunc (fp, digest); + if (ret) + { + fprintf (stderr, "%s failed with error %d\n", streamfunc_name, -ret); + exit (1); + } + if (memcmp (digest, expected, digest_size) != 0) + { + int i; + fprintf (stderr, "%s produced wrong result.\n", streamfunc_name); + fprintf (stderr, "Expected: "); + for (i = 0; i < digest_size; i++) + fprintf (stderr, "\\x%02x", ((const unsigned char *) expected)[i]); + fprintf (stderr, "\n"); + fprintf (stderr, "Got: "); + for (i = 0; i < digest_size; i++) + fprintf (stderr, "\\x%02x", ((const unsigned char *) digest)[i]); + fprintf (stderr, "\n"); + exit (1); + } + fclose (fp); + free (digest - 1); + } + } + + unlink (TESTFILE); +} diff --git a/tests/test-md5.c b/tests/test-md5.c index 13b6a76..7109343 100644 --- a/tests/test-md5.c +++ b/tests/test-md5.c @@ -22,7 +22,12 @@ #include "md5.h" #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <unistd.h> + +#define TESTFILE "test-md5.data" +#include "test-digest.h" int main (void) @@ -63,5 +68,11 @@ main (void) return 1; } + /* Test md5_stream. */ + test_digest_on_files (md5_stream, "md5_stream", 16, + "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e", + "\x0d\x70\x06\xcd\x05\x5e\x94\xcf\x61\x45\x87\xe1\xd2\xae\x0c\x8e", + "\xec\x99\x67\x9b\xff\xc0\xf9\xb0\x6d\x18\x30\x6b\x06\xd6\x56\x23"); + return 0; } -- 2.7.4
>From 804251c6ac3c7fe2d6c4aa51a32b70a01b6baaf0 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 5 May 2018 17:39:57 +0200 Subject: [PATCH 2/4] sha1 tests: Add test for sha1_stream. * tests/test-sha1.c: Include test-digest.h. (main): Invoke test_digest_on_files on 'sha1_stream'. * modules/crypto/sha1-tests (Files): Add tests/test-digest.h. --- ChangeLog | 7 +++++++ modules/crypto/sha1-tests | 1 + tests/test-sha1.c | 11 +++++++++++ 3 files changed, 19 insertions(+) diff --git a/ChangeLog b/ChangeLog index 926727d..00ca4dc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2018-05-05 Bruno Haible <br...@clisp.org> + sha1 tests: Add test for sha1_stream. + * tests/test-sha1.c: Include test-digest.h. + (main): Invoke test_digest_on_files on 'sha1_stream'. + * modules/crypto/sha1-tests (Files): Add tests/test-digest.h. + +2018-05-05 Bruno Haible <br...@clisp.org> + md5 tests: Add test for md5_stream. * tests/test-digest.h: New file. * tests/test-md5.c: Include test-digest.h. diff --git a/modules/crypto/sha1-tests b/modules/crypto/sha1-tests index df83721..493b6db 100644 --- a/modules/crypto/sha1-tests +++ b/modules/crypto/sha1-tests @@ -1,5 +1,6 @@ Files: tests/test-sha1.c +tests/test-digest.h Depends-on: diff --git a/tests/test-sha1.c b/tests/test-sha1.c index b06ca1f..9b6cde3 100644 --- a/tests/test-sha1.c +++ b/tests/test-sha1.c @@ -20,7 +20,12 @@ #include "sha1.h" #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <unistd.h> + +#define TESTFILE "test-sha1.data" +#include "test-digest.h" int main (void) @@ -44,5 +49,11 @@ main (void) return 1; } + /* Test sha1_stream. */ + test_digest_on_files (sha1_stream, "sha1_stream", 20, + "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18\x90\xaf\xd8\x07\x09", + "\x9c\x04\xcd\x63\x72\x07\x7e\x9b\x11\xf7\x0c\xa1\x11\xc9\x80\x7d\xc7\x13\x7e\x4b", + "\x91\xab\x6b\x1b\x8d\x29\x25\x3c\xcb\x8d\xce\xb7\x7a\x25\x26\x2c\x92\xc9\x22\x09"); + return 0; } -- 2.7.4
>From 36a10add8d8b9d9e2faa53460adacba31433249b Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 5 May 2018 17:41:35 +0200 Subject: [PATCH 3/4] sha256: Add tests. * tests/test-sha256.c: New file. * modules/crypto/sha256-tests: New file. --- ChangeLog | 6 ++++++ modules/crypto/sha256-tests | 12 ++++++++++++ tests/test-sha256.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 modules/crypto/sha256-tests create mode 100644 tests/test-sha256.c diff --git a/ChangeLog b/ChangeLog index 00ca4dc..d1e4990 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2018-05-05 Bruno Haible <br...@clisp.org> + sha256: Add tests. + * tests/test-sha256.c: New file. + * modules/crypto/sha256-tests: New file. + +2018-05-05 Bruno Haible <br...@clisp.org> + sha1 tests: Add test for sha1_stream. * tests/test-sha1.c: Include test-digest.h. (main): Invoke test_digest_on_files on 'sha1_stream'. diff --git a/modules/crypto/sha256-tests b/modules/crypto/sha256-tests new file mode 100644 index 0000000..3ecbc32 --- /dev/null +++ b/modules/crypto/sha256-tests @@ -0,0 +1,12 @@ +Files: +tests/test-sha256.c +tests/test-digest.h + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-sha256 +check_PROGRAMS += test-sha256 +test_sha256_LDADD = $(LDADD) @LIB_CRYPTO@ diff --git a/tests/test-sha256.c b/tests/test-sha256.c new file mode 100644 index 0000000..0a2667a --- /dev/null +++ b/tests/test-sha256.c @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2018 Free Software Foundation, Inc. + * + * 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 <https://www.gnu.org/licenses/>. */ + +#include <config.h> + +#include "sha256.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define TESTFILE "test-sha256.data" +#include "test-digest.h" + +int +main (void) +{ + /* Test sha224_stream. */ + test_digest_on_files (sha224_stream, "sha224_stream", 28, + "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f", + "\xe8\x87\x99\xb0\xd0\xd5\xbe\xcc\x67\x91\x83\x7f\xa9\x53\x88\xd4\x05\x6f\x12\x50\xa5\x11\xd1\x48\x29\x76\x66\x63", + "\x86\xa2\x5a\xc3\x6a\x70\x8e\xf3\x4e\x78\x55\xf0\xb1\x97\x8a\xa8\x61\x78\x6c\xb8\x87\xcd\x5c\xf1\x19\x3b\x53\xe4"); + + /* Test sha256_stream. */ + test_digest_on_files (sha256_stream, "sha256_stream", 32, + "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55", + "\xb4\x7c\xc0\xf1\x04\xb6\x2d\x4c\x7c\x30\xbc\xd6\x8f\xd8\xe6\x76\x13\xe2\x87\xdc\x4a\xd8\xc3\x10\xef\x10\xcb\xad\xea\x9c\x43\x80", + "\xe5\x19\x95\xff\x0b\xc4\x95\xcf\x49\xdd\x31\x42\x0a\x6b\xe0\x6b\x38\xef\xef\x43\xab\xa6\xf4\x76\x64\x32\x99\x4a\x47\x1a\x0f\xb5"); + + return 0; +} -- 2.7.4
>From 9e4e9ac2037610ee321b79a16c2509936aa9fcef Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 5 May 2018 17:42:46 +0200 Subject: [PATCH 4/4] sha512: Add tests. * tests/test-sha512.c: New file. * modules/crypto/sha512-tests: New file. --- ChangeLog | 6 ++++++ modules/crypto/sha512-tests | 12 ++++++++++++ tests/test-sha512.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 modules/crypto/sha512-tests create mode 100644 tests/test-sha512.c diff --git a/ChangeLog b/ChangeLog index d1e4990..122f8da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2018-05-05 Bruno Haible <br...@clisp.org> + sha512: Add tests. + * tests/test-sha512.c: New file. + * modules/crypto/sha512-tests: New file. + +2018-05-05 Bruno Haible <br...@clisp.org> + sha256: Add tests. * tests/test-sha256.c: New file. * modules/crypto/sha256-tests: New file. diff --git a/modules/crypto/sha512-tests b/modules/crypto/sha512-tests new file mode 100644 index 0000000..ee24afb --- /dev/null +++ b/modules/crypto/sha512-tests @@ -0,0 +1,12 @@ +Files: +tests/test-sha512.c +tests/test-digest.h + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-sha512 +check_PROGRAMS += test-sha512 +test_sha512_LDADD = $(LDADD) @LIB_CRYPTO@ diff --git a/tests/test-sha512.c b/tests/test-sha512.c new file mode 100644 index 0000000..577feb1 --- /dev/null +++ b/tests/test-sha512.c @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2018 Free Software Foundation, Inc. + * + * 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 <https://www.gnu.org/licenses/>. */ + +#include <config.h> + +#include "sha512.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define TESTFILE "test-sha512.data" +#include "test-digest.h" + +int +main (void) +{ + /* Test sha384_stream. */ + test_digest_on_files (sha384_stream, "sha384_stream", 48, + "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48\x98\xb9\x5b", + "\xd5\x1d\x28\xd0\x14\x1e\x56\xf6\x92\x95\x2e\xa1\x48\x61\x89\x8e\x2b\x41\x7b\x92\x28\x31\xe0\xf4\xbc\xdb\xc3\x26\xa7\xfe\x1e\x9d\x95\x63\x18\x2e\x83\xd3\xa8\xaf\x66\xf6\x85\x36\xe0\xd4\x2b\x88", + "\xab\x32\x29\xa1\xc8\x43\xce\x38\x85\x93\xb3\xa6\x7f\x5a\x36\xdc\xda\xf2\xac\x33\x22\x80\xee\xbc\x5b\x1b\x70\x8b\x9b\x96\x4b\xc1\x75\x60\x00\xae\xdc\xfd\x60\x70\x24\x7f\x0b\x8b\x3a\x89\xd3\xbb"); + + /* Test sha512_stream. */ + test_digest_on_files (sha512_stream, "sha512_stream", 64, + "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a\xf9\x27\xda\x3e", + "\x02\x0d\xa0\xf4\xd8\xa4\xc8\xbf\xbc\x98\x27\x40\x27\x74\x00\x61\xd7\xdf\x52\xee\x07\x09\x1e\xd6\x59\x5a\x08\x3e\x0f\x45\x32\x7b\xbe\x59\x42\x43\x12\xd8\x6f\x21\x8b\x74\xed\x2e\x25\x50\x7a\xba\xf5\xc7\xa5\xfc\xf4\xca\xfc\xf9\x53\x8b\x70\x58\x08\xfd\x55\xec", + "\x38\x34\x58\xf3\xde\x68\x70\x33\x0c\xa4\xd4\x71\x4d\x0f\x41\xc5\xed\xc9\xf4\x67\x3e\x19\x0f\x87\x2b\x53\x72\x00\xa8\x35\x73\xf7\x0b\xf1\xf2\x0d\x9a\xa8\xd6\x08\x65\x2f\xe2\x28\xbd\x2d\x0c\xd5\xd6\xd4\xd3\xb2\x84\x3e\x32\x32\x5d\x62\x54\x7b\x39\xfd\x47\x04"); + + return 0; +} -- 2.7.4