tst-test_compare_blob.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Basic test for the TEST_COMPARE_BLOB macro.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <string.h>
  16. #include <support/check.h>
  17. #include <support/capture_subprocess.h>
  18. static void
  19. subprocess (void *closure)
  20. {
  21. /* These tests should fail. They were chosen to cover differences
  22. in length (with the same contents), single-bit mismatches, and
  23. mismatching null pointers. */
  24. TEST_COMPARE_BLOB ("", 0, "", 1); /* Line 29. */
  25. TEST_COMPARE_BLOB ("X", 1, "", 1); /* Line 30. */
  26. TEST_COMPARE_BLOB ("abcd", 3, "abcd", 4); /* Line 31. */
  27. TEST_COMPARE_BLOB ("abcd", 4, "abcD", 4); /* Line 32. */
  28. TEST_COMPARE_BLOB ("abcd", 4, NULL, 0); /* Line 33. */
  29. TEST_COMPARE_BLOB (NULL, 0, "abcd", 4); /* Line 34. */
  30. }
  31. /* Same contents, different addresses. */
  32. char buffer_abc_1[] = "abc";
  33. char buffer_abc_2[] = "abc";
  34. static int
  35. do_test (void)
  36. {
  37. /* This should succeed. Even if the pointers and array contents are
  38. different, zero-length inputs are not different. */
  39. TEST_COMPARE_BLOB ("", 0, "", 0);
  40. TEST_COMPARE_BLOB ("", 0, buffer_abc_1, 0);
  41. TEST_COMPARE_BLOB (buffer_abc_1, 0, "", 0);
  42. TEST_COMPARE_BLOB (NULL, 0, "", 0);
  43. TEST_COMPARE_BLOB ("", 0, NULL, 0);
  44. TEST_COMPARE_BLOB (NULL, 0, NULL, 0);
  45. /* Check equality of blobs containing a single NUL byte. */
  46. TEST_COMPARE_BLOB ("", 1, "", 1);
  47. TEST_COMPARE_BLOB ("", 1, &buffer_abc_1[3], 1);
  48. /* Check equality of blobs of varying lengths. */
  49. for (size_t i = 0; i <= sizeof (buffer_abc_1); ++i)
  50. TEST_COMPARE_BLOB (buffer_abc_1, i, buffer_abc_2, i);
  51. struct support_capture_subprocess proc = support_capture_subprocess
  52. (&subprocess, NULL);
  53. /* Discard the reported error. */
  54. support_record_failure_reset ();
  55. puts ("info: *** subprocess output starts ***");
  56. fputs (proc.out.buffer, stdout);
  57. puts ("info: *** subprocess output ends ***");
  58. TEST_VERIFY
  59. (strcmp (proc.out.buffer,
  60. "tst-test_compare_blob.c:29: error: blob comparison failed\n"
  61. " left length: 0 bytes (from 0)\n"
  62. " right length: 1 bytes (from 1)\n"
  63. " right (evaluated from \"\"):\n"
  64. " \"\\000\"\n"
  65. " 00\n"
  66. "tst-test_compare_blob.c:30: error: blob comparison failed\n"
  67. " blob length: 1 bytes\n"
  68. " left (evaluated from \"X\"):\n"
  69. " \"X\"\n"
  70. " 58\n"
  71. " right (evaluated from \"\"):\n"
  72. " \"\\000\"\n"
  73. " 00\n"
  74. "tst-test_compare_blob.c:31: error: blob comparison failed\n"
  75. " left length: 3 bytes (from 3)\n"
  76. " right length: 4 bytes (from 4)\n"
  77. " left (evaluated from \"abcd\"):\n"
  78. " \"abc\"\n"
  79. " 61 62 63\n"
  80. " right (evaluated from \"abcd\"):\n"
  81. " \"abcd\"\n"
  82. " 61 62 63 64\n"
  83. "tst-test_compare_blob.c:32: error: blob comparison failed\n"
  84. " blob length: 4 bytes\n"
  85. " left (evaluated from \"abcd\"):\n"
  86. " \"abcd\"\n"
  87. " 61 62 63 64\n"
  88. " right (evaluated from \"abcD\"):\n"
  89. " \"abcD\"\n"
  90. " 61 62 63 44\n"
  91. "tst-test_compare_blob.c:33: error: blob comparison failed\n"
  92. " left length: 4 bytes (from 4)\n"
  93. " right length: 0 bytes (from 0)\n"
  94. " left (evaluated from \"abcd\"):\n"
  95. " \"abcd\"\n"
  96. " 61 62 63 64\n"
  97. "tst-test_compare_blob.c:34: error: blob comparison failed\n"
  98. " left length: 0 bytes (from 0)\n"
  99. " right length: 4 bytes (from 4)\n"
  100. " right (evaluated from \"abcd\"):\n"
  101. " \"abcd\"\n"
  102. " 61 62 63 64\n"
  103. ) == 0);
  104. /* Check that there is no output on standard error. */
  105. support_capture_subprocess_check (&proc, "TEST_COMPARE_BLOB",
  106. 0, sc_allow_stdout);
  107. return 0;
  108. }
  109. #include <support/test-driver.c>