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