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 <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 = UA_Boolean_new();
  52. *running = true;
  53. config = UA_ServerConfig_new_default();
  54. server = UA_Server_new(config);
  55. UA_Server_run_startup(server);
  56. addVariable(16366);
  57. THREAD_CREATE(server_thread, serverloop);
  58. }
  59. static void teardown(void) {
  60. *running = false;
  61. THREAD_JOIN(server_thread);
  62. UA_Server_run_shutdown(server);
  63. UA_Boolean_delete(running);
  64. UA_Server_delete(server);
  65. UA_ServerConfig_delete(config);
  66. }
  67. START_TEST(Client_connect) {
  68. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  69. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  70. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  71. UA_Client_disconnect(client);
  72. UA_Client_delete(client);
  73. }
  74. END_TEST
  75. START_TEST(Client_connect_username) {
  76. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  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(UA_ClientConfig_default);
  85. UA_EndpointDescription* endpointArray = NULL;
  86. size_t endpointArraySize = 0;
  87. UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:4840",
  88. &endpointArraySize, &endpointArray);
  89. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  90. ck_assert_msg(endpointArraySize > 0);
  91. UA_Array_delete(endpointArray,endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  92. UA_Client_delete(client);
  93. }
  94. END_TEST
  95. START_TEST(Client_endpoints_empty) {
  96. /* Issue a getEndpoints call with empty endpointUrl.
  97. * Using UA_Client_getEndpoints automatically passes the client->endpointUrl as requested endpointUrl.
  98. * The spec says:
  99. * The Server should return a suitable default URL if it does not recognize the HostName in the URL.
  100. *
  101. * See https://github.com/open62541/open62541/issues/775
  102. */
  103. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  104. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  105. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  106. UA_GetEndpointsRequest request;
  107. UA_GetEndpointsRequest_init(&request);
  108. request.requestHeader.timestamp = UA_DateTime_now();
  109. request.requestHeader.timeoutHint = 10000;
  110. UA_GetEndpointsResponse response;
  111. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_GETENDPOINTSREQUEST],
  112. &response, &UA_TYPES[UA_TYPES_GETENDPOINTSRESPONSE]);
  113. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  114. ck_assert_msg(response.endpointsSize > 0);
  115. UA_GetEndpointsResponse_deleteMembers(&response);
  116. UA_GetEndpointsRequest_deleteMembers(&request);
  117. UA_Client_delete(client);
  118. }
  119. END_TEST
  120. START_TEST(Client_read) {
  121. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  122. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  123. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  124. UA_Variant val;
  125. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  126. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  127. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  128. UA_Variant_deleteMembers(&val);
  129. UA_Client_disconnect(client);
  130. UA_Client_delete(client);
  131. }
  132. END_TEST
  133. START_TEST(Client_renewSecureChannel) {
  134. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  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_fakeSleep((UA_UInt32)((UA_Double)UA_ClientConfig_default.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_ClientConfig uaClientConfig = UA_ClientConfig_default;
  150. uaClientConfig.secureChannelLifeTime = 10000;
  151. UA_Client *client = UA_Client_new(uaClientConfig);
  152. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  153. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  154. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  155. request.requestedLifetimeCount = 1000;
  156. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  157. NULL, NULL, NULL);
  158. (void)response;
  159. for(int i = 0; i < 15; ++i) {
  160. UA_sleep_ms(1000);
  161. retval = UA_Client_run_iterate(client, 0);
  162. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  163. }
  164. UA_Client_disconnect(client);
  165. UA_Client_delete(client);
  166. } END_TEST
  167. START_TEST(Client_reconnect) {
  168. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  169. UA_Client *client = UA_Client_new(clientConfig);
  170. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  171. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  172. UA_Variant val;
  173. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  174. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  175. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  176. UA_Variant_deleteMembers(&val);
  177. // restart server to test reconnect
  178. teardown();
  179. setup();
  180. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  181. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  182. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  183. ck_assert_msg(retval == UA_STATUSCODE_GOOD, UA_StatusCode_name(retval));
  184. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  185. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  186. UA_Variant_deleteMembers(&val);
  187. UA_Client_disconnect(client);
  188. UA_Client_delete(client);
  189. }
  190. END_TEST
  191. START_TEST(Client_delete_without_connect) {
  192. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  193. UA_Client *client = UA_Client_new(clientConfig);
  194. ck_assert_msg(client != NULL);
  195. UA_Client_delete(client);
  196. }
  197. END_TEST
  198. // TODO ACTIVATE THE TESTS WHEN SESSION RECOVERY IS GOOD
  199. #ifdef UA_SESSION_RECOVERY
  200. START_TEST(Client_activateSessionClose) {
  201. // restart server
  202. teardown();
  203. setup();
  204. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  205. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  206. UA_Client *client = UA_Client_new(clientConfig);
  207. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  208. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  209. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  210. UA_Variant val;
  211. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  212. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  213. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  214. UA_Variant_deleteMembers(&val);
  215. UA_Client_disconnect(client);
  216. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  217. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  218. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  219. nodeId = UA_NODEID_STRING(1, "my.variable");
  220. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  221. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  222. UA_Variant_deleteMembers(&val);
  223. UA_Client_disconnect(client);
  224. UA_Client_delete(client);
  225. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  226. }
  227. END_TEST
  228. START_TEST(Client_activateSessionTimeout) {
  229. // restart server
  230. teardown();
  231. setup();
  232. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  233. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  234. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  235. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  236. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  237. UA_Variant val;
  238. UA_Variant_init(&val);
  239. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  240. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  241. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  242. UA_Variant_deleteMembers(&val);
  243. UA_Client_recv = client->connection.recv;
  244. client->connection.recv = UA_Client_recvTesting;
  245. /* Simulate network cable unplugged (no response from server) */
  246. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  247. UA_Variant_init(&val);
  248. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  249. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  250. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  251. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  252. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  253. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  254. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  255. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  256. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  257. UA_Variant_deleteMembers(&val);
  258. UA_Client_delete(client);
  259. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  260. }
  261. END_TEST
  262. #endif /* UA_SESSION_RECOVERY */
  263. static Suite* testSuite_Client(void) {
  264. Suite *s = suite_create("Client");
  265. TCase *tc_client = tcase_create("Client Basic");
  266. tcase_add_checked_fixture(tc_client, setup, teardown);
  267. tcase_add_test(tc_client, Client_connect);
  268. tcase_add_test(tc_client, Client_connect_username);
  269. tcase_add_test(tc_client, Client_delete_without_connect);
  270. tcase_add_test(tc_client, Client_endpoints);
  271. tcase_add_test(tc_client, Client_endpoints_empty);
  272. tcase_add_test(tc_client, Client_read);
  273. suite_add_tcase(s,tc_client);
  274. TCase *tc_client_reconnect = tcase_create("Client Reconnect");
  275. tcase_add_checked_fixture(tc_client_reconnect, setup, teardown);
  276. tcase_add_test(tc_client_reconnect, Client_renewSecureChannel);
  277. tcase_add_test(tc_client_reconnect, Client_renewSecureChannelWithActiveSubscription);
  278. tcase_add_test(tc_client_reconnect, Client_reconnect);
  279. #ifdef UA_SESSION_RECOVERY
  280. tcase_add_test(tc_client_reconnect, Client_activateSessionClose);
  281. tcase_add_test(tc_client_reconnect, Client_activateSessionTimeout);
  282. #endif /* UA_SESSION_RECOVERY */
  283. suite_add_tcase(s,tc_client_reconnect);
  284. return s;
  285. }
  286. int main(void) {
  287. Suite *s = testSuite_Client();
  288. SRunner *sr = srunner_create(s);
  289. srunner_set_fork_status(sr, CK_NOFORK);
  290. srunner_run_all(sr,CK_NORMAL);
  291. int number_failed = srunner_ntests_failed(sr);
  292. srunner_free(sr);
  293. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  294. }