check_client_securechannel.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "ua_types.h"
  5. #include "ua_server.h"
  6. #include "ua_client.h"
  7. #include "client/ua_client_internal.h"
  8. #include "ua_config_default.h"
  9. #include "ua_client_highlevel.h"
  10. #include "ua_network_tcp.h"
  11. #include "testing_clock.h"
  12. #include "testing_networklayers.h"
  13. #include "check.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(UA_ClientConfig_default);
  41. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  42. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  43. UA_fakeSleep(UA_ClientConfig_default.secureChannelLifeTime);
  44. UA_Variant val;
  45. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  46. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  47. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  48. UA_Variant_deleteMembers(&val);
  49. UA_Client_disconnect(client);
  50. UA_Client_delete(client);
  51. }
  52. END_TEST
  53. /* Send the next message after the securechannel timed out */
  54. START_TEST(SecureChannel_timeout_fail) {
  55. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  56. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  57. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  58. UA_fakeSleep(UA_ClientConfig_default.secureChannelLifeTime+1);
  59. UA_realSleep(50 + 1); // UA_MAXTIMEOUT+1 wait to be sure UA_Server_run_iterate can be completely executed
  60. UA_Variant val;
  61. UA_Variant_init(&val);
  62. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  63. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  64. ck_assert(retval != UA_STATUSCODE_GOOD);
  65. UA_Variant_deleteMembers(&val);
  66. UA_Client_disconnect(client);
  67. UA_Client_delete(client);
  68. }
  69. END_TEST
  70. /* Send an async message and receive the response when the securechannel timed out */
  71. START_TEST(SecureChannel_networkfail) {
  72. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  73. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  74. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  75. UA_ReadRequest rq;
  76. UA_ReadRequest_init(&rq);
  77. UA_ReadValueId rvi;
  78. UA_ReadValueId_init(&rvi);
  79. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  80. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  81. rq.nodesToRead = &rvi;
  82. rq.nodesToReadSize = 1;
  83. /* Forward the clock after recv in the client */
  84. UA_Client_recv = client->connection.recv;
  85. client->connection.recv = UA_Client_recvTesting;
  86. UA_Client_recvSleepDuration = UA_ClientConfig_default.secureChannelLifeTime+1;
  87. UA_Variant val;
  88. UA_Variant_init(&val);
  89. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  90. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  91. ck_assert_msg(retval == UA_STATUSCODE_BADSECURECHANNELCLOSED);
  92. UA_Client_disconnect(client);
  93. UA_Client_delete(client);
  94. }
  95. END_TEST
  96. START_TEST(SecureChannel_reconnect) {
  97. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  98. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  99. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  100. client->state = UA_CLIENTSTATE_CONNECTED;
  101. retval = UA_Client_disconnect(client);
  102. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  103. UA_fakeSleep(UA_ClientConfig_default.secureChannelLifeTime+1);
  104. UA_realSleep(50 + 1);
  105. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  106. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  107. UA_Client_delete(client);
  108. }
  109. END_TEST
  110. START_TEST(SecureChannel_cableunplugged) {
  111. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  112. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  113. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  114. UA_Variant val;
  115. UA_Variant_init(&val);
  116. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  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_recv = client->connection.recv;
  121. client->connection.recv = UA_Client_recvTesting;
  122. /* Simulate network cable unplugged (no response from server) */
  123. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  124. UA_Variant_init(&val);
  125. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  126. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  127. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  128. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  129. UA_Client_delete(client);
  130. }
  131. END_TEST
  132. int main(void) {
  133. TCase *tc_sc = tcase_create("Client SecureChannel");
  134. tcase_add_checked_fixture(tc_sc, setup, teardown);
  135. tcase_add_test(tc_sc, SecureChannel_timeout_max);
  136. tcase_add_test(tc_sc, SecureChannel_timeout_fail);
  137. tcase_add_test(tc_sc, SecureChannel_networkfail);
  138. tcase_add_test(tc_sc, SecureChannel_reconnect);
  139. tcase_add_test(tc_sc, SecureChannel_cableunplugged);
  140. Suite *s = suite_create("Client");
  141. suite_add_tcase(s, tc_sc);
  142. SRunner *sr = srunner_create(s);
  143. srunner_set_fork_status(sr, CK_NOFORK);
  144. srunner_run_all(sr,CK_NORMAL);
  145. int number_failed = srunner_ntests_failed(sr);
  146. srunner_free(sr);
  147. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  148. }