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