Testcases for rename()

This commit is contained in:
Sebastian Messmer 2014-11-28 17:40:54 +01:00
parent 0f2f6fb9bc
commit ea6c1fea3a
5 changed files with 197 additions and 1 deletions

View File

@ -29,8 +29,8 @@ public:
virtual void mkdir(const boost::filesystem::path &path, mode_t mode) = 0;
virtual void rmdir(const boost::filesystem::path &path) = 0;
virtual void unlink(const boost::filesystem::path &path) = 0;
//TODO Unit-Tests for all functions below
virtual void rename(const boost::filesystem::path &from, const boost::filesystem::path &to) = 0;
//TODO Unit-Tests for all functions below
virtual std::unique_ptr<std::vector<std::string>> readDir(const boost::filesystem::path &path) = 0;
virtual void utimens(const boost::filesystem::path &path, const timespec times[2]) = 0;
virtual void statfs(const boost::filesystem::path &path, struct statvfs *fsstat) = 0;

View File

@ -0,0 +1,28 @@
#include "testutils/FuseRenameTest.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 FuseRenameErrorTest: public FuseRenameTest, public WithParamInterface<int> {
};
INSTANTIATE_TEST_CASE_P(FuseRenameErrorTest, FuseRenameErrorTest, Values(EACCES, EBUSY, EDQUOT, EFAULT, EINVAL, EISDIR, ELOOP, EMLINK, ENAMETOOLONG, ENOENT, ENOMEM, ENOSPC, ENOTDIR, ENOTEMPTY, EEXIST, EPERM, EROFS, EXDEV, EBADF, ENOTDIR));
TEST_P(FuseRenameErrorTest, ReturnedErrorIsCorrect) {
ReturnIsFileOnLstat(FILENAME1);
ReturnIsFileOnLstat(FILENAME2);
EXPECT_CALL(fsimpl, rename(StrEq(FILENAME1), StrEq(FILENAME2)))
.Times(1).WillOnce(Throw(FuseErrnoException(GetParam())));
int retval = RenameAllowError(FILENAME1, FILENAME2);
EXPECT_EQ(GetParam(), errno);
EXPECT_EQ(-1, retval);
}

View File

