check_services_call.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_SERVERSTATUS); // 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. /* Minimal nodeset does not add any method nodes we may call here */
  91. #ifdef UA_GENERATED_NAMESPACE_ZERO
  92. UA_CallMethodRequest callMethodRequest;
  93. UA_CallMethodRequest_init(&callMethodRequest);
  94. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  95. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); // not connected via hasComponent
  96. UA_CallMethodResult result;
  97. UA_CallMethodResult_init(&result);
  98. result = UA_Server_call(server, &callMethodRequest);
  99. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADMETHODINVALID);
  100. #endif
  101. } END_TEST
  102. START_TEST(callMethodAndObjectExistsButNoFunctionPointerAttached) {
  103. UA_CallMethodRequest callMethodRequest;
  104. UA_CallMethodRequest_init(&callMethodRequest);
  105. callMethodRequest.methodId = UA_NODEID_STRING(1, "nofunctionpointer");
  106. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  107. UA_CallMethodResult result;
  108. UA_CallMethodResult_init(&result);
  109. result = UA_Server_call(server, &callMethodRequest);
  110. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADINTERNALERROR);
  111. } END_TEST
  112. START_TEST(callMethodNonExecutable) {
  113. UA_CallMethodRequest callMethodRequest;
  114. UA_CallMethodRequest_init(&callMethodRequest);
  115. callMethodRequest.methodId = UA_NODEID_STRING(1, "nonexec");
  116. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  117. UA_CallMethodResult result;
  118. UA_CallMethodResult_init(&result);
  119. result = UA_Server_call(server, &callMethodRequest);
  120. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADNOTEXECUTABLE);
  121. } END_TEST
  122. START_TEST(callMethodWithMissingArguments) {
  123. /* Minimal nodeset does not add any method nodes we may call here */
  124. #ifdef UA_GENERATED_NAMESPACE_ZERO
  125. UA_CallMethodRequest callMethodRequest;
  126. UA_CallMethodRequest_init(&callMethodRequest);
  127. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  128. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  129. UA_CallMethodResult result;
  130. UA_CallMethodResult_init(&result);
  131. result = UA_Server_call(server, &callMethodRequest);
  132. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADARGUMENTSMISSING);
  133. #endif
  134. } END_TEST
  135. START_TEST(callMethodWithTooManyArguments) {
  136. /* Minimal nodeset does not add any method nodes we may call here */
  137. #ifdef UA_GENERATED_NAMESPACE_ZERO
  138. UA_Variant inputArguments[2];
  139. UA_Variant_init(&inputArguments[0]);
  140. UA_Variant_init(&inputArguments[1]);
  141. UA_CallMethodRequest callMethodRequest;
  142. UA_CallMethodRequest_init(&callMethodRequest);
  143. callMethodRequest.inputArgumentsSize = 2; // 1 would be correct
  144. callMethodRequest.inputArguments = (UA_Variant*)&inputArguments;
  145. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  146. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  147. UA_CallMethodResult result;
  148. UA_CallMethodResult_init(&result);
  149. result = UA_Server_call(server, &callMethodRequest);
  150. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADTOOMANYARGUMENTS);
  151. #endif
  152. } END_TEST
  153. START_TEST(callMethodWithWronglyTypedArguments) {
  154. /* Minimal nodeset does not add any method nodes we may call here */
  155. #ifdef UA_GENERATED_NAMESPACE_ZERO
  156. UA_Variant inputArgument;
  157. UA_Variant_init(&inputArgument);
  158. UA_Double wrongType = 1.0;
  159. UA_Variant_setScalar(&inputArgument, &wrongType, &UA_TYPES[UA_TYPES_DOUBLE]); // UA_UInt32 would be correct
  160. UA_CallMethodRequest callMethodRequest;
  161. UA_CallMethodRequest_init(&callMethodRequest);
  162. callMethodRequest.inputArgumentsSize = 1;
  163. callMethodRequest.inputArguments = &inputArgument;
  164. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  165. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  166. UA_CallMethodResult result;
  167. UA_CallMethodResult_init(&result);
  168. result = UA_Server_call(server, &callMethodRequest);
  169. ck_assert_int_gt(result.inputArgumentResultsSize, 0);
  170. ck_assert_int_eq(result.inputArgumentResults[0], UA_STATUSCODE_BADTYPEMISMATCH);
  171. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_BADINVALIDARGUMENT);
  172. UA_Array_delete(result.inputArgumentResults, result.inputArgumentResultsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  173. #endif
  174. } END_TEST
  175. int main(void) {
  176. Suite *s = suite_create("services_call");
  177. TCase *tc_call = tcase_create("call - error branches");
  178. tcase_add_checked_fixture(tc_call, setup, teardown);
  179. tcase_add_test(tc_call, callUnknownMethod);
  180. tcase_add_test(tc_call, callKnownMethodOnUnknownObject);
  181. tcase_add_test(tc_call, callMethodAndObjectExistsButMethodHasWrongNodeClass);
  182. tcase_add_test(tc_call, callMethodAndObjectExistsButNoFunctionPointerAttached);
  183. tcase_add_test(tc_call, callMethodNonExecutable);
  184. tcase_add_test(tc_call, callMethodOnUnrelatedObject);
  185. tcase_add_test(tc_call, callMethodWithMissingArguments);
  186. tcase_add_test(tc_call, callMethodWithTooManyArguments);
  187. tcase_add_test(tc_call, callMethodWithWronglyTypedArguments);
  188. suite_add_tcase(s, tc_call);
  189. SRunner *sr = srunner_create(s);
  190. srunner_set_fork_status(sr, CK_NOFORK);
  191. srunner_run_all(sr, CK_NORMAL);
  192. int number_failed = srunner_ntests_failed(sr);
  193. srunner_free(sr);
  194. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  195. }