check_client.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_NULL, attr, NULL, NULL);
  37. UA_free(array);
  38. }
  39. static void * serverloop(void *_) {
  40. while(*running)
  41. UA_Server_run_iterate(server, true);
  42. return NULL;
  43. }
  44. static void setup(void) {
  45. running = UA_Boolean_new();
  46. *running = true;
  47. config = UA_ServerConfig_new_default();
  48. server = UA_Server_new(config);
  49. UA_Server_run_startup(server);
  50. addVariable(16366);
  51. pthread_create(&server_thread, NULL, serverloop, NULL);
  52. }
  53. static void teardown(void) {
  54. *running = false;
  55. pthread_join(server_thread, NULL);
  56. UA_Server_run_shutdown(server);
  57. UA_Boolean_delete(running);
  58. UA_Server_delete(server);
  59. UA_ServerConfig_delete(config);
  60. }
  61. START_TEST(Client_connect) {
  62. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  63. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  64. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  65. UA_Client_disconnect(client);
  66. UA_Client_delete(client);
  67. }
  68. END_TEST
  69. START_TEST(Client_read) {
  70. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  71. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  72. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  73. UA_Variant val;
  74. UA_NodeId nodeId = UA_NODEID_STRING(1, "my.variable");
  75. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  76. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  77. UA_Variant_deleteMembers(&val);
  78. UA_Client_disconnect(client);
  79. UA_Client_delete(client);
  80. }
  81. END_TEST
  82. static Suite* testSuite_Client(void) {
  83. Suite *s = suite_create("Client");
  84. TCase *tc_client = tcase_create("Client Basic");
  85. tcase_add_checked_fixture(tc_client, setup, teardown);
  86. tcase_add_test(tc_client, Client_connect);
  87. tcase_add_test(tc_client, Client_read);
  88. suite_add_tcase(s,tc_client);
  89. return s;
  90. }
  91. int main(void) {
  92. Suite *s = testSuite_Client();
  93. SRunner *sr = srunner_create(s);
  94. srunner_set_fork_status(sr, CK_NOFORK);
  95. srunner_run_all(sr,CK_NORMAL);
  96. int number_failed = srunner_ntests_failed(sr);
  97. srunner_free(sr);
  98. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  99. }