check_memory.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. srandom(42);
  111. for(int n = 0;n < 100;n++) {
  112. for(UA_Int32 i = 0;i < buflen;i++) msg1.data[i] = (UA_Byte)random(); // when
  113. UA_UInt32 pos = 0;
  114. retval |= UA_.types[_i].new(&obj1);
  115. retval |= UA_.types[_i].encodings[0].decode(&msg1, &pos, obj1);
  116. //then
  117. ck_assert_msg(retval == UA_SUCCESS, "Decoding %s from random buffer", UA_.types[_i].name);
  118. // finally
  119. UA_.types[_i].delete(obj1);
  120. }
  121. UA_ByteString_deleteMembers(&msg1);
  122. }
  123. END_TEST
  124. START_TEST(decodeComplexTypeFromRandomBufferShallSurvive) {
  125. // given
  126. void *obj1 = UA_NULL;
  127. UA_ByteString msg1;
  128. UA_Int32 retval = UA_SUCCESS;
  129. UA_Int32 buflen = 256;
  130. UA_ByteString_newMembers(&msg1, buflen); // fixed size
  131. srandom(42);
  132. // when
  133. for(int n = 0;n < 100;n++) {
  134. for(UA_Int32 i = 0;i < buflen;i++) msg1.data[i] = (UA_Byte)random();
  135. UA_UInt32 pos = 0;
  136. retval |= UA_.types[_i].new(&obj1);
  137. retval |= UA_.types[_i].encodings[0].decode(&msg1, &pos, obj1);
  138. //this is allowed to fail and return UA_ERROR
  139. //ck_assert_msg(retval == UA_SUCCESS, "Decoding %s from random buffer", UA_.types[_i].name);
  140. UA_.types[_i].delete(obj1);
  141. }
  142. // finally
  143. UA_ByteString_deleteMembers(&msg1);
  144. }
  145. END_TEST
  146. int main() {
  147. int number_failed = 0;
  148. SRunner *sr;
  149. Suite *s = suite_create("testMemoryHandling");
  150. TCase *tc = tcase_create("Empty Objects");
  151. tcase_add_loop_test(tc, newAndEmptyObjectShallBeDeleted, UA_BOOLEAN, UA_INVALIDTYPE-1);
  152. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  153. tcase_add_loop_test(tc, encodeShallYieldDecode, UA_BOOLEAN, UA_INVALIDTYPE-1);
  154. suite_add_tcase(s, tc);
  155. tc = tcase_create("Truncated Buffers");
  156. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive, UA_BOOLEAN, UA_INVALIDTYPE-1);
  157. suite_add_tcase(s, tc);
  158. tc = tcase_create("Fuzzing with Random Buffers");
  159. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed, UA_BOOLEAN, UA_DOUBLE);
  160. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_STRING, UA_DIAGNOSTICINFO);
  161. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_IDTYPE, UA_INVALIDTYPE);
  162. suite_add_tcase(s, tc);
  163. sr = srunner_create(s);
  164. //for debugging puposes only, will break make check
  165. //srunner_set_fork_status(sr, CK_NOFORK);
  166. srunner_run_all (sr, CK_NORMAL);
  167. number_failed += srunner_ntests_failed(sr);
  168. srunner_free(sr);
  169. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  170. }