check_client.c 13 KB

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