check_chunking.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. UA_ChunkInfo ci;
  30. bufIndex = 0;
  31. counter = 0;
  32. dataCount = 0;
  33. UA_Int32 *ar = UA_Array_new(arraySize,&UA_TYPES[UA_TYPES_INT32]);
  34. buffers = UA_Array_new(chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  35. for(size_t i=0;i<chunkCount;i++){
  36. UA_ByteString_allocBuffer(&buffers[i],chunkSize);
  37. }
  38. UA_ByteString workingBuffer=buffers[0];
  39. for(size_t i=0;i<arraySize;i++){
  40. ar[i]=(UA_Int32)i;
  41. }
  42. UA_Variant v;
  43. UA_Variant_setArrayCopy(&v,ar,arraySize,&UA_TYPES[UA_TYPES_INT32]);
  44. UA_StatusCode retval = UA_encodeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&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. }
  53. END_TEST
  54. START_TEST(encodeStringIntoFiveChunksShallWork) {
  55. size_t stringLength = 120; //number of elements within the array which should be encoded
  56. size_t offset = 0; // encoding offset
  57. size_t chunkCount = 6; // maximum chunk count
  58. size_t chunkSize = 30; //size in bytes of each chunk
  59. UA_String string;
  60. UA_ChunkInfo ci;
  61. bufIndex = 0;
  62. counter = 0;
  63. dataCount = 0;
  64. UA_String_init(&string);
  65. string.data = malloc(stringLength);
  66. string.length = stringLength;
  67. char tmpString[9] = {'o','p','e','n','6','2','5','4','1'};
  68. //char tmpString[9] = {'1','4','5','2','6','n','e','p','o'};
  69. buffers = UA_Array_new(chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  70. for(size_t i=0;i<chunkCount;i++){
  71. UA_ByteString_allocBuffer(&buffers[i],chunkSize);
  72. }
  73. UA_ByteString workingBuffer=buffers[0];
  74. for(size_t i=0;i<stringLength;i++){
  75. size_t tmp = i % 9;
  76. string.data[i] = tmpString[tmp];
  77. }
  78. UA_Variant v;
  79. UA_Variant_setScalarCopy(&v,&string,&UA_TYPES[UA_TYPES_STRING]);
  80. UA_StatusCode retval = UA_encodeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&offset);
  81. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  82. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  83. dataCount += offset; //last piece of data - no callback was called
  84. ck_assert_int_eq(UA_calcSizeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT]), dataCount);
  85. UA_Variant_deleteMembers(&v);
  86. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  87. UA_String_deleteMembers(&string);
  88. }
  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],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&offset);
  115. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  116. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  117. ck_assert_int_eq(UA_calcSizeBinary(&string,&UA_TYPES[UA_TYPES_STRING]), dataCount + offset);
  118. retval = UA_encodeBinary(&string,&UA_TYPES[UA_TYPES_STRING],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&offset);
  119. dataCount += offset; //last piece of data - no callback was called
  120. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  121. ck_assert_int_eq(counter,9); //10 chunks allocated - callback called 4 times
  122. ck_assert_int_eq(2 * UA_calcSizeBinary(&string,&UA_TYPES[UA_TYPES_STRING]), dataCount);
  123. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  124. UA_String_deleteMembers(&string);
  125. }
  126. END_TEST
  127. static Suite *testSuite_builtin(void) {
  128. Suite *s = suite_create("Chunked encoding");
  129. TCase *tc_message = tcase_create("encode chunking");
  130. tcase_add_test(tc_message,encodeArrayIntoFiveChunksShallWork);
  131. tcase_add_test(tc_message,encodeStringIntoFiveChunksShallWork);
  132. tcase_add_test(tc_message,encodeTwoStringsIntoTenChunksShallWork);
  133. suite_add_tcase(s, tc_message);
  134. return s;
  135. }
  136. int main(void) {
  137. int number_failed = 0;
  138. Suite *s;
  139. SRunner *sr;
  140. s = testSuite_builtin();
  141. sr = srunner_create(s);
  142. srunner_set_fork_status(sr, CK_NOFORK);
  143. srunner_run_all(sr, CK_NORMAL);
  144. number_failed += srunner_ntests_failed(sr);
  145. srunner_free(sr);
  146. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  147. }