check_memory.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * check_memory.c
  3. *
  4. * Created on: 10.04.2014
  5. * Author: mrt
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "opcua.h"
  10. #include "check.h"
  11. START_TEST (newAndEmptyObjectShallBeDeleted)
  12. {
  13. // given
  14. UA_Int32 retval;
  15. void* obj;
  16. // when
  17. retval = UA_[_i].new(&obj);
  18. retval |= UA_[_i].delete(obj);
  19. // then
  20. ck_assert_int_eq(retval,UA_SUCCESS);
  21. }
  22. END_TEST
  23. START_TEST (encodeShallYieldDecode)
  24. {
  25. void *obj1 = UA_NULL, *obj2 = UA_NULL;
  26. UA_ByteString msg1, msg2;
  27. // UA_ByteString x;
  28. UA_Int32 retval, pos;
  29. // printf("testing idx=%d,name=%s\n",_i,UA_[_i].name);
  30. // create src object
  31. retval = UA_[_i].new(&obj1);
  32. // printf("retval=%d, ",retval); x.length = UA_[_i].calcSize(UA_NULL); x.data = (UA_Byte*) obj1; UA_ByteString_printx_hex("obj1=",&x);
  33. // encode obj into buffer
  34. UA_ByteString_newMembers(&msg1,UA_[_i].calcSize(obj1));
  35. pos = 0;
  36. retval = UA_[_i].encodeBinary(obj1, &pos, &msg1);
  37. // printf("retval=%d, ",retval); x.length = pos; x.data = (UA_Byte*) msg1.data; UA_ByteString_printx_hex("msg1=",&x);
  38. // create dst object
  39. UA_[_i].new(&obj2);
  40. pos = 0;
  41. retval = UA_[_i].decodeBinary(&msg1, &pos, obj2);
  42. // printf("retval=%d, ",retval); x.length = UA_[_i].calcSize(UA_NULL); x.data = (UA_Byte*) obj2; UA_ByteString_printx_hex("obj2=",&x);
  43. UA_ByteString_newMembers(&msg2,UA_[_i].calcSize(obj2));
  44. pos = 0;
  45. retval = UA_[_i].encodeBinary(obj2, &pos, &msg2);
  46. // printf("retval=%d, ",retval); x.length = pos; x.data = (UA_Byte*) msg2.data; UA_ByteString_printx_hex("msg2=",&x);
  47. ck_assert_msg(UA_ByteString_compare(&msg1,&msg2)==0,"messages differ idx=%d,name=%s",_i,UA_[_i].name);
  48. ck_assert_int_eq(retval,UA_SUCCESS);
  49. }
  50. END_TEST
  51. START_TEST (decodeShallFailWithTruncatedBufferButSurvive)
  52. {
  53. void *obj1 = UA_NULL, *obj2 = UA_NULL;
  54. UA_ByteString msg1;
  55. UA_Int32 retval, pos;
  56. retval = UA_[_i].new(&obj1);
  57. UA_ByteString_newMembers(&msg1,UA_[_i].calcSize(obj1));
  58. pos = 0;
  59. retval = UA_[_i].encodeBinary(obj1, &pos, &msg1);
  60. ck_assert_int_eq(retval,UA_SUCCESS);
  61. UA_[_i].new(&obj2);
  62. pos = 0;
  63. msg1.length = msg1.length / 2;
  64. retval = UA_[_i].decodeBinary(&msg1, &pos, obj2);
  65. ck_assert_msg(retval!=UA_SUCCESS,"testing %s with half buffer",UA_[_i].name);
  66. pos = 0;
  67. msg1.length = msg1.length / 4;
  68. retval = UA_[_i].decodeBinary(&msg1, &pos, obj2);
  69. ck_assert_msg(retval!=UA_SUCCESS,"testing %s with quarter buffer",UA_[_i].name);
  70. }
  71. END_TEST
  72. int main() {
  73. int number_failed = 0;
  74. SRunner *sr;
  75. Suite *s = suite_create("testMemoryHandling");
  76. TCase *tc = tcase_create("Empty Objects");
  77. tcase_add_loop_test(tc, newAndEmptyObjectShallBeDeleted,UA_BOOLEAN,UA_INVALIDTYPE-1);
  78. tcase_add_loop_test(tc, encodeShallYieldDecode,UA_BOOLEAN,UA_INVALIDTYPE-1);
  79. suite_add_tcase(s,tc);
  80. tc = tcase_create("Truncated Buffers");
  81. tcase_add_loop_test(tc, decodeShallFailWithTruncatedBufferButSurvive,UA_BOOLEAN,UA_INVALIDTYPE-1);
  82. suite_add_tcase(s,tc);
  83. sr = srunner_create(s);
  84. //for debugging puposes only, will break make check
  85. //srunner_set_fork_status(sr,CK_NOFORK);
  86. srunner_run_all(sr,CK_NORMAL);
  87. number_failed += srunner_ntests_failed(sr);
  88. srunner_free(sr);
  89. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  90. }