Bläddra i källkod

simplify client securechannel test (#1385)

* simplify client securechannel test

* rename sleeping on simulated testing clock to UA_fakeSleep

* let the server start before calling with the client
Julius Pfrommer 7 år sedan
förälder
incheckning
6278950c4b

+ 1 - 1
tests/client/check_client.c

@@ -106,7 +106,7 @@ START_TEST(Client_renewSecureChannel) {
     ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
 
     /* Forward the time */
-    UA_sleep((UA_UInt32)((UA_Double)UA_ClientConfig_default.secureChannelLifeTime * 0.8));
+    UA_fakeSleep((UA_UInt32)((UA_Double)UA_ClientConfig_default.secureChannelLifeTime * 0.8));
 
     /* Now read */
     UA_Variant val;

+ 1 - 1
tests/client/check_client_async.c

@@ -48,7 +48,7 @@ asyncReadCallback(UA_Client *client, void *userdata,
                   UA_UInt32 requestId, const UA_ReadResponse *response) {
     UA_UInt16 *asyncCounter = (UA_UInt16*)userdata;
     (*asyncCounter)++;
-    UA_sleep(10);
+    UA_fakeSleep(10);
 }
 
 START_TEST(Client_read_async) {

+ 5 - 32
tests/client/check_client_securechannel.c

@@ -16,35 +16,8 @@
 UA_Server *server;
 UA_ServerConfig *config;
 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_default;
-    UA_Int32* array = (UA_Int32*)UA_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_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
-                              attr, NULL, NULL);
-
-    UA_free(array);
-}
-
 static void * serverloop(void *_) {
     while(*running)
         UA_Server_run_iterate(server, true);
@@ -57,8 +30,8 @@ static void setup(void) {
     config = UA_ServerConfig_new_default();
     server = UA_Server_new(config);
     UA_Server_run_startup(server);
-    addVariable(16366);
     pthread_create(&server_thread, NULL, serverloop, NULL);
+    UA_realSleep(100);
 }
 
 static void teardown(void) {
@@ -75,10 +48,10 @@ START_TEST(SecureChannel_timeout_max) {
     UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
     ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
 
-    UA_sleep(UA_ClientConfig_default.secureChannelLifeTime);
+    UA_fakeSleep(UA_ClientConfig_default.secureChannelLifeTime);
 
     UA_Variant val;
-    UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
+    UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
     retval = UA_Client_readValueAttribute(client, nodeId, &val);
     ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
 
@@ -94,11 +67,11 @@ START_TEST(SecureChannel_timeout_fail) {
     UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
     ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
 
-    UA_sleep(UA_ClientConfig_default.secureChannelLifeTime+1);
+    UA_fakeSleep(UA_ClientConfig_default.secureChannelLifeTime+1);
 
     UA_Variant val;
     UA_Variant_init(&val);
-    UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
+    UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
     retval = UA_Client_readValueAttribute(client, nodeId, &val);
     ck_assert(retval != UA_STATUSCODE_GOOD);
 

+ 1 - 1
tests/client/check_client_subscriptions.c

@@ -70,7 +70,7 @@ START_TEST(Client_subscription) {
                                                       NULL, &monId, 250);
     ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
 
-    UA_sleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
+    UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
 
     notificationReceived = false;
     retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);

+ 14 - 18
tests/server/check_discovery.c

@@ -16,19 +16,15 @@
 #endif
 
 #include <stdio.h>
-#include <stdlib.h>
 #include <unistd.h>
 #include <pthread.h>
-#include <ua_util.h>
-#include <ua_types_generated.h>
-#include <server/ua_server_internal.h>
-#include <ua_types.h>
 #include <fcntl.h>
+#include <check.h>
 
+#include "server/ua_server_internal.h"
 #include "ua_client.h"
 #include "ua_config_default.h"
 #include "ua_network_tcp.h"
-#include "check.h"
 #include "testing_clock.h"
 
 // set register timeout to 1 second so we are able to test it.
@@ -70,8 +66,8 @@ static void setup_lds(void) {
     pthread_create(&server_thread_lds, NULL, serverloop_lds, NULL);
 
     // wait until LDS started
-    UA_sleep(1000);
-    sleep(1);
+    UA_fakeSleep(1000);
+    UA_realSleep(1000);
 }
 
 static void teardown_lds(void) {
@@ -166,8 +162,8 @@ END_TEST
 
 START_TEST(Server_unregister_periodic) {
     // wait for first register delay
-    UA_sleep(1000);
-    sleep(1);
+    UA_fakeSleep(1000);
+    UA_realSleep(1000);
     UA_Server_removeRepeatedCallback(server_register, periodicRegisterCallbackId);
     UA_StatusCode retval = UA_Server_unregister_discovery(server_register,
                                                           "opc.tcp://localhost:4840");
@@ -471,29 +467,29 @@ END_TEST
 
 START_TEST(Util_wait_timeout) {
     // wait until server is removed by timeout. Additionally wait a few seconds more to be sure.
-    UA_sleep(100000 * checkWait);
-    sleep(1);
+    UA_fakeSleep(100000 * checkWait);
+    UA_realSleep(1000);
 }
 END_TEST
 
 #ifdef UA_ENABLE_DISCOVERY_MULTICAST
 START_TEST(Util_wait_mdns) {
-    UA_sleep(1000);
-    sleep(1);
+    UA_fakeSleep(1000);
+    UA_realSleep(1000);
 }
 END_TEST
 #endif
 
 START_TEST(Util_wait_startup) {
-    UA_sleep(1000);
-    sleep(1);
+    UA_fakeSleep(1000);
+    UA_realSleep(1000);
 }
 END_TEST
 
 START_TEST(Util_wait_retry) {
     // first retry is after 2 seconds, then 4, so it should be enough to wait 3 seconds
-    UA_sleep(3000);
-    sleep(3);
+    UA_fakeSleep(3000);
+    UA_realSleep(3000);
 }
 END_TEST
 

+ 3 - 3
tests/server/check_server_jobs.c

@@ -39,11 +39,11 @@ START_TEST(Server_addRemoveRepeatedCallback) {
     UA_Server_addRepeatedCallback(server, dummyCallback, NULL, 10, &id);
 
     /* Wait until the callback has surely timed out */
-    UA_sleep(15);
+    UA_fakeSleep(15);
     UA_Server_run_iterate(server, false);
 
     /* Wait a bit longer until the workers have picked up the dispatched callback */
-    UA_realsleep(100);
+    UA_realSleep(100);
     ck_assert_uint_eq(*executed, true);
 
     UA_Server_removeRepeatedCallback(server, id);
@@ -62,7 +62,7 @@ START_TEST(Server_repeatedCallbackRemoveItself) {
     cbId = UA_UInt64_new();
     UA_Server_addRepeatedCallback(server, removeItselfCallback, NULL, 10, cbId);
 
-    UA_sleep(15);
+    UA_fakeSleep(15);
     UA_Server_run_iterate(server, false);
 
     UA_UInt64_delete(cbId);

+ 2 - 2
tests/server/check_services_subscriptions.c

@@ -169,9 +169,9 @@ START_TEST(Server_publishCallback) {
         ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount);
 
     /* Sleep until the publishing interval times out */
-    UA_sleep((UA_UInt32)publishingInterval + 1);
+    UA_fakeSleep((UA_UInt32)publishingInterval + 1);
     UA_Server_run_iterate(server, false);
-    UA_realsleep(100);
+    UA_realSleep(100);
 
     LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry)
         ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount+1);

+ 23 - 10
tests/testing-plugins/testing_clock.c

@@ -2,11 +2,22 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-#include "testing_clock.h"
-#ifdef UA_ENABLE_MULTITHREADING
-#include <time.h>
+/* Enable POSIX features */
+#ifndef _XOPEN_SOURCE
+# define _XOPEN_SOURCE 600
+#endif
+#ifndef _DEFAULT_SOURCE
+# define _DEFAULT_SOURCE
+#endif
+/* On older systems we need to define _BSD_SOURCE.
+ * _DEFAULT_SOURCE is an alias for that. */
+#ifndef _BSD_SOURCE
+# define _BSD_SOURCE
 #endif
 
+#include <time.h>
+#include "testing_clock.h"
+
 UA_DateTime testingClock = 0;
 
 UA_DateTime UA_DateTime_now(void) {
@@ -18,17 +29,19 @@ UA_DateTime UA_DateTime_nowMonotonic(void) {
 }
 
 void
-UA_sleep(UA_UInt32 duration) {
+UA_fakeSleep(UA_UInt32 duration) {
     testingClock += duration * UA_MSEC_TO_DATETIME;
 }
 
-#define NANO_SECOND_MULTIPLIER 1000000 // 1 millisecond = 1,000,000 Nanoseconds
+/* 1 millisecond = 1,000,000 Nanoseconds */
+#define NANO_SECOND_MULTIPLIER 1000000
+
 void
-UA_realsleep(UA_UInt32 duration) {
-#ifdef UA_ENABLE_MULTITHREADING
+UA_realSleep(UA_UInt32 duration) {
+    UA_UInt32 sec = duration / 1000;
+    UA_UInt32 ns = (duration % 1000) * NANO_SECOND_MULTIPLIER;
     struct timespec sleepValue;
-    sleepValue.tv_sec = 0;
-    sleepValue.tv_nsec = duration * NANO_SECOND_MULTIPLIER;
+    sleepValue.tv_sec = sec;
+    sleepValue.tv_nsec = ns;
     nanosleep(&sleepValue, NULL);
-#endif
 }

+ 2 - 2
tests/testing-plugins/testing_clock.h

@@ -15,10 +15,10 @@
  * UA_DateTime UA_EXPORT UA_DateTime_nowMonotonic(void); */
 
 /* Forwards the testing clock by the given duration in ms */
-void UA_sleep(UA_UInt32 duration);
+void UA_fakeSleep(UA_UInt32 duration);
 
 /* Sleep for the duration in milliseconds. Used to wait for workers to complete.
  * Does not do anything in single-threaded mode. */
-void UA_realsleep(UA_UInt32 duration);
+void UA_realSleep(UA_UInt32 duration);
 
 #endif /* TESTING_CLOCK_H_ */