Mercurial > gnulib
changeset 39318:4fca5604a306
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.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sat, 05 May 2018 17:38:39 +0200 |
parents | a9be6af8a363 |
children | d08e3ed7a079 |
files | ChangeLog modules/crypto/md5-tests tests/test-digest.h tests/test-md5.c |
diffstat | 4 files changed, 135 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Sat May 05 08:18:49 2018 -0700 +++ b/ChangeLog Sat May 05 17:38:39 2018 +0200 @@ -1,3 +1,11 @@ +2018-05-05 Bruno Haible <bruno@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 <mcroce@redhat.com> md5sum: Use AF_ALG when available.
--- a/modules/crypto/md5-tests Sat May 05 08:18:49 2018 -0700 +++ b/modules/crypto/md5-tests Sat May 05 17:38:39 2018 +0200 @@ -1,5 +1,6 @@ Files: tests/test-md5.c +tests/test-digest.h Depends-on:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-digest.h Sat May 05 17:38:39 2018 +0200 @@ -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); +}
--- a/tests/test-md5.c Sat May 05 08:18:49 2018 -0700 +++ b/tests/test-md5.c Sat May 05 17:38:39 2018 +0200 @@ -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 @@ 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; }