udp_protocol_tests.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*******************************************************************************
  2. * Copyright (c) 2020, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #include <CppUTest/TestHarness.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. extern "C" {
  10. #include "ciptypes.h"
  11. #include "udp_protocol.h"
  12. }
  13. TEST_GROUP(UdpProtocol) {
  14. };
  15. TEST(UdpProtocol, SetSourcePort) {
  16. UDPHeader header = {0};
  17. UDPHeaderSetSourcePort(&header, 10);
  18. CHECK_EQUAL( 10, header.source_port );
  19. }
  20. TEST(UdpProtocol, GetSourcePort) {
  21. UDPHeader header = {0};
  22. header.source_port = 5643;
  23. CHECK_EQUAL( 5643, UDPHeaderGetSourcePort(&header) );
  24. }
  25. TEST(UdpProtocol, SetDestinationPort) {
  26. UDPHeader header = {0};
  27. header.destination_port = 1640;
  28. CHECK_EQUAL( 1640, header.destination_port );
  29. }
  30. TEST(UdpProtocol, GetDestinationPort) {
  31. UDPHeader header = {0};
  32. UDPHeaderSetDestinationPort(&header, 9824);
  33. CHECK_EQUAL( 9824, UDPHeaderGetDestinationPort(&header) );
  34. }
  35. TEST(UdpProtocol, SetPacketLength) {
  36. UDPHeader header = {0};
  37. UDPHeaderSetPacketLength(&header, 26814);
  38. CHECK_EQUAL( 26814, header.packet_length);
  39. }
  40. TEST(UdpProtocol, GetPacketLength) {
  41. UDPHeader header = {0};
  42. header.packet_length = 36521;
  43. CHECK_EQUAL( 36521, UDPHeaderGetPacketLength(&header) );
  44. }
  45. TEST(UdpProtocol, SetChecksum) {
  46. UDPHeader header = {0};
  47. UDPHeaderSetChecksum(&header, 0x8eaf);
  48. CHECK_EQUAL( 0x8eaf, header.checksum);
  49. }
  50. TEST(UdpProtocol, GetChecksum) {
  51. UDPHeader header = {0};
  52. header.checksum = 0xaf8e;
  53. CHECK_EQUAL( 0xaf8e, UDPHeaderGetChecksum(&header) );
  54. }
  55. TEST(UdpProtocol, HeaderGenerate) {
  56. char message[OPENER_UDP_HEADER_LENGTH] = {0};
  57. UDPHeader header = {0};
  58. header.source_port = 5643;
  59. header.destination_port = 1640;
  60. header.packet_length = 36521;
  61. header.checksum = 0xaf8e;
  62. UDPHeaderGenerate(&header, message);
  63. CHECK_EQUAL( htons(5643), *( (uint16_t *)message ) );
  64. CHECK_EQUAL( htons(1640), *( ( (uint16_t *)message ) + 1 ) );
  65. CHECK_EQUAL( htons(36521), *( ( (uint16_t *)message ) + 2 ) );
  66. CHECK_EQUAL( htons(0xaf8e), *( ( (uint16_t *)message ) + 3 ) );
  67. }
  68. TEST(UdpProtocol, CalculateChecksumOddLength) {
  69. char message[OPENER_UDP_HEADER_LENGTH + 13];
  70. memset(message, 0, OPENER_UDP_HEADER_LENGTH + 13);
  71. UDPHeader header = {0};
  72. header.source_port = 5643;
  73. header.destination_port = 1640;
  74. header.packet_length = OPENER_UDP_HEADER_LENGTH + 13;
  75. header.checksum = 0;
  76. UDPHeaderGenerate(&header, message);
  77. for(size_t i = kUdpHeaderLength; i < OPENER_UDP_HEADER_LENGTH + 13; i++) {
  78. message[i] = i;
  79. }
  80. in_addr_t source_addr = 0x0A000001;
  81. in_addr_t destination_addr = 0x0A000002;
  82. uint16_t checksum = UDPHeaderCalculateChecksum(message,
  83. OPENER_UDP_HEADER_LENGTH + 13,
  84. source_addr,
  85. destination_addr);
  86. CHECK_EQUAL(0xD591, checksum); // Aquired via the function under test - correctness verified via Wireshark
  87. }
  88. TEST(UdpProtocol, CalculateChecksumEvenLength) {
  89. char message[OPENER_UDP_HEADER_LENGTH + 12];
  90. memset(message, 0, OPENER_UDP_HEADER_LENGTH + 12);
  91. UDPHeader header = {0};
  92. header.source_port = 5643;
  93. header.destination_port = 1640;
  94. header.packet_length = OPENER_UDP_HEADER_LENGTH + 12;
  95. header.checksum = 0;
  96. UDPHeaderGenerate(&header, message);
  97. for(size_t i = kUdpHeaderLength; i < OPENER_UDP_HEADER_LENGTH + 12; i++) {
  98. message[i] = i;
  99. }
  100. in_addr_t source_addr = 0x0A000001;
  101. in_addr_t destination_addr = 0x0A000002;
  102. uint16_t checksum = UDPHeaderCalculateChecksum(message,
  103. OPENER_UDP_HEADER_LENGTH + 12,
  104. source_addr,
  105. destination_addr);
  106. CHECK_EQUAL(0xEB91, checksum); // Aquired via the function under test - correctness verified via Wireshark
  107. }