cipcommontests.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*******************************************************************************
  2. * Copyright (c) 2018, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #include <CppUTest/TestHarness.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include "CppUTestExt/MockSupport.h"
  10. extern "C" {
  11. #include "api/opener_api.h"
  12. #include "cipstring.h"
  13. }
  14. ENIPMessage message; /**< Test variable holds ENIP message*/
  15. TEST_GROUP(CipCommon){
  16. void setup(){ InitializeENIPMessage(&message);
  17. }
  18. }
  19. ;
  20. TEST(CipCommon, EncodeCipBool) {
  21. const CipBool value = false;
  22. EncodeCipBool(&value, &message);
  23. CHECK_EQUAL(0, *(CipBool*)message.message_buffer);
  24. POINTERS_EQUAL(message.message_buffer + 1, message.current_message_position);
  25. }
  26. TEST(CipCommon, EncodeCipByte) {
  27. const CipByte value = 173U;
  28. EncodeCipBool(&value, &message);
  29. CHECK_EQUAL(value, *(CipByte*)message.message_buffer);
  30. POINTERS_EQUAL(message.message_buffer + 1, message.current_message_position);
  31. }
  32. TEST(CipCommon, EncodeCipWord) {
  33. const CipWord value = 53678U;
  34. EncodeCipWord(&value, &message);
  35. CHECK_EQUAL(value, *(CipWord*)(message.message_buffer));
  36. POINTERS_EQUAL(message.message_buffer + 2, message.current_message_position);
  37. }
  38. TEST(CipCommon, EncodeCipDword) {
  39. const CipDword value = 5357678U;
  40. EncodeCipDword(&value, &message);
  41. CHECK_EQUAL(value, *(CipDword*)(message.message_buffer));
  42. POINTERS_EQUAL(message.message_buffer + 4, message.current_message_position);
  43. }
  44. TEST(CipCommon, EncodeCipLword) {
  45. const CipLword value = 8353457678U;
  46. EncodeCipLword(&value, &message);
  47. CHECK_EQUAL(value, *(CipLword*)(message.message_buffer));
  48. POINTERS_EQUAL(message.message_buffer + 8, message.current_message_position);
  49. }
  50. TEST(CipCommon, EncodeCipUsint) {
  51. const CipUsint value = 212U;
  52. EncodeCipBool(&value, &message);
  53. CHECK_EQUAL(value, *(CipUsint*)message.message_buffer);
  54. POINTERS_EQUAL(message.message_buffer + 1, message.current_message_position);
  55. }
  56. TEST(CipCommon, EncodeCipUint) {
  57. const CipUint value = 42568U;
  58. EncodeCipUint(&value, &message);
  59. CHECK_EQUAL(value, *(CipUint*)(message.message_buffer));
  60. POINTERS_EQUAL(message.message_buffer + 2, message.current_message_position);
  61. }
  62. TEST(CipCommon, EncodeCipUdint) {
  63. const CipUdint value = 1653245U;
  64. EncodeCipUdint(&value, &message);
  65. CHECK_EQUAL(value, *(CipUdint*)(message.message_buffer));
  66. POINTERS_EQUAL(message.message_buffer + 4, message.current_message_position);
  67. }
  68. TEST(CipCommon, EncodeCipUlint) {
  69. const CipUlint value = 5357678U;
  70. EncodeCipUlint(&value, &message);
  71. CHECK_EQUAL(value, *(CipUlint*)(message.message_buffer));
  72. POINTERS_EQUAL(message.message_buffer + 8, message.current_message_position);
  73. }
  74. TEST(CipCommon, DecodeCipString) {
  75. CipMessageRouterRequest request;
  76. size_t number_of_strings = 4;
  77. size_t length_of_string[] = { 8, 4, 0, 2 };
  78. size_t pos_in_data = 0;
  79. const CipOctet data[] =
  80. "\x08\x00\x4F\x44\x56\x41\x5F\x44\x55\x54\x04\x00\x4F\x44\x56\x41\x00\x00"
  81. "\x02\x00\x43\x41"; // hex data
  82. request.data = data;
  83. request.request_data_size = sizeof(data) - 1;
  84. CipMessageRouterResponse response;
  85. CipString string = {};
  86. for (size_t i = 0; i < number_of_strings; i++) {
  87. if (0 != length_of_string[i]) {
  88. mock().expectOneCall("CipCalloc");
  89. mock().expectOneCall("CipFree");
  90. }
  91. DecodeCipString(&string, &request, &response);
  92. // check string + length
  93. CHECK_EQUAL(*(data + pos_in_data),
  94. string.length); // first element at current pos contains the
  95. // length of the following string
  96. MEMCMP_EQUAL(data + pos_in_data + 2,
  97. string.string,
  98. string.length); // pos_in_data + 2 bytes for length
  99. pos_in_data += length_of_string[i] + 2;
  100. }
  101. ClearCipString(&string);
  102. }
  103. TEST(CipCommon, DecodeCipShortString) {
  104. CipMessageRouterRequest request;
  105. size_t number_of_strings = 4;
  106. size_t length_of_string[] = { 8, 4, 0, 2 };
  107. size_t pos_in_data = 0;
  108. const CipOctet data[] =
  109. "\x08\x4F\x44\x56\x41\x5F\x44\x55\x54\x04\x4F\x44\x56\x41\x00\x02\x43"
  110. "\x41"; // hex data
  111. request.data = data;
  112. request.request_data_size = sizeof(data) - 1;
  113. CipMessageRouterResponse response;
  114. CipShortString short_string = {};
  115. for (size_t i = 0; i < number_of_strings; i++) {
  116. if (0 != length_of_string[i]) {
  117. mock().expectOneCall("CipCalloc");
  118. mock().expectOneCall("CipFree");
  119. }
  120. DecodeCipShortString(&short_string, &request, &response);
  121. // check string + length
  122. CHECK_EQUAL(*(data + pos_in_data),
  123. short_string.length); // first element at current pos contains
  124. // the length of the following string
  125. MEMCMP_EQUAL(data + pos_in_data + 1,
  126. short_string.string,
  127. short_string.length); // pos_in_data + 1 byte for length
  128. pos_in_data += length_of_string[i] + 1;
  129. }
  130. ClearCipShortString(&short_string);
  131. }