check_memory.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #define _XOPEN_SOURCE 500
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "ua_types.h"
  5. #include "ua_util.h"
  6. #include "ua_namespace_0.h"
  7. #include "check.h"
  8. START_TEST(newAndEmptyObjectShallBeDeleted) {
  9. // given
  10. void *obj = UA_TYPES[_i].new();
  11. // when
  12. #ifdef DEBUG //no print functions if not in debug mode
  13. UA_TYPES[_i].print(obj, stdout);
  14. #endif
  15. // then
  16. ck_assert_ptr_ne(obj, UA_NULL);
  17. // finally
  18. UA_TYPES[_i].delete(obj);
  19. }
  20. END_TEST
  21. START_TEST(arrayCopyShallMakeADeepCopy) {
  22. // given
  23. UA_String a1[3];
  24. a1[0] = (UA_String){1, (UA_Byte*)"a"};
  25. a1[1] = (UA_String){2, (UA_Byte*)"bb"};
  26. a1[2] = (UA_String){3, (UA_Byte*)"ccc"};
  27. // when
  28. UA_String *a2;
  29. UA_Int32 retval = UA_Array_copy((const void *)a1, 3, &UA_TYPES[UA_STRING], (void **)&a2);
  30. // then
  31. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  32. ck_assert_int_eq(a1[0].length, 1);
  33. ck_assert_int_eq(a1[1].length, 2);
  34. ck_assert_int_eq(a1[2].length, 3);
  35. ck_assert_int_eq(a1[0].length, a2[0].length);
  36. ck_assert_int_eq(a1[1].length, a2[1].length);
  37. ck_assert_int_eq(a1[2].length, a2[2].length);
  38. ck_assert_ptr_ne(a1[0].data, a2[0].data);
  39. ck_assert_ptr_ne(a1[1].data, a2[1].data);
  40. ck_assert_ptr_ne(a1[2].data, a2[2].data);
  41. ck_assert_int_eq(a1[0].data[0], a2[0].data[0]);
  42. ck_assert_int_eq(a1[1].data[0], a2[1].data[0]);
  43. ck_assert_int_eq(a1[2].data[0], a2[2].data[0]);
  44. // finally
  45. UA_Array_delete((void *)a2, 3, &UA_TYPES[UA_STRING]);
  46. }
  47. END_TEST
  48. START_TEST(encodeShallYieldDecode) {
  49. // given
  50. UA_ByteString msg1, msg2;
  51. UA_UInt32 pos = 0;
  52. void *obj1 = UA_TYPES[_i].new();
  53. UA_ByteString_newMembers(&msg1, UA_TYPES[_i].encodings[UA_ENCODING_BINARY].calcSize(obj1));
  54. UA_StatusCode retval = UA_TYPES[_i].encodings[UA_ENCODING_BINARY].encode(obj1, &msg1, &pos);
  55. if(retval != UA_STATUSCODE_GOOD) {
  56. // this happens, e.g. when we encode a variant (with UA_TYPES[UA_INVALIDTYPE] in the vtable)
  57. UA_TYPES[_i].delete(obj1);
  58. UA_ByteString_deleteMembers(&msg1);
  59. return;
  60. }
  61. // when
  62. void *obj2 = UA_TYPES[_i].new();
  63. pos = 0; retval = UA_TYPES[_i].encodings[UA_ENCODING_BINARY].decode(&msg1, &pos, obj2);
  64. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "messages differ idx=%d,name=%s", _i, UA_TYPES[_i].name);
  65. retval = UA_ByteString_newMembers(&msg2, UA_TYPES[_i].encodings[UA_ENCODING_BINARY].calcSize(obj2));
  66. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  67. pos = 0; retval = UA_TYPES[_i].encodings[UA_ENCODING_BINARY].encode(obj2, &msg2, &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,name=%s", _i, UA_TYPES[_i].name);
  71. // finally
  72. UA_TYPES[_i].delete(obj1);
  73. UA_TYPES[_i].delete(obj2);
  74. UA_ByteString_deleteMembers(&msg1);
  75. UA_ByteString_deleteMembers(&msg2);
  76. }
  77. END_TEST
  78. START_TEST(decodeShallFailWithTruncatedBufferButSurvive) {
  79. // given
  80. UA_ByteString msg1;
  81. UA_UInt32 pos;
  82. void *obj1 = UA_TYPES[_i].new();
  83. UA_ByteString_newMembers(&msg1, UA_TYPES[_i].encodings[0].calcSize(obj1));
  84. pos = 0; UA_TYPES[_i].encodings[0].encode(obj1, &msg1, &pos);
  85. UA_TYPES[_i].delete(obj1);
  86. // when
  87. void *obj2 = UA_TYPES[_i].new();
  88. pos = 0;
  89. msg1.length = msg1.length / 2;
  90. //fprintf(stderr,"testing %s with half buffer\n",UA_TYPES[_i].name);
  91. UA_TYPES[_i].encodings[0].decode(&msg1, &pos, obj2);
  92. //then
  93. // finally
  94. //fprintf(stderr,"delete %s with half buffer\n",UA_TYPES[_i].name);
  95. UA_TYPES[_i].delete(obj2);
  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. UA_UInt32 pos = 0;
  123. obj1 = UA_TYPES[_i].new();
  124. retval |= UA_TYPES[_i].encodings[0].decode(&msg1, &pos, obj1);
  125. //then
  126. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Decoding %s from random buffer", UA_TYPES[_i].name);
  127. // finally
  128. UA_TYPES[_i].delete(obj1);
  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. UA_UInt32 pos = 0;
  156. void *obj1 = UA_TYPES[_i].new();
  157. retval |= UA_TYPES[_i].encodings[0].decode(&msg1, &pos, obj1);
  158. UA_TYPES[_i].delete(obj1);
  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_BOOLEAN, UA_INVALIDTYPE-1);
  170. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  171. tcase_add_loop_test(tc, encodeShallYieldDecode, UA_BOOLEAN, UA_INVALIDTYPE-1);
  172. suite_add_tcase(s, tc);
  173. tc = tcase_create("Truncated Buffers");
  174. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive, UA_BOOLEAN, UA_INVALIDTYPE-1);
  175. suite_add_tcase(s, tc);
  176. tc = tcase_create("Fuzzing with Random Buffers");
  177. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed, UA_BOOLEAN, UA_DOUBLE);
  178. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_STRING, UA_DIAGNOSTICINFO);
  179. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_IDTYPE, UA_INVALIDTYPE);
  180. suite_add_tcase(s, tc);
  181. sr = srunner_create(s);
  182. //srunner_set_fork_status(sr, CK_NOFORK);
  183. srunner_run_all (sr, CK_NORMAL);
  184. number_failed += srunner_ntests_failed(sr);
  185. srunner_free(sr);
  186. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  187. }