Kaynağa Gözat

Adds tests for UDP protcol

Signed-off-by: Martin Melik Merkumians <melik-merkumians@acin.tuwien.ac.at>
Martin Melik Merkumians 5 yıl önce
ebeveyn
işleme
b4b342aee3

+ 1 - 1
source/CMakeLists.txt

@@ -141,7 +141,7 @@ if( OpENer_TESTS )
   INCLUDE( ${OpENer_BUILDSUPPORT_DIR}/CodeCoverage.cmake )
   APPEND_COVERAGE_COMPILER_FLAGS()
   # The used CppUTest framework does not support parallel jobs
-  SETUP_TARGET_FOR_COVERAGE_LCOV(NAME ${PROJECT_NAME}_coverage EXECUTABLE OpENer_Tests EXCLUDE "tests/*" "src/ports/*/sample_application/*")
+  SETUP_TARGET_FOR_COVERAGE_LCOV(NAME ${PROJECT_NAME}_coverage EXECUTABLE OpENer_Tests EXCLUDE "tests/*" "src/ports/*/sample_application/*" "${CPPUTEST_HOME}/*")
   add_test_includes()
   add_subdirectory( tests )
 endif( OpENer_TESTS )

+ 2 - 1
source/src/ports/udp_protocol.h

@@ -24,7 +24,8 @@
 typedef uint32_t in_addr_t;
 #endif
 
-static const size_t kUdpHeaderLength = 8U; /**< UDP header length in bytes */
+#define OPENER_UDP_HEADER_LENGTH (8U)
+static const size_t kUdpHeaderLength = OPENER_UDP_HEADER_LENGTH; /**< UDP header length in bytes */
 
 /** @brief Representing the needed information for the UDP header
  *

+ 1 - 0
source/tests/OpENerTests.h

@@ -11,3 +11,4 @@ IMPORT_TEST_GROUP(CipConnectionObject);
 IMPORT_TEST_GROUP(SocketTimer);
 IMPORT_TEST_GROUP(DoublyLinkedList);
 IMPORT_TEST_GROUP(EncapsulationProtocol);
+IMPORT_TEST_GROUP(UdpProtocol);

+ 1 - 1
source/tests/ports/CMakeLists.txt

@@ -9,7 +9,7 @@ opener_common_includes()
 #######################################
 opener_platform_support("INCLUDES")
 
-set( PortsTestSrc socket_timer_tests.cpp )
+set( PortsTestSrc socket_timer_tests.cpp udp_protocol_tests.cpp)
 
 include_directories( ${SRC_DIR}/ports )
 

+ 102 - 0
source/tests/ports/udp_protocol_tests.cpp

@@ -0,0 +1,102 @@
+/*******************************************************************************
+ * Copyright (c) 2020, Rockwell Automation, Inc.
+ * All rights reserved.
+ *
+ ******************************************************************************/
+
+#include <CppUTest/TestHarness.h>
+#include <stdint.h>
+#include <string.h>
+
+extern "C" {
+#include "ciptypes.h"
+#include "udp_protocol.h"
+}
+
+TEST_GROUP(UdpProtocol) {
+
+};
+
+TEST(UdpProtocol, SetSourcePort) {
+  UDPHeader header = {0};
+  UDPHeaderSetSourcePort(&header, 10);
+  CHECK_EQUAL( 10, header.source_port );
+}
+
+TEST(UdpProtocol, GetSourcePort) {
+  UDPHeader header = {0};
+  header.source_port =  5643;
+  CHECK_EQUAL( 5643, UDPHeaderGetSourcePort(&header) );
+}
+
+TEST(UdpProtocol, SetDestinationPort) {
+  UDPHeader header = {0};
+  header.destination_port = 1640;
+  CHECK_EQUAL( 1640, header.destination_port );
+}
+
+TEST(UdpProtocol, GetDestinationPort) {
+  UDPHeader header = {0};
+  UDPHeaderSetDestinationPort(&header, 9824);
+  CHECK_EQUAL( 9824, UDPHeaderGetDestinationPort(&header) );
+}
+
+TEST(UdpProtocol, SetPacketLength) {
+  UDPHeader header = {0};
+  UDPHeaderSetPacketLength(&header, 26814);
+  CHECK_EQUAL( 26814, header.packet_length);
+}
+
+TEST(UdpProtocol, GetPacketLength) {
+  UDPHeader header = {0};
+  header.packet_length = 36521;
+  CHECK_EQUAL( 36521, UDPHeaderGetPacketLength(&header) );
+}
+
+TEST(UdpProtocol, SetChecksum) {
+  UDPHeader header = {0};
+  UDPHeaderSetChecksum(&header, 0x8eaf);
+  CHECK_EQUAL( 0x8eaf, header.checksum);
+}
+
+TEST(UdpProtocol, GetChecksum) {
+  UDPHeader header = {0};
+  header.checksum = 0xaf8e;
+  CHECK_EQUAL( 0xaf8e, UDPHeaderGetChecksum(&header) );
+}
+
+TEST(UdpProtocol, HeaderGenerate) {
+  char message[OPENER_UDP_HEADER_LENGTH] = {0};
+  UDPHeader header = {0};
+  header.source_port =  5643;
+  header.destination_port = 1640;
+  header.packet_length = 36521;
+  header.checksum = 0xaf8e;
+  UDPHeaderGenerate(&header, message);
+  CHECK_EQUAL( htons(5643), *( (uint16_t *)message ) );
+  CHECK_EQUAL( htons(1640), *( ( (uint16_t *)message ) + 1 ) );
+  CHECK_EQUAL( htons(36521), *( ( (uint16_t *)message ) + 2 ) );
+  CHECK_EQUAL( htons(0xaf8e), *( ( (uint16_t *)message ) + 3 ) );
+}
+
+TEST(UdpProtocol, CalculateChecksum) {
+  char message[OPENER_UDP_HEADER_LENGTH + 13] = {0};
+  UDPHeader header = {0};
+  header.source_port =  5643;
+  header.destination_port = 1640;
+  header.packet_length = OPENER_UDP_HEADER_LENGTH + 13;
+  header.checksum = 0;
+  UDPHeaderGenerate(&header, message);
+  for(size_t i = kUdpHeaderLength; i < OPENER_UDP_HEADER_LENGTH + 13; 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 + 13,
+                                                 source_addr,
+                                                 destination_addr);
+  CHECK_EQUAL(0x9491, checksum); // Aquired via the function under test - correctness verified via Wireshark
+}