check_chunking.c 6.8 KB

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