check_chunking.c 6.4 KB

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