check_memory.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #define _XOPEN_SOURCE 500
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "ua_types.h"
  5. #include "util/ua_util.h"
  6. #include "ua_namespace_0.h"
  7. #include "check.h"
  8. START_TEST(newAndEmptyObjectShallBeDeleted) {
  9. // given
  10. UA_Int32 retval;
  11. void *obj;
  12. // when
  13. retval = UA_.types[_i].new(&obj);
  14. #ifdef DEBUG //no print functions if not in debug mode
  15. UA_.types[_i].print(obj, stdout);
  16. #endif
  17. retval |= UA_.types[_i].delete(obj);
  18. // then
  19. ck_assert_int_eq(retval, UA_SUCCESS);
  20. }
  21. END_TEST
  22. START_TEST(arrayCopyShallMakeADeepCopy) {
  23. // given
  24. UA_String a1[3];
  25. a1[0] = (UA_String){1, (UA_Byte*)"a"};
  26. a1[1] = (UA_String){2, (UA_Byte*)"bb"};
  27. a1[2] = (UA_String){3, (UA_Byte*)"ccc"};
  28. // when
  29. UA_String *a2;
  30. UA_Int32 retval = UA_Array_copy((const void *)a1, 3, &UA_.types[UA_STRING], (void **)&a2);
  31. // then
  32. ck_assert_int_eq(retval, UA_SUCCESS);
  33. ck_assert_int_eq(a1[0].length, 1);
  34. ck_assert_int_eq(a1[1].length, 2);
  35. ck_assert_int_eq(a1[2].length, 3);
  36. ck_assert_int_eq(a1[0].length, a2[0].length);
  37. ck_assert_int_eq(a1[1].length, a2[1].length);
  38. ck_assert_int_eq(a1[2].length, a2[2].length);
  39. ck_assert_ptr_ne(a1[0].data, a2[0].data);
  40. ck_assert_ptr_ne(a1[1].data, a2[1].data);
  41. ck_assert_ptr_ne(a1[2].data, a2[2].data);
  42. ck_assert_int_eq(a1[0].data[0], a2[0].data[0]);
  43. ck_assert_int_eq(a1[1].data[0], a2[1].data[0]);
  44. ck_assert_int_eq(a1[2].data[0], a2[2].data[0]);
  45. // finally
  46. UA_Array_delete((void *)a2, 3, &UA_.types[UA_STRING]);
  47. }
  48. END_TEST
  49. START_TEST(encodeShallYieldDecode) {
  50. // given
  51. void *obj1 = UA_NULL, *obj2 = UA_NULL;
  52. UA_ByteString msg1, msg2;
  53. UA_Int32 retval;
  54. UA_UInt32 pos = 0;
  55. retval = UA_.types[_i].new(&obj1);
  56. UA_ByteString_newMembers(&msg1, UA_.types[_i].encodings[UA_ENCODING_BINARY].calcSize(obj1));
  57. retval |= UA_.types[_i].encodings[UA_ENCODING_BINARY].encode(obj1, &msg1, &pos);
  58. if(retval != UA_SUCCESS) {
  59. // this happens, e.g. when we encode a variant (with UA_.types[UA_INVALIDTYPE] in the vtable)
  60. UA_.types[_i].delete(obj1);
  61. UA_ByteString_deleteMembers(&msg1);
  62. return;
  63. }
  64. // when
  65. UA_.types[_i].new(&obj2);
  66. pos = 0; retval = UA_.types[_i].encodings[UA_ENCODING_BINARY].decode(&msg1, &pos, obj2);
  67. ck_assert_msg(retval == UA_SUCCESS, "messages differ idx=%d,name=%s", _i, UA_.types[_i].name);
  68. retval = UA_ByteString_newMembers(&msg2, UA_.types[_i].encodings[UA_ENCODING_BINARY].calcSize(obj2));
  69. ck_assert_int_eq(retval, UA_SUCCESS);
  70. pos = 0; retval = UA_.types[_i].encodings[UA_ENCODING_BINARY].encode(obj2, &msg2, &pos);
  71. ck_assert_int_eq(retval, UA_SUCCESS);
  72. // then
  73. ck_assert_msg(UA_ByteString_equal(&msg1, &msg2) == 0, "messages differ idx=%d,name=%s", _i, UA_.types[_i].name);
  74. // finally
  75. UA_.types[_i].delete(obj1);
  76. UA_.types[_i].delete(obj2);
  77. UA_ByteString_deleteMembers(&msg1);
  78. UA_ByteString_deleteMembers(&msg2);
  79. }
  80. END_TEST
  81. START_TEST(decodeShallFailWithTruncatedBufferButSurvive) {
  82. // given
  83. void *obj1 = UA_NULL, *obj2 = UA_NULL;
  84. UA_ByteString msg1;
  85. UA_UInt32 pos;
  86. UA_.types[_i].new(&obj1);
  87. UA_ByteString_newMembers(&msg1, UA_.types[_i].encodings[0].calcSize(obj1));
  88. pos = 0; UA_.types[_i].encodings[0].encode(obj1, &msg1, &pos);
  89. UA_.types[_i].delete(obj1);
  90. // when
  91. UA_.types[_i].new(&obj2);
  92. pos = 0;
  93. msg1.length = msg1.length / 2;
  94. //fprintf(stderr,"testing %s with half buffer\n",UA_[_i].name);
  95. UA_.types[_i].encodings[0].decode(&msg1, &pos, obj2);
  96. //then
  97. // finally
  98. //fprintf(stderr,"delete %s with half buffer\n",UA_[_i].name);
  99. UA_.types[_i].delete(obj2);
  100. UA_ByteString_deleteMembers(&msg1);
  101. }
  102. END_TEST
  103. START_TEST(decodeScalarBasicTypeFromRandomBufferShallSucceed) {
  104. // given
  105. void *obj1 = UA_NULL;
  106. UA_ByteString msg1;
  107. UA_Int32 retval = UA_SUCCESS;
  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 < 100;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. UA_UInt32 pos = 0;
  126. retval |= UA_.types[_i].new(&obj1);
  127. retval |= UA_.types[_i].encodings[0].decode(&msg1, &pos, obj1);
  128. //then
  129. ck_assert_msg(retval == UA_SUCCESS, "Decoding %s from random buffer", UA_.types[_i].name);
  130. // finally
  131. UA_.types[_i].delete(obj1);
  132. }
  133. UA_ByteString_deleteMembers(&msg1);
  134. }
  135. END_TEST
  136. START_TEST(decodeComplexTypeFromRandomBufferShallSurvive) {
  137. // given
  138. void *obj1 = UA_NULL;
  139. UA_ByteString msg1;
  140. UA_Int32 retval = UA_SUCCESS;
  141. UA_Int32 buflen = 256;
  142. UA_ByteString_newMembers(&msg1, buflen); // fixed size
  143. #ifdef WIN32
  144. srand(42);
  145. #else
  146. srandom(42);
  147. #endif
  148. // when
  149. for(int n = 0;n < 100;n++) {
  150. for(UA_Int32 i = 0;i < buflen;i++){
  151. #ifdef WIN32
  152. UA_UInt32 rnd;
  153. rnd = rand();
  154. msg1.data[i] = rnd;
  155. #else
  156. msg1.data[i] = (UA_Byte)random(); // when
  157. #endif
  158. }
  159. UA_UInt32 pos = 0;
  160. retval |= UA_.types[_i].new(&obj1);
  161. retval |= UA_.types[_i].encodings[0].decode(&msg1, &pos, obj1);
  162. //this is allowed to fail and return UA_ERROR
  163. //ck_assert_msg(retval == UA_SUCCESS, "Decoding %s from random buffer", UA_.types[_i].name);
  164. UA_.types[_i].delete(obj1);
  165. }
  166. // finally
  167. UA_ByteString_deleteMembers(&msg1);
  168. }
  169. END_TEST
  170. int main() {
  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_BOOLEAN, UA_INVALIDTYPE-1);
  176. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  177. tcase_add_loop_test(tc, encodeShallYieldDecode, UA_BOOLEAN, UA_INVALIDTYPE-1);
  178. suite_add_tcase(s, tc);
  179. tc = tcase_create("Truncated Buffers");
  180. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive, UA_BOOLEAN, UA_INVALIDTYPE-1);
  181. suite_add_tcase(s, tc);
  182. tc = tcase_create("Fuzzing with Random Buffers");
  183. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed, UA_BOOLEAN, UA_DOUBLE);
  184. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_STRING, UA_DIAGNOSTICINFO);
  185. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_IDTYPE, UA_INVALIDTYPE);
  186. suite_add_tcase(s, tc);
  187. sr = srunner_create(s);
  188. //for debugging puposes only, will break make check
  189. //srunner_set_fork_status(sr, CK_NOFORK);
  190. srunner_run_all (sr, CK_NORMAL);
  191. number_failed += srunner_ntests_failed(sr);
  192. srunner_free(sr);
  193. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  194. }