check_memory.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. retval |= UA_.types[_i].delete(obj);
  15. // then
  16. ck_assert_int_eq(retval, UA_SUCCESS);
  17. }
  18. END_TEST
  19. START_TEST(arrayCopyShallMakeADeepCopy) {
  20. // given
  21. UA_String a1[3];
  22. a1[0] = (UA_String){1, (UA_Byte*)"a"};
  23. a1[1] = (UA_String){2, (UA_Byte*)"bb"};
  24. a1[2] = (UA_String){3, (UA_Byte*)"ccc"};
  25. // when
  26. UA_String *a2;
  27. UA_Int32 retval = UA_Array_copy((const void *)a1, 3, &UA_.types[UA_STRING], (void **)&a2);
  28. // then
  29. ck_assert_int_eq(retval, UA_SUCCESS);
  30. ck_assert_int_eq(a1[0].length, 1);
  31. ck_assert_int_eq(a1[1].length, 2);
  32. ck_assert_int_eq(a1[2].length, 3);
  33. ck_assert_int_eq(a1[0].length, a2[0].length);
  34. ck_assert_int_eq(a1[1].length, a2[1].length);
  35. ck_assert_int_eq(a1[2].length, a2[2].length);
  36. ck_assert_ptr_ne(a1[0].data, a2[0].data);
  37. ck_assert_ptr_ne(a1[1].data, a2[1].data);
  38. ck_assert_ptr_ne(a1[2].data, a2[2].data);
  39. ck_assert_int_eq(a1[0].data[0], a2[0].data[0]);
  40. ck_assert_int_eq(a1[1].data[0], a2[1].data[0]);
  41. ck_assert_int_eq(a1[2].data[0], a2[2].data[0]);
  42. // finally
  43. UA_Array_delete((void *)a2, 3, &UA_.types[UA_STRING]);
  44. }
  45. END_TEST
  46. START_TEST(encodeShallYieldDecode) {
  47. // given
  48. void *obj1 = UA_NULL, *obj2 = UA_NULL;
  49. UA_ByteString msg1, msg2;
  50. UA_Int32 retval;
  51. UA_UInt32 pos = 0;
  52. retval = UA_.types[_i].new(&obj1);
  53. UA_ByteString_newMembers(&msg1, UA_.types[_i].encodings[0].calcSize(obj1));
  54. retval |= UA_.types[_i].encodings[UA_ENCODING_BINARY].encode(obj1, &msg1, &pos);
  55. if(retval != UA_SUCCESS) {
  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. UA_.types[_i].new(&obj2);
  63. pos = 0; retval = UA_.types[_i].encodings[UA_ENCODING_BINARY].decode(&msg1, &pos, obj2);
  64. ck_assert_msg(retval == UA_SUCCESS, "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_SUCCESS);
  67. pos = 0; retval = UA_.types[_i].encodings[UA_ENCODING_BINARY].encode(obj2, &msg2, &pos);
  68. ck_assert_int_eq(retval, UA_SUCCESS);
  69. // then
  70. ck_assert_msg(UA_ByteString_equal(&msg1, &msg2) == 0, "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. void *obj1 = UA_NULL, *obj2 = UA_NULL;
  81. UA_ByteString msg1;
  82. UA_UInt32 pos;
  83. UA_.types[_i].new(&obj1);
  84. UA_ByteString_newMembers(&msg1, UA_.types[_i].encodings[0].calcSize(obj1));
  85. pos = 0; UA_.types[_i].encodings[0].encode(obj1, &msg1, &pos);
  86. UA_.types[_i].delete(obj1);
  87. // when
  88. UA_.types[_i].new(&obj2);
  89. pos = 0;
  90. msg1.length = msg1.length / 2;
  91. //fprintf(stderr,"testing %s with half buffer\n",UA_[_i].name);
  92. UA_.types[_i].encodings[0].decode(&msg1, &pos, obj2);
  93. //then
  94. // finally
  95. //fprintf(stderr,"delete %s with half buffer\n",UA_[_i].name);
  96. UA_.types[_i].delete(obj2);
  97. UA_ByteString_deleteMembers(&msg1);
  98. }
  99. END_TEST
  100. START_TEST(decodeScalarBasicTypeFromRandomBufferShallSucceed) {
  101. // given
  102. void *obj1 = UA_NULL;
  103. UA_ByteString msg1;
  104. UA_Int32 retval = UA_SUCCESS;
  105. UA_Int32 buflen = 256;
  106. UA_ByteString_newMembers(&msg1, buflen); // fixed size
  107. srandom(42);
  108. for(int n = 0;n < 100;n++) {
  109. for(UA_Int32 i = 0;i < buflen;i++) msg1.data[i] = (UA_Byte)random(); // when
  110. UA_UInt32 pos = 0;
  111. retval |= UA_.types[_i].new(&obj1);
  112. retval |= UA_.types[_i].encodings[0].decode(&msg1, &pos, obj1);
  113. //then
  114. ck_assert_msg(retval == UA_SUCCESS, "Decoding %s from random buffer", UA_.types[_i].name);
  115. // finally
  116. UA_.types[_i].delete(obj1);
  117. }
  118. UA_ByteString_deleteMembers(&msg1);
  119. }
  120. END_TEST
  121. START_TEST(decodeComplexTypeFromRandomBufferShallSurvive) {
  122. // given
  123. void *obj1 = UA_NULL;
  124. UA_ByteString msg1;
  125. UA_Int32 retval = UA_SUCCESS;
  126. UA_Int32 buflen = 256;
  127. UA_ByteString_newMembers(&msg1, buflen); // fixed size
  128. srandom(42);
  129. // when
  130. for(int n = 0;n < 100;n++) {
  131. for(UA_Int32 i = 0;i < buflen;i++) msg1.data[i] = (UA_Byte)random();
  132. UA_UInt32 pos = 0;
  133. retval |= UA_.types[_i].new(&obj1);
  134. retval |= UA_.types[_i].encodings[0].decode(&msg1, &pos, obj1);
  135. //this is allowed to fail and return UA_ERROR
  136. //ck_assert_msg(retval == UA_SUCCESS, "Decoding %s from random buffer", UA_.types[_i].name);
  137. UA_.types[_i].delete(obj1);
  138. }
  139. // finally
  140. UA_ByteString_deleteMembers(&msg1);
  141. }
  142. END_TEST
  143. int main() {
  144. int number_failed = 0;
  145. SRunner *sr;
  146. Suite *s = suite_create("testMemoryHandling");
  147. TCase *tc = tcase_create("Empty Objects");
  148. tcase_add_loop_test(tc, newAndEmptyObjectShallBeDeleted, UA_BOOLEAN, UA_INVALIDTYPE-1);
  149. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  150. tcase_add_loop_test(tc, encodeShallYieldDecode, UA_BOOLEAN, UA_INVALIDTYPE-1);
  151. suite_add_tcase(s, tc);
  152. tc = tcase_create("Truncated Buffers");
  153. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive, UA_BOOLEAN, UA_INVALIDTYPE-1);
  154. suite_add_tcase(s, tc);
  155. tc = tcase_create("Fuzzing with Random Buffers");
  156. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed, UA_BOOLEAN, UA_DOUBLE);
  157. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_STRING, UA_DIAGNOSTICINFO);
  158. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_IDTYPE, UA_INVALIDTYPE);
  159. suite_add_tcase(s, tc);
  160. sr = srunner_create(s);
  161. //for debugging puposes only, will break make check
  162. //srunner_set_fork_status(sr, CK_NOFORK);
  163. srunner_run_all (sr, CK_NORMAL);
  164. number_failed += srunner_ntests_failed(sr);
  165. srunner_free(sr);
  166. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  167. }