check_client.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_msg(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_msg(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. /* Now read */
  138. UA_Variant val;
  139. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  140. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  141. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  142. UA_Variant_deleteMembers(&val);
  143. UA_Client_disconnect(client);
  144. UA_Client_delete(client);
  145. } END_TEST
  146. START_TEST(Client_renewSecureChannelWithActiveSubscription) {
  147. UA_Client *client = UA_Client_new();
  148. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  149. UA_ClientConfig *cc = UA_Client_getConfig(client);
  150. cc->secureChannelLifeTime = 10000;
  151. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  152. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  153. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  154. request.requestedLifetimeCount = 1000;
  155. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  156. NULL, NULL, NULL);
  157. (void)response;
  158. for(int i = 0; i < 15; ++i) {
  159. UA_sleep_ms(1000);
  160. retval = UA_Client_run_iterate(client, 0);
  161. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  162. }
  163. UA_Client_disconnect(client);
  164. UA_Client_delete(client);
  165. } END_TEST
  166. START_TEST(Client_reconnect) {
  167. UA_Client *client = UA_Client_new();
  168. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  169. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  170. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  171. UA_Variant val;
  172. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  173. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  174. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  175. UA_Variant_deleteMembers(&val);
  176. // restart server to test reconnect
  177. teardown();
  178. setup();
  179. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  180. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  181. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  182. ck_assert_msg(retval == UA_STATUSCODE_GOOD, UA_StatusCode_name(retval));
  183. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  184. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  185. UA_Variant_deleteMembers(&val);
  186. UA_Client_disconnect(client);
  187. UA_Client_delete(client);
  188. }
  189. END_TEST
  190. START_TEST(Client_delete_without_connect) {
  191. UA_Client *client = UA_Client_new();
  192. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  193. ck_assert_msg(client != NULL);
  194. UA_Client_delete(client);
  195. }
  196. END_TEST
  197. // TODO ACTIVATE THE TESTS WHEN SESSION RECOVERY IS GOOD
  198. #ifdef UA_SESSION_RECOVERY
  199. START_TEST(Client_activateSessionClose) {
  200. // restart server
  201. teardown();
  202. setup();
  203. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  204. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  205. UA_Client *client = UA_Client_new(clientConfig);
  206. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  207. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  208. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  209. UA_Variant val;
  210. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  211. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  212. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  213. UA_Variant_deleteMembers(&val);
  214. UA_Client_disconnect(client);
  215. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  216. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  217. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  218. 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. UA_Client_delete(client);
  224. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  225. }
  226. END_TEST
  227. START_TEST(Client_activateSessionTimeout) {
  228. // restart server
  229. teardown();
  230. setup();
  231. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  232. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  233. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  234. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  235. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  236. UA_Variant val;
  237. UA_Variant_init(&val);
  238. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  239. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  240. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  241. UA_Variant_deleteMembers(&val);
  242. UA_Client_recv = client->connection.recv;
  243. client->connection.recv = UA_Client_recvTesting;
  244. /* Simulate network cable unplugged (no response from server) */
  245. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  246. UA_Variant_init(&val);
  247. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  248. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  249. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  250. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  251. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  252. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  253. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  254. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  255. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  256. UA_Variant_deleteMembers(&val);
  257. UA_Client_delete(client);
  258. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  259. }
  260. END_TEST
  261. #endif /* UA_SESSION_RECOVERY */
  262. static Suite* testSuite_Client(void) {
  263. Suite *s = suite_create("Client");
  264. TCase *tc_client = tcase_create("Client Basic");
  265. tcase_add_checked_fixture(tc_client, setup, teardown);
  266. tcase_add_test(tc_client, Client_connect);
  267. tcase_add_test(tc_client, Client_connect_username);
  268. tcase_add_test(tc_client, Client_delete_without_connect);
  269. tcase_add_test(tc_client, Client_endpoints);
  270. tcase_add_test(tc_client, Client_endpoints_empty);
  271. tcase_add_test(tc_client, Client_read);
  272. suite_add_tcase(s,tc_client);
  273. TCase *tc_client_reconnect = tcase_create("Client Reconnect");
  274. tcase_add_checked_fixture(tc_client_reconnect, setup, teardown);
  275. tcase_add_test(tc_client_reconnect, Client_renewSecureChannel);
  276. tcase_add_test(tc_client_reconnect, Client_renewSecureChannelWithActiveSubscription);
  277. tcase_add_test(tc_client_reconnect, Client_reconnect);
  278. #ifdef UA_SESSION_RECOVERY
  279. tcase_add_test(tc_client_reconnect, Client_activateSessionClose);
  280. tcase_add_test(tc_client_reconnect, Client_activateSessionTimeout);
  281. #endif /* UA_SESSION_RECOVERY */
  282. suite_add_tcase(s,tc_client_reconnect);
  283. return s;
  284. }
  285. int main(void) {
  286. Suite *s = testSuite_Client();
  287. SRunner *sr = srunner_create(s);
  288. srunner_set_fork_status(sr, CK_NOFORK);
  289. srunner_run_all(sr,CK_NORMAL);
  290. int number_failed = srunner_ntests_failed(sr);
  291. srunner_free(sr);
  292. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  293. }