1 /* Test legacy IPv4 text-to-address function inet_aton.
2 Copyright (C) 1998-2019 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 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
26 static const struct tests
35 { "256", 1, 0x00000100 },
39 { "0x100", 1, 0x00000100 },
40 { "0200.0x123456", 1, 0x80123456 },
41 { "0300.0x89123456.", 0 ,0 },
42 { "0100.-0xffff0000", 0, 0 },
43 { "0.0xffffff", 1, 0x00ffffff },
44 { "0.0x1000000", 0, 0 },
45 { "0377.16777215", 1, 0xffffffff },
46 { "0377.16777216", 0, 0 },
47 { "0x87.077777777", 1, 0x87ffffff },
48 { "0x87.0100000000", 0, 0 },
49 { "0.1.3", 1, 0x00010003 },
52 { "0.1.0x10000", 0, 0 },
53 { "0.1.0xffff", 1, 0x0001ffff },
56 { "1.2.3.4", 1, 0x01020304 },
57 { "0400.2.3.4", 0, 0 },
58 { "1.0x100.3.4", 0, 0 },
59 { "1.2.256.4", 0, 0 },
60 { "1.2.3.0x100", 0, 0 },
61 { "323543357756889", 0, 0 },
62 { "10.1.2.3.4", 0, 0 },
63 { "192.0.2.1", 1, 0xc0000201 },
64 { "192.0.2.2\nX", 1, 0xc0000202 },
65 { "192.0.2.3 Y", 1, 0xc0000203 },
66 { "192.0.2.3Z", 0, 0 },
67 { "192.000.002.010", 1, 0xc0000208 },
77 for (cnt
= 0; cnt
< array_length (tests
); ++cnt
)
81 if ((int) inet_aton (tests
[cnt
].input
, &addr
) != tests
[cnt
].valid
)
84 printf ("\"%s\" not seen as valid IP address\n", tests
[cnt
].input
);
86 printf ("\"%s\" seen as valid IP address\n", tests
[cnt
].input
);
89 else if (tests
[cnt
].valid
&& addr
.s_addr
!= ntohl (tests
[cnt
].result
))
91 printf ("\"%s\" not converted correctly: is %08x, should be %08x\n",
92 tests
[cnt
].input
, addr
.s_addr
, tests
[cnt
].result
);
100 #include <support/test-driver.c>