1 #include <test.h>
2 
3 #include <alloc.h>
4 #include <addr_lib.h>
5 
6 
test_ParseHostPort()7 static void test_ParseHostPort()
8 {
9     char *hostname, *port;
10 
11     char test_string[64];
12 
13     // Domain name:
14     ParseHostPort(strcpy(test_string, "www.cfengine.com"), &hostname, &port);
15     assert_string_equal(hostname, "www.cfengine.com");
16     assert_int_equal(port, NULL);
17     hostname = NULL; port = NULL;
18 
19     ParseHostPort(strcpy(test_string, "www.cfengine.com:80"), &hostname, &port);
20     assert_string_equal(hostname, "www.cfengine.com");
21     assert_string_equal(port, "80");
22     hostname = NULL; port = NULL;
23 
24     ParseHostPort(strcpy(test_string, "www.cfengine.com:"), &hostname, &port);
25     assert_string_equal(hostname, "www.cfengine.com");
26     assert_int_equal(port, NULL);
27     hostname = NULL; port = NULL;
28 
29     ParseHostPort(strcpy(test_string, "localhost"), &hostname, &port);
30     assert_string_equal(hostname, "localhost");
31     assert_int_equal(port, NULL);
32     hostname = NULL; port = NULL;
33 
34     ParseHostPort(strcpy(test_string, "localhost:"), &hostname, &port);
35     assert_string_equal(hostname, "localhost");
36     assert_int_equal(port, NULL);
37     hostname = NULL; port = NULL;
38 
39     ParseHostPort(strcpy(test_string, "localhost:80"), &hostname, &port);
40     assert_string_equal(hostname, "localhost");
41     assert_string_equal(port, "80");
42     hostname = NULL; port = NULL;
43 
44     ParseHostPort(strcpy(test_string, "[localhost]"), &hostname, &port);
45     assert_string_equal(hostname, "localhost");
46     assert_int_equal(port, NULL);
47     hostname = NULL; port = NULL;
48 
49     ParseHostPort(strcpy(test_string, "[localhost]:"), &hostname, &port);
50     assert_string_equal(hostname, "localhost");
51     assert_int_equal(port, NULL);
52     hostname = NULL; port = NULL;
53 
54     ParseHostPort(strcpy(test_string, "[localhost]:80"), &hostname, &port);
55     assert_string_equal(hostname, "localhost");
56     assert_string_equal(port, "80");
57     hostname = NULL; port = NULL;
58 
59     ParseHostPort(strcpy(test_string, "[www.cfengine.com]"), &hostname, &port);
60     assert_string_equal(hostname, "www.cfengine.com");
61     assert_int_equal(port, NULL);
62     hostname = NULL; port = NULL;
63 
64     ParseHostPort(strcpy(test_string, "[www.cfengine.com]:80"), &hostname, &port);
65     assert_string_equal(hostname, "www.cfengine.com");
66     assert_string_equal(port, "80");
67     hostname = NULL; port = NULL;
68 
69     ParseHostPort(strcpy(test_string, "[www.cfengine.com]:"), &hostname, &port);
70     assert_string_equal(hostname, "www.cfengine.com");
71     assert_int_equal(port, NULL);
72     hostname = NULL; port = NULL;
73 
74     // IPv4:
75     ParseHostPort(strcpy(test_string, "1.2.3.4"), &hostname, &port);
76     assert_string_equal(hostname, "1.2.3.4");
77     assert_int_equal(port, NULL);
78     hostname = NULL; port = NULL;
79 
80     ParseHostPort(strcpy(test_string, "1.2.3.4:80"), &hostname, &port);
81     assert_string_equal(hostname, "1.2.3.4");
82     assert_string_equal(port, "80");
83     hostname = NULL; port = NULL;
84 
85     ParseHostPort(strcpy(test_string, "1.2.3.4:"), &hostname, &port);
86     assert_string_equal(hostname, "1.2.3.4");
87     assert_int_equal(port, NULL);
88     hostname = NULL; port = NULL;
89 
90     // IPv6 with square brackets:
91     ParseHostPort(strcpy(test_string, "[ffff::dd:12:34]"), &hostname, &port);
92     assert_string_equal(hostname, "ffff::dd:12:34");
93     assert_int_equal(port, NULL);
94     hostname = NULL; port = NULL;
95 
96     ParseHostPort(strcpy(test_string, "[ffff::dd:12:34]:80"), &hostname, &port);
97     assert_string_equal(hostname, "ffff::dd:12:34");
98     assert_string_equal(port, "80");
99     hostname = NULL; port = NULL;
100 
101     ParseHostPort(strcpy(test_string, "[ffff::dd:12:34]:"), &hostname, &port);
102     assert_string_equal(hostname, "ffff::dd:12:34");
103     assert_int_equal(port, NULL);
104     hostname = NULL; port = NULL;
105 
106     // IPv6 without square brackets:
107     ParseHostPort(strcpy(test_string, "ffff::dd:12:34"), &hostname, &port);
108     assert_string_equal(hostname, "ffff::dd:12:34");
109     assert_int_equal(port, NULL);
110     hostname = NULL; port = NULL;
111 
112     // IPv4 mapped IPv6 addresses:
113     ParseHostPort(strcpy(test_string, "::ffff:192.0.2.128"), &hostname, &port);
114     assert_string_equal(hostname, "::ffff:192.0.2.128");
115     assert_int_equal(port, NULL);
116     hostname = NULL; port = NULL;
117 
118     ParseHostPort(strcpy(test_string, "[::ffff:192.0.2.128]"), &hostname, &port);
119     assert_string_equal(hostname, "::ffff:192.0.2.128");
120     assert_int_equal(port, NULL);
121     hostname = NULL; port = NULL;
122 
123     /***** CORNER CASES *****/
124 
125     ParseHostPort(strcpy(test_string, ""), &hostname, &port);
126     assert_int_equal(hostname, NULL);
127     assert_int_equal(port, NULL);
128     hostname = NULL; port = NULL;
129 
130     ParseHostPort(strcpy(test_string, "[]"), &hostname, &port);
131     assert_int_equal(hostname, NULL);
132     assert_int_equal(port, NULL);
133     hostname = NULL; port = NULL;
134 
135     ParseHostPort(strcpy(test_string, "[]:"), &hostname, &port);
136     assert_int_equal(hostname, NULL);
137     assert_int_equal(port, NULL);
138     hostname = NULL; port = NULL;
139 
140     ParseHostPort(strcpy(test_string, ":"), &hostname, &port);
141     assert_int_equal(hostname, NULL);
142     assert_int_equal(port, NULL);
143     hostname = NULL; port = NULL;
144 }
145 
146 
main()147 int main()
148 {
149     PRINT_TEST_BANNER();
150     const UnitTest tests[] =
151     {
152         unit_test(test_ParseHostPort)
153     };
154 
155     return run_tests(tests);
156 }
157