mirror of
https://github.com/falk-werner/webfused
synced 2024-10-27 20:44:08 +00:00
refactor: generalize auth_settings
This commit is contained in:
parent
609fbee24f
commit
1625869696
@ -54,7 +54,7 @@ add_library(webfused-static STATIC
|
||||
src/webfused/config/config.c
|
||||
src/webfused/config/factory.c
|
||||
src/webfused/config/builder.c
|
||||
src/webfused/config/auth_settings.c
|
||||
src/webfused/config/settings.c
|
||||
src/webfused/auth/authenticator.c
|
||||
src/webfused/auth/factory.c
|
||||
src/webfused/auth/file_authenticator.c
|
||||
@ -116,10 +116,10 @@ add_executable(alltests
|
||||
test/mock_config_builder.cc
|
||||
test/mock_logger.cc
|
||||
test/mock_credentials.cc
|
||||
test/mock_auth_settings.cc
|
||||
test/mock_settings.cc
|
||||
test/test_config_factory.cc
|
||||
test/test_config.cc
|
||||
test/test_auth_settings.cc
|
||||
test/test_settings.cc
|
||||
test/test_auth_factory.cc
|
||||
test/test_file_authenticator.cc
|
||||
test/test_mountpoint_factory.cc
|
||||
@ -137,8 +137,7 @@ target_compile_options(alltests PRIVATE ${GMOCK_CFLAGS} ${GTEST_CFLAGS} "-pthrea
|
||||
target_link_libraries(alltests PRIVATE
|
||||
-Wl,--wrap=wf_credentials_type
|
||||
-Wl,--wrap=wf_credentials_get
|
||||
-Wl,--wrap=wfd_auth_settings_get_provider
|
||||
-Wl,--wrap=wfd_auth_settings_get
|
||||
-Wl,--wrap=wfd_settings_get
|
||||
webfused-static
|
||||
userdb
|
||||
${LIBCONFIG_LIBRARIES}
|
||||
|
@ -27,5 +27,17 @@ authentication:
|
||||
|
||||
filesystems:
|
||||
(
|
||||
{name = "test", mount_point = "/tmp" }
|
||||
{name = "test", mount_point = "/tmp/webfused" }
|
||||
)
|
||||
|
||||
log:
|
||||
{
|
||||
provider: "syslog"
|
||||
level: "warning"
|
||||
settings:
|
||||
{
|
||||
ident = "webfused"
|
||||
facility = "daemon"
|
||||
log_pid = true
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,24 @@
|
||||
#include "webfused/auth/factory.h"
|
||||
#include "webfused/auth/settings.h"
|
||||
#include "webfused/auth/file_authenticator.h"
|
||||
#include "webfused/config/settings.h"
|
||||
#include "webfused/log/log.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
bool
|
||||
wfd_authenticator_create(
|
||||
struct wfd_auth_settings * settings,
|
||||
char const * provider,
|
||||
struct wfd_settings * settings,
|
||||
struct wfd_authenticator * authenticator)
|
||||
{
|
||||
bool result = false;
|
||||
char const * provider_name = wfd_auth_settings_get_provider(settings);
|
||||
if (0 == strcmp("file", provider_name))
|
||||
if (0 == strcmp("file", provider))
|
||||
{
|
||||
result = wfd_file_authenticator_create(settings, authenticator);
|
||||
}
|
||||
else
|
||||
{
|
||||
WFD_ERROR("failed to create authenticator: unknown type \"%s\"", provider_name);
|
||||
WFD_ERROR("failed to create authenticator: unknown type \"%s\"", provider);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -11,11 +11,12 @@ extern "C"
|
||||
#endif
|
||||
|
||||
struct wfd_authenticator;
|
||||
struct wfd_auth_settings;
|
||||
struct wfd_settings;
|
||||
|
||||
extern bool
|
||||
wfd_authenticator_create(
|
||||
struct wfd_auth_settings * settings,
|
||||
char const * provider,
|
||||
struct wfd_settings * settings,
|
||||
struct wfd_authenticator * authenticator);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -1,11 +1,11 @@
|
||||
#include "webfused/auth/file_authenticator.h"
|
||||
#include "webfused/auth/settings.h"
|
||||
#include "webfused/auth/authenticator.h"
|
||||
|
||||
#include "webfuse/adapter/credentials.h"
|
||||
#include "webfused/config/settings.h"
|
||||
#include "userdb/userdb.h"
|
||||
#include "webfused/log/log.h"
|
||||
|
||||
#include <webfuse/adapter/credentials.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@ -71,12 +71,12 @@ wfd_file_authenticator_vtable =
|
||||
|
||||
bool
|
||||
wfd_file_authenticator_create(
|
||||
struct wfd_auth_settings * settings,
|
||||
struct wfd_settings * settings,
|
||||
struct wfd_authenticator * authenticator)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
char const * filename = wfd_auth_settings_get(settings, "file");
|
||||
char const * filename = wfd_settings_get(settings, "file");
|
||||
if (NULL != filename)
|
||||
{
|
||||
struct wfd_file_authenticator * data = malloc(sizeof(struct wfd_file_authenticator));
|
||||
|
@ -12,11 +12,11 @@ extern "C"
|
||||
#endif
|
||||
|
||||
struct wfd_authenticator;
|
||||
struct wfd_auth_settings;
|
||||
struct wfd_settings;
|
||||
|
||||
extern bool
|
||||
wfd_file_authenticator_create(
|
||||
struct wfd_auth_settings * settings,
|
||||
struct wfd_settings * settings,
|
||||
struct wfd_authenticator * authenticator);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -1,30 +0,0 @@
|
||||
#ifndef WFD_AUTH_SETTINGS_H
|
||||
#define WFD_AUTH_SETTINGS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfd_auth_settings;
|
||||
|
||||
|
||||
extern char const *
|
||||
wfd_auth_settings_get_provider(
|
||||
struct wfd_auth_settings * settings);
|
||||
|
||||
extern char const *
|
||||
wfd_auth_settings_get(
|
||||
struct wfd_auth_settings * settings,
|
||||
char const * key);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -1,51 +0,0 @@
|
||||
#include "webfused/auth/settings.h"
|
||||
#include "webfused/config/auth_settings.h"
|
||||
|
||||
#include <libconfig.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wfd_auth_settings
|
||||
{
|
||||
char * provider_name;
|
||||
struct config_setting_t * settings;
|
||||
};
|
||||
|
||||
struct wfd_auth_settings *
|
||||
wfd_auth_settings_create(
|
||||
char const * provider_name,
|
||||
struct config_setting_t * settings)
|
||||
{
|
||||
struct wfd_auth_settings * auth_settings = malloc(sizeof(struct wfd_auth_settings));
|
||||
auth_settings->provider_name = strdup(provider_name);
|
||||
auth_settings->settings = settings;
|
||||
|
||||
return auth_settings;
|
||||
}
|
||||
|
||||
void
|
||||
wfd_auth_settings_dispose(
|
||||
struct wfd_auth_settings * settings)
|
||||
{
|
||||
free(settings->provider_name);
|
||||
free(settings);
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_auth_settings_get_provider(
|
||||
struct wfd_auth_settings * settings)
|
||||
{
|
||||
return settings->provider_name;
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_auth_settings_get(
|
||||
struct wfd_auth_settings * settings,
|
||||
char const * key)
|
||||
{
|
||||
char const * result;
|
||||
int rc = config_setting_lookup_string(settings->settings, key, &result);
|
||||
|
||||
return (CONFIG_TRUE == rc) ? result : NULL;
|
||||
}
|
||||
|
@ -1,25 +0,0 @@
|
||||
#ifndef WFD_CONFIG_AUTH_SETTINGS_H
|
||||
#define WFD_CONFIG_AUTH_SETTINGS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfd_auth_settings;
|
||||
struct config_setting_t;
|
||||
|
||||
extern struct wfd_auth_settings *
|
||||
wfd_auth_settings_create(
|
||||
char const * provider_name,
|
||||
struct config_setting_t * settings);
|
||||
|
||||
extern void
|
||||
wfd_auth_settings_dispose(
|
||||
struct wfd_auth_settings * settings);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -43,9 +43,10 @@ wfd_config_builder_set_server_document_root(
|
||||
bool
|
||||
wfd_config_builder_add_auth_provider(
|
||||
struct wfd_config_builder builder,
|
||||
struct wfd_auth_settings * settings)
|
||||
char const * provider,
|
||||
struct wfd_settings * settings)
|
||||
{
|
||||
return builder.vtable->add_auth_provider(builder.data, settings);
|
||||
return builder.vtable->add_auth_provider(builder.data, provider, settings);
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -10,7 +10,7 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfd_auth_settings;
|
||||
struct wfd_settings;
|
||||
|
||||
typedef void
|
||||
wfd_config_builder_set_server_vhostname_fn(
|
||||
@ -40,7 +40,8 @@ wfd_config_builder_set_server_document_root_fn(
|
||||
typedef bool
|
||||
wfd_config_builder_add_auth_provider_fn(
|
||||
void * data,
|
||||
struct wfd_auth_settings * settings);
|
||||
char const * provider,
|
||||
struct wfd_settings * settings);
|
||||
|
||||
typedef bool
|
||||
wfd_config_builder_add_filesystem_fn(
|
||||
@ -93,7 +94,8 @@ wfd_config_builder_set_server_document_root(
|
||||
extern bool
|
||||
wfd_config_builder_add_auth_provider(
|
||||
struct wfd_config_builder builder,
|
||||
struct wfd_auth_settings * settings);
|
||||
char const * provider,
|
||||
struct wfd_settings * settings);
|
||||
|
||||
extern bool
|
||||
wfd_config_builder_add_filesystem(
|
||||
|
@ -65,14 +65,15 @@ wfd_config_set_server_document_root(
|
||||
static bool
|
||||
wfd_config_add_auth_provider(
|
||||
void * data,
|
||||
struct wfd_auth_settings * settings)
|
||||
char const * provider,
|
||||
struct wfd_settings * settings)
|
||||
{
|
||||
bool result = false;
|
||||
struct wfd_config * config = data;
|
||||
|
||||
if (!config->has_authenticator)
|
||||
{
|
||||
result = wfd_authenticator_create(settings, &config->authenticator);
|
||||
result = wfd_authenticator_create(provider, settings, &config->authenticator);
|
||||
if (result)
|
||||
{
|
||||
wf_server_config_add_authenticator(
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "webfused/config/factory.h"
|
||||
#include "webfused/config/auth_settings.h"
|
||||
#include "webfused/config/settings_intern.h"
|
||||
#include "webfused/log/log.h"
|
||||
|
||||
#include <libconfig.h>
|
||||
@ -131,13 +131,12 @@ wfd_config_read_authentication(
|
||||
|
||||
if (result)
|
||||
{
|
||||
struct wfd_auth_settings * auth_settings = wfd_auth_settings_create(
|
||||
provider_name, settings);
|
||||
struct wfd_settings auth_settings;
|
||||
wfd_settings_init(&auth_settings, settings);
|
||||
|
||||
result = wfd_config_builder_add_auth_provider(builder, auth_settings);
|
||||
wfd_auth_settings_dispose(auth_settings);
|
||||
result = wfd_config_builder_add_auth_provider(builder, provider_name, &auth_settings);
|
||||
wfd_settings_cleanup(&auth_settings);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
32
src/webfused/config/settings.c
Normal file
32
src/webfused/config/settings.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include "webfused/config/settings.h"
|
||||
#include "webfused/config/settings_intern.h"
|
||||
|
||||
#include <libconfig.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void
|
||||
wfd_settings_init(
|
||||
struct wfd_settings * settings,
|
||||
struct config_setting_t * setting)
|
||||
{
|
||||
settings->setting = setting;
|
||||
}
|
||||
|
||||
void
|
||||
wfd_settings_cleanup(
|
||||
struct wfd_settings * settings)
|
||||
{
|
||||
settings->setting = NULL;
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_settings_get(
|
||||
struct wfd_settings * settings,
|
||||
char const * key)
|
||||
{
|
||||
char const * result;
|
||||
int rc = config_setting_lookup_string(settings->setting, key, &result);
|
||||
|
||||
return (CONFIG_TRUE == rc) ? result : NULL;
|
||||
}
|
||||
|
22
src/webfused/config/settings.h
Normal file
22
src/webfused/config/settings.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef WFD_CONFIG_SETTINGS_H
|
||||
#define WFD_CONFIG_SETTINGS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfd_settings;
|
||||
|
||||
|
||||
extern char const *
|
||||
wfd_settings_get(
|
||||
struct wfd_settings * settings,
|
||||
char const * key);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
30
src/webfused/config/settings_intern.h
Normal file
30
src/webfused/config/settings_intern.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef WFD_CONFIG_SETTINGS_INTERN_H
|
||||
#define WFD_CONFIG_SETTINGS_INTERN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct config_setting_t;
|
||||
|
||||
struct wfd_settings
|
||||
{
|
||||
struct config_setting_t * setting;
|
||||
};
|
||||
|
||||
|
||||
extern void
|
||||
wfd_settings_init(
|
||||
struct wfd_settings * settings,
|
||||
struct config_setting_t * setting);
|
||||
|
||||
extern void
|
||||
wfd_settings_cleanup(
|
||||
struct wfd_settings * settings);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,63 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
#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
|
@ -52,10 +52,11 @@ wfd_MockConfigBuilder_set_server_document_root(
|
||||
static bool
|
||||
wfd_MockConfigBuilder_add_auth_provider(
|
||||
void * data,
|
||||
struct wfd_auth_settings * settings)
|
||||
char const * provider,
|
||||
struct wfd_settings * settings)
|
||||
{
|
||||
auto * builder = reinterpret_cast<IConfigBuilder*>(data);
|
||||
return builder->addAuthProvider(settings);
|
||||
return builder->addAuthProvider(provider, settings);
|
||||
}
|
||||
|
||||
static bool
|
||||
|
@ -16,7 +16,7 @@ public:
|
||||
virtual void setServerKey(char const * key_path) = 0;
|
||||
virtual void setServerCert(char const * cert_path) = 0;
|
||||
virtual void setServerDocumentRoot(char const * document_root) = 0;
|
||||
virtual bool addAuthProvider(wfd_auth_settings * settings) = 0;
|
||||
virtual bool addAuthProvider(char const * provider, wfd_settings * settings) = 0;
|
||||
virtual bool addFilesystem(char const * name, char const * mountpoint) = 0;
|
||||
};
|
||||
|
||||
@ -29,7 +29,7 @@ public:
|
||||
MOCK_METHOD1(setServerKey, void (char const * key_path));
|
||||
MOCK_METHOD1(setServerCert, void (char const * cert_path));
|
||||
MOCK_METHOD1(setServerDocumentRoot, void (char const * document_root));
|
||||
MOCK_METHOD1(addAuthProvider, bool (wfd_auth_settings * settings));
|
||||
MOCK_METHOD2(addAuthProvider, bool (char const * provider, wfd_settings * settings));
|
||||
MOCK_METHOD2(addFilesystem, bool (char const * name, char const * mountpoint));
|
||||
|
||||
struct wfd_config_builder getBuilder();
|
||||
|
45
test/mock_settings.cc
Normal file
45
test/mock_settings.cc
Normal file
@ -0,0 +1,45 @@
|
||||
#include "mock_settings.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
using webfused_test::ISettings;
|
||||
|
||||
static ISettings * wfd_mock_settings = nullptr;
|
||||
|
||||
extern char const *
|
||||
__real_wfd_settings_get(
|
||||
struct wfd_settings * settings,
|
||||
char const * key);
|
||||
|
||||
char const *
|
||||
__wrap_wfd_settings_get(
|
||||
struct wfd_settings * settings,
|
||||
char const * key)
|
||||
{
|
||||
if (nullptr == wfd_mock_settings)
|
||||
{
|
||||
return __real_wfd_settings_get(settings, key);
|
||||
}
|
||||
else
|
||||
{
|
||||
return wfd_mock_settings->get(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
namespace webfused_test
|
||||
{
|
||||
|
||||
MockSettings::MockSettings()
|
||||
{
|
||||
wfd_mock_settings = this;
|
||||
}
|
||||
|
||||
MockSettings::~MockSettings()
|
||||
{
|
||||
wfd_mock_settings = nullptr;
|
||||
}
|
||||
|
||||
}
|
29
test/mock_settings.hpp
Normal file
29
test/mock_settings.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef WFD_MOCK_AUTH_SETTINGS_HPP
|
||||
#define WFD_MOCK_AUTH_SETTINGS_HPP
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "webfused/config/settings.h"
|
||||
|
||||
namespace webfused_test
|
||||
{
|
||||
|
||||
class ISettings
|
||||
{
|
||||
public:
|
||||
virtual ~ISettings() = default;
|
||||
virtual char const * get(char const * key) = 0;
|
||||
};
|
||||
|
||||
class MockSettings: public ISettings
|
||||
{
|
||||
public:
|
||||
MockSettings();
|
||||
~MockSettings() override;
|
||||
MOCK_METHOD1(get, char const * (char const * key));
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -1,7 +1,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfused/auth/factory.h"
|
||||
#include "webfused/auth/authenticator.h"
|
||||
#include "webfused/config/auth_settings.h"
|
||||
#include "webfused/log/log.h"
|
||||
#include "mock_logger.hpp"
|
||||
|
||||
@ -15,12 +14,7 @@ TEST(auth_factory, fail_unknown_provider)
|
||||
EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1);
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
wfd_auth_settings * auth_settings = wfd_auth_settings_create(
|
||||
"unknown", NULL);
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool result = wfd_authenticator_create(auth_settings, &authenticator);
|
||||
bool result = wfd_authenticator_create("unknown", NULL, &authenticator);
|
||||
ASSERT_FALSE(result);
|
||||
|
||||
wfd_auth_settings_dispose(auth_settings);
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfused/auth/settings.h"
|
||||
#include "webfused/config/auth_settings.h"
|
||||
#include <libconfig.h>
|
||||
|
||||
TEST(auth_settings, auth_settings)
|
||||
{
|
||||
char const settings_text[] =
|
||||
"settings:\n"
|
||||
"{\n"
|
||||
" string_value = \"some.string\"\n"
|
||||
" int_value = 42\n"
|
||||
"}\n"
|
||||
;
|
||||
config_t config;
|
||||
config_init(&config);
|
||||
int rc = config_read_string(&config, settings_text);
|
||||
ASSERT_TRUE(CONFIG_TRUE == rc);
|
||||
|
||||
config_setting_t * settings = config_lookup(&config, "settings");
|
||||
ASSERT_NE(nullptr, settings);
|
||||
|
||||
wfd_auth_settings * auth_settings = wfd_auth_settings_create("test_provider", settings);
|
||||
ASSERT_STREQ("test_provider", wfd_auth_settings_get_provider(auth_settings));
|
||||
ASSERT_STREQ("some.string", wfd_auth_settings_get(auth_settings, "string_value"));
|
||||
ASSERT_EQ(nullptr, wfd_auth_settings_get(auth_settings, "int_value"));
|
||||
ASSERT_EQ(nullptr, wfd_auth_settings_get(auth_settings, "invalid_value"));
|
||||
|
||||
wfd_auth_settings_dispose(auth_settings);
|
||||
config_destroy(&config);
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfused/config/config.h"
|
||||
#include "mock_auth_settings.hpp"
|
||||
#include "mock_settings.hpp"
|
||||
|
||||
#include "webfused/log/logger.h"
|
||||
#include "webfused/log/log.h"
|
||||
@ -8,7 +8,7 @@
|
||||
using ::webfused_test::MockLogger;
|
||||
using ::testing::_;
|
||||
|
||||
using ::webfused_test::MockAuthSettings;
|
||||
using ::webfused_test::MockSettings;
|
||||
using ::testing::Return;
|
||||
using ::testing::StrEq;
|
||||
|
||||
@ -37,11 +37,10 @@ TEST(config, auth_config)
|
||||
|
||||
wfd_config_builder builder = wfd_config_get_builder(config);
|
||||
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("/any/path"));
|
||||
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, nullptr);
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, "file", nullptr);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
wfd_config_dispose(config);
|
||||
@ -54,14 +53,13 @@ TEST(config, auth_config_failed_to_add_second_provider)
|
||||
|
||||
wfd_config_builder builder = wfd_config_get_builder(config);
|
||||
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("/any/path"));
|
||||
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, nullptr);
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, "file", nullptr);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
success = wfd_config_builder_add_auth_provider(builder, nullptr);
|
||||
success = wfd_config_builder_add_auth_provider(builder, "file", nullptr);
|
||||
ASSERT_FALSE(success);
|
||||
|
||||
wfd_config_dispose(config);
|
||||
@ -74,10 +72,7 @@ TEST(config, auth_config_failed_to_add_unknown_provider)
|
||||
|
||||
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);
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, "unknown", nullptr);
|
||||
ASSERT_FALSE(success);
|
||||
|
||||
wfd_config_dispose(config);
|
||||
|
@ -19,7 +19,7 @@ TEST(config, is_loadable)
|
||||
StrictMock<MockConfigBuilder> builder;
|
||||
EXPECT_CALL(builder, setServerVhostname(StrEq("localhost"))).Times(1);
|
||||
EXPECT_CALL(builder, setServerPort(8080)).Times(1);
|
||||
EXPECT_CALL(builder, addAuthProvider(_)).Times(1).WillOnce(Return(true));
|
||||
EXPECT_CALL(builder, addAuthProvider(_, _)).Times(1).WillOnce(Return(true));
|
||||
EXPECT_CALL(builder, addFilesystem(_,_)).Times(1).WillOnce(Return(true));
|
||||
|
||||
bool result = wfd_config_load_file(builder.getBuilder(), "webfused.conf");
|
||||
@ -262,7 +262,7 @@ TEST(config, authentication)
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
StrictMock<MockConfigBuilder> builder;
|
||||
EXPECT_CALL(builder, addAuthProvider(_)).Times(1).WillOnce(Return(true));
|
||||
EXPECT_CALL(builder, addAuthProvider(_, _)).Times(1).WillOnce(Return(true));
|
||||
|
||||
char const config_text[] =
|
||||
"version = { major = 1, minor = 0 }\n"
|
||||
@ -283,7 +283,7 @@ TEST(config, failed_create_authenticator)
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
StrictMock<MockConfigBuilder> builder;
|
||||
EXPECT_CALL(builder, addAuthProvider(_)).Times(1).WillOnce(Return(false));
|
||||
EXPECT_CALL(builder, addAuthProvider(_, _)).Times(1).WillOnce(Return(false));
|
||||
|
||||
char const config_text[] =
|
||||
"version = { major = 1, minor = 0 }\n"
|
||||
|
@ -1,22 +1,22 @@
|
||||
#include "webfused/auth/file_authenticator.h"
|
||||
#include "webfused/auth/authenticator.h"
|
||||
#include "webfused/config/auth_settings.h"
|
||||
#include "webfused/config/settings.h"
|
||||
#include "webfused/auth/factory.h"
|
||||
|
||||
#include "mock_credentials.hpp"
|
||||
#include "mock_auth_settings.hpp"
|
||||
#include "mock_settings.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <libconfig.h>
|
||||
|
||||
using ::webfused_test::MockAuthSettings;
|
||||
using ::webfused_test::MockSettings;
|
||||
using ::webfused_test::MockCredentials;
|
||||
using ::testing::Return;
|
||||
using ::testing::StrEq;
|
||||
|
||||
TEST(file_authenticator, create)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
@ -28,7 +28,7 @@ TEST(file_authenticator, create)
|
||||
|
||||
TEST(file_authenticator, create_fail_missing_file)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return(nullptr));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
@ -38,12 +38,11 @@ TEST(file_authenticator, create_fail_missing_file)
|
||||
|
||||
TEST(file_authenticator, create_via_factory)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_authenticator_create("file", nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
wfd_authenticator_dispose(authenticator);
|
||||
@ -51,12 +50,11 @@ TEST(file_authenticator, create_via_factory)
|
||||
|
||||
TEST(file_authenticator, authenticate)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
MockCredentials creds;
|
||||
@ -71,12 +69,11 @@ TEST(file_authenticator, authenticate)
|
||||
|
||||
TEST(file_authenticator, authenticate_fail_wrong_passwd)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
MockCredentials creds;
|
||||
@ -91,12 +88,11 @@ TEST(file_authenticator, authenticate_fail_wrong_passwd)
|
||||
|
||||
TEST(file_authenticator, authenticate_fail_no_passwd_file)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("unknown_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
MockCredentials creds;
|
||||
@ -111,12 +107,11 @@ TEST(file_authenticator, authenticate_fail_no_passwd_file)
|
||||
|
||||
TEST(file_authenticator, authenticate_fail_missing_username)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
MockCredentials creds;
|
||||
@ -131,12 +126,11 @@ TEST(file_authenticator, authenticate_fail_missing_username)
|
||||
|
||||
TEST(file_authenticator, authenticate_fail_missing_password)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
MockCredentials creds;
|
||||
@ -151,7 +145,7 @@ TEST(file_authenticator, authenticate_fail_missing_password)
|
||||
|
||||
TEST(file_authenticator, get_type)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("/any/path"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
|
31
test/test_settings.cc
Normal file
31
test/test_settings.cc
Normal file
@ -0,0 +1,31 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfused/config/settings_intern.h"
|
||||
#include "webfused/config/settings.h"
|
||||
#include <libconfig.h>
|
||||
|
||||
TEST(settings, settings)
|
||||
{
|
||||
char const settings_text[] =
|
||||
"settings:\n"
|
||||
"{\n"
|
||||
" string_value = \"some.string\"\n"
|
||||
" int_value = 42\n"
|
||||
"}\n"
|
||||
;
|
||||
config_t config;
|
||||
config_init(&config);
|
||||
int rc = config_read_string(&config, settings_text);
|
||||
ASSERT_TRUE(CONFIG_TRUE == rc);
|
||||
|
||||
config_setting_t * setting = config_lookup(&config, "settings");
|
||||
ASSERT_NE(nullptr, setting);
|
||||
|
||||
wfd_settings settings;
|
||||
wfd_settings_init(&settings, setting);
|
||||
ASSERT_STREQ("some.string", wfd_settings_get(&settings, "string_value"));
|
||||
ASSERT_EQ(nullptr, wfd_settings_get(&settings, "int_value"));
|
||||
ASSERT_EQ(nullptr, wfd_settings_get(&settings, "invalid_value"));
|
||||
|
||||
wfd_settings_cleanup(&settings);
|
||||
config_destroy(&config);
|
||||
}
|
Loading…
Reference in New Issue
Block a user