check_client.c 13 KB

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