1 #include <muleunit/test.h>
2 #include <NetworkFunctions.h>
3
4 #define itemsof(x) (sizeof(x)/sizeof(x[0]))
5
6 using namespace muleunit;
7
8 // Needed for Boost-enabled build
9 namespace MuleNotify {
HandleNotificationAlways(const class CMuleNotiferBase &)10 void HandleNotificationAlways(const class CMuleNotiferBase&) {}
11 };
12
13 DECLARE_SIMPLE(NetworkFunctions)
14
TEST(NetworkFunctions,StringIPtoUint32)15 TEST(NetworkFunctions, StringIPtoUint32)
16 {
17 unsigned int values[] = { 0, 1, 127, 254, 255 };
18 const wxChar whitespace[] = { wxT(' '), wxT('\t'), wxT('\n') };
19 int items = itemsof(values);
20 int whites = 2;
21 int zeros = 2;
22
23 // Test a few standard IP combinations
24 for (int wl = 0; wl < whites; ++wl) {
25 for (int wr = 0; wr < whites; ++wr) {
26 for (int a = 0; a < items; ++a) {
27 for (int za = 0; za < zeros; ++za) {
28 for (int b = 0; b < items; ++b) {
29 for (int zb = 0; zb < zeros; ++zb) {
30 for (int c = 0; c < items; ++c) {
31 for (int zc = 0; zc < zeros; ++zc) {
32 for (int d = 0; d < items; ++d) {
33 for (int zd = 0; zd < zeros; ++zd) {
34 wxString IP;
35
36 IP << wxString(wxT(' '), wl)
37 << wxString(wxT('0'), za)
38 << values[a]
39 << wxT('.')
40 << wxString(wxT('0'), zb)
41 << values[b]
42 << wxT('.')
43 << wxString(wxT('0'), zc)
44 << values[c]
45 << wxT('.')
46 << wxString(wxT('0'), zd)
47 << values[d]
48 << wxString(wxT(' '), wr);
49
50 uint32 resultIP = 17;
51
52 ASSERT_TRUE(StringIPtoUint32(IP, resultIP));
53
54 uint32 expected = (values[d] << 24) | (values[c] << 16) | (values[b] << 8) | values[a];
55
56 ASSERT_EQUALS(expected, resultIP);
57 }
58 }
59 }
60 }
61 }
62 }
63 }
64 }
65 }
66 }
67
68
69 // Test invalid IPs
70 uint32 dummyIP = 27;
71
72 // Missing fields
73 ASSERT_FALSE(StringIPtoUint32(wxT(".2.3.4"), dummyIP));
74 ASSERT_FALSE(StringIPtoUint32(wxT("1..3.4"), dummyIP));
75 ASSERT_FALSE(StringIPtoUint32(wxT("1.2..4"), dummyIP));
76 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3."), dummyIP));
77 ASSERT_FALSE(StringIPtoUint32(wxT(".2.3."), dummyIP));
78
79 // Extra dots
80 ASSERT_FALSE(StringIPtoUint32(wxT(".1.2.3.4"), dummyIP));
81 ASSERT_FALSE(StringIPtoUint32(wxT("1..2.3.4"), dummyIP));
82 ASSERT_FALSE(StringIPtoUint32(wxT("1.2..3.4"), dummyIP));
83 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3..4"), dummyIP));
84 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.4."), dummyIP));
85 ASSERT_FALSE(StringIPtoUint32(wxT(".1.2.3.4."), dummyIP));
86
87 // Garbage
88 ASSERT_FALSE(StringIPtoUint32(wxT("abc"), dummyIP));
89 ASSERT_FALSE(StringIPtoUint32(wxT("a1.1.3.4"), dummyIP));
90 ASSERT_FALSE(StringIPtoUint32(wxT("1.1.3.4b"), dummyIP));
91 ASSERT_FALSE(StringIPtoUint32(wxT("a.1.3.4"), dummyIP));
92 ASSERT_FALSE(StringIPtoUint32(wxT("1.b.3.4"), dummyIP));
93 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.c.4"), dummyIP));
94 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.d"), dummyIP));
95 ASSERT_FALSE(StringIPtoUint32(wxT("1.b.3.d"), dummyIP));
96
97 // Invalid fields
98 ASSERT_FALSE(StringIPtoUint32(wxT("256.2.3.4"), dummyIP));
99 ASSERT_FALSE(StringIPtoUint32(wxT("1.256.3.4"), dummyIP));
100 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.256.4"), dummyIP));
101 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.256"), dummyIP));
102 ASSERT_FALSE(StringIPtoUint32(wxT("256.2.3.256"), dummyIP));
103
104 // Negative fields
105 ASSERT_FALSE(StringIPtoUint32(wxT("-1.2.3.4"), dummyIP));
106 ASSERT_FALSE(StringIPtoUint32(wxT("1.-2.3.4"), dummyIP));
107 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.-3.4"), dummyIP));
108 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.-4"), dummyIP));
109
110 // Whitespace between fields
111 for (unsigned i = 0; i < itemsof(whitespace); ++i) {
112 wxChar c = whitespace[i];
113
114 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1%c.2.3.4"), c), dummyIP));
115 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1.%c2.3.4"), c), dummyIP));
116 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1.2%c.3.4"), c), dummyIP));
117 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1.2.%c3.4"), c), dummyIP));
118 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1.2.3%c.4"), c), dummyIP));
119 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1.2.3.%c4"), c), dummyIP));
120 }
121
122
123 // Faar too large values (triggered overflow and became negative)
124 ASSERT_FALSE(StringIPtoUint32(wxT("2147483648.2.3.4"), dummyIP));
125 ASSERT_FALSE(StringIPtoUint32(wxT("1.2147483648.3.4"), dummyIP));
126 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.2147483648.4"), dummyIP));
127 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.2147483648"), dummyIP));
128
129 // Values greater than 2 ** 32 - 1 (triggered overflow and becames x - (2 ** 32 - 1))
130 ASSERT_FALSE(StringIPtoUint32(wxT("4294967296.2.3.4"), dummyIP));
131 ASSERT_FALSE(StringIPtoUint32(wxT("1.4294967296.3.4"), dummyIP));
132 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.4294967296.4"), dummyIP));
133 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.4294967296"), dummyIP));
134
135
136 // The dummyIP value shouldn't have been changed by any of these calls
137 ASSERT_EQUALS(27u, dummyIP);
138 }
139
140
141 // Testing the IsGoodIP() and IsLanIP() functions
TEST(NetworkFunctions,IsGoodIP)142 TEST(NetworkFunctions, IsGoodIP)
143 {
144 struct {
145 wxString ip;
146 bool isgood;
147 bool islan;
148 } ipList[] = {
149 { wxT("0.0.0.0"), false, false },
150 { wxT("0.0.0.1"), false, false },
151 { wxT("0.0.1.0"), false, false },
152 { wxT("0.1.0.0"), false, false },
153 { wxT("1.0.0.0"), true, false },
154 { wxT("10.0.0.1"), true, true },
155 { wxT("10.0.1.0"), true, true },
156 { wxT("10.1.0.0"), true, true },
157 { wxT("14.156.39.4"), true, false },
158 { wxT("24.93.63.177"), true, false },
159 { wxT("172.15.0.0"), true, false },
160 { wxT("172.16.0.0"), true, true },
161 { wxT("172.17.0.0"), true, true },
162 { wxT("172.31.0.0"), true, true },
163 { wxT("172.32.0.0"), true, false },
164 { wxT("192.88.98.176"), true, false },
165 { wxT("192.88.99.175"), false, false },
166 { wxT("192.88.100.17"), true, false },
167 { wxT("192.167.0.0"), true, false },
168 { wxT("192.168.0.0"), true, true },
169 { wxT("192.168.255.255"), true, true },
170 { wxT("192.169.0.0"), true, false },
171 { wxT("198.17.0.0"), true, false },
172 { wxT("198.18.0.0"), false, false },
173 { wxT("198.19.0.0"), false, false },
174 { wxT("198.20.0.0"), true, false }
175 };
176
177 unsigned int ip;
178 for (unsigned int i = 0; i < itemsof(ipList); i++) {
179 ASSERT_TRUE(StringIPtoUint32(ipList[i].ip, ip));
180 ASSERT_EQUALS(ipList[i].isgood, IsGoodIP(ip, false));
181 ASSERT_EQUALS(ipList[i].islan, IsLanIP(ip));
182 ASSERT_EQUALS(ipList[i].isgood && !ipList[i].islan, IsGoodIP(ip, true));
183 }
184 }
185