check_client_securechannel.c 6.5 KB

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