From ec74b4a8036e8a47d6f7c607329faa26fafea4cb Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 17 Mar 2019 15:31:26 +0100 Subject: [PATCH] added unit tests for authenticators --- CMakeLists.txt | 10 ++- lib/wsfs/adapter/authenticators.c | 10 +-- lib/wsfs/adapter/authenticators.h | 9 ++ test/mock_authenticator.cc | 42 ++++++++++ test/mock_authenticator.hpp | 34 ++++++++ test/test_authenticator.cc | 63 ++++++++++++++ test/test_authenticators.cc | 135 ++++++++++++++++++++++++++++++ 7 files changed, 291 insertions(+), 12 deletions(-) create mode 100644 test/mock_authenticator.cc create mode 100644 test/mock_authenticator.hpp create mode 100644 test/test_authenticator.cc create mode 100644 test/test_authenticators.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 7218be6..96b829d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,6 +186,7 @@ endif(NOT WITHOUT_EXAMPLE) if(NOT WITHOUT_TESTS) pkg_check_modules(GTEST gtest_main) +pkg_check_modules(GMOCK gmock) add_library(wsfs-adapter-static STATIC ${WSFS_ADAPTER_SOURCES}) set_target_properties(wsfs-adapter-static PROPERTIES OUTPUT_NAME wsfs-adapter) @@ -200,18 +201,21 @@ target_compile_options(wsfs-provider-static PUBLIC ${EXTRA_CFLAGS}) add_executable(alltests test/msleep.cc + test/mock_authenticator.cc test/test_response_parser.cc test/test_server.cc test/test_timepoint.cc test/test_timer.cc test/test_url.cc test/test_credentials.cc + test/test_authenticator.cc + test/test_authenticators.cc ${WSFS_COMMON_SOURCES} ) -target_link_libraries(alltests PUBLIC wsfs-adapter-static wsfs-provider-static ${EXTRA_LIBS} ${GTEST_LIBRARIES}) -target_include_directories(alltests PUBLIC lib ${EXTRA_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS}) -target_compile_options(alltests PUBLIC ${EXTRA_CFLAGS} ${GTEST_CFLAGS}) +target_link_libraries(alltests PUBLIC wsfs-adapter-static wsfs-provider-static ${EXTRA_LIBS} ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES}) +target_include_directories(alltests PUBLIC lib ${EXTRA_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS}) +target_compile_options(alltests PUBLIC ${EXTRA_CFLAGS} ${GMOCK_CFLAGS} ${GTEST_CFLAGS}) enable_testing() add_test(alltests alltests) diff --git a/lib/wsfs/adapter/authenticators.c b/lib/wsfs/adapter/authenticators.c index aa59809..58176bb 100644 --- a/lib/wsfs/adapter/authenticators.c +++ b/lib/wsfs/adapter/authenticators.c @@ -78,7 +78,7 @@ bool wsfs_authenticators_authenticate( struct wsfs_authenticators * authenticators, struct wsfs_credentials * credentials) { - bool result; + bool result = (NULL == authenticators->first); if (NULL != credentials) { @@ -87,14 +87,6 @@ bool wsfs_authenticators_authenticate( { result = wsfs_authenticator_autenticate(authenticator, credentials); } - else - { - result = false; - } - } - else - { - result = (NULL == authenticators->first); } return result; diff --git a/lib/wsfs/adapter/authenticators.h b/lib/wsfs/adapter/authenticators.h index c7cadf6..bbabf68 100644 --- a/lib/wsfs/adapter/authenticators.h +++ b/lib/wsfs/adapter/authenticators.h @@ -15,6 +15,11 @@ struct wsfs_authenticators struct wsfs_authenticator * first; }; +#ifdef __cplusplus +extern "C" +{ +#endif + extern void wsfs_authenticators_init( struct wsfs_authenticators * authenticators); @@ -35,4 +40,8 @@ extern bool wsfs_authenticators_authenticate( struct wsfs_authenticators * authenticators, struct wsfs_credentials * credentials); +#ifdef __cplusplus +} +#endif + #endif diff --git a/test/mock_authenticator.cc b/test/mock_authenticator.cc new file mode 100644 index 0000000..f455270 --- /dev/null +++ b/test/mock_authenticator.cc @@ -0,0 +1,42 @@ +#include "mock_authenticator.hpp" + +#define WSFS_AUTHENTICTOR_COUNT 3 + +namespace +{ + +wsfs_test::Authenticator * g_authenticators[WSFS_AUTHENTICTOR_COUNT]; + +} + + +namespace wsfs_test +{ + +void set_authenticator(Authenticator * authenticator) +{ + set_authenticator(0, authenticator); +} + +void set_authenticator(size_t i, Authenticator * authenticator) +{ + g_authenticators[i] = authenticator; +} + +bool authenticate(struct wsfs_credentials * creds, void * user_data) +{ + return g_authenticators[0]->authenticate(creds, user_data); +} + +bool authenticate_1(struct wsfs_credentials * creds, void * user_data) +{ + return g_authenticators[1]->authenticate(creds, user_data); +} + +bool authenticate_2(struct wsfs_credentials * creds, void * user_data) +{ + return g_authenticators[2]->authenticate(creds, user_data); +} + + +} \ No newline at end of file diff --git a/test/mock_authenticator.hpp b/test/mock_authenticator.hpp new file mode 100644 index 0000000..ef5d909 --- /dev/null +++ b/test/mock_authenticator.hpp @@ -0,0 +1,34 @@ +#ifndef MOCK_AUTHENTICATOR_H +#define MOCK_AUTHENTICATOR_H + +#include +#include "wsfs/adapter/authenticator.h" + +namespace wsfs_test +{ + +class Authenticator +{ +public: + virtual ~Authenticator() { } + virtual bool authenticate( + struct wsfs_credentials * credentials, + void * user_data) = 0; +}; + +class MockAuthenticator: public Authenticator +{ +public: + MOCK_METHOD2(authenticate, bool (struct wsfs_credentials * credentials, void * user_data)); +}; + +void set_authenticator(Authenticator * authenticator); +void set_authenticator(size_t index, Authenticator * authenticator); + +bool authenticate(struct wsfs_credentials * creds, void * user_data); +bool authenticate_1(struct wsfs_credentials * creds, void * user_data); +bool authenticate_2(struct wsfs_credentials * creds, void * user_data); + +} + +#endif diff --git a/test/test_authenticator.cc b/test/test_authenticator.cc new file mode 100644 index 0000000..71989f2 --- /dev/null +++ b/test/test_authenticator.cc @@ -0,0 +1,63 @@ +#include +#include + +#include "mock_authenticator.hpp" + +#include "wsfs/adapter/authenticator.h" +#include "wsfs/adapter/credentials_intern.h" + +using ::testing::Return; +using ::testing::_; +using ::wsfs_test::Authenticator; +using ::wsfs_test::MockAuthenticator; +using ::wsfs_test::set_authenticator; +using ::wsfs_test::authenticate; + + +TEST(Authenticator, Authenticate) +{ + MockAuthenticator mock; + set_authenticator(&mock); + + struct wsfs_credentials creds; + wsfs_credentials_init(&creds, "username", nullptr); + char dummy[] = "usr_data"; + void * user_data = reinterpret_cast(dummy); + + EXPECT_CALL(mock, authenticate(&creds, user_data)) + .Times(1) + .WillRepeatedly(Return(true)); + + struct wsfs_authenticator * authenticator = wsfs_authenticator_create( + "username", + &authenticate, + user_data); + + bool result = wsfs_authenticator_autenticate(authenticator, &creds); + ASSERT_TRUE(result); + + wsfs_authenticator_dispose(authenticator); + wsfs_credentials_cleanup(&creds); +} + +TEST(Authenticator, SkipAuthenticationWithWrongType) +{ + MockAuthenticator mock; + set_authenticator(&mock); + + struct wsfs_credentials creds; + wsfs_credentials_init(&creds, "username", nullptr); + EXPECT_CALL(mock, authenticate(_, _)) + .Times(0); + + struct wsfs_authenticator * authenticator = wsfs_authenticator_create( + "certificate", + &authenticate, + nullptr); + + bool result = wsfs_authenticator_autenticate(authenticator, &creds); + ASSERT_FALSE(result); + + wsfs_authenticator_dispose(authenticator); + wsfs_credentials_cleanup(&creds); +} \ No newline at end of file diff --git a/test/test_authenticators.cc b/test/test_authenticators.cc new file mode 100644 index 0000000..bcd2c5e --- /dev/null +++ b/test/test_authenticators.cc @@ -0,0 +1,135 @@ +#include +#include + +#include "wsfs/adapter/authenticators.h" +#include "wsfs/adapter/credentials_intern.h" +#include "mock_authenticator.hpp" + +using ::testing::_; +using ::testing::Return; +using ::wsfs_test::MockAuthenticator; +using ::wsfs_test::set_authenticator; +using ::wsfs_test::authenticate; +using ::wsfs_test::authenticate_1; +using ::wsfs_test::authenticate_2; + + +TEST(Authenticators, CloneEmpty) +{ + struct wsfs_authenticators authenticators; + struct wsfs_authenticators clone; + + wsfs_authenticators_init(&authenticators); + ASSERT_EQ(nullptr, authenticators.first); + + wsfs_authenticators_clone(&authenticators, &clone); + ASSERT_EQ(nullptr, clone.first); + + wsfs_authenticators_cleanup(&authenticators); + wsfs_authenticators_cleanup(&clone); +} + +TEST(Authenticators, Clone) +{ + struct wsfs_authenticators authenticators; + struct wsfs_authenticators clone; + + wsfs_authenticators_init(&authenticators); + wsfs_authenticators_add(&authenticators, "username", &authenticate, nullptr); + ASSERT_NE(nullptr, authenticators.first); + + wsfs_authenticators_clone(&authenticators, &clone); + ASSERT_NE(nullptr, clone.first); + ASSERT_NE(authenticators.first, clone.first); + + wsfs_authenticators_cleanup(&authenticators); + wsfs_authenticators_cleanup(&clone); +} + +TEST(Authenticators, AuthenticateWithoutAuthenticators) +{ + struct wsfs_credentials creds; + wsfs_credentials_init(&creds, "username", nullptr); + + struct wsfs_authenticators authenticators; + wsfs_authenticators_init(&authenticators); + + bool result = wsfs_authenticators_authenticate(&authenticators, &creds); + ASSERT_TRUE(result); + + result = wsfs_authenticators_authenticate(&authenticators, nullptr); + ASSERT_TRUE(result); + + wsfs_authenticators_cleanup(&authenticators); + wsfs_credentials_cleanup(&creds); +} + +TEST(Authenticators, FailToAuthenticateWithoutCredentials) +{ + MockAuthenticator mock; + set_authenticator(&mock); + + struct wsfs_authenticators authenticators; + wsfs_authenticators_init(&authenticators); + wsfs_authenticators_add(&authenticators, "username", &authenticate, nullptr); + + bool result = wsfs_authenticators_authenticate(&authenticators, nullptr); + ASSERT_FALSE(result); + + wsfs_authenticators_cleanup(&authenticators); +} + +TEST(Authenticators, AuthenticateWithMultipleCredentials) +{ + struct wsfs_credentials creds; + wsfs_credentials_init(&creds, "username", nullptr); + + MockAuthenticator username_mock; + set_authenticator(1, &username_mock); + EXPECT_CALL(username_mock, authenticate(&creds, nullptr)) + .Times(1) + .WillRepeatedly(Return(true)); + + MockAuthenticator certificate_mock; + set_authenticator(2, &certificate_mock); + EXPECT_CALL(certificate_mock, authenticate(_, _)) + .Times(0); + + struct wsfs_authenticators authenticators; + wsfs_authenticators_init(&authenticators); + wsfs_authenticators_add(&authenticators, "username", &authenticate_1, nullptr); + wsfs_authenticators_add(&authenticators, "certificate", &authenticate_2, nullptr); + + bool result = wsfs_authenticators_authenticate(&authenticators, &creds); + ASSERT_TRUE(result); + + wsfs_authenticators_cleanup(&authenticators); + wsfs_credentials_cleanup(&creds); +} + +TEST(Authenticators, FailedAuthenticateWithWrongType) +{ + struct wsfs_credentials creds; + wsfs_credentials_init(&creds, "token", nullptr); + + MockAuthenticator username_mock; + set_authenticator(1, &username_mock); + EXPECT_CALL(username_mock, authenticate(&creds, nullptr)) + .Times(0); + + MockAuthenticator certificate_mock; + set_authenticator(2, &certificate_mock); + EXPECT_CALL(certificate_mock, authenticate(_, _)) + .Times(0); + + struct wsfs_authenticators authenticators; + wsfs_authenticators_init(&authenticators); + wsfs_authenticators_add(&authenticators, "username", &authenticate_1, nullptr); + wsfs_authenticators_add(&authenticators, "certificate", &authenticate_2, nullptr); + + bool result = wsfs_authenticators_authenticate(&authenticators, &creds); + ASSERT_FALSE(result); + + wsfs_authenticators_cleanup(&authenticators); + wsfs_credentials_cleanup(&creds); +} \ No newline at end of file