blob: 3e945f1309bd0f3f07b8298485f5c808e400ed13 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include <stdio.h>
2#include <sys/socket.h>
3#include <netinet/in.h>
4#include <arpa/inet.h>
5
6/* Note: uClibc only supports the standard notation 'a.b.c.d' */
7
8static struct tests
9{
10 const char *input;
11 int valid;
12 uint32_t result;
13} tests[] =
14{
15 { "", 0, 0 },
16 { "-1", 0, 0 },
17/*
18 { "256", 1, 0x00000100 },
19*/
20 { "256.", 0, 0 },
21 { "256a", 0, 0 },
22/*
23 { "0x100", 1, 0x00000100 },
24 { "0200.0x123456", 1, 0x80123456 },
25 { "0300.0x89123456.", 0 ,0 },
26 { "0100.-0xffff0000", 0, 0 },
27 { "0.0xffffff", 1, 0x00ffffff },
28 { "0.0x1000000", 0, 0 },
29 { "0377.16777215", 1, 0xffffffff },
30 { "0377.16777216", 0, 0 },
31 { "0x87.077777777", 1, 0x87ffffff },
32 { "0x87.0100000000", 0, 0 },
33 { "0.1.3", 1, 0x00010003 },
34*/
35 { "0.256.3", 0, 0 },
36 { "256.1.3", 0, 0 },
37/*
38 { "0.1.0x10000", 0, 0 },
39 { "0.1.0xffff", 1, 0x0001ffff },
40*/
41 { "0.1a.3", 0, 0 },
42 { "0.1.a3", 0, 0 },
43 { "1.2.3.4", 1, 0x01020304 },
44 { "0400.2.3.4", 0, 0 },
45 { "1.0x100.3.4", 0, 0 },
46 { "1.2.256.4", 0, 0 },
47 { "1.2.3.0x100", 0, 0 },
48 { "323543357756889", 0, 0 },
49 { "10.1.2.3.4", 0, 0},
50};
51
52
53int
54main (int argc, char *argv[])
55{
56 int result = 0;
57 size_t cnt;
58
59 for (cnt = 0; cnt < sizeof (tests) / sizeof (tests[0]); ++cnt)
60 {
61 struct in_addr addr;
62
63 if ((int) inet_aton (tests[cnt].input, &addr) != tests[cnt].valid)
64 {
65 if (tests[cnt].valid)
66 printf ("\"%s\" not seen as valid IP address\n", tests[cnt].input);
67 else
68 printf ("\"%s\" seen as valid IP address\n", tests[cnt].input);
69 result = 1;
70 }
71 else if (tests[cnt].valid && addr.s_addr != ntohl (tests[cnt].result))
72 {
73 printf ("\"%s\" not converted correctly: is %08x, should be %08x\n",
74 tests[cnt].input, addr.s_addr, tests[cnt].result);
75 result = 1;
76 }
77 }
78
79 return result;
80}