check_services_nodemanagement.c 4.7 KB

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