check_memory.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. /* floating point types may change the representaton due to several possible NaN values. */
  47. if(_i != UA_TYPES_FLOAT || _i != UA_TYPES_DOUBLE ||
  48. _i != UA_TYPES_CREATESESSIONREQUEST || _i != UA_TYPES_CREATESESSIONRESPONSE ||
  49. _i != UA_TYPES_VARIABLEATTRIBUTES || _i != UA_TYPES_READREQUEST ||
  50. _i != UA_TYPES_MONITORINGPARAMETERS || _i != UA_TYPES_MONITOREDITEMCREATERESULT ||
  51. _i != UA_TYPES_CREATESUBSCRIPTIONREQUEST || _i != UA_TYPES_CREATESUBSCRIPTIONRESPONSE)
  52. return;
  53. // given
  54. UA_ByteString msg1, msg2;
  55. size_t pos = 0;
  56. void *obj1 = UA_new(&UA_TYPES[_i]);
  57. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg1, 65000); // fixed buf size
  58. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  59. retval = UA_encodeBinary(obj1, &UA_TYPES[_i], NULL, NULL, &msg1, &pos);
  60. msg1.length = pos;
  61. if(retval != UA_STATUSCODE_GOOD) {
  62. UA_delete(obj1, &UA_TYPES[_i]);
  63. UA_ByteString_deleteMembers(&msg1);
  64. return;
  65. }
  66. // when
  67. void *obj2 = UA_new(&UA_TYPES[_i]);
  68. pos = 0; retval = UA_decodeBinary(&msg1, &pos, obj2, &UA_TYPES[_i]);
  69. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "could not decode idx=%d,nodeid=%i",
  70. _i, UA_TYPES[_i].typeId.identifier.numeric);
  71. ck_assert(!memcmp(obj1, obj2, UA_TYPES[_i].memSize)); // bit identical decoding
  72. retval = UA_ByteString_allocBuffer(&msg2, 65000);
  73. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  74. pos = 0; retval = UA_encodeBinary(obj2, &UA_TYPES[_i], NULL, NULL, &msg2, &pos);
  75. msg2.length = pos;
  76. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  77. // then
  78. ck_assert_msg(UA_ByteString_equal(&msg1, &msg2) == true, "messages differ idx=%d,nodeid=%i", _i,
  79. UA_TYPES[_i].typeId.identifier.numeric);
  80. // finally
  81. UA_delete(obj1, &UA_TYPES[_i]);
  82. UA_delete(obj2, &UA_TYPES[_i]);
  83. UA_ByteString_deleteMembers(&msg1);
  84. UA_ByteString_deleteMembers(&msg2);
  85. }
  86. END_TEST
  87. START_TEST(decodeShallFailWithTruncatedBufferButSurvive) {
  88. // given
  89. UA_ByteString msg1;
  90. void *obj1 = UA_new(&UA_TYPES[_i]);
  91. size_t pos = 0;
  92. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg1, 65000); // fixed buf size
  93. retval |= UA_encodeBinary(obj1, &UA_TYPES[_i], NULL, NULL, &msg1, &pos);
  94. UA_delete(obj1, &UA_TYPES[_i]);
  95. if(retval != UA_STATUSCODE_GOOD) {
  96. UA_ByteString_deleteMembers(&msg1);
  97. return; // e.g. variants cannot be encoded after an init without failing (no datatype set)
  98. }
  99. // when
  100. void *obj2 = UA_new(&UA_TYPES[_i]);
  101. msg1.length = pos / 2;
  102. pos = 0;
  103. //fprintf(stderr,"testing %s with half buffer\n",UA_TYPES[_i].name);
  104. retval = UA_decodeBinary(&msg1, &pos, obj2, &UA_TYPES[_i]);
  105. ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
  106. //then
  107. // finally
  108. //fprintf(stderr,"delete %s with half buffer\n",UA_TYPES[_i].name);
  109. UA_delete(obj2, &UA_TYPES[_i]);
  110. UA_ByteString_deleteMembers(&msg1);
  111. }
  112. END_TEST
  113. #define RANDOM_TESTS 1000
  114. START_TEST(decodeScalarBasicTypeFromRandomBufferShallSucceed) {
  115. // given
  116. void *obj1 = NULL;
  117. UA_ByteString msg1;
  118. UA_Int32 retval = UA_STATUSCODE_GOOD;
  119. UA_Int32 buflen = 256;
  120. retval = UA_ByteString_allocBuffer(&msg1, buflen); // fixed size
  121. #ifdef _WIN32
  122. srand(42);
  123. #else
  124. srandom(42);
  125. #endif
  126. for(int n = 0;n < RANDOM_TESTS;n++) {
  127. for(UA_Int32 i = 0;i < buflen;i++) {
  128. #ifdef _WIN32
  129. UA_UInt32 rnd;
  130. rnd = rand();
  131. msg1.data[i] = rnd;
  132. #else
  133. msg1.data[i] = (UA_Byte)random(); // when
  134. #endif
  135. }
  136. size_t pos = 0;
  137. obj1 = UA_new(&UA_TYPES[_i]);
  138. retval |= UA_decodeBinary(&msg1, &pos, obj1, &UA_TYPES[_i]);
  139. //then
  140. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Decoding %d from random buffer", UA_TYPES[_i].typeId.identifier.numeric);
  141. // finally
  142. UA_delete(obj1, &UA_TYPES[_i]);
  143. }
  144. UA_ByteString_deleteMembers(&msg1);
  145. }
  146. END_TEST
  147. START_TEST(decodeComplexTypeFromRandomBufferShallSurvive) {
  148. // given
  149. UA_ByteString msg1;
  150. UA_Int32 retval = UA_STATUSCODE_GOOD;
  151. UA_Int32 buflen = 256;
  152. retval = UA_ByteString_allocBuffer(&msg1, buflen); // fixed size
  153. #ifdef _WIN32
  154. srand(42);
  155. #else
  156. srandom(42);
  157. #endif
  158. // when
  159. for(int n = 0;n < RANDOM_TESTS;n++) {
  160. for(UA_Int32 i = 0;i < buflen;i++) {
  161. #ifdef _WIN32
  162. UA_UInt32 rnd;
  163. rnd = rand();
  164. msg1.data[i] = rnd;
  165. #else
  166. msg1.data[i] = (UA_Byte)random(); // when
  167. #endif
  168. }
  169. size_t pos = 0;
  170. void *obj1 = UA_new(&UA_TYPES[_i]);
  171. retval |= UA_decodeBinary(&msg1, &pos, obj1, &UA_TYPES[_i]);
  172. UA_delete(obj1, &UA_TYPES[_i]);
  173. }
  174. // finally
  175. UA_ByteString_deleteMembers(&msg1);
  176. }
  177. END_TEST
  178. START_TEST(calcSizeBinaryShallBeCorrect) {
  179. /* Empty variants (with no type defined) cannot be encoded. This is intentional. */
  180. if(_i == UA_TYPES_VARIANT ||
  181. _i == UA_TYPES_VARIABLEATTRIBUTES ||
  182. _i == UA_TYPES_VARIABLETYPEATTRIBUTES)
  183. return;
  184. void *obj = UA_new(&UA_TYPES[_i]);
  185. size_t predicted_size = UA_calcSizeBinary(obj, &UA_TYPES[_i]);
  186. ck_assert_int_ne(predicted_size, 0);
  187. UA_ByteString msg;
  188. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg, predicted_size);
  189. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  190. size_t offset = 0;
  191. retval = UA_encodeBinary(obj, &UA_TYPES[_i], NULL, NULL, &msg, &offset);
  192. if(retval)
  193. printf("%i\n",_i);
  194. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  195. ck_assert_int_eq(offset, predicted_size);
  196. UA_delete(obj, &UA_TYPES[_i]);
  197. UA_ByteString_deleteMembers(&msg);
  198. }
  199. END_TEST
  200. int main(void) {
  201. int number_failed = 0;
  202. SRunner *sr;
  203. Suite *s = suite_create("testMemoryHandling");
  204. TCase *tc = tcase_create("Empty Objects");
  205. tcase_add_loop_test(tc, newAndEmptyObjectShallBeDeleted, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  206. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  207. tcase_add_loop_test(tc, encodeShallYieldDecode, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  208. suite_add_tcase(s, tc);
  209. tc = tcase_create("Truncated Buffers");
  210. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  211. suite_add_tcase(s, tc);
  212. tc = tcase_create("Fuzzing with Random Buffers");
  213. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed, UA_TYPES_BOOLEAN, UA_TYPES_DOUBLE);
  214. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive, UA_TYPES_NODEID, UA_TYPES_COUNT - 1);
  215. suite_add_tcase(s, tc);
  216. tc = tcase_create("Test calcSizeBinary");
  217. tcase_add_loop_test(tc, calcSizeBinaryShallBeCorrect, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  218. suite_add_tcase(s, tc);
  219. sr = srunner_create(s);
  220. srunner_set_fork_status(sr, CK_NOFORK);
  221. srunner_run_all (sr, CK_NORMAL);
  222. number_failed += srunner_ntests_failed(sr);
  223. srunner_free(sr);
  224. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  225. }