1
0
mirror of https://github.com/falk-werner/webfused synced 2026-03-02 04:09:19 +00:00

add authenticator to server config

This commit is contained in:
Falk Werner
2020-03-17 16:49:17 +01:00
parent d90afed1b2
commit 7e7cbd5d42
9 changed files with 268 additions and 96 deletions

View File

@@ -0,0 +1,63 @@
#include "mock_auth_settings.hpp"
extern "C"
{
using webfused_test::IAuthSettings;
static IAuthSettings * wfd_mock_auth_settings = nullptr;
extern char const *
__real_wfd_auth_settings_get_provider(
struct wfd_auth_settings * settings);
extern char const *
__real_wfd_auth_settings_get(
struct wfd_auth_settings * settings,
char const * key);
char const *
__wrap_wfd_auth_settings_get_provider(
struct wfd_auth_settings * settings)
{
if (nullptr == wfd_mock_auth_settings)
{
return __real_wfd_auth_settings_get_provider(settings);
}
else
{
return wfd_mock_auth_settings->getProvider();
}
}
char const *
__wrap_wfd_auth_settings_get(
struct wfd_auth_settings * settings,
char const * key)
{
if (nullptr == wfd_mock_auth_settings)
{
return __real_wfd_auth_settings_get(settings, key);
}
else
{
return wfd_mock_auth_settings->get(key);
}
}
}
namespace webfused_test
{
MockAuthSettings::MockAuthSettings()
{
wfd_mock_auth_settings = this;
}
MockAuthSettings::~MockAuthSettings()
{
wfd_mock_auth_settings = nullptr;
}
}

View File

@@ -0,0 +1,31 @@
#ifndef WFD_MOCK_AUTH_SETTINGS_HPP
#define WFD_MOCK_AUTH_SETTINGS_HPP
#include <gmock/gmock.h>
#include "webfused/auth/settings.h"
namespace webfused_test
{
class IAuthSettings
{
public:
virtual ~IAuthSettings() = default;
virtual char const * getProvider() = 0;
virtual char const * get(char const * key) = 0;
};
class MockAuthSettings: public IAuthSettings
{
public:
MockAuthSettings();
~MockAuthSettings() override;
MOCK_METHOD0(getProvider, char const * ());
MOCK_METHOD1(get, char const * (char const * key));
};
}
#endif

View File

@@ -1,5 +1,10 @@
#include <gtest/gtest.h>
#include "webfused/config/config.h"
#include "mock_auth_settings.hpp"
using ::webfused_test::MockAuthSettings;
using ::testing::Return;
using ::testing::StrEq;
TEST(config, server_config)
{
@@ -16,5 +21,58 @@ TEST(config, server_config)
wf_server_config * server_config = wfd_config_get_server_config(config);
ASSERT_NE(nullptr, server_config);
wfd_config_dispose(config);
}
TEST(config, auth_config)
{
wfd_config * config = wfd_config_create();
ASSERT_NE(nullptr, config);
wfd_config_builder builder = wfd_config_get_builder(config);
MockAuthSettings settings;
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("/any/path"));
bool success = wfd_config_builder_add_auth_provider(builder, nullptr);
ASSERT_TRUE(success);
wfd_config_dispose(config);
}
TEST(config, auth_config_failed_to_add_second_provider)
{
wfd_config * config = wfd_config_create();
ASSERT_NE(nullptr, config);
wfd_config_builder builder = wfd_config_get_builder(config);
MockAuthSettings settings;
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("/any/path"));
bool success = wfd_config_builder_add_auth_provider(builder, nullptr);
ASSERT_TRUE(success);
success = wfd_config_builder_add_auth_provider(builder, nullptr);
ASSERT_FALSE(success);
wfd_config_dispose(config);
}
TEST(config, auth_config_failed_to_add_unknown_provider)
{
wfd_config * config = wfd_config_create();
ASSERT_NE(nullptr, config);
wfd_config_builder builder = wfd_config_get_builder(config);
MockAuthSettings settings;
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("unknown"));
bool success = wfd_config_builder_add_auth_provider(builder, nullptr);
ASSERT_FALSE(success);
wfd_config_dispose(config);
}

View File

