cipelectronickeytest.cpp 4.5 KB

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