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