Просмотр исходного кода

Improve binary encoding/chunking tests

Bostjan Vesnicer лет назад: 7
Родитель
Сommit
3fa676c08f
2 измененных файлов с 91 добавлено и 0 удалено
  1. 46 0
      tests/check_chunking.c
  2. 45 0
      tests/check_client.c

+ 46 - 0
tests/check_chunking.c

@@ -108,12 +108,58 @@ START_TEST(encodeStringIntoFiveChunksShallWork) {
 }
 END_TEST
 
+START_TEST(encodeTwoStringsIntoTenChunksShallWork) {
+
+    size_t stringLength = 143; //number of elements within the array which should be encoded
+    size_t offset = 0; // encoding offset
+    size_t chunkCount = 10; // maximum chunk count
+    size_t chunkSize = 30; //size in bytes of each chunk
+
+    UA_String string;
+    UA_ChunkInfo ci;
+    bufIndex = 0;
+    counter = 0;
+    dataCount = 0;
+    UA_String_init(&string);
+    string.data = malloc(stringLength);
+    string.length = stringLength;
+    char tmpString[9] = {'o','p','e','n','6','2','5','4','1'};
+    //char tmpString[9] = {'1','4','5','2','6','n','e','p','o'};
+    buffers = UA_Array_new(chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
+    for(size_t i=0;i<chunkCount;i++){
+        UA_ByteString_allocBuffer(&buffers[i],chunkSize);
+    }
+
+    UA_ByteString workingBuffer=buffers[0];
+
+    for(size_t i=0;i<stringLength;i++){
+        size_t tmp = i % 9;
+        string.data[i] =  tmpString[tmp];
+    }
+
+    UA_StatusCode retval = UA_encodeBinary(&string,&UA_TYPES[UA_TYPES_STRING],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&offset);
+    ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
+    ck_assert_int_eq(counter,4); //5 chunks allocated - callback called 4 times
+    ck_assert_int_eq(UA_calcSizeBinary(&string,&UA_TYPES[UA_TYPES_STRING]), dataCount + offset);
+
+    retval = UA_encodeBinary(&string,&UA_TYPES[UA_TYPES_STRING],(UA_exchangeEncodeBuffer)sendChunkMockUp,&ci,&workingBuffer,&offset);
+    dataCount += offset; //last piece of data - no callback was called
+    ck_assert_uint_eq(retval,UA_STATUSCODE_GOOD);
+    ck_assert_int_eq(counter,9); //10 chunks allocated - callback called 4 times
+    ck_assert_int_eq(2 * UA_calcSizeBinary(&string,&UA_TYPES[UA_TYPES_STRING]), dataCount);
+
+    UA_Array_delete(buffers, chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);
+    UA_String_deleteMembers(&string);
+}
+END_TEST
+
 
 static Suite *testSuite_builtin(void) {
     Suite *s = suite_create("Chunked encoding");
     TCase *tc_message = tcase_create("encode chunking");
     tcase_add_test(tc_message,encodeArrayIntoFiveChunksShallWork);
     tcase_add_test(tc_message,encodeStringIntoFiveChunksShallWork);
+    tcase_add_test(tc_message,encodeTwoStringsIntoTenChunksShallWork);
     suite_add_tcase(s, tc_message);
     return s;
 }

+ 45 - 0
tests/check_client.c

@@ -18,6 +18,32 @@ UA_Boolean *running;
 UA_ServerNetworkLayer nl;
 pthread_t server_thread;
 
+static void
+addVariable(size_t size) {
+    /* Define the attribute of the myInteger variable node */
+    UA_VariableAttributes attr;
+    UA_VariableAttributes_init(&attr);
+    UA_Int32* array = malloc(size * sizeof(UA_Int32));
+    memset(array, 0, size * sizeof(UA_Int32));
+    UA_Variant_setArray(&attr.value, array, size, &UA_TYPES[UA_TYPES_INT32]);
+
+    char name[] = "my.variable";
+    attr.description = UA_LOCALIZEDTEXT("en_US", name);
+    attr.displayName = UA_LOCALIZEDTEXT("en_US", name);
+    attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
+
+    /* Add the variable node to the information model */
+    UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, name);
+    UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, name);
+    UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
+    UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
+    UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
+                              parentReferenceNodeId, myIntegerName,
+                              UA_NODEID_NULL, attr, NULL, NULL);
+
+    free(array);
+}
+
 static void * serverloop(void *_) {
     while(*running)
         UA_Server_run_iterate(server, true);
@@ -33,6 +59,7 @@ static void setup(void) {
     config.networkLayersSize = 1;
     server = UA_Server_new(config);
     UA_Server_run_startup(server);
+    addVariable(16366);
     pthread_create(&server_thread, NULL, serverloop, NULL);
 }
 
@@ -56,11 +83,29 @@ START_TEST(Client_connect) {
 }
 END_TEST
 
+START_TEST(Client_read) {
+    UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
+    UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:16664");
+    ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
+
+    UA_Variant val;
+    UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
+    retval = UA_Client_readValueAttribute(client, nodeId, &val);
+    ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
+
+    UA_Variant_deleteMembers(&val);
+
+    UA_Client_disconnect(client);
+    UA_Client_delete(client);
+}
+END_TEST
+
 static Suite* testSuite_Client(void) {
     Suite *s = suite_create("Client");
     TCase *tc_client = tcase_create("Client Basic");
     tcase_add_checked_fixture(tc_client, setup, teardown);
     tcase_add_test(tc_client, Client_connect);
+    tcase_add_test(tc_client, Client_read);
     suite_add_tcase(s,tc_client);
     return s;
 }