1 #include <muleunit/test.h>
2 #include <NetworkFunctions.h>
4 #define itemsof(x) (sizeof(x)/sizeof(x[0]))
6 using namespace muleunit
;
8 DECLARE_SIMPLE(NetworkFunctions
)
10 TEST(NetworkFunctions
, StringIPtoUint32
)
12 unsigned char values
[] = { 0, 1, 127, 254, 255 };
13 const wxChar whitespace
[] = { wxT(' '), wxT('\t'), wxT('\n') };
14 int items
= itemsof(values
);
18 // Test a few standard IP combinations
19 for (int wl
= 0; wl
< whites
; ++wl
) {
20 for (int wr
= 0; wr
< whites
; ++wr
) {
21 for (int a
= 0; a
< items
; ++a
) {
22 for (int za
= 0; za
< zeros
; ++za
) {
23 for (int b
= 0; b
< items
; ++b
) {
24 for (int zb
= 0; zb
< zeros
; ++zb
) {
25 for (int c
= 0; c
< items
; ++c
) {
26 for (int zc
= 0; zc
< zeros
; ++zc
) {
27 for (int d
= 0; d
< items
; ++d
) {
28 for (int zd
= 0; zd
< zeros
; ++zd
) {
31 IP
<< wxString(wxT(' '), wl
)
32 << wxString(wxT('0'), za
)
35 << wxString(wxT('0'), zb
)
38 << wxString(wxT('0'), zc
)
41 << wxString(wxT('0'), zd
)
43 << wxString(wxT(' '), wr
);
47 ASSERT_TRUE(StringIPtoUint32(IP
, resultIP
));
49 uint32 expected
= (values
[d
] << 24) | (values
[c
] << 16) | (values
[b
] << 8) | values
[a
];
51 ASSERT_EQUALS(expected
, resultIP
);
68 ASSERT_FALSE(StringIPtoUint32(wxT(".2.3.4"), dummyIP
));
69 ASSERT_FALSE(StringIPtoUint32(wxT("1..3.4"), dummyIP
));
70 ASSERT_FALSE(StringIPtoUint32(wxT("1.2..4"), dummyIP
));
71 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3."), dummyIP
));
72 ASSERT_FALSE(StringIPtoUint32(wxT(".2.3."), dummyIP
));
75 ASSERT_FALSE(StringIPtoUint32(wxT(".1.2.3.4"), dummyIP
));
76 ASSERT_FALSE(StringIPtoUint32(wxT("1..2.3.4"), dummyIP
));
77 ASSERT_FALSE(StringIPtoUint32(wxT("1.2..3.4"), dummyIP
));
78 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3..4"), dummyIP
));
79 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.4."), dummyIP
));
80 ASSERT_FALSE(StringIPtoUint32(wxT(".1.2.3.4."), dummyIP
));
83 ASSERT_FALSE(StringIPtoUint32(wxT("abc"), dummyIP
));
84 ASSERT_FALSE(StringIPtoUint32(wxT("a1.1.3.4"), dummyIP
));
85 ASSERT_FALSE(StringIPtoUint32(wxT("1.1.3.4b"), dummyIP
));
86 ASSERT_FALSE(StringIPtoUint32(wxT("a.1.3.4"), dummyIP
));
87 ASSERT_FALSE(StringIPtoUint32(wxT("1.b.3.4"), dummyIP
));
88 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.c.4"), dummyIP
));
89 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.d"), dummyIP
));
90 ASSERT_FALSE(StringIPtoUint32(wxT("1.b.3.d"), dummyIP
));
93 ASSERT_FALSE(StringIPtoUint32(wxT("256.2.3.4"), dummyIP
));
94 ASSERT_FALSE(StringIPtoUint32(wxT("1.256.3.4"), dummyIP
));
95 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.256.4"), dummyIP
));
96 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.256"), dummyIP
));
97 ASSERT_FALSE(StringIPtoUint32(wxT("256.2.3.256"), dummyIP
));
100 ASSERT_FALSE(StringIPtoUint32(wxT("-1.2.3.4"), dummyIP
));
101 ASSERT_FALSE(StringIPtoUint32(wxT("1.-2.3.4"), dummyIP
));
102 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.-3.4"), dummyIP
));
103 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.-4"), dummyIP
));
105 // Whitespace between fields
106 for (unsigned i
= 0; i
< itemsof(whitespace
); ++i
) {
107 wxChar c
= whitespace
[i
];
109 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1%c.2.3.4"), c
), dummyIP
));
110 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1.%c2.3.4"), c
), dummyIP
));
111 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1.2%c.3.4"), c
), dummyIP
));
112 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1.2.%c3.4"), c
), dummyIP
));
113 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1.2.3%c.4"), c
), dummyIP
));
114 ASSERT_FALSE(StringIPtoUint32(wxString::Format(wxT("1.2.3.%c4"), c
), dummyIP
));
118 // Faar too large values (triggered overflow and became negative)
119 ASSERT_FALSE(StringIPtoUint32(wxT("2147483648.2.3.4"), dummyIP
));
120 ASSERT_FALSE(StringIPtoUint32(wxT("1.2147483648.3.4"), dummyIP
));
121 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.2147483648.4"), dummyIP
));
122 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.2147483648"), dummyIP
));
124 // Values greater than 2 ** 32 - 1 (triggered overflow and becames x - (2 ** 32 - 1))
125 ASSERT_FALSE(StringIPtoUint32(wxT("4294967296.2.3.4"), dummyIP
));
126 ASSERT_FALSE(StringIPtoUint32(wxT("1.4294967296.3.4"), dummyIP
));
127 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.4294967296.4"), dummyIP
));
128 ASSERT_FALSE(StringIPtoUint32(wxT("1.2.3.4294967296"), dummyIP
));
131 // The dummyIP value shouldn't have been changed by any of these calls
132 ASSERT_EQUALS(27u, dummyIP
);