check_client_securechannel.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <pthread.h>
  5. #include "ua_types.h"
  6. #include "ua_server.h"
  7. #include "ua_client.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. UA_Server *server;
  14. UA_ServerConfig *config;
  15. UA_Boolean *running;
  16. pthread_t server_thread;
  17. static void * serverloop(void *_) {
  18. while(*running)
  19. UA_Server_run_iterate(server, true);
  20. return NULL;
  21. }
  22. static void setup(void) {
  23. running = UA_Boolean_new();
  24. *running = true;
  25. config = UA_ServerConfig_new_default();
  26. server = UA_Server_new(config);
  27. UA_Server_run_startup(server);
  28. pthread_create(&server_thread, NULL, serverloop, NULL);
  29. UA_realSleep(100);
  30. }
  31. static void teardown(void) {
  32. *running = false;
  33. pthread_join(server_thread, NULL);
  34. UA_Server_run_shutdown(server);
  35. UA_Boolean_delete(running);
  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. START_TEST(SecureChannel_timeout_fail) {
  54. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  55. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  56. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  57. UA_fakeSleep(UA_ClientConfig_default.secureChannelLifeTime+1);
  58. UA_Variant val;
  59. UA_Variant_init(&val);
  60. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  61. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  62. ck_assert(retval != UA_STATUSCODE_GOOD);
  63. UA_Variant_deleteMembers(&val);
  64. UA_Client_disconnect(client);
  65. UA_Client_delete(client);
  66. }
  67. END_TEST
  68. int main(void) {
  69. TCase *tc_sc = tcase_create("Client SecureChannel");
  70. tcase_add_checked_fixture(tc_sc, setup, teardown);
  71. tcase_add_test(tc_sc, SecureChannel_timeout_max);
  72. tcase_add_test(tc_sc, SecureChannel_timeout_fail);
  73. Suite *s = suite_create("Client");
  74. suite_add_tcase(s, tc_sc);
  75. SRunner *sr = srunner_create(s);
  76. srunner_set_fork_status(sr, CK_NOFORK);
  77. srunner_run_all(sr,CK_NORMAL);
  78. int number_failed = srunner_ntests_failed(sr);
  79. srunner_free(sr);
  80. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  81. }