check_client.c 13 KB

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