check_memory.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. UA_Int32 retval;
  11. void *obj;
  12. // when
  13. retval = UA_[_i].new(&obj);
  14. #ifdef DEBUG //no print functions if not in debug mode
  15. UA_[_i].print(obj, stdout);
  16. #endif
  17. UA_[_i].delete(obj);
  18. // then
  19. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  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_[UA_STRING], (void **)&a2);
  31. // then
  32. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  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_[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_[_i].new(&obj1);
  56. UA_ByteString_newMembers(&msg1, UA_[_i].encodings[UA_ENCODING_BINARY].calcSize(obj1));
  57. retval |= UA_[_i].encodings[UA_ENCODING_BINARY].encode(obj1, &msg1, &pos);
  58. if(retval != UA_STATUSCODE_GOOD) {
  59. // this happens, e.g. when we encode a variant (with UA_[UA_INVALIDTYPE] in the vtable)
  60. UA_[_i].delete(obj1);
  61. UA_ByteString_deleteMembers(&msg1);
  62. return;
  63. }
  64. // when
  65. UA_[_i].new(&obj2);
  66. pos = 0; retval = UA_[_i].encodings[UA_ENCODING_BINARY].decode(&msg1, &pos, obj2);
  67. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "messages differ idx=%d,name=%s", _i, UA_[_i].name);
  68. retval = UA_ByteString_newMembers(&msg2, UA_[_i].encodings[UA_ENCODING_BINARY].calcSize(obj2));
  69. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  70. pos = 0; retval = UA_[_i].encodings[UA_ENCODING_BINARY].encode(obj2, &msg2, &pos);
  71. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  72. // then
  73. ck_assert_msg(UA_ByteString_equal(&msg1, &msg2) == 0, "messages differ idx=%d,name=%s", _i, UA_[_i].name);
  74. // finally
  75. UA_[_i].delete(obj1);
  76. UA_[_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_[_i].new(&obj1);
  87. UA_ByteString_newMembers(&msg1, UA_[_i].encodings[0].calcSize(obj1));
  88. pos = 0; UA_[_i].encodings[0].encode(obj1, &msg1, &pos);
  89. UA_[_i].delete(obj1);
  90. // when
  91. UA_[_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_[_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_[_i].delete(obj2);
  100. UA_ByteString_deleteMembers(&msg1);
  101. }
  102. END_TEST
  103. #define RANDOM_TESTS 1000
  104. START_TEST(decodeScalarBasicTypeFromRandomBufferShallSucceed) {
  105. // given
  106. void *obj1 = UA_NULL;
  107. UA_ByteString msg1;
  108. UA_Int32 retval = UA_STATUSCODE_GOOD;
  109. UA_Int32 buflen = 256;
  110. UA_ByteString_newMembers(&msg1, buflen); // fixed size
  111. #ifdef WIN32
  112. srand(42);
  113. #else
  114. srandom(42);
  115. #endif
  116. for(int n = 0;n < RANDOM_TESTS;n++) {
  117. for(UA_Int32 i = 0;i < buflen;i++) {
  118. #ifdef WIN32
  119. UA_UInt32 rnd;
  120. rnd = rand();
  121. msg1.data[i] = rnd;
  122. #else
  123. msg1.data[i] = (UA_Byte)random(); // when
  124. #endif
  125. }
  126. UA_UInt32 pos = 0;
  127. retval |= UA_[_i].new(&obj1);
  128. retval |= UA_[_i].encodings[0].decode(&msg1, &pos, obj1);
  129. //then
  130. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Decoding %s from random buffer", UA_[_i].name);
  131. // finally
  132. UA_[_i].delete(obj1);
  133. }
  134. UA_ByteString_deleteMembers(&msg1);
  135. }
  136. END_TEST
  137. START_TEST(decodeComplexTypeFromRandomBufferShallSurvive) {
  138. // given
  139. void *obj1 = UA_NULL;
  140. UA_ByteString msg1;
  141. UA_Int32 retval = UA_STATUSCODE_GOOD;
  142. UA_Int32 buflen = 256;
  143. UA_ByteString_newMembers(&msg1, buflen); // fixed size
  144. #ifdef WIN32
  145. srand(42);
  146. #else
  147. srandom(42);
  148. #endif
  149. // when
  150. for(int n = 0;n < RANDOM_TESTS;n++) {
  151. for(UA_Int32 i = 0;i < buflen;i++){
  152. #ifdef WIN32
  153. UA_UInt32 rnd;
  154. rnd = rand();
  155. msg1.data[i] = rnd;
  156. #else
  157. msg1.data[i] = (UA_Byte)random(); // when
  158. #endif
  159. }
  160. UA_UInt32 pos = 0;
  161. retval |= UA_[_i].new(&obj1);
  162. retval |= UA_[_i].encodings[0].decode(&msg1, &pos, obj1);
  163. UA_[_i].delete(obj1);
  164. }
  165. // finally
  166. UA_ByteString_deleteMembers(&msg1);
  167. }
  168. END_TEST
  169. int main() {
  170. int number_failed = 0;
  171. SRunner *sr;
  172. Suite *s = suite_create("testMemoryHandling");
  173. TCase *tc = tcase_create("Empty Objects");
  174. tcase_add_loop_test(tc, newAndEmptyObjectShallBeDeleted, UA_BOOLEAN, UA_INVALIDTYPE-1);
  175. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  176. tcase_add_loop_test(tc, encodeShallYieldDecode, UA_BOOLEAN, UA_INVALIDTYPE-1);
  177. suite_add_tcase(s, tc);
  178. tc = tcase_create("Truncated Buffers");
  179. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive, UA_BOOLEAN, UA_INVALIDTYPE-1);
  180. suite_add_tcase(s, tc);
  181. tc = tcase_create("Fuzzing with Random Buffers");
  182. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed, UA_BOOLEAN, UA_DOUBLE);
  183. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_STRING, UA_DIAGNOSTICINFO);
  184. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_IDTYPE, UA_INVALIDTYPE);
  185. suite_add_tcase(s, tc);
  186. sr = srunner_create(s);
  187. //for debugging puposes only, will break make check
  188. srunner_set_fork_status(sr, CK_NOFORK);
  189. srunner_run_all (sr, CK_NORMAL);
  190. number_failed += srunner_ntests_failed(sr);
  191. srunner_free(sr);
  192. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  193. }