check_client_securechannel.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 = UA_Boolean_new();
  26. *running = true;
  27. config = UA_ServerConfig_new_default();
  28. server = UA_Server_new(config);
  29. UA_Server_run_startup(server);
  30. THREAD_CREATE(server_thread, serverloop);
  31. UA_realSleep(100);
  32. }
  33. static void teardown(void) {
  34. *running = false;
  35. THREAD_JOIN(server_thread);
  36. UA_Server_run_shutdown(server);
  37. UA_Boolean_delete(running);
  38. UA_Server_delete(server);
  39. UA_ServerConfig_delete(config);
  40. }
  41. START_TEST(SecureChannel_timeout_max) {
  42. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  43. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  44. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  45. UA_fakeSleep(UA_ClientConfig_default.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(UA_ClientConfig_default);
  58. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  59. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  60. UA_fakeSleep(UA_ClientConfig_default.secureChannelLifeTime+1);
  61. UA_realSleep(50 + 1); // UA_MAXTIMEOUT+1 wait to be sure UA_Server_run_iterate can be completely executed
  62. UA_Variant val;
  63. UA_Variant_init(&val);
  64. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  65. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  66. ck_assert(retval != UA_STATUSCODE_GOOD);
  67. UA_Variant_deleteMembers(&val);
  68. UA_Client_disconnect(client);
  69. UA_Client_delete(client);
  70. }
  71. END_TEST
  72. /* Send an async message and receive the response when the securechannel timed out */
  73. START_TEST(SecureChannel_networkfail) {
  74. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  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_Client_recv = client->connection.recv;
  87. client->connection.recv = UA_Client_recvTesting;
  88. UA_Client_recvSleepDuration = UA_ClientConfig_default.secureChannelLifeTime+1;
  89. UA_Variant val;
  90. UA_Variant_init(&val);
  91. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  92. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  93. ck_assert_msg(retval == UA_STATUSCODE_BADSECURECHANNELCLOSED);
  94. UA_Client_disconnect(client);
  95. UA_Client_delete(client);
  96. }
  97. END_TEST
  98. START_TEST(SecureChannel_reconnect) {
  99. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  100. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  101. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  102. client->state = UA_CLIENTSTATE_CONNECTED;
  103. retval = UA_Client_disconnect(client);
  104. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  105. UA_fakeSleep(UA_ClientConfig_default.secureChannelLifeTime+1);
  106. UA_realSleep(50 + 1);
  107. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  108. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  109. UA_Client_delete(client);
  110. }
  111. END_TEST
  112. START_TEST(SecureChannel_cableunplugged) {
  113. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  114. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  115. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  116. UA_Variant val;
  117. UA_Variant_init(&val);
  118. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  119. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  120. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  121. UA_Variant_deleteMembers(&val);
  122. UA_Client_recv = client->connection.recv;
  123. client->connection.recv = UA_Client_recvTesting;
  124. /* Simulate network cable unplugged (no response from server) */
  125. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  126. UA_Variant_init(&val);
  127. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  128. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  129. ck_assert_msg(UA_Client_getState(client) == UA_CLIENTSTATE_DISCONNECTED);
  130. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  131. UA_Client_delete(client);
  132. }
  133. END_TEST
  134. int main(void) {
  135. TCase *tc_sc = tcase_create("Client SecureChannel");
  136. tcase_add_checked_fixture(tc_sc, setup, teardown);
  137. tcase_add_test(tc_sc, SecureChannel_timeout_max);
  138. tcase_add_test(tc_sc, SecureChannel_timeout_fail);
  139. tcase_add_test(tc_sc, SecureChannel_networkfail);
  140. tcase_add_test(tc_sc, SecureChannel_reconnect);
  141. tcase_add_test(tc_sc, SecureChannel_cableunplugged);
  142. Suite *s = suite_create("Client");
  143. suite_add_tcase(s, tc_sc);
  144. SRunner *sr = srunner_create(s);
  145. srunner_set_fork_status(sr, CK_NOFORK);
  146. srunner_run_all(sr,CK_NORMAL);
  147. int number_failed = srunner_ntests_failed(sr);
  148. srunner_free(sr);
  149. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  150. }