cipcommontests.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*******************************************************************************
  2. * Copyright (c) 2018, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #include <CppUTest/TestHarness.h>
  7. #include "CppUTestExt/MockSupport.h"
  8. #include <stdint.h>
  9. #include <string.h>
  10. extern "C" {
  11. #include "opener_api.h"
  12. #include "cipstring.h"
  13. }
  14. ENIPMessage message; /**< Test variable holds ENIP message*/
  15. TEST_GROUP(CipCommon) {
  16. void setup() {
  17. InitializeENIPMessage(&message);
  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\x02\x00\x43\x41"; // hex data
  81. request.data = data;
  82. request.request_data_size = sizeof(data) - 1;
  83. CipMessageRouterResponse response;
  84. CipString string = {};
  85. for(size_t i = 0; i < number_of_strings; i++) {
  86. if(0 != length_of_string[i]) {
  87. mock().expectOneCall("CipCalloc");
  88. mock().expectOneCall("CipFree");
  89. }
  90. DecodeCipString(&string, &request, &response);
  91. // check string + length
  92. CHECK_EQUAL(*(data + pos_in_data), string.length); // first element at current pos contains the length of the following string
  93. MEMCMP_EQUAL(data + pos_in_data + 2, string.string, string.length ); // pos_in_data + 2 bytes for length
  94. pos_in_data += length_of_string[i] + 2;
  95. }
  96. ClearCipString(&string);
  97. }
  98. TEST (CipCommon, DecodeCipShortString) {
  99. CipMessageRouterRequest request;
  100. size_t number_of_strings = 4;
  101. size_t length_of_string[] = {8,4,0,2};
  102. size_t pos_in_data = 0;
  103. const CipOctet data[] =
  104. "\x08\x4F\x44\x56\x41\x5F\x44\x55\x54\x04\x4F\x44\x56\x41\x00\x02\x43\x41"; // hex data
  105. request.data = data;
  106. request.request_data_size = sizeof(data) - 1;
  107. CipMessageRouterResponse response;
  108. CipShortString short_string = {};
  109. for(size_t i = 0; i < number_of_strings; i++) {
  110. if(0 != length_of_string[i]) {
  111. mock().expectOneCall("CipCalloc");
  112. mock().expectOneCall("CipFree");
  113. }
  114. DecodeCipShortString(&short_string, &request, &response);
  115. // check string + length
  116. CHECK_EQUAL(*(data + pos_in_data), short_string.length); // first element at current pos contains the length of the following string
  117. MEMCMP_EQUAL(data + pos_in_data + 1,
  118. short_string.string,
  119. short_string.length ); // pos_in_data + 1 byte for length
  120. pos_in_data += length_of_string[i] + 1;
  121. }
  122. ClearCipShortString(&short_string);
  123. }