check_services_nodemanagement.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "check.h"
  5. #include "server/ua_nodestore.h"
  6. #include "server/ua_services.h"
  7. #include "ua_client.h"
  8. #include "ua_nodeids.h"
  9. #include "ua_types.h"
  10. #include "ua_config_standard.h"
  11. #include "server/ua_server_internal.h"
  12. #ifdef UA_ENABLE_MULTITHREADING
  13. #include <pthread.h>
  14. #include <urcu.h>
  15. #endif
  16. static UA_StatusCode
  17. instantiationMethod(UA_NodeId newNodeId, UA_NodeId templateId, void *handle ) {
  18. *((UA_Int32 *) handle) += 1;
  19. return UA_STATUSCODE_GOOD;
  20. }
  21. START_TEST(AddVariableNode) {
  22. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  23. /* add a variable node to the address space */
  24. UA_VariableAttributes attr;
  25. UA_VariableAttributes_init(&attr);
  26. UA_Int32 myInteger = 42;
  27. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  28. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  29. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  30. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  31. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  32. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  33. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  34. UA_StatusCode res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  35. myIntegerName, UA_NODEID_NULL, attr, NULL, NULL);
  36. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  37. UA_Server_delete(server);
  38. } END_TEST
  39. START_TEST(AddComplexTypeWithInheritance) {
  40. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  41. /* add a variable node to the address space */
  42. UA_ObjectAttributes attr;
  43. UA_ObjectAttributes_init(&attr);
  44. attr.description = UA_LOCALIZEDTEXT("en_US","fakeServerStruct");
  45. attr.displayName = UA_LOCALIZEDTEXT("en_US","fakeServerStruct");
  46. UA_NodeId myObjectNodeId = UA_NODEID_STRING(1, "the.fake.Server.Struct");
  47. UA_QualifiedName myObjectName = UA_QUALIFIEDNAME(1, "the.fake.Server.Struct");
  48. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  49. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  50. UA_Int32 handleCalled;
  51. UA_InstantiationCallback iCallback = {.method=instantiationMethod, .handle = (void *) &handleCalled};
  52. UA_StatusCode res = UA_Server_addObjectNode(server, myObjectNodeId, parentNodeId, parentReferenceNodeId,
  53. myObjectName, UA_NODEID_NUMERIC(0, 2004), attr, &iCallback, NULL);
  54. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  55. ck_assert_int_gt(handleCalled, 0); // Should be 58, but may depend on NS0 XML detail
  56. UA_Server_delete(server);
  57. } END_TEST
  58. START_TEST(AddNodeTwiceGivesError) {
  59. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  60. /* add a variable node to the address space */
  61. UA_VariableAttributes attr;
  62. UA_VariableAttributes_init(&attr);
  63. UA_Int32 myInteger = 42;
  64. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  65. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  66. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  67. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  68. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  69. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  70. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  71. UA_StatusCode res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  72. myIntegerName, UA_NODEID_NULL, attr, NULL, NULL);
  73. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  74. res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  75. myIntegerName, UA_NODEID_NULL, attr, NULL, NULL);
  76. ck_assert_int_eq(res, UA_STATUSCODE_BADNODEIDEXISTS);
  77. UA_Server_delete(server);
  78. } END_TEST
  79. static Suite * testSuite_services_nodemanagement(void) {
  80. Suite *s = suite_create("services_nodemanagement");
  81. TCase *tc_addnodes = tcase_create("addnodes");
  82. tcase_add_test(tc_addnodes, AddVariableNode);
  83. tcase_add_test(tc_addnodes, AddComplexTypeWithInheritance);
  84. tcase_add_test(tc_addnodes, AddNodeTwiceGivesError);
  85. suite_add_tcase(s, tc_addnodes);
  86. return s;
  87. }
  88. int main(void) {
  89. int number_failed = 0;
  90. Suite *s;
  91. s = testSuite_services_nodemanagement();
  92. SRunner *sr = srunner_create(s);
  93. srunner_set_fork_status(sr, CK_NOFORK);
  94. srunner_run_all(sr, CK_NORMAL);
  95. number_failed += srunner_ntests_failed(sr);
  96. srunner_free(sr);
  97. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  98. }