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.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
20 #include <support/check.h>
21 #include <support/capture_subprocess.h>
24 subprocess (void *closure
)
26 /* These tests should fail. They were chosen to cover differences
27 in length (with the same contents), single-bit mismatches, and
28 mismatching null pointers. */
29 TEST_COMPARE_BLOB ("", 0, "", 1); /* Line 29. */
30 TEST_COMPARE_BLOB ("X", 1, "", 1); /* Line 30. */
31 TEST_COMPARE_BLOB ("abcd", 3, "abcd", 4); /* Line 31. */
32 TEST_COMPARE_BLOB ("abcd", 4, "abcD", 4); /* Line 32. */
33 TEST_COMPARE_BLOB ("abcd", 4, NULL
, 0); /* Line 33. */
34 TEST_COMPARE_BLOB (NULL
, 0, "abcd", 4); /* Line 34. */
37 /* Same contents, different addresses. */
38 char buffer_abc_1
[] = "abc";
39 char buffer_abc_2
[] = "abc";
44 /* This should succeed. Even if the pointers and array contents are
45 different, zero-length inputs are not different. */
46 TEST_COMPARE_BLOB ("", 0, "", 0);
47 TEST_COMPARE_BLOB ("", 0, buffer_abc_1
, 0);
48 TEST_COMPARE_BLOB (buffer_abc_1
, 0, "", 0);
49 TEST_COMPARE_BLOB (NULL
, 0, "", 0);
50 TEST_COMPARE_BLOB ("", 0, NULL
, 0);
51 TEST_COMPARE_BLOB (NULL
, 0, NULL
, 0);
53 /* Check equality of blobs containing a single NUL byte. */
54 TEST_COMPARE_BLOB ("", 1, "", 1);
55 TEST_COMPARE_BLOB ("", 1, &buffer_abc_1
[3], 1);
57 /* Check equality of blobs of varying lengths. */
58 for (size_t i
= 0; i
<= sizeof (buffer_abc_1
); ++i
)
59 TEST_COMPARE_BLOB (buffer_abc_1
, i
, buffer_abc_2
, i
);
61 struct support_capture_subprocess proc
= support_capture_subprocess
64 /* Discard the reported error. */
65 support_record_failure_reset ();
67 puts ("info: *** subprocess output starts ***");
68 fputs (proc
.out
.buffer
, stdout
);
69 puts ("info: *** subprocess output ends ***");
72 (strcmp (proc
.out
.buffer
,
73 "tst-test_compare_blob.c:29: error: blob comparison failed\n"
74 " left length: 0 bytes (from 0)\n"
75 " right length: 1 bytes (from 1)\n"
76 " right (evaluated from \"\"):\n"
79 "tst-test_compare_blob.c:30: error: blob comparison failed\n"
80 " blob length: 1 bytes\n"
81 " left (evaluated from \"X\"):\n"
84 " right (evaluated from \"\"):\n"
87 "tst-test_compare_blob.c:31: error: blob comparison failed\n"
88 " left length: 3 bytes (from 3)\n"
89 " right length: 4 bytes (from 4)\n"
90 " left (evaluated from \"abcd\"):\n"
93 " right (evaluated from \"abcd\"):\n"
96 "tst-test_compare_blob.c:32: error: blob comparison failed\n"
97 " blob length: 4 bytes\n"
98 " left (evaluated from \"abcd\"):\n"
101 " right (evaluated from \"abcD\"):\n"
104 "tst-test_compare_blob.c:33: error: blob comparison failed\n"
105 " left length: 4 bytes (from 4)\n"
106 " right length: 0 bytes (from 0)\n"
107 " left (evaluated from \"abcd\"):\n"
110 "tst-test_compare_blob.c:34: error: blob comparison failed\n"
111 " left length: 0 bytes (from 0)\n"
112 " right length: 4 bytes (from 4)\n"
113 " right (evaluated from \"abcd\"):\n"
118 /* Check that there is no output on standard error. */
119 support_capture_subprocess_check (&proc
, "TEST_COMPARE_BLOB",
125 #include <support/test-driver.c>