1 /* Test of message digests.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 test_digest_on_files (int (*streamfunc
) (FILE *, void *),
19 const char *streamfunc_name
,
21 const void *expected_for_empty_file
,
22 const void *expected_for_small_file
,
23 const void *expected_for_large_file
)
28 for (pass
= 0; pass
< 5; pass
++)
31 FILE *fp
= fopen (TESTFILE
, "wb");
34 fprintf (stderr
, "Could not create file %s.\n", TESTFILE
);
40 /* Nothing to do for the empty file. */
43 /* Fill the small file, with some header that will be skipped. */
47 /* Fill the small file. */
48 fputs ("The quick brown fox jumps over the lazy dog.\n", fp
);
51 /* Fill the large file, with some header that will be skipped. */
55 /* Fill the large file (8 MiB). */
58 for (i
= 0; i
< 0x400000; i
++)
61 unsigned int j
= i
* (i
-1) * (i
-5);
62 c
[0] = (unsigned char)(j
>> 6);
63 c
[1] = (i
% 499) + (i
% 101);
71 fprintf (stderr
, "Could not write data to file %s.\n", TESTFILE
);
77 /* Test an unaligned digest. */
78 char *digest
= (char *) malloc (digest_size
+ 1) + 1;
85 case 0: expected
= expected_for_empty_file
; break;
86 case 1: case 2: expected
= expected_for_small_file
; break;
87 case 3: case 4: expected
= expected_for_large_file
; break;
91 fp
= fopen (TESTFILE
, "rb");
94 fprintf (stderr
, "Could not open file %s.\n", TESTFILE
);
103 if (fread (header
, 1, sizeof (header
), fp
) != sizeof (header
))
105 fprintf (stderr
, "Could not read the header of %s.\n",
112 ret
= streamfunc (fp
, digest
);
115 fprintf (stderr
, "%s failed with error %d\n", streamfunc_name
, -ret
);
118 if (memcmp (digest
, expected
, digest_size
) != 0)
121 fprintf (stderr
, "%s produced wrong result.\n", streamfunc_name
);
122 fprintf (stderr
, "Expected: ");
123 for (i
= 0; i
< digest_size
; i
++)
124 fprintf (stderr
, "\\x%02x", ((const unsigned char *) expected
)[i
]);
125 fprintf (stderr
, "\n");
126 fprintf (stderr
, "Got: ");
127 for (i
= 0; i
< digest_size
; i
++)
128 fprintf (stderr
, "\\x%02x", ((const unsigned char *) digest
)[i
]);
129 fprintf (stderr
, "\n");
132 /* Verify that fp is now positioned at end of file. */
133 if (getc (fp
) != EOF
)
135 fprintf (stderr
, "%s left the stream not at EOF\n", streamfunc_name
);