--------------------- PatchSet 5977 Date: 2007/10/17 10:24:07 Author: amosjeffries Branch: squid3-ipv6 Tag: (none) Log: Add initial unit-tests for ICMP base class methods. Members: src/tests/testICMP.cc:1.1->1.1.2.1 src/tests/testICMP.h:1.1->1.1.2.1 --- /dev/null Thu Oct 18 00:20:10 2007 +++ squid3/src/tests/testICMP.cc Thu Oct 18 00:20:10 2007 @@ -0,0 +1,81 @@ +#include "squid.h" +//#include +#include + +#include "testICMP.h" + + +CPPUNIT_TEST_SUITE_REGISTRATION( testICMP ); + +#if USE_ICMP + +void +testICMP::testChecksum() +{ + stubICMP icmp; + short unsigned int buf[10] = {1,2,3,4,5,6,7,8,9}; + +// TODO calculate the actual checksum expected for each test case. + + // NULL data + CPPUNIT_ASSERT_EQUAL(icmp.testChecksum(NULL,0), 0); + + // NULL data with length!! + CPPUNIT_ASSERT_EQUAL(icmp.testChecksum(buf,0), 0); + + // data with 0 length + CPPUNIT_ASSERT_EQUAL(icmp.testChecksum(buf,0), 0); + + // data with invalid length (low) + CPPUNIT_ASSERT_EQUAL(icmp.testChecksum(buf,1), 0); + + // data with invalid length (max-low) + CPPUNIT_ASSERT_EQUAL(icmp.testChecksum(buf,9), 0); + + // data with accurate length + CPPUNIT_ASSERT_EQUAL(icmp.testChecksum(buf,10), 0); + + // data with invalid length (overrun) + CPPUNIT_ASSERT_EQUAL(icmp.testChecksum(buf,11), 0); +} + +void +testICMP::testHops() +{ + stubICMP icmp; + + /* test invalid -(under values) */ + // negative : n > 33 + CPPUNIT_ASSERT_EQUAL(icmp.testHops(-1),34); + // zero + CPPUNIT_ASSERT_EQUAL(icmp.testHops(0), 33); + + /* test each valid case boundary */ + // n(1...32) : 32 >= n >= 1 + CPPUNIT_ASSERT_EQUAL(icmp.testHops(1), 32); + CPPUNIT_ASSERT_EQUAL(icmp.testHops(32), 1); + + // n(33...62) : 32 >= n >= 1 + CPPUNIT_ASSERT_EQUAL(icmp.testHops(33), 32); + CPPUNIT_ASSERT_EQUAL(icmp.testHops(62), 1); + + // n(63...128) : 64 >= n >= 1 + CPPUNIT_ASSERT_EQUAL(icmp.testHops(63), 64); + CPPUNIT_ASSERT_EQUAL(icmp.testHops(128), 1); + + // n(129...192) : 64 >= n >= 1 + CPPUNIT_ASSERT_EQUAL(icmp.testHops(129), 64); + CPPUNIT_ASSERT_EQUAL(icmp.testHops(192), 1); + + // n(193...) : n < 255 + CPPUNIT_ASSERT_EQUAL(icmp.testHops(193), 64); + CPPUNIT_ASSERT_EQUAL(icmp.testHops(255), 1); + + /* test invalid (over values) */ + // 256 - produces zero + CPPUNIT_ASSERT_EQUAL(icmp.testHops(256), 0); + // 257 - produces negative hops + CPPUNIT_ASSERT_EQUAL(icmp.testHops(257), -1); +} + +#endif /* USE_ICMP */ --- /dev/null Thu Oct 18 00:20:10 2007 +++ squid3/src/tests/testICMP.h Thu Oct 18 00:20:10 2007 @@ -0,0 +1,51 @@ + +#ifndef SQUID_SRC_TEST_URL_H +#define SQUID_SRC_TEST_URL_H + +#include + +#include "ICMP.h" + +#if USE_ICMP + +class stubICMP : public ICMP +{ +public: + stubICMP() {}; + virtual ~stubICMP() {}; + virtual int Open() { return 0; }; + virtual void Close() {}; + + /// Construct ECHO request + virtual void SendEcho(IPAddress &to, int opcode, const char *payload, int len) {}; + + /// Handle ICMP responses. + virtual void Recv(void) {}; + +/* methods to relay test data from tester to private methods being tested */ + int testChecksum(unsigned short *ptr, int size) { return CheckSum(ptr,size); }; + int testHops(int ttl) { return ipHops(ttl); }; +}; + +#endif /* USE_ICMP */ + +/** + * test the ICMP base class. + */ +class testICMP : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE( testICMP ); +#if USE_ICMP + CPPUNIT_TEST( testChecksum ); + CPPUNIT_TEST( testHops ); +#endif /* USE_ICMP */ + CPPUNIT_TEST_SUITE_END(); + +protected: +#if USE_ICMP + void testChecksum(); + void testHops(); +#endif /* USE_ICMP */ +}; + +#endif