>From fd8f83c7b100f841f7772e9dd83a0b7e182d3b3b Mon Sep 17 00:00:00 2001 From: Bruno Haible 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 + + 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 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 . */ + +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 +#include #include +#include + +#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