check_client.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_Boolean *running;
  15. UA_ServerNetworkLayer nl;
  16. pthread_t server_thread;
  17. static void
  18. addVariable(size_t size) {
  19. /* Define the attribute of the myInteger variable node */
  20. UA_VariableAttributes attr;
  21. UA_VariableAttributes_init(&attr);
  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. UA_ServerConfig config = UA_ServerConfig_standard;
  48. nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  49. config.networkLayers = &nl;
  50. config.networkLayersSize = 1;
  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. nl.deleteMembers(&nl);
  63. }
  64. START_TEST(Client_connect) {
  65. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  66. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:16664");
  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_standard);
  74. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:16664");
  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. static Suite* testSuite_Client(void) {
  86. Suite *s = suite_create("Client");
  87. TCase *tc_client = tcase_create("Client Basic");
  88. tcase_add_checked_fixture(tc_client, setup, teardown);
  89. tcase_add_test(tc_client, Client_connect);
  90. tcase_add_test(tc_client, Client_read);
  91. suite_add_tcase(s,tc_client);
  92. return s;
  93. }
  94. int main(void) {
  95. Suite *s = testSuite_Client();
  96. SRunner *sr = srunner_create(s);
  97. srunner_set_fork_status(sr, CK_NOFORK);
  98. srunner_run_all(sr,CK_NORMAL);
  99. int number_failed = srunner_ntests_failed(sr);
  100. srunner_free(sr);
  101. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  102. }