From 4d6662d96be41d811946ad82bfc23c5cf2022e48 Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Fri, 20 Nov 2009 17:59:46 +1300 Subject: [PATCH] Correct ICMP Checksum unit-tests. * Correct bitmaps to network-order for Endian handling. * Fix mishap confusing array offset with bitmap bytes. --- src/icmp/Icmp.cc | 2 +- src/icmp/testIcmp.cc | 45 +++++++++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/icmp/Icmp.cc b/src/icmp/Icmp.cc index 102c6de3b3..c660197fb5 100644 --- a/src/icmp/Icmp.cc +++ b/src/icmp/Icmp.cc @@ -64,7 +64,7 @@ Icmp::CheckSum(unsigned short *ptr, int size) unsigned short oddbyte; unsigned short answer; - if (!ptr) return 65535; // bad input. + if (!ptr) return (int)htons(0xffff); // bad input. sum = 0; diff --git a/src/icmp/testIcmp.cc b/src/icmp/testIcmp.cc index 0e5c129612..970be9eb10 100644 --- a/src/icmp/testIcmp.cc +++ b/src/icmp/testIcmp.cc @@ -15,28 +15,51 @@ void testIcmp::testChecksum() { stubIcmp icmp; - short unsigned int buf[10] = {1,2,3,4,5,6,7,8,9}; + short unsigned int buf[10] = {htons(1),htons(2),htons(3),htons(4),htons(5),htons(6),htons(7),htons(8),htons(9), htons(10)}; // NULL data - CPPUNIT_ASSERT_EQUAL(65535, icmp.testChecksum(NULL,0)); + CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(NULL,0)); // NULL data with length!! - CPPUNIT_ASSERT_EQUAL(65535, icmp.testChecksum(NULL,1)); + CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(NULL,1)); // data with 0 length - CPPUNIT_ASSERT_EQUAL(65535, icmp.testChecksum(buf,0)); + CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(buf,0)); // data with invalid length (low) - CPPUNIT_ASSERT_EQUAL(65534, icmp.testChecksum(buf,1)); + CPPUNIT_ASSERT_EQUAL((int)htons(0xffff), icmp.testChecksum(buf,1)); - // data with invalid length (max-low) - CPPUNIT_ASSERT_EQUAL(65520, icmp.testChecksum(buf,9)); + CPPUNIT_ASSERT_EQUAL((int)htons(0xfffe), icmp.testChecksum(buf,2)); // 1 + CPPUNIT_ASSERT_EQUAL((int)htons(0xfffe), icmp.testChecksum(buf,3)); + + CPPUNIT_ASSERT_EQUAL((int)htons(0xfffc), icmp.testChecksum(buf,4)); // 1+2 + CPPUNIT_ASSERT_EQUAL((int)htons(0xfffc), icmp.testChecksum(buf,5)); - // data with accurate length - CPPUNIT_ASSERT_EQUAL(65520, icmp.testChecksum(buf,10)); + CPPUNIT_ASSERT_EQUAL((int)htons(0xfff9), icmp.testChecksum(buf,6)); // 1+2+3 + CPPUNIT_ASSERT_EQUAL((int)htons(0xfff9), icmp.testChecksum(buf,7)); - // data with invalid length (overrun) - CPPUNIT_ASSERT_EQUAL(65514, icmp.testChecksum(buf,11)); + CPPUNIT_ASSERT_EQUAL((int)htons(0xfff5), icmp.testChecksum(buf,8)); // 1+2+3+4 + CPPUNIT_ASSERT_EQUAL((int)htons(0xfff5), icmp.testChecksum(buf,9)); + + CPPUNIT_ASSERT_EQUAL((int)htons(0xfff0), icmp.testChecksum(buf,10)); // 1+2...+5 + CPPUNIT_ASSERT_EQUAL((int)htons(0xfff0), icmp.testChecksum(buf,11)); + + CPPUNIT_ASSERT_EQUAL((int)htons(0xffea), icmp.testChecksum(buf,12)); // 1+2...+6 + CPPUNIT_ASSERT_EQUAL((int)htons(0xffea), icmp.testChecksum(buf,13)); + + CPPUNIT_ASSERT_EQUAL((int)htons(0xffe3), icmp.testChecksum(buf,14)); // 1+2...+7 + CPPUNIT_ASSERT_EQUAL((int)htons(0xffe3), icmp.testChecksum(buf,15)); + + CPPUNIT_ASSERT_EQUAL((int)htons(0xffdb), icmp.testChecksum(buf,16)); // 1+2...+8 + CPPUNIT_ASSERT_EQUAL((int)htons(0xffdb), icmp.testChecksum(buf,17)); + + CPPUNIT_ASSERT_EQUAL((int)htons(0xffd2), icmp.testChecksum(buf,18)); // 1+2...+9 + CPPUNIT_ASSERT_EQUAL((int)htons(0xffd2), icmp.testChecksum(buf,19)); + + // data with accurate length + CPPUNIT_ASSERT_EQUAL((int)htons(0xffc8), icmp.testChecksum(buf,20)); // 1+2...+10 + + // data with invalid length (overrun) ==> Garbage checksum... } void -- 2.47.2