check_memory.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #define _XOPEN_SOURCE 500
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "ua_types.h"
  5. #include "ua_types_generated.h"
  6. #include "ua_types_encoding_binary.h"
  7. #include "ua_util.h"
  8. #include "check.h"
  9. START_TEST(newAndEmptyObjectShallBeDeleted) {
  10. // given
  11. void *obj = UA_new(&UA_TYPES[_i]);
  12. // then
  13. ck_assert_ptr_ne(obj, NULL);
  14. // finally
  15. UA_delete(obj, &UA_TYPES[_i]);
  16. }
  17. END_TEST
  18. START_TEST(arrayCopyShallMakeADeepCopy) {
  19. // given
  20. UA_String a1[3];
  21. a1[0] = (UA_String){1, (UA_Byte*)"a"};
  22. a1[1] = (UA_String){2, (UA_Byte*)"bb"};
  23. a1[2] = (UA_String){3, (UA_Byte*)"ccc"};
  24. // when
  25. UA_String *a2;
  26. UA_Int32 retval = UA_Array_copy((const void *)a1, 3, (void **)&a2, &UA_TYPES[UA_TYPES_STRING]);
  27. // then
  28. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  29. ck_assert_int_eq(a1[0].length, 1);
  30. ck_assert_int_eq(a1[1].length, 2);
  31. ck_assert_int_eq(a1[2].length, 3);
  32. ck_assert_int_eq(a1[0].length, a2[0].length);
  33. ck_assert_int_eq(a1[1].length, a2[1].length);
  34. ck_assert_int_eq(a1[2].length, a2[2].length);
  35. ck_assert_ptr_ne(a1[0].data, a2[0].data);
  36. ck_assert_ptr_ne(a1[1].data, a2[1].data);
  37. ck_assert_ptr_ne(a1[2].data, a2[2].data);
  38. ck_assert_int_eq(a1[0].data[0], a2[0].data[0]);
  39. ck_assert_int_eq(a1[1].data[0], a2[1].data[0]);
  40. ck_assert_int_eq(a1[2].data[0], a2[2].data[0]);
  41. // finally
  42. UA_Array_delete((void *)a2, 3, &UA_TYPES[UA_TYPES_STRING]);
  43. }
  44. END_TEST
  45. START_TEST(encodeShallYieldDecode) {
  46. // given
  47. UA_ByteString msg1, msg2;
  48. size_t pos = 0;
  49. void *obj1 = UA_new(&UA_TYPES[_i]);
  50. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg1, 65000); // fixed buf size
  51. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  52. retval = UA_encodeBinary(obj1, &UA_TYPES[_i], &msg1, &pos);
  53. msg1.length = pos;
  54. if(retval != UA_STATUSCODE_GOOD) {
  55. UA_delete(obj1, &UA_TYPES[_i]);
  56. UA_ByteString_deleteMembers(&msg1);
  57. return;
  58. }
  59. // when
  60. void *obj2 = UA_new(&UA_TYPES[_i]);
  61. pos = 0; retval = UA_decodeBinary(&msg1, &pos, obj2, &UA_TYPES[_i]);
  62. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "could not decode idx=%d,nodeid=%i", _i, UA_TYPES[_i].typeId.identifier.numeric);
  63. ck_assert(!memcmp(obj1, obj2, UA_TYPES[_i].memSize)); // bit identical decoding
  64. assert(!memcmp(obj1, obj2, UA_TYPES[_i].memSize));
  65. retval = UA_ByteString_allocBuffer(&msg2, 65000);
  66. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  67. pos = 0; retval = UA_encodeBinary(obj2, &UA_TYPES[_i], &msg2, &pos);
  68. msg2.length = pos;
  69. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  70. // then
  71. ck_assert_msg(UA_ByteString_equal(&msg1, &msg2) == true, "messages differ idx=%d,nodeid=%i", _i,
  72. UA_TYPES[_i].typeId.identifier.numeric);
  73. // finally
  74. UA_delete(obj1, &UA_TYPES[_i]);
  75. UA_delete(obj2, &UA_TYPES[_i]);
  76. UA_ByteString_deleteMembers(&msg1);
  77. UA_ByteString_deleteMembers(&msg2);
  78. }
  79. END_TEST
  80. START_TEST(decodeShallFailWithTruncatedBufferButSurvive) {
  81. // given
  82. UA_ByteString msg1;
  83. void *obj1 = UA_new(&UA_TYPES[_i]);
  84. size_t pos = 0;
  85. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg1, 65000); // fixed buf size
  86. retval |= UA_encodeBinary(obj1, &UA_TYPES[_i], &msg1, &pos);
  87. UA_delete(obj1, &UA_TYPES[_i]);
  88. if(retval != UA_STATUSCODE_GOOD) {
  89. UA_ByteString_deleteMembers(&msg1);
  90. return; // e.g. variants cannot be encoded after an init without failing (no datatype set)
  91. }
  92. // when
  93. void *obj2 = UA_new(&UA_TYPES[_i]);
  94. pos = 0;
  95. msg1.length = pos / 2;
  96. //fprintf(stderr,"testing %s with half buffer\n",UA_TYPES[_i].name);
  97. retval = UA_decodeBinary(&msg1, &pos, obj2, &UA_TYPES[_i]);
  98. ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
  99. //then
  100. // finally
  101. //fprintf(stderr,"delete %s with half buffer\n",UA_TYPES[_i].name);
  102. UA_delete(obj2, &UA_TYPES[_i]);
  103. UA_ByteString_deleteMembers(&msg1);
  104. }
  105. END_TEST
  106. #define RANDOM_TESTS 1000
  107. START_TEST(decodeScalarBasicTypeFromRandomBufferShallSucceed) {
  108. // given
  109. void *obj1 = NULL;
  110. UA_ByteString msg1;
  111. UA_Int32 retval = UA_STATUSCODE_GOOD;
  112. UA_Int32 buflen = 256;
  113. retval = UA_ByteString_allocBuffer(&msg1, buflen); // fixed size
  114. #ifdef _WIN32
  115. srand(42);
  116. #else
  117. srandom(42);
  118. #endif
  119. for(int n = 0;n < RANDOM_TESTS;n++) {
  120. for(UA_Int32 i = 0;i < buflen;i++) {
  121. #ifdef _WIN32
  122. UA_UInt32 rnd;
  123. rnd = rand();
  124. msg1.data[i] = rnd;
  125. #else
  126. msg1.data[i] = (UA_Byte)random(); // when
  127. #endif
  128. }
  129. size_t pos = 0;
  130. obj1 = UA_new(&UA_TYPES[_i]);
  131. retval |= UA_decodeBinary(&msg1, &pos, obj1, &UA_TYPES[_i]);
  132. //then
  133. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Decoding %d from random buffer", UA_TYPES[_i].typeId.identifier.numeric);
  134. // finally
  135. UA_delete(obj1, &UA_TYPES[_i]);
  136. }
  137. UA_ByteString_deleteMembers(&msg1);
  138. }
  139. END_TEST
  140. START_TEST(decodeComplexTypeFromRandomBufferShallSurvive) {
  141. // given
  142. UA_ByteString msg1;
  143. UA_Int32 retval = UA_STATUSCODE_GOOD;
  144. UA_Int32 buflen = 256;
  145. retval = UA_ByteString_allocBuffer(&msg1, buflen); // fixed size
  146. #ifdef _WIN32
  147. srand(42);
  148. #else
  149. srandom(42);
  150. #endif
  151. // when
  152. for(int n = 0;n < RANDOM_TESTS;n++) {
  153. for(UA_Int32 i = 0;i < buflen;i++) {
  154. #ifdef _WIN32
  155. UA_UInt32 rnd;
  156. rnd = rand();
  157. msg1.data[i] = rnd;
  158. #else
  159. msg1.data[i] = (UA_Byte)random(); // when
  160. #endif
  161. }
  162. size_t pos = 0;
  163. void *obj1 = UA_new(&UA_TYPES[_i]);
  164. retval |= UA_decodeBinary(&msg1, &pos, obj1, &UA_TYPES[_i]);
  165. UA_delete(obj1, &UA_TYPES[_i]);
  166. }
  167. // finally
  168. UA_ByteString_deleteMembers(&msg1);
  169. }
  170. END_TEST
  171. START_TEST(calcSizeBinaryShallBeCorrect) {
  172. /* Empty variants (with no type defined) cannot be encoded. This is intentional. */
  173. if(_i == UA_TYPES_VARIANT ||
  174. _i == UA_TYPES_VARIABLEATTRIBUTES ||
  175. _i == UA_TYPES_VARIABLETYPEATTRIBUTES)
  176. return;
  177. void *obj = UA_new(&UA_TYPES[_i]);
  178. size_t predicted_size = UA_calcSizeBinary(obj, &UA_TYPES[_i]);
  179. ck_assert_int_ne(predicted_size, 0);
  180. UA_ByteString msg;
  181. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg, predicted_size);
  182. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  183. size_t offset = 0;
  184. retval = UA_encodeBinary(obj, &UA_TYPES[_i], &msg, &offset);
  185. if(retval)
  186. printf("%i\n",_i);
  187. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  188. ck_assert_int_eq(offset, predicted_size);
  189. UA_delete(obj, &UA_TYPES[_i]);
  190. UA_ByteString_deleteMembers(&msg);
  191. }
  192. END_TEST
  193. int main(void) {
  194. int number_failed = 0;
  195. SRunner *sr;
  196. Suite *s = suite_create("testMemoryHandling");
  197. TCase *tc = tcase_create("Empty Objects");
  198. tcase_add_loop_test(tc, newAndEmptyObjectShallBeDeleted, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  199. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  200. tcase_add_loop_test(tc, encodeShallYieldDecode, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  201. suite_add_tcase(s, tc);
  202. tc = tcase_create("Truncated Buffers");
  203. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  204. suite_add_tcase(s, tc);
  205. tc = tcase_create("Fuzzing with Random Buffers");
  206. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed, UA_TYPES_BOOLEAN, UA_TYPES_DOUBLE);
  207. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_TYPES_NODEID, UA_TYPES_COUNT - 1);
  208. suite_add_tcase(s, tc);
  209. tc = tcase_create("Test calcSizeBinary");
  210. tcase_add_loop_test(tc, calcSizeBinaryShallBeCorrect, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  211. suite_add_tcase(s, tc);
  212. sr = srunner_create(s);
  213. srunner_set_fork_status(sr, CK_NOFORK);
  214. srunner_run_all (sr, CK_NORMAL);
  215. number_failed += srunner_ntests_failed(sr);
  216. srunner_free(sr);
  217. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  218. }