From 2b91f159cf278850444645f910393435b761b169 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 16 Feb 2020 17:33:11 +0100 Subject: [PATCH] added unit tests of uuid_mountpoint_factory --- cmake/unit_tests.cmake | 2 + .../adapter/impl/uuid_mountpoint_factory.c | 30 ++++++++-- .../adapter/impl/uuid_mountpoint_factory.h | 4 +- test/adapter/test_uuid_mountpoint.cc | 38 ++---------- test/adapter/test_uuid_mountpoint_factory.cc | 60 +++++++++++++++++++ test/file_utils.cc | 38 ++++++++++++ test/file_utils.hpp | 17 ++++++ 7 files changed, 148 insertions(+), 41 deletions(-) create mode 100644 test/adapter/test_uuid_mountpoint_factory.cc create mode 100644 test/file_utils.cc create mode 100644 test/file_utils.hpp diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index 1301b15..933d78e 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -8,6 +8,7 @@ pkg_check_modules(GMOCK gmock) add_executable(alltests test/tempdir.cc + test/file_utils.cc test/msleep.cc test/die_if.cc test/mock_authenticator.cc @@ -29,6 +30,7 @@ add_executable(alltests test/adapter/test_authenticators.cc test/adapter/test_mountpoint.cc test/adapter/test_uuid_mountpoint.cc + test/adapter/test_uuid_mountpoint_factory.cc test/adapter/test_fuse_req.cc test/adapter/jsonrpc/test_util.cc test/adapter/jsonrpc/test_is_request.cc diff --git a/lib/webfuse/adapter/impl/uuid_mountpoint_factory.c b/lib/webfuse/adapter/impl/uuid_mountpoint_factory.c index f982f5b..1f28e64 100644 --- a/lib/webfuse/adapter/impl/uuid_mountpoint_factory.c +++ b/lib/webfuse/adapter/impl/uuid_mountpoint_factory.c @@ -7,20 +7,36 @@ #include #include +#include struct wf_impl_uuid_mountpoint_factory { char * root_path; + bool root_created; }; void * wf_impl_uuid_mountpoint_factory_create( - char * root_path) + char const * root_path) { - mkdir(root_path, 0755); + struct wf_impl_uuid_mountpoint_factory * factory = NULL; + bool root_created = false; + + struct stat info; + int rc = stat(root_path, &info); + if ((0 != rc) || (!S_ISDIR(info.st_mode))) + { + rc = mkdir(root_path, 0755); + root_created = true; + } + + if (0 == rc) + { + factory = malloc(sizeof(struct wf_impl_uuid_mountpoint_factory)); + factory->root_path = strdup(root_path); + factory->root_created = root_created; + } - struct wf_impl_uuid_mountpoint_factory * factory = malloc(sizeof(struct wf_impl_uuid_mountpoint_factory)); - factory->root_path = strdup(root_path); return factory; } @@ -31,7 +47,11 @@ wf_impl_uuid_mountpoint_factory_dispose( { struct wf_impl_uuid_mountpoint_factory * factory = user_data; - rmdir(factory->root_path); + if (factory->root_created) + { + rmdir(factory->root_path); + } + free(factory->root_path); free(factory); } diff --git a/lib/webfuse/adapter/impl/uuid_mountpoint_factory.h b/lib/webfuse/adapter/impl/uuid_mountpoint_factory.h index a406597..52d5c57 100644 --- a/lib/webfuse/adapter/impl/uuid_mountpoint_factory.h +++ b/lib/webfuse/adapter/impl/uuid_mountpoint_factory.h @@ -10,14 +10,14 @@ struct wf_mountpoint; extern void * wf_impl_uuid_mountpoint_factory_create( - char * root_path); + char const * root_path); extern void wf_impl_uuid_mountpoint_factory_dispose( void * user_data); extern struct wf_mountpoint * -wf_impl_uuid_mountpoint_factory_create_mountpiont( +wf_impl_uuid_mountpoint_factory_create_mountpoint( char const * filesystem, void * user_data); diff --git a/test/adapter/test_uuid_mountpoint.cc b/test/adapter/test_uuid_mountpoint.cc index cfa4000..8bff760 100644 --- a/test/adapter/test_uuid_mountpoint.cc +++ b/test/adapter/test_uuid_mountpoint.cc @@ -1,46 +1,16 @@ #include -#include -#include -#include - #include "tempdir.hpp" +#include "file_utils.hpp" #include "webfuse_adapter.h" #include "webfuse/adapter/impl/uuid_mountpoint.h" #include using webfuse_test::TempDir; - -namespace -{ - bool is_dir(std::string const & path) - { - struct stat info; - int rc = stat(path.c_str(), &info); - - return (0 == rc) && (S_ISDIR(info.st_mode)); - } - - bool is_symlink(std::string const & path) - { - struct stat info; - int rc = lstat(path.c_str(), &info); - - return (0 == rc) && (S_ISLNK(info.st_mode)); - } - - bool is_same_path(std::string const & path, std::string const & other) - { - struct stat info; - int rc = stat(path.c_str(), &info); - - struct stat info_other; - int rc_other = stat(other.c_str(), &info_other); - - return (0 == rc) && (0 == rc_other) && (info.st_ino == info_other.st_ino); - } -} +using webfuse_test::is_dir; +using webfuse_test::is_symlink; +using webfuse_test::is_same_path; TEST(uuid_mountpoint, create) { diff --git a/test/adapter/test_uuid_mountpoint_factory.cc b/test/adapter/test_uuid_mountpoint_factory.cc new file mode 100644 index 0000000..abb0b27 --- /dev/null +++ b/test/adapter/test_uuid_mountpoint_factory.cc @@ -0,0 +1,60 @@ +#include +#include "webfuse_adapter.h" +#include "webfuse/adapter/impl/uuid_mountpoint_factory.h" +#include "tempdir.hpp" +#include "file_utils.hpp" + +using webfuse_test::TempDir; +using webfuse_test::is_dir; + +TEST(uuid_mountpoint_factory, create_existing_dir) +{ + TempDir temp("uuid_mountpoint_factory"); + + void * factory = wf_impl_uuid_mountpoint_factory_create(temp.path()); + ASSERT_NE(nullptr, factory); + ASSERT_TRUE(is_dir(temp.path())); + + wf_mountpoint * mountpoint = wf_impl_uuid_mountpoint_factory_create_mountpoint("dummy", factory); + std::string path = wf_mountpoint_get_path(mountpoint); + ASSERT_NE(nullptr, factory); + ASSERT_TRUE(is_dir(path)); + + wf_mountpoint_dispose(mountpoint); + ASSERT_FALSE(is_dir(path)); + + wf_impl_uuid_mountpoint_factory_dispose(factory); + // keep dir not created by factory + ASSERT_TRUE(is_dir(temp.path())); +} + +TEST(uuid_mountpoint_factory, create_nonexisting_dir) +{ + TempDir temp("uuid_mountpoint_factory"); + std::string root_path = std::string(temp.path()) + "/root"; + + void * factory = wf_impl_uuid_mountpoint_factory_create(root_path.c_str()); + ASSERT_NE(nullptr, factory); + ASSERT_TRUE(is_dir(root_path)); + + wf_mountpoint * mountpoint = wf_impl_uuid_mountpoint_factory_create_mountpoint("dummy", factory); + std::string path = wf_mountpoint_get_path(mountpoint); + ASSERT_NE(nullptr, factory); + ASSERT_TRUE(is_dir(path)); + + wf_mountpoint_dispose(mountpoint); + ASSERT_FALSE(is_dir(path)); + + wf_impl_uuid_mountpoint_factory_dispose(factory); + // remove dir, created by factory + ASSERT_FALSE(is_dir(root_path)); +} + +TEST(uuid_mountpoint_factory, fail_to_created_nested_dir) +{ + TempDir temp("uuid_mountpoint_factory"); + std::string root_path = std::string(temp.path()) + "/nested/root"; + + void * factory = wf_impl_uuid_mountpoint_factory_create(root_path.c_str()); + ASSERT_EQ(nullptr, factory); +} \ No newline at end of file diff --git a/test/file_utils.cc b/test/file_utils.cc new file mode 100644 index 0000000..0d3ff81 --- /dev/null +++ b/test/file_utils.cc @@ -0,0 +1,38 @@ +#include "file_utils.hpp" + +#include +#include +#include + +namespace webfuse_test +{ + +bool is_dir(std::string const & path) +{ + struct stat info; + int rc = stat(path.c_str(), &info); + + return (0 == rc) && (S_ISDIR(info.st_mode)); +} + +bool is_symlink(std::string const & path) +{ + struct stat info; + int rc = lstat(path.c_str(), &info); + + return (0 == rc) && (S_ISLNK(info.st_mode)); +} + +bool is_same_path(std::string const & path, std::string const & other) +{ + struct stat info; + int rc = stat(path.c_str(), &info); + + struct stat info_other; + int rc_other = stat(other.c_str(), &info_other); + + return (0 == rc) && (0 == rc_other) && (info.st_ino == info_other.st_ino); +} + + +} \ No newline at end of file diff --git a/test/file_utils.hpp b/test/file_utils.hpp new file mode 100644 index 0000000..0c8cbc1 --- /dev/null +++ b/test/file_utils.hpp @@ -0,0 +1,17 @@ +#ifndef WF_TEST_FILE_UTILS_HPP +#define WF_TEST_FILE_UTILS_HPP + +#include + +namespace webfuse_test +{ + +bool is_dir(std::string const & path); + +bool is_symlink(std::string const & path); + +bool is_same_path(std::string const & path, std::string const & other); + +} + +#endif