check_client.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <stdio.h>
  5. #include <stdlib.h>
  6. #include <pthread.h>
  7. #include "ua_types.h"
  8. #include "ua_server.h"
  9. #include "ua_client.h"
  10. #include "ua_config_default.h"
  11. #include "ua_client_highlevel.h"
  12. #include "ua_network_tcp.h"
  13. #include "testing_clock.h"
  14. #include "check.h"
  15. UA_Server *server;
  16. UA_ServerConfig *config;
  17. UA_Boolean *running;
  18. UA_ServerNetworkLayer nl;
  19. pthread_t server_thread;
  20. static void
  21. addVariable(size_t size) {
  22. /* Define the attribute of the myInteger variable node */
  23. UA_VariableAttributes attr = UA_VariableAttributes_default;
  24. UA_Int32* array = (UA_Int32*)UA_malloc(size * sizeof(UA_Int32));
  25. memset(array, 0, size * sizeof(UA_Int32));
  26. UA_Variant_setArray(&attr.value, array, size, &UA_TYPES[UA_TYPES_INT32]);
  27. char name[] = "my.variable";
  28. attr.description = UA_LOCALIZEDTEXT("en-US", name);
  29. attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  30. attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  31. /* Add the variable node to the information model */
  32. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, name);
  33. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, name);
  34. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  35. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  36. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  37. parentReferenceNodeId, myIntegerName,
  38. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  39. attr, NULL, NULL);
  40. UA_free(array);
  41. }
  42. static void * serverloop(void *_) {
  43. while(*running)
  44. UA_Server_run_iterate(server, true);
  45. return NULL;
  46. }
  47. static void setup(void) {
  48. running = UA_Boolean_new();
  49. *running = true;
  50. config = UA_ServerConfig_new_default();
  51. server = UA_Server_new(config);
  52. UA_Server_run_startup(server);
  53. addVariable(16366);
  54. pthread_create(&server_thread, NULL, serverloop, NULL);
  55. }
  56. static void teardown(void) {
  57. *running = false;
  58. pthread_join(server_thread, NULL);
  59. UA_Server_run_shutdown(server);
  60. UA_Boolean_delete(running);
  61. UA_Server_delete(server);
  62. UA_ServerConfig_delete(config);
  63. }
  64. START_TEST(Client_connect) {
  65. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  66. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  67. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  68. UA_Client_disconnect(client);
  69. UA_Client_delete(client);
  70. }
  71. END_TEST
  72. START_TEST(Client_read) {
  73. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  74. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  75. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  76. UA_Variant val;
  77. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  78. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  79. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  80. UA_Variant_deleteMembers(&val);
  81. UA_Client_disconnect(client);
  82. UA_Client_delete(client);
  83. }
  84. END_TEST
  85. START_TEST(Client_renewSecureChannel) {
  86. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  87. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  88. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  89. /* Forward the time */
  90. UA_fakeSleep((UA_UInt32)((UA_Double)UA_ClientConfig_default.secureChannelLifeTime * 0.8));
  91. /* Now read */
  92. UA_Variant val;
  93. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  94. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  95. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  96. UA_Variant_deleteMembers(&val);
  97. UA_Client_disconnect(client);
  98. UA_Client_delete(client);
  99. } END_TEST
  100. START_TEST(Client_reconnect) {
  101. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  102. clientConfig.timeout = 100;
  103. UA_Client *client = UA_Client_new(clientConfig);
  104. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  105. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  106. UA_Variant val;
  107. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  108. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  109. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  110. UA_Variant_deleteMembers(&val);
  111. // restart server to test reconnect
  112. teardown();
  113. setup();
  114. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  115. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  116. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  117. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  118. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  119. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  120. UA_Variant_deleteMembers(&val);
  121. UA_Client_disconnect(client);
  122. UA_Client_delete(client);
  123. }
  124. END_TEST
  125. static Suite* testSuite_Client(void) {
  126. Suite *s = suite_create("Client");
  127. TCase *tc_client = tcase_create("Client Basic");
  128. tcase_add_checked_fixture(tc_client, setup, teardown);
  129. tcase_add_test(tc_client, Client_connect);
  130. tcase_add_test(tc_client, Client_read);
  131. suite_add_tcase(s,tc_client);
  132. TCase *tc_client_reconnect = tcase_create("Client Reconnect");
  133. tcase_add_checked_fixture(tc_client_reconnect, setup, teardown);
  134. tcase_add_test(tc_client_reconnect, Client_renewSecureChannel);
  135. tcase_add_test(tc_client_reconnect, Client_reconnect);
  136. suite_add_tcase(s,tc_client_reconnect);
  137. return s;
  138. }
  139. int main(void) {
  140. Suite *s = testSuite_Client();
  141. SRunner *sr = srunner_create(s);
  142. srunner_set_fork_status(sr, CK_NOFORK);
  143. srunner_run_all(sr,CK_NORMAL);
  144. int number_failed = srunner_ntests_failed(sr);
  145. srunner_free(sr);
  146. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  147. }