Upstream tarball 9891
[amule.git] / unittests / tests / NetworkFunctionsTest.cpp
blob32eb143ef1add22388256a74a3f1b1d78cf30a90
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);
15 int whites = 2;
16 int zeros = 2;
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) {
29 wxString IP;
31 IP << wxString(wxT(' '), wl)
32 << wxString(wxT('0'), za)
33 << values[a]
34 << wxT('.')
35 << wxString(wxT('0'), zb)
36 << values[b]
37 << wxT('.')
38 << wxString(wxT('0'), zc)
39 << values[c]
40 << wxT('.')
41 << wxString(wxT('0'), zd)
42 << values[d]
43 << wxString(wxT(' '), wr);
45 uint32 resultIP = 17;
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);
64 // Test invalid IPs
65 uint32 dummyIP = 27;
67 // Missing fields
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));
74 // Extra dots
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));
82 // Garbage
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));
92 // Invalid fields
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));
99 // Negative fields
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);