check_accesscontrol.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include <stdlib.h>
  5. #include <pthread.h>
  6. #include "ua_types.h"
  7. #include "ua_server.h"
  8. #include "ua_client.h"
  9. #include "ua_config_standard.h"
  10. #include "check.h"
  11. UA_Server *server;
  12. UA_ServerConfig *config;
  13. UA_Boolean *running;
  14. UA_ServerNetworkLayer nl;
  15. pthread_t server_thread;
  16. static void * serverloop(void *_) {
  17. while(*running)
  18. UA_Server_run_iterate(server, true);
  19. return NULL;
  20. }
  21. static void setup(void) {
  22. running = UA_Boolean_new();
  23. *running = true;
  24. config = UA_ServerConfig_new_default();
  25. server = UA_Server_new(config);
  26. UA_Server_run_startup(server);
  27. pthread_create(&server_thread, NULL, serverloop, NULL);
  28. }
  29. static void teardown(void) {
  30. *running = false;
  31. pthread_join(server_thread, NULL);
  32. UA_Server_run_shutdown(server);
  33. UA_Boolean_delete(running);
  34. UA_Server_delete(server);
  35. UA_ServerConfig_delete(config);
  36. }
  37. START_TEST(Client_anonymous) {
  38. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  39. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  40. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  41. UA_Client_disconnect(client);
  42. UA_Client_delete(client);
  43. }
  44. END_TEST
  45. START_TEST(Client_user_pass_ok) {
  46. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  47. UA_StatusCode retval = UA_Client_connect_username(client, "opc.tcp://localhost:4840", "user1", "password");
  48. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  49. UA_Client_disconnect(client);
  50. UA_Client_delete(client);
  51. }
  52. END_TEST
  53. START_TEST(Client_user_fail) {
  54. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  55. UA_StatusCode retval = UA_Client_connect_username(client, "opc.tcp://localhost:4840", "user0", "password");
  56. ck_assert_uint_eq(retval, UA_STATUSCODE_BADUSERACCESSDENIED);
  57. UA_Client_disconnect(client);
  58. UA_Client_delete(client);
  59. }
  60. END_TEST
  61. START_TEST(Client_pass_fail) {
  62. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  63. UA_StatusCode retval = UA_Client_connect_username(client, "opc.tcp://localhost:4840", "user1", "secret");
  64. ck_assert_uint_eq(retval, UA_STATUSCODE_BADUSERACCESSDENIED);
  65. UA_Client_disconnect(client);
  66. UA_Client_delete(client);
  67. }
  68. END_TEST
  69. static Suite* testSuite_Client(void) {
  70. Suite *s = suite_create("Client");
  71. TCase *tc_client_user = tcase_create("Client User/Password");
  72. tcase_add_checked_fixture(tc_client_user, setup, teardown);
  73. tcase_add_test(tc_client_user, Client_anonymous);
  74. tcase_add_test(tc_client_user, Client_user_pass_ok);
  75. tcase_add_test(tc_client_user, Client_user_fail);
  76. tcase_add_test(tc_client_user, Client_pass_fail);
  77. suite_add_tcase(s,tc_client_user);
  78. return s;
  79. }
  80. int main(void) {
  81. Suite *s = testSuite_Client();
  82. SRunner *sr = srunner_create(s);
  83. srunner_set_fork_status(sr, CK_NOFORK);
  84. srunner_run_all(sr,CK_NORMAL);
  85. int number_failed = srunner_ntests_failed(sr);
  86. srunner_free(sr);
  87. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  88. }