check_client.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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_reconnect) {
  149. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  150. UA_Client *client = UA_Client_new(clientConfig);
  151. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  152. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  153. UA_Variant val;
  154. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  155. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  156. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  157. UA_Variant_deleteMembers(&val);
  158. // restart server to test reconnect
  159. teardown();
  160. setup();
  161. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  162. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  163. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  164. ck_assert_msg(retval == UA_STATUSCODE_GOOD, UA_StatusCode_name(retval));
  165. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  166. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  167. UA_Variant_deleteMembers(&val);
  168. UA_Client_disconnect(client);
  169. UA_Client_delete(client);
  170. }
  171. END_TEST
  172. START_TEST(Client_delete_without_connect) {
  173. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  174. UA_Client *client = UA_Client_new(clientConfig);
  175. ck_assert_msg(client != NULL);
  176. UA_Client_delete(client);
  177. }
  178. END_TEST
  179. // TODO ACTIVATE THE TESTS WHEN SESSION RECOVERY IS GOOD
  180. #ifdef UA_SESSION_RECOVERY
  181. START_TEST(Client_activateSessionClose) {
  182. // restart server
  183. teardown();
  184. setup();
  185. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  186. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  187. UA_Client *client = UA_Client_new(clientConfig);
  188. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  189. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  190. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  191. UA_Variant val;
  192. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  193. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  194. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  195. UA_Variant_deleteMembers(&val);
  196. UA_Client_close(client);
  197. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  198. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  199. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  200. nodeId = UA_NODEID_STRING(1, "my.variable");
  201. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  202. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  203. UA_Variant_deleteMembers(&val);
  204. UA_Client_disconnect(client);
  205. UA_Client_delete(client);
  206. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  207. }
  208. END_TEST
  209. START_TEST(Client_activateSessionTimeout) {
  210. // restart server
  211. teardown();
  212. setup();
  213. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  214. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  215. UA_StatusCode 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. UA_Variant val;
  219. UA_Variant_init(&val);
  220. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  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_recv = client->connection.recv;
  225. client->connection.recv = UA_Client_recvTesting;
  226. /* Simulate network cable unplugged (no response from server) */
  227. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  228. UA_Variant_init(&val);
  229. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  230. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  231. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  232. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  233. 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. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  237. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  238. UA_Variant_deleteMembers(&val);
  239. UA_Client_delete(client);
  240. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  241. }
  242. END_TEST
  243. #endif /* UA_SESSION_RECOVERY */
  244. static Suite* testSuite_Client(void) {
  245. Suite *s = suite_create("Client");
  246. TCase *tc_client = tcase_create("Client Basic");
  247. tcase_add_checked_fixture(tc_client, setup, teardown);
  248. tcase_add_test(tc_client, Client_connect);
  249. tcase_add_test(tc_client, Client_connect_username);
  250. tcase_add_test(tc_client, Client_delete_without_connect);
  251. tcase_add_test(tc_client, Client_endpoints);
  252. tcase_add_test(tc_client, Client_endpoints_empty);
  253. tcase_add_test(tc_client, Client_read);
  254. suite_add_tcase(s,tc_client);
  255. TCase *tc_client_reconnect = tcase_create("Client Reconnect");
  256. tcase_add_checked_fixture(tc_client_reconnect, setup, teardown);
  257. tcase_add_test(tc_client_reconnect, Client_renewSecureChannel);
  258. tcase_add_test(tc_client_reconnect, Client_reconnect);
  259. #ifdef UA_SESSION_RECOVERY
  260. tcase_add_test(tc_client_reconnect, Client_activateSessionClose);
  261. tcase_add_test(tc_client_reconnect, Client_activateSessionTimeout);
  262. #endif /* UA_SESSION_RECOVERY */
  263. suite_add_tcase(s,tc_client_reconnect);
  264. return s;
  265. }
  266. int main(void) {
  267. Suite *s = testSuite_Client();
  268. SRunner *sr = srunner_create(s);
  269. srunner_set_fork_status(sr, CK_NOFORK);
  270. srunner_run_all(sr,CK_NORMAL);
  271. int number_failed = srunner_ntests_failed(sr);
  272. srunner_free(sr);
  273. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  274. }