check_client.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_activateSessionClose) {
  173. // restart server
  174. teardown();
  175. setup();
  176. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  177. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  178. UA_Client *client = UA_Client_new(clientConfig);
  179. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  180. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  181. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  182. UA_Variant val;
  183. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  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_close(client);
  188. 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. nodeId = UA_NODEID_STRING(1, "my.variable");
  192. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  193. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  194. UA_Variant_deleteMembers(&val);
  195. UA_Client_disconnect(client);
  196. UA_Client_delete(client);
  197. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  198. }
  199. END_TEST
  200. START_TEST(Client_delete_without_connect) {
  201. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  202. UA_Client *client = UA_Client_new(clientConfig);
  203. ck_assert_msg(client != NULL);
  204. UA_Client_delete(client);
  205. }
  206. END_TEST
  207. START_TEST(Client_activateSessionTimeout) {
  208. // restart server
  209. teardown();
  210. setup();
  211. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  212. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  213. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  214. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  215. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  216. UA_Variant val;
  217. UA_Variant_init(&val);
  218. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  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_recv = client->connection.recv;
  223. client->connection.recv = UA_Client_recvTesting;
  224. /* Simulate network cable unplugged (no response from server) */
  225. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  226. UA_Variant_init(&val);
  227. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  228. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  229. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  230. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  231. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  232. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  233. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  234. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  235. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  236. UA_Variant_deleteMembers(&val);
  237. UA_Client_delete(client);
  238. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  239. }
  240. END_TEST
  241. static Suite* testSuite_Client(void) {
  242. Suite *s = suite_create("Client");
  243. TCase *tc_client = tcase_create("Client Basic");
  244. tcase_add_checked_fixture(tc_client, setup, teardown);
  245. tcase_add_test(tc_client, Client_connect);
  246. tcase_add_test(tc_client, Client_connect_username);
  247. tcase_add_test(tc_client, Client_endpoints);
  248. tcase_add_test(tc_client, Client_endpoints_empty);
  249. tcase_add_test(tc_client, Client_read);
  250. tcase_add_test(tc_client, Client_delete_without_connect);
  251. suite_add_tcase(s,tc_client);
  252. TCase *tc_client_reconnect = tcase_create("Client Reconnect");
  253. tcase_add_checked_fixture(tc_client_reconnect, setup, teardown);
  254. tcase_add_test(tc_client_reconnect, Client_renewSecureChannel);
  255. tcase_add_test(tc_client_reconnect, Client_reconnect);
  256. tcase_add_test(tc_client_reconnect, Client_activateSessionClose);
  257. tcase_add_test(tc_client_reconnect, Client_activateSessionTimeout);
  258. suite_add_tcase(s,tc_client_reconnect);
  259. return s;
  260. }
  261. int main(void) {
  262. Suite *s = testSuite_Client();
  263. SRunner *sr = srunner_create(s);
  264. srunner_set_fork_status(sr, CK_NOFORK);
  265. srunner_run_all(sr,CK_NORMAL);
  266. int number_failed = srunner_ntests_failed(sr);
  267. srunner_free(sr);
  268. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  269. }