check_types_memory.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #define _XOPEN_SOURCE 500
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include "ua_types.h"
  8. #include "ua_server.h"
  9. #include "ua_types_generated.h"
  10. #include "ua_types_generated_handling.h"
  11. #include "ua_types_encoding_binary.h"
  12. #include "ua_util.h"
  13. #include "check.h"
  14. /* Define types to a dummy value if they are not available (e.g. not built with
  15. * NS0 full) */
  16. #ifndef UA_TYPES_UNION
  17. #define UA_TYPES_UNION UA_TYPES_COUNT
  18. #endif
  19. #ifndef UA_TYPES_HISTORYREADDETAILS
  20. #define UA_TYPES_HISTORYREADDETAILS UA_TYPES_COUNT
  21. #endif
  22. #ifndef UA_TYPES_NOTIFICATIONDATA
  23. #define UA_TYPES_NOTIFICATIONDATA UA_TYPES_COUNT
  24. #endif
  25. #ifndef UA_TYPES_MONITORINGFILTER
  26. #define UA_TYPES_MONITORINGFILTER UA_TYPES_COUNT
  27. #endif
  28. #ifndef UA_TYPES_MONITORINGFILTERRESULT
  29. #define UA_TYPES_MONITORINGFILTERRESULT UA_TYPES_COUNT
  30. #endif
  31. START_TEST(newAndEmptyObjectShallBeDeleted) {
  32. // given
  33. void *obj = UA_new(&UA_TYPES[_i]);
  34. // then
  35. ck_assert_ptr_ne(obj, NULL);
  36. // finally
  37. UA_delete(obj, &UA_TYPES[_i]);
  38. }
  39. END_TEST
  40. START_TEST(arrayCopyShallMakeADeepCopy) {
  41. // given
  42. UA_String a1[3];
  43. a1[0] = (UA_String){1, (UA_Byte*)"a"};
  44. a1[1] = (UA_String){2, (UA_Byte*)"bb"};
  45. a1[2] = (UA_String){3, (UA_Byte*)"ccc"};
  46. // when
  47. UA_String *a2;
  48. UA_Int32 retval = UA_Array_copy((const void *)a1, 3, (void **)&a2, &UA_TYPES[UA_TYPES_STRING]);
  49. // then
  50. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  51. ck_assert_int_eq(a1[0].length, 1);
  52. ck_assert_int_eq(a1[1].length, 2);
  53. ck_assert_int_eq(a1[2].length, 3);
  54. ck_assert_int_eq(a1[0].length, a2[0].length);
  55. ck_assert_int_eq(a1[1].length, a2[1].length);
  56. ck_assert_int_eq(a1[2].length, a2[2].length);
  57. ck_assert_ptr_ne(a1[0].data, a2[0].data);
  58. ck_assert_ptr_ne(a1[1].data, a2[1].data);
  59. ck_assert_ptr_ne(a1[2].data, a2[2].data);
  60. ck_assert_int_eq(a1[0].data[0], a2[0].data[0]);
  61. ck_assert_int_eq(a1[1].data[0], a2[1].data[0]);
  62. ck_assert_int_eq(a1[2].data[0], a2[2].data[0]);
  63. // finally
  64. UA_Array_delete((void *)a2, 3, &UA_TYPES[UA_TYPES_STRING]);
  65. }
  66. END_TEST
  67. START_TEST(encodeShallYieldDecode) {
  68. /* floating point types may change the representaton due to several possible NaN values. */
  69. if(_i != UA_TYPES_FLOAT || _i != UA_TYPES_DOUBLE ||
  70. _i != UA_TYPES_CREATESESSIONREQUEST || _i != UA_TYPES_CREATESESSIONRESPONSE ||
  71. _i != UA_TYPES_VARIABLEATTRIBUTES || _i != UA_TYPES_READREQUEST ||
  72. _i != UA_TYPES_MONITORINGPARAMETERS || _i != UA_TYPES_MONITOREDITEMCREATERESULT ||
  73. _i != UA_TYPES_CREATESUBSCRIPTIONREQUEST || _i != UA_TYPES_CREATESUBSCRIPTIONRESPONSE)
  74. return;
  75. // given
  76. UA_ByteString msg1, msg2;
  77. void *obj1 = UA_new(&UA_TYPES[_i]);
  78. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg1, 65000); // fixed buf size
  79. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  80. UA_Byte *pos = msg1.data;
  81. const UA_Byte *end = &msg1.data[msg1.length];
  82. retval = UA_encodeBinary(obj1, &UA_TYPES[_i],
  83. &pos, &end, NULL, NULL);
  84. if(retval != UA_STATUSCODE_GOOD) {
  85. UA_delete(obj1, &UA_TYPES[_i]);
  86. UA_ByteString_deleteMembers(&msg1);
  87. return;
  88. }
  89. // when
  90. void *obj2 = UA_new(&UA_TYPES[_i]);
  91. size_t offset = 0;
  92. retval = UA_decodeBinary(&msg1, &offset, obj2, &UA_TYPES[_i], 0, NULL);
  93. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "could not decode idx=%d,nodeid=%i",
  94. _i, UA_TYPES[_i].typeId.identifier.numeric);
  95. ck_assert(!memcmp(obj1, obj2, UA_TYPES[_i].memSize)); // bit identical decoding
  96. retval = UA_ByteString_allocBuffer(&msg2, 65000);
  97. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  98. pos = msg2.data;
  99. end = &msg2.data[msg2.length];
  100. retval = UA_encodeBinary(obj2, &UA_TYPES[_i], &pos, &end, NULL, NULL);
  101. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  102. // then
  103. msg1.length = offset;
  104. msg2.length = offset;
  105. ck_assert_msg(UA_ByteString_equal(&msg1, &msg2) == true,
  106. "messages differ idx=%d,nodeid=%i", _i,
  107. UA_TYPES[_i].typeId.identifier.numeric);
  108. // finally
  109. UA_delete(obj1, &UA_TYPES[_i]);
  110. UA_delete(obj2, &UA_TYPES[_i]);
  111. UA_ByteString_deleteMembers(&msg1);
  112. UA_ByteString_deleteMembers(&msg2);
  113. }
  114. END_TEST
  115. START_TEST(decodeShallFailWithTruncatedBufferButSurvive) {
  116. //Skip test for void*
  117. if (_i == UA_TYPES_DISCOVERYCONFIGURATION ||
  118. _i == UA_TYPES_FILTEROPERAND ||
  119. _i == UA_TYPES_UNION ||
  120. _i == UA_TYPES_HISTORYREADDETAILS ||
  121. _i == UA_TYPES_NOTIFICATIONDATA ||
  122. _i == UA_TYPES_MONITORINGFILTER ||
  123. _i == UA_TYPES_MONITORINGFILTERRESULT)
  124. return;
  125. // given
  126. UA_ByteString msg1;
  127. void *obj1 = UA_new(&UA_TYPES[_i]);
  128. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg1, 65000); // fixed buf size
  129. UA_Byte *pos = msg1.data;
  130. const UA_Byte *end = &msg1.data[msg1.length];
  131. retval |= UA_encodeBinary(obj1, &UA_TYPES[_i], &pos, &end, NULL, NULL);
  132. UA_delete(obj1, &UA_TYPES[_i]);
  133. if(retval != UA_STATUSCODE_GOOD) {
  134. UA_ByteString_deleteMembers(&msg1);
  135. return; // e.g. variants cannot be encoded after an init without failing (no datatype set)
  136. }
  137. size_t half = (uintptr_t)(pos - msg1.data) / 2;
  138. msg1.length = half;
  139. // when
  140. void *obj2 = UA_new(&UA_TYPES[_i]);
  141. size_t offset = 0;
  142. retval = UA_decodeBinary(&msg1, &offset, obj2, &UA_TYPES[_i], 0, NULL);
  143. ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
  144. UA_delete(obj2, &UA_TYPES[_i]);
  145. UA_ByteString_deleteMembers(&msg1);
  146. }
  147. END_TEST
  148. #define RANDOM_TESTS 1000
  149. START_TEST(decodeScalarBasicTypeFromRandomBufferShallSucceed) {
  150. // given
  151. void *obj1 = NULL;
  152. UA_ByteString msg1;
  153. UA_Int32 retval = UA_STATUSCODE_GOOD;
  154. UA_Int32 buflen = 256;
  155. retval = UA_ByteString_allocBuffer(&msg1, buflen); // fixed size
  156. #ifdef _WIN32
  157. srand(42);
  158. #else
  159. srandom(42);
  160. #endif
  161. for(int n = 0;n < RANDOM_TESTS;n++) {
  162. for(UA_Int32 i = 0;i < buflen;i++) {
  163. #ifdef _WIN32
  164. UA_UInt32 rnd;
  165. rnd = rand();
  166. msg1.data[i] = rnd;
  167. #else
  168. msg1.data[i] = (UA_Byte)random(); // when
  169. #endif
  170. }
  171. size_t pos = 0;
  172. obj1 = UA_new(&UA_TYPES[_i]);
  173. retval |= UA_decodeBinary(&msg1, &pos, obj1, &UA_TYPES[_i], 0, NULL);
  174. //then
  175. ck_assert_msg(retval == UA_STATUSCODE_GOOD,
  176. "Decoding %d from random buffer",
  177. UA_TYPES[_i].typeId.identifier.numeric);
  178. // finally
  179. UA_delete(obj1, &UA_TYPES[_i]);
  180. }
  181. UA_ByteString_deleteMembers(&msg1);
  182. }
  183. END_TEST
  184. START_TEST(decodeComplexTypeFromRandomBufferShallSurvive) {
  185. // given
  186. UA_ByteString msg1;
  187. UA_Int32 retval = UA_STATUSCODE_GOOD;
  188. UA_Int32 buflen = 256;
  189. retval = UA_ByteString_allocBuffer(&msg1, buflen); // fixed size
  190. #ifdef _WIN32
  191. srand(42);
  192. #else
  193. srandom(42);
  194. #endif
  195. // when
  196. for(int n = 0;n < RANDOM_TESTS;n++) {
  197. for(UA_Int32 i = 0;i < buflen;i++) {
  198. #ifdef _WIN32
  199. UA_UInt32 rnd;
  200. rnd = rand();
  201. msg1.data[i] = rnd;
  202. #else
  203. msg1.data[i] = (UA_Byte)random(); // when
  204. #endif
  205. }
  206. size_t pos = 0;
  207. void *obj1 = UA_new(&UA_TYPES[_i]);
  208. retval |= UA_decodeBinary(&msg1, &pos, obj1, &UA_TYPES[_i], 0, NULL);
  209. UA_delete(obj1, &UA_TYPES[_i]);
  210. }
  211. // finally
  212. UA_ByteString_deleteMembers(&msg1);
  213. }
  214. END_TEST
  215. START_TEST(calcSizeBinaryShallBeCorrect) {
  216. /* Empty variants (with no type defined) cannot be encoded. This is
  217. * intentional. Discovery configuration is just a base class and void * */
  218. if(_i == UA_TYPES_VARIANT ||
  219. _i == UA_TYPES_VARIABLEATTRIBUTES ||
  220. _i == UA_TYPES_VARIABLETYPEATTRIBUTES ||
  221. _i == UA_TYPES_FILTEROPERAND ||
  222. _i == UA_TYPES_DISCOVERYCONFIGURATION ||
  223. _i == UA_TYPES_UNION ||
  224. _i == UA_TYPES_HISTORYREADDETAILS ||
  225. _i == UA_TYPES_NOTIFICATIONDATA ||
  226. _i == UA_TYPES_MONITORINGFILTER ||
  227. _i == UA_TYPES_MONITORINGFILTERRESULT)
  228. return;
  229. void *obj = UA_new(&UA_TYPES[_i]);
  230. size_t predicted_size = UA_calcSizeBinary(obj, &UA_TYPES[_i]);
  231. ck_assert_int_ne(predicted_size, 0);
  232. UA_ByteString msg;
  233. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg, predicted_size);
  234. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  235. UA_Byte *pos = msg.data;
  236. const UA_Byte *end = &msg.data[msg.length];
  237. retval = UA_encodeBinary(obj, &UA_TYPES[_i], &pos, &end, NULL, NULL);
  238. if(retval)
  239. printf("%i\n",_i);
  240. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  241. ck_assert_int_eq((uintptr_t)(pos - msg.data), predicted_size);
  242. UA_delete(obj, &UA_TYPES[_i]);
  243. UA_ByteString_deleteMembers(&msg);
  244. }
  245. END_TEST
  246. int main(void) {
  247. int number_failed = 0;
  248. SRunner *sr;
  249. Suite *s = suite_create("testMemoryHandling");
  250. TCase *tc = tcase_create("Empty Objects");
  251. tcase_add_loop_test(tc, newAndEmptyObjectShallBeDeleted, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  252. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  253. tcase_add_loop_test(tc, encodeShallYieldDecode, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  254. suite_add_tcase(s, tc);
  255. tc = tcase_create("Truncated Buffers");
  256. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive,
  257. UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  258. suite_add_tcase(s, tc);
  259. tc = tcase_create("Fuzzing with Random Buffers");
  260. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed,
  261. UA_TYPES_BOOLEAN, UA_TYPES_DOUBLE);
  262. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive,
  263. UA_TYPES_NODEID, UA_TYPES_COUNT - 1);
  264. suite_add_tcase(s, tc);
  265. tc = tcase_create("Test calcSizeBinary");
  266. tcase_add_loop_test(tc, calcSizeBinaryShallBeCorrect, UA_TYPES_BOOLEAN, UA_TYPES_COUNT - 1);
  267. suite_add_tcase(s, tc);
  268. sr = srunner_create(s);
  269. srunner_set_fork_status(sr, CK_NOFORK);
  270. srunner_run_all (sr, CK_NORMAL);
  271. number_failed += srunner_ntests_failed(sr);
  272. srunner_free(sr);
  273. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  274. }