1 /* Test ns_name-related functions.
2 Copyright (C) 2017 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 <http://www.gnu.org/licenses/>. */
19 /* This test program processes the tst-ns_name.data file. */
27 #include <support/check.h>
28 #include <support/support.h>
29 #include <support/xstdio.h>
31 /* A byte buffer and its length. */
38 /* Convert a base64-encoded string to its binary representation. */
40 base64_to_buffer (const char *base64
, struct buffer
*result
)
42 /* "-" denotes an empty input. */
43 if (strcmp (base64
, "-") == 0)
45 result
->data
= xmalloc (1);
50 size_t size
= strlen (base64
);
51 unsigned char *data
= xmalloc (size
);
52 int ret
= b64_pton (base64
, data
, size
);
53 if (ret
< 0 || ret
> size
)
55 result
->data
= xrealloc (data
, ret
);
60 /* A test case for ns_name_unpack and ns_name_ntop. */
68 struct buffer unpack_output
;
73 /* Deallocate the buffers associated with the test case. */
75 free_test_case (struct test_case
*t
)
79 free (t
->unpack_output
.data
);
83 /* Extract the test case information from a test file line. */
85 parse_test_case (const char *path
, size_t lineno
, const char *line
,
86 struct test_case
*result
)
88 memset (result
, 0, sizeof (*result
));
89 result
->path
= xstrdup (path
);
90 result
->lineno
= lineno
;
91 result
->ntop_result
= -1;
93 char *unpack_output
= NULL
;
94 int ret
= sscanf (line
, "%ms %zu %d %ms %d %ms",
95 &input
, &result
->input_offset
,
96 &result
->unpack_result
, &unpack_output
,
97 &result
->ntop_result
, &result
->ntop_text
);
100 printf ("%s:%zu: error: missing input fields\n", path
, lineno
);
104 if (!base64_to_buffer (input
, &result
->input
))
106 printf ("%s:%zu: error: malformed base64 input data\n", path
, lineno
);
108 free (unpack_output
);
109 free (result
->ntop_text
);
114 if (unpack_output
== NULL
)
115 result
->unpack_output
= (struct buffer
) { NULL
, 0 };
116 else if (!base64_to_buffer (unpack_output
, &result
->unpack_output
))
118 printf ("%s:%zu: error: malformed base64 unpack data\n", path
, lineno
);
119 free (result
->input
.data
);
120 free (unpack_output
);
121 free (result
->ntop_text
);
124 free (unpack_output
);
126 /* At this point, all allocated buffers have been transferred to
129 if (result
->input_offset
> result
->input
.length
)
131 printf ("%s:%zu: error: input offset %zu exceeds buffer size %zu\n",
132 path
, lineno
, result
->input_offset
, result
->input
.length
);
133 free_test_case (result
);
136 if (result
->unpack_result
< -1)
138 printf ("%s:%zu: error: invalid unpack result %d\n",
139 path
, lineno
, result
->unpack_result
);
140 free_test_case (result
);
143 if (result
->ntop_result
< -1)
145 printf ("%s:%zu: error: invalid ntop result %d\n",
146 path
, lineno
, result
->ntop_result
);
147 free_test_case (result
);
151 bool fields_consistent
;
155 fields_consistent
= result
->unpack_result
== -1;
158 fields_consistent
= result
->unpack_result
!= -1
159 && result
->ntop_result
== -1;
162 fields_consistent
= result
->unpack_result
!= -1
163 && result
->ntop_result
!= -1;
166 fields_consistent
= false;
168 if (!fields_consistent
)
170 printf ("%s:%zu: error: wrong number of fields: %d\n",
172 free_test_case (result
);
178 /* Format the buffer as a hexadecimal string and write it to standard
181 print_hex (const char *label
, struct buffer buffer
)
183 printf (" %s ", label
);
184 unsigned char *p
= buffer
.data
;
185 unsigned char *end
= p
+ buffer
.length
;
188 printf ("%02X", *p
& 0xFF);
194 /* Run the test case specified in *T. */
196 run_test_case (struct test_case
*t
)
198 unsigned char *unpacked
= xmalloc (NS_MAXCDNAME
);
199 int consumed
= ns_name_unpack
200 (t
->input
.data
, t
->input
.data
+ t
->input
.length
,
201 t
->input
.data
+ t
->input_offset
,
202 unpacked
, NS_MAXCDNAME
);
203 if (consumed
!= t
->unpack_result
)
205 support_record_failure ();
206 printf ("%s:%zu: error: wrong result from ns_name_unpack\n"
209 t
->path
, t
->lineno
, t
->unpack_result
, consumed
);
214 if (memcmp (unpacked
, t
->unpack_output
.data
, consumed
) != 0)
216 support_record_failure ();
217 printf ("%s:%zu: error: wrong data from ns_name_unpack\n",
219 print_hex ("expected:", t
->unpack_output
);
220 print_hex ("actual: ", (struct buffer
) { unpacked
, consumed
});
224 char *text
= xmalloc (NS_MAXDNAME
);
225 int ret
= ns_name_ntop (unpacked
, text
, NS_MAXDNAME
);
226 if (ret
!= t
->ntop_result
)
228 support_record_failure ();
229 printf ("%s:%zu: error: wrong result from ns_name_top\n"
232 t
->path
, t
->lineno
, t
->ntop_result
, ret
);
237 if (strcmp (text
, t
->ntop_text
) != 0)
239 support_record_failure ();
240 printf ("%s:%zu: error: wrong data from ns_name_ntop\n",
242 printf (" expected: \"%s\"\n", t
->ntop_text
);
243 printf (" actual: \"%s\"\n", text
);
252 /* Open the file at PATH, parse the test cases contained in it, and
255 run_test_file (const char *path
)
257 FILE *fp
= xfopen (path
, "re");
259 size_t line_allocated
= 0;
264 ssize_t ret
= getline (&line
, &line_allocated
, fp
);
269 printf ("%s: error reading file: %m\n", path
);
272 TEST_VERIFY (feof (fp
));
280 if (*p
== '\0' || *p
== '#')
283 struct test_case test_case
;
284 if (!parse_test_case (path
, lineno
, line
, &test_case
))
286 support_record_failure ();
289 run_test_case (&test_case
);
290 free_test_case (&test_case
);
299 run_test_file ("tst-ns_name.data");
303 #include <support/test-driver.c>