1 /* Test inet_pton functions.
2 Copyright (C) 2017-2018 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/next_to_fault.h>
26 #include <support/xunistd.h>
34 /* True if AF_INET parses successfully. */
37 /* True if AF_INET6 parses successfully. */
40 /* Expected result for AF_INET. */
41 unsigned char ipv4_expected
[4];
43 /* Expected result for AF_INET6. */
44 unsigned char ipv6_expected
[16];
48 check_result (const char *what
, const struct test_case
*t
, int family
,
49 void *result_buffer
, int inet_ret
)
51 TEST_VERIFY_EXIT (inet_ret
>= -1);
52 TEST_VERIFY_EXIT (inet_ret
<= 1);
55 const unsigned char *expected
;
61 expected
= t
->ipv4_expected
;
66 expected
= t
->ipv6_expected
;
70 FAIL_EXIT1 ("invalid address family %d", family
);
75 support_record_failure ();
76 printf ("error: %s return value mismatch for [[%s]], family %d\n"
79 what
, t
->input
, family
, ok
, inet_ret
);
82 if (memcmp (result_buffer
, expected
, result_size
) != 0)
84 support_record_failure ();
85 printf ("error: %s result mismatch for [[%s]], family %d\n",
86 what
, t
->input
, family
);
91 run_one_test (const struct test_case
*t
)
93 size_t test_len
= strlen (t
->input
);
95 struct support_next_to_fault ntf_out4
= support_next_to_fault_allocate (4);
96 struct support_next_to_fault ntf_out6
= support_next_to_fault_allocate (16);
98 /* inet_pton requires NUL termination. */
100 struct support_next_to_fault ntf_in
101 = support_next_to_fault_allocate (test_len
+ 1);
102 memcpy (ntf_in
.buffer
, t
->input
, test_len
+ 1);
103 memset (ntf_out4
.buffer
, 0, 4);
104 check_result ("inet_pton", t
, AF_INET
, ntf_out4
.buffer
,
105 inet_pton (AF_INET
, ntf_in
.buffer
, ntf_out4
.buffer
));
106 memset (ntf_out6
.buffer
, 0, 16);
107 check_result ("inet_pton", t
, AF_INET6
, ntf_out6
.buffer
,
108 inet_pton (AF_INET6
, ntf_in
.buffer
, ntf_out6
.buffer
));
109 support_next_to_fault_free (&ntf_in
);
112 /* __inet_pton_length does not require NUL termination. */
114 struct support_next_to_fault ntf_in
115 = support_next_to_fault_allocate (test_len
);
116 memcpy (ntf_in
.buffer
, t
->input
, test_len
);
117 memset (ntf_out4
.buffer
, 0, 4);
118 check_result ("__inet_pton_length", t
, AF_INET
, ntf_out4
.buffer
,
119 __inet_pton_length (AF_INET
, ntf_in
.buffer
, ntf_in
.length
,
121 memset (ntf_out6
.buffer
, 0, 16);
122 check_result ("__inet_pton_length", t
, AF_INET6
, ntf_out6
.buffer
,
123 __inet_pton_length (AF_INET6
, ntf_in
.buffer
, ntf_in
.length
,
125 support_next_to_fault_free (&ntf_in
);
128 support_next_to_fault_free (&ntf_out4
);
129 support_next_to_fault_free (&ntf_out6
);
132 /* The test cases were manually crafted and the set enhanced with
133 American Fuzzy Lop. */
134 const struct test_case test_cases
[] =
139 .ipv4_expected
= {0, 0, 0, 0},
143 {.input
= "0000000", },
144 {.input
= "00000000000000000", },
146 {.input
= "10.0.301.2", },
147 {.input
= "127.0.0.1",
149 .ipv4_expected
= {127, 0, 0, 1},
152 {.input
= "192.0.2.-1", },
153 {.input
= "192.0.2.01", },
154 {.input
= "192.0.2.1.", },
155 {.input
= "192.0.2.1192.", },
156 {.input
= "192.0.2.192.\377..", },
157 {.input
= "192.0.2.256", },
158 {.input
= "192.0.2.27",
160 .ipv4_expected
= {192, 0, 2, 27},
162 {.input
= "192.0.201.", },
163 {.input
= "192.0.261.", },
164 {.input
= "192.0.2\256", },
165 {.input
= "192.0.\262.", },
166 {.input
= "192.062.", },
167 {.input
= "192.092.\256", },
168 {.input
= "192.0\2562.", },
169 {.input
= "192.192.0.2661\031", },
170 {.input
= "192.192.00n2.1.", },
171 {.input
= "192.192.2.190.", },
172 {.input
= "192.255.255.2555", },
173 {.input
= "192.92.219\023.", },
174 {.input
= "192.\260.2.", },
175 {.input
= "1:1::1:1",
178 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0,
179 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
184 {.input
= "2001:db8:00001::f", },
185 {.input
= "2001:db8:10000::f", },
186 {.input
= "2001:db8:1234:5678:abcd:ef01:2345:67",
189 0x20, 0x1, 0xd, 0xb8, 0x12, 0x34, 0x56, 0x78,
190 0xab, 0xcd, 0xef, 0x1, 0x23, 0x45, 0x0, 0x67
193 {.input
= "2001:db8:1234:5678:abcd:ef01:2345:6789:1", },
194 {.input
= "2001:db8:1234:5678:abcd:ef01:2345::6789", },
195 {.input
= "2001:db8::0",
198 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
199 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
202 {.input
= "2001:db8::00",
205 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
206 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
209 {.input
= "2001:db8::1",
212 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
213 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
216 {.input
= "2001:db8::10",
219 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
220 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10
223 {.input
= "2001:db8::19",
226 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
227 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19
230 {.input
= "2001:db8::1::\012", },
231 {.input
= "2001:db8::1::2\012", },
232 {.input
= "2001:db8::2",
235 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
236 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2
239 {.input
= "2001:db8::3",
242 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
243 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3
246 {.input
= "2001:db8::4",
249 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
250 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4
253 {.input
= "2001:db8::5",
256 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
257 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5
260 {.input
= "2001:db8::6",
263 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
264 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6
267 {.input
= "2001:db8::7",
270 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
271 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7
274 {.input
= "2001:db8::8",
277 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
278 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8
281 {.input
= "2001:db8::9",
284 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
285 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9
288 {.input
= "2001:db8::A",
291 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
292 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
295 {.input
= "2001:db8::B",
298 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
299 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
302 {.input
= "2001:db8::C",
305 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
306 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
309 {.input
= "2001:db8::D",
312 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
313 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
316 {.input
= "2001:db8::E",
319 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
320 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
323 {.input
= "2001:db8::F",
326 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
327 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
330 {.input
= "2001:db8::a",
333 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
334 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
337 {.input
= "2001:db8::b",
340 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
341 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
344 {.input
= "2001:db8::c",
347 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
348 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
351 {.input
= "2001:db8::d",
354 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
355 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
358 {.input
= "2001:db8::e",
361 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
362 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
365 {.input
= "2001:db8::f",
368 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
369 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
372 {.input
= "2001:db8::ff",
375 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
376 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff
379 {.input
= "2001:db8::ffff:2\012", },
381 {.input
= "2222@", },
382 {.input
= "255.255.255.255",
384 .ipv4_expected
= {255, 255, 255, 255},
386 {.input
= "255.255.255.255\001", },
387 {.input
= "255.255.255.25555", },
389 {.input
= "2:a:8:EEEE::EEEE:F:EEE8:EEEE\034*:", },
390 {.input
= "2:ff:1:1:7:ff:1:1:7.", },
391 {.input
= "2f:0000000000000000000000000000000000000000000000000000000000"
392 "0000000000000000000000000000000000000000000000000000000000000000000000"
395 {.input
= "429495", },
396 {.input
= "5::5::", },
402 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
403 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
406 {.input
= "::00001", },
410 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
411 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
414 {.input
= "::10000", },
418 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
419 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
422 {.input
= "::ff:1:1:7.0.0.1",
425 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff,
426 0x0, 0x1, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
429 {.input
= "::ff:1:1:7:ff:1:1:7.", },
430 {.input
= "::ff:1:1:7ff:1:8:7.0.0.1", },
431 {.input
= "::ff:1:1:7ff:1:8f:1:1:71", },
432 {.input
= "::ffff:02fff:127.0.S1", },
433 {.input
= "::ffff:127.0.0.1",
436 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
437 0x0, 0x0, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x1
440 {.input
= "::ffff:1:7.0.0.1",
443 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
444 0xff, 0xff, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
447 {.input
= ":\272", },
448 {.input
= "A:f:ff:1:1:D:ff:1:1::7.", },
449 {.input
= "AAAAA.", },
455 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
456 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
459 {.input
= "F:A:8:EEEE:8:EEEE\034*:", },
460 {.input
= "F:a:8:EEEE:8:EEEE\034*:", },
461 {.input
= "F:ff:100:7ff:1:8:7.0.10.1",
464 0x0, 0xf, 0x0, 0xff, 0x1, 0x0, 0x7, 0xff,
465 0x0, 0x1, 0x0, 0x8, 0x7, 0x0, 0xa, 0x1
469 {.input
= "ff:00000000000000000000000000000000000000000000000000000000000"
470 "00000000000000000000000000000000000000000000000000000000000000000001",
472 {.input
= "fff2:2::ff2:2:f7",
475 0xff, 0xf2, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0,
476 0x0, 0x0, 0xf, 0xf2, 0x0, 0x2, 0x0, 0xf7
479 {.input
= "ffff:ff:ff:fff:ff:ff:ff:", },
480 {.input
= "\272:", },
487 for (size_t i
= 0; test_cases
[i
].input
!= NULL
; ++i
)
488 run_one_test (test_cases
+ i
);
492 #include <support/test-driver.c>