check_chunking.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_ByteString *dst, size_t offset) {
  18. bufIndex++;
  19. dst->data = buffers[bufIndex].data;
  20. dst->length = buffers[bufIndex].length;
  21. counter++;
  22. dataCount += offset;
  23. return UA_STATUSCODE_GOOD;
  24. }
  25. START_TEST(encodeArrayIntoFiveChunksShallWork) {
  26. size_t arraySize = 30; //number of elements within the array which should be encoded
  27. size_t offset = 0; // encoding offset
  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_StatusCode retval = UA_encodeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT],
  46. (UA_exchangeEncodeBuffer)sendChunkMockUp,
  47. &ci,&workingBuffer,&offset);
  48. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  49. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  50. dataCount += offset; //last piece of data - no callback was called
  51. ck_assert_int_eq(UA_calcSizeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT]), dataCount);
  52. UA_Variant_deleteMembers(&v);
  53. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  54. UA_Array_delete(ar, arraySize, &UA_TYPES[UA_TYPES_INT32]);
  55. }
  56. END_TEST
  57. START_TEST(encodeStringIntoFiveChunksShallWork) {
  58. size_t stringLength = 120; //number of elements within the array which should be encoded
  59. size_t offset = 0; // encoding offset
  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_StatusCode retval = UA_encodeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&offset);
  84. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  85. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  86. dataCount += offset; //last piece of data - no callback was called
  87. ck_assert_int_eq(UA_calcSizeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT]), dataCount);
  88. UA_Variant_deleteMembers(&v);
  89. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  90. UA_String_deleteMembers(&string);
  91. }
  92. END_TEST
  93. static Suite *testSuite_builtin(void) {
  94. Suite *s = suite_create("Chunked encoding");
  95. TCase *tc_message = tcase_create("encode chunking");
  96. tcase_add_test(tc_message,encodeArrayIntoFiveChunksShallWork);
  97. tcase_add_test(tc_message,encodeStringIntoFiveChunksShallWork);
  98. suite_add_tcase(s, tc_message);
  99. return s;
  100. }
  101. int main(void) {
  102. int number_failed = 0;
  103. Suite *s;
  104. SRunner *sr;
  105. s = testSuite_builtin();
  106. sr = srunner_create(s);
  107. srunner_set_fork_status(sr, CK_NOFORK);
  108. srunner_run_all(sr, CK_NORMAL);
  109. number_failed += srunner_ntests_failed(sr);
  110. srunner_free(sr);
  111. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  112. }