check_memory.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * check_memory.c
  3. *
  4. * Created on: 10.04.2014
  5. * Author: mrt
  6. */
  7. #define _XOPEN_SOURCE 500
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "opcua.h"
  11. #include "check.h"
  12. START_TEST (newAndEmptyObjectShallBeDeleted)
  13. {
  14. // given
  15. UA_Int32 retval;
  16. void* obj;
  17. // when
  18. retval = UA_[_i].new(&obj);
  19. retval |= UA_[_i].delete(obj);
  20. // then
  21. ck_assert_int_eq(retval,UA_SUCCESS);
  22. }
  23. END_TEST
  24. START_TEST (arrayCopyShallMakeADeepCopy)
  25. {
  26. // given
  27. UA_String **a1; UA_Array_new((void***)&a1,3,UA_STRING);
  28. UA_String_copycstring("a",a1[0]);
  29. UA_String_copycstring("bb",a1[1]);
  30. UA_String_copycstring("ccc",a1[2]);
  31. // when
  32. UA_String **a2;
  33. UA_Int32 retval = UA_Array_copy((void const*const*)a1,3,UA_STRING,(void ***)&a2);
  34. // then
  35. ck_assert_int_eq(retval,UA_SUCCESS);
  36. ck_assert_int_eq(((UA_String **)a1)[0]->length,1);
  37. ck_assert_int_eq(((UA_String **)a1)[1]->length,2);
  38. ck_assert_int_eq(((UA_String **)a1)[2]->length,3);
  39. ck_assert_int_eq(((UA_String **)a1)[0]->length,((UA_String **)a2)[0]->length);
  40. ck_assert_int_eq(((UA_String **)a1)[1]->length,((UA_String **)a2)[1]->length);
  41. ck_assert_int_eq(((UA_String **)a1)[2]->length,((UA_String **)a2)[2]->length);
  42. ck_assert_ptr_ne(((UA_String **)a1)[0]->data,((UA_String **)a2)[0]->data);
  43. ck_assert_ptr_ne(((UA_String **)a1)[1]->data,((UA_String **)a2)[1]->data);
  44. ck_assert_ptr_ne(((UA_String **)a1)[2]->data,((UA_String **)a2)[2]->data);
  45. ck_assert_int_eq(((UA_String **)a1)[0]->data[0],((UA_String **)a2)[0]->data[0]);
  46. ck_assert_int_eq(((UA_String **)a1)[1]->data[0],((UA_String **)a2)[1]->data[0]);
  47. ck_assert_int_eq(((UA_String **)a1)[2]->data[0],((UA_String **)a2)[2]->data[0]);
  48. // finally
  49. UA_Array_delete((void***)&a1,3,UA_STRING);
  50. UA_Array_delete((void***)&a2,3,UA_STRING);
  51. }
  52. END_TEST
  53. START_TEST (encodeShallYieldDecode)
  54. {
  55. // given
  56. void *obj1 = UA_NULL, *obj2 = UA_NULL;
  57. UA_ByteString msg1, msg2;
  58. UA_Int32 retval, pos = 0;
  59. retval = UA_[_i].new(&obj1);
  60. UA_ByteString_newMembers(&msg1,UA_[_i].calcSize(obj1));
  61. retval = UA_[_i].encodeBinary(obj1, &pos, &msg1);
  62. // when
  63. UA_[_i].new(&obj2);
  64. pos = 0; retval = UA_[_i].decodeBinary(&msg1, &pos, obj2);
  65. UA_ByteString_newMembers(&msg2,UA_[_i].calcSize(obj2));
  66. pos = 0; retval = UA_[_i].encodeBinary(obj2, &pos, &msg2);
  67. // then
  68. ck_assert_msg(UA_ByteString_compare(&msg1,&msg2)==0,"messages differ idx=%d,name=%s",_i,UA_[_i].name);
  69. ck_assert_int_eq(retval,UA_SUCCESS);
  70. // finally
  71. UA_[_i].delete(obj1);
  72. UA_[_i].delete(obj2);
  73. UA_ByteString_deleteMembers(&msg1);
  74. UA_ByteString_deleteMembers(&msg2);
  75. }
  76. END_TEST
  77. START_TEST (decodeShallFailWithTruncatedBufferButSurvive)
  78. {
  79. // given
  80. void *obj1 = UA_NULL, *obj2 = UA_NULL;
  81. UA_ByteString msg1;
  82. UA_Int32 pos;
  83. UA_[_i].new(&obj1);
  84. UA_ByteString_newMembers(&msg1,UA_[_i].calcSize(obj1));
  85. pos = 0; UA_[_i].encodeBinary(obj1, &pos, &msg1);
  86. UA_[_i].delete(obj1);
  87. // when
  88. UA_[_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_[_i].decodeBinary(&msg1, &pos, obj2);
  93. //then
  94. // finally
  95. //fprintf(stderr,"delete %s with half buffer\n",UA_[_i].name);
  96. UA_[_i].delete(obj2);
  97. UA_ByteString_deleteMembers(&msg1);
  98. }
  99. END_TEST
  100. START_TEST (decodeScalarBasicTypeFromRandomBufferShallSucceed)
  101. {
  102. // given
  103. void *obj1 = UA_NULL;
  104. UA_ByteString msg1;
  105. UA_Int32 retval, buflen;
  106. buflen = 256;
  107. UA_ByteString_newMembers(&msg1,buflen); // fixed size
  108. srandom(42);
  109. retval = UA_SUCCESS;
  110. for(int n=0;n<100;n++) {
  111. retval |= UA_[_i].new(&obj1);
  112. UA_Int32 i; for(i=0;i<buflen;i++) { msg1.data[i] = (UA_Byte) random(); }
  113. // when
  114. UA_Int32 pos = 0;
  115. retval |= UA_[_i].decodeBinary(&msg1, &pos, obj1);
  116. //then
  117. ck_assert_msg(retval==UA_SUCCESS,"Decoding %s from random buffer",UA_[_i].name);
  118. // finally
  119. UA_[_i].delete(obj1);
  120. }
  121. UA_ByteString_deleteMembers(&msg1);
  122. }
  123. END_TEST
  124. START_TEST (decodeComplexTypeFromRandomBufferShallSurvive)
  125. {
  126. // given
  127. void *obj1 = UA_NULL;
  128. UA_ByteString msg1;
  129. UA_Int32 retval, buflen;
  130. buflen = 256;
  131. UA_ByteString_newMembers(&msg1,buflen); // fixed size
  132. srandom(42);
  133. retval = UA_SUCCESS;
  134. for(int n=0;n<100;n++) {
  135. retval |= UA_[_i].new(&obj1);
  136. UA_Int32 i; for(i=0;i<buflen;i++) { msg1.data[i] = (UA_Byte) random(); }
  137. // when
  138. UA_Int32 pos = 0;
  139. retval |= UA_[_i].decodeBinary(&msg1, &pos, obj1);
  140. //then
  141. ck_assert_msg(retval==UA_SUCCESS||retval==UA_ERROR,"Decoding %s from random buffer",UA_[_i].name);
  142. // finally
  143. UA_[_i].delete(obj1);
  144. }
  145. UA_ByteString_deleteMembers(&msg1);
  146. }
  147. END_TEST
  148. int main() {
  149. int number_failed = 0;
  150. SRunner *sr;
  151. Suite *s = suite_create("testMemoryHandling");
  152. TCase *tc = tcase_create("Empty Objects");
  153. tcase_add_loop_test(tc, newAndEmptyObjectShallBeDeleted,UA_BOOLEAN,UA_INVALIDTYPE-1);
  154. tcase_add_test(tc, arrayCopyShallMakeADeepCopy);
  155. tcase_add_loop_test(tc, encodeShallYieldDecode,UA_BOOLEAN,UA_INVALIDTYPE-1);
  156. suite_add_tcase(s,tc);
  157. tc = tcase_create("Truncated Buffers");
  158. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive,UA_BOOLEAN,UA_INVALIDTYPE-1);
  159. suite_add_tcase(s,tc);
  160. tc = tcase_create("Fuzzing with Random Buffers");
  161. tcase_add_loop_test(tc, decodeScalarBasicTypeFromRandomBufferShallSucceed,UA_BOOLEAN,UA_DOUBLE);
  162. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive,UA_STRING,UA_DIAGNOSTICINFO);
  163. tcase_add_loop_test(tc, decodeComplexTypeFromRandomBufferShallSurvive,UA_IDTYPE,UA_INVALIDTYPE);
  164. sr = srunner_create(s);
  165. //for debugging puposes only, will break make check
  166. //srunner_set_fork_status(sr,CK_NOFORK);
  167. srunner_run_all(sr,CK_NORMAL);
  168. number_failed += srunner_ntests_failed(sr);
  169. srunner_free(sr);
  170. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  171. }