]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Correct ICMP Checksum unit-tests.
authorAmos Jeffries <squid3@treenet.co.nz>
Fri, 20 Nov 2009 04:59:46 +0000 (17:59 +1300)
committerAmos Jeffries <squid3@treenet.co.nz>
Fri, 20 Nov 2009 04:59:46 +0000 (17:59 +1300)
 * Correct bitmaps to network-order for Endian handling.
 * Fix mishap confusing array offset with bitmap bytes.

src/icmp/Icmp.cc
src/icmp/testIcmp.cc

index 102c6de3b3c969ca420cc4d74897117ad781865d..c660197fb5ed6fbb7544f78dc206273834819292 100644 (file)
@@ -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;
 
index 0e5c129612256edc8a7cac243440065427ad3d4b..970be9eb10b8f344f58ba85999e064ae7a52dc5b 100644 (file)
@@ -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