From 5145129c1860be2abf5361ac4a8dd48949170f30 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 09:48:59 +0100 Subject: [PATCH 01/13] fix: let valgrind fail on memcheck error --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 34cda4f..392bb86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -197,7 +197,7 @@ add_custom_target(check add_dependencies(check alltests) add_custom_target(memcheck - valgrind ./alltests) + valgrind --leak-check=full --error-exitcode=1 ./alltests) add_dependencies(memcheck alltests) add_custom_target(coverage From 7553846629f5351579c1f067bafc25f129349e81 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 09:52:56 +0100 Subject: [PATCH 02/13] fixed memory leak in test --- test/test_pam_authenticator.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_pam_authenticator.cc b/test/test_pam_authenticator.cc index 6f9c4a4..2bbeff0 100644 --- a/test/test_pam_authenticator.cc +++ b/test/test_pam_authenticator.cc @@ -120,6 +120,8 @@ int valid_conversation( int rc = conv->conv(2, messages, &responses, conv->appdata_ptr); if (PAM_SUCCESS == rc) { + free(responses[0].resp); + free(responses[1].resp); free(responses); } From ba270f1d0d7aabbcef26e43244af90d85e737cf1 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 10:26:41 +0100 Subject: [PATCH 03/13] increased test coverage --- CMakeLists.txt | 12 +++ src/webfused/change_user.c | 2 +- src/webfused/config/factory.c | 24 +++-- src/webfused/mountpoint_factory.c | 6 +- test/mock_libconfig.cc | 27 ++++++ test/mock_libconfig.hpp | 32 +++++++ test/mock_linux.cc | 31 ++++++ test/mock_linux.hpp | 41 ++++++++ test/test_change_user.cc | 154 ++++++++++++++++++++++++++++++ test/test_config_factory.cc | 55 ++++++++++- test/wrap_utils.hpp | 46 +++++++++ 11 files changed, 414 insertions(+), 16 deletions(-) create mode 100644 test/mock_libconfig.cc create mode 100644 test/mock_libconfig.hpp create mode 100644 test/mock_linux.cc create mode 100644 test/mock_linux.hpp create mode 100644 test/test_change_user.cc create mode 100644 test/wrap_utils.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 392bb86..505b695 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,6 +124,8 @@ add_executable(alltests test/mock_credentials.cc test/mock_settings.cc test/mock_pam.cc + test/mock_libconfig.cc + test/mock_linux.cc test/test_config_factory.cc test/test_config.cc test/test_settings.cc @@ -136,6 +138,7 @@ add_executable(alltests test/test_stderr_logger.cc test/test_syslog_logger.cc test/test_daemon.cc + test/test_change_user.cc ) target_include_directories(alltests PRIVATE @@ -172,6 +175,15 @@ target_link_libraries(alltests PRIVATE -Wl,--wrap=pam_authenticate -Wl,--wrap=pam_acct_mgmt + -Wl,--wrap=config_setting_get_elem + + -Wl,--wrap=getuid + -Wl,--wrap=getgrnam + -Wl,--wrap=setgid + -Wl,--wrap=setgroups + -Wl,--wrap=getpwnam + -Wl,--wrap=setuid + webfused-static userdb ${LIBCONFIG_LIBRARIES} diff --git a/src/webfused/change_user.c b/src/webfused/change_user.c index c651af8..5fbbfac 100644 --- a/src/webfused/change_user.c +++ b/src/webfused/change_user.c @@ -94,7 +94,7 @@ wfd_change_user( if (is_root) { - result = ((NULL != user) || (NULL != group)); + result = ((NULL != user) && (NULL != group)); if (!result) { WFD_ERROR("webfuse daemon cannot be run as root: specify user and group in config"); diff --git a/src/webfused/config/factory.c b/src/webfused/config/factory.c index 3c0d8d4..83400bb 100644 --- a/src/webfused/config/factory.c +++ b/src/webfused/config/factory.c @@ -71,7 +71,7 @@ wfd_config_read_logger( int rc = config_lookup_string(config, "log.provider", &provider); if (CONFIG_TRUE != rc) { - WFD_ERROR("missing log provider"); + WFD_ERROR("failed to load config: missing log provider"); result = false; } @@ -81,7 +81,7 @@ wfd_config_read_logger( rc = config_lookup_string(config, "log.level", &level_str); if (CONFIG_TRUE != rc) { - WFD_ERROR("missing log level"); + WFD_ERROR("failed to load config: missing log level"); result = false; } } @@ -92,7 +92,7 @@ wfd_config_read_logger( bool success = wfd_log_level_parse(level_str, &level); if (!success) { - WFD_ERROR("failed to parse log level: unknown valuie \'%s\'", level_str); + WFD_ERROR("failed to parse log level: unknown value \'%s\'", level_str); result = false; } } @@ -110,7 +110,7 @@ wfd_config_read_logger( return result; } -static bool +static void wfd_config_read_server( config_t * config, struct wfd_config * builder) @@ -149,8 +149,6 @@ wfd_config_read_server( { wfd_config_set_server_document_root(builder, doc_root); } - - return true; } static bool @@ -159,6 +157,10 @@ wfd_config_read_authenticator( struct wfd_config * builder) { bool result = (NULL != authenticator); + if (!result) + { + WFD_ERROR("failed to load config: invalid authentication section"); + } char const * provider_name = NULL; if (result) @@ -166,7 +168,7 @@ wfd_config_read_authenticator( int rc = config_setting_lookup_string(authenticator, "provider", &provider_name); if (CONFIG_TRUE != rc) { - WFD_ERROR("missing authentication provider"); + WFD_ERROR("failed to load config: missing authentication provider"); result = false; } } @@ -177,7 +179,7 @@ wfd_config_read_authenticator( settings = config_setting_lookup(authenticator, "settings"); if (NULL == settings) { - WFD_ERROR("missing authentication settings"); + WFD_ERROR("failed to load config: missing authentication settings"); result = false; } } @@ -312,12 +314,16 @@ wfd_config_load( bool success = wfd_config_check_version(config) && wfd_config_read_logger(config, result) - && wfd_config_read_server(config, result) && wfd_config_read_authentication(config, result) && wfd_config_read_filesystems(config, result) && wfd_config_read_user(config, result) ; + if (success) + { + wfd_config_read_server(config, result); + } + if (!success) { wfd_config_dispose(result); diff --git a/src/webfused/mountpoint_factory.c b/src/webfused/mountpoint_factory.c index 3fc641e..5f1aa14 100644 --- a/src/webfused/mountpoint_factory.c +++ b/src/webfused/mountpoint_factory.c @@ -141,10 +141,6 @@ wfd_mountpoint_factory_create_mountpoint( wf_mountpoint_set_userdata(result, &fs->in_use, &wfd_mountpoint_factory_release_mountpoint); - WFD_INFO("create mountpoint \'%s\' at path \'%s\': %s", - filesystem, - fs->mount_point, - result ? "success" : "failure"); - + WFD_INFO("created mountpoint \'%s\' at path \'%s\'", filesystem, fs->mount_point); return result; } diff --git a/test/mock_libconfig.cc b/test/mock_libconfig.cc new file mode 100644 index 0000000..9896ba1 --- /dev/null +++ b/test/mock_libconfig.cc @@ -0,0 +1,27 @@ +#include "mock_libconfig.hpp" +#include "wrap_utils.hpp" + +extern "C" +{ + +static webfused_test::ILibConfig * wfd_MockLibConfig = nullptr; + +WFD_WRAP_FUNC2(wfd_MockLibConfig, config_setting_t *, config_setting_get_elem, + config_setting_t const *, unsigned int); + +} + +namespace webfused_test +{ + +MockLibConfig::MockLibConfig() +{ + wfd_MockLibConfig = this; +} + +MockLibConfig::~MockLibConfig() +{ + wfd_MockLibConfig = nullptr; +} + +} \ No newline at end of file diff --git a/test/mock_libconfig.hpp b/test/mock_libconfig.hpp new file mode 100644 index 0000000..c142315 --- /dev/null +++ b/test/mock_libconfig.hpp @@ -0,0 +1,32 @@ +#ifndef WFD_MOCK_LIBCONFIG_HPP +#define WFD_MOCK_LIBCONFIG_HPP + +#include +#include + +namespace webfused_test +{ + +class ILibConfig +{ +public: + virtual ~ILibConfig() = default; + virtual config_setting_t * config_setting_get_elem( + config_setting_t const * setting, + unsigned int i) = 0; +}; + +class MockLibConfig: public ILibConfig +{ +public: + MockLibConfig(); + ~MockLibConfig() override; + MOCK_METHOD2(config_setting_get_elem, config_setting_t * ( + config_setting_t const * setting, + unsigned int i)); + +}; + +} + +#endif diff --git a/test/mock_linux.cc b/test/mock_linux.cc new file mode 100644 index 0000000..6bf3f0c --- /dev/null +++ b/test/mock_linux.cc @@ -0,0 +1,31 @@ +#include "mock_linux.hpp" +#include "wrap_utils.hpp" + +extern "C" +{ + +static webfused_test::ILinux * wfd_MockLinux = nullptr; + +WFD_WRAP_FUNC0( wfd_MockLinux, uid_t, getuid); +WFD_WRAP_FUNC1( wfd_MockLinux, struct group *, getgrnam, char const *); +WFD_WRAP_FUNC1( wfd_MockLinux, int, setgid, gid_t); +WFD_WRAP_FUNC2( wfd_MockLinux, int, setgroups, int, gid_t *); +WFD_WRAP_FUNC1( wfd_MockLinux, struct passwd *, getpwnam, char const *); +WFD_WRAP_FUNC1( wfd_MockLinux, int, setuid, uid_t); + +} + +namespace webfused_test +{ + +MockLinux::MockLinux() +{ + wfd_MockLinux = this; +} + +MockLinux::~MockLinux() +{ + wfd_MockLinux = nullptr; +} + +} \ No newline at end of file diff --git a/test/mock_linux.hpp b/test/mock_linux.hpp new file mode 100644 index 0000000..1a261a4 --- /dev/null +++ b/test/mock_linux.hpp @@ -0,0 +1,41 @@ +#ifndef WFD_MOCK_LINUX_HPP +#define WFD_MOCK_LINUX_HPP + +#include +#include +#include +#include + +#include + +namespace webfused_test +{ + +class ILinux +{ +public: + virtual ~ILinux() = default; + virtual uid_t getuid() = 0; + virtual struct group * getgrnam(char const * name) = 0; + virtual int setgid(gid_t gid) = 0; + virtual int setgroups(int size, gid_t * list) = 0; + virtual struct passwd * getpwnam(char const * name) = 0; + virtual int setuid(uid_t uid) = 0; +}; + +class MockLinux: public ILinux +{ +public: + MockLinux(); + ~MockLinux(); + MOCK_METHOD0(getuid, uid_t()); + MOCK_METHOD1(getgrnam, struct group * (char const * name)); + MOCK_METHOD1(setgid, int (gid_t gid)); + MOCK_METHOD2(setgroups, int (int size, gid_t * list)); + MOCK_METHOD1(getpwnam, struct passwd * (char const * name)); + MOCK_METHOD1(setuid, int (uid_t uid)); +}; + +} + +#endif diff --git a/test/test_change_user.cc b/test/test_change_user.cc new file mode 100644 index 0000000..244ebf7 --- /dev/null +++ b/test/test_change_user.cc @@ -0,0 +1,154 @@ +#include "webfused/change_user.h" +#include "mock_linux.hpp" +#include + +using ::webfused_test::MockLinux; +using ::testing::StrictMock; +using ::testing::Return; +using ::testing::StrEq; + +TEST(change_user, nop_id_not_root) +{ + StrictMock sys; + EXPECT_CALL(sys, getuid).Times(1).WillOnce(Return(42)); + + bool success = wfd_change_user("webfused", "daemons"); + ASSERT_TRUE(success); +} + +TEST(change_user, change_successfully) +{ + struct group group; + group.gr_gid = 23; + + struct passwd userinfo; + userinfo.pw_uid = 42; + + StrictMock sys; + EXPECT_CALL(sys, getuid).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getgrnam(StrEq("daemons"))).Times(1).WillOnce(Return(&group)); + EXPECT_CALL(sys, setgid(23)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, setgroups(0, nullptr)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getpwnam("webfused")).Times(1).WillOnce(Return(&userinfo)); + EXPECT_CALL(sys, setuid(42)).Times(1).WillOnce(Return(0)); + + bool success = wfd_change_user("webfused", "daemons"); + ASSERT_TRUE(success); +} + +TEST(change_user, fail_no_username_or_password) +{ + StrictMock sys; + EXPECT_CALL(sys, getuid).WillRepeatedly(Return(0)); + + ASSERT_FALSE(wfd_change_user(nullptr, "daemons")); + ASSERT_FALSE(wfd_change_user("webfused", nullptr)); + ASSERT_FALSE(wfd_change_user(nullptr, nullptr)); +} + +TEST(change_user, fail_setuid) +{ + struct group group; + group.gr_gid = 23; + + struct passwd userinfo; + userinfo.pw_uid = 42; + + StrictMock sys; + EXPECT_CALL(sys, getuid).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getgrnam(StrEq("daemons"))).Times(1).WillOnce(Return(&group)); + EXPECT_CALL(sys, setgid(23)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, setgroups(0, nullptr)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getpwnam("webfused")).Times(1).WillOnce(Return(&userinfo)); + EXPECT_CALL(sys, setuid(42)).Times(1).WillOnce(Return(-1)); + + bool success = wfd_change_user("webfused", "daemons"); + ASSERT_FALSE(success); +} + +TEST(change_user, fail_getpwnam) +{ + struct group group; + group.gr_gid = 23; + + StrictMock sys; + EXPECT_CALL(sys, getuid).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getgrnam(StrEq("daemons"))).Times(1).WillOnce(Return(&group)); + EXPECT_CALL(sys, setgid(23)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, setgroups(0, nullptr)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getpwnam("webfused")).Times(1).WillOnce(Return(nullptr)); + + bool success = wfd_change_user("webfused", "daemons"); + ASSERT_FALSE(success); +} + +TEST(change_user, fail_setgroups) +{ + struct group group; + group.gr_gid = 23; + + StrictMock sys; + EXPECT_CALL(sys, getuid).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getgrnam(StrEq("daemons"))).Times(1).WillOnce(Return(&group)); + EXPECT_CALL(sys, setgid(23)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, setgroups(0, nullptr)).Times(1).WillOnce(Return(-1)); + + bool success = wfd_change_user("webfused", "daemons"); + ASSERT_FALSE(success); +} + +TEST(change_user, fail_setgid) +{ + struct group group; + group.gr_gid = 23; + + StrictMock sys; + EXPECT_CALL(sys, getuid).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getgrnam(StrEq("daemons"))).Times(1).WillOnce(Return(&group)); + EXPECT_CALL(sys, setgid(23)).Times(1).WillOnce(Return(-1)); + + bool success = wfd_change_user("webfused", "daemons"); + ASSERT_FALSE(success); +} + +TEST(change_user, fail_getgrpnam) +{ + StrictMock sys; + EXPECT_CALL(sys, getuid).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getgrnam(StrEq("daemons"))).Times(1).WillOnce(Return(nullptr)); + + bool success = wfd_change_user("webfused", "daemons"); + ASSERT_FALSE(success); +} + +TEST(change_user, fail_switch_to_root) +{ + struct group group; + group.gr_gid = 23; + + struct passwd userinfo; + userinfo.pw_uid = 0; + + StrictMock sys; + EXPECT_CALL(sys, getuid).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getgrnam(StrEq("daemons"))).Times(1).WillOnce(Return(&group)); + EXPECT_CALL(sys, setgid(23)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, setgroups(0, nullptr)).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getpwnam("webfused")).Times(1).WillOnce(Return(&userinfo)); + + bool success = wfd_change_user("webfused", "daemons"); + ASSERT_FALSE(success); +} + +TEST(change_user, fail_switch_to_root_group) +{ + struct group group; + group.gr_gid = 0; + + StrictMock sys; + EXPECT_CALL(sys, getuid).Times(1).WillOnce(Return(0)); + EXPECT_CALL(sys, getgrnam(StrEq("daemons"))).Times(1).WillOnce(Return(&group)); + + bool success = wfd_change_user("webfused", "daemons"); + ASSERT_FALSE(success); +} diff --git a/test/test_config_factory.cc b/test/test_config_factory.cc index c09ca82..e318559 100644 --- a/test/test_config_factory.cc +++ b/test/test_config_factory.cc @@ -5,7 +5,7 @@ #include "webfused/log/log.h" #include "mock_logger.hpp" #include "mock_config_builder.hpp" - +#include "mock_libconfig.hpp" using ::testing::_; using ::testing::Return; @@ -13,6 +13,7 @@ using ::testing::StrictMock; using ::testing::StrEq; using ::webfused_test::MockLogger; using ::webfused_test::MockConfigBuilder; +using ::webfused_test::MockLibConfig; TEST(config, is_loadable) { @@ -372,6 +373,33 @@ TEST(config, failed_missing_auth_settings) ASSERT_EQ(nullptr, config); } +TEST(config, failed_auth_settings_get_elem) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, dispose(_)).Times(1); + + MockLibConfig libconfig; + EXPECT_CALL(libconfig, config_setting_get_elem(_,_)).Times(1).WillOnce(Return(nullptr)); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "authentication:\n" + "(\n" + " {\n" + " provider = \"test\"\n" + " settings: { }\n" + " }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + TEST(config, filesystems) { MockLogger logger; @@ -479,6 +507,31 @@ TEST(config, filesystems_failed_missing_mountpoint) ASSERT_EQ(nullptr, config); } +TEST(config, filesystems_failed_missing_elem) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, dispose(_)).Times(1); + EXPECT_CALL(builder, addFilesystem(_, _)).Times(0); + + MockLibConfig libconfig; + EXPECT_CALL(libconfig, config_setting_get_elem(_,_)).Times(1).WillOnce(Return(nullptr)); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "filesystems:\n" + "(\n" + " {name = \"foo\", mount_point = \"/tmp/test\" }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + TEST(config, log) { MockLogger logger; diff --git a/test/wrap_utils.hpp b/test/wrap_utils.hpp new file mode 100644 index 0000000..17c0394 --- /dev/null +++ b/test/wrap_utils.hpp @@ -0,0 +1,46 @@ +#ifndef WFD_WRAP_UTILS_HPP +#define WFD_WRAP_UTILS_HPP + +#define WFD_WRAP_FUNC0( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (); \ + RETURN_TYPE __wrap_ ## FUNC_NAME () \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(); \ + } \ + } + +#define WFD_WRAP_FUNC1( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1); \ + } \ + } + +#define WFD_WRAP_FUNC2( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1, arg2); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1, arg2); \ + } \ + } + +#endif From 1223363c6cc50899331b04105e7243f6d30b9a9c Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 10:28:36 +0100 Subject: [PATCH 04/13] set codecov badge to master --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 046990a..d4075f3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![Build Status](https://travis-ci.org/falk-werner/webfused.svg?branch=master)](https://travis-ci.org/falk-werner/webfused) -[![codecov](https://codecov.io/gh/falk-werner/webfused/branch/config_file/graph/badge.svg)](https://codecov.io/gh/falk-werner/webfused) +[![codecov](https://codecov.io/gh/falk-werner/webfused/branch/master/graph/badge.svg)](https://codecov.io/gh/falk-werner/webfused) # webfused From 35122136fd4b10bd04ae9143f0b99f342147c999 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 11:32:45 +0100 Subject: [PATCH 05/13] added daemon test --- CMakeLists.txt | 3 +++ test/mock_server.cc | 25 +++++++++++++++++++++++++ test/mock_server.hpp | 28 ++++++++++++++++++++++++++++ test/test_daemon.cc | 20 ++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 test/mock_server.cc create mode 100644 test/mock_server.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 505b695..ed06df5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,6 +126,7 @@ add_executable(alltests test/mock_pam.cc test/mock_libconfig.cc test/mock_linux.cc + test/mock_server.cc test/test_config_factory.cc test/test_config.cc test/test_settings.cc @@ -153,6 +154,8 @@ target_link_libraries(alltests PRIVATE -Wl,--wrap=wf_credentials_type -Wl,--wrap=wf_credentials_get + -Wl,--wrap=wf_server_create + -Wl,--wrap=wfd_settings_get_string -Wl,--wrap=wfd_settings_get_string_or_default -Wl,--wrap=wfd_settings_get_bool diff --git a/test/mock_server.cc b/test/mock_server.cc new file mode 100644 index 0000000..0e73929 --- /dev/null +++ b/test/mock_server.cc @@ -0,0 +1,25 @@ +#include "mock_server.hpp" +#include "wrap_utils.hpp" + +extern "C" +{ +static webfused_test::IServer * wfd_MockServer = nullptr; + +WFD_WRAP_FUNC1(wfd_MockServer, struct wf_server *, wf_server_create, struct wf_server_config *); + +} + +namespace webfused_test +{ + +MockServer::MockServer() +{ + wfd_MockServer = this; +} + +MockServer::~MockServer() +{ + wfd_MockServer = nullptr; +} + +} \ No newline at end of file diff --git a/test/mock_server.hpp b/test/mock_server.hpp new file mode 100644 index 0000000..f4befe3 --- /dev/null +++ b/test/mock_server.hpp @@ -0,0 +1,28 @@ +#ifndef WFD_MOCK_SERVER_HPP +#define WFD_MOCK_SERVER_HPP + +#include +#include + +namespace webfused_test +{ + +class IServer +{ +public: + virtual ~IServer() = default; + virtual struct wf_server * wf_server_create(struct wf_server_config * config) = 0; +}; + +class MockServer: public IServer +{ +public: + MockServer(); + ~MockServer() override; + MOCK_METHOD1(wf_server_create, struct wf_server * (struct wf_server_config * config)); +}; + + +} + +#endif diff --git a/test/test_daemon.cc b/test/test_daemon.cc index a0ed0c5..c81f6cd 100644 --- a/test/test_daemon.cc +++ b/test/test_daemon.cc @@ -1,10 +1,16 @@ #include "webfused/daemon.h" +#include "mock_server.hpp" + #include #include #include #include +using ::webfused_test::MockServer; +using ::testing::_; +using ::testing::Return; + TEST(daemon, print_usage) { char argv0[] = "daemon"; @@ -66,3 +72,17 @@ TEST(daemon, run) thread.join(); } + +TEST(daemon, run_failed_to_create_server) +{ + MockServer server; + EXPECT_CALL(server, wf_server_create(_)).Times(1).WillOnce(Return(nullptr)); + + char argv0[] = "daemon"; + char argv1[] = "-f"; + char argv2[] = "webfused.conf"; + char * argv[] = { argv0, argv1, argv2, NULL}; + + int exit_code = wfd_daemon_run(3, argv); + ASSERT_EQ(EXIT_FAILURE, exit_code); +} From 364d19bdea3adf4c41321dfe896f34d2389a871d Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 13:25:58 +0100 Subject: [PATCH 06/13] added tests for userdb --- CMakeLists.txt | 1 + src/passwd/main.c | 6 +- src/userdb/userdb.c | 108 +++---- src/userdb/userdb.h | 6 +- src/webfused/auth/file_authenticator.c | 2 +- test/test_userdb.cc | 407 +++++++++++++++++++++++++ 6 files changed, 474 insertions(+), 56 deletions(-) create mode 100644 test/test_userdb.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index ed06df5..f248572 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,6 +140,7 @@ add_executable(alltests test/test_syslog_logger.cc test/test_daemon.cc test/test_change_user.cc + test/test_userdb.cc ) target_include_directories(alltests PRIVATE diff --git a/src/passwd/main.c b/src/passwd/main.c index fda2171..f2313b4 100644 --- a/src/passwd/main.c +++ b/src/passwd/main.c @@ -173,7 +173,7 @@ static int add_user(struct args * args) } struct userdb * db = userdb_create(args->pepper); - userdb_load(db, args->file); + userdb_load_file(db, args->file); userdb_add(db, args->username, args->password); bool result = userdb_save(db, args->file); userdb_dispose(db); @@ -191,7 +191,7 @@ static int remove_user(struct args * args) } struct userdb * db = userdb_create(args->pepper); - userdb_load(db, args->file); + userdb_load_file(db, args->file); userdb_remove(db, args->username); bool result = userdb_save(db, args->file); userdb_dispose(db); @@ -216,7 +216,7 @@ static int check_password(struct args * args) } struct userdb * db = userdb_create(args->pepper); - userdb_load(db, args->file); + userdb_load_file(db, args->file); bool result = userdb_check(db, args->username, args->password); userdb_dispose(db); diff --git a/src/userdb/userdb.c b/src/userdb/userdb.c index 172742a..1eabdae 100644 --- a/src/userdb/userdb.c +++ b/src/userdb/userdb.c @@ -77,20 +77,17 @@ static char hex_char(unsigned char value) static char * to_hex(unsigned char const * value, size_t length) { char * result = malloc((2 * length) + 1); - if (NULL != result) + for (size_t i = 0, j = 0; i < length; i++, j+=2) { - for (size_t i = 0, j = 0; i < length; i++, j+=2) - { - unsigned char high = (value[i] >> 4) & 0x0f; - unsigned char low = value[i] & 0x0f; + unsigned char high = (value[i] >> 4) & 0x0f; + unsigned char low = value[i] & 0x0f; - result[j ] = hex_char(high); - result[j + 1] = hex_char(low); - } - - result[2 * length] = '\0'; + result[j ] = hex_char(high); + result[j + 1] = hex_char(low); } + result[2 * length] = '\0'; + return result; } @@ -120,21 +117,46 @@ static char * compute_hash( } char * result = NULL; + EVP_MD_CTX * context = EVP_MD_CTX_new(); + EVP_DigestInit_ex(context, digest, NULL); + EVP_DigestUpdate(context, password, strlen(password)); + EVP_DigestUpdate(context, salt, strlen(salt)); + EVP_DigestUpdate(context, db->pepper, strlen(db->pepper)); + unsigned int hash_size = EVP_MD_size(digest); unsigned char * hash = malloc(hash_size); + EVP_DigestFinal_ex(context, hash, &hash_size); + EVP_MD_CTX_free(context); - if (NULL != hash) + result = to_hex(hash, hash_size); + free(hash); + + return result; +} + +static bool userdb_load( + struct userdb * db, + json_t * container) +{ + bool result = false; + if (NULL != container) { - EVP_MD_CTX * context = EVP_MD_CTX_new(); - EVP_DigestInit_ex(context, digest, NULL); - EVP_DigestUpdate(context, password, strlen(password)); - EVP_DigestUpdate(context, salt, strlen(salt)); - EVP_DigestUpdate(context, db->pepper, strlen(db->pepper)); - EVP_DigestFinal_ex(context, hash, &hash_size); - EVP_MD_CTX_free(context); + json_t * meta = json_object_get(container, "meta"); + json_t * users = json_object_get(container, "users"); - result = to_hex(hash, hash_size); - free(hash); + if ((is_compatible(meta)) && (json_is_object(users))) { + json_t * hash_algorithm = json_object_get(meta, "hash_algorithm"); + free(db->hash_algorithm); + db->hash_algorithm = strdup(json_string_value(hash_algorithm)); + + json_decref(db->users); + json_incref(users); + db->users = users; + + result = true; + } + + json_decref(container); } return result; @@ -144,12 +166,9 @@ struct userdb * userdb_create( char const * pepper) { struct userdb * db = malloc(sizeof(struct userdb)); - if (NULL != db) - { - db->users = json_object(); - db->pepper = strdup(pepper); - db->hash_algorithm = strdup(USERDB_HASH_ALGORITHM); - } + db->users = json_object(); + db->pepper = strdup(pepper); + db->hash_algorithm = strdup(USERDB_HASH_ALGORITHM); return db; } @@ -184,36 +203,23 @@ bool userdb_save( return (0 == result); } - -bool userdb_load( +bool userdb_load_file( struct userdb * db, char const * filename) { - bool result = false; json_t * container = json_load_file(filename, 0, NULL); - if (NULL != container) - { - json_t * meta = json_object_get(container, "meta"); - json_t * users = json_object_get(container, "users"); - - if ((is_compatible(meta)) && (json_is_object(users))) { - json_t * hash_algorithm = json_object_get(meta, "hash_algorithm"); - free(db->hash_algorithm); - db->hash_algorithm = strdup(json_string_value(hash_algorithm)); - - json_decref(db->users); - json_incref(users); - db->users = users; - - result = true; - } - - json_decref(container); - } - - return result; + return userdb_load(db, container); } +bool userdb_load_string( + struct userdb * db, + char const * contents) +{ + json_t * container = json_loads(contents, 0, NULL); + return userdb_load(db, container); +} + + void userdb_add( struct userdb * db, char const * username, @@ -243,7 +249,7 @@ static char const * json_object_get_string( json_t * object, char const * key) { - char const * result = NULL; + char const * result = ""; json_t * string_holder = json_object_get(object, key); if (json_is_string(string_holder)) diff --git a/src/userdb/userdb.h b/src/userdb/userdb.h index cf133d0..211051e 100644 --- a/src/userdb/userdb.h +++ b/src/userdb/userdb.h @@ -21,10 +21,14 @@ extern bool userdb_save( struct userdb * db, char const * filename); -extern bool userdb_load( +extern bool userdb_load_file( struct userdb * db, char const * filename); +extern bool userdb_load_string( + struct userdb * db, + char const * contents); + extern void userdb_add( struct userdb * db, char const * username, diff --git a/src/webfused/auth/file_authenticator.c b/src/webfused/auth/file_authenticator.c index 371bbeb..31737fc 100644 --- a/src/webfused/auth/file_authenticator.c +++ b/src/webfused/auth/file_authenticator.c @@ -37,7 +37,7 @@ wfd_file_authenticator_authenticate( if ((NULL != username) && (NULL != password)) { struct userdb * db = userdb_create(""); - result = userdb_load(db, authenticator->filename); + result = userdb_load_file(db, authenticator->filename); if (result) { result = userdb_check(db, username, password); diff --git a/test/test_userdb.cc b/test/test_userdb.cc new file mode 100644 index 0000000..7b4a1d4 --- /dev/null +++ b/test/test_userdb.cc @@ -0,0 +1,407 @@ +#include "userdb/userdb.h" +#include + +TEST(userdb, load_file) +{ + userdb * db = userdb_create(""); + + bool success = userdb_load_file(db, "test_passwd.json"); + ASSERT_TRUE(success); + + ASSERT_TRUE(userdb_check(db, "bob", "secret")); + + userdb_dispose(db); +} + +TEST(userdb, load_file_failed_no_file) +{ + userdb * db = userdb_create(""); + + bool success = userdb_load_file(db, "non_existing.json"); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, save) +{ + { + userdb * db = userdb_create(""); + userdb_add(db, "bob", "secret"); + ASSERT_TRUE(userdb_save(db, "/tmp/webfused_test.json")); + userdb_dispose(db); + } + + { + userdb * db = userdb_create(""); + ASSERT_TRUE(userdb_load_file(db, "/tmp/webfused_test.json")); + ASSERT_TRUE(userdb_check(db, "bob", "secret")); + userdb_dispose(db); + } +} + + +TEST(userdb, add) +{ + userdb * db = userdb_create(""); + + userdb_add(db, "bob", "secret"); + ASSERT_TRUE(userdb_check(db, "bob", "secret")); + ASSERT_FALSE(userdb_check(db, "bob", "i_dont_know")); + ASSERT_FALSE(userdb_check(db, "anna", "secret")); + + userdb_dispose(db); +} + +TEST(userdb, remove) +{ + userdb * db = userdb_create(""); + ASSERT_NE(nullptr, db); + + userdb_add(db, "bob", "secret"); + ASSERT_TRUE(userdb_check(db, "bob", "secret")); + + userdb_remove(db, "bob"); + ASSERT_FALSE(userdb_check(db, "bob", "secret")); + + userdb_dispose(db); +} + +TEST(userdb, update) +{ + userdb * db = userdb_create(""); + ASSERT_NE(nullptr, db); + + userdb_add(db, "bob", "secret"); + ASSERT_TRUE(userdb_check(db, "bob", "secret")); + + userdb_add(db, "bob", "new_secret"); + ASSERT_FALSE(userdb_check(db, "bob", "secret")); + ASSERT_TRUE(userdb_check(db, "bob", "new_secret")); + + userdb_dispose(db); +} + +TEST(userdb, load_string) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"major\": 1," + " \"minor\": 0," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_TRUE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_no_json) +{ + userdb * db = userdb_create(""); + char const contents[] = "brummni"; + + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_invalid_type) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"any-userdb\"," + " \"major\": 1," + " \"minor\": 0," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_invalid_version) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"major\": 2," + " \"minor\": 0," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_missing_type) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"major\": 1," + " \"minor\": 0," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_type_not_string) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": 42," + " \"major\": 1," + " \"minor\": 0," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_missing_major_version) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"minor\": 0," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_major_version_not_int) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"major\": false," + " \"minor\": 0," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_missing_minor_version) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"major\": 1," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_minor_version_not_int) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"major\": 1," + " \"minor\": false," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_missing_hash_alg) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"major\": 1," + " \"minor\": 0," + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_hash_alg_not_string) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"major\": 1," + " \"minor\": 0," + " \"hash_algorithm\": 42" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_missing_meta) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, load_fail_unsupported_hash) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"major\": 1," + " \"minor\": 0," + " \"hash_algorithm\": \"brummni\"" + "}," + "\"users\": {" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_FALSE(success); + + userdb_dispose(db); +} + +TEST(userdb, fail_missing_user_salt) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"major\": 1," + " \"minor\": 0," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + " \"bob\": {" + " \"password_hash\": \"e51e27ce47054feead3d83068d47f2a07307d4877ac67da668ef43e0e466fe8c7b66651af14fdb8d48c51592ef5afa0c63f874d20861c6b9ef8e6513bfcaa330\"" + " }" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_TRUE(success); + + ASSERT_FALSE(userdb_check(db, "bob", "secret")); + userdb_dispose(db); +} + +TEST(userdb, fail_missing_user_hash) +{ + userdb * db = userdb_create(""); + char const contents[] = + "{" + "\"meta\": {" + " \"type\": \"wf-userdb\"," + " \"major\": 1," + " \"minor\": 0," + " \"hash_algorithm\": \"sha512\"" + "}," + "\"users\": {" + " \"bob\": {" + " \"salt\": \"b3be6979921edecfea88c50d0d1ec40b7f8c383831b2276c65969ead18e47c03\"" + " }" + "}" + "}" + ; + bool success = userdb_load_string(db, contents); + ASSERT_TRUE(success); + + ASSERT_FALSE(userdb_check(db, "bob", "secret")); + userdb_dispose(db); +} \ No newline at end of file From 933789b045daf552bb1fc23737c654fd5ca00da4 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 13:48:58 +0100 Subject: [PATCH 07/13] refactored tests --- CMakeLists.txt | 45 ++++++++++--------- .../{test_auth_factory.cc => auth/factory.cc} | 2 +- .../file_authenticator.cc} | 4 +- .../pam_authenticator.cc} | 6 +-- test/{test_change_user.cc => change_user.cc} | 2 +- test/{test_config.cc => config/config.cc} | 4 +- .../factory.cc} | 6 +-- test/{test_settings.cc => config/settings.cc} | 0 test/{test_daemon.cc => daemon.cc} | 2 +- test/{test_log.cc => log/log.cc} | 2 +- .../log_manager.cc} | 0 .../stderr_logger.cc} | 0 .../syslog_logger.cc} | 2 +- .../config_builder.cc} | 2 +- .../config_builder.hpp} | 0 .../credentials.cc} | 2 +- .../credentials.hpp} | 0 test/{mock_libconfig.cc => mock/libconfig.cc} | 4 +- .../libconfig.hpp} | 0 test/{mock_linux.cc => mock/linux.cc} | 4 +- test/{mock_linux.hpp => mock/linux.hpp} | 0 test/{mock_logger.cc => mock/logger.cc} | 2 +- test/{mock_logger.hpp => mock/logger.hpp} | 0 test/{mock_pam.cc => mock/pam.cc} | 2 +- test/{mock_pam.hpp => mock/pam.hpp} | 0 test/{mock_server.cc => mock/server.cc} | 4 +- test/{mock_server.hpp => mock/server.hpp} | 0 test/{mock_settings.cc => mock/settings.cc} | 2 +- test/{mock_settings.hpp => mock/settings.hpp} | 0 ...point_factory.cc => mountpoint_factory.cc} | 0 test/{test_userdb.cc => userdb.cc} | 0 test/{wrap_utils.hpp => util/wrap.hpp} | 4 +- 32 files changed, 51 insertions(+), 50 deletions(-) rename test/{test_auth_factory.cc => auth/factory.cc} (94%) rename test/{test_file_authenticator.cc => auth/file_authenticator.cc} (98%) rename test/{test_pam_authenticator.cc => auth/pam_authenticator.cc} (99%) rename test/{test_change_user.cc => change_user.cc} (99%) rename test/{test_config.cc => config/config.cc} (98%) rename test/{test_config_factory.cc => config/factory.cc} (99%) rename test/{test_settings.cc => config/settings.cc} (100%) rename test/{test_daemon.cc => daemon.cc} (98%) rename test/{test_log.cc => log/log.cc} (99%) rename test/{test_log_manager.cc => log/log_manager.cc} (100%) rename test/{test_stderr_logger.cc => log/stderr_logger.cc} (100%) rename test/{test_syslog_logger.cc => log/syslog_logger.cc} (97%) rename test/{mock_config_builder.cc => mock/config_builder.cc} (99%) rename test/{mock_config_builder.hpp => mock/config_builder.hpp} (100%) rename test/{mock_credentials.cc => mock/credentials.cc} (97%) rename test/{mock_credentials.hpp => mock/credentials.hpp} (100%) rename test/{mock_libconfig.cc => mock/libconfig.cc} (86%) rename test/{mock_libconfig.hpp => mock/libconfig.hpp} (100%) rename test/{mock_linux.cc => mock/linux.cc} (91%) rename test/{mock_linux.hpp => mock/linux.hpp} (100%) rename test/{mock_logger.cc => mock/logger.cc} (97%) rename test/{mock_logger.hpp => mock/logger.hpp} (100%) rename test/{mock_pam.cc => mock/pam.cc} (98%) rename test/{mock_pam.hpp => mock/pam.hpp} (100%) rename test/{mock_server.cc => mock/server.cc} (85%) rename test/{mock_server.hpp => mock/server.hpp} (100%) rename test/{mock_settings.cc => mock/settings.cc} (98%) rename test/{mock_settings.hpp => mock/settings.hpp} (100%) rename test/{test_mountpoint_factory.cc => mountpoint_factory.cc} (100%) rename test/{test_userdb.cc => userdb.cc} (100%) rename test/{wrap_utils.hpp => util/wrap.hpp} (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f248572..60e3713 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,32 +119,33 @@ include(GoogleTest) pkg_check_modules(GMOCK gmock) add_executable(alltests - test/mock_config_builder.cc - test/mock_logger.cc - test/mock_credentials.cc - test/mock_settings.cc - test/mock_pam.cc - test/mock_libconfig.cc - test/mock_linux.cc - test/mock_server.cc - test/test_config_factory.cc - test/test_config.cc - test/test_settings.cc - test/test_auth_factory.cc - test/test_file_authenticator.cc - test/test_pam_authenticator.cc - test/test_mountpoint_factory.cc - test/test_log.cc - test/test_log_manager.cc - test/test_stderr_logger.cc - test/test_syslog_logger.cc - test/test_daemon.cc - test/test_change_user.cc - test/test_userdb.cc + test/mock/config_builder.cc + test/mock/logger.cc + test/mock/credentials.cc + test/mock/settings.cc + test/mock/pam.cc + test/mock/libconfig.cc + test/mock/linux.cc + test/mock/server.cc + test/config/factory.cc + test/config/config.cc + test/config/settings.cc + test/auth/factory.cc + test/auth/file_authenticator.cc + test/auth/pam_authenticator.cc + test/log/log.cc + test/log/log_manager.cc + test/log/stderr_logger.cc + test/log/syslog_logger.cc + test/daemon.cc + test/change_user.cc + test/mountpoint_factory.cc + test/userdb.cc ) target_include_directories(alltests PRIVATE src + test ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS} ) diff --git a/test/test_auth_factory.cc b/test/auth/factory.cc similarity index 94% rename from test/test_auth_factory.cc rename to test/auth/factory.cc index 2e26cca..79a4bdf 100644 --- a/test/test_auth_factory.cc +++ b/test/auth/factory.cc @@ -2,7 +2,7 @@ #include "webfused/auth/factory.h" #include "webfused/auth/authenticator.h" #include "webfused/log/log.h" -#include "mock_logger.hpp" +#include "mock/logger.hpp" using ::testing::_; using ::webfused_test::MockLogger; diff --git a/test/test_file_authenticator.cc b/test/auth/file_authenticator.cc similarity index 98% rename from test/test_file_authenticator.cc rename to test/auth/file_authenticator.cc index ff3293e..9e1075a 100644 --- a/test/test_file_authenticator.cc +++ b/test/auth/file_authenticator.cc @@ -3,8 +3,8 @@ #include "webfused/config/settings.h" #include "webfused/auth/factory.h" -#include "mock_credentials.hpp" -#include "mock_settings.hpp" +#include "mock/credentials.hpp" +#include "mock/settings.hpp" #include #include diff --git a/test/test_pam_authenticator.cc b/test/auth/pam_authenticator.cc similarity index 99% rename from test/test_pam_authenticator.cc rename to test/auth/pam_authenticator.cc index 2bbeff0..36689cd 100644 --- a/test/test_pam_authenticator.cc +++ b/test/auth/pam_authenticator.cc @@ -3,9 +3,9 @@ #include "webfused/config/settings.h" #include "webfused/auth/factory.h" -#include "mock_credentials.hpp" -#include "mock_settings.hpp" -#include "mock_pam.hpp" +#include "mock/credentials.hpp" +#include "mock/settings.hpp" +#include "mock/pam.hpp" #include diff --git a/test/test_change_user.cc b/test/change_user.cc similarity index 99% rename from test/test_change_user.cc rename to test/change_user.cc index 244ebf7..840f756 100644 --- a/test/test_change_user.cc +++ b/test/change_user.cc @@ -1,5 +1,5 @@ #include "webfused/change_user.h" -#include "mock_linux.hpp" +#include "mock/linux.hpp" #include using ::webfused_test::MockLinux; diff --git a/test/test_config.cc b/test/config/config.cc similarity index 98% rename from test/test_config.cc rename to test/config/config.cc index 8225b10..e1b56ce 100644 --- a/test/test_config.cc +++ b/test/config/config.cc @@ -1,11 +1,11 @@ #include #include "webfused/config/config.h" #include "webfused/config/config_intern.h" -#include "mock_settings.hpp" +#include "mock/settings.hpp" #include "webfused/log/logger.h" #include "webfused/log/log.h" -#include "mock_logger.hpp" +#include "mock/logger.hpp" using ::webfused_test::MockLogger; using ::testing::_; diff --git a/test/test_config_factory.cc b/test/config/factory.cc similarity index 99% rename from test/test_config_factory.cc rename to test/config/factory.cc index e318559..4f443a7 100644 --- a/test/test_config_factory.cc +++ b/test/config/factory.cc @@ -3,9 +3,9 @@ #include "webfused/config/factory.h" #include "webfused/log/logger.h" #include "webfused/log/log.h" -#include "mock_logger.hpp" -#include "mock_config_builder.hpp" -#include "mock_libconfig.hpp" +#include "mock/logger.hpp" +#include "mock/config_builder.hpp" +#include "mock/libconfig.hpp" using ::testing::_; using ::testing::Return; diff --git a/test/test_settings.cc b/test/config/settings.cc similarity index 100% rename from test/test_settings.cc rename to test/config/settings.cc diff --git a/test/test_daemon.cc b/test/daemon.cc similarity index 98% rename from test/test_daemon.cc rename to test/daemon.cc index c81f6cd..d053293 100644 --- a/test/test_daemon.cc +++ b/test/daemon.cc @@ -1,6 +1,6 @@ #include "webfused/daemon.h" -#include "mock_server.hpp" +#include "mock/server.hpp" #include #include diff --git a/test/test_log.cc b/test/log/log.cc similarity index 99% rename from test/test_log.cc rename to test/log/log.cc index 331d0eb..b000608 100644 --- a/test/test_log.cc +++ b/test/log/log.cc @@ -3,7 +3,7 @@ #include "webfused/log/logger.h" #include "webfused/log/log.h" -#include "mock_logger.hpp" +#include "mock/logger.hpp" using ::testing::_; using ::testing::StrEq; diff --git a/test/test_log_manager.cc b/test/log/log_manager.cc similarity index 100% rename from test/test_log_manager.cc rename to test/log/log_manager.cc diff --git a/test/test_stderr_logger.cc b/test/log/stderr_logger.cc similarity index 100% rename from test/test_stderr_logger.cc rename to test/log/stderr_logger.cc diff --git a/test/test_syslog_logger.cc b/test/log/syslog_logger.cc similarity index 97% rename from test/test_syslog_logger.cc rename to test/log/syslog_logger.cc index 9976560..b5aad9a 100644 --- a/test/test_syslog_logger.cc +++ b/test/log/syslog_logger.cc @@ -3,7 +3,7 @@ #include "webfused/log/logger.h" #include "webfused/log/log.h" -#include "mock_settings.hpp" +#include "mock/settings.hpp" #include #include diff --git a/test/mock_config_builder.cc b/test/mock/config_builder.cc similarity index 99% rename from test/mock_config_builder.cc rename to test/mock/config_builder.cc index e2d8bce..99c0382 100644 --- a/test/mock_config_builder.cc +++ b/test/mock/config_builder.cc @@ -1,4 +1,4 @@ -#include "mock_config_builder.hpp" +#include "mock/config_builder.hpp" extern "C" { diff --git a/test/mock_config_builder.hpp b/test/mock/config_builder.hpp similarity index 100% rename from test/mock_config_builder.hpp rename to test/mock/config_builder.hpp diff --git a/test/mock_credentials.cc b/test/mock/credentials.cc similarity index 97% rename from test/mock_credentials.cc rename to test/mock/credentials.cc index a6023b1..04652ea 100644 --- a/test/mock_credentials.cc +++ b/test/mock/credentials.cc @@ -1,4 +1,4 @@ -#include "mock_credentials.hpp" +#include "mock/credentials.hpp" extern "C" diff --git a/test/mock_credentials.hpp b/test/mock/credentials.hpp similarity index 100% rename from test/mock_credentials.hpp rename to test/mock/credentials.hpp diff --git a/test/mock_libconfig.cc b/test/mock/libconfig.cc similarity index 86% rename from test/mock_libconfig.cc rename to test/mock/libconfig.cc index 9896ba1..2ce80f8 100644 --- a/test/mock_libconfig.cc +++ b/test/mock/libconfig.cc @@ -1,5 +1,5 @@ -#include "mock_libconfig.hpp" -#include "wrap_utils.hpp" +#include "mock/libconfig.hpp" +#include "util/wrap.hpp" extern "C" { diff --git a/test/mock_libconfig.hpp b/test/mock/libconfig.hpp similarity index 100% rename from test/mock_libconfig.hpp rename to test/mock/libconfig.hpp diff --git a/test/mock_linux.cc b/test/mock/linux.cc similarity index 91% rename from test/mock_linux.cc rename to test/mock/linux.cc index 6bf3f0c..1769d97 100644 --- a/test/mock_linux.cc +++ b/test/mock/linux.cc @@ -1,5 +1,5 @@ -#include "mock_linux.hpp" -#include "wrap_utils.hpp" +#include "mock/linux.hpp" +#include "util/wrap.hpp" extern "C" { diff --git a/test/mock_linux.hpp b/test/mock/linux.hpp similarity index 100% rename from test/mock_linux.hpp rename to test/mock/linux.hpp diff --git a/test/mock_logger.cc b/test/mock/logger.cc similarity index 97% rename from test/mock_logger.cc rename to test/mock/logger.cc index a36d89a..4749962 100644 --- a/test/mock_logger.cc +++ b/test/mock/logger.cc @@ -1,4 +1,4 @@ -#include "mock_logger.hpp" +#include "mock/logger.hpp" #include "webfused/log/logger.h" #include "webfused/log/log.h" diff --git a/test/mock_logger.hpp b/test/mock/logger.hpp similarity index 100% rename from test/mock_logger.hpp rename to test/mock/logger.hpp diff --git a/test/mock_pam.cc b/test/mock/pam.cc similarity index 98% rename from test/mock_pam.cc rename to test/mock/pam.cc index b637d9b..1155e68 100644 --- a/test/mock_pam.cc +++ b/test/mock/pam.cc @@ -1,4 +1,4 @@ -#include "mock_pam.hpp" +#include "mock/pam.hpp" extern "C" { diff --git a/test/mock_pam.hpp b/test/mock/pam.hpp similarity index 100% rename from test/mock_pam.hpp rename to test/mock/pam.hpp diff --git a/test/mock_server.cc b/test/mock/server.cc similarity index 85% rename from test/mock_server.cc rename to test/mock/server.cc index 0e73929..d439b8d 100644 --- a/test/mock_server.cc +++ b/test/mock/server.cc @@ -1,5 +1,5 @@ -#include "mock_server.hpp" -#include "wrap_utils.hpp" +#include "mock/server.hpp" +#include "util/wrap.hpp" extern "C" { diff --git a/test/mock_server.hpp b/test/mock/server.hpp similarity index 100% rename from test/mock_server.hpp rename to test/mock/server.hpp diff --git a/test/mock_settings.cc b/test/mock/settings.cc similarity index 98% rename from test/mock_settings.cc rename to test/mock/settings.cc index 3cc36ff..d6b2f1c 100644 --- a/test/mock_settings.cc +++ b/test/mock/settings.cc @@ -1,4 +1,4 @@ -#include "mock_settings.hpp" +#include "mock/settings.hpp" extern "C" { diff --git a/test/mock_settings.hpp b/test/mock/settings.hpp similarity index 100% rename from test/mock_settings.hpp rename to test/mock/settings.hpp diff --git a/test/test_mountpoint_factory.cc b/test/mountpoint_factory.cc similarity index 100% rename from test/test_mountpoint_factory.cc rename to test/mountpoint_factory.cc diff --git a/test/test_userdb.cc b/test/userdb.cc similarity index 100% rename from test/test_userdb.cc rename to test/userdb.cc diff --git a/test/wrap_utils.hpp b/test/util/wrap.hpp similarity index 95% rename from test/wrap_utils.hpp rename to test/util/wrap.hpp index 17c0394..efe7b0c 100644 --- a/test/wrap_utils.hpp +++ b/test/util/wrap.hpp @@ -1,5 +1,5 @@ -#ifndef WFD_WRAP_UTILS_HPP -#define WFD_WRAP_UTILS_HPP +#ifndef WFD_UTIL_WRAP_HPP +#define WFD_UTIL_WRAP_HPP #define WFD_WRAP_FUNC0( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME ) \ extern RETURN_TYPE __real_ ## FUNC_NAME (); \ From d7fd1fdf83030f662070aaa5fdfa0173399b2e21 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 14:02:38 +0100 Subject: [PATCH 08/13] use wrap utility --- test/auth/pam_authenticator.cc | 40 ++++++++--------- test/mock/pam.cc | 82 +++------------------------------- test/mock/pam.hpp | 20 ++++----- test/util/wrap.hpp | 28 ++++++++++++ 4 files changed, 64 insertions(+), 106 deletions(-) diff --git a/test/auth/pam_authenticator.cc b/test/auth/pam_authenticator.cc index 36689cd..1946111 100644 --- a/test/auth/pam_authenticator.cc +++ b/test/auth/pam_authenticator.cc @@ -51,10 +51,10 @@ TEST(pam_authenticator, get_type) TEST(pam_authenticator, authenticate) { MockPam pam; - EXPECT_CALL(pam, start(StrEq("webfused"), nullptr, _, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, authenticate(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, acct_mgmt(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_start(StrEq("webfused"), nullptr, _, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_authenticate(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_acct_mgmt(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); wfd_authenticator authenticator; bool success = wfd_pam_authenticator_create(nullptr, &authenticator); @@ -73,10 +73,10 @@ TEST(pam_authenticator, authenticate) TEST(pam_authenticator, authenticate_with_custom_service_name) { MockPam pam; - EXPECT_CALL(pam, start(StrEq("brummni"), nullptr, _, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, authenticate(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, acct_mgmt(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_start(StrEq("brummni"), nullptr, _, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_authenticate(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_acct_mgmt(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); MockSettings settings; EXPECT_CALL(settings, getStringOrDefault(StrEq("service_name"), StrEq("webfused"))) @@ -133,11 +133,11 @@ int valid_conversation( TEST(pam_authenticator, conversation_with_valid_messages) { MockPam pam; - EXPECT_CALL(pam, start(StrEq("webfused"), nullptr, _, _)) + EXPECT_CALL(pam, pam_start(StrEq("webfused"), nullptr, _, _)) .Times(1).WillOnce(Invoke(&valid_conversation)); - EXPECT_CALL(pam, authenticate(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, acct_mgmt(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_authenticate(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_acct_mgmt(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); wfd_authenticator authenticator; bool success = wfd_pam_authenticator_create(nullptr, &authenticator); @@ -182,7 +182,7 @@ int invalid_conversation( TEST(pam_authenticator, conversation_with_invalid_messages) { MockPam pam; - EXPECT_CALL(pam, start(StrEq("webfused"), nullptr, _, _)) + EXPECT_CALL(pam, pam_start(StrEq("webfused"), nullptr, _, _)) .Times(1).WillOnce(Invoke(&invalid_conversation)); wfd_authenticator authenticator; @@ -202,9 +202,9 @@ TEST(pam_authenticator, conversation_with_invalid_messages) TEST(pam_authenticator, authenticate_fail_authenticate) { MockPam pam; - EXPECT_CALL(pam, start(StrEq("webfused"), nullptr, _, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, authenticate(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(-1)); - EXPECT_CALL(pam, end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_start(StrEq("webfused"), nullptr, _, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_authenticate(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(-1)); + EXPECT_CALL(pam, pam_end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); wfd_authenticator authenticator; bool success = wfd_pam_authenticator_create(nullptr, &authenticator); @@ -223,10 +223,10 @@ TEST(pam_authenticator, authenticate_fail_authenticate) TEST(pam_authenticator, authenticate_fail_acct_mgmt) { MockPam pam; - EXPECT_CALL(pam, start(StrEq("webfused"), nullptr, _, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, authenticate(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); - EXPECT_CALL(pam, acct_mgmt(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(-1)); - EXPECT_CALL(pam, end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_start(StrEq("webfused"), nullptr, _, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_authenticate(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(PAM_SUCCESS)); + EXPECT_CALL(pam, pam_acct_mgmt(_, PAM_DISALLOW_NULL_AUTHTOK)).Times(1).WillOnce(Return(-1)); + EXPECT_CALL(pam, pam_end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); wfd_authenticator authenticator; bool success = wfd_pam_authenticator_create(nullptr, &authenticator); diff --git a/test/mock/pam.cc b/test/mock/pam.cc index 1155e68..44ff44f 100644 --- a/test/mock/pam.cc +++ b/test/mock/pam.cc @@ -1,85 +1,15 @@ #include "mock/pam.hpp" +#include "util/wrap.hpp" extern "C" { - static webfused_test::IPam * wfd_MockPam = nullptr; -extern int __real_pam_start( - char const * service_name, - char const * user, - struct pam_conv const * conversation, - pam_handle_t * * handle); -extern int __real_pam_end(pam_handle_t * handle, int status); -extern int __real_pam_authenticate(pam_handle_t * handle, int flags); -extern int __real_pam_acct_mgmt(pam_handle_t * handle, int flags); -extern char const * __real_pam_strerror(pam_handle_t * handle, int errnum); - -int __wrap_pam_start( - char const * service_name, - char const * user, - struct pam_conv const * conversation, - pam_handle_t * * handle) -{ - if (nullptr == wfd_MockPam) - { - return __real_pam_start(service_name, user, conversation, handle); - } - else - { - return wfd_MockPam->start(service_name, user, conversation, handle); - } - -} - -int __wrap_pam_end(pam_handle_t * handle, int status) -{ - if (nullptr == wfd_MockPam) - { - return __real_pam_end(handle, status); - } - else - { - return wfd_MockPam->end(handle, status); - } -} - -int __wrap_pam_authenticate(pam_handle_t * handle, int flags) -{ - if (nullptr == wfd_MockPam) - { - return __real_pam_authenticate(handle, flags); - } - else - { - return wfd_MockPam->authenticate(handle, flags); - } -} - -int __wrap_pam_acct_mgmt(pam_handle_t * handle, int flags) -{ - if (nullptr == wfd_MockPam) - { - return __real_pam_acct_mgmt(handle, flags); - } - else - { - return wfd_MockPam->acct_mgmt(handle, flags); - } -} - -char const * __wrap_pam_strerror(pam_handle_t * handle, int errnum) -{ - if (nullptr == wfd_MockPam) - { - return __real_pam_strerror(handle, errnum); - } - else - { - return wfd_MockPam->strerror(handle, errnum); - } -} - +WFD_WRAP_FUNC4(wfd_MockPam, int, pam_start, char const *, char const *, struct pam_conv const *, pam_handle_t **); +WFD_WRAP_FUNC2(wfd_MockPam, int, pam_end, pam_handle_t *, int); +WFD_WRAP_FUNC2(wfd_MockPam, int, pam_authenticate, pam_handle_t *, int); +WFD_WRAP_FUNC2(wfd_MockPam, int, pam_acct_mgmt, pam_handle_t *, int); +WFD_WRAP_FUNC2(wfd_MockPam, char const *, pam_strerror, pam_handle_t *, int); } diff --git a/test/mock/pam.hpp b/test/mock/pam.hpp index a191dc8..cb6a19f 100644 --- a/test/mock/pam.hpp +++ b/test/mock/pam.hpp @@ -11,15 +11,15 @@ class IPam { public: virtual ~IPam() = default; - virtual int start( + virtual int pam_start( char const * service_name, char const * user, struct pam_conv const * conversation, pam_handle_t * * handle) = 0; - virtual int end(pam_handle_t * handle, int status) = 0; - virtual int authenticate(pam_handle_t * handle, int flags) = 0; - virtual int acct_mgmt(pam_handle_t * handle, int flags) = 0; - virtual char const * strerror(pam_handle_t * handle, int errnum) = 0; + virtual int pam_end(pam_handle_t * handle, int status) = 0; + virtual int pam_authenticate(pam_handle_t * handle, int flags) = 0; + virtual int pam_acct_mgmt(pam_handle_t * handle, int flags) = 0; + virtual char const * pam_strerror(pam_handle_t * handle, int errnum) = 0; }; class MockPam: public IPam @@ -28,16 +28,16 @@ public: MockPam(); ~MockPam() override; - MOCK_METHOD4(start, int ( + MOCK_METHOD4(pam_start, int ( char const * service_name, char const * user, struct pam_conv const * conversation, pam_handle_t * * handle)); - MOCK_METHOD2(end, int(pam_handle_t * handle, int status)); - MOCK_METHOD2(authenticate, int(pam_handle_t * handle, int flags)); - MOCK_METHOD2(acct_mgmt, int (pam_handle_t * handle, int flags)); - MOCK_METHOD2(strerror, char const * (pam_handle_t * handle, int errnum)); + MOCK_METHOD2(pam_end, int(pam_handle_t * handle, int status)); + MOCK_METHOD2(pam_authenticate, int(pam_handle_t * handle, int flags)); + MOCK_METHOD2(pam_acct_mgmt, int (pam_handle_t * handle, int flags)); + MOCK_METHOD2(pam_strerror, char const * (pam_handle_t * handle, int errnum)); }; } diff --git a/test/util/wrap.hpp b/test/util/wrap.hpp index efe7b0c..dc572ba 100644 --- a/test/util/wrap.hpp +++ b/test/util/wrap.hpp @@ -43,4 +43,32 @@ } \ } +#define WFD_WRAP_FUNC3( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1, arg2, arg3); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3); \ + } \ + } + +#define WFD_WRAP_FUNC4( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4); \ + } \ + } + #endif From 90fc495d9584050791d9c84673a989da4c5d642d Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 14:16:32 +0100 Subject: [PATCH 09/13] use wrap utility --- test/auth/file_authenticator.cc | 19 +++++---- test/auth/pam_authenticator.cc | 2 +- test/config/config.cc | 4 +- test/log/syslog_logger.cc | 6 +-- test/mock/settings.cc | 76 ++++----------------------------- test/mock/settings.hpp | 12 +++--- 6 files changed, 30 insertions(+), 89 deletions(-) diff --git a/test/auth/file_authenticator.cc b/test/auth/file_authenticator.cc index 9e1075a..3973abf 100644 --- a/test/auth/file_authenticator.cc +++ b/test/auth/file_authenticator.cc @@ -13,11 +13,12 @@ using ::webfused_test::MockSettings; using ::webfused_test::MockCredentials; using ::testing::Return; using ::testing::StrEq; +using ::testing::_; TEST(file_authenticator, create) { MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); wfd_authenticator authenticator; bool success = wfd_file_authenticator_create(nullptr, &authenticator); @@ -29,7 +30,7 @@ TEST(file_authenticator, create) TEST(file_authenticator, create_fail_missing_file) { MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return(nullptr)); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return(nullptr)); wfd_authenticator authenticator; bool success = wfd_file_authenticator_create(nullptr, &authenticator); @@ -39,7 +40,7 @@ TEST(file_authenticator, create_fail_missing_file) TEST(file_authenticator, create_via_factory) { MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); wfd_authenticator authenticator; bool success = wfd_authenticator_create("file", nullptr, &authenticator); @@ -51,7 +52,7 @@ TEST(file_authenticator, create_via_factory) TEST(file_authenticator, authenticate) { MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); wfd_authenticator authenticator; bool success = wfd_file_authenticator_create(nullptr, &authenticator); @@ -70,7 +71,7 @@ TEST(file_authenticator, authenticate) TEST(file_authenticator, authenticate_fail_wrong_passwd) { MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); wfd_authenticator authenticator; bool success = wfd_file_authenticator_create(nullptr, &authenticator); @@ -89,7 +90,7 @@ TEST(file_authenticator, authenticate_fail_wrong_passwd) TEST(file_authenticator, authenticate_fail_no_passwd_file) { MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return("unknown_passwd.json")); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("unknown_passwd.json")); wfd_authenticator authenticator; bool success = wfd_file_authenticator_create(nullptr, &authenticator); @@ -108,7 +109,7 @@ TEST(file_authenticator, authenticate_fail_no_passwd_file) TEST(file_authenticator, authenticate_fail_missing_username) { MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); wfd_authenticator authenticator; bool success = wfd_file_authenticator_create(nullptr, &authenticator); @@ -127,7 +128,7 @@ TEST(file_authenticator, authenticate_fail_missing_username) TEST(file_authenticator, authenticate_fail_missing_password) { MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json")); wfd_authenticator authenticator; bool success = wfd_file_authenticator_create(nullptr, &authenticator); @@ -146,7 +147,7 @@ TEST(file_authenticator, authenticate_fail_missing_password) TEST(file_authenticator, get_type) { MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return("/any/path")); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("/any/path")); wfd_authenticator authenticator; bool success = wfd_file_authenticator_create(nullptr, &authenticator); diff --git a/test/auth/pam_authenticator.cc b/test/auth/pam_authenticator.cc index 1946111..3b30119 100644 --- a/test/auth/pam_authenticator.cc +++ b/test/auth/pam_authenticator.cc @@ -79,7 +79,7 @@ TEST(pam_authenticator, authenticate_with_custom_service_name) EXPECT_CALL(pam, pam_end(_, _)).Times(1).WillOnce(Return(PAM_SUCCESS)); MockSettings settings; - EXPECT_CALL(settings, getStringOrDefault(StrEq("service_name"), StrEq("webfused"))) + EXPECT_CALL(settings, wfd_settings_get_string_or_default(_,StrEq("service_name"), StrEq("webfused"))) .Times(1).WillOnce(Return("brummni")); wfd_authenticator authenticator; diff --git a/test/config/config.cc b/test/config/config.cc index e1b56ce..e28c200 100644 --- a/test/config/config.cc +++ b/test/config/config.cc @@ -36,7 +36,7 @@ TEST(config, auth_config) ASSERT_NE(nullptr, config); MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return("/any/path")); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("/any/path")); bool success = wfd_config_add_auth_provider(config, "file", nullptr); ASSERT_TRUE(success); @@ -50,7 +50,7 @@ TEST(config, auth_config_failed_to_add_second_provider) ASSERT_NE(nullptr, config); MockSettings settings; - EXPECT_CALL(settings, getString(StrEq("file"))).Times(1).WillOnce(Return("/any/path")); + EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("/any/path")); bool success = wfd_config_add_auth_provider(config, "file", nullptr); ASSERT_TRUE(success); diff --git a/test/log/syslog_logger.cc b/test/log/syslog_logger.cc index b5aad9a..8432513 100644 --- a/test/log/syslog_logger.cc +++ b/test/log/syslog_logger.cc @@ -22,9 +22,9 @@ TEST(syslog_logger, init) TEST(syslog_logger, init_fail_invalid_facility) { MockSettings settings; - EXPECT_CALL(settings, getStringOrDefault(StrEq("ident"), _)).Times(1).WillOnce(Return("webfused_test")); - EXPECT_CALL(settings, getStringOrDefault(StrEq("facility"), _)).Times(1).WillOnce(Return("invalid")); - EXPECT_CALL(settings, getBool(StrEq("log_pid"))).Times(1).WillOnce(Return(false)); + EXPECT_CALL(settings, wfd_settings_get_string_or_default(_,StrEq("ident"), _)).Times(1).WillOnce(Return("webfused_test")); + EXPECT_CALL(settings, wfd_settings_get_string_or_default(_,StrEq("facility"), _)).Times(1).WillOnce(Return("invalid")); + EXPECT_CALL(settings, wfd_settings_get_bool(_,StrEq("log_pid"))).Times(1).WillOnce(Return(false)); wfd_settings * fake_settings = reinterpret_cast(0xDEADBEEF); ASSERT_FALSE(wfd_syslog_logger_init(WFD_LOGLEVEL_ALL, fake_settings)); diff --git a/test/mock/settings.cc b/test/mock/settings.cc index d6b2f1c..ca37aab 100644 --- a/test/mock/settings.cc +++ b/test/mock/settings.cc @@ -1,74 +1,14 @@ #include "mock/settings.hpp" +#include "util/wrap.hpp" extern "C" { -using webfused_test::ISettings; - -static ISettings * wfd_mock_settings = nullptr; - -extern char const * -__real_wfd_settings_get_string( - struct wfd_settings * settings, - char const * key); - -extern char const * -__real_wfd_settings_get_string_or_default( - struct wfd_settings * settings, - char const * key, - char const * default_value); - -extern bool -__real_wfd_settings_get_bool( - struct wfd_settings * settings, - char const * key); - -char const * -__wrap_wfd_settings_get_string( - struct wfd_settings * settings, - char const * key) -{ - if (nullptr == wfd_mock_settings) - { - return __real_wfd_settings_get_string(settings, key); - } - else - { - return wfd_mock_settings->getString(key); - } -} - -char const * -__wrap_wfd_settings_get_string_or_default( - struct wfd_settings * settings, - char const * key, - char const * default_value) -{ - if (nullptr == wfd_mock_settings) - { - return __real_wfd_settings_get_string_or_default( - settings, key, default_value); - } - else - { - return wfd_mock_settings->getStringOrDefault(key, default_value); - } -} - -bool -__wrap_wfd_settings_get_bool( - struct wfd_settings * settings, - char const * key) -{ - if (nullptr == wfd_mock_settings) - { - return __real_wfd_settings_get_bool(settings, key); - } - else - { - return wfd_mock_settings->getBool(key); - } -} +static webfused_test::ISettings * wfd_MockSettings = nullptr; +WFD_WRAP_FUNC2(wfd_MockSettings, char const *, wfd_settings_get_string, struct wfd_settings *, char const *); +WFD_WRAP_FUNC3(wfd_MockSettings, char const *, wfd_settings_get_string_or_default, + struct wfd_settings *, char const *, char const *); +WFD_WRAP_FUNC2(wfd_MockSettings, bool, wfd_settings_get_bool, struct wfd_settings *, char const *); } namespace webfused_test @@ -76,12 +16,12 @@ namespace webfused_test MockSettings::MockSettings() { - wfd_mock_settings = this; + wfd_MockSettings = this; } MockSettings::~MockSettings() { - wfd_mock_settings = nullptr; + wfd_MockSettings = nullptr; } } \ No newline at end of file diff --git a/test/mock/settings.hpp b/test/mock/settings.hpp index f3d8692..6fbf9d3 100644 --- a/test/mock/settings.hpp +++ b/test/mock/settings.hpp @@ -11,9 +11,9 @@ class ISettings { public: virtual ~ISettings() = default; - virtual char const * getString(char const * key) = 0; - virtual char const * getStringOrDefault(char const * key, char const * default_value) = 0; - virtual bool getBool(char const * key) = 0; + virtual char const * wfd_settings_get_string(wfd_settings * settings, char const * key) = 0; + virtual char const * wfd_settings_get_string_or_default(wfd_settings * settings, char const * key, char const * default_value) = 0; + virtual bool wfd_settings_get_bool(wfd_settings * settings, char const * key) = 0; }; class MockSettings: public ISettings @@ -21,9 +21,9 @@ class MockSettings: public ISettings public: MockSettings(); ~MockSettings() override; - MOCK_METHOD1(getString, char const * (char const * key)); - MOCK_METHOD2(getStringOrDefault, char const * (char const * key, char const * default_value)); - MOCK_METHOD1(getBool, bool (char const * key)); + MOCK_METHOD2(wfd_settings_get_string, char const * (wfd_settings * settings, char const * key)); + MOCK_METHOD3(wfd_settings_get_string_or_default, char const * (wfd_settings * settings, char const * key, char const * default_value)); + MOCK_METHOD2(wfd_settings_get_bool, bool (wfd_settings * settings, char const * key)); }; } From ea56c6e86c5151b9960dea87afb3f61b54bc7dab Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 14:26:41 +0100 Subject: [PATCH 10/13] use wrap utility --- test/auth/file_authenticator.cc | 20 ++++++------- test/auth/pam_authenticator.cc | 32 ++++++++++----------- test/mock/credentials.cc | 50 ++++----------------------------- test/mock/credentials.hpp | 8 +++--- 4 files changed, 36 insertions(+), 74 deletions(-) diff --git a/test/auth/file_authenticator.cc b/test/auth/file_authenticator.cc index 3973abf..48f1e27 100644 --- a/test/auth/file_authenticator.cc +++ b/test/auth/file_authenticator.cc @@ -59,8 +59,8 @@ TEST(file_authenticator, authenticate) 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")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secret")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_TRUE(is_authenticated); @@ -78,8 +78,8 @@ TEST(file_authenticator, authenticate_fail_wrong_passwd) 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")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("unkown")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_FALSE(is_authenticated); @@ -97,8 +97,8 @@ TEST(file_authenticator, authenticate_fail_no_passwd_file) 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")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secred")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_FALSE(is_authenticated); @@ -116,8 +116,8 @@ TEST(file_authenticator, authenticate_fail_missing_username) 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")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return(nullptr)); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("unkown")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_FALSE(is_authenticated); @@ -135,8 +135,8 @@ TEST(file_authenticator, authenticate_fail_missing_password) 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)); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return(nullptr)); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_FALSE(is_authenticated); diff --git a/test/auth/pam_authenticator.cc b/test/auth/pam_authenticator.cc index 3b30119..4051098 100644 --- a/test/auth/pam_authenticator.cc +++ b/test/auth/pam_authenticator.cc @@ -61,8 +61,8 @@ TEST(pam_authenticator, authenticate) 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")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secret")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_TRUE(is_authenticated); @@ -87,8 +87,8 @@ TEST(pam_authenticator, authenticate_with_custom_service_name) 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")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secret")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_TRUE(is_authenticated); @@ -144,8 +144,8 @@ TEST(pam_authenticator, conversation_with_valid_messages) 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")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secret")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_TRUE(is_authenticated); @@ -190,8 +190,8 @@ TEST(pam_authenticator, conversation_with_invalid_messages) 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")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secret")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_FALSE(is_authenticated); @@ -211,8 +211,8 @@ TEST(pam_authenticator, authenticate_fail_authenticate) 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")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secret")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_FALSE(is_authenticated); @@ -233,8 +233,8 @@ TEST(pam_authenticator, authenticate_fail_acct_mgmt) 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")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secret")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_FALSE(is_authenticated); @@ -251,8 +251,8 @@ TEST(pam_authenticator, authenticate_fail_missing_username) 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("secret")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return(nullptr)); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secret")); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_FALSE(is_authenticated); @@ -269,8 +269,8 @@ TEST(pam_authenticator, authenticate_fail_missing_password) 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)); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob")); + EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return(nullptr)); bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr); ASSERT_FALSE(is_authenticated); diff --git a/test/mock/credentials.cc b/test/mock/credentials.cc index 04652ea..e10477a 100644 --- a/test/mock/credentials.cc +++ b/test/mock/credentials.cc @@ -1,50 +1,12 @@ #include "mock/credentials.hpp" - +#include "util/wrap.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); - } -} - +static webfused_test::ICredentials * wfd_MockCredentials = nullptr; +WFD_WRAP_FUNC1(wfd_MockCredentials, char const *, wf_credentials_type, struct wf_credentials const *); +WFD_WRAP_FUNC2(wfd_MockCredentials, char const *, wf_credentials_get, struct wf_credentials const *, char const *); } @@ -53,12 +15,12 @@ namespace webfused_test MockCredentials::MockCredentials() { - wfd_mock_credentials = this; + wfd_MockCredentials = this; } MockCredentials::~MockCredentials() { - wfd_mock_credentials = nullptr; + wfd_MockCredentials = nullptr; } } diff --git a/test/mock/credentials.hpp b/test/mock/credentials.hpp index 6f0ca1e..5b37fb2 100644 --- a/test/mock/credentials.hpp +++ b/test/mock/credentials.hpp @@ -11,8 +11,8 @@ class ICredentials { public: virtual ~ICredentials() = default; - virtual char const * type() = 0; - virtual char const * get(char const * key) = 0; + virtual char const * wf_credentials_type(struct wf_credentials const * credentials) = 0; + virtual char const * wf_credentials_get(struct wf_credentials const * credentials, char const * key) = 0; }; class MockCredentials: public ICredentials @@ -20,8 +20,8 @@ class MockCredentials: public ICredentials public: MockCredentials(); virtual ~MockCredentials(); - MOCK_METHOD0(type, char const*()); - MOCK_METHOD1(get, char const *(char const * key)); + MOCK_METHOD1(wf_credentials_type, char const*(struct wf_credentials const * credentials)); + MOCK_METHOD2(wf_credentials_get, char const *(struct wf_credentials const * credentials, char const * key)); }; } From bd4323e53b67520deeec4ec87e780559fb16f873 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 14:51:57 +0100 Subject: [PATCH 11/13] use wrap utility --- test/config/factory.cc | 160 +++++++++++------------ test/mock/config_builder.cc | 240 ++--------------------------------- test/mock/config_builder.hpp | 44 +++---- 3 files changed, 115 insertions(+), 329 deletions(-) diff --git a/test/config/factory.cc b/test/config/factory.cc index 4f443a7..28b52bc 100644 --- a/test/config/factory.cc +++ b/test/config/factory.cc @@ -18,12 +18,12 @@ using ::webfused_test::MockLibConfig; TEST(config, is_loadable) { StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, setLogger(_,_,_)).Times(1).WillOnce(Return(true)); - 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, addFilesystem(_,_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_logger(_,_,_,_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(builder, wfd_config_set_server_vhostname(_,StrEq("localhost"))).Times(1); + EXPECT_CALL(builder, wfd_config_set_server_port(_,8080)).Times(1); + EXPECT_CALL(builder, wfd_config_add_auth_provider(_,_, _)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_,_)).Times(1).WillOnce(Return(true)); struct wfd_config * config = wfd_config_load_file("webfused.conf"); ASSERT_NE(nullptr, config); @@ -36,7 +36,7 @@ TEST(config, minimal_config) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); char const minimal[] = "version = { major = 1, minor = 0 }\n"; struct wfd_config * config = wfd_config_load_string(minimal); @@ -50,8 +50,8 @@ TEST(config, invalid_config) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(0); - EXPECT_CALL(builder, dispose(_)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(0); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(0); char const syntax_error[] = "version.major = 1\n"; @@ -66,8 +66,8 @@ TEST(config, invalid_config_file) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(0); - EXPECT_CALL(builder, dispose(_)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(0); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(0); struct wfd_config * config = wfd_config_load_file("invalid.conf"); ASSERT_EQ(nullptr, config); @@ -80,8 +80,8 @@ TEST(config, invalid_major_version_too_low) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); char const too_low[] = "version = { major = 0, minor = 0 }\n"; @@ -96,8 +96,8 @@ TEST(config, invalid_major_version_too_high) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); char const too_high[] = "version = { major = 2, minor = 0 }\n"; @@ -112,8 +112,8 @@ TEST(config, invalid_missing_major_version) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); char const too_high[] = "version = { minor = 0 }\n"; @@ -128,8 +128,8 @@ TEST(config, invalid_missing_minor_version) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); char const too_high[] = "version = { major = 1 }\n"; @@ -144,7 +144,7 @@ TEST(config, valid_older_minor) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); char const valid[] = "version = { major = 1, minor = -1 }\n"; @@ -159,7 +159,7 @@ TEST(config, valid_newer_minor) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); char const valid[] = "version = { major = 1, minor = 1 }\n"; @@ -174,8 +174,8 @@ TEST(config, vhost_name) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, setServerVhostname(StrEq("some.host"))).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_server_vhostname(_,StrEq("some.host"))).Times(1); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -195,8 +195,8 @@ TEST(config, port) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, setServerPort(54321)).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_server_port(_,54321)).Times(1); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -216,8 +216,8 @@ TEST(config, tls_certificate) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, setServerCert(StrEq("/path/to/cert.pem"))).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_server_cert(_, StrEq("/path/to/cert.pem"))).Times(1); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -240,8 +240,8 @@ TEST(config, tls_key) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, setServerKey(StrEq("/path/to/key.pem"))).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_server_key(_,StrEq("/path/to/key.pem"))).Times(1); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -264,8 +264,8 @@ TEST(config, document_root) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, setServerDocumentRoot(StrEq("/var/www"))).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_server_document_root(_,StrEq("/var/www"))).Times(1); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -285,8 +285,8 @@ TEST(config, authentication) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, addAuthProvider(_, _)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_add_auth_provider(_,_, _)).Times(1).WillOnce(Return(true)); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -309,9 +309,9 @@ TEST(config, failed_create_authenticator) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, addAuthProvider(_, _)).Times(1).WillOnce(Return(false)); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_add_auth_provider(_,_, _)).Times(1).WillOnce(Return(false)); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -334,8 +334,8 @@ TEST(config, failed_missing_auth_provider) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -357,8 +357,8 @@ TEST(config, failed_missing_auth_settings) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -380,8 +380,8 @@ TEST(config, failed_auth_settings_get_elem) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); MockLibConfig libconfig; EXPECT_CALL(libconfig, config_setting_get_elem(_,_)).Times(1).WillOnce(Return(nullptr)); @@ -407,8 +407,8 @@ TEST(config, filesystems) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, addFilesystem(_, _)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(1).WillOnce(Return(true)); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -428,8 +428,8 @@ TEST(config, filesystems_empty) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, addFilesystem(_, _)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -448,9 +448,9 @@ TEST(config, filesystems_failed_add) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, addFilesystem(_, _)).Times(1).WillOnce(Return(false)); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(1).WillOnce(Return(false)); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -470,9 +470,9 @@ TEST(config, filesystems_failed_missing_name) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, addFilesystem(_, _)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -492,9 +492,9 @@ TEST(config, filesystems_failed_missing_mountpoint) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, addFilesystem(_, _)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -514,9 +514,9 @@ TEST(config, filesystems_failed_missing_elem) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, addFilesystem(_, _)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); MockLibConfig libconfig; EXPECT_CALL(libconfig, config_setting_get_elem(_,_)).Times(1).WillOnce(Return(nullptr)); @@ -539,8 +539,8 @@ TEST(config, log) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, setLogger(_, _, _)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(1).WillOnce(Return(true)); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -561,9 +561,9 @@ TEST(config, log_fail_set_logger) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, setLogger(_, _, _)).Times(1).WillOnce(Return(false)); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(1).WillOnce(Return(false)); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -584,9 +584,9 @@ TEST(config, log_fail_missing_provider) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, setLogger(_, _, _)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(0); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -606,9 +606,9 @@ TEST(config, log_fail_missing_level) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, setLogger(_, _, _)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(0); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -629,9 +629,9 @@ TEST(config, log_fail_invalid_level) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, setLogger(_, _, _)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_logger(_, _, _, _)).Times(0); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -651,8 +651,8 @@ TEST(config, set_user) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, setUser(_, _)).Times(1); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_user(_, _, _)).Times(1); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -673,9 +673,9 @@ TEST(config, set_user_fail_missing_name) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, setUser(_, _)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_user(_, _, _)).Times(0); char const config_text[] = "version = { major = 1, minor = 0 }\n" @@ -695,9 +695,9 @@ TEST(config, set_user_fail_missing_group) EXPECT_CALL(logger, onclose()).Times(1); StrictMock builder; - EXPECT_CALL(builder, create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, dispose(_)).Times(1); - EXPECT_CALL(builder, setUser(_, _)).Times(0); + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_user(_, _, _)).Times(0); char const config_text[] = "version = { major = 1, minor = 0 }\n" diff --git a/test/mock/config_builder.cc b/test/mock/config_builder.cc index 99c0382..3815edf 100644 --- a/test/mock/config_builder.cc +++ b/test/mock/config_builder.cc @@ -1,235 +1,21 @@ #include "mock/config_builder.hpp" +#include "util/wrap.hpp" extern "C" { -using webfused_test::IConfigBuilder; - -static IConfigBuilder * wfd_MockConfigBuilder = nullptr; - -extern struct wfd_config * -__real_wfd_config_create(void); - -extern void -__real_wfd_config_dispose( - struct wfd_config * config); - -extern void -__real_wfd_config_set_server_vhostname( - struct wfd_config * config, - char const * vhost_name); - -extern void -__real_wfd_config_set_server_port( - struct wfd_config * config, - int port); - -extern void -__real_wfd_config_set_server_key( - struct wfd_config * config, - char const * key_path); - -extern void -__real_wfd_config_set_server_cert( - struct wfd_config * config, - char const * cert_path); - -extern void -__real_wfd_config_set_server_document_root( - struct wfd_config * config, - char const * document_root); - -extern bool -__real_wfd_config_add_auth_provider( - struct wfd_config * config, - char const * provider, - struct wfd_settings * settings); - -extern bool -__real_wfd_config_add_filesystem( - struct wfd_config * config, - char const * name, - char const * mount_point); - -extern bool -__real_wfd_config_set_logger( - struct wfd_config * config, - char const * provider, - int level, - struct wfd_settings * settings); - -extern void -__real_wfd_config_set_user( - struct wfd_config * config, - char const * user, - char const * group); - -struct wfd_config * -__wrap_wfd_config_create(void) -{ - if (nullptr == wfd_MockConfigBuilder) - { - return __real_wfd_config_create(); - } - else - { - return wfd_MockConfigBuilder->create(); - } -} - -void -__wrap_wfd_config_dispose( - struct wfd_config * config) -{ - if (nullptr == wfd_MockConfigBuilder) - { - __real_wfd_config_dispose(config); - } - else - { - wfd_MockConfigBuilder->dispose(config); - } -} - -void -__wrap_wfd_config_set_server_vhostname( - struct wfd_config * config, - char const * vhost_name) -{ - if (nullptr == wfd_MockConfigBuilder) - { - __real_wfd_config_set_server_vhostname(config, vhost_name); - } - else - { - wfd_MockConfigBuilder->setServerVhostname(vhost_name); - } -} - -void -__wrap_wfd_config_set_server_port( - struct wfd_config * config, - int port) -{ - if (nullptr == wfd_MockConfigBuilder) - { - __real_wfd_config_set_server_port(config, port); - } - else - { - wfd_MockConfigBuilder->setServerPort(port); - } -} - -void -__wrap_wfd_config_set_server_key( - struct wfd_config * config, - char const * key_path) -{ - if (nullptr == wfd_MockConfigBuilder) - { - __real_wfd_config_set_server_key(config, key_path); - } - else - { - wfd_MockConfigBuilder->setServerKey(key_path); - } -} - -void -__wrap_wfd_config_set_server_cert( - struct wfd_config * config, - char const * cert_path) -{ - if (nullptr == wfd_MockConfigBuilder) - { - __real_wfd_config_set_server_cert(config, cert_path); - } - else - { - wfd_MockConfigBuilder->setServerCert(cert_path); - } -} - -void -__wrap_wfd_config_set_server_document_root( - struct wfd_config * config, - char const * document_root) -{ - if (nullptr == wfd_MockConfigBuilder) - { - __real_wfd_config_set_server_document_root(config, document_root); - } - else - { - wfd_MockConfigBuilder->setServerDocumentRoot(document_root); - } -} - -bool -__wrap_wfd_config_add_auth_provider( - struct wfd_config * config, - char const * provider, - struct wfd_settings * settings) -{ - if (nullptr == wfd_MockConfigBuilder) - { - return __real_wfd_config_add_auth_provider(config, provider, settings); - } - else - { - return wfd_MockConfigBuilder->addAuthProvider(provider, settings); - } -} - -bool -__wrap_wfd_config_add_filesystem( - struct wfd_config * config, - char const * name, - char const * mount_point) -{ - if (nullptr == wfd_MockConfigBuilder) - { - return __real_wfd_config_add_filesystem(config, name, mount_point); - } - else - { - return wfd_MockConfigBuilder->addFilesystem(name, mount_point); - } -} - -bool -__wrap_wfd_config_set_logger( - struct wfd_config * config, - char const * provider, - int level, - struct wfd_settings * settings) -{ - if (nullptr == wfd_MockConfigBuilder) - { - return __real_wfd_config_set_logger(config, provider, level, settings); - } - else - { - return wfd_MockConfigBuilder->setLogger(provider, level, settings); - } -} - -void -__wrap_wfd_config_set_user( - struct wfd_config * config, - char const * user, - char const * group) -{ - if (nullptr == wfd_MockConfigBuilder) - { - __real_wfd_config_set_user(config, user, group); - } - else - { - wfd_MockConfigBuilder->setUser(user, group); - } -} +static webfused_test::IConfigBuilder * wfd_MockConfigBuilder = nullptr; +WFD_WRAP_FUNC0(wfd_MockConfigBuilder, wfd_config *, wfd_config_create); +WFD_WRAP_FUNC1(wfd_MockConfigBuilder, void, wfd_config_dispose, wfd_config *); +WFD_WRAP_FUNC2(wfd_MockConfigBuilder, void, wfd_config_set_server_vhostname, wfd_config *, char const *); +WFD_WRAP_FUNC2(wfd_MockConfigBuilder, void, wfd_config_set_server_port, wfd_config *, int); +WFD_WRAP_FUNC2(wfd_MockConfigBuilder, void, wfd_config_set_server_key, wfd_config *, char const *); +WFD_WRAP_FUNC2(wfd_MockConfigBuilder, void, wfd_config_set_server_cert, wfd_config *, char const *); +WFD_WRAP_FUNC2(wfd_MockConfigBuilder, void, wfd_config_set_server_document_root, wfd_config *, char const *); +WFD_WRAP_FUNC3(wfd_MockConfigBuilder, bool, wfd_config_add_auth_provider, wfd_config *, char const *, wfd_settings *); +WFD_WRAP_FUNC3(wfd_MockConfigBuilder, bool, wfd_config_add_filesystem, wfd_config *, char const *, char const *); +WFD_WRAP_FUNC4(wfd_MockConfigBuilder, bool, wfd_config_set_logger, wfd_config *, char const *, int, wfd_settings *); +WFD_WRAP_FUNC3(wfd_MockConfigBuilder, void, wfd_config_set_user, wfd_config *, char const *, char const *); } namespace webfused_test diff --git a/test/mock/config_builder.hpp b/test/mock/config_builder.hpp index 3e414e1..b687821 100644 --- a/test/mock/config_builder.hpp +++ b/test/mock/config_builder.hpp @@ -11,17 +11,17 @@ class IConfigBuilder { public: virtual ~IConfigBuilder() = default; - virtual wfd_config * create(void) = 0; - virtual void dispose(wfd_config * config) = 0; - virtual void setServerVhostname(char const * vhostname) = 0; - virtual void setServerPort(int port) = 0; - 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(char const * provider, wfd_settings * settings) = 0; - virtual bool addFilesystem(char const * name, char const * mountpoint) = 0; - virtual bool setLogger(char const * provider, int level, wfd_settings * settings) = 0; - virtual void setUser(char const * user, char const * group) = 0; + virtual wfd_config * wfd_config_create(void) = 0; + virtual void wfd_config_dispose(wfd_config * config) = 0; + virtual void wfd_config_set_server_vhostname(wfd_config * config, char const * vhostname) = 0; + virtual void wfd_config_set_server_port(wfd_config * config, int port) = 0; + virtual void wfd_config_set_server_key(wfd_config * config, char const * key_path) = 0; + virtual void wfd_config_set_server_cert(wfd_config * config, char const * cert_path) = 0; + virtual void wfd_config_set_server_document_root(wfd_config * config, char const * document_root) = 0; + virtual bool wfd_config_add_auth_provider(wfd_config * config, char const * provider, wfd_settings * settings) = 0; + virtual bool wfd_config_add_filesystem(wfd_config * config, char const * name, char const * mountpoint) = 0; + virtual bool wfd_config_set_logger(wfd_config * config, char const * provider, int level, wfd_settings * settings) = 0; + virtual void wfd_config_set_user(wfd_config * config, char const * user, char const * group) = 0; }; class MockConfigBuilder: public IConfigBuilder @@ -29,17 +29,17 @@ class MockConfigBuilder: public IConfigBuilder public: MockConfigBuilder(); ~MockConfigBuilder() override; - MOCK_METHOD0(create, wfd_config * (void)); - MOCK_METHOD1(dispose, void (wfd_config * config)); - MOCK_METHOD1(setServerVhostname, void (char const * vhostname)); - MOCK_METHOD1(setServerPort, void (int port)); - 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_METHOD2(addAuthProvider, bool (char const * provider, wfd_settings * settings)); - MOCK_METHOD2(addFilesystem, bool (char const * name, char const * mountpoint)); - MOCK_METHOD3(setLogger, bool (char const * provider, int level, wfd_settings * settings)); - MOCK_METHOD2(setUser, void (char const * user, char const * group)); + MOCK_METHOD0(wfd_config_create, wfd_config * (void)); + MOCK_METHOD1(wfd_config_dispose, void (wfd_config * config)); + MOCK_METHOD2(wfd_config_set_server_vhostname, void (wfd_config * config, char const * vhostname)); + MOCK_METHOD2(wfd_config_set_server_port, void (wfd_config * config, int port)); + MOCK_METHOD2(wfd_config_set_server_key, void (wfd_config * config, char const * key_path)); + MOCK_METHOD2(wfd_config_set_server_cert, void (wfd_config * config, char const * cert_path)); + MOCK_METHOD2(wfd_config_set_server_document_root, void (wfd_config * config, char const * document_root)); + MOCK_METHOD3(wfd_config_add_auth_provider, bool (wfd_config * config, char const * provider, wfd_settings * settings)); + MOCK_METHOD3(wfd_config_add_filesystem, bool (wfd_config * config, char const * name, char const * mountpoint)); + MOCK_METHOD4(wfd_config_set_logger, bool (wfd_config * config, char const * provider, int level, wfd_settings * settings)); + MOCK_METHOD3(wfd_config_set_user, void (wfd_config * config, char const * user, char const * group)); struct wfd_config * getBuilder(); }; From 390acffe7b920285eac211f99df53960fbf285f3 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 15:10:58 +0100 Subject: [PATCH 12/13] split factory tests --- CMakeLists.txt | 8 +- test/config/configfile.cc | 74 +++ test/config/configfile_auth.cc | 138 ++++++ test/config/configfile_filesystem.cc | 148 ++++++ test/config/configfile_log.cc | 128 +++++ test/config/configfile_server.cc | 127 +++++ test/config/configfile_user.cc | 82 +++ test/config/configfile_version.cc | 110 +++++ test/config/factory.cc | 711 --------------------------- 9 files changed, 814 insertions(+), 712 deletions(-) create mode 100644 test/config/configfile.cc create mode 100644 test/config/configfile_auth.cc create mode 100644 test/config/configfile_filesystem.cc create mode 100644 test/config/configfile_log.cc create mode 100644 test/config/configfile_server.cc create mode 100644 test/config/configfile_user.cc create mode 100644 test/config/configfile_version.cc delete mode 100644 test/config/factory.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 60e3713..662d363 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,7 +127,13 @@ add_executable(alltests test/mock/libconfig.cc test/mock/linux.cc test/mock/server.cc - test/config/factory.cc + test/config/configfile.cc + test/config/configfile_version.cc + test/config/configfile_server.cc + test/config/configfile_auth.cc + test/config/configfile_filesystem.cc + test/config/configfile_log.cc + test/config/configfile_user.cc test/config/config.cc test/config/settings.cc test/auth/factory.cc diff --git a/test/config/configfile.cc b/test/config/configfile.cc new file mode 100644 index 0000000..8aab81b --- /dev/null +++ b/test/config/configfile.cc @@ -0,0 +1,74 @@ +#include + +#include "webfused/config/factory.h" +#include "webfused/log/logger.h" +#include "webfused/log/log.h" +#include "mock/logger.hpp" +#include "mock/config_builder.hpp" +#include "mock/libconfig.hpp" + +using ::testing::_; +using ::testing::Return; +using ::testing::StrictMock; +using ::testing::StrEq; +using ::webfused_test::MockLogger; +using ::webfused_test::MockConfigBuilder; +using ::webfused_test::MockLibConfig; + +TEST(configfile, is_loadable) +{ + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_logger(_,_,_,_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(builder, wfd_config_set_server_vhostname(_,StrEq("localhost"))).Times(1); + EXPECT_CALL(builder, wfd_config_set_server_port(_,8080)).Times(1); + EXPECT_CALL(builder, wfd_config_add_auth_provider(_,_, _)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_,_)).Times(1).WillOnce(Return(true)); + + struct wfd_config * config = wfd_config_load_file("webfused.conf"); + ASSERT_NE(nullptr, config); +} + +TEST(configfile, minimal_config) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + + char const minimal[] = "version = { major = 1, minor = 0 }\n"; + struct wfd_config * config = wfd_config_load_string(minimal); + ASSERT_NE(nullptr, config); +} + +TEST(configfile, invalid_config) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(0); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(0); + + char const syntax_error[] = "version.major = 1\n"; + + wfd_config * config = wfd_config_load_string(syntax_error); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile, invalid_config_file) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(0); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(0); + + struct wfd_config * config = wfd_config_load_file("invalid.conf"); + ASSERT_EQ(nullptr, config); +} diff --git a/test/config/configfile_auth.cc b/test/config/configfile_auth.cc new file mode 100644 index 0000000..ae6b80b --- /dev/null +++ b/test/config/configfile_auth.cc @@ -0,0 +1,138 @@ +#include + +#include "webfused/config/factory.h" +#include "webfused/log/logger.h" +#include "webfused/log/log.h" +#include "mock/logger.hpp" +#include "mock/config_builder.hpp" +#include "mock/libconfig.hpp" + +using ::testing::_; +using ::testing::Return; +using ::testing::StrictMock; +using ::testing::StrEq; +using ::webfused_test::MockLogger; +using ::webfused_test::MockConfigBuilder; +using ::webfused_test::MockLibConfig; + +TEST(config_auth, authentication) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_add_auth_provider(_,_, _)).Times(1).WillOnce(Return(true)); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "authentication:\n" + "(\n" + " {\n" + " provider = \"test\"\n" + " settings: { }\n" + " }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_NE(nullptr, config); +} + +TEST(config_auth, failed_create_authenticator) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_add_auth_provider(_,_, _)).Times(1).WillOnce(Return(false)); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "authentication:\n" + "(\n" + " {\n" + " provider = \"test\"\n" + " settings: { }\n" + " }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + +TEST(config_auth, failed_missing_auth_provider) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "authentication:\n" + "(\n" + " {\n" + " settings: { }\n" + " }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + +TEST(config_auth, failed_missing_auth_settings) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "authentication:\n" + "(\n" + " {\n" + " provider = \"test\"\n" + " }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + +TEST(config_auth, failed_auth_settings_get_elem) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + + MockLibConfig libconfig; + EXPECT_CALL(libconfig, config_setting_get_elem(_,_)).Times(1).WillOnce(Return(nullptr)); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "authentication:\n" + "(\n" + " {\n" + " provider = \"test\"\n" + " settings: { }\n" + " }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} diff --git a/test/config/configfile_filesystem.cc b/test/config/configfile_filesystem.cc new file mode 100644 index 0000000..2d34cce --- /dev/null +++ b/test/config/configfile_filesystem.cc @@ -0,0 +1,148 @@ +#include + +#include "webfused/config/factory.h" +#include "webfused/log/logger.h" +#include "webfused/log/log.h" +#include "mock/logger.hpp" +#include "mock/config_builder.hpp" +#include "mock/libconfig.hpp" + +using ::testing::_; +using ::testing::Return; +using ::testing::StrictMock; +using ::testing::StrEq; +using ::webfused_test::MockLogger; +using ::webfused_test::MockConfigBuilder; +using ::webfused_test::MockLibConfig; + +TEST(configfile_fs, filesystems) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(1).WillOnce(Return(true)); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "filesystems:\n" + "(\n" + " {name = \"foo\", mount_point = \"/tmp/test\" }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_NE(nullptr, config); +} + +TEST(configfile_fs, filesystems_empty) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "filesystems:\n" + "(\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_NE(nullptr, config); +} + +TEST(configfile_fs, filesystems_failed_add) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(1).WillOnce(Return(false)); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "filesystems:\n" + "(\n" + " {name = \"foo\", mount_point = \"/tmp/test\" }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_fs, filesystems_failed_missing_name) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "filesystems:\n" + "(\n" + " {mount_point = \"/tmp/test\" }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_fs, filesystems_failed_missing_mountpoint) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "filesystems:\n" + "(\n" + " {name = \"foo\"}\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_fs, filesystems_failed_missing_elem) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); + + MockLibConfig libconfig; + EXPECT_CALL(libconfig, config_setting_get_elem(_,_)).Times(1).WillOnce(Return(nullptr)); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "filesystems:\n" + "(\n" + " {name = \"foo\", mount_point = \"/tmp/test\" }\n" + ")\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} diff --git a/test/config/configfile_log.cc b/test/config/configfile_log.cc new file mode 100644 index 0000000..6740821 --- /dev/null +++ b/test/config/configfile_log.cc @@ -0,0 +1,128 @@ +#include + +#include "webfused/config/factory.h" +#include "webfused/log/logger.h" +#include "webfused/log/log.h" +#include "mock/logger.hpp" +#include "mock/config_builder.hpp" +#include "mock/libconfig.hpp" + +using ::testing::_; +using ::testing::Return; +using ::testing::StrictMock; +using ::testing::StrEq; +using ::webfused_test::MockLogger; +using ::webfused_test::MockConfigBuilder; +using ::webfused_test::MockLibConfig; + +TEST(configfile_log, set_logger) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(1).WillOnce(Return(true)); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "log:\n" + "{\n" + " provider = \"stderr\"\n" + " level = \"all\"\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_NE(nullptr, config); +} + +TEST(configfile_log, log_fail_set_logger) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(1).WillOnce(Return(false)); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "log:\n" + "{\n" + " provider = \"stderr\"\n" + " level = \"all\"\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_log, log_fail_missing_provider) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(0); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "log:\n" + "{\n" + " level = \"all\"\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_log, log_fail_missing_level) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(0); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "log:\n" + "{\n" + " provider = \"stderr\"\n" + " level = \"fancy\"\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_log, log_fail_invalid_level) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_logger(_, _, _, _)).Times(0); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "log:\n" + "{\n" + " provider = \"stderr\"\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} diff --git a/test/config/configfile_server.cc b/test/config/configfile_server.cc new file mode 100644 index 0000000..bb34e62 --- /dev/null +++ b/test/config/configfile_server.cc @@ -0,0 +1,127 @@ +#include + +#include "webfused/config/factory.h" +#include "webfused/log/logger.h" +#include "webfused/log/log.h" +#include "mock/logger.hpp" +#include "mock/config_builder.hpp" +#include "mock/libconfig.hpp" + +using ::testing::_; +using ::testing::Return; +using ::testing::StrictMock; +using ::testing::StrEq; +using ::webfused_test::MockLogger; +using ::webfused_test::MockConfigBuilder; +using ::webfused_test::MockLibConfig; + +TEST(configfile_server, vhost_name) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_server_vhostname(_,StrEq("some.host"))).Times(1); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "server:\n" + "{\n" + " vhost_name = \"some.host\"\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_NE(nullptr, config); +} + +TEST(configfile_server, port) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_server_port(_,54321)).Times(1); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "server:\n" + "{\n" + " port = 54321\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_NE(nullptr, config); +} + +TEST(configfile_server, tls_certificate) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_server_cert(_, StrEq("/path/to/cert.pem"))).Times(1); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "server:\n" + "{\n" + " tls:\n" + " {\n" + " certificate = \"/path/to/cert.pem\"\n" + " }\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_NE(nullptr, config); +} + +TEST(configfile_server, tls_key) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_server_key(_,StrEq("/path/to/key.pem"))).Times(1); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "server:\n" + "{\n" + " tls:\n" + " {\n" + " key = \"/path/to/key.pem\"\n" + " }\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_NE(nullptr, config); +} + +TEST(configfile_server, document_root) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_server_document_root(_,StrEq("/var/www"))).Times(1); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "server:\n" + "{\n" + " document_root = \"/var/www\"\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_NE(nullptr, config); +} diff --git a/test/config/configfile_user.cc b/test/config/configfile_user.cc new file mode 100644 index 0000000..0a494ce --- /dev/null +++ b/test/config/configfile_user.cc @@ -0,0 +1,82 @@ +#include + +#include "webfused/config/factory.h" +#include "webfused/log/logger.h" +#include "webfused/log/log.h" +#include "mock/logger.hpp" +#include "mock/config_builder.hpp" +#include "mock/libconfig.hpp" + +using ::testing::_; +using ::testing::Return; +using ::testing::StrictMock; +using ::testing::StrEq; +using ::webfused_test::MockLogger; +using ::webfused_test::MockConfigBuilder; +using ::webfused_test::MockLibConfig; + +TEST(configfile_user, set_user) +{ + MockLogger logger; + EXPECT_CALL(logger, log(_, _, _)).Times(0); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_set_user(_, _, _)).Times(1); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "user:\n" + "{\n" + " name = \"webfused\"\n" + " group = \"webfused\"\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_NE(nullptr, config); +} + +TEST(configfile_user, set_user_fail_missing_name) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_user(_, _, _)).Times(0); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "user:\n" + "{\n" + " group = \"webfused\"\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_user, set_user_fail_missing_group) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + EXPECT_CALL(builder, wfd_config_set_user(_, _, _)).Times(0); + + char const config_text[] = + "version = { major = 1, minor = 0 }\n" + "user:\n" + "{\n" + " name = \"webfused\"\n" + "}\n" + ; + struct wfd_config * config = wfd_config_load_string(config_text); + ASSERT_EQ(nullptr, config); +} diff --git a/test/config/configfile_version.cc b/test/config/configfile_version.cc new file mode 100644 index 0000000..95e350f --- /dev/null +++ b/test/config/configfile_version.cc @@ -0,0 +1,110 @@ +#include + +#include "webfused/config/factory.h" +#include "webfused/log/logger.h" +#include "webfused/log/log.h" +#include "mock/logger.hpp" +#include "mock/config_builder.hpp" +#include "mock/libconfig.hpp" + +using ::testing::_; +using ::testing::Return; +using ::testing::StrictMock; +using ::testing::StrEq; +using ::webfused_test::MockLogger; +using ::webfused_test::MockConfigBuilder; +using ::webfused_test::MockLibConfig; + +TEST(configfile_version, invalid_major_version_too_low) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + + char const too_low[] = "version = { major = 0, minor = 0 }\n"; + + struct wfd_config * config= wfd_config_load_string(too_low); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_version, invalid_major_version_too_high) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + + char const too_high[] = "version = { major = 2, minor = 0 }\n"; + + struct wfd_config * config = wfd_config_load_string(too_high); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_version, invalid_missing_major_version) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + + char const too_high[] = "version = { minor = 0 }\n"; + + struct wfd_config * config = wfd_config_load_string(too_high); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_version, invalid_missing_minor_version) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); + + char const too_high[] = "version = { major = 1 }\n"; + + struct wfd_config * config = wfd_config_load_string(too_high); + ASSERT_EQ(nullptr, config); +} + +TEST(configfile_version, valid_older_minor) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_INFO, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + + char const valid[] = "version = { major = 1, minor = -1 }\n"; + + struct wfd_config * config = wfd_config_load_string(valid); + ASSERT_NE(nullptr, config); +} + +TEST(configfile_version, valid_newer_minor) +{ + MockLogger logger; + EXPECT_CALL(logger, log(WFD_LOGLEVEL_WARN, _, _)).Times(1); + EXPECT_CALL(logger, onclose()).Times(1); + + StrictMock builder; + EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); + + char const valid[] = "version = { major = 1, minor = 1 }\n"; + + struct wfd_config * config = wfd_config_load_string(valid); + ASSERT_NE(nullptr, config); +} diff --git a/test/config/factory.cc b/test/config/factory.cc deleted file mode 100644 index 28b52bc..0000000 --- a/test/config/factory.cc +++ /dev/null @@ -1,711 +0,0 @@ -#include - -#include "webfused/config/factory.h" -#include "webfused/log/logger.h" -#include "webfused/log/log.h" -#include "mock/logger.hpp" -#include "mock/config_builder.hpp" -#include "mock/libconfig.hpp" - -using ::testing::_; -using ::testing::Return; -using ::testing::StrictMock; -using ::testing::StrEq; -using ::webfused_test::MockLogger; -using ::webfused_test::MockConfigBuilder; -using ::webfused_test::MockLibConfig; - -TEST(config, is_loadable) -{ - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_set_logger(_,_,_,_)).Times(1).WillOnce(Return(true)); - EXPECT_CALL(builder, wfd_config_set_server_vhostname(_,StrEq("localhost"))).Times(1); - EXPECT_CALL(builder, wfd_config_set_server_port(_,8080)).Times(1); - EXPECT_CALL(builder, wfd_config_add_auth_provider(_,_, _)).Times(1).WillOnce(Return(true)); - EXPECT_CALL(builder, wfd_config_add_filesystem(_,_,_)).Times(1).WillOnce(Return(true)); - - struct wfd_config * config = wfd_config_load_file("webfused.conf"); - ASSERT_NE(nullptr, config); -} - -TEST(config, minimal_config) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - - char const minimal[] = "version = { major = 1, minor = 0 }\n"; - struct wfd_config * config = wfd_config_load_string(minimal); - ASSERT_NE(nullptr, config); -} - -TEST(config, invalid_config) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(0); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(0); - - char const syntax_error[] = "version.major = 1\n"; - - wfd_config * config = wfd_config_load_string(syntax_error); - ASSERT_EQ(nullptr, config); -} - -TEST(config, invalid_config_file) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(0); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(0); - - struct wfd_config * config = wfd_config_load_file("invalid.conf"); - ASSERT_EQ(nullptr, config); -} - -TEST(config, invalid_major_version_too_low) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - - char const too_low[] = "version = { major = 0, minor = 0 }\n"; - - struct wfd_config * config= wfd_config_load_string(too_low); - ASSERT_EQ(nullptr, config); -} - -TEST(config, invalid_major_version_too_high) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - - char const too_high[] = "version = { major = 2, minor = 0 }\n"; - - struct wfd_config * config = wfd_config_load_string(too_high); - ASSERT_EQ(nullptr, config); -} - -TEST(config, invalid_missing_major_version) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - - char const too_high[] = "version = { minor = 0 }\n"; - - struct wfd_config * config = wfd_config_load_string(too_high); - ASSERT_EQ(nullptr, config); -} - -TEST(config, invalid_missing_minor_version) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - - char const too_high[] = "version = { major = 1 }\n"; - - struct wfd_config * config = wfd_config_load_string(too_high); - ASSERT_EQ(nullptr, config); -} - -TEST(config, valid_older_minor) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_INFO, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - - char const valid[] = "version = { major = 1, minor = -1 }\n"; - - struct wfd_config * config = wfd_config_load_string(valid); - ASSERT_NE(nullptr, config); -} - -TEST(config, valid_newer_minor) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_WARN, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - - char const valid[] = "version = { major = 1, minor = 1 }\n"; - - struct wfd_config * config = wfd_config_load_string(valid); - ASSERT_NE(nullptr, config); -} - -TEST(config, vhost_name) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_set_server_vhostname(_,StrEq("some.host"))).Times(1); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "server:\n" - "{\n" - " vhost_name = \"some.host\"\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_NE(nullptr, config); -} - -TEST(config, port) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_set_server_port(_,54321)).Times(1); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "server:\n" - "{\n" - " port = 54321\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_NE(nullptr, config); -} - -TEST(config, tls_certificate) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_set_server_cert(_, StrEq("/path/to/cert.pem"))).Times(1); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "server:\n" - "{\n" - " tls:\n" - " {\n" - " certificate = \"/path/to/cert.pem\"\n" - " }\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_NE(nullptr, config); -} - -TEST(config, tls_key) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_set_server_key(_,StrEq("/path/to/key.pem"))).Times(1); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "server:\n" - "{\n" - " tls:\n" - " {\n" - " key = \"/path/to/key.pem\"\n" - " }\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_NE(nullptr, config); -} - -TEST(config, document_root) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_set_server_document_root(_,StrEq("/var/www"))).Times(1); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "server:\n" - "{\n" - " document_root = \"/var/www\"\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_NE(nullptr, config); -} - -TEST(config, authentication) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_add_auth_provider(_,_, _)).Times(1).WillOnce(Return(true)); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "authentication:\n" - "(\n" - " {\n" - " provider = \"test\"\n" - " settings: { }\n" - " }\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_NE(nullptr, config); -} - -TEST(config, failed_create_authenticator) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_add_auth_provider(_,_, _)).Times(1).WillOnce(Return(false)); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "authentication:\n" - "(\n" - " {\n" - " provider = \"test\"\n" - " settings: { }\n" - " }\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, failed_missing_auth_provider) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "authentication:\n" - "(\n" - " {\n" - " settings: { }\n" - " }\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, failed_missing_auth_settings) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "authentication:\n" - "(\n" - " {\n" - " provider = \"test\"\n" - " }\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, failed_auth_settings_get_elem) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - - MockLibConfig libconfig; - EXPECT_CALL(libconfig, config_setting_get_elem(_,_)).Times(1).WillOnce(Return(nullptr)); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "authentication:\n" - "(\n" - " {\n" - " provider = \"test\"\n" - " settings: { }\n" - " }\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, filesystems) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(1).WillOnce(Return(true)); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "filesystems:\n" - "(\n" - " {name = \"foo\", mount_point = \"/tmp/test\" }\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_NE(nullptr, config); -} - -TEST(config, filesystems_empty) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "filesystems:\n" - "(\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_NE(nullptr, config); -} - -TEST(config, filesystems_failed_add) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(1).WillOnce(Return(false)); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "filesystems:\n" - "(\n" - " {name = \"foo\", mount_point = \"/tmp/test\" }\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, filesystems_failed_missing_name) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "filesystems:\n" - "(\n" - " {mount_point = \"/tmp/test\" }\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, filesystems_failed_missing_mountpoint) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "filesystems:\n" - "(\n" - " {name = \"foo\"}\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, filesystems_failed_missing_elem) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_add_filesystem(_,_, _)).Times(0); - - MockLibConfig libconfig; - EXPECT_CALL(libconfig, config_setting_get_elem(_,_)).Times(1).WillOnce(Return(nullptr)); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "filesystems:\n" - "(\n" - " {name = \"foo\", mount_point = \"/tmp/test\" }\n" - ")\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, log) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(1).WillOnce(Return(true)); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "log:\n" - "{\n" - " provider = \"stderr\"\n" - " level = \"all\"\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_NE(nullptr, config); -} - -TEST(config, log_fail_set_logger) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(1).WillOnce(Return(false)); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "log:\n" - "{\n" - " provider = \"stderr\"\n" - " level = \"all\"\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, log_fail_missing_provider) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(0); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "log:\n" - "{\n" - " level = \"all\"\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, log_fail_missing_level) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_set_logger(_,_, _, _)).Times(0); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "log:\n" - "{\n" - " provider = \"stderr\"\n" - " level = \"fancy\"\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, log_fail_invalid_level) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_set_logger(_, _, _, _)).Times(0); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "log:\n" - "{\n" - " provider = \"stderr\"\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, set_user) -{ - MockLogger logger; - EXPECT_CALL(logger, log(_, _, _)).Times(0); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_set_user(_, _, _)).Times(1); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "user:\n" - "{\n" - " name = \"webfused\"\n" - " group = \"webfused\"\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_NE(nullptr, config); -} - -TEST(config, set_user_fail_missing_name) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_set_user(_, _, _)).Times(0); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "user:\n" - "{\n" - " group = \"webfused\"\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} - -TEST(config, set_user_fail_missing_group) -{ - MockLogger logger; - EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1); - EXPECT_CALL(logger, onclose()).Times(1); - - StrictMock builder; - EXPECT_CALL(builder, wfd_config_create).Times(1).WillOnce(Return(builder.getBuilder())); - EXPECT_CALL(builder, wfd_config_dispose(_)).Times(1); - EXPECT_CALL(builder, wfd_config_set_user(_, _, _)).Times(0); - - char const config_text[] = - "version = { major = 1, minor = 0 }\n" - "user:\n" - "{\n" - " name = \"webfused\"\n" - "}\n" - ; - struct wfd_config * config = wfd_config_load_string(config_text); - ASSERT_EQ(nullptr, config); -} From 6492b0fd51c4525f4a96e2bb999e6069238f123d Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 20 Mar 2020 16:27:48 +0100 Subject: [PATCH 13/13] split docu --- README.md | 271 ++------------------------------------------------ doc/build.md | 95 ++++++++++++++++++ doc/config.md | 168 +++++++++++++++++++++++++++++++ 3 files changed, 273 insertions(+), 261 deletions(-) create mode 100644 doc/build.md create mode 100644 doc/config.md diff --git a/README.md b/README.md index d4075f3..950eb8a 100644 --- a/README.md +++ b/README.md @@ -1,269 +1,18 @@ [![Build Status](https://travis-ci.org/falk-werner/webfused.svg?branch=master)](https://travis-ci.org/falk-werner/webfused) [![codecov](https://codecov.io/gh/falk-werner/webfused/branch/master/graph/badge.svg)](https://codecov.io/gh/falk-werner/webfused) -# webfused +# Webfuse Daemon -Reference implementation of webfuse daemon. +Reference implementation of webfuse daemon (webfused). -## Build and run +## Further information -To install dependecies, see below. +- [Build Instructions](doc/build.md) +- [Configuration](doc/config.md) +- [Webfuse Protocol Specification](https://github.com/falk-werner/webfuse/blob/master/doc/protocol.md) - cd webfused - mkdir build - cd build - cmake .. - make - ./webfused -f webfused.conf +## Fellow Repositories -## Config file - -``` -version = { major = 1, minor = 0 } - -server: -{ - vhost_name = "localhost" - port = 8080 - - tls: - { - certificate = "/etc/webfused/cert.pem" - - key = "/etc/webfused/key.pem" - } - - document_root = "/var/www" -} - -authentication: -( - { - provider = "pam" - settings: - { - service_name = "webfused" - } - } -) - -filesystems: -( - {name = "test", mount_point = "/tmp/webfused" } -) - -log: -{ - provider: "syslog" - level: "warning" - settings: - { - ident = "webfused" - facility = "daemon" - log_pid = true - } -} - -user: -{ - name = "webfused" - group = "webfused" -} -``` - -### Version - -The version sections specifies the schema version of the config file. -Currently, there is only one schema version defined: 1.0 - -### Server - -| Setting | Type | Default value | Description | -| ------------- | ------ | ------------- | ------------------------ | -| vhostname | string | localhost | Name of the virtual host | -| port | int | 8080 | Port number of server | -| document_root | string | *-empty-* | Path of HTTP files | -| tls | object | *-empty-* | see below | - -When *document_root* is omitted, no HTTP files are served. - -#### TLS - -| Setting | Type | Default value | Description | -| ----------- | ------ | ------------- | ------------------------------------------- | -| certificate | string | *-empty-* | Path to servers own certificate (.pem file) | -| key | string | *-empty-* | Path to servers own private key (.pem file) | - -TLS is only activated, when both, *certificate* and *key* are specified. -Otherwise, plain websockes without TLS are used. - -### Authentication - -| Setting | Type | Default value | Description | -| -------- | ------ | ------------- | ----------------------------------------------- | -| provider | string | *-required-* | Name of the authentication provider (see below) | -| settings | object | *-empty-* | Provider specific settings (see below) - -Currently, the following providers are supported: - -- *file*: file based authentication -- *pam*: authentication based on Linux PAM - -### File Authenticaton Provider - -Allows authentication against a file containing username and password. - -| Setting | Type | Default value | Description | -| -------- | ------ | ------------- | ------------------------------- | -| file | string | *-required-* | Path to the authentication file | - -### PAM Authenticaton Provider - -Allows authentication using Linux PAM. - -| Setting | Type | Default value | Description | -| ------------ | ------ | ------------- | ---------------------- | -| service_name | string | webfused | PAM service identifier | - -### Filesystems - -Contains a list of file systems that can be provided by webfuse providers. - -| Setting | Type | Default value | Description | -| ----------- | ------ | ------------- | ---------------------------------- | -| name | string | *-required-* | Name of the filesystem | -| mount_point | string | *-required-* | Local path to mount the filesystem | - -### Log - -| Setting | Type | Default value | Description | -| ----------- | ------ | ------------- | -------------------------------------- | -| provider | string | *-required-* | Name of log provider (see below) | -| level | string | *-required-* | Log level (see below) | -| settings | object | *-empty-* | Provider specific settings (see below) | - -The following log levels are supported: - -- *none*: diabled logging -- *fatal*: log only fatal errors -- *error*: log all kind of errors -- *warn*: log errors and warnings -- *info*: log info messages, warnings and errors -- *debug*: log debug and info messages as well as warnings and errors -- *all*: log all kind of messages - -Currently, the following providers are available: - -- *stderr*: logs to console error output -- *syslog*: logs to syslog - -#### Stderr Logger - -This logger does not provide any settings. - -#### Syslog Logger - -| Setting | Type | Default value | Description | -| ----------- | ------ | ------------- | ------------------------------------------ | -| ident | string | webfused | Syslog ident (see syslog documentation) | -| facility | string | daemon | Syslog facility (see syslog documentation) | -| log_pid | bool | false | Add process ID to log messages | - -### User - -| Setting | Type | Default value | Description | -| ------- | ------ | ------------- | ------------------------------- | -| name | string | *-required-* | Name of the user to switch to. | -| group | string | *-required-* | Name of the group to switch to. | - -Webfuse daemon will not run as root. If started as root, webfuse daemon tries to -switch to *user* and *group* provided in config file. - -*Note*: user and group are not switched, when webfuse daemon is not started as root. - -## Dependencies - -- [webfuse](https://github.com/falk-werner/webfuse) - - [libfuse](https://github.com/libfuse/libfuse/) - - [libwebsockets](https://libwebsockets.org/) - - [jansson](https://github.com/akheron/jansson) -- [openssl](https://www.openssl.org/) -- [libconfig](https://hyperrealm.github.io/libconfig/) -- [linux-pam](http://www.linux-pam.org/) -- [Google Test](https://github.com/google/googletest) *(Test only)* - -### Installing dependencies - -#### libfuse - -To install libfuse, meson is needed. Please refer to [meson quick guide](https://mesonbuild.com/Quick-guide.html) for setup instructions. - - wget https://github.com/libfuse/libfuse/archive/fuse-3.9.0.tar.gz -O fuse.tar.gz - tar -xf fuse.tar.gz - cd libfuse-fuse-3.9.0 - mkdir .build - cd .build - meson .. - ninja - sudo ninja install - -#### libwebsockets - - wget https://github.com/warmcat/libwebsockets/archive/v3.2.0.tar.gz -O libwebsockets.tar.gz - tar -xf libwebsockets.tar.gz - cd libwebsockets-3.2.0 - mkdir .build - cd .build - cmake .. - make - sudo make install - -#### jansson - - wget https://github.com/akheron/jansson/archive/v2.12.tar.gz -O jansson.tar.gz - tar -xf jansson.tar.gz - cd jansson-2.12 - mkdir .build - cd .build - cmake .. - make - sudo make install - -#### openssl - - sudo apt update - sudo install openssl libssl-dev - -#### webfuse - - wget https://github.com/falk-werner/webfuse/archive/v0.2.0.tar.gz -O webfuse.tar.gz - tar -xf webfuse.tar.gz - cd webfuse-0.2.0 - mkdir .build - cd .build - cmake -DWITHOUT_TESTS=ON .. - make - sudo make install - -#### libconfig - - sudo apt update - sudo apt install libconfig-dev - -#### linux-pam - - sudo apt update - sudo apt install libpam0g-dev - -#### GoogleTest - -Installation of GoogleTest is optional webfuse library, but required to compile tests. - - wget -O gtest-1.10.0.tar.gz https://github.com/google/googletest/archive/release-1.10.0.tar.gz - tar -xf gtest-1.10.0.tar.gz - cd googletest-release-1.10.0 - mkdir .build - cd .build - cmake .. - make - sudo make install +- **[webfuse](https://github.com/falk-werner/webfuse)**: webfuse library +- **[webfuse-example](https://github.com/falk-werner/webfuse-example)**: example of webfuse +- **[webfuse-provider](https://github.com/falk-werner/webfuse-provider)**: reference implementation of webfuse provider diff --git a/doc/build.md b/doc/build.md new file mode 100644 index 0000000..5ff5afb --- /dev/null +++ b/doc/build.md @@ -0,0 +1,95 @@ +# Build Instructions + +To install dependecies, see below. + + cd webfused + mkdir build + cd build + cmake .. + make + ./webfused -f webfused.conf + +## Dependencies + +- [webfuse](https://github.com/falk-werner/webfuse) + - [libfuse](https://github.com/libfuse/libfuse/) + - [libwebsockets](https://libwebsockets.org/) + - [jansson](https://github.com/akheron/jansson) +- [openssl](https://www.openssl.org/) +- [libconfig](https://hyperrealm.github.io/libconfig/) +- [linux-pam](http://www.linux-pam.org/) +- [Google Test](https://github.com/google/googletest) *(Test only)* + +### libfuse + +To install libfuse, meson is needed. Please refer to [meson quick guide](https://mesonbuild.com/Quick-guide.html) for setup instructions. + + wget https://github.com/libfuse/libfuse/archive/fuse-3.9.0.tar.gz -O fuse.tar.gz + tar -xf fuse.tar.gz + cd libfuse-fuse-3.9.0 + mkdir .build + cd .build + meson .. + ninja + sudo ninja install + +### libwebsockets + + wget https://github.com/warmcat/libwebsockets/archive/v3.2.0.tar.gz -O libwebsockets.tar.gz + tar -xf libwebsockets.tar.gz + cd libwebsockets-3.2.0 + mkdir .build + cd .build + cmake .. + make + sudo make install + +### jansson + + wget https://github.com/akheron/jansson/archive/v2.12.tar.gz -O jansson.tar.gz + tar -xf jansson.tar.gz + cd jansson-2.12 + mkdir .build + cd .build + cmake .. + make + sudo make install + +### openssl + + sudo apt update + sudo install openssl libssl-dev + +### webfuse + + wget https://github.com/falk-werner/webfuse/archive/v0.2.0.tar.gz -O webfuse.tar.gz + tar -xf webfuse.tar.gz + cd webfuse-0.2.0 + mkdir .build + cd .build + cmake -DWITHOUT_TESTS=ON .. + make + sudo make install + +### libconfig + + sudo apt update + sudo apt install libconfig-dev + +### linux-pam + + sudo apt update + sudo apt install libpam0g-dev + +### GoogleTest + +Installation of GoogleTest is optional webfuse library, but required to compile tests. + + wget -O gtest-1.10.0.tar.gz https://github.com/google/googletest/archive/release-1.10.0.tar.gz + tar -xf gtest-1.10.0.tar.gz + cd googletest-release-1.10.0 + mkdir .build + cd .build + cmake .. + make + sudo make install diff --git a/doc/config.md b/doc/config.md new file mode 100644 index 0000000..81e4c56 --- /dev/null +++ b/doc/config.md @@ -0,0 +1,168 @@ +# Configuration + +A config file is used to configure webfuse daemon. + +## Config file + +``` +version = { major = 1, minor = 0 } + +server: +{ + vhost_name = "localhost" + port = 8080 + + tls: + { + certificate = "/etc/webfused/cert.pem" + + key = "/etc/webfused/key.pem" + } + + document_root = "/var/www" +} + +authentication: +( + { + provider = "pam" + settings: + { + service_name = "webfused" + } + } +) + +filesystems: +( + {name = "test", mount_point = "/tmp/webfused" } +) + +log: +{ + provider: "syslog" + level: "warning" + settings: + { + ident = "webfused" + facility = "daemon" + log_pid = true + } +} + +user: +{ + name = "webfused" + group = "webfused" +} +``` + +## Version + +The version sections specifies the schema version of the config file. +Currently, there is only one schema version defined: 1.0 + +## Server + +| Setting | Type | Default value | Description | +| ------------- | ------ | ------------- | ------------------------ | +| vhostname | string | localhost | Name of the virtual host | +| port | int | 8080 | Port number of server | +| document_root | string | *-empty-* | Path of HTTP files | +| tls | object | *-empty-* | see below | + +When *document_root* is omitted, no HTTP files are served. + +### TLS + +| Setting | Type | Default value | Description | +| ----------- | ------ | ------------- | ------------------------------------------- | +| certificate | string | *-empty-* | Path to servers own certificate (.pem file) | +| key | string | *-empty-* | Path to servers own private key (.pem file) | + +TLS is only activated, when both, *certificate* and *key* are specified. +Otherwise, plain websockes without TLS are used. + +## Authentication + +| Setting | Type | Default value | Description | +| -------- | ------ | ------------- | ----------------------------------------------- | +| provider | string | *-required-* | Name of the authentication provider (see below) | +| settings | object | *-empty-* | Provider specific settings (see below) + +Currently, the following providers are supported: + +- *file*: file based authentication +- *pam*: authentication based on Linux PAM + +## File Authenticaton Provider + +Allows authentication against a file containing username and password. + +| Setting | Type | Default value | Description | +| -------- | ------ | ------------- | ------------------------------- | +| file | string | *-required-* | Path to the authentication file | + +## PAM Authenticaton Provider + +Allows authentication using Linux PAM. + +| Setting | Type | Default value | Description | +| ------------ | ------ | ------------- | ---------------------- | +| service_name | string | webfused | PAM service identifier | + +## Filesystems + +Contains a list of file systems that can be provided by webfuse providers. + +| Setting | Type | Default value | Description | +| ----------- | ------ | ------------- | ---------------------------------- | +| name | string | *-required-* | Name of the filesystem | +| mount_point | string | *-required-* | Local path to mount the filesystem | + +## Log + +| Setting | Type | Default value | Description | +| ----------- | ------ | ------------- | -------------------------------------- | +| provider | string | *-required-* | Name of log provider (see below) | +| level | string | *-required-* | Log level (see below) | +| settings | object | *-empty-* | Provider specific settings (see below) | + +The following log levels are supported: + +- *none*: diabled logging +- *fatal*: log only fatal errors +- *error*: log all kind of errors +- *warn*: log errors and warnings +- *info*: log info messages, warnings and errors +- *debug*: log debug and info messages as well as warnings and errors +- *all*: log all kind of messages + +Currently, the following providers are available: + +- *stderr*: logs to console error output +- *syslog*: logs to syslog + +### Stderr Logger + +This logger does not provide any settings. + +### Syslog Logger + +| Setting | Type | Default value | Description | +| ----------- | ------ | ------------- | ------------------------------------------ | +| ident | string | webfused | Syslog ident (see syslog documentation) | +| facility | string | daemon | Syslog facility (see syslog documentation) | +| log_pid | bool | false | Add process ID to log messages | + +## User + +| Setting | Type | Default value | Description | +| ------- | ------ | ------------- | ------------------------------- | +| name | string | *-required-* | Name of the user to switch to. | +| group | string | *-required-* | Name of the group to switch to. | + +Webfuse daemon will not run as root. If started as root, webfuse daemon tries to +switch to *user* and *group* provided in config file. + +*Note*: user and group are not switched, when webfuse daemon is not started as root.