check_chunking.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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(void *_, 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. bufIndex = 0;
  31. counter = 0;
  32. dataCount = 0;
  33. buffers = (UA_ByteString*)UA_Array_new(chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  34. for(size_t i=0;i<chunkCount;i++){
  35. UA_ByteString_allocBuffer(&buffers[i],chunkSize);
  36. }
  37. UA_Int32 *ar = (UA_Int32*)UA_Array_new(arraySize,&UA_TYPES[UA_TYPES_INT32]);
  38. for(size_t i = 0; i < arraySize; i++)
  39. ar[i] = (UA_Int32)i;
  40. UA_Variant v;
  41. UA_Variant_setArrayCopy(&v, ar, arraySize, &UA_TYPES[UA_TYPES_INT32]);
  42. UA_ByteString workingBuffer = buffers[0];
  43. UA_Byte *pos = workingBuffer.data;
  44. const UA_Byte *end = &workingBuffer.data[workingBuffer.length];
  45. UA_StatusCode retval = UA_encodeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT],
  46. &pos, &end, sendChunkMockUp, NULL);
  47. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  48. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  49. dataCount += (uintptr_t)(pos - buffers[bufIndex].data);
  50. ck_assert_int_eq(UA_calcSizeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT]), dataCount);
  51. UA_Variant_deleteMembers(&v);
  52. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  53. UA_Array_delete(ar, arraySize, &UA_TYPES[UA_TYPES_INT32]);
  54. } END_TEST
  55. START_TEST(encodeStringIntoFiveChunksShallWork) {
  56. size_t stringLength = 120; //number of elements within the array which should be encoded
  57. size_t chunkCount = 6; // maximum chunk count
  58. size_t chunkSize = 30; //size in bytes of each chunk
  59. UA_String string;
  60. bufIndex = 0;
  61. counter = 0;
  62. dataCount = 0;
  63. UA_String_init(&string);
  64. string.data = (UA_Byte*)UA_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_ByteString*)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_Byte *pos = workingBuffer.data;
  80. const UA_Byte *end = &workingBuffer.data[workingBuffer.length];
  81. UA_StatusCode retval = UA_encodeBinary(&v, &UA_TYPES[UA_TYPES_VARIANT],
  82. &pos, &end, sendChunkMockUp, NULL);
  83. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  84. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  85. dataCount += (uintptr_t)(pos - buffers[bufIndex].data);
  86. ck_assert_int_eq(UA_calcSizeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT]), dataCount);
  87. UA_Variant_deleteMembers(&v);
  88. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  89. UA_String_deleteMembers(&string);
  90. } END_TEST
  91. START_TEST(encodeTwoStringsIntoTenChunksShallWork) {
  92. size_t stringLength = 143; //number of elements within the array which should be encoded
  93. size_t chunkCount = 10; // maximum chunk count
  94. size_t chunkSize = 30; //size in bytes of each chunk
  95. UA_String string;
  96. bufIndex = 0;
  97. counter = 0;
  98. dataCount = 0;
  99. UA_String_init(&string);
  100. string.data = (UA_Byte*)UA_malloc(stringLength);
  101. string.length = stringLength;
  102. char tmpString[9] = {'o','p','e','n','6','2','5','4','1'};
  103. buffers = (UA_ByteString*)UA_Array_new(chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  104. for(size_t i=0;i<chunkCount;i++){
  105. UA_ByteString_allocBuffer(&buffers[i],chunkSize);
  106. }
  107. UA_ByteString workingBuffer=buffers[0];
  108. for(size_t i=0;i<stringLength;i++){
  109. size_t tmp = i % 9;
  110. string.data[i] = tmpString[tmp];
  111. }
  112. UA_Byte *pos = workingBuffer.data;
  113. const UA_Byte *end = &workingBuffer.data[workingBuffer.length];
  114. UA_StatusCode retval = UA_encodeBinary(&string, &UA_TYPES[UA_TYPES_STRING],
  115. &pos, &end, sendChunkMockUp, NULL);
  116. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  117. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  118. size_t offset = (uintptr_t)(pos - buffers[bufIndex].data);
  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. &pos, &end, sendChunkMockUp, NULL);
  122. dataCount += (uintptr_t)(pos - buffers[bufIndex].data);
  123. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  124. ck_assert_int_eq(counter,9); //10 chunks allocated - callback called 4 times
  125. ck_assert_int_eq(2 * UA_calcSizeBinary(&string,&UA_TYPES[UA_TYPES_STRING]), dataCount);
  126. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  127. UA_String_deleteMembers(&string);
  128. } END_TEST
  129. int main(void) {
  130. Suite *s = suite_create("Chunked encoding");
  131. TCase *tc_message = tcase_create("encode chunking");
  132. tcase_add_test(tc_message,encodeArrayIntoFiveChunksShallWork);
  133. tcase_add_test(tc_message,encodeStringIntoFiveChunksShallWork);
  134. tcase_add_test(tc_message,encodeTwoStringsIntoTenChunksShallWork);
  135. suite_add_tcase(s, tc_message);
  136. SRunner *sr = srunner_create(s);
  137. srunner_set_fork_status(sr, CK_NOFORK);
  138. srunner_run_all(sr, CK_NORMAL);
  139. int number_failed = srunner_ntests_failed(sr);
  140. srunner_free(sr);
  141. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  142. }