From 2128147f9631aeeabb708738a02a9cc995478dda Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 19 Nov 2014 17:03:06 +0100 Subject: [PATCH] Added some lstat tests --- src/test/fspp/fuse/FuseLstatTest.cpp | 51 +++++++++++++++++++++++----- src/test/fspp/fuse/FuseOpenTest.cpp | 2 +- src/test/testutils/FuseTest.cpp | 5 --- src/test/testutils/FuseTest.h | 16 ++++++++- 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/test/fspp/fuse/FuseLstatTest.cpp b/src/test/fspp/fuse/FuseLstatTest.cpp index 61467e55..a667c462 100644 --- a/src/test/fspp/fuse/FuseLstatTest.cpp +++ b/src/test/fspp/fuse/FuseLstatTest.cpp @@ -8,18 +8,51 @@ using namespace fspp::fuse; using ::testing::_; using ::testing::StrEq; +using ::testing::Action; +using ::testing::Invoke; -typedef FuseTest FuseLstatTest; +using std::vector; +using std::string; -TEST_F(FuseLstatTest, lstat) { - const char *filename = "/myfile"; - EXPECT_CALL(fsimpl, lstat(StrEq(filename), _)).WillOnce(ReturnIsFileStat); +class FuseLstatTest: public FuseTest { +public: + struct stat LstatPath(const string &path) { + auto fs = TestFS(); - auto fs = TestFS(); + auto realpath = fs->mountDir() / path; + struct stat stat; + ::lstat(realpath.c_str(), &stat); - auto realpath = fs->mountDir() / filename; - struct stat stat; - ::lstat(realpath.c_str(), &stat); + return stat; + } +}; - EXPECT_TRUE(S_ISREG(stat.st_mode)); + +TEST_F(FuseLstatTest, PathParameterIsCorrectRoot) { + EXPECT_CALL(fsimpl, lstat(StrEq("/"), _)).Times(1); + LstatPath("/"); +} + +TEST_F(FuseLstatTest, PathParameterIsCorrectSimpleFile) { + EXPECT_CALL(fsimpl, lstat(StrEq("/myfile"), _)).Times(1); + LstatPath("/myfile"); +} + +TEST_F(FuseLstatTest, PathParameterIsCorrectSimpleDir) { + EXPECT_CALL(fsimpl, lstat(StrEq("/mydir"), _)).Times(1); + LstatPath("/mydir/"); +} + +TEST_F(FuseLstatTest, PathParameterIsCorrectNestedFile) { + ReturnIsDirOnLstat("/mydir"); + ReturnIsDirOnLstat("/mydir/mydir2"); + EXPECT_CALL(fsimpl, lstat(StrEq("/mydir/mydir2/myfile"), _)).Times(1); + LstatPath("/mydir/mydir2/myfile"); +} + +TEST_F(FuseLstatTest, PathParameterIsCorrectNestedDir) { + ReturnIsDirOnLstat("/mydir"); + ReturnIsDirOnLstat("/mydir/mydir2"); + EXPECT_CALL(fsimpl, lstat(StrEq("/mydir/mydir2/mydir3"), _)).Times(1); + LstatPath("/mydir/mydir2/mydir3/"); } diff --git a/src/test/fspp/fuse/FuseOpenTest.cpp b/src/test/fspp/fuse/FuseOpenTest.cpp index 7271654c..569de0b6 100644 --- a/src/test/fspp/fuse/FuseOpenTest.cpp +++ b/src/test/fspp/fuse/FuseOpenTest.cpp @@ -13,7 +13,7 @@ typedef FuseTest FuseOpenTest; TEST_F(FuseOpenTest, openFile) { const char *filename = "/myfile"; - EXPECT_CALL(fsimpl, lstat(StrEq(filename), _)).WillOnce(ReturnIsFileStat); + ReturnIsFileOnLstat(filename); EXPECT_CALL(fsimpl, openFile(StrEq(filename), OpenFlagsEq(O_RDWR))) .Times(1); diff --git a/src/test/testutils/FuseTest.cpp b/src/test/testutils/FuseTest.cpp index 0ec45fef..014c2178 100644 --- a/src/test/testutils/FuseTest.cpp +++ b/src/test/testutils/FuseTest.cpp @@ -2,8 +2,3 @@ using ::testing::Action; using ::testing::Invoke; - -Action FuseTest::ReturnIsFileStat = - Invoke([](const char*, struct ::stat* result) { - result->st_mode = S_IFREG; - }); diff --git a/src/test/testutils/FuseTest.h b/src/test/testutils/FuseTest.h index 0271f714..2390282d 100644 --- a/src/test/testutils/FuseTest.h +++ b/src/test/testutils/FuseTest.h @@ -121,7 +121,21 @@ public: MockFilesystem fsimpl; - static ::testing::Action ReturnIsFileStat; + void ReturnIsFileOnLstat(const bf::path &path) { + EXPECT_CALL(fsimpl, lstat(::testing::StrEq(path.c_str()), ::testing::_)).WillRepeatedly( + ::testing::Invoke([](const char*, struct ::stat* result) { + result->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH; + result->st_nlink = 1; + })); + } + + void ReturnIsDirOnLstat(const bf::path &path) { + EXPECT_CALL(fsimpl, lstat(::testing::StrEq(path.c_str()), ::testing::_)).WillRepeatedly( + ::testing::Invoke([](const char*, struct ::stat* result) { + result->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH; + result->st_nlink = 1; + })); + } }; MATCHER_P(OpenFlagsEq, expectedFlags, "") {