check_client.c 5.1 KB

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