Use ASCII in LC_TIME of om_ET for better readability
[glibc.git] / resolv / tst-inet_pton.c
blob4bb9f8119378b4670b06da044c0f63158ea6cb52
1 /* Test inet_pton 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 #include <arpa/inet.h>
20 #include <resolv/resolv-internal.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <support/check.h>
25 #include <support/xunistd.h>
26 #include <sys/mman.h>
27 #include <sys/param.h>
28 #include <unistd.h>
30 /* Memory region created by next_to_fault_allocate. */
31 struct next_to_fault
33 /* The user data. */
34 char *buffer;
35 size_t length;
37 /* The entire allocated region. */
38 void *region_start;
39 size_t region_size;
42 /* Allocate a buffer of SIZE bytes just before a page which is mapped
43 with PROT_NONE (so that overrunning the buffer will cause a
44 fault). */
45 static struct next_to_fault
46 next_to_fault_allocate (size_t size)
48 long page_size = sysconf (_SC_PAGE_SIZE);
49 TEST_VERIFY_EXIT (page_size > 0);
50 struct next_to_fault result;
51 result.region_size = roundup (size, page_size) + page_size;
52 TEST_VERIFY_EXIT (size + page_size > size);
53 TEST_VERIFY_EXIT (result.region_size > size);
54 result.region_start
55 = xmmap (NULL, result.region_size, PROT_READ | PROT_WRITE,
56 MAP_PRIVATE | MAP_ANONYMOUS, -1);
57 /* Unmap the page after the allocation. */
58 xmprotect (result.region_start + (result.region_size - page_size),
59 page_size, PROT_NONE);
60 /* Align the allocation within the region so that it ends just
61 before the PROT_NONE page. */
62 result.buffer = result.region_start + result.region_size - page_size - size;
63 result.length = size;
64 return result;
67 /* Deallocate the memory region allocated by
68 next_to_fault_allocate. */
69 static void
70 next_to_fault_free (struct next_to_fault *ntf)
72 xmunmap (ntf->region_start, ntf->region_size);
73 *ntf = (struct next_to_fault) { NULL, };
76 struct test_case
78 /* The input data. */
79 const char *input;
81 /* True if AF_INET parses successfully. */
82 bool ipv4_ok;
84 /* True if AF_INET6 parses successfully. */
85 bool ipv6_ok;
87 /* Expected result for AF_INET. */
88 unsigned char ipv4_expected[4];
90 /* Expected result for AF_INET6. */
91 unsigned char ipv6_expected[16];
94 static void
95 check_result (const char *what, const struct test_case *t, int family,
96 void *result_buffer, int inet_ret)
98 TEST_VERIFY_EXIT (inet_ret >= -1);
99 TEST_VERIFY_EXIT (inet_ret <= 1);
101 int ok;
102 const unsigned char *expected;
103 size_t result_size;
104 switch (family)
106 case AF_INET:
107 ok = t->ipv4_ok;
108 expected = t->ipv4_expected;
109 result_size = 4;
110 break;
111 case AF_INET6:
112 ok = t->ipv6_ok;
113 expected = t->ipv6_expected;
114 result_size = 16;
115 break;
116 default:
117 FAIL_EXIT1 ("invalid address family %d", family);
120 if (inet_ret != ok)
122 support_record_failure ();
123 printf ("error: %s return value mismatch for [[%s]], family %d\n"
124 " expected: %d\n"
125 " actual: %d\n",
126 what, t->input, family, ok, inet_ret);
127 return;
129 if (memcmp (result_buffer, expected, result_size) != 0)
131 support_record_failure ();
132 printf ("error: %s result mismatch for [[%s]], family %d\n",
133 what, t->input, family);
137 static void
138 run_one_test (const struct test_case *t)
140 size_t test_len = strlen (t->input);
142 struct next_to_fault ntf_out4 = next_to_fault_allocate (4);
143 struct next_to_fault ntf_out6 = next_to_fault_allocate (16);
145 /* inet_pton requires NUL termination. */
147 struct next_to_fault ntf_in = next_to_fault_allocate (test_len + 1);
148 memcpy (ntf_in.buffer, t->input, test_len + 1);
149 memset (ntf_out4.buffer, 0, 4);
150 check_result ("inet_pton", t, AF_INET, ntf_out4.buffer,
151 inet_pton (AF_INET, ntf_in.buffer, ntf_out4.buffer));
152 memset (ntf_out6.buffer, 0, 16);
153 check_result ("inet_pton", t, AF_INET6, ntf_out6.buffer,
154 inet_pton (AF_INET6, ntf_in.buffer, ntf_out6.buffer));
155 next_to_fault_free (&ntf_in);
158 /* __inet_pton_length does not require NUL termination. */
160 struct next_to_fault ntf_in = next_to_fault_allocate (test_len);
161 memcpy (ntf_in.buffer, t->input, test_len);
162 memset (ntf_out4.buffer, 0, 4);
163 check_result ("__inet_pton_length", t, AF_INET, ntf_out4.buffer,
164 __inet_pton_length (AF_INET, ntf_in.buffer, ntf_in.length,
165 ntf_out4.buffer));
166 memset (ntf_out6.buffer, 0, 16);
167 check_result ("__inet_pton_length", t, AF_INET6, ntf_out6.buffer,
168 __inet_pton_length (AF_INET6, ntf_in.buffer, ntf_in.length,
169 ntf_out6.buffer));
170 next_to_fault_free (&ntf_in);
173 next_to_fault_free (&ntf_out4);
174 next_to_fault_free (&ntf_out6);
177 /* The test cases were manually crafted and the set enhanced with
178 American Fuzzy Lop. */
179 const struct test_case test_cases[] =
181 {.input = ".:", },
182 {.input = "0.0.0.0",
183 .ipv4_ok = true,
184 .ipv4_expected = {0, 0, 0, 0},
186 {.input = "0.:", },
187 {.input = "00", },
188 {.input = "0000000", },
189 {.input = "00000000000000000", },
190 {.input = "092.", },
191 {.input = "10.0.301.2", },
192 {.input = "127.0.0.1",
193 .ipv4_ok = true,
194 .ipv4_expected = {127, 0, 0, 1},
196 {.input = "19..", },
197 {.input = "192.0.2.-1", },
198 {.input = "192.0.2.01", },
199 {.input = "192.0.2.1.", },
200 {.input = "192.0.2.1192.", },
201 {.input = "192.0.2.192.\377..", },
202 {.input = "192.0.2.256", },
203 {.input = "192.0.2.27",
204 .ipv4_ok = true,
205 .ipv4_expected = {192, 0, 2, 27},
207 {.input = "192.0.201.", },
208 {.input = "192.0.261.", },
209 {.input = "192.0.2\256", },
210 {.input = "192.0.\262.", },
211 {.input = "192.062.", },
212 {.input = "192.092.\256", },
213 {.input = "192.0\2562.", },
214 {.input = "192.192.0.2661\031", },
215 {.input = "192.192.00n2.1.", },
216 {.input = "192.192.2.190.", },
217 {.input = "192.255.255.2555", },
218 {.input = "192.92.219\023.", },
219 {.input = "192.\260.2.", },
220 {.input = "1:1::1:1",
221 .ipv6_ok = true,
222 .ipv6_expected = {
223 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0,
224 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
227 {.input = "2", },
228 {.input = "2.", },
229 {.input = "2001:db8:00001::f", },
230 {.input = "2001:db8:10000::f", },
231 {.input = "2001:db8:1234:5678:abcd:ef01:2345:67",
232 .ipv6_ok = true,
233 .ipv6_expected = {
234 0x20, 0x1, 0xd, 0xb8, 0x12, 0x34, 0x56, 0x78,
235 0xab, 0xcd, 0xef, 0x1, 0x23, 0x45, 0x0, 0x67
238 {.input = "2001:db8:1234:5678:abcd:ef01:2345:6789:1", },
239 {.input = "2001:db8:1234:5678:abcd:ef01:2345::6789", },
240 {.input = "2001:db8::0",
241 .ipv6_ok = true,
242 .ipv6_expected = {
243 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
244 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
247 {.input = "2001:db8::00",
248 .ipv6_ok = true,
249 .ipv6_expected = {
250 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
251 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
254 {.input = "2001:db8::1",
255 .ipv6_ok = true,
256 .ipv6_expected = {
257 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
258 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
261 {.input = "2001:db8::10",
262 .ipv6_ok = true,
263 .ipv6_expected = {
264 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
265 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10
268 {.input = "2001:db8::19",
269 .ipv6_ok = true,
270 .ipv6_expected = {
271 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
272 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19
275 {.input = "2001:db8::1::\012", },
276 {.input = "2001:db8::1::2\012", },
277 {.input = "2001:db8::2",
278 .ipv6_ok = true,
279 .ipv6_expected = {
280 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
281 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2
284 {.input = "2001:db8::3",
285 .ipv6_ok = true,
286 .ipv6_expected = {
287 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
288 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3
291 {.input = "2001:db8::4",
292 .ipv6_ok = true,
293 .ipv6_expected = {
294 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
295 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4
298 {.input = "2001:db8::5",
299 .ipv6_ok = true,
300 .ipv6_expected = {
301 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
302 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5
305 {.input = "2001:db8::6",
306 .ipv6_ok = true,
307 .ipv6_expected = {
308 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
309 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6
312 {.input = "2001:db8::7",
313 .ipv6_ok = true,
314 .ipv6_expected = {
315 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
316 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7
319 {.input = "2001:db8::8",
320 .ipv6_ok = true,
321 .ipv6_expected = {
322 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
323 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8
326 {.input = "2001:db8::9",
327 .ipv6_ok = true,
328 .ipv6_expected = {
329 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
330 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9
333 {.input = "2001:db8::A",
334 .ipv6_ok = true,
335 .ipv6_expected = {
336 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
337 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
340 {.input = "2001:db8::B",
341 .ipv6_ok = true,
342 .ipv6_expected = {
343 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
344 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
347 {.input = "2001:db8::C",
348 .ipv6_ok = true,
349 .ipv6_expected = {
350 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
351 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
354 {.input = "2001:db8::D",
355 .ipv6_ok = true,
356 .ipv6_expected = {
357 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
358 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
361 {.input = "2001:db8::E",
362 .ipv6_ok = true,
363 .ipv6_expected = {
364 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
365 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
368 {.input = "2001:db8::F",
369 .ipv6_ok = true,
370 .ipv6_expected = {
371 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
372 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
375 {.input = "2001:db8::a",
376 .ipv6_ok = true,
377 .ipv6_expected = {
378 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
379 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
382 {.input = "2001:db8::b",
383 .ipv6_ok = true,
384 .ipv6_expected = {
385 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
386 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
389 {.input = "2001:db8::c",
390 .ipv6_ok = true,
391 .ipv6_expected = {
392 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
393 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
396 {.input = "2001:db8::d",
397 .ipv6_ok = true,
398 .ipv6_expected = {
399 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
400 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
403 {.input = "2001:db8::e",
404 .ipv6_ok = true,
405 .ipv6_expected = {
406 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
407 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
410 {.input = "2001:db8::f",
411 .ipv6_ok = true,
412 .ipv6_expected = {
413 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
414 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
417 {.input = "2001:db8::ff",
418 .ipv6_ok = true,
419 .ipv6_expected = {
420 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
421 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff
424 {.input = "2001:db8::ffff:2\012", },
425 {.input = "22", },
426 {.input = "2222@", },
427 {.input = "255.255.255.255",
428 .ipv4_ok = true,
429 .ipv4_expected = {255, 255, 255, 255},
431 {.input = "255.255.255.255\001", },
432 {.input = "255.255.255.25555", },
433 {.input = "2:", },
434 {.input = "2:a:8:EEEE::EEEE:F:EEE8:EEEE\034*:", },
435 {.input = "2:ff:1:1:7:ff:1:1:7.", },
436 {.input = "2f:0000000000000000000000000000000000000000000000000000000000"
437 "0000000000000000000000000000000000000000000000000000000000000000000000"
438 "0G01",
440 {.input = "429495", },
441 {.input = "5::5::", },
442 {.input = "6.6.", },
443 {.input = "992.", },
444 {.input = "::",
445 .ipv6_ok = true,
446 .ipv6_expected = {
447 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
448 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
451 {.input = "::00001", },
452 {.input = "::1",
453 .ipv6_ok = true,
454 .ipv6_expected = {
455 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
456 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
459 {.input = "::10000", },
460 {.input = "::1:1",
461 .ipv6_ok = true,
462 .ipv6_expected = {
463 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
464 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
467 {.input = "::ff:1:1:7.0.0.1",
468 .ipv6_ok = true,
469 .ipv6_expected = {
470 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff,
471 0x0, 0x1, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
474 {.input = "::ff:1:1:7:ff:1:1:7.", },
475 {.input = "::ff:1:1:7ff:1:8:7.0.0.1", },
476 {.input = "::ff:1:1:7ff:1:8f:1:1:71", },
477 {.input = "::ffff:02fff:127.0.S1", },
478 {.input = "::ffff:127.0.0.1",
479 .ipv6_ok = true,
480 .ipv6_expected = {
481 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
482 0x0, 0x0, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x1
485 {.input = "::ffff:1:7.0.0.1",
486 .ipv6_ok = true,
487 .ipv6_expected = {
488 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
489 0xff, 0xff, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
492 {.input = ":\272", },
493 {.input = "A:f:ff:1:1:D:ff:1:1::7.", },
494 {.input = "AAAAA.", },
495 {.input = "D:::", },
496 {.input = "DF8F", },
497 {.input = "F::",
498 .ipv6_ok = true,
499 .ipv6_expected = {
500 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
501 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
504 {.input = "F:A:8:EEEE:8:EEEE\034*:", },
505 {.input = "F:a:8:EEEE:8:EEEE\034*:", },
506 {.input = "F:ff:100:7ff:1:8:7.0.10.1",
507 .ipv6_ok = true,
508 .ipv6_expected = {
509 0x0, 0xf, 0x0, 0xff, 0x1, 0x0, 0x7, 0xff,
510 0x0, 0x1, 0x0, 0x8, 0x7, 0x0, 0xa, 0x1
513 {.input = "d92.", },
514 {.input = "ff:00000000000000000000000000000000000000000000000000000000000"
515 "00000000000000000000000000000000000000000000000000000000000000000001",
517 {.input = "fff2:2::ff2:2:f7",
518 .ipv6_ok = true,
519 .ipv6_expected = {
520 0xff, 0xf2, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0,
521 0x0, 0x0, 0xf, 0xf2, 0x0, 0x2, 0x0, 0xf7
524 {.input = "ffff:ff:ff:fff:ff:ff:ff:", },
525 {.input = "\272:", },
526 {NULL}
529 static int
530 do_test (void)
532 for (size_t i = 0; test_cases[i].input != NULL; ++i)
533 run_one_test (test_cases + i);
534 return 0;
537 #include <support/test-driver.c>