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>
24 #include <support/check.h>
25 #include <support/xunistd.h>
27 #include <sys/param.h>
30 /* Memory region created by next_to_fault_allocate. */
37 /* The entire allocated region. */
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
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
);
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
;
67 /* Deallocate the memory region allocated by
68 next_to_fault_allocate. */
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
, };
81 /* True if AF_INET parses successfully. */
84 /* True if AF_INET6 parses successfully. */
87 /* Expected result for AF_INET. */
88 unsigned char ipv4_expected
[4];
90 /* Expected result for AF_INET6. */
91 unsigned char ipv6_expected
[16];
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);
102 const unsigned char *expected
;
108 expected
= t
->ipv4_expected
;
113 expected
= t
->ipv6_expected
;
117 FAIL_EXIT1 ("invalid address family %d", family
);
122 support_record_failure ();
123 printf ("error: %s return value mismatch for [[%s]], family %d\n"
126 what
, t
->input
, family
, ok
, inet_ret
);
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
);
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
,
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
,
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
[] =
184 .ipv4_expected
= {0, 0, 0, 0},
188 {.input
= "0000000", },
189 {.input
= "00000000000000000", },
191 {.input
= "10.0.301.2", },
192 {.input
= "127.0.0.1",
194 .ipv4_expected
= {127, 0, 0, 1},
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",
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",
223 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0,
224 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
229 {.input
= "2001:db8:00001::f", },
230 {.input
= "2001:db8:10000::f", },
231 {.input
= "2001:db8:1234:5678:abcd:ef01:2345:67",
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",
243 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
244 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
247 {.input
= "2001:db8::00",
250 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
251 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
254 {.input
= "2001:db8::1",
257 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
258 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
261 {.input
= "2001:db8::10",
264 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
265 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10
268 {.input
= "2001:db8::19",
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",
280 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
281 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2
284 {.input
= "2001:db8::3",
287 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
288 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3
291 {.input
= "2001:db8::4",
294 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
295 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4
298 {.input
= "2001:db8::5",
301 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
302 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5
305 {.input
= "2001:db8::6",
308 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
309 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6
312 {.input
= "2001:db8::7",
315 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
316 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7
319 {.input
= "2001:db8::8",
322 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
323 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8
326 {.input
= "2001:db8::9",
329 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
330 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9
333 {.input
= "2001:db8::A",
336 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
337 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
340 {.input
= "2001:db8::B",
343 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
344 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
347 {.input
= "2001:db8::C",
350 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
351 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
354 {.input
= "2001:db8::D",
357 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
358 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
361 {.input
= "2001:db8::E",
364 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
365 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
368 {.input
= "2001:db8::F",
371 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
372 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
375 {.input
= "2001:db8::a",
378 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
379 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
382 {.input
= "2001:db8::b",
385 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
386 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
389 {.input
= "2001:db8::c",
392 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
393 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
396 {.input
= "2001:db8::d",
399 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
400 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
403 {.input
= "2001:db8::e",
406 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
407 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
410 {.input
= "2001:db8::f",
413 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
414 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
417 {.input
= "2001:db8::ff",
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", },
426 {.input
= "2222@", },
427 {.input
= "255.255.255.255",
429 .ipv4_expected
= {255, 255, 255, 255},
431 {.input
= "255.255.255.255\001", },
432 {.input
= "255.255.255.25555", },
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"
440 {.input
= "429495", },
441 {.input
= "5::5::", },
447 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
448 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
451 {.input
= "::00001", },
455 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
456 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
459 {.input
= "::10000", },
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",
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",
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",
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.", },
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",
509 0x0, 0xf, 0x0, 0xff, 0x1, 0x0, 0x7, 0xff,
510 0x0, 0x1, 0x0, 0x8, 0x7, 0x0, 0xa, 0x1
514 {.input
= "ff:00000000000000000000000000000000000000000000000000000000000"
515 "00000000000000000000000000000000000000000000000000000000000000000001",
517 {.input
= "fff2:2::ff2:2:f7",
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:", },
532 for (size_t i
= 0; test_cases
[i
].input
!= NULL
; ++i
)
533 run_one_test (test_cases
+ i
);
537 #include <support/test-driver.c>