cipelectronickeyformattest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*******************************************************************************
  2. * Copyright (c) 2016, 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 "cipelectronickey.h"
  11. #include "cipepath.h"
  12. }
  13. TEST_GROUP(CipElectronicKeyFormat) {
  14. ElectronicKeyFormat4* key;
  15. void setup() {
  16. key = ElectronicKeyFormat4New();
  17. }
  18. void teardown() {
  19. ElectronicKeyFormat4Delete(&key);
  20. }
  21. };
  22. TEST(CipElectronicKeyFormat, CreateElectronicKey) {
  23. CipOctet dummyArea[kElectronicKeyFormat4Size];
  24. memset(dummyArea, 0, sizeof(dummyArea));
  25. MEMCMP_EQUAL(dummyArea, key, kElectronicKeyFormat4Size);
  26. };
  27. TEST(CipElectronicKeyFormat, DeleteElectronicKey) {
  28. ElectronicKeyFormat4Delete(&key);
  29. POINTERS_EQUAL(NULL, key);
  30. }
  31. TEST(CipElectronicKeyFormat, SetVendorID) {
  32. CipOctet demoArea[kElectronicKeyFormat4Size];
  33. memset(demoArea, 0, sizeof(demoArea));
  34. CipUint* vendor_id = (CipUint*)demoArea;
  35. *vendor_id = 1;
  36. ElectronicKeyFormat4SetVendorId(key, 1);
  37. MEMCMP_EQUAL(demoArea, key, kElectronicKeyFormat4Size);
  38. }
  39. TEST(CipElectronicKeyFormat, GetVendorID) {
  40. CipUint expected_vendor_id = 1;
  41. CipUint* vendor_id_data = (CipUint*)key;
  42. *vendor_id_data = expected_vendor_id;
  43. CipUint vendor_id = ElectronicKeyFormat4GetVendorId(key);
  44. CHECK_EQUAL(expected_vendor_id, vendor_id);
  45. }
  46. TEST(CipElectronicKeyFormat, SetDeviceType) {
  47. CipOctet demoArea[kElectronicKeyFormat4Size];
  48. memset(demoArea, 0, sizeof(demoArea));
  49. CipUint* device_type = (CipUint*)demoArea + 1;
  50. *device_type = 1;
  51. ElectronicKeyFormat4SetDeviceType(key, 1);
  52. MEMCMP_EQUAL(demoArea, key, kElectronicKeyFormat4Size);
  53. }
  54. TEST(CipElectronicKeyFormat, GetDeviceType) {
  55. CipUint expected_device_type = 1;
  56. CipUint* device_type_data = (CipUint*)key + 1;
  57. *device_type_data = expected_device_type;
  58. CipUint device_type = ElectronicKeyFormat4GetDeviceType(key);
  59. CHECK_EQUAL(expected_device_type, device_type);
  60. }
  61. TEST(CipElectronicKeyFormat, SetProductCode) {
  62. CipOctet demoArea[kElectronicKeyFormat4Size];
  63. memset(demoArea, 0, sizeof(demoArea));
  64. CipUint* product_code = (CipUint*)demoArea + 2;
  65. *product_code = 1;
  66. ElectronicKeyFormat4SetProductCode(key, 1);
  67. MEMCMP_EQUAL(demoArea, key, kElectronicKeyFormat4Size);
  68. }
  69. TEST(CipElectronicKeyFormat, GetProductCode) {
  70. CipUint expected_product_code = 1;
  71. CipUint* product_code_data = (CipUint*)key + 2;
  72. *product_code_data = expected_product_code;
  73. CipUint product_code = ElectronicKeyFormat4GetProductCode(key);
  74. CHECK_EQUAL(expected_product_code, product_code);
  75. }
  76. TEST(CipElectronicKeyFormat, SetMajorRevisionCompatibility) {
  77. CipOctet demoArea[kElectronicKeyFormat4Size];
  78. memset(demoArea, 0, sizeof(demoArea));
  79. CipByte* major_revision_compatiblitiy = (CipByte*)demoArea + 6;
  80. *major_revision_compatiblitiy = 0x81;
  81. ElectronicKeyFormat4SetMajorRevisionCompatibility(key, 0x81);
  82. MEMCMP_EQUAL(demoArea, key, kElectronicKeyFormat4Size);
  83. }
  84. TEST(CipElectronicKeyFormat, GetMajorRevision) {
  85. const CipUsint expected_major_revision = 0x1;
  86. CipUsint set_major_revision = 0x1;
  87. CipByte* expected_major_data = (CipByte*)key + 6;
  88. *expected_major_data = set_major_revision;
  89. CipUint product_code = ElectronicKeyFormat4GetMajorRevision(key);
  90. CHECK_EQUAL(expected_major_revision, product_code);
  91. }
  92. TEST(CipElectronicKeyFormat, GetMajorRevisionCompatibility) {
  93. const CipUsint expected_major_revision = 0x81;
  94. CipByte* expected_major_data = (CipByte*)key + 6;
  95. *expected_major_data = expected_major_revision;
  96. bool compatibility = ElectronicKeyFormat4GetMajorRevisionCompatibility(key);
  97. CHECK_TEXT(compatibility, "Compatibility flag not working");
  98. }
  99. TEST(CipElectronicKeyFormat, SetMinorRevision) {
  100. CipOctet demoArea[kElectronicKeyFormat4Size];
  101. memset(demoArea, 0, sizeof(demoArea));
  102. CipByte* minor_revision_compatiblitiy = (CipByte*)demoArea + 7;
  103. *minor_revision_compatiblitiy = 0x81;
  104. ElectronicKeyFormat4SetMinorRevision(key, 0x81);
  105. MEMCMP_EQUAL(demoArea, key, kElectronicKeyFormat4Size);
  106. }
  107. TEST(CipElectronicKeyFormat, GetMinorRevision) {
  108. CipUsint expected_minor_revision = 0x1;
  109. CipByte* expected_minor_data = (CipByte*)key + 7;
  110. *expected_minor_data = expected_minor_revision;
  111. CipUint product_code = ElectronicKeyFormat4GetMinorRevision(key);
  112. CHECK_EQUAL(expected_minor_revision, product_code);
  113. }
  114. TEST(CipElectronicKeyFormat, ParseElectronicKeyTest) {
  115. /* Size of an electronic key is 1 + 1 + 8 (Segment, Key format, Key) */
  116. const CipOctet message[] = { 0x34, 0x04, 0x00, 0x01, 0x00,
  117. 0x02, 0x00, 0x03, 0x84, 0x05 };
  118. const CipOctet* message_buffer = message;
  119. GetElectronicKeyFormat4FromMessage(&message_buffer, key);
  120. message_buffer = message;
  121. CHECK_EQUAL(256, ElectronicKeyFormat4GetVendorId(key));
  122. CHECK_EQUAL(512, ElectronicKeyFormat4GetDeviceType(key));
  123. CHECK_EQUAL(768, ElectronicKeyFormat4GetProductCode(key));
  124. CHECK_TRUE(ElectronicKeyFormat4GetMajorRevisionCompatibility(key));
  125. CHECK_EQUAL(0x04, ElectronicKeyFormat4GetMajorRevision(key));
  126. CHECK_EQUAL(0x05, ElectronicKeyFormat4GetMinorRevision(key));
  127. MEMCMP_EQUAL(message_buffer + 2, key, 8);
  128. }
  129. // TEST(CipElectronicKeyFormat, CheckElectronicKeyWrongVendorId) {
  130. // const CipOctet message [] = "\x34\x04\x02\x00\x0c\x00\xe9\xfd\x01\x02";
  131. // const CipOctet *message_buffer = message;
  132. // EipUint16 *extended_status = 0;
  133. //
  134. // GetElectronicKeyFormat4FromMessage((const CipOctet**)&message_buffer,
  135. // key);
  136. //
  137. // CheckElectronicKeyData(4, key_data, extended_status);
  138. //
  139. // }