@@ -4,86 +4,59 @@
#include "webfused/auth/factory.h"
#include "mock_credentials.hpp"
#include "mock_auth_settings.hpp"
#include <gtest/gtest.h>
#include <libconfig.h>
using ::webfused_test::MockAuthSettings;
using ::webfused_test::MockCredentials;
using ::testing::Return;
using ::testing::StrEq;
TEST(file_authenticator, create)
{
char const config_text[] =
"file = \"/tmp/webfuse_passwd.json\"\n"
;
config_t config;
config_init(&config);
config_read_string(&config, config_text);
config_setting_t * settings = config_root_setting(&config);
wfd_auth_settings * auth_settings = wfd_auth_settings_create("file", settings);
MockAuthSettings settings;
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
bool success = wfd_file_authenticator_create(auth_settings, &authenticator);
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
wfd_auth_settings_dispose(auth_settings);
wfd_authenticator_dispose(authenticator);
config_destroy(&config);
}
TEST(file_authenticator, create_fail_missing_file)
{
config_t config;
config_init(&config);
config_setting_t * settings = config_root_setting(&config);
wfd_auth_settings * auth_settings = wfd_auth_settings_create("file", settings);
MockAuthSettings settings;
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return(nullptr));
wfd_authenticator authenticator;
bool success = wfd_file_authenticator_create(auth_settings, &authenticator);
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_FALSE(success);
wfd_auth_settings_dispose(auth_settings);
config_destroy(&config);
}
TEST(file_authenticator, create_via_factory)
{
char const config_text[] =
"file = \"/tmp/webfuse_passwd.json\"\n"
;
config_t config;
config_init(&config);
config_read_string(&config, config_text);
config_setting_t * settings = config_root_setting(&config);
wfd_auth_settings * auth_settings = wfd_auth_settings_create("file", settings);
MockAuthSettings settings;
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
bool success = wfd_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
wfd_auth_settings_dispose(auth_settings);
wfd_authenticator_dispose(authenticator);
config_destroy(&config);
}
TEST(file_authenticator, authenticate)
{
char const config_text[] =
"file = \"test_passwd.json\"\n"
;
config_t config;
config_init(&config);
config_read_string(&config, config_text);
config_setting_t * settings = config_root_setting(&config);
wfd_auth_settings * auth_settings = wfd_auth_settings_create("file", settings);
MockAuthSettings settings;
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
bool success = wfd_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
@@ -93,25 +66,17 @@ TEST(file_authenticator, authenticate)
bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr);
ASSERT_TRUE(is_authenticated);
wfd_auth_settings_dispose(auth_settings);
wfd_authenticator_dispose(authenticator);
config_destroy(&config);
}
TEST(file_authenticator, authenticate_fail_wrong_passwd)
{
char const config_text[] =
"file = \"test_passwd.json\"\n"
;
config_t config;
config_init(&config);
config_read_string(&config, config_text);
config_setting_t * settings = config_root_setting(&config);
wfd_auth_settings * auth_settings = wfd_auth_settings_create("file", settings);
MockAuthSettings settings;
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
bool success = wfd_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
@@ -121,25 +86,17 @@ TEST(file_authenticator, authenticate_fail_wrong_passwd)
bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr);
ASSERT_FALSE(is_authenticated);
wfd_auth_settings_dispose(auth_settings);
wfd_authenticator_dispose(authenticator);
config_destroy(&config);
}
TEST(file_authenticator, authenticate_fail_no_passwd_file)
{
char const config_text[] =
"file = \"non_existing_passwd.json\"\n"
;
config_t config;
config_init(&config);
config_read_string(&config, config_text);
config_setting_t * settings = config_root_setting(&config);
wfd_auth_settings * auth_settings = wfd_auth_settings_create("file", settings);
MockAuthSettings settings;
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("unknown_passwd.json"));
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
bool success = wfd_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
@@ -149,25 +106,17 @@ TEST(file_authenticator, authenticate_fail_no_passwd_file)
bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr);
ASSERT_FALSE(is_authenticated);
wfd_auth_settings_dispose(auth_settings);
wfd_authenticator_dispose(authenticator);
config_destroy(&config);
}
TEST(file_authenticator, authenticate_fail_missing_username)
{
char const config_text[] =
"file = \"test_passwd.json\"\n"
;
config_t config;
config_init(&config);
config_read_string(&config, config_text);
config_setting_t * settings = config_root_setting(&config);
wfd_auth_settings * auth_settings = wfd_auth_settings_create("file", settings);
MockAuthSettings settings;
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
bool success = wfd_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
@@ -177,25 +126,17 @@ TEST(file_authenticator, authenticate_fail_missing_username)
bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr);
ASSERT_FALSE(is_authenticated);
wfd_auth_settings_dispose(auth_settings);
wfd_authenticator_dispose(authenticator);
config_destroy(&config);
}
TEST(file_authenticator, authenticate_fail_missing_password)
{
char const config_text[] =
"file = \"test_passwd.json\"\n"
;
config_t config;
config_init(&config);
config_read_string(&config, config_text);
config_setting_t * settings = config_root_setting(&config);
wfd_auth_settings * auth_settings = wfd_auth_settings_create("file", settings);
MockAuthSettings settings;
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
bool success = wfd_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
@@ -205,7 +146,19 @@ TEST(file_authenticator, authenticate_fail_missing_password)
bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr);
ASSERT_FALSE(is_authenticated);
wfd_auth_settings_dispose(auth_settings);
wfd_authenticator_dispose(authenticator);
config_destroy(&config);
}
}
TEST(file_authenticator, get_type)
{
MockAuthSettings settings;
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("/any/path"));
wfd_authenticator authenticator;
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
ASSERT_STREQ("username", wfd_authenticator_get_type(authenticator));
wfd_authenticator_dispose(authenticator);
}