check_chunking.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. static Suite *testSuite_builtin(void) {
  91. Suite *s = suite_create("Chunked encoding");
  92. TCase *tc_message = tcase_create("encode chunking");
  93. tcase_add_test(tc_message,encodeArrayIntoFiveChunksShallWork);
  94. tcase_add_test(tc_message,encodeStringIntoFiveChunksShallWork);
  95. suite_add_tcase(s, tc_message);
  96. return s;
  97. }
  98. int main(void) {
  99. int number_failed = 0;
  100. Suite *s;
  101. SRunner *sr;
  102. s = testSuite_builtin();
  103. sr = srunner_create(s);
  104. srunner_set_fork_status(sr, CK_NOFORK);
  105. srunner_run_all(sr, CK_NORMAL);
  106. number_failed += srunner_ntests_failed(sr);
  107. srunner_free(sr);
  108. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  109. }