]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/testIcmp.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / icmp / testIcmp.cc
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #define SQUID_HELPER 1
10
11 #include "squid.h"
12
13 #include <cppunit/TestAssert.h>
14
15 #include "testIcmp.h"
16
17 CPPUNIT_TEST_SUITE_REGISTRATION( testIcmp );
18
19 #if USE_ICMP
20
21 void
22 testIcmp::testChecksum()
23 {
24 stubIcmp icmp;
25 uint16_t buf[10], tmpval;
26 for (tmpval=0; tmpval < 10; ++tmpval)
27 buf[tmpval]=htons(1+tmpval);
28
29 // NULL data
30 CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(NULL,0));
31
32 // NULL data with length!!
33 CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(NULL,1));
34
35 // data with 0 length
36 CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(buf,0));
37
38 // data with invalid length (low)
39 CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(buf,1));
40
41 CPPUNIT_ASSERT_EQUAL((int)htons(0xfffe), icmp.testChecksum(buf,2)); // 1
42 CPPUNIT_ASSERT_EQUAL((int)htons(0xfffe), icmp.testChecksum(buf,3));
43
44 CPPUNIT_ASSERT_EQUAL((int)htons(0xfffc), icmp.testChecksum(buf,4)); // 1+2
45 CPPUNIT_ASSERT_EQUAL((int)htons(0xfffc), icmp.testChecksum(buf,5));
46
47 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff9), icmp.testChecksum(buf,6)); // 1+2+3
48 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff9), icmp.testChecksum(buf,7));
49
50 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff5), icmp.testChecksum(buf,8)); // 1+2+3+4
51 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff5), icmp.testChecksum(buf,9));
52
53 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff0), icmp.testChecksum(buf,10)); // 1+2...+5
54 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff0), icmp.testChecksum(buf,11));
55
56 CPPUNIT_ASSERT_EQUAL((int)htons(0xffea), icmp.testChecksum(buf,12)); // 1+2...+6
57 CPPUNIT_ASSERT_EQUAL((int)htons(0xffea), icmp.testChecksum(buf,13));
58
59 CPPUNIT_ASSERT_EQUAL((int)htons(0xffe3), icmp.testChecksum(buf,14)); // 1+2...+7
60 CPPUNIT_ASSERT_EQUAL((int)htons(0xffe3), icmp.testChecksum(buf,15));
61
62 CPPUNIT_ASSERT_EQUAL((int)htons(0xffdb), icmp.testChecksum(buf,16)); // 1+2...+8
63 CPPUNIT_ASSERT_EQUAL((int)htons(0xffdb), icmp.testChecksum(buf,17));
64
65 CPPUNIT_ASSERT_EQUAL((int)htons(0xffd2), icmp.testChecksum(buf,18)); // 1+2...+9
66 CPPUNIT_ASSERT_EQUAL((int)htons(0xffd2), icmp.testChecksum(buf,19));
67
68 // data with accurate length
69 CPPUNIT_ASSERT_EQUAL((int)htons(0xffc8), icmp.testChecksum(buf,20)); // 1+2...+10
70
71 // data with invalid length (overrun) ==> Garbage checksum...
72 }
73
74 void
75 testIcmp::testHops()
76 {
77 stubIcmp icmp;
78
79 /* test invalid -(under values) */
80 // negative : n > 33
81 CPPUNIT_ASSERT_EQUAL(34, icmp.testHops(-1));
82 // zero
83 CPPUNIT_ASSERT_EQUAL(33, icmp.testHops(0));
84
85 /* test each valid case boundary */
86 // n(1...32) : 32 >= n >= 1
87 CPPUNIT_ASSERT_EQUAL(32, icmp.testHops(1));
88 CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(32));
89
90 // n(33...62) : 30 >= n >= 1
91 CPPUNIT_ASSERT_EQUAL(30, icmp.testHops(33));
92 CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(62));
93
94 // n(63...64) : 2 >= n >= 1
95 CPPUNIT_ASSERT_EQUAL(2, icmp.testHops(63));
96 CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(64));
97
98 // n(65...128) : 64 >= n >= 1
99 CPPUNIT_ASSERT_EQUAL(64, icmp.testHops(65));
100 CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(128));
101
102 // n(129...192) : 64 >= n >= 1
103 CPPUNIT_ASSERT_EQUAL(64, icmp.testHops(129));
104 CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(192));
105
106 // n(193...) : n < 63
107 CPPUNIT_ASSERT_EQUAL(63, icmp.testHops(193));
108 CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(255));
109
110 /* test invalid (over values) */
111 // 256 - produces zero
112 CPPUNIT_ASSERT_EQUAL(0, icmp.testHops(256));
113 // 257 - produces negative hops
114 CPPUNIT_ASSERT_EQUAL(-1, icmp.testHops(257));
115 }
116
117 #endif /* USE_ICMP */
118