check_client.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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(UA_ClientConfig_default);
  67. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  68. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  69. UA_Client_disconnect(client);
  70. UA_Client_delete(client);
  71. }
  72. END_TEST
  73. START_TEST(Client_connect_username) {
  74. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  75. UA_StatusCode retval = UA_Client_connect_username(client, "opc.tcp://localhost:4840", "user1", "password");
  76. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  77. UA_Client_disconnect(client);
  78. UA_Client_delete(client);
  79. }
  80. END_TEST
  81. START_TEST(Client_endpoints) {
  82. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  83. UA_EndpointDescription* endpointArray = NULL;
  84. size_t endpointArraySize = 0;
  85. UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:4840",
  86. &endpointArraySize, &endpointArray);
  87. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  88. ck_assert_msg(endpointArraySize > 0);
  89. UA_Array_delete(endpointArray,endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  90. UA_Client_delete(client);
  91. }
  92. END_TEST
  93. START_TEST(Client_endpoints_empty) {
  94. /* Issue a getEndpoints call with empty endpointUrl.
  95. * Using UA_Client_getEndpoints automatically passes the client->endpointUrl as requested endpointUrl.
  96. * The spec says:
  97. * The Server should return a suitable default URL if it does not recognize the HostName in the URL.
  98. *
  99. * See https://github.com/open62541/open62541/issues/775
  100. */
  101. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  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(UA_ClientConfig_default);
  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(UA_ClientConfig_default);
  133. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  134. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  135. /* Forward the time */
  136. UA_fakeSleep((UA_UInt32)((UA_Double)UA_ClientConfig_default.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_ClientConfig uaClientConfig = UA_ClientConfig_default;
  148. uaClientConfig.secureChannelLifeTime = 10000;
  149. UA_Client *client = UA_Client_new(uaClientConfig);
  150. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  151. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  152. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  153. request.requestedLifetimeCount = 1000;
  154. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  155. NULL, NULL, NULL);
  156. (void)response;
  157. for(int i = 0; i < 15; ++i) {
  158. UA_sleep_ms(1000);
  159. retval = UA_Client_run_iterate(client, 0);
  160. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  161. }
  162. UA_Client_disconnect(client);
  163. UA_Client_delete(client);
  164. } END_TEST
  165. START_TEST(Client_reconnect) {
  166. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  167. UA_Client *client = UA_Client_new(clientConfig);
  168. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  169. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  170. UA_Variant val;
  171. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  172. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  173. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  174. UA_Variant_deleteMembers(&val);
  175. // restart server to test reconnect
  176. teardown();
  177. setup();
  178. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  179. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  180. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  181. ck_assert_msg(retval == UA_STATUSCODE_GOOD, UA_StatusCode_name(retval));
  182. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  183. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  184. UA_Variant_deleteMembers(&val);
  185. UA_Client_disconnect(client);
  186. UA_Client_delete(client);
  187. }
  188. END_TEST
  189. START_TEST(Client_delete_without_connect) {
  190. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  191. UA_Client *client = UA_Client_new(clientConfig);
  192. ck_assert_msg(client != NULL);
  193. UA_Client_delete(client);
  194. }
  195. END_TEST
  196. // TODO ACTIVATE THE TESTS WHEN SESSION RECOVERY IS GOOD
  197. #ifdef UA_SESSION_RECOVERY
  198. START_TEST(Client_activateSessionClose) {
  199. // restart server
  200. teardown();
  201. setup();
  202. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  203. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  204. UA_Client *client = UA_Client_new(clientConfig);
  205. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  206. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  207. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  208. UA_Variant val;
  209. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  210. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  211. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  212. UA_Variant_deleteMembers(&val);
  213. UA_Client_disconnect(client);
  214. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  215. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  216. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  217. 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. UA_Client_delete(client);
  223. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  224. }
  225. END_TEST
  226. START_TEST(Client_activateSessionTimeout) {
  227. // restart server
  228. teardown();
  229. setup();
  230. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  231. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  232. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  233. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  234. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  235. UA_Variant val;
  236. UA_Variant_init(&val);
  237. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  238. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  239. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  240. UA_Variant_deleteMembers(&val);
  241. UA_Client_recv = client->connection.recv;
  242. client->connection.recv = UA_Client_recvTesting;
  243. /* Simulate network cable unplugged (no response from server) */
  244. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  245. UA_Variant_init(&val);
  246. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  247. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  248. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  249. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  250. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  251. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  252. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  253. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  254. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  255. UA_Variant_deleteMembers(&val);
  256. UA_Client_delete(client);
  257. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  258. }
  259. END_TEST
  260. #endif /* UA_SESSION_RECOVERY */
  261. static Suite* testSuite_Client(void) {
  262. Suite *s = suite_create("Client");
  263. TCase *tc_client = tcase_create("Client Basic");
  264. tcase_add_checked_fixture(tc_client, setup, teardown);
  265. tcase_add_test(tc_client, Client_connect);
  266. tcase_add_test(tc_client, Client_connect_username);
  267. tcase_add_test(tc_client, Client_delete_without_connect);
  268. tcase_add_test(tc_client, Client_endpoints);
  269. tcase_add_test(tc_client, Client_endpoints_empty);
  270. tcase_add_test(tc_client, Client_read);
  271. suite_add_tcase(s,tc_client);
  272. TCase *tc_client_reconnect = tcase_create("Client Reconnect");
  273. tcase_add_checked_fixture(tc_client_reconnect, setup, teardown);
  274. tcase_add_test(tc_client_reconnect, Client_renewSecureChannel);
  275. tcase_add_test(tc_client_reconnect, Client_renewSecureChannelWithActiveSubscription);
  276. tcase_add_test(tc_client_reconnect, Client_reconnect);
  277. #ifdef UA_SESSION_RECOVERY
  278. tcase_add_test(tc_client_reconnect, Client_activateSessionClose);
  279. tcase_add_test(tc_client_reconnect, Client_activateSessionTimeout);
  280. #endif /* UA_SESSION_RECOVERY */
  281. suite_add_tcase(s,tc_client_reconnect);
  282. return s;
  283. }
  284. int main(void) {
  285. Suite *s = testSuite_Client();
  286. SRunner *sr = srunner_create(s);
  287. srunner_set_fork_status(sr, CK_NOFORK);
  288. srunner_run_all(sr,CK_NORMAL);
  289. int number_failed = srunner_ntests_failed(sr);
  290. srunner_free(sr);
  291. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  292. }