check_services_call.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <open62541/server.h>
  8. #include <open62541/server_config_default.h>
  9. #include <open62541/types.h>
  10. #include "server/ua_server_internal.h"
  11. #include "server/ua_services.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <time.h>
  15. #include "check.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(callMethodOnUnrelatedObject) {
  90. UA_CallMethodRequest callMethodRequest;
  91. UA_CallMethodRequest_init(&callMethodRequest);
  92. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  93. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); // not connected via hasComponent
  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_BADMETHODINVALID);
  98. } END_TEST
  99. START_TEST(callMethodAndObjectExistsButNoFunctionPointerAttached) {
  100. UA_CallMethodRequest callMethodRequest;
  101. UA_CallMethodRequest_init(&callMethodRequest);
  102. callMethodRequest.methodId = UA_NODEID_STRING(1, "nofunctionpointer");
  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_BADINTERNALERROR);
  108. } END_TEST
  109. START_TEST(callMethodNonExecutable) {
  110. UA_CallMethodRequest callMethodRequest;
  111. UA_CallMethodRequest_init(&callMethodRequest);
  112. callMethodRequest.methodId = UA_NODEID_STRING(1, "nonexec");
  113. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  114. UA_CallMethodResult result;
  115. UA_CallMethodResult_init(&result);
  116. result = UA_Server_call(server, &callMethodRequest);
  117. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADNOTEXECUTABLE);
  118. } END_TEST
  119. START_TEST(callMethodWithMissingArguments) {
  120. UA_CallMethodRequest callMethodRequest;
  121. UA_CallMethodRequest_init(&callMethodRequest);
  122. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  123. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  124. UA_CallMethodResult result;
  125. UA_CallMethodResult_init(&result);
  126. result = UA_Server_call(server, &callMethodRequest);
  127. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADARGUMENTSMISSING);
  128. } END_TEST
  129. START_TEST(callMethodWithTooManyArguments) {
  130. UA_Variant inputArguments[2];
  131. UA_Variant_init(&inputArguments[0]);
  132. UA_Variant_init(&inputArguments[1]);
  133. UA_CallMethodRequest callMethodRequest;
  134. UA_CallMethodRequest_init(&callMethodRequest);
  135. callMethodRequest.inputArgumentsSize = 2; // 1 would be correct
  136. callMethodRequest.inputArguments = (UA_Variant*)&inputArguments;
  137. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  138. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  139. UA_CallMethodResult result;
  140. UA_CallMethodResult_init(&result);
  141. result = UA_Server_call(server, &callMethodRequest);
  142. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADTOOMANYARGUMENTS);
  143. } END_TEST
  144. START_TEST(callMethodWithWronglyTypedArguments) {
  145. UA_Variant inputArgument;
  146. UA_Variant_init(&inputArgument);
  147. UA_Double wrongType = 1.0;
  148. UA_Variant_setScalar(&inputArgument, &wrongType, &UA_TYPES[UA_TYPES_DOUBLE]); // UA_UInt32 would be correct
  149. UA_CallMethodRequest callMethodRequest;
  150. UA_CallMethodRequest_init(&callMethodRequest);
  151. callMethodRequest.inputArgumentsSize = 1;
  152. callMethodRequest.inputArguments = &inputArgument;
  153. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  154. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  155. UA_CallMethodResult result;
  156. UA_CallMethodResult_init(&result);
  157. result = UA_Server_call(server, &callMethodRequest);
  158. ck_assert_int_eq(result.inputArgumentResults[0], UA_STATUSCODE_BADTYPEMISMATCH);
  159. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADINVALIDARGUMENT);
  160. UA_Array_delete(result.inputArgumentResults, result.inputArgumentResultsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  161. } END_TEST
  162. int main(void) {
  163. Suite *s = suite_create("services_call");
  164. TCase *tc_call = tcase_create("call - error branches");
  165. tcase_add_checked_fixture(tc_call, setup, teardown);
  166. tcase_add_test(tc_call, callUnknownMethod);
  167. tcase_add_test(tc_call, callKnownMethodOnUnknownObject);
  168. tcase_add_test(tc_call, callMethodAndObjectExistsButMethodHasWrongNodeClass);
  169. tcase_add_test(tc_call, callMethodAndObjectExistsButNoFunctionPointerAttached);
  170. tcase_add_test(tc_call, callMethodNonExecutable);
  171. tcase_add_test(tc_call, callMethodOnUnrelatedObject);
  172. tcase_add_test(tc_call, callMethodWithMissingArguments);
  173. tcase_add_test(tc_call, callMethodWithTooManyArguments);
  174. tcase_add_test(tc_call, callMethodWithWronglyTypedArguments);
  175. suite_add_tcase(s, tc_call);
  176. SRunner *sr = srunner_create(s);
  177. srunner_set_fork_status(sr, CK_NOFORK);
  178. srunner_run_all(sr, CK_NORMAL);
  179. int number_failed = srunner_ntests_failed(sr);
  180. srunner_free(sr);
  181. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  182. }