check_chunking.c 6.6 KB

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