1 /* Basic test for the TEST_COMPARE_BLOB macro.
2    Copyright (C) 2018-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
19 #include <string.h>
20 #include <support/check.h>
21 #include <support/capture_subprocess.h>
22 
23 static void
subprocess(void * closure)24 subprocess (void *closure)
25 {
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.  */
35 }
36 
37 /* Same contents, different addresses.  */
38 char buffer_abc_1[] = "abc";
39 char buffer_abc_2[] = "abc";
40 
41 static int
do_test(void)42 do_test (void)
43 {
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);
52 
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);
56 
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);
60 
61   struct support_capture_subprocess proc = support_capture_subprocess
62     (&subprocess, NULL);
63 
64   /* Discard the reported error.  */
65   support_record_failure_reset ();
66 
67   puts ("info: *** subprocess output starts ***");
68   fputs (proc.out.buffer, stdout);
69   puts ("info: *** subprocess output ends ***");
70 
71   TEST_VERIFY
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"
77 "      \"\\000\"\n"
78 "      00\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"
82 "      \"X\"\n"
83 "      58\n"
84 "  right (evaluated from \"\"):\n"
85 "      \"\\000\"\n"
86 "      00\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"
91 "      \"abc\"\n"
92 "      61 62 63\n"
93 "  right (evaluated from \"abcd\"):\n"
94 "      \"abcd\"\n"
95 "      61 62 63 64\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"
99 "      \"abcd\"\n"
100 "      61 62 63 64\n"
101 "  right (evaluated from \"abcD\"):\n"
102 "      \"abcD\"\n"
103 "      61 62 63 44\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"
108 "      \"abcd\"\n"
109 "      61 62 63 64\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"
114 "      \"abcd\"\n"
115 "      61 62 63 64\n"
116              ) == 0);
117 
118   /* Check that there is no output on standard error.  */
119   support_capture_subprocess_check (&proc, "TEST_COMPARE_BLOB",
120                                     0, sc_allow_stdout);
121 
122   return 0;
123 }
124 
125 #include <support/test-driver.c>
126