check_services_call.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_StatusCode
  18. methodCallback(UA_Server *serverArg,
  19. const UA_NodeId *sessionId, void *sessionHandle,
  20. const UA_NodeId *methodId, void *methodContext,
  21. const UA_NodeId *objectId, void *objectContext,
  22. size_t inputSize, const UA_Variant *input,
  23. size_t outputSize, UA_Variant *output)
  24. {
  25. return UA_STATUSCODE_GOOD;
  26. }
  27. static void setup(void) {
  28. server = UA_Server_new();
  29. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  30. UA_MethodAttributes noFpAttr = UA_MethodAttributes_default;
  31. noFpAttr.description = UA_LOCALIZEDTEXT("en-US","No function pointer attached");
  32. noFpAttr.displayName = UA_LOCALIZEDTEXT("en-US","No function pointer attached");
  33. noFpAttr.executable = true;
  34. noFpAttr.userExecutable = true;
  35. UA_Server_addMethodNode(server, UA_NODEID_STRING(1, "nofunctionpointer"),
  36. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  37. UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
  38. UA_QUALIFIEDNAME(1, "No function pointer"),
  39. noFpAttr, NULL, // no callback
  40. 0, NULL, 0, NULL, NULL, NULL);
  41. UA_MethodAttributes nonExecAttr = UA_MethodAttributes_default;
  42. nonExecAttr.description = UA_LOCALIZEDTEXT("en-US","Not executable");
  43. nonExecAttr.displayName = UA_LOCALIZEDTEXT("en-US","Not executable");
  44. nonExecAttr.executable = false;
  45. nonExecAttr.userExecutable = true;
  46. UA_Server_addMethodNode(server, UA_NODEID_STRING(1, "nonexec"),
  47. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  48. UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
  49. UA_QUALIFIEDNAME(1, "Not executable"),
  50. nonExecAttr, &methodCallback,
  51. 0, NULL, 0, NULL, NULL, NULL);
  52. }
  53. static void teardown(void) {
  54. UA_Server_delete(server);
  55. }
  56. START_TEST(callUnknownMethod) {
  57. const UA_UInt32 UA_NS0ID_UNKNOWN_METHOD = 60000;
  58. UA_CallMethodRequest callMethodRequest;
  59. UA_CallMethodRequest_init(&callMethodRequest);
  60. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_UNKNOWN_METHOD);
  61. UA_CallMethodResult result;
  62. UA_CallMethodResult_init(&result);
  63. result = UA_Server_call(server, &callMethodRequest);
  64. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADNODEIDUNKNOWN);
  65. } END_TEST
  66. START_TEST(callKnownMethodOnUnknownObject) {
  67. const UA_UInt32 UA_NS0ID_UNKNOWN_OBJECT = 60000;
  68. UA_CallMethodRequest callMethodRequest;
  69. UA_CallMethodRequest_init(&callMethodRequest);
  70. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_REQUESTSERVERSTATECHANGE);
  71. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_UNKNOWN_OBJECT);
  72. UA_CallMethodResult result;
  73. UA_CallMethodResult_init(&result);
  74. result = UA_Server_call(server, &callMethodRequest);
  75. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADNODEIDUNKNOWN);
  76. } END_TEST
  77. START_TEST(callMethodAndObjectExistsButMethodHasWrongNodeClass) {
  78. UA_CallMethodRequest callMethodRequest;
  79. UA_CallMethodRequest_init(&callMethodRequest);
  80. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS); // not a method
  81. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  82. UA_CallMethodResult result;
  83. UA_CallMethodResult_init(&result);
  84. result = UA_Server_call(server, &callMethodRequest);
  85. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADNODECLASSINVALID);
  86. } END_TEST
  87. START_TEST(callMethodOnUnrelatedObject) {
  88. /* Minimal nodeset does not add any method nodes we may call here */
  89. #ifdef UA_GENERATED_NAMESPACE_ZERO
  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. #endif
  99. } END_TEST
  100. START_TEST(callMethodAndObjectExistsButNoFunctionPointerAttached) {
  101. UA_CallMethodRequest callMethodRequest;
  102. UA_CallMethodRequest_init(&callMethodRequest);
  103. callMethodRequest.methodId = UA_NODEID_STRING(1, "nofunctionpointer");
  104. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  105. UA_CallMethodResult result;
  106. UA_CallMethodResult_init(&result);
  107. result = UA_Server_call(server, &callMethodRequest);
  108. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADINTERNALERROR);
  109. } END_TEST
  110. START_TEST(callMethodNonExecutable) {
  111. UA_CallMethodRequest callMethodRequest;
  112. UA_CallMethodRequest_init(&callMethodRequest);
  113. callMethodRequest.methodId = UA_NODEID_STRING(1, "nonexec");
  114. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  115. UA_CallMethodResult result;
  116. UA_CallMethodResult_init(&result);
  117. result = UA_Server_call(server, &callMethodRequest);
  118. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADNOTEXECUTABLE);
  119. } END_TEST
  120. START_TEST(callMethodWithMissingArguments) {
  121. /* Minimal nodeset does not add any method nodes we may call here */
  122. #ifdef UA_GENERATED_NAMESPACE_ZERO
  123. UA_CallMethodRequest callMethodRequest;
  124. UA_CallMethodRequest_init(&callMethodRequest);
  125. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  126. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  127. UA_CallMethodResult result;
  128. UA_CallMethodResult_init(&result);
  129. result = UA_Server_call(server, &callMethodRequest);
  130. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADARGUMENTSMISSING);
  131. #endif
  132. } END_TEST
  133. START_TEST(callMethodWithTooManyArguments) {
  134. /* Minimal nodeset does not add any method nodes we may call here */
  135. #ifdef UA_GENERATED_NAMESPACE_ZERO
  136. UA_Variant inputArguments[2];
  137. UA_Variant_init(&inputArguments[0]);
  138. UA_Variant_init(&inputArguments[1]);
  139. UA_CallMethodRequest callMethodRequest;
  140. UA_CallMethodRequest_init(&callMethodRequest);
  141. callMethodRequest.inputArgumentsSize = 2; // 1 would be correct
  142. callMethodRequest.inputArguments = (UA_Variant*)&inputArguments;
  143. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  144. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  145. UA_CallMethodResult result;
  146. UA_CallMethodResult_init(&result);
  147. result = UA_Server_call(server, &callMethodRequest);
  148. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADTOOMANYARGUMENTS);
  149. #endif
  150. } END_TEST
  151. START_TEST(callMethodWithWronglyTypedArguments) {
  152. /* Minimal nodeset does not add any method nodes we may call here */
  153. #ifdef UA_GENERATED_NAMESPACE_ZERO
  154. UA_Variant inputArgument;
  155. UA_Variant_init(&inputArgument);
  156. UA_Double wrongType = 1.0;
  157. UA_Variant_setScalar(&inputArgument, &wrongType, &UA_TYPES[UA_TYPES_DOUBLE]); // UA_UInt32 would be correct
  158. UA_CallMethodRequest callMethodRequest;
  159. UA_CallMethodRequest_init(&callMethodRequest);
  160. callMethodRequest.inputArgumentsSize = 1;
  161. callMethodRequest.inputArguments = &inputArgument;
  162. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  163. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  164. UA_CallMethodResult result;
  165. UA_CallMethodResult_init(&result);
  166. result = UA_Server_call(server, &callMethodRequest);
  167. ck_assert_int_gt(result.inputArgumentResultsSize, 0);
  168. ck_assert_int_eq(result.inputArgumentResults[0], UA_STATUSCODE_BADTYPEMISMATCH);
  169. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADINVALIDARGUMENT);
  170. UA_Array_delete(result.inputArgumentResults, result.inputArgumentResultsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  171. #endif
  172. } END_TEST
  173. int main(void) {
  174. Suite *s = suite_create("services_call");
  175. TCase *tc_call = tcase_create("call - error branches");
  176. tcase_add_checked_fixture(tc_call, setup, teardown);
  177. tcase_add_test(tc_call, callUnknownMethod);
  178. tcase_add_test(tc_call, callKnownMethodOnUnknownObject);
  179. tcase_add_test(tc_call, callMethodAndObjectExistsButMethodHasWrongNodeClass);
  180. tcase_add_test(tc_call, callMethodAndObjectExistsButNoFunctionPointerAttached);
  181. tcase_add_test(tc_call, callMethodNonExecutable);
  182. tcase_add_test(tc_call, callMethodOnUnrelatedObject);
  183. tcase_add_test(tc_call, callMethodWithMissingArguments);
  184. tcase_add_test(tc_call, callMethodWithTooManyArguments);
  185. tcase_add_test(tc_call, callMethodWithWronglyTypedArguments);
  186. suite_add_tcase(s, tc_call);
  187. SRunner *sr = srunner_create(s);
  188. srunner_set_fork_status(sr, CK_NOFORK);
  189. srunner_run_all(sr, CK_NORMAL);
  190. int number_failed = srunner_ntests_failed(sr);
  191. srunner_free(sr);
  192. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  193. }