check_memory.c 6.0 KB

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