check_accesscontrol.c 3.2 KB

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