check_client_securechannel.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "check.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 = UA_Boolean_new();
  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_Boolean_delete(running);
  37. UA_Server_delete(server);
  38. UA_ServerConfig_delete(config);
  39. }
  40. START_TEST(SecureChannel_timeout_max) {
  41. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  42. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  43. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  44. UA_fakeSleep(UA_ClientConfig_default.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(UA_ClientConfig_default);
  57. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  58. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  59. UA_fakeSleep(UA_ClientConfig_default.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. static void
  72. read_cb(UA_Client *client, void *userdata,
  73. UA_UInt32 requestId, const void *response) {
  74. }
  75. /* Send an async message and receive the response when the securechannel timed out */
  76. START_TEST(SecureChannel_networkfail) {
  77. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  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. UA_UInt32 rqId;
  89. __UA_Client_AsyncService(client, &rq, &UA_TYPES[UA_TYPES_READREQUEST],
  90. read_cb, &UA_TYPES[UA_TYPES_READRESPONSE], NULL, &rqId);
  91. UA_fakeSleep(UA_ClientConfig_default.secureChannelLifeTime+1);
  92. UA_realSleep(100); // UA_MAXTIMEOUT+1 wait to be sure UA_Server_run_iterate can be completely executed
  93. retval = UA_Client_runAsync(client, 50);
  94. ck_assert_msg(retval != UA_STATUSCODE_GOOD, UA_StatusCode_name(retval));
  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(UA_ClientConfig_default);
  101. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  102. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  103. client->state = UA_CLIENTSTATE_CONNECTED;
  104. retval = UA_Client_disconnect(client);
  105. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  106. UA_fakeSleep(UA_ClientConfig_default.secureChannelLifeTime+1);
  107. UA_realSleep(50 + 1);
  108. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  109. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  110. UA_Client_delete(client);
  111. }
  112. END_TEST
  113. int main(void) {
  114. TCase *tc_sc = tcase_create("Client SecureChannel");
  115. tcase_add_checked_fixture(tc_sc, setup, teardown);
  116. tcase_add_test(tc_sc, SecureChannel_timeout_max);
  117. tcase_add_test(tc_sc, SecureChannel_timeout_fail);
  118. tcase_add_test(tc_sc, SecureChannel_networkfail);
  119. tcase_add_test(tc_sc, SecureChannel_reconnect);
  120. Suite *s = suite_create("Client");
  121. suite_add_tcase(s, tc_sc);
  122. SRunner *sr = srunner_create(s);
  123. srunner_set_fork_status(sr, CK_NOFORK);
  124. srunner_run_all(sr,CK_NORMAL);
  125. int number_failed = srunner_ntests_failed(sr);
  126. srunner_free(sr);
  127. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  128. }