check_memory.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #define _XOPEN_SOURCE 500
  2. #include <stdlib.h>
  3. #include "ua_types.h"
  4. #include "ua_types_generated.h"
  5. #include "ua_types_encoding_binary.h"
  6. #include "ua_util.h"
  7. #include "check.h"
  8. START_TEST(newAndEmptyObjectShallBeDeleted) {
  9. // given
  10. void *obj = UA_new(&UA_TYPES[_i]);
  11. // then
  12. ck_assert_ptr_ne(obj, NULL);
  13. // finally
  14. UA_delete(obj, &UA_TYPES[_i]);
  15. }
  16. END_TEST
  17. START_TEST(arrayCopyShallMakeADeepCopy) {
  18. // given
  19. UA_String a1[3];
  20. a1[0] = (UA_String){1, (UA_Byte*)"a"};
  21. a1[1] = (UA_String){2, (UA_Byte*)"bb"};
  22. a1[2] = (UA_String){3, (UA_Byte*)"ccc"};
  23. // when
  24. UA_String *a2;
  25. UA_Int32 retval = UA_Array_copy((const void *)a1, 3, (void **)&a2, &UA_TYPES[UA_TYPES_STRING]);
  26. // then
  27. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  28. ck_assert_int_eq(a1[0].length, 1);
  29. ck_assert_int_eq(a1[1].length, 2);
  30. ck_assert_int_eq(a1[2].length, 3);
  31. ck_assert_int_eq(a1[0].length, a2[0].length);
  32. ck_assert_int_eq(a1[1].length, a2[1].length);
  33. ck_assert_int_eq(a1[2].length, a2[2].length);
  34. ck_assert_ptr_ne(a1[0].data, a2[0].data);
  35. ck_assert_ptr_ne(a1[1].data, a2[1].data);
  36. ck_assert_ptr_ne(a1[2].data, a2[2].data);
  37. ck_assert_int_eq(a1[0].data[0], a2[0].data[0]);
  38. ck_assert_int_eq(a1[1].data[0], a2[1].data[0]);
  39. ck_assert_int_eq(a1[2].data[0], a2[2].data[0]);
  40. // finally
  41. UA_Array_delete((void *)a2, 3, &UA_TYPES[UA_TYPES_STRING]);
  42. }
  43. END_TEST
  44. START_TEST(encodeShallYieldDecode) {
  45. // given
  46. UA_ByteString msg1, msg2;
  47. size_t pos = 0;
  48. void *obj1 = UA_new(&UA_TYPES[_i]);
  49. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg1, 65000); // fixed buf size
  50. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  51. retval = UA_encodeBinary(obj1, &UA_TYPES[_i], &msg1, &pos);
  52. msg1.length = pos;
  53. if(retval != UA_STATUSCODE_GOOD) {
  54. UA_delete(obj1, &UA_TYPES[_i]);
  55. UA_ByteString_deleteMembers(&msg1);
  56. return;
  57. }
  58. // when
  59. void *obj2 = UA_new(&UA_TYPES[_i]);
  60. pos = 0; retval = UA_decodeBinary(&msg1, &pos, obj2, &UA_TYPES[_i]);
  61. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "could not decode idx=%d,nodeid=%i", _i, UA_TYPES[_i].typeId.identifier.numeric);
  62. ck_assert(!memcmp(obj1, obj2, UA_TYPES[_i].memSize)); // bit identical decoding
  63. assert(!memcmp(obj1, obj2, UA_TYPES[_i].memSize));
  64. retval = UA_ByteString_allocBuffer(&msg2, 65000);
  65. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  66. pos = 0; retval = UA_encodeBinary(obj2, &UA_TYPES[_i], &msg2, &pos);
  67. msg2.length = pos;
  68. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  69. // then
  70. ck_assert_msg(UA_ByteString_equal(&msg1, &msg2) == UA_TRUE, "messages differ idx=%d,nodeid=%i", _i,
  71. UA_TYPES[_i].typeId.identifier.numeric);
  72. // finally
  73. UA_delete(obj1, &UA_TYPES[_i]);
  74. UA_delete(obj2, &UA_TYPES[_i]);
  75. UA_ByteString_deleteMembers(&msg1);
  76. UA_ByteString_deleteMembers(&msg2);
  77. }
  78. END_TEST
  79. START_TEST(decodeShallFailWithTruncatedBufferButSurvive) {
  80. // given
  81. UA_ByteString msg1;
  82. void *obj1 = UA_new(&UA_TYPES[_i]);
  83. size_t pos = 0;
  84. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg1, 65000); // fixed buf size
  85. retval |= UA_encodeBinary(obj1, &UA_TYPES[_i], &msg1, &pos);
  86. UA_delete(obj1, &UA_TYPES[_i]);
  87. if(retval != UA_STATUSCODE_GOOD) {
  88. UA_ByteString_deleteMembers(&msg1);
  89. return; // e.g. variants cannot be encoded after an init without failing (no datatype set)
  90. }
  91. // when
  92. void *obj2 = UA_new(&UA_TYPES[_i]);
  93. pos = 0;
  94. msg1.length = pos / 2;
  95. //fprintf(stderr,"testing %s with half buffer\n",UA_TYPES[_i].name);
  96. retval = UA_decodeBinary(&msg1, &pos, obj2, &UA_TYPES[_i]);
  97. ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
  98. //then
  99. // finally
  100. //fprintf(stderr,"delete %s with half buffer\n",UA_TYPES[_i].name);
  101. UA_delete(obj2, &UA_TYPES[_i]);
  102. UA_ByteString_deleteMembers(&msg1);
  103. }
  104. END_TEST
  105. #define RANDOM_TESTS 1000
  106. START_TEST(decodeScalarBasicTypeFromRandomBufferShallSucceed) {
  107. // given
  108. void *obj1 = NULL;
  109. UA_ByteString msg1;
  110. UA_Int32 retval = UA_STATUSCODE_GOOD;
  111. UA_Int32 buflen = 256;
  112. retval = UA_ByteString_allocBuffer(&msg1, buflen); // fixed size
  113. #ifdef _WIN32
  114. srand(42);
  115. #else
  116. srandom(42);
  117. #endif
  118. for(int n = 0;n < RANDOM_TESTS;n++) {
  119. for(UA_Int32 i = 0;i < buflen;i++) {
  120. #ifdef _WIN32
  121. UA_UInt32 rnd;
  122. rnd = rand();
  123. msg1.data[i] = rnd;
  124. #else
  125. msg1.data[i] = (UA_Byte)random(); // when
  126. #endif
  127. }
  128. size_t pos = 0;
  129. obj1 = UA_new(&UA_TYPES[_i]);
  130. retval |= UA_decodeBinary(&msg1, &pos, obj1, &UA_TYPES[_i]);
  131. //then
  132. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Decoding %d from random buffer", UA_TYPES[_i].typeId.identifier.numeric);
  133. // finally
  134. UA_delete(obj1, &UA_TYPES[_i]);
  135. }
  136. UA_ByteString_deleteMembers(&msg1);
  137. }
  138. END_TEST
  139. START_TEST(decodeComplexTypeFromRandomBufferShallSurvive) {
  140. // given
  141. UA_ByteString msg1;
  142. UA_Int32 retval = UA_STATUSCODE_GOOD;
  143. UA_Int32 buflen = 256;
  144. retval = UA_ByteString_allocBuffer(&msg1, buflen); // fixed size
  145. #ifdef _WIN32
  146. srand(42);
  147. #else
  148. srandom(42);
  149. #endif
  150. // when
  151. for(int n = 0;n < RANDOM_TESTS;n++) {
  152. for(UA_Int32 i = 0;i < buflen;i++) {
  153. #ifdef _WIN32
  154. UA_UInt32 rnd;
  155. rnd = rand();
  156. msg1.data[i] = rnd;
  157. #else
  158. msg1.data[i] = (UA_Byte)random(); // when
  159. #endif
  160. }
  161. size_t pos = 0;
  162. void *obj1 = UA_new(&UA_TYPES[_i]);
  163. retval |= UA_decodeBinary(&msg1, &pos, obj1, &UA_TYPES[_i]);
  164. UA_delete(obj1, &UA_TYPES[_i]);
  165. }
  166. // finally
  167. UA_ByteString_deleteMembers(&msg1);
  168. }
  169. END_TEST
  170. int main(void) {
  171. int number_failed = 0;
  172. SRunner *sr;
  173. Suite *s = suite_create("testMemoryHandling");
  174. TCase *tc = tcase_create("Empty Objects");
  175. tcase_add_loop_test(tc, newAndEmptyObjectShallBeDeleted, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  176. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  177. tcase_add_loop_test(tc, encodeShallYieldDecode, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  178. suite_add_tcase(s, tc);
  179. tc = tcase_create("Truncated Buffers");
  180. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  181. suite_add_tcase(s, tc);
  182. tc = tcase_create("Fuzzing with Random Buffers");
  183. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed, UA_TYPES_BOOLEAN, UA_TYPES_DOUBLE);
  184. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_TYPES_NODEID, UA_TYPES_COUNT - 1);
  185. suite_add_tcase(s, tc);
  186. sr = srunner_create(s);
  187. srunner_set_fork_status(sr, CK_NOFORK);
  188. srunner_run_all (sr, CK_NORMAL);
  189. number_failed += srunner_ntests_failed(sr);
  190. srunner_free(sr);
  191. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  192. }