Testcases for mkdir()

This commit is contained in:
Sebastian Messmer 2014-11-28 15:54:18 +01:00
parent 5118d5decc
commit 787896ef58
6 changed files with 136 additions and 1 deletions

View File

@ -26,8 +26,8 @@ public:
virtual void fsync(int descriptor) = 0;
virtual void fdatasync(int descriptor) = 0;
virtual void access(const boost::filesystem::path &path, int mask) = 0;
//TODO Unit-Tests for all functions below
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 unlink(const boost::filesystem::path &path) = 0;
virtual void rename(const boost::filesystem::path &from, const boost::filesystem::path &to) = 0;

View File

@ -0,0 +1,46 @@
#include "testutils/FuseMkdirTest.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
using ::testing::_;
using ::testing::StrEq;
using ::testing::Return;
using ::testing::Invoke;
using ::testing::Action;
class FuseMkdirDirnameTest: public FuseMkdirTest {
};
TEST_F(FuseMkdirDirnameTest, Mkdir) {
ReturnDoesntExistOnLstat("/mydir");
EXPECT_CALL(fsimpl, mkdir(StrEq("/mydir"), _))
// After mkdir was called, lstat should return that it is a dir.
// This is needed to make the ::mkdir() syscall pass.
.Times(1).WillOnce(FromNowOnReturnIsDirOnLstat());
Mkdir("/mydir", 0);
}
TEST_F(FuseMkdirDirnameTest, MkdirNested) {
ReturnIsDirOnLstat("/mydir");
ReturnDoesntExistOnLstat("/mydir/mysubdir");
EXPECT_CALL(fsimpl, mkdir(StrEq("/mydir/mysubdir"), _))
// After mkdir was called, lstat should return that it is a dir.
// This is needed to make the ::mkdir() syscall pass.
.Times(1).WillOnce(FromNowOnReturnIsDirOnLstat());
Mkdir("/mydir/mysubdir", 0);
}
TEST_F(FuseMkdirDirnameTest, MkdirNested2) {
ReturnIsDirOnLstat("/mydir");
ReturnIsDirOnLstat("/mydir/mydir2");
ReturnDoesntExistOnLstat("/mydir/mydir2/mydir3");
EXPECT_CALL(fsimpl, mkdir(StrEq("/mydir/mydir2/mydir3"), _))
// After mkdir was called, lstat should return that it is a dir.
// This is needed to make the ::mkdir() syscall pass.
.Times(1).WillOnce(FromNowOnReturnIsDirOnLstat());
Mkdir("/mydir/mydir2/mydir3", 0);
}

View File

@ -0,0 +1,27 @@
#include "testutils/FuseMkdirTest.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 FuseMkdirErrorTest: public FuseMkdirTest, public WithParamInterface<int> {
};
INSTANTIATE_TEST_CASE_P(FuseMkdirErrorTest, FuseMkdirErrorTest, Values(EACCES, EDQUOT, EEXIST, EFAULT, ELOOP, EMLINK, ENAMETOOLONG, ENOENT, ENOMEM, ENOSPC, ENOTDIR, EPERM, EROFS, EBADF));
TEST_P(FuseMkdirErrorTest, ReturnedErrorIsCorrect) {
ReturnDoesntExistOnLstat(DIRNAME);
EXPECT_CALL(fsimpl, mkdir(StrEq(DIRNAME), _))
.Times(1).WillOnce(Throw(FuseErrnoException(GetParam())));
int retval = MkdirAllowError(DIRNAME, 0);
EXPECT_EQ(GetParam(), errno);
EXPECT_EQ(-1, retval);
}

View File

@ -0,0 +1,23 @@
#include "testutils/FuseMkdirTest.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
using ::testing::_;
using ::testing::StrEq;
using ::testing::Return;
using ::testing::WithParamInterface;
using ::testing::Values;
class FuseMkdirModeTest: public FuseMkdirTest, public WithParamInterface<mode_t> {
};
INSTANTIATE_TEST_CASE_P(FuseMkdirModeTest, FuseMkdirModeTest, Values(0, S_IRUSR, S_IRGRP, S_IXOTH, S_IRUSR|S_IRGRP|S_IROTH|S_IRGRP));
TEST_P(FuseMkdirModeTest, Mkdir) {
ReturnDoesntExistOnLstat(DIRNAME);
EXPECT_CALL(fsimpl, mkdir(StrEq(DIRNAME), GetParam()))
.Times(1).WillOnce(FromNowOnReturnIsDirOnLstat());
Mkdir(DIRNAME, GetParam());
}

View File

@ -0,0 +1,22 @@
#include <test/fspp/fuse/mkdir/testutils/FuseMkdirTest.h>
using ::testing::Action;
using ::testing::Invoke;
void FuseMkdirTest::Mkdir(const char *dirname, mode_t mode) {
int retval = MkdirAllowError(dirname, mode);
EXPECT_EQ(0, retval);
}
int FuseMkdirTest::MkdirAllowError(const char *dirname, mode_t mode) {
auto fs = TestFS();
auto realpath = fs->mountDir() / dirname;
return ::mkdir(realpath.c_str(), mode);
}
Action<void(const char*, mode_t)> FuseMkdirTest::FromNowOnReturnIsDirOnLstat() {
return Invoke([this](const char *filename, mode_t) {
ReturnIsDirOnLstat(filename);
});
}

View File

@ -0,0 +1,17 @@
#pragma once
#ifndef TEST_FSPP_FUSE_MKDIR_TESTUTILS_FUSEMKDIRTEST_H_
#define TEST_FSPP_FUSE_MKDIR_TESTUTILS_FUSEMKDIRTEST_H_
#include "test/testutils/FuseTest.h"
class FuseMkdirTest: public FuseTest {
public:
const char *DIRNAME = "/mydir";
void Mkdir(const char *dirname, mode_t mode);
int MkdirAllowError(const char *dirname, mode_t mode);
::testing::Action<void(const char*, mode_t)> FromNowOnReturnIsDirOnLstat();
};
#endif