check_chunking.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "ua_types.h"
  2. #include "ua_types_encoding_binary.h"
  3. #include "ua_types_generated.h"
  4. #include "ua_types_generated_handling.h"
  5. #include "ua_types_generated_encoding_binary.h"
  6. #include "ua_securechannel.h"
  7. #include "ua_util.h"
  8. #include "check.h"
  9. UA_ByteString *buffers;
  10. size_t bufIndex;
  11. size_t counter;
  12. size_t dataCount;
  13. static UA_StatusCode sendChunkMockUp(UA_ChunkInfo *ci, UA_ByteString *dst, size_t offset) {
  14. bufIndex++;
  15. dst->data = buffers[bufIndex].data;
  16. dst->length = buffers[bufIndex].length;
  17. counter++;
  18. dataCount += offset;
  19. return UA_STATUSCODE_GOOD;
  20. }
  21. START_TEST(encodeArrayIntoFiveChunksShallWork) {
  22. size_t arraySize = 30; //number of elements within the array which should be encoded
  23. size_t offset = 0; // encoding offset
  24. size_t chunkCount = 6; // maximum chunk count
  25. size_t chunkSize = 30; //size in bytes of each chunk
  26. UA_ChunkInfo ci;
  27. bufIndex = 0;
  28. counter = 0;
  29. dataCount = 0;
  30. UA_Int32 *ar = UA_Array_new(arraySize,&UA_TYPES[UA_TYPES_INT32]);
  31. buffers = UA_Array_new(chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  32. for(size_t i=0;i<chunkCount;i++){
  33. UA_ByteString_allocBuffer(&buffers[i],chunkSize);
  34. }
  35. UA_ByteString workingBuffer=buffers[0];
  36. for(size_t i=0;i<arraySize;i++){
  37. ar[i]=(UA_Int32)i;
  38. }
  39. UA_Variant v;
  40. UA_Variant_setArrayCopy(&v,ar,arraySize,&UA_TYPES[UA_TYPES_INT32]);
  41. UA_StatusCode retval = UA_encodeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&offset);
  42. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  43. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  44. dataCount += offset; //last piece of data - no callback was called
  45. ck_assert_int_eq(UA_calcSizeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT]), dataCount);
  46. UA_Variant_deleteMembers(&v);
  47. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  48. UA_Array_delete(ar, arraySize, &UA_TYPES[UA_TYPES_INT32]);
  49. }
  50. END_TEST
  51. START_TEST(encodeStringIntoFiveChunksShallWork) {
  52. size_t stringLength = 120; //number of elements within the array which should be encoded
  53. size_t offset = 0; // encoding offset
  54. size_t chunkCount = 6; // maximum chunk count
  55. size_t chunkSize = 30; //size in bytes of each chunk
  56. UA_String string;
  57. UA_ChunkInfo ci;
  58. bufIndex = 0;
  59. counter = 0;
  60. dataCount = 0;
  61. UA_String_init(&string);
  62. string.data = malloc(stringLength);
  63. string.length = stringLength;
  64. char tmpString[9] = {'o','p','e','n','6','2','5','4','1'};
  65. //char tmpString[9] = {'1','4','5','2','6','n','e','p','o'};
  66. buffers = UA_Array_new(chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  67. for(size_t i=0;i<chunkCount;i++){
  68. UA_ByteString_allocBuffer(&buffers[i],chunkSize);
  69. }
  70. UA_ByteString workingBuffer=buffers[0];
  71. for(size_t i=0;i<stringLength;i++){
  72. size_t tmp = i % 9;
  73. string.data[i] = tmpString[tmp];
  74. }
  75. UA_Variant v;
  76. UA_Variant_setScalarCopy(&v,&string,&UA_TYPES[UA_TYPES_STRING]);
  77. UA_StatusCode retval = UA_encodeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&offset);
  78. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  79. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  80. dataCount += offset; //last piece of data - no callback was called
  81. ck_assert_int_eq(UA_calcSizeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT]), dataCount);
  82. UA_Variant_deleteMembers(&v);
  83. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  84. UA_String_deleteMembers(&string);
  85. }
  86. END_TEST
  87. static Suite *testSuite_builtin(void) {
  88. Suite *s = suite_create("Chunked encoding");
  89. TCase *tc_message = tcase_create("encode chunking");
  90. tcase_add_test(tc_message,encodeArrayIntoFiveChunksShallWork);
  91. tcase_add_test(tc_message,encodeStringIntoFiveChunksShallWork);
  92. suite_add_tcase(s, tc_message);
  93. return s;
  94. }
  95. int main(void) {
  96. int number_failed = 0;
  97. Suite *s;
  98. SRunner *sr;
  99. s = testSuite_builtin();
  100. sr = srunner_create(s);
  101. srunner_set_fork_status(sr, CK_NOFORK);
  102. srunner_run_all(sr, CK_NORMAL);
  103. number_failed += srunner_ntests_failed(sr);
  104. srunner_free(sr);
  105. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  106. }