Procházet zdrojové kódy

Adds test for even UDP length

Signed-off-by: Martin Melik Merkumians <melik-merkumians@acin.tuwien.ac.at>
Martin Melik Merkumians před 5 roky
rodič
revize
8203cb9eae

+ 3 - 1
source/src/ports/udp_protocol.c

@@ -73,9 +73,11 @@ uint16_t UDPHeaderCalculateChecksum(const void *udp_packet,
     udp_packet_words++;
   }
 
-  if (i > 0) {
+  if (i > 1) {
     checksum += (*( ( (uint8_t *)udp_packet_words ) + 1 ) << 8);
+    i--;
   }
+  OPENER_ASSERT(0 == i); /* data processed */
 
   const uint16_t *const source_addr_as_words =
     (const uint16_t *const )&source_addr;

+ 24 - 1
source/tests/ports/udp_protocol_tests.cpp

@@ -79,7 +79,7 @@ TEST(UdpProtocol, HeaderGenerate) {
   CHECK_EQUAL( htons(0xaf8e), *( ( (uint16_t *)message ) + 3 ) );
 }
 
-TEST(UdpProtocol, CalculateChecksum) {
+IGONRE_TEST(UdpProtocol, CalculateChecksumOddLength) {
   char message[OPENER_UDP_HEADER_LENGTH + 13];
   memset(message, 0, OPENER_UDP_HEADER_LENGTH + 13);
   UDPHeader header = {0};
@@ -101,3 +101,26 @@ TEST(UdpProtocol, CalculateChecksum) {
                                                  destination_addr);
   CHECK_EQUAL(0x9491, checksum); // Aquired via the function under test - correctness verified via Wireshark
 }
+
+TEST(UdpProtocol, CalculateChecksumEvenLength) {
+  char message[OPENER_UDP_HEADER_LENGTH + 12];
+  memset(message, 0, OPENER_UDP_HEADER_LENGTH + 12);
+  UDPHeader header = {0};
+  header.source_port =  5643;
+  header.destination_port = 1640;
+  header.packet_length = OPENER_UDP_HEADER_LENGTH + 12;
+  header.checksum = 0;
+  UDPHeaderGenerate(&header, message);
+  for(size_t i = kUdpHeaderLength; i < OPENER_UDP_HEADER_LENGTH + 12; i++) {
+    message[i] = i;
+  }
+
+  in_addr_t source_addr = 0x0A000001;
+  in_addr_t destination_addr = 0x0A000002;
+
+  uint16_t checksum = UDPHeaderCalculateChecksum(message,
+                                                 OPENER_UDP_HEADER_LENGTH + 12,
+                                                 source_addr,
+                                                 destination_addr);
+  CHECK_EQUAL(0xEB91, checksum); // Aquired via the function under test - correctness verified via Wireshark
+}