| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Testcase for http://sourceware.org/bugzilla/show_bug.cgi?id=14090. | 
|  | 2 | Copyright (C) 2012-2016 Free Software Foundation, Inc. | 
|  | 3 | This file is part of the GNU C Library. | 
|  | 4 |  | 
|  | 5 | This program is free software; you can redistribute it and/or modify | 
|  | 6 | it under the terms of the GNU General Public License as published | 
|  | 7 | by the Free Software Foundation; version 2 of the License, or | 
|  | 8 | (at your option) any later version. | 
|  | 9 |  | 
|  | 10 | This program is distributed in the hope that it will be useful, | 
|  | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 13 | GNU General Public License for more details. | 
|  | 14 |  | 
|  | 15 | You should have received a copy of the GNU General Public License | 
|  | 16 | along with this program; if not, see <http://www.gnu.org/licenses/>.  */ | 
|  | 17 |  | 
|  | 18 | #include <stdint.h> | 
|  | 19 | #include <stdio.h> | 
|  | 20 | #include <stdlib.h> | 
|  | 21 | #include <string.h> | 
|  | 22 | #include <sys/mman.h> | 
|  | 23 |  | 
|  | 24 | #include "md5.h" | 
|  | 25 |  | 
|  | 26 | /* This test will not work with 32-bit size_t, so let it succeed | 
|  | 27 | there.  */ | 
|  | 28 | #if SIZE_MAX <= UINT32_MAX | 
|  | 29 | static int | 
|  | 30 | do_test (void) | 
|  | 31 | { | 
|  | 32 | return 0; | 
|  | 33 | } | 
|  | 34 | #else | 
|  | 35 |  | 
|  | 36 | # define CONST_2G  0x080000000 | 
|  | 37 | # define CONST_10G 0x280000000 | 
|  | 38 |  | 
|  | 39 | /* MD5 sum values of zero-filled blocks of specified sizes.  */ | 
|  | 40 | static const struct test_data_s | 
|  | 41 | { | 
|  | 42 | const char ref[16]; | 
|  | 43 | size_t len; | 
|  | 44 | } test_data[] = | 
|  | 45 | { | 
|  | 46 | { "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e", | 
|  | 47 | 0x000000000 }, | 
|  | 48 | { "\xa9\x81\x13\x0c\xf2\xb7\xe0\x9f\x46\x86\xdc\x27\x3c\xf7\x18\x7e", | 
|  | 49 | 0x080000000 }, | 
|  | 50 | { "\xc9\xa5\xa6\x87\x8d\x97\xb4\x8c\xc9\x65\xc1\xe4\x18\x59\xf0\x34", | 
|  | 51 | 0x100000000 }, | 
|  | 52 | { "\x58\xcf\x63\x8a\x73\x3f\x91\x90\x07\xb4\x28\x7c\xf5\x39\x6d\x0c", | 
|  | 53 | 0x180000000 }, | 
|  | 54 | { "\xb7\x70\x35\x1f\xad\xae\x5a\x96\xbb\xaf\x97\x02\xed\x97\xd2\x8d", | 
|  | 55 | 0x200000000 }, | 
|  | 56 | { "\x2d\xd2\x6c\x4d\x47\x99\xeb\xd2\x9f\xa3\x1e\x48\xd4\x9e\x8e\x53", | 
|  | 57 | 0x280000000 }, | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | static int | 
|  | 61 | report (const char *id, const char *md5, size_t len, const char *ref) | 
|  | 62 | { | 
|  | 63 | if (memcmp (md5, ref, 16)) | 
|  | 64 | { | 
|  | 65 | printf ("test %s with size %zd failed\n", id, len); | 
|  | 66 | return 1; | 
|  | 67 | } | 
|  | 68 | return 0; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | /* Test md5 in a single md5_process_bytes call.  */ | 
|  | 72 | static int | 
|  | 73 | test_single (void *buf, size_t len, const char *ref) | 
|  | 74 | { | 
|  | 75 | char sum[16]; | 
|  | 76 | struct md5_ctx ctx; | 
|  | 77 |  | 
|  | 78 | __md5_init_ctx (&ctx); | 
|  | 79 | __md5_process_bytes (buf, len, &ctx); | 
|  | 80 | __md5_finish_ctx (&ctx, sum); | 
|  | 81 |  | 
|  | 82 | return report ("single", sum, len, ref); | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | /* Test md5 with two md5_process_bytes calls to trigger a | 
|  | 86 | different path in md5_process_block for sizes > 2 GB.  */ | 
|  | 87 | static int | 
|  | 88 | test_double (void *buf, size_t len, const char *ref) | 
|  | 89 | { | 
|  | 90 | char sum[16]; | 
|  | 91 | struct md5_ctx ctx; | 
|  | 92 |  | 
|  | 93 | __md5_init_ctx (&ctx); | 
|  | 94 | if (len >= CONST_2G) | 
|  | 95 | { | 
|  | 96 | __md5_process_bytes (buf, CONST_2G, &ctx); | 
|  | 97 | __md5_process_bytes (buf + CONST_2G, len - CONST_2G, &ctx); | 
|  | 98 | } | 
|  | 99 | else | 
|  | 100 | __md5_process_bytes (buf, len, &ctx); | 
|  | 101 |  | 
|  | 102 | __md5_finish_ctx (&ctx, sum); | 
|  | 103 |  | 
|  | 104 | return report ("double", sum, len, ref); | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 |  | 
|  | 108 | static int | 
|  | 109 | do_test (void) | 
|  | 110 | { | 
|  | 111 | void *buf; | 
|  | 112 | unsigned int j; | 
|  | 113 | int result = 0; | 
|  | 114 |  | 
|  | 115 | buf = mmap64 (0, CONST_10G, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | 
|  | 116 | if (buf == MAP_FAILED) | 
|  | 117 | { | 
|  | 118 | puts ("Could not allocate 10 GB via mmap, skipping test."); | 
|  | 119 | return 0; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | for (j = 0; j < sizeof (test_data) / sizeof (struct test_data_s); j++) | 
|  | 123 | { | 
|  | 124 | if (test_single (buf, test_data[j].len, test_data[j].ref)) | 
|  | 125 | result = 1; | 
|  | 126 | if (test_double (buf, test_data[j].len, test_data[j].ref)) | 
|  | 127 | result = 1; | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | return result; | 
|  | 131 | } | 
|  | 132 | #endif | 
|  | 133 |  | 
|  | 134 | /* This needs on a fast machine 90s.  */ | 
|  | 135 | #define TIMEOUT 480 | 
|  | 136 | #define TEST_FUNCTION do_test () | 
|  | 137 | #include "../test-skeleton.c" |