check_client.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_read) {
  96. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  97. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  98. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  99. UA_Variant val;
  100. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  101. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  102. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  103. UA_Variant_deleteMembers(&val);
  104. UA_Client_disconnect(client);
  105. UA_Client_delete(client);
  106. }
  107. END_TEST
  108. START_TEST(Client_renewSecureChannel) {
  109. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  110. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  111. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  112. /* Forward the time */
  113. UA_fakeSleep((UA_UInt32)((UA_Double)UA_ClientConfig_default.secureChannelLifeTime * 0.8));
  114. /* Now read */
  115. UA_Variant val;
  116. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  117. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  118. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  119. UA_Variant_deleteMembers(&val);
  120. UA_Client_disconnect(client);
  121. UA_Client_delete(client);
  122. } END_TEST
  123. START_TEST(Client_reconnect) {
  124. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  125. clientConfig.timeout = 100;
  126. UA_Client *client = UA_Client_new(clientConfig);
  127. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  128. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  129. UA_Variant val;
  130. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  131. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  132. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  133. UA_Variant_deleteMembers(&val);
  134. // restart server to test reconnect
  135. teardown();
  136. setup();
  137. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  138. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  139. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  140. ck_assert_msg(retval == UA_STATUSCODE_GOOD, UA_StatusCode_name(retval));
  141. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  142. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  143. UA_Variant_deleteMembers(&val);
  144. UA_Client_disconnect(client);
  145. UA_Client_delete(client);
  146. }
  147. END_TEST
  148. START_TEST(Client_activateSessionClose) {
  149. // restart server
  150. teardown();
  151. setup();
  152. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  153. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  154. UA_Client *client = UA_Client_new(clientConfig);
  155. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  156. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  157. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  158. UA_Variant val;
  159. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  160. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  161. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  162. UA_Variant_deleteMembers(&val);
  163. UA_Client_close(client);
  164. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  165. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  166. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  167. nodeId = UA_NODEID_STRING(1, "my.variable");
  168. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  169. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  170. UA_Variant_deleteMembers(&val);
  171. UA_Client_disconnect(client);
  172. UA_Client_delete(client);
  173. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  174. }
  175. END_TEST
  176. START_TEST(Client_activateSessionTimeout) {
  177. // restart server
  178. teardown();
  179. setup();
  180. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  181. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  182. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  183. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  184. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  185. UA_Variant val;
  186. UA_Variant_init(&val);
  187. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  188. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  189. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  190. UA_Variant_deleteMembers(&val);
  191. UA_Client_recv = client->connection.recv;
  192. client->connection.recv = UA_Client_recvTesting;
  193. /* Simulate network cable unplugged (no response from server) */
  194. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  195. UA_Variant_init(&val);
  196. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  197. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  198. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  199. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  200. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  201. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  202. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  203. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  204. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  205. UA_Variant_deleteMembers(&val);
  206. UA_Client_delete(client);
  207. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  208. }
  209. END_TEST
  210. static Suite* testSuite_Client(void) {
  211. Suite *s = suite_create("Client");
  212. TCase *tc_client = tcase_create("Client Basic");
  213. tcase_add_checked_fixture(tc_client, setup, teardown);
  214. tcase_add_test(tc_client, Client_connect);
  215. tcase_add_test(tc_client, Client_connect_username);
  216. tcase_add_test(tc_client, Client_endpoints);
  217. tcase_add_test(tc_client, Client_read);
  218. suite_add_tcase(s,tc_client);
  219. TCase *tc_client_reconnect = tcase_create("Client Reconnect");
  220. tcase_add_checked_fixture(tc_client_reconnect, setup, teardown);
  221. tcase_add_test(tc_client_reconnect, Client_renewSecureChannel);
  222. tcase_add_test(tc_client_reconnect, Client_reconnect);
  223. tcase_add_test(tc_client_reconnect, Client_activateSessionClose);
  224. tcase_add_test(tc_client_reconnect, Client_activateSessionTimeout);
  225. suite_add_tcase(s,tc_client_reconnect);
  226. return s;
  227. }
  228. int main(void) {
  229. Suite *s = testSuite_Client();
  230. SRunner *sr = srunner_create(s);
  231. srunner_set_fork_status(sr, CK_NOFORK);
  232. srunner_run_all(sr,CK_NORMAL);
  233. int number_failed = srunner_ntests_failed(sr);
  234. srunner_free(sr);
  235. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  236. }