Improve testutils implementation for lstat

This commit is contained in:
Sebastian Messmer 2014-11-20 15:35:52 +01:00
parent 748e51271c
commit eadeedb5a5
11 changed files with 143 additions and 117 deletions

View File

@ -1,4 +1,4 @@
#include "FuseLstatTest.h"
#include "testutils/FuseLstatTest.h"
using ::testing::_;
using ::testing::StrEq;

View File

@ -1,6 +1,6 @@
#include "FuseLstatReturnTest.h"
#include "testutils/FuseLstatReturnTest.h"
class FuseLstatReturnPropertyGidTest: public FuseLstatReturnPropertyTest<gid_t> {
class FuseLstatReturnGidTest: public FuseLstatReturnTest<gid_t> {
public:
const gid_t GID1 = 0;
const gid_t GID2 = 10;
@ -10,22 +10,22 @@ private:
}
};
TEST_F(FuseLstatReturnPropertyGidTest, ReturnedFileGidIsCorrect1) {
TEST_F(FuseLstatReturnGidTest, ReturnedFileGidIsCorrect1) {
struct ::stat result = CallFileLstatWithValue(GID1);
EXPECT_EQ(GID1, result.st_gid);
}
TEST_F(FuseLstatReturnPropertyGidTest, ReturnedFileGidIsCorrect2) {
TEST_F(FuseLstatReturnGidTest, ReturnedFileGidIsCorrect2) {
struct ::stat result = CallFileLstatWithValue(GID2);
EXPECT_EQ(GID2, result.st_gid);
}
TEST_F(FuseLstatReturnPropertyGidTest, ReturnedDirGidIsCorrect1) {
TEST_F(FuseLstatReturnGidTest, ReturnedDirGidIsCorrect1) {
struct ::stat result = CallDirLstatWithValue(GID1);
EXPECT_EQ(GID1, result.st_gid);
}
TEST_F(FuseLstatReturnPropertyGidTest, ReturnedDirGidIsCorrect2) {
TEST_F(FuseLstatReturnGidTest, ReturnedDirGidIsCorrect2) {
struct ::stat result = CallDirLstatWithValue(GID2);
EXPECT_EQ(GID2, result.st_gid);
}

View File

@ -1,6 +1,6 @@
#include "FuseLstatReturnTest.h"
#include "testutils/FuseLstatReturnTest.h"
class FuseLstatReturnPropertyModeTest: public FuseLstatTest {
class FuseLstatReturnModeTest: public FuseLstatTest {
public:
const mode_t MODE1 = S_IFREG | S_IRUSR | S_IWGRP | S_IXOTH;
const mode_t MODE2 = S_IFDIR | S_IWUSR | S_IXGRP | S_IROTH;
@ -12,12 +12,12 @@ public:
}
};
TEST_F(FuseLstatReturnPropertyModeTest, ReturnedModeIsCorrect1) {
TEST_F(FuseLstatReturnModeTest, ReturnedModeIsCorrect1) {
struct ::stat result = CallLstatWithValue(MODE1);
EXPECT_EQ(MODE1, result.st_mode);
}
TEST_F(FuseLstatReturnPropertyModeTest, ReturnedModeIsCorrect2) {
TEST_F(FuseLstatReturnModeTest, ReturnedModeIsCorrect2) {
struct ::stat result = CallLstatWithValue(MODE2);
EXPECT_EQ(MODE2, result.st_mode);
}

View File

@ -1,6 +1,6 @@
#include "FuseLstatReturnTest.h"
#include "testutils/FuseLstatReturnTest.h"
class FuseLstatReturnPropertyNlinkTest: public FuseLstatReturnPropertyTest<nlink_t> {
class FuseLstatReturnNlinkTest: public FuseLstatReturnTest<nlink_t> {
public:
const nlink_t NLINK1 = 1;
const nlink_t NLINK2 = 5;
@ -10,22 +10,22 @@ private:
}
};
TEST_F(FuseLstatReturnPropertyNlinkTest, ReturnedFileNlinkIsCorrect1) {
TEST_F(FuseLstatReturnNlinkTest, ReturnedFileNlinkIsCorrect1) {
struct ::stat result = CallDirLstatWithValue(NLINK1);
EXPECT_EQ(NLINK1, result.st_nlink);
}
TEST_F(FuseLstatReturnPropertyNlinkTest, ReturnedFileNlinkIsCorrect2) {
TEST_F(FuseLstatReturnNlinkTest, ReturnedFileNlinkIsCorrect2) {
struct ::stat result = CallDirLstatWithValue(NLINK2);
EXPECT_EQ(NLINK2, result.st_nlink);
}
TEST_F(FuseLstatReturnPropertyNlinkTest, ReturnedDirNlinkIsCorrect1) {
TEST_F(FuseLstatReturnNlinkTest, ReturnedDirNlinkIsCorrect1) {
struct ::stat result = CallDirLstatWithValue(NLINK1);
EXPECT_EQ(NLINK1, result.st_nlink);
}
TEST_F(FuseLstatReturnPropertyNlinkTest, ReturnedDirNlinkIsCorrect2) {
TEST_F(FuseLstatReturnNlinkTest, ReturnedDirNlinkIsCorrect2) {
struct ::stat result = CallDirLstatWithValue(NLINK2);
EXPECT_EQ(NLINK2, result.st_nlink);
}

View File

@ -1,6 +1,6 @@
#include "FuseLstatReturnTest.h"
#include "testutils/FuseLstatReturnTest.h"
class FuseLstatReturnPropertySizeTest: public FuseLstatReturnPropertyTest<off_t> {
class FuseLstatReturnSizeTest: public FuseLstatReturnTest<off_t> {
public:
const off_t SIZE1 = 0;
const off_t SIZE2 = 4096;
@ -11,32 +11,32 @@ private:
}
};
TEST_F(FuseLstatReturnPropertySizeTest, ReturnedFileSizeIsCorrect1) {
TEST_F(FuseLstatReturnSizeTest, ReturnedFileSizeIsCorrect1) {
struct ::stat result = CallDirLstatWithValue(SIZE1);
EXPECT_EQ(SIZE1, result.st_size);
}
TEST_F(FuseLstatReturnPropertySizeTest, ReturnedFileSizeIsCorrect2) {
TEST_F(FuseLstatReturnSizeTest, ReturnedFileSizeIsCorrect2) {
struct ::stat result = CallDirLstatWithValue(SIZE2);
EXPECT_EQ(SIZE2, result.st_size);
}
TEST_F(FuseLstatReturnPropertySizeTest, ReturnedFileSizeIsCorrect3) {
TEST_F(FuseLstatReturnSizeTest, ReturnedFileSizeIsCorrect3) {
struct ::stat result = CallDirLstatWithValue(SIZE3);
EXPECT_EQ(SIZE3, result.st_size);
}
TEST_F(FuseLstatReturnPropertySizeTest, ReturnedDirSizeIsCorrect1) {
TEST_F(FuseLstatReturnSizeTest, ReturnedDirSizeIsCorrect1) {
struct ::stat result = CallDirLstatWithValue(SIZE1);
EXPECT_EQ(SIZE1, result.st_size);
}
TEST_F(FuseLstatReturnPropertySizeTest, ReturnedDirSizeIsCorrect2) {
TEST_F(FuseLstatReturnSizeTest, ReturnedDirSizeIsCorrect2) {
struct ::stat result = CallDirLstatWithValue(SIZE2);
EXPECT_EQ(SIZE2, result.st_size);
}
TEST_F(FuseLstatReturnPropertySizeTest, ReturnedDirSizeIsCorrect3) {
TEST_F(FuseLstatReturnSizeTest, ReturnedDirSizeIsCorrect3) {
struct ::stat result = CallDirLstatWithValue(SIZE3);
EXPECT_EQ(SIZE3, result.st_size);
}

View File

@ -1,26 +0,0 @@
#pragma once
#ifndef TEST_FSPP_FUSE_LSTAT_FUSELSTATRETURNTEST_H_
#define TEST_FSPP_FUSE_LSTAT_FUSELSTATRETURNTEST_H_
#include "FuseLstatTest.h"
template<typename Property>
class FuseLstatReturnPropertyTest: public FuseLstatTest {
public:
struct stat CallFileLstatWithValue(Property value) {
return CallFileLstatWithImpl(SetPropertyImpl(value));
}
struct stat CallDirLstatWithValue(Property value) {
return CallDirLstatWithImpl(SetPropertyImpl(value));
}
private:
std::function<void(struct stat*)> SetPropertyImpl(Property value) {
return [this, value] (struct stat *stat) {
set(stat, value);
};
}
virtual void set(struct stat *stat, Property value) = 0;
};
#endif

View File

@ -1,6 +1,6 @@
#include "FuseLstatReturnTest.h"
#include "testutils/FuseLstatReturnTest.h"
class FuseLstatReturnPropertyUidTest: public FuseLstatReturnPropertyTest<uid_t> {
class FuseLstatReturnUidTest: public FuseLstatReturnTest<uid_t> {
public:
const uid_t UID1 = 0;
const uid_t UID2 = 10;
@ -10,22 +10,22 @@ private:
}
};
TEST_F(FuseLstatReturnPropertyUidTest, ReturnedFileUidIsCorrect1) {
TEST_F(FuseLstatReturnUidTest, ReturnedFileUidIsCorrect1) {
struct ::stat result = CallFileLstatWithValue(UID1);
EXPECT_EQ(UID1, result.st_uid);
}
TEST_F(FuseLstatReturnPropertyUidTest, ReturnedFileUidIsCorrect2) {
TEST_F(FuseLstatReturnUidTest, ReturnedFileUidIsCorrect2) {
struct ::stat result = CallFileLstatWithValue(UID2);
EXPECT_EQ(UID2, result.st_uid);
}
TEST_F(FuseLstatReturnPropertyUidTest, ReturnedDirUidIsCorrect1) {
TEST_F(FuseLstatReturnUidTest, ReturnedDirUidIsCorrect1) {
struct ::stat result = CallDirLstatWithValue(UID1);
EXPECT_EQ(UID1, result.st_uid);
}
TEST_F(FuseLstatReturnPropertyUidTest, ReturnedDirUidIsCorrect2) {
TEST_F(FuseLstatReturnUidTest, ReturnedDirUidIsCorrect2) {
struct ::stat result = CallDirLstatWithValue(UID2);
EXPECT_EQ(UID2, result.st_uid);
}

View File

@ -1,60 +0,0 @@
#pragma once
#ifndef TEST_FSPP_FUSE_LSTAT_FUSELSTATTEST_H_
#define TEST_FSPP_FUSE_LSTAT_FUSELSTATTEST_H_
#include "gmock/gmock.h"
#include <string>
#include <functional>
#include <sys/stat.h>
#include "test/testutils/FuseTest.h"
class FuseLstatTest: public FuseTest {
public:
const char *FILENAME = "/myfile";
void LstatPath(const std::string &path) {
struct stat dummy;
LstatPath(path, &dummy);
}
void LstatPath(const std::string &path, struct stat *result) {
auto fs = TestFS();
auto realpath = fs->mountDir() / path;
int retval = ::lstat(realpath.c_str(), result);
EXPECT_EQ(0, retval) << "lstat syscall failed. errno: " << errno;
}
protected:
struct stat CallFileLstatWithImpl(std::function<void(struct stat*)> implementation) {
return CallLstatWithModeAndImpl(S_IFREG, implementation);
}
struct stat CallDirLstatWithImpl(std::function<void(struct stat*)> implementation) {
return CallLstatWithModeAndImpl(S_IFDIR, implementation);
}
struct stat CallLstatWithImpl(std::function<void(struct stat*)> implementation) {
EXPECT_CALL(fsimpl, lstat(::testing::StrEq(FILENAME), ::testing::_)).WillRepeatedly(::testing::Invoke([implementation](const char*, struct ::stat *stat) {
implementation(stat);
}));
struct stat result;
LstatPath(FILENAME, &result);
return result;
}
private:
struct stat CallLstatWithModeAndImpl(mode_t mode, std::function<void(struct stat*)> implementation) {
return CallLstatWithImpl([mode, implementation] (struct stat *stat) {
stat->st_mode = mode;
implementation(stat);
});
}
};
#endif

View File

@ -0,0 +1,37 @@
#pragma once
#ifndef TEST_FSPP_FUSE_LSTAT_FUSELSTATRETURNTEST_H_
#define TEST_FSPP_FUSE_LSTAT_FUSELSTATRETURNTEST_H_
#include "FuseLstatTest.h"
template<typename Property>
class FuseLstatReturnTest: public FuseLstatTest {
public:
struct stat CallFileLstatWithValue(Property value);
struct stat CallDirLstatWithValue(Property value);
private:
std::function<void(struct stat*)> SetPropertyImpl(Property value);
virtual void set(struct stat *stat, Property value) = 0;
};
template<typename Property>
struct stat FuseLstatReturnTest<Property>::CallFileLstatWithValue(Property value) {
return CallFileLstatWithImpl(SetPropertyImpl(value));
}
template<typename Property>
struct stat FuseLstatReturnTest<Property>::CallDirLstatWithValue(Property value) {
return CallDirLstatWithImpl(SetPropertyImpl(value));
}
template<typename Property>
std::function<void(struct stat*)> FuseLstatReturnTest<Property>::SetPropertyImpl(Property value) {
return [this, value] (struct stat *stat) {
set(stat, value);
};
}
#endif

View File

@ -0,0 +1,45 @@
#include "FuseLstatTest.h"
using std::function;
using ::testing::StrEq;
using ::testing::_;
using ::testing::Invoke;
void FuseLstatTest::LstatPath(const std::string &path) {
struct stat dummy;
LstatPath(path, &dummy);
}
void FuseLstatTest::LstatPath(const std::string &path, struct stat *result) {
auto fs = TestFS();
auto realpath = fs->mountDir() / path;
int retval = ::lstat(realpath.c_str(), result);
EXPECT_EQ(0, retval) << "lstat syscall failed. errno: " << errno;
}
struct stat FuseLstatTest::CallFileLstatWithImpl(function<void(struct stat*)> implementation) {
return CallLstatWithModeAndImpl(S_IFREG, implementation);
}
struct stat FuseLstatTest::CallDirLstatWithImpl(function<void(struct stat*)> implementation) {
return CallLstatWithModeAndImpl(S_IFDIR, implementation);
}
struct stat FuseLstatTest::CallLstatWithImpl(function<void(struct stat*)> implementation) {
EXPECT_CALL(fsimpl, lstat(StrEq(FILENAME), _)).WillRepeatedly(Invoke([implementation](const char*, struct ::stat *stat) {
implementation(stat);
}));
struct stat result;
LstatPath(FILENAME, &result);
return result;
}
struct stat FuseLstatTest::CallLstatWithModeAndImpl(mode_t mode, function<void(struct stat*)> implementation) {
return CallLstatWithImpl([mode, implementation] (struct stat *stat) {
stat->st_mode = mode;
implementation(stat);
});
}

View File

@ -0,0 +1,30 @@
#pragma once
#ifndef TEST_FSPP_FUSE_LSTAT_FUSELSTATTEST_H_
#define TEST_FSPP_FUSE_LSTAT_FUSELSTATTEST_H_
#include "gmock/gmock.h"
#include <string>
#include <functional>
#include <sys/stat.h>
#include "test/testutils/FuseTest.h"
class FuseLstatTest: public FuseTest {
public:
const char *FILENAME = "/myfile";
void LstatPath(const std::string &path);
void LstatPath(const std::string &path, struct stat *result);
protected:
struct stat CallFileLstatWithImpl(std::function<void(struct stat*)> implementation);
struct stat CallDirLstatWithImpl(std::function<void(struct stat*)> implementation);
struct stat CallLstatWithImpl(std::function<void(struct stat*)> implementation);
private:
struct stat CallLstatWithModeAndImpl(mode_t mode, std::function<void(struct stat*)> implementation);
};
#endif