From 3a43c8b0f26280b55e15f40122c86ed17222c0c7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 16 Sep 2020 00:09:40 +0100 Subject: Concrete entcache types Allows specific constructor for group cache that takes a user cache --- netfs/daemon/daemonService.cpp | 2 +- netfs/fuse/fuseMappersImpl.cpp | 2 +- netfs/lib/defaultMapper.cpp | 2 +- netfs/lib/entCache.h | 16 +++++++++- netfs/lib/entCache.impl.h | 11 ++++--- netfs/unittests/testLib.cpp | 67 ++++++++++++++++++++++++++++++------------ 6 files changed, 72 insertions(+), 28 deletions(-) diff --git a/netfs/daemon/daemonService.cpp b/netfs/daemon/daemonService.cpp index 6ffcf71..38d40c7 100644 --- a/netfs/daemon/daemonService.cpp +++ b/netfs/daemon/daemonService.cpp @@ -5,7 +5,7 @@ #include ServiceServer::ServiceServer(NetFS::Daemon::ConfigurationPtr c) : - userLookup(std::make_shared>()), groupLookup(std::make_shared>()), + userLookup(std::make_shared()), groupLookup(std::make_shared(userLookup)), config(std::move(c)) { } diff --git a/netfs/fuse/fuseMappersImpl.cpp b/netfs/fuse/fuseMappersImpl.cpp index 9a99eca..9e82b74 100644 --- a/netfs/fuse/fuseMappersImpl.cpp +++ b/netfs/fuse/fuseMappersImpl.cpp @@ -3,7 +3,7 @@ namespace NetFS::Client { HideUnknownMapperImpl::HideUnknownMapperImpl() : - HideUnknownMapperImpl(std::make_shared>(), std::make_shared>()) + users(std::make_shared()), groups(std::make_shared(users)) { } diff --git a/netfs/lib/defaultMapper.cpp b/netfs/lib/defaultMapper.cpp index 27b20d3..865cca1 100644 --- a/netfs/lib/defaultMapper.cpp +++ b/netfs/lib/defaultMapper.cpp @@ -4,7 +4,7 @@ namespace NetFS::Mapping { DefaultMapper::DefaultMapper() : - DefaultMapper(std::make_shared>(), std::make_shared>()) + users(std::make_shared()), groups(std::make_shared(users)) { } diff --git a/netfs/lib/entCache.h b/netfs/lib/entCache.h index 7a9a3ef..e394b05 100644 --- a/netfs/lib/entCache.h +++ b/netfs/lib/entCache.h @@ -1,6 +1,7 @@ #ifndef ENTCACHE_H #define ENTCACHE_H +#include "entries.h" #include "entryResolver.h" #include #include @@ -26,7 +27,7 @@ public: }; protected: - void fillCache() const noexcept; + virtual void fillCache() const noexcept = 0; template[[nodiscard]] entry_ptr getEntryInternal(const key_t &) const noexcept; template[[nodiscard]] entry_ptr getEntryNoFill(const key_t &) const noexcept; @@ -36,4 +37,17 @@ protected: mutable time_t fillTime {0}; }; +class UserEntCache : public EntCache { + void fillCache() const noexcept override; +}; + +class GroupEntCache : public EntCache { +public: + GroupEntCache(EntryResolverPtr); + +private: + void fillCache() const noexcept override; + EntryResolverPtr users; +}; + #endif diff --git a/netfs/lib/entCache.impl.h b/netfs/lib/entCache.impl.h index 6523d32..1d7bbc7 100644 --- a/netfs/lib/entCache.impl.h +++ b/netfs/lib/entCache.impl.h @@ -53,9 +53,8 @@ EntCache::getEntryNoFill(const key_t & key) const noexcept const int BUFLEN = 8196; -template<> void -EntCache::fillCache() const noexcept +UserEntCache::fillCache() const noexcept { Lock(lock); setpwent(); @@ -70,9 +69,10 @@ EntCache::fillCache() const noexcept time(&fillTime); } -template<> +GroupEntCache::GroupEntCache(EntryResolverPtr u) : users(std::move(u)) { } + void -EntCache::fillCache() const noexcept +GroupEntCache::fillCache() const noexcept { Lock(lock); setgrent(); @@ -80,11 +80,10 @@ EntCache::fillCache() const noexcept idcache->clear(); struct group grpbuf { }, *grp; - EntCache instance; while (getgrent_r(&grpbuf, buf.data(), buf.size(), &grp) == 0) { auto g = std::make_shared(grp->gr_gid, grp->gr_name); for (auto member = grp->gr_mem; *member; member++) { - if (auto ent = instance.getEntry(*member)) { + if (auto ent = users->getEntry(*member)) { g->members.insert(ent->id); } } diff --git a/netfs/unittests/testLib.cpp b/netfs/unittests/testLib.cpp index 2985c93..d46b072 100644 --- a/netfs/unittests/testLib.cpp +++ b/netfs/unittests/testLib.cpp @@ -1,26 +1,20 @@ #define BOOST_TEST_MODULE TestNetFSLib #include #include +#include #include #include -struct TestEntry { - TestEntry(int i, std::string n) : id(i), name(std::move(n)) { } - - int id; - std::string name; +class TestEntCache : public EntCache { + void + fillCache() const noexcept override + { + Lock(lock); + idcache->insert(std::make_shared(1, "user1", 1)); + idcache->insert(std::make_shared(2, "user2", 1)); + idcache->insert(std::make_shared(3, "user3", 1)); + } }; -using TestEntCache = EntCache; - -template<> -void -EntCache::fillCache() const noexcept -{ - Lock(lock); - idcache->insert(std::make_shared(1, "user1")); - idcache->insert(std::make_shared(2, "user2")); - idcache->insert(std::make_shared(3, "user3")); -} const auto GoodIds = boost::unit_test::data::make({1, 2, 3}); const auto BadIds = boost::unit_test::data::make({0, -1, 10}); @@ -67,7 +61,7 @@ BOOST_AUTO_TEST_CASE(group_membership) } // These tests make some assumptions about local system users -BOOST_FIXTURE_TEST_CASE(usercache, EntCache) +BOOST_FIXTURE_TEST_CASE(usercache, UserEntCache) { auto root = getEntry(0); BOOST_REQUIRE(root); @@ -79,7 +73,11 @@ BOOST_FIXTURE_TEST_CASE(usercache, EntCache) BOOST_REQUIRE_EQUAL(root, rootByName); } -BOOST_FIXTURE_TEST_CASE(groupcache, EntCache) +struct TestGroupEntCache : public GroupEntCache { + TestGroupEntCache() : GroupEntCache(std::make_shared()) { } +}; + +BOOST_FIXTURE_TEST_CASE(groupcache, TestGroupEntCache) { auto root = getEntry(0); BOOST_REQUIRE(root); @@ -90,3 +88,36 @@ BOOST_FIXTURE_TEST_CASE(groupcache, EntCache) auto rootByName = getEntry(root->name); BOOST_REQUIRE_EQUAL(root, rootByName); } + +BOOST_FIXTURE_TEST_SUITE(dm, NetFS::Mapping::DefaultMapper); + +BOOST_AUTO_TEST_CASE(good_maptransport) +{ + auto fs = mapTransport("root", "root"); + BOOST_CHECK_EQUAL(0, fs.uid); + BOOST_CHECK_EQUAL(0, fs.gid); + BOOST_CHECK_EQUAL(0, fs.mask); +} + +BOOST_AUTO_TEST_CASE(good_mapfs) +{ + auto tn = mapFileSystem(0, 0); + BOOST_CHECK_EQUAL("root", tn.username); + BOOST_CHECK_EQUAL("root", tn.groupname); +} + +BOOST_AUTO_TEST_CASE(bad_maptransport) +{ + BOOST_REQUIRE_THROW(mapTransport("root", ""), NetFS::SystemError); + BOOST_REQUIRE_THROW(mapTransport("", "root"), NetFS::SystemError); + BOOST_REQUIRE_THROW(mapTransport("", ""), NetFS::SystemError); +} + +BOOST_AUTO_TEST_CASE(bad_mapfilesystem) +{ + BOOST_REQUIRE_THROW(mapFileSystem(0, -1), NetFS::SystemError); + BOOST_REQUIRE_THROW(mapFileSystem(-1, 0), NetFS::SystemError); + BOOST_REQUIRE_THROW(mapFileSystem(-1, -1), NetFS::SystemError); +} + +BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3