testIcmp.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 #include "compat/cppunit.h"
13 #include "unitTestMain.h"
14 
15 #include <cppunit/TestAssert.h>
16 
17 #if USE_ICMP
18 
19 #include "icmp/Icmp.h"
20 
21 class IcmpStub : public Icmp
22 {
23 public:
24  IcmpStub() {};
25  ~IcmpStub() override {};
26  int Open() override { return 0; };
27  void Close() override {};
28 
30  void SendEcho(Ip::Address &, int, const char *, int) override {}
31 
33  void Recv(void) override {};
34 
35  /* methods to relay test data from tester to private methods being tested */
36  int testChecksum(unsigned short *ptr, int size) { return CheckSum(ptr, size); };
37  int testHops(int ttl) { return ipHops(ttl); };
38 };
39 #endif
40 
44 class TestIcmp : public CPPUNIT_NS::TestFixture
45 {
50 
51 protected:
52  void testChecksum();
53  void testHops();
54 };
55 
57 
58 void
60 {
61 #if USE_ICMP
62  IcmpStub icmp;
63  uint16_t buf[10], tmpval;
64  for (tmpval=0; tmpval < 10; ++tmpval)
65  buf[tmpval]=htons(1+tmpval);
66 
67  // NULL data
68  CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(nullptr,0));
69 
70  // NULL data with length!!
71  CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(nullptr,1));
72 
73  // data with 0 length
74  CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(buf,0));
75 
76  // data with invalid length (low)
77  CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(buf,1));
78 
79  CPPUNIT_ASSERT_EQUAL((int)htons(0xfffe), icmp.testChecksum(buf,2)); // 1
80  CPPUNIT_ASSERT_EQUAL((int)htons(0xfffe), icmp.testChecksum(buf,3));
81 
82  CPPUNIT_ASSERT_EQUAL((int)htons(0xfffc), icmp.testChecksum(buf,4)); // 1+2
83  CPPUNIT_ASSERT_EQUAL((int)htons(0xfffc), icmp.testChecksum(buf,5));
84 
85  CPPUNIT_ASSERT_EQUAL((int)htons(0xfff9), icmp.testChecksum(buf,6)); // 1+2+3
86  CPPUNIT_ASSERT_EQUAL((int)htons(0xfff9), icmp.testChecksum(buf,7));
87 
88  CPPUNIT_ASSERT_EQUAL((int)htons(0xfff5), icmp.testChecksum(buf,8)); // 1+2+3+4
89  CPPUNIT_ASSERT_EQUAL((int)htons(0xfff5), icmp.testChecksum(buf,9));
90 
91  CPPUNIT_ASSERT_EQUAL((int)htons(0xfff0), icmp.testChecksum(buf,10)); // 1+2...+5
92  CPPUNIT_ASSERT_EQUAL((int)htons(0xfff0), icmp.testChecksum(buf,11));
93 
94  CPPUNIT_ASSERT_EQUAL((int)htons(0xffea), icmp.testChecksum(buf,12)); // 1+2...+6
95  CPPUNIT_ASSERT_EQUAL((int)htons(0xffea), icmp.testChecksum(buf,13));
96 
97  CPPUNIT_ASSERT_EQUAL((int)htons(0xffe3), icmp.testChecksum(buf,14)); // 1+2...+7
98  CPPUNIT_ASSERT_EQUAL((int)htons(0xffe3), icmp.testChecksum(buf,15));
99 
100  CPPUNIT_ASSERT_EQUAL((int)htons(0xffdb), icmp.testChecksum(buf,16)); // 1+2...+8
101  CPPUNIT_ASSERT_EQUAL((int)htons(0xffdb), icmp.testChecksum(buf,17));
102 
103  CPPUNIT_ASSERT_EQUAL((int)htons(0xffd2), icmp.testChecksum(buf,18)); // 1+2...+9
104  CPPUNIT_ASSERT_EQUAL((int)htons(0xffd2), icmp.testChecksum(buf,19));
105 
106  // data with accurate length
107  CPPUNIT_ASSERT_EQUAL((int)htons(0xffc8), icmp.testChecksum(buf,20)); // 1+2...+10
108 
109  // data with invalid length (overrun) ==> Garbage checksum...
110 #endif
111 }
112 
113 void
115 {
116 #if USE_ICMP
117  IcmpStub icmp;
118 
119  /* test invalid -(under values) */
120  // negative : n > 33
121  CPPUNIT_ASSERT_EQUAL(34, icmp.testHops(-1));
122  // zero
123  CPPUNIT_ASSERT_EQUAL(33, icmp.testHops(0));
124 
125  /* test each valid case boundary */
126  // n(1...32) : 32 >= n >= 1
127  CPPUNIT_ASSERT_EQUAL(32, icmp.testHops(1));
128  CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(32));
129 
130  // n(33...62) : 30 >= n >= 1
131  CPPUNIT_ASSERT_EQUAL(30, icmp.testHops(33));
132  CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(62));
133 
134  // n(63...64) : 2 >= n >= 1
135  CPPUNIT_ASSERT_EQUAL(2, icmp.testHops(63));
136  CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(64));
137 
138  // n(65...128) : 64 >= n >= 1
139  CPPUNIT_ASSERT_EQUAL(64, icmp.testHops(65));
140  CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(128));
141 
142  // n(129...192) : 64 >= n >= 1
143  CPPUNIT_ASSERT_EQUAL(64, icmp.testHops(129));
144  CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(192));
145 
146  // n(193...) : n < 63
147  CPPUNIT_ASSERT_EQUAL(63, icmp.testHops(193));
148  CPPUNIT_ASSERT_EQUAL(1, icmp.testHops(255));
149 
150  /* test invalid (over values) */
151  // 256 - produces zero
152  CPPUNIT_ASSERT_EQUAL(0, icmp.testHops(256));
153  // 257 - produces negative hops
154  CPPUNIT_ASSERT_EQUAL(-1, icmp.testHops(257));
155 #endif
156 }
157 
158 int
159 main(int argc, char *argv[])
160 {
161  return TestProgram().run(argc, argv);
162 }
163 
int testHops(int ttl)
Definition: testIcmp.cc:37
void Recv(void) override
Handle ICMP responses.
Definition: testIcmp.cc:33
implements test program's main() function while enabling customization
Definition: unitTestMain.h:25
IcmpStub()
Definition: testIcmp.cc:24
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
void Close() override
Shutdown pinger helper and control channel.
Definition: testIcmp.cc:27
int size
Definition: ModDevPoll.cc:69
CPPUNIT_TEST_SUITE_REGISTRATION(TestIcmp)
int Open() override
Start pinger helper and initiate control channel.
Definition: testIcmp.cc:26
void testChecksum()
Definition: testIcmp.cc:59
CPPUNIT_TEST(testChecksum)
void testHops()
Definition: testIcmp.cc:114
CPPUNIT_TEST_SUITE(TestIcmp)
void SendEcho(Ip::Address &, int, const char *, int) override
Construct ECHO request.
Definition: testIcmp.cc:30
~IcmpStub() override
Definition: testIcmp.cc:25
int main(int argc, char *argv[])
Definition: testIcmp.cc:159
CPPUNIT_TEST_SUITE_END()
int testChecksum(unsigned short *ptr, int size)
Definition: testIcmp.cc:36
int CheckSum(unsigned short *ptr, int size)
Calculate a packet checksum.
Definition: Icmp.cc:38
Definition: Icmp.h:67
int ipHops(int ttl)
Definition: Icmp.cc:67

 

Introduction

Documentation

Support

Miscellaneous