1
0
mirror of https://github.com/falk-werner/webfuse synced 2025-06-13 12:54:15 +00:00

added unit tests for authenticators

This commit is contained in:
Falk Werner 2019-03-17 15:31:26 +01:00
parent fca001113f
commit ec74b4a803
7 changed files with 291 additions and 12 deletions

View File

@ -186,6 +186,7 @@ endif(NOT WITHOUT_EXAMPLE)
if(NOT WITHOUT_TESTS) if(NOT WITHOUT_TESTS)
pkg_check_modules(GTEST gtest_main) pkg_check_modules(GTEST gtest_main)
pkg_check_modules(GMOCK gmock)
add_library(wsfs-adapter-static STATIC ${WSFS_ADAPTER_SOURCES}) add_library(wsfs-adapter-static STATIC ${WSFS_ADAPTER_SOURCES})
set_target_properties(wsfs-adapter-static PROPERTIES OUTPUT_NAME wsfs-adapter) 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 add_executable(alltests
test/msleep.cc test/msleep.cc
test/mock_authenticator.cc
test/test_response_parser.cc test/test_response_parser.cc
test/test_server.cc test/test_server.cc
test/test_timepoint.cc test/test_timepoint.cc
test/test_timer.cc test/test_timer.cc
test/test_url.cc test/test_url.cc
test/test_credentials.cc test/test_credentials.cc
test/test_authenticator.cc
test/test_authenticators.cc
${WSFS_COMMON_SOURCES} ${WSFS_COMMON_SOURCES}
) )
target_link_libraries(alltests PUBLIC wsfs-adapter-static wsfs-provider-static ${EXTRA_LIBS} ${GTEST_LIBRARIES}) 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} ${GTEST_INCLUDE_DIRS}) target_include_directories(alltests PUBLIC lib ${EXTRA_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS})
target_compile_options(alltests PUBLIC ${EXTRA_CFLAGS} ${GTEST_CFLAGS}) target_compile_options(alltests PUBLIC ${EXTRA_CFLAGS} ${GMOCK_CFLAGS} ${GTEST_CFLAGS})
enable_testing() enable_testing()
add_test(alltests alltests) add_test(alltests alltests)

View File

@ -78,7 +78,7 @@ bool wsfs_authenticators_authenticate(
struct wsfs_authenticators * authenticators, struct wsfs_authenticators * authenticators,
struct wsfs_credentials * credentials) struct wsfs_credentials * credentials)
{ {
bool result; bool result = (NULL == authenticators->first);
if (NULL != credentials) if (NULL != credentials)
{ {
@ -87,14 +87,6 @@ bool wsfs_authenticators_authenticate(
{ {
result = wsfs_authenticator_autenticate(authenticator, credentials); result = wsfs_authenticator_autenticate(authenticator, credentials);
} }
else
{
result = false;
}
}
else
{
result = (NULL == authenticators->first);
} }
return result; return result;

View File

@ -15,6 +15,11 @@ struct wsfs_authenticators
struct wsfs_authenticator * first; struct wsfs_authenticator * first;
}; };
#ifdef __cplusplus
extern "C"
{
#endif
extern void wsfs_authenticators_init( extern void wsfs_authenticators_init(
struct wsfs_authenticators * authenticators); struct wsfs_authenticators * authenticators);
@ -35,4 +40,8 @@ extern bool wsfs_authenticators_authenticate(
struct wsfs_authenticators * authenticators, struct wsfs_authenticators * authenticators,
struct wsfs_credentials * credentials); struct wsfs_credentials * credentials);
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -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);
}
}

View File

@ -0,0 +1,34 @@
#ifndef MOCK_AUTHENTICATOR_H
#define MOCK_AUTHENTICATOR_H
#include <gmock/gmock.h>
#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

View File

@ -0,0 +1,63 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#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<void*>(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);
}

135
test/test_authenticators.cc Normal file
View File

@ -0,0 +1,135 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#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);
}