check_client.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. UA_Client *client = UA_Client_new(clientConfig);
  126. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  127. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  128. UA_Variant val;
  129. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  130. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  131. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  132. UA_Variant_deleteMembers(&val);
  133. // restart server to test reconnect
  134. teardown();
  135. setup();
  136. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  137. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  138. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  139. ck_assert_msg(retval == UA_STATUSCODE_GOOD, UA_StatusCode_name(retval));
  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. }
  146. END_TEST
  147. START_TEST(Client_activateSessionClose) {
  148. // restart server
  149. teardown();
  150. setup();
  151. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  152. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  153. UA_Client *client = UA_Client_new(clientConfig);
  154. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  155. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  156. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  157. UA_Variant val;
  158. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  159. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  160. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  161. UA_Variant_deleteMembers(&val);
  162. UA_Client_close(client);
  163. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  164. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  165. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  166. nodeId = UA_NODEID_STRING(1, "my.variable");
  167. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  168. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  169. UA_Variant_deleteMembers(&val);
  170. UA_Client_disconnect(client);
  171. UA_Client_delete(client);
  172. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  173. }
  174. END_TEST
  175. START_TEST(Client_delete_without_connect) {
  176. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  177. UA_Client *client = UA_Client_new(clientConfig);
  178. ck_assert_msg(client != NULL);
  179. UA_Client_delete(client);
  180. }
  181. END_TEST
  182. START_TEST(Client_activateSessionTimeout) {
  183. // restart server
  184. teardown();
  185. setup();
  186. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  187. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  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_Variant_init(&val);
  193. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  194. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  195. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  196. UA_Variant_deleteMembers(&val);
  197. UA_Client_recv = client->connection.recv;
  198. client->connection.recv = UA_Client_recvTesting;
  199. /* Simulate network cable unplugged (no response from server) */
  200. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  201. UA_Variant_init(&val);
  202. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  203. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  204. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  205. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  206. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  207. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  208. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 1);
  209. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  210. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  211. UA_Variant_deleteMembers(&val);
  212. UA_Client_delete(client);
  213. ck_assert_uint_eq(server->sessionManager.currentSessionCount, 0);
  214. }
  215. END_TEST
  216. static Suite* testSuite_Client(void) {
  217. Suite *s = suite_create("Client");
  218. TCase *tc_client = tcase_create("Client Basic");
  219. tcase_add_checked_fixture(tc_client, setup, teardown);
  220. tcase_add_test(tc_client, Client_connect);
  221. tcase_add_test(tc_client, Client_connect_username);
  222. tcase_add_test(tc_client, Client_endpoints);
  223. tcase_add_test(tc_client, Client_read);
  224. tcase_add_test(tc_client, Client_delete_without_connect);
  225. suite_add_tcase(s,tc_client);
  226. TCase *tc_client_reconnect = tcase_create("Client Reconnect");
  227. tcase_add_checked_fixture(tc_client_reconnect, setup, teardown);
  228. tcase_add_test(tc_client_reconnect, Client_renewSecureChannel);
  229. tcase_add_test(tc_client_reconnect, Client_reconnect);
  230. tcase_add_test(tc_client_reconnect, Client_activateSessionClose);
  231. tcase_add_test(tc_client_reconnect, Client_activateSessionTimeout);
  232. suite_add_tcase(s,tc_client_reconnect);
  233. return s;
  234. }
  235. int main(void) {
  236. Suite *s = testSuite_Client();
  237. SRunner *sr = srunner_create(s);
  238. srunner_set_fork_status(sr, CK_NOFORK);
  239. srunner_run_all(sr,CK_NORMAL);
  240. int number_failed = srunner_ntests_failed(sr);
  241. srunner_free(sr);
  242. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  243. }