@ -0,0 +1,135 @@
#include "testutils/FuseRenameTest.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
using ::testing::_;
using ::testing::StrEq;
using ::testing::Return;
using ::testing::Invoke;
using ::testing::Action;
class FuseRenameFilenameTest: public FuseRenameTest {
};
TEST_F(FuseRenameFilenameTest, RenameFileRootToRoot) {
ReturnIsFileOnLstat("/myfile");
ReturnDoesntExistOnLstat("/myrenamedfile");
EXPECT_CALL(fsimpl, rename(StrEq("/myfile"), StrEq("/myrenamedfile")))
.Times(1).WillOnce(Return());
Rename("/myfile", "/myrenamedfile");
}
TEST_F(FuseRenameFilenameTest, RenameFileRootToNested) {
ReturnIsFileOnLstat("/myfile");
ReturnIsDirOnLstat("/mydir");
ReturnDoesntExistOnLstat("/mydir/myrenamedfile");
EXPECT_CALL(fsimpl, rename(StrEq("/myfile"), StrEq("/mydir/myrenamedfile")))
.Times(1).WillOnce(Return());
Rename("/myfile", "/mydir/myrenamedfile");
}
TEST_F(FuseRenameFilenameTest, RenameFileNestedToRoot) {
ReturnDoesntExistOnLstat("/myrenamedfile");
ReturnIsDirOnLstat("/mydir");
ReturnIsFileOnLstat("/mydir/myfile");
EXPECT_CALL(fsimpl, rename(StrEq("/mydir/myfile"), StrEq("/myrenamedfile")))
.Times(1).WillOnce(Return());
Rename("/mydir/myfile", "/myrenamedfile");
}
TEST_F(FuseRenameFilenameTest, RenameFileNestedToNested) {
ReturnIsDirOnLstat("/mydir");
ReturnIsFileOnLstat("/mydir/myfile");
ReturnDoesntExistOnLstat("/mydir/myrenamedfile");
EXPECT_CALL(fsimpl, rename(StrEq("/mydir/myfile"), StrEq("/mydir/myrenamedfile")))
.Times(1).WillOnce(Return());
Rename("/mydir/myfile", "/mydir/myrenamedfile");
}
TEST_F(FuseRenameFilenameTest, RenameFileNestedToNested2) {
ReturnIsDirOnLstat("/mydir");
ReturnIsDirOnLstat("/mydir/mydir2");
ReturnIsFileOnLstat("/mydir/mydir2/myfile");
ReturnDoesntExistOnLstat("/mydir/mydir2/myrenamedfile");
EXPECT_CALL(fsimpl, rename(StrEq("/mydir/mydir2/myfile"), StrEq("/mydir/mydir2/myrenamedfile")))
.Times(1).WillOnce(Return());
Rename("/mydir/mydir2/myfile", "/mydir/mydir2/myrenamedfile");
}
TEST_F(FuseRenameFilenameTest, RenameFileNestedToNested_DifferentFolder) {
ReturnIsDirOnLstat("/mydir");
ReturnIsDirOnLstat("/mydir2");
ReturnIsFileOnLstat("/mydir/myfile");
ReturnDoesntExistOnLstat("/mydir2/myrenamedfile");
EXPECT_CALL(fsimpl, rename(StrEq("/mydir/myfile"), StrEq("/mydir2/myrenamedfile")))
.Times(1).WillOnce(Return());
Rename("/mydir/myfile", "/mydir2/myrenamedfile");
}
TEST_F(FuseRenameFilenameTest, RenameDirRootToRoot) {
ReturnIsDirOnLstat("/mydir");
ReturnDoesntExistOnLstat("/myrenameddir");
EXPECT_CALL(fsimpl, rename(StrEq("/mydir"), StrEq("/myrenameddir")))
.Times(1).WillOnce(Return());
Rename("/mydir", "/myrenameddir");
}
TEST_F(FuseRenameFilenameTest, RenameDirRootToNested) {
ReturnIsDirOnLstat("/mydir");
ReturnIsDirOnLstat("/myrootdir");
ReturnDoesntExistOnLstat("/myrootdir/myrenameddir");
EXPECT_CALL(fsimpl, rename(StrEq("/mydir"), StrEq("/myrootdir/myrenameddir")))
.Times(1).WillOnce(Return());
Rename("/mydir", "/myrootdir/myrenameddir");
}
TEST_F(FuseRenameFilenameTest, RenameDirNestedToRoot) {
ReturnDoesntExistOnLstat("/myrenameddir");
ReturnIsDirOnLstat("/myrootdir");
ReturnIsDirOnLstat("/myrootdir/mydir");
EXPECT_CALL(fsimpl, rename(StrEq("/myrootdir/mydir"), StrEq("/myrenameddir")))
.Times(1).WillOnce(Return());
Rename("/myrootdir/mydir", "/myrenameddir");
}
TEST_F(FuseRenameFilenameTest, RenameDirNestedToNested) {
ReturnIsDirOnLstat("/myrootdir");
ReturnIsDirOnLstat("/myrootdir/mydir");
ReturnDoesntExistOnLstat("/myrootdir/myrenameddir");
EXPECT_CALL(fsimpl, rename(StrEq("/myrootdir/mydir"), StrEq("/myrootdir/myrenameddir")))
.Times(1).WillOnce(Return());
Rename("/myrootdir/mydir", "/myrootdir/myrenameddir");
}
TEST_F(FuseRenameFilenameTest, RenameDirNestedToNested2) {
ReturnIsDirOnLstat("/myrootdir");
ReturnIsDirOnLstat("/myrootdir/myrootdir2");
ReturnIsDirOnLstat("/myrootdir/myrootdir2/mydir");
ReturnDoesntExistOnLstat("/myrootdir/myrootdir2/myrenameddir");
EXPECT_CALL(fsimpl, rename(StrEq("/myrootdir/myrootdir2/mydir"), StrEq("/myrootdir/myrootdir2/myrenameddir")))
.Times(1).WillOnce(Return());
Rename("/myrootdir/myrootdir2/mydir", "/myrootdir/myrootdir2/myrenameddir");
}
TEST_F(FuseRenameFilenameTest, RenameDirNestedToNested_DifferentFolder) {
ReturnIsDirOnLstat("/myrootdir");
ReturnIsDirOnLstat("/myrootdir2");
ReturnIsDirOnLstat("/myrootdir/mydir");
ReturnDoesntExistOnLstat("/myrootdir2/myrenameddir");
EXPECT_CALL(fsimpl, rename(StrEq("/myrootdir/mydir"), StrEq("/myrootdir2/myrenameddir")))
.Times(1).WillOnce(Return());
Rename("/myrootdir/mydir", "/myrootdir2/myrenameddir");
}

View File

@ -0,0 +1,17 @@
#include "FuseRenameTest.h"
using ::testing::Action;
using ::testing::Invoke;
void FuseRenameTest::Rename(const char *from, const char *to) {
int retval = RenameAllowError(from, to);
EXPECT_EQ(0, retval);
}
int FuseRenameTest::RenameAllowError(const char *from, const char *to) {
auto fs = TestFS();
auto realfrom = fs->mountDir() / from;
auto realto = fs->mountDir() / to;
return ::rename(realfrom.c_str(), realto.c_str());
}

View File

@ -0,0 +1,16 @@
#pragma once
#ifndef TEST_FSPP_FUSE_RENAME_TESTUTILS_FUSERENAMETEST_H_
#define TEST_FSPP_FUSE_RENAME_TESTUTILS_FUSERENAMETEST_H_
#include "test/testutils/FuseTest.h"
class FuseRenameTest: public FuseTest {
public:
const char *FILENAME1 = "/myfile1";
const char *FILENAME2 = "/myfile2";
void Rename(const char *from, const char *to);
int RenameAllowError(const char *from, const char *to);
};
#endif