Written testcases for rmdir()

This commit is contained in:
Sebastian Messmer 2014-11-28 16:06:28 +01:00
parent 787896ef58
commit 6b4497bab2
6 changed files with 117 additions and 3 deletions

View File

@ -27,8 +27,8 @@ public:
virtual void fdatasync(int descriptor) = 0; virtual void fdatasync(int descriptor) = 0;
virtual void access(const boost::filesystem::path &path, int mask) = 0; virtual void access(const boost::filesystem::path &path, int mask) = 0;
virtual void mkdir(const boost::filesystem::path &path, mode_t mode) = 0; virtual void mkdir(const boost::filesystem::path &path, mode_t mode) = 0;
//TODO Unit-Tests for all functions below
virtual void rmdir(const boost::filesystem::path &path) = 0; virtual void rmdir(const boost::filesystem::path &path) = 0;
//TODO Unit-Tests for all functions below
virtual void unlink(const boost::filesystem::path &path) = 0; virtual void unlink(const boost::filesystem::path &path) = 0;
virtual void rename(const boost::filesystem::path &from, const boost::filesystem::path &to) = 0; virtual void rename(const boost::filesystem::path &from, const boost::filesystem::path &to) = 0;
virtual std::unique_ptr<std::vector<std::string>> readDir(const boost::filesystem::path &path) = 0; virtual std::unique_ptr<std::vector<std::string>> readDir(const boost::filesystem::path &path) = 0;
@ -36,6 +36,8 @@ public:
virtual void statfs(const boost::filesystem::path &path, struct statvfs *fsstat) = 0; virtual void statfs(const boost::filesystem::path &path, struct statvfs *fsstat) = 0;
}; };
//TODO Test error cases handled by libfuse (e.g. mkdir: Already exists, rmdir: Doesn't exist, ...)
} }
} }

View File

@ -16,7 +16,7 @@ int FuseMkdirTest::MkdirAllowError(const char *dirname, mode_t mode) {
} }
Action<void(const char*, mode_t)> FuseMkdirTest::FromNowOnReturnIsDirOnLstat() { Action<void(const char*, mode_t)> FuseMkdirTest::FromNowOnReturnIsDirOnLstat() {
return Invoke([this](const char *filename, mode_t) { return Invoke([this](const char *dirname, mode_t) {
ReturnIsDirOnLstat(filename); ReturnIsDirOnLstat(dirname);
}); });
} }

View File

@ -0,0 +1,46 @@
#include "testutils/FuseRmdirTest.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
using ::testing::_;
using ::testing::StrEq;
using ::testing::Return;
using ::testing::Invoke;
using ::testing::Action;
class FuseRmdirDirnameTest: public FuseRmdirTest {
};
TEST_F(FuseRmdirDirnameTest, Rmdir) {
ReturnIsDirOnLstat("/mydir");
EXPECT_CALL(fsimpl, rmdir(StrEq("/mydir")))
// After rmdir was called, lstat should return that it doesn't exist anymore
// This is needed to make the ::rmdir() syscall pass.
.Times(1).WillOnce(FromNowOnReturnDoesntExistOnLstat());
Rmdir("/mydir");
}
TEST_F(FuseRmdirDirnameTest, RmdirNested) {
ReturnIsDirOnLstat("/mydir");
ReturnIsDirOnLstat("/mydir/mysubdir");
EXPECT_CALL(fsimpl, rmdir(StrEq("/mydir/mysubdir")))
// After rmdir was called, lstat should return that it doesn't exist anymore
// This is needed to make the ::rmdir() syscall pass.
.Times(1).WillOnce(FromNowOnReturnDoesntExistOnLstat());
Rmdir("/mydir/mysubdir");
}
TEST_F(FuseRmdirDirnameTest, RmdirNested2) {
ReturnIsDirOnLstat("/mydir");
ReturnIsDirOnLstat("/mydir/mydir2");
ReturnIsDirOnLstat("/mydir/mydir2/mydir3");
EXPECT_CALL(fsimpl, rmdir(StrEq("/mydir/mydir2/mydir3")))
// After rmdir was called, lstat should return that it doesn't exist anymore
// This is needed to make the ::rmdir() syscall pass.
.Times(1).WillOnce(FromNowOnReturnDoesntExistOnLstat());
Rmdir("/mydir/mydir2/mydir3");
}

View File

@ -0,0 +1,27 @@
#include "testutils/FuseRmdirTest.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "fspp/fuse/FuseErrnoException.h"
using ::testing::_;
using ::testing::StrEq;
using ::testing::Throw;
using ::testing::WithParamInterface;
using ::testing::Values;
using namespace fspp::fuse;
class FuseRmdirErrorTest: public FuseRmdirTest, public WithParamInterface<int> {
};
INSTANTIATE_TEST_CASE_P(FuseRmdirErrorTest, FuseRmdirErrorTest, Values(EACCES, EBUSY, EFAULT, EINVAL, ELOOP, ENAMETOOLONG, ENOENT, ENOMEM, ENOTDIR, ENOTEMPTY, EPERM, EROFS));
TEST_P(FuseRmdirErrorTest, ReturnedErrorIsCorrect) {
ReturnIsDirOnLstat(DIRNAME);
EXPECT_CALL(fsimpl, rmdir(StrEq(DIRNAME)))
.Times(1).WillOnce(Throw(FuseErrnoException(GetParam())));
int retval = RmdirAllowError(DIRNAME);
EXPECT_EQ(GetParam(), errno);
EXPECT_EQ(-1, retval);
}

View File

@ -0,0 +1,22 @@
#include "FuseRmdirTest.h"
using ::testing::Action;
using ::testing::Invoke;
void FuseRmdirTest::Rmdir(const char *dirname) {
int retval = RmdirAllowError(dirname);
EXPECT_EQ(0, retval);
}
int FuseRmdirTest::RmdirAllowError(const char *dirname) {
auto fs = TestFS();
auto realpath = fs->mountDir() / dirname;
return ::rmdir(realpath.c_str());
}
Action<void(const char*)> FuseRmdirTest::FromNowOnReturnDoesntExistOnLstat() {
return Invoke([this](const char *dirname) {
ReturnDoesntExistOnLstat(dirname);
});
}

View File

@ -0,0 +1,17 @@
#pragma once
#ifndef TEST_FSPP_FUSE_RMDIR_TESTUTILS_FUSERMDIRTEST_H_
#define TEST_FSPP_FUSE_RMDIR_TESTUTILS_FUSERMDIRTEST_H_
#include "test/testutils/FuseTest.h"
class FuseRmdirTest: public FuseTest {
public:
const char *DIRNAME = "/mydir";
void Rmdir(const char *dirname);
int RmdirAllowError(const char *dirname);
::testing::Action<void(const char*)> FromNowOnReturnDoesntExistOnLstat();
};
#endif