check_memory.c 6.1 KB

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