check_client.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <open62541/client_config_default.h>
  5. #include <open62541/server_config_default.h>
  6. #include "client/ua_client_internal.h"
  7. #include "ua_server_internal.h"
  8. #include <check.h>
  9. #include <stdlib.h>
  10. #include "testing_clock.h"
  11. #include "testing_networklayers.h"
  12. #include "thread_wrapper.h"
  13. UA_Server *server;
  14. UA_Boolean running;
  15. UA_ServerNetworkLayer nl;
  16. THREAD_HANDLE server_thread;
  17. static void
  18. addVariable(size_t size) {
  19. /* Define the attribute of the myInteger variable node */
  20. UA_VariableAttributes attr = UA_VariableAttributes_default;
  21. UA_Int32* array = (UA_Int32*)UA_malloc(size * sizeof(UA_Int32));
  22. memset(array, 0, size * sizeof(UA_Int32));
  23. UA_Variant_setArray(&attr.value, array, size, &UA_TYPES[UA_TYPES_INT32]);
  24. char name[] = "my.variable";
  25. attr.description = UA_LOCALIZEDTEXT("en-US", name);
  26. attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  27. attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  28. /* Add the variable node to the information model */
  29. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, name);
  30. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, name);
  31. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  32. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  33. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  34. parentReferenceNodeId, myIntegerName,
  35. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  36. attr, NULL, NULL);
  37. UA_free(array);
  38. }
  39. THREAD_CALLBACK(serverloop) {
  40. while(running)
  41. UA_Server_run_iterate(server, true);
  42. return 0;
  43. }
  44. static void setup(void) {
  45. running = true;
  46. server = UA_Server_new();
  47. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  48. UA_Server_run_startup(server);
  49. addVariable(16366);
  50. THREAD_CREATE(server_thread, serverloop);
  51. }
  52. static void teardown(void) {
  53. running = false;
  54. THREAD_JOIN(server_thread);
  55. UA_Server_run_shutdown(server);
  56. UA_Server_delete(server);
  57. }
  58. START_TEST(Client_connect) {
  59. UA_Client *client = UA_Client_new();
  60. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  61. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  62. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  63. UA_Client_disconnect(client);
  64. UA_Client_delete(client);
  65. }
  66. END_TEST
  67. START_TEST(Client_connect_username) {
  68. UA_Client *client = UA_Client_new();
  69. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  70. UA_StatusCode retval = UA_Client_connect_username(client, "opc.tcp://localhost:4840", "user1", "password");
  71. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  72. UA_Client_disconnect(client);
  73. UA_Client_delete(client);
  74. }
  75. END_TEST
  76. START_TEST(Client_endpoints) {
  77. UA_Client *client = UA_Client_new();
  78. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  79. UA_EndpointDescription* endpointArray = NULL;
  80. size_t endpointArraySize = 0;
  81. UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:4840",
  82. &endpointArraySize, &endpointArray);
  83. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  84. ck_assert(endpointArraySize > 0);
  85. UA_Array_delete(endpointArray,endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  86. UA_Client_delete(client);
  87. }
  88. END_TEST
  89. START_TEST(Client_endpoints_empty) {
  90. /* Issue a getEndpoints call with empty endpointUrl.
  91. * Using UA_Client_getEndpoints automatically passes the client->endpointUrl as requested endpointUrl.
  92. * The spec says:
  93. * The Server should return a suitable default URL if it does not recognize the HostName in the URL.
  94. *
  95. * See https://github.com/open62541/open62541/issues/775
  96. */
  97. UA_Client *client = UA_Client_new();
  98. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  99. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  100. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  101. UA_GetEndpointsRequest request;
  102. UA_GetEndpointsRequest_init(&request);
  103. request.requestHeader.timestamp = UA_DateTime_now();
  104. request.requestHeader.timeoutHint = 10000;
  105. UA_GetEndpointsResponse response;
  106. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_GETENDPOINTSREQUEST],
  107. &response, &UA_TYPES[UA_TYPES_GETENDPOINTSRESPONSE]);
  108. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  109. ck_assert(response.endpointsSize > 0);
  110. UA_GetEndpointsResponse_deleteMembers(&response);
  111. UA_GetEndpointsRequest_deleteMembers(&request);
  112. UA_Client_delete(client);
  113. }
  114. END_TEST
  115. START_TEST(Client_read) {
  116. UA_Client *client = UA_Client_new();
  117. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  118. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  119. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  120. UA_Variant val;
  121. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  122. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  123. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  124. UA_Variant_deleteMembers(&val);
  125. UA_Client_disconnect(client);
  126. UA_Client_delete(client);
  127. }
  128. END_TEST
  129. START_TEST(Client_renewSecureChannel) {
  130. UA_Client *client = UA_Client_new();
  131. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  132. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  133. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  134. /* Forward the time */
  135. UA_ClientConfig *cc = UA_Client_getConfig(client);
  136. UA_fakeSleep((UA_UInt32)((UA_Double)cc->secureChannelLifeTime * 0.8));
  137. /* Make the client renew the channel */
  138. retval = UA_Client_run_iterate(client, 0);
  139. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  140. /* Now read */
  141. UA_Variant val;
  142. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  143. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  144. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  145. UA_Variant_deleteMembers(&val);
  146. UA_Client_disconnect(client);
  147. UA_Client_delete(client);
  148. } END_TEST
  149. START_TEST(Client_renewSecureChannelWithActiveSubscription) {
  150. UA_Client *client = UA_Client_new();
  151. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  152. UA_ClientConfig *cc = UA_Client_getConfig(client);
  153. cc->secureChannelLifeTime = 10000;
  154. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  155. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  156. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  157. /* Force the server to send keep alive responses every second to trigg
  158. * the client to send new publish requests. Requests from the client
  159. * will make the server to change to the new SecurityToken after renewal.
  160. */
  161. request.requestedPublishingInterval = 1000;
  162. request.requestedMaxKeepAliveCount = 1;
  163. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  164. NULL, NULL, NULL);
  165. (void)response;
  166. for(int i = 0; i < 15; ++i) {
  167. UA_sleep_ms(1000);
  168. retval = UA_Client_run_iterate(client, 0);
  169. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  170. }
  171. UA_Client_disconnect(client);
  172. UA_Client_delete(client);
  173. } END_TEST
  174. START_TEST(Client_reconnect) {
  175. UA_Client *client = UA_Client_new();
  176. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  177. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  178. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  179. UA_Variant val;
  180. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  181. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  182. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  183. UA_Variant_deleteMembers(&val);
  184. // restart server to test reconnect
  185. teardown();
  186. setup();
  187. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  188. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  189. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  190. ck_assert_msg(retval == UA_STATUSCODE_GOOD, UA_StatusCode_name(retval));
  191. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  192. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  193. UA_Variant_deleteMembers(&val);
  194. UA_Client_disconnect(client);
  195. UA_Client_delete(client);
  196. }
  197. END_TEST
  198. START_TEST(Client_delete_without_connect) {
  199. UA_Client *client = UA_Client_new();
  200. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  201. ck_assert(client != NULL);
  202. UA_Client_delete(client);
  203. }
  204. END_TEST
  205. // TODO ACTIVATE THE TESTS WHEN SESSION RECOVERY IS GOOD
  206. #ifdef UA_SESSION_RECOVERY
  207. START_TEST(Client_activateSessionClose) {
  208. // restart server
  209. teardown();
  210. setup();
  211. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  212. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  213. UA_Client *client = UA_Client_new(clientConfig);
  214. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  215. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  216. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  217. UA_Variant val;
  218. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  219. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  220. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  221. UA_Variant_deleteMembers(&val);
  222. UA_Client_disconnect(client);
  223. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  224. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  225. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  226. nodeId = UA_NODEID_STRING(1, "my.variable");
  227. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  228. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  229. UA_Variant_deleteMembers(&val);
  230. UA_Client_disconnect(client);
  231. UA_Client_delete(client);
  232. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  233. }
  234. END_TEST
  235. START_TEST(Client_activateSessionTimeout) {
  236. // restart server
  237. teardown();
  238. setup();
  239. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  240. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  241. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  242. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  243. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  244. UA_Variant val;
  245. UA_Variant_init(&val);
  246. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  247. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  248. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  249. UA_Variant_deleteMembers(&val);
  250. UA_Client_recv = client->connection.recv;
  251. client->connection.recv = UA_Client_recvTesting;
  252. /* Simulate network cable unplugged (no response from server) */
  253. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  254. UA_Variant_init(&val);
  255. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  256. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  257. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  258. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  259. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  260. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  261. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  262. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  263. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  264. UA_Variant_deleteMembers(&val);
  265. UA_Client_delete(client);
  266. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  267. }
  268. END_TEST
  269. #endif /* UA_SESSION_RECOVERY */
  270. static Suite* testSuite_Client(void) {
  271. Suite *s = suite_create("Client");
  272. TCase *tc_client = tcase_create("Client Basic");
  273. tcase_add_checked_fixture(tc_client, setup, teardown);
  274. tcase_add_test(tc_client, Client_connect);
  275. tcase_add_test(tc_client, Client_connect_username);
  276. tcase_add_test(tc_client, Client_delete_without_connect);
  277. tcase_add_test(tc_client, Client_endpoints);
  278. tcase_add_test(tc_client, Client_endpoints_empty);
  279. tcase_add_test(tc_client, Client_read);
  280. suite_add_tcase(s,tc_client);
  281. TCase *tc_client_reconnect = tcase_create("Client Reconnect");
  282. tcase_add_checked_fixture(tc_client_reconnect, setup, teardown);
  283. tcase_add_test(tc_client_reconnect, Client_renewSecureChannel);
  284. tcase_add_test(tc_client_reconnect, Client_renewSecureChannelWithActiveSubscription);
  285. tcase_add_test(tc_client_reconnect, Client_reconnect);
  286. #ifdef UA_SESSION_RECOVERY
  287. tcase_add_test(tc_client_reconnect, Client_activateSessionClose);
  288. tcase_add_test(tc_client_reconnect, Client_activateSessionTimeout);
  289. #endif /* UA_SESSION_RECOVERY */
  290. suite_add_tcase(s,tc_client_reconnect);
  291. return s;
  292. }
  293. int main(void) {
  294. Suite *s = testSuite_Client();
  295. SRunner *sr = srunner_create(s);
  296. srunner_set_fork_status(sr, CK_NOFORK);
  297. srunner_run_all(sr,CK_NORMAL);
  298. int number_failed = srunner_ntests_failed(sr);
  299. srunner_free(sr);
  300. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  301. }