check_chunking.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "ua_types.h"
  2. #include "ua_types_encoding_binary.h"
  3. #include "ua_types_generated.h"
  4. #include "ua_types_generated_encoding_binary.h"
  5. #include "ua_securechannel.h"
  6. #include "ua_util.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 sendChunkMockUp(UA_ChunkInfo *ci, UA_ByteString *dst, size_t offset) {
  13. bufIndex++;
  14. dst->data = buffers[bufIndex].data;
  15. dst->length = buffers[bufIndex].length;
  16. counter++;
  17. dataCount += offset;
  18. return UA_STATUSCODE_GOOD;
  19. }
  20. START_TEST(encodeArrayIntoFiveChunksShallWork) {
  21. size_t arraySize = 30; //number of elements within the array which should be encoded
  22. size_t offset = 0; // encoding offset
  23. size_t chunkCount = 6; // maximum chunk count
  24. size_t chunkSize = 30; //size in bytes of each chunk
  25. UA_ChunkInfo ci;
  26. bufIndex = 0;
  27. counter = 0;
  28. dataCount = 0;
  29. UA_Int32 *ar = UA_Array_new(arraySize,&UA_TYPES[UA_TYPES_INT32]);
  30. buffers = UA_Array_new(chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  31. for(size_t i=0;i<chunkCount;i++){
  32. UA_ByteString_allocBuffer(&buffers[i],chunkSize);
  33. }
  34. UA_ByteString workingBuffer=buffers[0];
  35. for(size_t i=0;i<arraySize;i++){
  36. ar[i]=(UA_Int32)i;
  37. }
  38. UA_Variant v;
  39. UA_Variant_setArrayCopy(&v,ar,arraySize,&UA_TYPES[UA_TYPES_INT32]);
  40. UA_StatusCode retval = UA_encodeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&offset);
  41. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  42. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  43. dataCount += offset; //last piece of data - no callback was called
  44. ck_assert_int_eq(UA_calcSizeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT]), dataCount);
  45. UA_Variant_deleteMembers(&v);
  46. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  47. UA_Array_delete(ar, arraySize, &UA_TYPES[UA_TYPES_INT32]);
  48. }
  49. END_TEST
  50. START_TEST(encodeStringIntoFiveChunksShallWork) {
  51. size_t stringLength = 120; //number of elements within the array which should be encoded
  52. size_t offset = 0; // encoding offset
  53. size_t chunkCount = 6; // maximum chunk count
  54. size_t chunkSize = 30; //size in bytes of each chunk
  55. UA_String string;
  56. UA_ChunkInfo ci;
  57. bufIndex = 0;
  58. counter = 0;
  59. dataCount = 0;
  60. UA_String_init(&string);
  61. string.data = malloc(stringLength);
  62. string.length = stringLength;
  63. char tmpString[9] = {'o','p','e','n','6','2','5','4','1'};
  64. //char tmpString[9] = {'1','4','5','2','6','n','e','p','o'};
  65. buffers = UA_Array_new(chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  66. for(size_t i=0;i<chunkCount;i++){
  67. UA_ByteString_allocBuffer(&buffers[i],chunkSize);
  68. }
  69. UA_ByteString workingBuffer=buffers[0];
  70. for(size_t i=0;i<stringLength;i++){
  71. size_t tmp = i % 9;
  72. string.data[i] = tmpString[tmp];
  73. }
  74. UA_Variant v;
  75. UA_Variant_setScalarCopy(&v,&string,&UA_TYPES[UA_TYPES_STRING]);
  76. UA_StatusCode retval = UA_encodeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&offset);
  77. ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
  78. ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
  79. dataCount += offset; //last piece of data - no callback was called
  80. ck_assert_int_eq(UA_calcSizeBinary(&v,&UA_TYPES[UA_TYPES_VARIANT]), dataCount);
  81. UA_Variant_deleteMembers(&v);
  82. UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
  83. UA_String_deleteMembers(&string);
  84. }
  85. END_TEST
  86. static Suite *testSuite_builtin(void) {
  87. Suite *s = suite_create("Chunked encoding");
  88. TCase *tc_message = tcase_create("encode chunking");
  89. tcase_add_test(tc_message,encodeArrayIntoFiveChunksShallWork);
  90. tcase_add_test(tc_message,encodeStringIntoFiveChunksShallWork);
  91. suite_add_tcase(s, tc_message);
  92. return s;
  93. }
  94. int main(void) {
  95. int number_failed = 0;
  96. Suite *s;
  97. SRunner *sr;
  98. s = testSuite_builtin();
  99. sr = srunner_create(s);
  100. srunner_set_fork_status(sr, CK_NOFORK);
  101. srunner_run_all(sr, CK_NORMAL);
  102. number_failed += srunner_ntests_failed(sr);
  103. srunner_free(sr);
  104. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  105. }