diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2020-09-16 00:09:40 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2020-09-16 00:09:40 +0100 |
commit | 3a43c8b0f26280b55e15f40122c86ed17222c0c7 (patch) | |
tree | da5ace4d759bc14a5b5836710365dfec2c1a718c | |
parent | Test EntCache basics for User and Group (diff) | |
download | netfs-3a43c8b0f26280b55e15f40122c86ed17222c0c7.tar.bz2 netfs-3a43c8b0f26280b55e15f40122c86ed17222c0c7.tar.xz netfs-3a43c8b0f26280b55e15f40122c86ed17222c0c7.zip |
Concrete entcache types
Allows specific constructor for group cache that takes a user cache
-rw-r--r-- | netfs/daemon/daemonService.cpp | 2 | ||||
-rw-r--r-- | netfs/fuse/fuseMappersImpl.cpp | 2 | ||||
-rw-r--r-- | netfs/lib/defaultMapper.cpp | 2 | ||||
-rw-r--r-- | netfs/lib/entCache.h | 16 | ||||
-rw-r--r-- | netfs/lib/entCache.impl.h | 11 | ||||
-rw-r--r-- | 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 <safeMapFind.h> ServiceServer::ServiceServer(NetFS::Daemon::ConfigurationPtr c) : - userLookup(std::make_shared<EntCache<User>>()), groupLookup(std::make_shared<EntCache<Group>>()), + userLookup(std::make_shared<UserEntCache>()), groupLookup(std::make_shared<GroupEntCache>(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<EntCache<User>>(), std::make_shared<EntCache<Group>>()) + users(std::make_shared<UserEntCache>()), groups(std::make_shared<GroupEntCache>(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<EntCache<User>>(), std::make_shared<EntCache<Group>>()) + users(std::make_shared<UserEntCache>()), groups(std::make_shared<GroupEntCache>(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 <c++11Helpers.h> #include <shared_mutex> @@ -26,7 +27,7 @@ public: }; protected: - void fillCache() const noexcept; + virtual void fillCache() const noexcept = 0; template<class key_t>[[nodiscard]] entry_ptr getEntryInternal(const key_t &) const noexcept; template<class key_t>[[nodiscard]] entry_ptr getEntryNoFill(const key_t &) const noexcept; @@ -36,4 +37,17 @@ protected: mutable time_t fillTime {0}; }; +class UserEntCache : public EntCache<User> { + void fillCache() const noexcept override; +}; + +class GroupEntCache : public EntCache<Group> { +public: + GroupEntCache(EntryResolverPtr<User>); + +private: + void fillCache() const noexcept override; + EntryResolverPtr<User> 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<entry_t>::getEntryNoFill(const key_t & key) const noexcept const int BUFLEN = 8196; -template<> void -EntCache<User>::fillCache() const noexcept +UserEntCache::fillCache() const noexcept { Lock(lock); setpwent(); @@ -70,9 +69,10 @@ EntCache<User>::fillCache() const noexcept time(&fillTime); } -template<> +GroupEntCache::GroupEntCache(EntryResolverPtr<User> u) : users(std::move(u)) { } + void -EntCache<Group>::fillCache() const noexcept +GroupEntCache::fillCache() const noexcept { Lock(lock); setgrent(); @@ -80,11 +80,10 @@ EntCache<Group>::fillCache() const noexcept idcache->clear(); struct group grpbuf { }, *grp; - EntCache<User> instance; while (getgrent_r(&grpbuf, buf.data(), buf.size(), &grp) == 0) { auto g = std::make_shared<Group>(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 <boost/test/data/test_case.hpp> #include <boost/test/unit_test.hpp> +#include <defaultMapper.h> #include <entCache.impl.h> #include <lockHelpers.h> -struct TestEntry { - TestEntry(int i, std::string n) : id(i), name(std::move(n)) { } - - int id; - std::string name; +class TestEntCache : public EntCache<User> { + void + fillCache() const noexcept override + { + Lock(lock); + idcache->insert(std::make_shared<User>(1, "user1", 1)); + idcache->insert(std::make_shared<User>(2, "user2", 1)); + idcache->insert(std::make_shared<User>(3, "user3", 1)); + } }; -using TestEntCache = EntCache<TestEntry>; - -template<> -void -EntCache<TestEntry>::fillCache() const noexcept -{ - Lock(lock); - idcache->insert(std::make_shared<TestEntry>(1, "user1")); - idcache->insert(std::make_shared<TestEntry>(2, "user2")); - idcache->insert(std::make_shared<TestEntry>(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<User>) +BOOST_FIXTURE_TEST_CASE(usercache, UserEntCache) { auto root = getEntry(0); BOOST_REQUIRE(root); @@ -79,7 +73,11 @@ BOOST_FIXTURE_TEST_CASE(usercache, EntCache<User>) BOOST_REQUIRE_EQUAL(root, rootByName); } -BOOST_FIXTURE_TEST_CASE(groupcache, EntCache<Group>) +struct TestGroupEntCache : public GroupEntCache { + TestGroupEntCache() : GroupEntCache(std::make_shared<UserEntCache>()) { } +}; + +BOOST_FIXTURE_TEST_CASE(groupcache, TestGroupEntCache) { auto root = getEntry(0); BOOST_REQUIRE(root); @@ -90,3 +88,36 @@ BOOST_FIXTURE_TEST_CASE(groupcache, EntCache<Group>) 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(); |