check_client_securechannel.c 6.5 KB

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