md5test-giant.c 3.5 KB

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