]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testIcmp.cc
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
12 #include "tests/testIcmp.h"
13 #include "unitTestMain.h"
15 #include <cppunit/TestAssert.h>
17 CPPUNIT_TEST_SUITE_REGISTRATION( testIcmp
);
20 testIcmp::testChecksum()
24 uint16_t buf
[10], tmpval
;
25 for (tmpval
=0; tmpval
< 10; ++tmpval
)
26 buf
[tmpval
]=htons(1+tmpval
);
29 CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp
.testChecksum(NULL
,0));
31 // NULL data with length!!
32 CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp
.testChecksum(NULL
,1));
35 CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp
.testChecksum(buf
,0));
37 // data with invalid length (low)
38 CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp
.testChecksum(buf
,1));
40 CPPUNIT_ASSERT_EQUAL((int)htons(0xfffe), icmp
.testChecksum(buf
,2)); // 1
41 CPPUNIT_ASSERT_EQUAL((int)htons(0xfffe), icmp
.testChecksum(buf
,3));
43 CPPUNIT_ASSERT_EQUAL((int)htons(0xfffc), icmp
.testChecksum(buf
,4)); // 1+2
44 CPPUNIT_ASSERT_EQUAL((int)htons(0xfffc), icmp
.testChecksum(buf
,5));
46 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff9), icmp
.testChecksum(buf
,6)); // 1+2+3
47 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff9), icmp
.testChecksum(buf
,7));
49 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff5), icmp
.testChecksum(buf
,8)); // 1+2+3+4
50 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff5), icmp
.testChecksum(buf
,9));
52 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff0), icmp
.testChecksum(buf
,10)); // 1+2...+5
53 CPPUNIT_ASSERT_EQUAL((int)htons(0xfff0), icmp
.testChecksum(buf
,11));
55 CPPUNIT_ASSERT_EQUAL((int)htons(0xffea), icmp
.testChecksum(buf
,12)); // 1+2...+6
56 CPPUNIT_ASSERT_EQUAL((int)htons(0xffea), icmp
.testChecksum(buf
,13));
58 CPPUNIT_ASSERT_EQUAL((int)htons(0xffe3), icmp
.testChecksum(buf
,14)); // 1+2...+7
59 CPPUNIT_ASSERT_EQUAL((int)htons(0xffe3), icmp
.testChecksum(buf
,15));
61 CPPUNIT_ASSERT_EQUAL((int)htons(0xffdb), icmp
.testChecksum(buf
,16)); // 1+2...+8
62 CPPUNIT_ASSERT_EQUAL((int)htons(0xffdb), icmp
.testChecksum(buf
,17));
64 CPPUNIT_ASSERT_EQUAL((int)htons(0xffd2), icmp
.testChecksum(buf
,18)); // 1+2...+9
65 CPPUNIT_ASSERT_EQUAL((int)htons(0xffd2), icmp
.testChecksum(buf
,19));
67 // data with accurate length
68 CPPUNIT_ASSERT_EQUAL((int)htons(0xffc8), icmp
.testChecksum(buf
,20)); // 1+2...+10
70 // data with invalid length (overrun) ==> Garbage checksum...
80 /* test invalid -(under values) */
82 CPPUNIT_ASSERT_EQUAL(34, icmp
.testHops(-1));
84 CPPUNIT_ASSERT_EQUAL(33, icmp
.testHops(0));
86 /* test each valid case boundary */
87 // n(1...32) : 32 >= n >= 1
88 CPPUNIT_ASSERT_EQUAL(32, icmp
.testHops(1));
89 CPPUNIT_ASSERT_EQUAL(1, icmp
.testHops(32));
91 // n(33...62) : 30 >= n >= 1
92 CPPUNIT_ASSERT_EQUAL(30, icmp
.testHops(33));
93 CPPUNIT_ASSERT_EQUAL(1, icmp
.testHops(62));
95 // n(63...64) : 2 >= n >= 1
96 CPPUNIT_ASSERT_EQUAL(2, icmp
.testHops(63));
97 CPPUNIT_ASSERT_EQUAL(1, icmp
.testHops(64));
99 // n(65...128) : 64 >= n >= 1
100 CPPUNIT_ASSERT_EQUAL(64, icmp
.testHops(65));
101 CPPUNIT_ASSERT_EQUAL(1, icmp
.testHops(128));
103 // n(129...192) : 64 >= n >= 1
104 CPPUNIT_ASSERT_EQUAL(64, icmp
.testHops(129));
105 CPPUNIT_ASSERT_EQUAL(1, icmp
.testHops(192));
107 // n(193...) : n < 63
108 CPPUNIT_ASSERT_EQUAL(63, icmp
.testHops(193));
109 CPPUNIT_ASSERT_EQUAL(1, icmp
.testHops(255));
111 /* test invalid (over values) */
112 // 256 - produces zero
113 CPPUNIT_ASSERT_EQUAL(0, icmp
.testHops(256));
114 // 257 - produces negative hops
115 CPPUNIT_ASSERT_EQUAL(-1, icmp
.testHops(257));