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

added implementation of file_authenticator

This commit is contained in:
Falk Werner
2020-03-17 13:17:33 +01:00
parent 5dadb6240d
commit 5e90853ecf
8 changed files with 408 additions and 1 deletions

64
test/mock_credentials.cc Normal file
View File

@@ -0,0 +1,64 @@
#include "mock_credentials.hpp"
extern "C"
{
using webfused_test::ICredentials;
static ICredentials * wfd_mock_credentials = nullptr;
extern char const *
__real_wf_credentials_type(
struct wf_credentials const * credentials);
char const *
__wrap_wf_credentials_type(
struct wf_credentials const * credentials)
{
if (nullptr == wfd_mock_credentials)
{
return __real_wf_credentials_type(credentials);
}
else
{
return wfd_mock_credentials->type();
}
}
extern char const * __real_wf_credentials_get(
struct wf_credentials const * credentials,
char const * key);
char const * __wrap_wf_credentials_get(
struct wf_credentials const * credentials,
char const * key)
{
if (nullptr == wfd_mock_credentials)
{
return __real_wf_credentials_get(credentials, key);
}
else
{
return wfd_mock_credentials->get(key);
}
}
}
namespace webfused_test
{
MockCredentials::MockCredentials()
{
wfd_mock_credentials = this;
}
MockCredentials::~MockCredentials()
{
wfd_mock_credentials = nullptr;
}
}

29
test/mock_credentials.hpp Normal file
View File

@@ -0,0 +1,29 @@
#ifndef WFD_MOCK_CREDENTIALS_HPP
#define WFD_MOCK_CREDENTIALS_HPP
#include "gmock/gmock.h"
#include "webfuse/adapter/credentials.h"
namespace webfused_test
{
class ICredentials
{
public:
virtual ~ICredentials() = default;
virtual char const * type() = 0;
virtual char const * get(char const * key) = 0;
};
class MockCredentials: public ICredentials
{
public:
MockCredentials();
virtual ~MockCredentials();
MOCK_METHOD0(type, char const*());
MOCK_METHOD1(get, char const *(char const * key));
};
}
#endif

View File

@@ -0,0 +1,211 @@
#include "webfused/auth/file_authenticator.h"
#include "webfused/auth/authenticator.h"
#include "webfused/config/auth_settings.h"
#include "webfused/auth/factory.h"
#include "mock_credentials.hpp"
#include <gtest/gtest.h>
#include <libconfig.h>
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);
wfd_authenticator authenticator;
bool success = wfd_file_authenticator_create(auth_settings, &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);
wfd_authenticator authenticator;
bool success = wfd_file_authenticator_create(auth_settings, &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);
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &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);
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
EXPECT_CALL(creds, get(StrEq("username"))).Times(1).WillOnce(Return("bob"));
EXPECT_CALL(creds, get(StrEq("password"))).Times(1).WillOnce(Return("secret"));
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);
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
EXPECT_CALL(creds, get(StrEq("username"))).Times(1).WillOnce(Return("bob"));
EXPECT_CALL(creds, get(StrEq("password"))).Times(1).WillOnce(Return("unkown"));
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);
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
EXPECT_CALL(creds, get(StrEq("username"))).Times(1).WillOnce(Return("bob"));
EXPECT_CALL(creds, get(StrEq("password"))).Times(1).WillOnce(Return("secred"));
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);
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
EXPECT_CALL(creds, get(StrEq("username"))).Times(1).WillOnce(Return(nullptr));
EXPECT_CALL(creds, get(StrEq("password"))).Times(1).WillOnce(Return("unkown"));
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);
wfd_authenticator authenticator;
bool success = wfd_authenticator_create(auth_settings, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
EXPECT_CALL(creds, get(StrEq("username"))).Times(1).WillOnce(Return("bob"));
EXPECT_CALL(creds, get(StrEq("password"))).Times(1).WillOnce(Return(nullptr));
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);
}

14
test/test_passwd.json Normal file
View File

@@ -0,0 +1,14 @@
{
"meta": {
"type": "wf-userdb",
"major": 1,
"minor": 0,
"hash_algorithm": "sha512"
},
"users": {
"bob": {
"password_hash": "e51e27ce47054feead3d83068d47f2a07307d4877ac67da668ef43e0e466fe8c7b66651af14fdb8d48c51592ef5afa0c63f874d20861c6b9ef8e6513bfcaa330",
"salt": "b3be6979921edecfea88c50d0d1ec40b7f8c383831b2276c65969ead18e47c03"
}
}
}