check_memory.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, 3, (void **)&a2, &UA_TYPES[UA_TYPES_STRING]);
  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, 3, &UA_TYPES[UA_TYPES_STRING]);
  43. }
  44. END_TEST
  45. START_TEST(encodeShallYieldDecode) {
  46. // given
  47. UA_ByteString msg1, msg2;
  48. UA_UInt32 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_IDS[_i]);
  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_IDS[_i]);
  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. UA_UInt32 pos;
  78. void *obj1 = UA_new(&UA_TYPES[_i]);
  79. UA_ByteString_newMembers(&msg1, UA_calcSizeBinary(obj1, &UA_TYPES[_i]));
  80. pos = 0;
  81. UA_StatusCode retval = UA_encodeBinary(obj1, &UA_TYPES[_i], &msg1, &pos);
  82. UA_delete(obj1, &UA_TYPES[_i]);
  83. if(retval != UA_STATUSCODE_GOOD)
  84. return; // e.g. variants cannot be encoded after an init without failing (no datatype set)
  85. // when
  86. void *obj2 = UA_new(&UA_TYPES[_i]);
  87. pos = 0;
  88. msg1.length = msg1.length / 2;
  89. //fprintf(stderr,"testing %s with half buffer\n",UA_TYPES[_i].name);
  90. UA_decodeBinary(&msg1, &pos, obj2, &UA_TYPES[_i]);
  91. //then
  92. // finally
  93. //fprintf(stderr,"delete %s with half buffer\n",UA_TYPES[_i].name);
  94. UA_delete(obj2, &UA_TYPES[_i]);
  95. UA_ByteString_deleteMembers(&msg1);
  96. }
  97. END_TEST
  98. #define RANDOM_TESTS 1000
  99. START_TEST(decodeScalarBasicTypeFromRandomBufferShallSucceed) {
  100. // given
  101. void *obj1 = UA_NULL;
  102. UA_ByteString msg1;
  103. UA_Int32 retval = UA_STATUSCODE_GOOD;
  104. UA_Int32 buflen = 256;
  105. UA_ByteString_newMembers(&msg1, buflen); // fixed size
  106. #ifdef _WIN32
  107. srand(42);
  108. #else
  109. srandom(42);
  110. #endif
  111. for(int n = 0;n < RANDOM_TESTS;n++) {
  112. for(UA_Int32 i = 0;i < buflen;i++) {
  113. #ifdef _WIN32
  114. UA_UInt32 rnd;
  115. rnd = rand();
  116. msg1.data[i] = rnd;
  117. #else
  118. msg1.data[i] = (UA_Byte)random(); // when
  119. #endif
  120. }
  121. UA_UInt32 pos = 0;
  122. obj1 = UA_new(&UA_TYPES[_i]);
  123. retval |= UA_decodeBinary(&msg1, &pos, obj1, &UA_TYPES[_i]);
  124. //then
  125. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Decoding %d from random buffer", UA_TYPES_IDS[_i]);
  126. // finally
  127. UA_delete(obj1, &UA_TYPES[_i]);
  128. }
  129. UA_ByteString_deleteMembers(&msg1);
  130. }
  131. END_TEST
  132. START_TEST(decodeComplexTypeFromRandomBufferShallSurvive) {
  133. // given
  134. UA_ByteString msg1;
  135. UA_Int32 retval = UA_STATUSCODE_GOOD;
  136. UA_Int32 buflen = 256;
  137. UA_ByteString_newMembers(&msg1, buflen); // fixed size
  138. #ifdef _WIN32
  139. srand(42);
  140. #else
  141. srandom(42);
  142. #endif
  143. // when
  144. for(int n = 0;n < RANDOM_TESTS;n++) {
  145. for(UA_Int32 i = 0;i < buflen;i++) {
  146. #ifdef _WIN32
  147. UA_UInt32 rnd;
  148. rnd = rand();
  149. msg1.data[i] = rnd;
  150. #else
  151. msg1.data[i] = (UA_Byte)random(); // when
  152. #endif
  153. }
  154. UA_UInt32 pos = 0;
  155. void *obj1 = UA_new(&UA_TYPES[_i]);
  156. retval |= UA_decodeBinary(&msg1, &pos, obj1, &UA_TYPES[_i]);
  157. UA_delete(obj1, &UA_TYPES[_i]);
  158. }
  159. // finally
  160. UA_ByteString_deleteMembers(&msg1);
  161. }
  162. END_TEST
  163. int main(void) {
  164. int number_failed = 0;
  165. SRunner *sr;
  166. Suite *s = suite_create("testMemoryHandling");
  167. TCase *tc = tcase_create("Empty Objects");
  168. tcase_add_loop_test(tc, newAndEmptyObjectShallBeDeleted, UA_TYPES_BOOLEAN, UA_TYPES_EVENTNOTIFICATIONLIST);
  169. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  170. tcase_add_loop_test(tc, encodeShallYieldDecode, UA_TYPES_BOOLEAN, UA_TYPES_EVENTNOTIFICATIONLIST);
  171. suite_add_tcase(s, tc);
  172. tc = tcase_create("Truncated Buffers");
  173. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive, UA_TYPES_BOOLEAN, UA_TYPES_EVENTNOTIFICATIONLIST);
  174. suite_add_tcase(s, tc);
  175. tc = tcase_create("Fuzzing with Random Buffers");
  176. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed, UA_TYPES_BOOLEAN, UA_TYPES_GUID);
  177. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_TYPES_NODEID, UA_TYPES_EVENTNOTIFICATIONLIST);
  178. suite_add_tcase(s, tc);
  179. sr = srunner_create(s);
  180. //srunner_set_fork_status(sr, CK_NOFORK);
  181. srunner_run_all (sr, CK_NORMAL);
  182. number_failed += srunner_ntests_failed(sr);
  183. srunner_free(sr);
  184. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  185. }