1 | /* |
---|
2 | * inet_aton test suite. |
---|
3 | * |
---|
4 | * Written by Russ Allbery <rra@stanford.edu> |
---|
5 | * Copyright 2009 Board of Trustees, Leland Stanford Jr. University |
---|
6 | * Copyright (c) 2004, 2005, 2006, 2007 |
---|
7 | * by Internet Systems Consortium, Inc. ("ISC") |
---|
8 | * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
---|
9 | * 2002, 2003 by The Internet Software Consortium and Rich Salz |
---|
10 | * |
---|
11 | * See LICENSE for licensing terms. |
---|
12 | */ |
---|
13 | |
---|
14 | #include <config.h> |
---|
15 | #include <portable/system.h> |
---|
16 | #include <portable/socket.h> |
---|
17 | |
---|
18 | #include <tests/tap/basic.h> |
---|
19 | |
---|
20 | int test_inet_aton(const char *, struct in_addr *); |
---|
21 | |
---|
22 | |
---|
23 | static void |
---|
24 | test_addr(const char *string, unsigned long addr) |
---|
25 | { |
---|
26 | int success; |
---|
27 | struct in_addr in; |
---|
28 | |
---|
29 | success = test_inet_aton(string, &in); |
---|
30 | ok(success, "inet_aton on %s", string); |
---|
31 | is_hex(htonl(addr), in.s_addr, "...matches expected value"); |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | static void |
---|
36 | test_fail(const char *string) |
---|
37 | { |
---|
38 | struct in_addr in; |
---|
39 | int success; |
---|
40 | |
---|
41 | in.s_addr = htonl(0x01020304UL); |
---|
42 | success = test_inet_aton(string, &in); |
---|
43 | ok(success == 0, "inet_aton on %s fails", string); |
---|
44 | is_hex(htonl(0x01020304UL), in.s_addr, "...and leaves in unchanged"); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | int |
---|
49 | main(void) |
---|
50 | { |
---|
51 | plan(92); |
---|
52 | |
---|
53 | test_addr( "0.0.0.0", 0); |
---|
54 | test_addr( "127.0.0.000000", 0x7f000000UL); |
---|
55 | test_addr( "255.255.255.255", 0xffffffffUL); |
---|
56 | test_addr( "172.200.232.199", 0xacc8e8c7UL); |
---|
57 | test_addr( "1.2.3.4", 0x01020304UL); |
---|
58 | |
---|
59 | test_addr( "0x0.0x0.0x0.0x0", 0); |
---|
60 | test_addr("0x7f.0x000.0x0.0x00", 0x7f000000UL); |
---|
61 | test_addr("0xff.0xFf.0xFF.0xff", 0xffffffffUL); |
---|
62 | test_addr("0xAC.0xc8.0xe8.0xC7", 0xacc8e8c7UL); |
---|
63 | test_addr("0xAa.0xbB.0xCc.0xdD", 0xaabbccddUL); |
---|
64 | test_addr("0xEe.0xfF.0.0x00000", 0xeeff0000UL); |
---|
65 | test_addr("0x1.0x2.0x00003.0x4", 0x01020304UL); |
---|
66 | |
---|
67 | test_addr( "000000.00.000.00", 0); |
---|
68 | test_addr( "0177.0", 0x7f000000UL); |
---|
69 | test_addr("0377.0377.0377.0377", 0xffffffffUL); |
---|
70 | test_addr("0254.0310.0350.0307", 0xacc8e8c7UL); |
---|
71 | test_addr("00001.02.3.00000004", 0x01020304UL); |
---|
72 | |
---|
73 | test_addr( "16909060", 0x01020304UL); |
---|
74 | test_addr( "172.062164307", 0xacc8e8c7UL); |
---|
75 | test_addr( "172.0xc8.0xe8c7", 0xacc8e8c7UL); |
---|
76 | test_addr( "127.1", 0x7f000001UL); |
---|
77 | test_addr( "0xffffffff", 0xffffffffUL); |
---|
78 | test_addr( "127.0xffffff", 0x7fffffffUL); |
---|
79 | test_addr( "127.127.0xffff", 0x7f7fffffUL); |
---|
80 | |
---|
81 | test_fail( ""); |
---|
82 | test_fail( "Donald Duck!"); |
---|
83 | test_fail( "a127.0.0.1"); |
---|
84 | test_fail( "aaaabbbb"); |
---|
85 | test_fail( "0x100000000"); |
---|
86 | test_fail( "0xfffffffff"); |
---|
87 | test_fail( "127.0xfffffff"); |
---|
88 | test_fail( "127.376926742"); |
---|
89 | test_fail( "127.127.01452466"); |
---|
90 | test_fail("127.127.127.0x100"); |
---|
91 | test_fail( "256.0"); |
---|
92 | test_fail( "127.0378.127.127"); |
---|
93 | test_fail("127.127.0x100.127"); |
---|
94 | test_fail( "127.0.o.1"); |
---|
95 | test_fail( "127.127.127.127v"); |
---|
96 | test_fail( "ef.127.127.127"); |
---|
97 | test_fail( "0128.127.127.127"); |
---|
98 | test_fail( "0xeg.127"); |
---|
99 | test_fail( ".127.127"); |
---|
100 | test_fail( "127.127."); |
---|
101 | test_fail( "127..127"); |
---|
102 | test_fail( "de.ad.be.ef"); |
---|
103 | |
---|
104 | return 0; |
---|
105 | } |
---|