sparc: Fix .udiv plt on libc
[glibc.git] / resolv / tst-ns_name.c
blob66d9a6666b2ebde201217d9dc71e753e6b759647
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. */
21 #include <ctype.h>
22 #include <resolv.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <support/check.h>
28 #include <support/support.h>
29 #include <support/xstdio.h>
31 /* A byte buffer and its length. */
32 struct buffer
34 unsigned char *data;
35 size_t length;
38 /* Convert a base64-encoded string to its binary representation. */
39 static bool
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);
46 result->length = 0;
47 return true;
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)
54 return false;
55 result->data = xrealloc (data, ret);
56 result->length = ret;
57 return true;
60 /* A test case for ns_name_unpack and ns_name_ntop. */
61 struct test_case
63 char *path;
64 size_t lineno;
65 struct buffer input;
66 size_t input_offset;
67 int unpack_result;
68 struct buffer unpack_output;
69 int ntop_result;
70 char *ntop_text;
73 /* Deallocate the buffers associated with the test case. */
74 static void
75 free_test_case (struct test_case *t)
77 free (t->path);
78 free (t->input.data);
79 free (t->unpack_output.data);
80 free (t->ntop_text);
83 /* Extract the test case information from a test file line. */
84 static bool
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;
92 char *input = NULL;
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);
98 if (ret < 3)
100 printf ("%s:%zu: error: missing input fields\n", path, lineno);
101 free (input);
102 return false;
104 if (!base64_to_buffer (input, &result->input))
106 printf ("%s:%zu: error: malformed base64 input data\n", path, lineno);
107 free (input);
108 free (unpack_output);
109 free (result->ntop_text);
110 return false;
112 free (input);
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);
122 return false;
124 free (unpack_output);
126 /* At this point, all allocated buffers have been transferred to
127 *result. */
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);
134 return false;
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);
141 return false;
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);
148 return false;
151 bool fields_consistent;
152 switch (ret)
154 case 3:
155 fields_consistent = result->unpack_result == -1;
156 break;
157 case 5:
158 fields_consistent = result->unpack_result != -1
159 && result->ntop_result == -1;
160 break;
161 case 6:
162 fields_consistent = result->unpack_result != -1
163 && result->ntop_result != -1;
164 break;
165 default:
166 fields_consistent = false;
168 if (!fields_consistent)
170 printf ("%s:%zu: error: wrong number of fields: %d\n",
171 path, lineno, ret);
172 free_test_case (result);
173 return false;
175 return true;
178 /* Format the buffer as a hexadecimal string and write it to standard
179 output. */
180 static void
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;
186 while (p < end)
188 printf ("%02X", *p & 0xFF);
189 ++p;
191 putchar ('\n');
194 /* Run the test case specified in *T. */
195 static void
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"
207 " expected: %d\n"
208 " actual: %d\n",
209 t->path, t->lineno, t->unpack_result, consumed);
210 return;
212 if (consumed != -1)
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",
218 t->path, t->lineno);
219 print_hex ("expected:", t->unpack_output);
220 print_hex ("actual: ", (struct buffer) { unpacked, consumed });
221 return;
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"
230 " expected: %d\n"
231 " actual: %d\n",
232 t->path, t->lineno, t->ntop_result, ret);
233 return;
235 if (ret != -1)
237 if (strcmp (text, t->ntop_text) != 0)
239 support_record_failure ();
240 printf ("%s:%zu: error: wrong data from ns_name_ntop\n",
241 t->path, t->lineno);
242 printf (" expected: \"%s\"\n", t->ntop_text);
243 printf (" actual: \"%s\"\n", text);
244 return;
247 free (text);
249 free (unpacked);
252 /* Open the file at PATH, parse the test cases contained in it, and
253 run them. */
254 static void
255 run_test_file (const char *path)
257 FILE *fp = xfopen (path, "re");
258 char *line = NULL;
259 size_t line_allocated = 0;
260 size_t lineno = 0;
262 while (true)
264 ssize_t ret = getline (&line, &line_allocated, fp);
265 if (ret < 0)
267 if (ferror (fp))
269 printf ("%s: error reading file: %m\n", path);
270 exit (1);
272 TEST_VERIFY (feof (fp));
273 break;
276 ++lineno;
277 char *p = line;
278 while (isspace (*p))
279 ++p;
280 if (*p == '\0' || *p == '#')
281 continue;
283 struct test_case test_case;
284 if (!parse_test_case (path, lineno, line, &test_case))
286 support_record_failure ();
287 continue;
289 run_test_case (&test_case);
290 free_test_case (&test_case);
292 free (line);
293 xfclose (fp);
296 static int
297 do_test (void)
299 run_test_file ("tst-ns_name.data");
300 return 0;
303 #include <support/test-driver.c>