check_services_call.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. *
  5. * Copyright 2019 (c) basysKom GmbH <opensource@basyskom.com> (Author: Frank Meerkötter)
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include "check.h"
  11. #include "server/ua_services.h"
  12. #include "ua_client.h"
  13. #include "ua_types.h"
  14. #include "ua_config_default.h"
  15. #include "server/ua_server_internal.h"
  16. static UA_Server *server = NULL;
  17. static UA_ServerConfig *config = NULL;
  18. static UA_StatusCode
  19. methodCallback(UA_Server *serverArg,
  20. const UA_NodeId *sessionId, void *sessionHandle,
  21. const UA_NodeId *methodId, void *methodContext,
  22. const UA_NodeId *objectId, void *objectContext,
  23. size_t inputSize, const UA_Variant *input,
  24. size_t outputSize, UA_Variant *output)
  25. {
  26. return UA_STATUSCODE_GOOD;
  27. }
  28. static void setup(void) {
  29. config = UA_ServerConfig_new_default();
  30. server = UA_Server_new(config);
  31. UA_MethodAttributes noFpAttr = UA_MethodAttributes_default;
  32. noFpAttr.description = UA_LOCALIZEDTEXT("en-US","No function pointer attached");
  33. noFpAttr.displayName = UA_LOCALIZEDTEXT("en-US","No function pointer attached");
  34. noFpAttr.executable = true;
  35. noFpAttr.userExecutable = true;
  36. UA_Server_addMethodNode(server, UA_NODEID_STRING(1, "nofunctionpointer"),
  37. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  38. UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
  39. UA_QUALIFIEDNAME(1, "No function pointer"),
  40. noFpAttr, NULL, // no callback
  41. 0, NULL, 0, NULL, NULL, NULL);
  42. UA_MethodAttributes nonExecAttr = UA_MethodAttributes_default;
  43. nonExecAttr.description = UA_LOCALIZEDTEXT("en-US","Not executable");
  44. nonExecAttr.displayName = UA_LOCALIZEDTEXT("en-US","Not executable");
  45. nonExecAttr.executable = false;
  46. nonExecAttr.userExecutable = true;
  47. UA_Server_addMethodNode(server, UA_NODEID_STRING(1, "nonexec"),
  48. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  49. UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
  50. UA_QUALIFIEDNAME(1, "Not executable"),
  51. nonExecAttr, &methodCallback,
  52. 0, NULL, 0, NULL, NULL, NULL);
  53. }
  54. static void teardown(void) {
  55. UA_Server_delete(server);
  56. UA_ServerConfig_delete(config);
  57. }
  58. START_TEST(callUnknownMethod) {
  59. const UA_UInt32 UA_NS0ID_UNKNOWN_METHOD = 60000;
  60. UA_CallMethodRequest callMethodRequest;
  61. UA_CallMethodRequest_init(&callMethodRequest);
  62. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_UNKNOWN_METHOD);
  63. UA_CallMethodResult result;
  64. UA_CallMethodResult_init(&result);
  65. result = UA_Server_call(server, &callMethodRequest);
  66. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADNODEIDUNKNOWN);
  67. } END_TEST
  68. START_TEST(callKnownMethodOnUnknownObject) {
  69. const UA_UInt32 UA_NS0ID_UNKNOWN_OBJECT = 60000;
  70. UA_CallMethodRequest callMethodRequest;
  71. UA_CallMethodRequest_init(&callMethodRequest);
  72. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_REQUESTSERVERSTATECHANGE);
  73. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_UNKNOWN_OBJECT);
  74. UA_CallMethodResult result;
  75. UA_CallMethodResult_init(&result);
  76. result = UA_Server_call(server, &callMethodRequest);
  77. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADNODEIDUNKNOWN);
  78. } END_TEST
  79. START_TEST(callMethodAndObjectExistsButMethodHasWrongNodeClass) {
  80. UA_CallMethodRequest callMethodRequest;
  81. UA_CallMethodRequest_init(&callMethodRequest);
  82. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_AUDITING); // not a method
  83. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  84. UA_CallMethodResult result;
  85. UA_CallMethodResult_init(&result);
  86. result = UA_Server_call(server, &callMethodRequest);
  87. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADNODECLASSINVALID);
  88. } END_TEST
  89. START_TEST(callMethodAndObjectExistsButNoFunctionPointerAttached) {
  90. UA_CallMethodRequest callMethodRequest;
  91. UA_CallMethodRequest_init(&callMethodRequest);
  92. callMethodRequest.methodId = UA_NODEID_STRING(1, "nofunctionpointer");
  93. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  94. UA_CallMethodResult result;
  95. UA_CallMethodResult_init(&result);
  96. result = UA_Server_call(server, &callMethodRequest);
  97. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADINTERNALERROR);
  98. } END_TEST
  99. START_TEST(callMethodNonExecutable) {
  100. UA_CallMethodRequest callMethodRequest;
  101. UA_CallMethodRequest_init(&callMethodRequest);
  102. callMethodRequest.methodId = UA_NODEID_STRING(1, "nonexec");
  103. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  104. UA_CallMethodResult result;
  105. UA_CallMethodResult_init(&result);
  106. result = UA_Server_call(server, &callMethodRequest);
  107. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADNOTEXECUTABLE);
  108. } END_TEST
  109. int main(void) {
  110. Suite *s = suite_create("services_call");
  111. TCase *tc_call = tcase_create("call - error branches");
  112. tcase_add_checked_fixture(tc_call, setup, teardown);
  113. tcase_add_test(tc_call, callUnknownMethod);
  114. tcase_add_test(tc_call, callKnownMethodOnUnknownObject);
  115. tcase_add_test(tc_call, callMethodAndObjectExistsButMethodHasWrongNodeClass);
  116. tcase_add_test(tc_call, callMethodAndObjectExistsButNoFunctionPointerAttached);
  117. tcase_add_test(tc_call, callMethodNonExecutable);;
  118. suite_add_tcase(s, tc_call);
  119. SRunner *sr = srunner_create(s);
  120. srunner_set_fork_status(sr, CK_NOFORK);
  121. srunner_run_all(sr, CK_NORMAL);
  122. int number_failed = srunner_ntests_failed(sr);
  123. srunner_free(sr);
  124. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  125. }