check_server.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 void setup(void) {
  18. server = UA_Server_new();
  19. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  20. }
  21. static void teardown(void) {
  22. UA_Server_delete(server);
  23. }
  24. START_TEST(checkGetConfig) {
  25. ck_assert_ptr_eq(UA_Server_getConfig(NULL), NULL);
  26. ck_assert_ptr_ne(UA_Server_getConfig(server), NULL);
  27. } END_TEST
  28. START_TEST(checkGetNamespaceByName) {
  29. size_t notFoundIndex = 62541;
  30. UA_StatusCode notFound = UA_Server_getNamespaceByName(server, UA_STRING("http://opcfoundation.org/UA/invalid"), &notFoundIndex);
  31. ck_assert_int_eq(notFoundIndex, 62541); // not changed
  32. ck_assert_int_eq(notFound, UA_STATUSCODE_BADNOTFOUND);
  33. size_t foundIndex = 62541;
  34. UA_StatusCode found = UA_Server_getNamespaceByName(server, UA_STRING("http://opcfoundation.org/UA/"), &foundIndex);
  35. ck_assert_int_eq(foundIndex, 0); // this namespace always has index 0 (defined by the standard)
  36. ck_assert_int_eq(found, UA_STATUSCODE_GOOD);
  37. } END_TEST
  38. static void timedCallbackHandler(UA_Server *s, void *data) {
  39. *((UA_Boolean*)data) = false; // stop the server via a timedCallback
  40. }
  41. START_TEST(checkServer_run) {
  42. UA_Boolean running = true;
  43. // 0 is in the past so the server will terminate on the first iteration
  44. UA_StatusCode ret;
  45. ret = UA_Server_addTimedCallback(server, &timedCallbackHandler, &running, 0, NULL);
  46. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  47. ret = UA_Server_run(server, &running);
  48. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  49. } END_TEST
  50. int main(void) {
  51. Suite *s = suite_create("server");
  52. TCase *tc_call = tcase_create("server - basics");
  53. tcase_add_checked_fixture(tc_call, setup, teardown);
  54. tcase_add_test(tc_call, checkGetConfig);
  55. tcase_add_test(tc_call, checkGetNamespaceByName);
  56. tcase_add_test(tc_call, checkServer_run);
  57. suite_add_tcase(s, tc_call);
  58. SRunner *sr = srunner_create(s);
  59. srunner_set_fork_status(sr, CK_NOFORK);
  60. srunner_run_all(sr, CK_NORMAL);
  61. int number_failed = srunner_ntests_failed(sr);
  62. srunner_free(sr);
  63. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  64. }