check_client_securechannel.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <open62541/client.h>
  5. #include <open62541/client_config_default.h>
  6. #include <open62541/client_highlevel.h>
  7. #include <open62541/server.h>
  8. #include <open62541/server_config_default.h>
  9. #include "client/ua_client_internal.h"
  10. #include "ua_network_tcp.h"
  11. #include "check.h"
  12. #include "testing_clock.h"
  13. #include "testing_networklayers.h"
  14. #include "thread_wrapper.h"
  15. UA_Server *server;
  16. UA_ServerConfig *config;
  17. UA_Boolean running;
  18. THREAD_HANDLE server_thread;
  19. THREAD_CALLBACK(serverloop) {
  20. while(running)
  21. UA_Server_run_iterate(server, true);
  22. return 0;
  23. }
  24. static void setup(void) {
  25. running = true;
  26. config = UA_ServerConfig_new_default();
  27. server = UA_Server_new(config);
  28. UA_Server_run_startup(server);
  29. THREAD_CREATE(server_thread, serverloop);
  30. UA_realSleep(100);
  31. }
  32. static void teardown(void) {
  33. running = false;
  34. THREAD_JOIN(server_thread);
  35. UA_Server_run_shutdown(server);
  36. UA_Server_delete(server);
  37. UA_ServerConfig_delete(config);
  38. }
  39. START_TEST(SecureChannel_timeout_max) {
  40. UA_Client *client = UA_Client_new();
  41. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  42. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  43. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  44. UA_ClientConfig *cconfig = UA_Client_getConfig(client);
  45. UA_fakeSleep(cconfig->secureChannelLifeTime);
  46. UA_Variant val;
  47. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  48. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  49. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  50. UA_Variant_deleteMembers(&val);
  51. UA_Client_disconnect(client);
  52. UA_Client_delete(client);
  53. }
  54. END_TEST
  55. /* Send the next message after the securechannel timed out */
  56. START_TEST(SecureChannel_timeout_fail) {
  57. UA_Client *client = UA_Client_new();
  58. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  59. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  60. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  61. UA_ClientConfig *cconfig = UA_Client_getConfig(client);
  62. UA_fakeSleep(cconfig->secureChannelLifeTime + 1);
  63. UA_realSleep(50 + 1); // UA_MAXTIMEOUT+1 wait to be sure UA_Server_run_iterate can be completely executed
  64. UA_Variant val;
  65. UA_Variant_init(&val);
  66. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  67. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  68. ck_assert(retval != UA_STATUSCODE_GOOD);
  69. UA_Variant_deleteMembers(&val);
  70. UA_Client_disconnect(client);
  71. UA_Client_delete(client);
  72. }
  73. END_TEST
  74. /* Send an async message and receive the response when the securechannel timed out */
  75. START_TEST(SecureChannel_networkfail) {
  76. UA_Client *client = UA_Client_new();
  77. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  78. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  79. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  80. UA_ReadRequest rq;
  81. UA_ReadRequest_init(&rq);
  82. UA_ReadValueId rvi;
  83. UA_ReadValueId_init(&rvi);
  84. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  85. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  86. rq.nodesToRead = &rvi;
  87. rq.nodesToReadSize = 1;
  88. /* Forward the clock after recv in the client */
  89. UA_ClientConfig *cconfig = UA_Client_getConfig(client);
  90. UA_Client_recv = client->connection.recv;
  91. client->connection.recv = UA_Client_recvTesting;
  92. UA_Client_recvSleepDuration = cconfig->secureChannelLifeTime + 1;
  93. UA_Variant val;
  94. UA_Variant_init(&val);
  95. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  96. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  97. ck_assert_msg(retval == UA_STATUSCODE_BADSECURECHANNELCLOSED);
  98. UA_Client_disconnect(client);
  99. UA_Client_delete(client);
  100. }
  101. END_TEST
  102. START_TEST(SecureChannel_reconnect) {
  103. UA_Client *client = UA_Client_new();
  104. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  105. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  106. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  107. client->state = UA_CLIENTSTATE_CONNECTED;
  108. retval = UA_Client_disconnect(client);
  109. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  110. UA_ClientConfig *cconfig = UA_Client_getConfig(client);
  111. UA_fakeSleep(cconfig->secureChannelLifeTime + 1);
  112. UA_realSleep(50 + 1);
  113. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  114. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  115. UA_Client_delete(client);
  116. }
  117. END_TEST
  118. START_TEST(SecureChannel_cableunplugged) {
  119. UA_Client *client = UA_Client_new();
  120. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  121. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  122. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  123. UA_Variant val;
  124. UA_Variant_init(&val);
  125. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  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_recv = client->connection.recv;
  130. client->connection.recv = UA_Client_recvTesting;
  131. /* Simulate network cable unplugged (no response from server) */
  132. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  133. UA_Variant_init(&val);
  134. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  135. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  136. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  137. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  138. UA_Client_delete(client);
  139. }
  140. END_TEST
  141. int main(void) {
  142. TCase *tc_sc = tcase_create("Client SecureChannel");
  143. tcase_add_checked_fixture(tc_sc, setup, teardown);
  144. tcase_add_test(tc_sc, SecureChannel_timeout_max);
  145. tcase_add_test(tc_sc, SecureChannel_timeout_fail);
  146. tcase_add_test(tc_sc, SecureChannel_networkfail);
  147. tcase_add_test(tc_sc, SecureChannel_reconnect);
  148. tcase_add_test(tc_sc, SecureChannel_cableunplugged);
  149. Suite *s = suite_create("Client");
  150. suite_add_tcase(s, tc_sc);
  151. SRunner *sr = srunner_create(s);
  152. srunner_set_fork_status(sr, CK_NOFORK);
  153. srunner_run_all(sr,CK_NORMAL);
  154. int number_failed = srunner_ntests_failed(sr);
  155. srunner_free(sr);
  156. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  157. }