Separate lstat tests into separate classes

This commit is contained in:
Sebastian Messmer 2014-11-20 15:15:24 +01:00
parent f0acb9f68f
commit c61e27b24e

View File

@ -20,17 +20,6 @@ using std::function;
class FuseLstatTest: public FuseTest { class FuseLstatTest: public FuseTest {
public: public:
const char *FILENAME = "/myfile"; const char *FILENAME = "/myfile";
const mode_t MODE_FILE = S_IFREG | S_IRUSR | S_IWGRP | S_IXOTH;
const mode_t MODE_DIR = S_IFDIR | S_IWUSR | S_IXGRP | S_IROTH;
const uid_t UID1 = 0;
const uid_t UID2 = 10;
const gid_t GID1 = 0;
const gid_t GID2 = 10;
const off_t SIZE1 = 0;
const off_t SIZE2 = 4096;
const off_t SIZE3 = 1024*1024*1024;
const nlink_t NLINK1 = 1;
const nlink_t NLINK2 = 5;
void LstatPath(const string &path) { void LstatPath(const string &path) {
struct stat dummy; struct stat dummy;
@ -45,67 +34,7 @@ public:
EXPECT_EQ(0, retval) << "lstat syscall failed. errno: " << errno; EXPECT_EQ(0, retval) << "lstat syscall failed. errno: " << errno;
} }
struct stat CallLstatWithMode(mode_t mode) { protected:
return CallLstatWithModeAndImpl(mode, [](struct stat*){});
}
struct stat CallFileLstatWithUid(uid_t uid) {
return CallFileLstatWithImpl(LstatUidImpl(uid));
}
struct stat CallDirLstatWithUid(uid_t uid) {
return CallDirLstatWithImpl(LstatUidImpl(uid));
}
struct stat CallFileLstatWithGid(gid_t gid) {
return CallFileLstatWithImpl(LstatGidImpl(gid));
}
struct stat CallDirLstatWithGid(gid_t gid) {
return CallDirLstatWithImpl(LstatGidImpl(gid));
}
struct stat CallFileLstatWithSize(off_t size) {
return CallFileLstatWithImpl(LstatSizeImpl(size));
}
struct stat CallDirLstatWithSize(off_t size) {
return CallDirLstatWithImpl(LstatSizeImpl(size));
}
struct stat CallFileLstatWithNlink(nlink_t nlink) {
return CallFileLstatWithImpl(LstatNlinkImpl(nlink));
}
struct stat CallDirLstatWithNlink(nlink_t nlink) {
return CallDirLstatWithImpl(LstatNlinkImpl(nlink));
}
private:
static function<void(struct stat*)> LstatUidImpl(uid_t uid) {
return [uid] (struct stat *stat) {
stat->st_uid = uid;
};
}
static function<void(struct stat*)> LstatGidImpl(gid_t gid) {
return [gid] (struct stat *stat) {
stat->st_gid = gid;
};
}
static function<void(struct stat*)> LstatSizeImpl(off_t size) {
return [size] (struct stat *stat) {
stat->st_size = size;
};
}
static function<void(struct stat*)> LstatNlinkImpl(nlink_t nlink) {
return [nlink] (struct stat *stat) {
stat->st_nlink = nlink;
};
}
struct stat CallFileLstatWithImpl(function<void(struct stat*)> implementation) { struct stat CallFileLstatWithImpl(function<void(struct stat*)> implementation) {
return CallLstatWithModeAndImpl(S_IFREG, implementation); return CallLstatWithModeAndImpl(S_IFREG, implementation);
} }
@ -114,13 +43,6 @@ private:
return CallLstatWithModeAndImpl(S_IFDIR, implementation); return CallLstatWithModeAndImpl(S_IFDIR, implementation);
} }
struct stat CallLstatWithModeAndImpl(mode_t mode, function<void(struct stat*)> implementation) {
return CallLstatWithImpl([mode, implementation] (struct stat *stat) {
stat->st_mode = mode;
implementation(stat);
});
}
struct stat CallLstatWithImpl(function<void(struct stat*)> implementation) { struct stat CallLstatWithImpl(function<void(struct stat*)> implementation) {
EXPECT_CALL(fsimpl, lstat(StrEq(FILENAME), _)).WillRepeatedly(Invoke([implementation](const char*, struct ::stat *stat) { EXPECT_CALL(fsimpl, lstat(StrEq(FILENAME), _)).WillRepeatedly(Invoke([implementation](const char*, struct ::stat *stat) {
implementation(stat); implementation(stat);
@ -131,6 +53,86 @@ private:
return result; return result;
} }
private:
struct stat CallLstatWithModeAndImpl(mode_t mode, function<void(struct stat*)> implementation) {
return CallLstatWithImpl([mode, implementation] (struct stat *stat) {
stat->st_mode = mode;
implementation(stat);
});
}
};
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:
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;
};
class FuseLstatReturnPropertyModeTest: 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;
struct stat CallLstatWithValue(mode_t mode) {
return CallLstatWithImpl([mode] (struct stat *stat) {
stat->st_mode = mode;
});
}
};
class FuseLstatReturnPropertyUidTest: public FuseLstatReturnPropertyTest<uid_t> {
public:
const uid_t UID1 = 0;
const uid_t UID2 = 10;
private:
void set(struct stat *stat, uid_t value) override {
stat->st_uid = value;
}
};
class FuseLstatReturnPropertyGidTest: public FuseLstatReturnPropertyTest<gid_t> {
public:
const gid_t GID1 = 0;
const gid_t GID2 = 10;
private:
void set(struct stat *stat, gid_t value) override {
stat->st_gid = value;
}
};
class FuseLstatReturnPropertySizeTest: public FuseLstatReturnPropertyTest<off_t> {
public:
const off_t SIZE1 = 0;
const off_t SIZE2 = 4096;
const off_t SIZE3 = 1024*1024*1024;
private:
void set(struct stat *stat, off_t value) override {
stat->st_size = value;
}
};
class FuseLstatReturnPropertyNlinkTest: public FuseLstatReturnPropertyTest<nlink_t> {
public:
const nlink_t NLINK1 = 1;
const nlink_t NLINK2 = 5;
private:
void set(struct stat *stat, nlink_t value) override {
stat->st_nlink = value;
}
}; };
@ -163,103 +165,103 @@ TEST_F(FuseLstatTest, PathParameterIsCorrectNestedDir) {
LstatPath("/mydir/mydir2/mydir3/"); LstatPath("/mydir/mydir2/mydir3/");
} }
TEST_F(FuseLstatTest, ReturnedFileModeIsCorrect) { TEST_F(FuseLstatReturnPropertyModeTest, ReturnedModeIsCorrect1) {
struct ::stat result = CallLstatWithMode(MODE_FILE); struct ::stat result = CallLstatWithValue(MODE1);
EXPECT_EQ(MODE_FILE, result.st_mode); EXPECT_EQ(MODE1, result.st_mode);
} }
TEST_F(FuseLstatTest, ReturnedDirModeIsCorrect) { TEST_F(FuseLstatReturnPropertyModeTest, ReturnedModeIsCorrect2) {
struct ::stat result = CallLstatWithMode(MODE_DIR); struct ::stat result = CallLstatWithValue(MODE2);
EXPECT_EQ(MODE_DIR, result.st_mode); EXPECT_EQ(MODE2, result.st_mode);
} }
TEST_F(FuseLstatTest, ReturnedFileUidIsCorrect1) { TEST_F(FuseLstatReturnPropertyUidTest, ReturnedFileUidIsCorrect1) {
struct ::stat result = CallFileLstatWithUid(UID1); struct ::stat result = CallFileLstatWithValue(UID1);
EXPECT_EQ(UID1, result.st_uid); EXPECT_EQ(UID1, result.st_uid);
} }
TEST_F(FuseLstatTest, ReturnedFileUidIsCorrect2) { TEST_F(FuseLstatReturnPropertyUidTest, ReturnedFileUidIsCorrect2) {
struct ::stat result = CallFileLstatWithUid(UID2); struct ::stat result = CallFileLstatWithValue(UID2);
EXPECT_EQ(UID2, result.st_uid); EXPECT_EQ(UID2, result.st_uid);
} }
TEST_F(FuseLstatTest, ReturnedDirUidIsCorrect1) { TEST_F(FuseLstatReturnPropertyUidTest, ReturnedDirUidIsCorrect1) {
struct ::stat result = CallDirLstatWithUid(UID1); struct ::stat result = CallDirLstatWithValue(UID1);
EXPECT_EQ(UID1, result.st_uid); EXPECT_EQ(UID1, result.st_uid);
} }
TEST_F(FuseLstatTest, ReturnedDirUidIsCorrect2) { TEST_F(FuseLstatReturnPropertyUidTest, ReturnedDirUidIsCorrect2) {
struct ::stat result = CallDirLstatWithUid(UID2); struct ::stat result = CallDirLstatWithValue(UID2);
EXPECT_EQ(UID2, result.st_uid); EXPECT_EQ(UID2, result.st_uid);
} }
TEST_F(FuseLstatTest, ReturnedFileGidIsCorrect1) { TEST_F(FuseLstatReturnPropertyGidTest, ReturnedFileGidIsCorrect1) {
struct ::stat result = CallFileLstatWithUid(GID1); struct ::stat result = CallFileLstatWithValue(GID1);
EXPECT_EQ(GID1, result.st_gid); EXPECT_EQ(GID1, result.st_gid);
} }
TEST_F(FuseLstatTest, ReturnedFileGidIsCorrect2) { TEST_F(FuseLstatReturnPropertyGidTest, ReturnedFileGidIsCorrect2) {
struct ::stat result = CallFileLstatWithGid(GID2); struct ::stat result = CallFileLstatWithValue(GID2);
EXPECT_EQ(GID2, result.st_gid); EXPECT_EQ(GID2, result.st_gid);
} }
TEST_F(FuseLstatTest, ReturnedDirGidIsCorrect1) { TEST_F(FuseLstatReturnPropertyGidTest, ReturnedDirGidIsCorrect1) {
struct ::stat result = CallDirLstatWithGid(GID1); struct ::stat result = CallDirLstatWithValue(GID1);
EXPECT_EQ(GID1, result.st_gid); EXPECT_EQ(GID1, result.st_gid);
} }
TEST_F(FuseLstatTest, ReturnedDirGidIsCorrect2) { TEST_F(FuseLstatReturnPropertyGidTest, ReturnedDirGidIsCorrect2) {
struct ::stat result = CallDirLstatWithGid(GID2); struct ::stat result = CallDirLstatWithValue(GID2);
EXPECT_EQ(GID2, result.st_gid); EXPECT_EQ(GID2, result.st_gid);
} }
TEST_F(FuseLstatTest, ReturnedFileSizeIsCorrect1) { TEST_F(FuseLstatReturnPropertySizeTest, ReturnedFileSizeIsCorrect1) {
struct ::stat result = CallFileLstatWithSize(SIZE1); struct ::stat result = CallDirLstatWithValue(SIZE1);
EXPECT_EQ(SIZE1, result.st_size); EXPECT_EQ(SIZE1, result.st_size);
} }
TEST_F(FuseLstatTest, ReturnedFileSizeIsCorrect2) { TEST_F(FuseLstatReturnPropertySizeTest, ReturnedFileSizeIsCorrect2) {
struct ::stat result = CallFileLstatWithSize(SIZE2); struct ::stat result = CallDirLstatWithValue(SIZE2);
EXPECT_EQ(SIZE2, result.st_size); EXPECT_EQ(SIZE2, result.st_size);
} }
TEST_F(FuseLstatTest, ReturnedFileSizeIsCorrect3) { TEST_F(FuseLstatReturnPropertySizeTest, ReturnedFileSizeIsCorrect3) {
struct ::stat result = CallFileLstatWithSize(SIZE3); struct ::stat result = CallDirLstatWithValue(SIZE3);
EXPECT_EQ(SIZE3, result.st_size); EXPECT_EQ(SIZE3, result.st_size);
} }
TEST_F(FuseLstatTest, ReturnedDirSizeIsCorrect1) { TEST_F(FuseLstatReturnPropertySizeTest, ReturnedDirSizeIsCorrect1) {
struct ::stat result = CallDirLstatWithSize(SIZE1); struct ::stat result = CallDirLstatWithValue(SIZE1);
EXPECT_EQ(SIZE1, result.st_size); EXPECT_EQ(SIZE1, result.st_size);
} }
TEST_F(FuseLstatTest, ReturnedDirSizeIsCorrect2) { TEST_F(FuseLstatReturnPropertySizeTest, ReturnedDirSizeIsCorrect2) {
struct ::stat result = CallDirLstatWithSize(SIZE2); struct ::stat result = CallDirLstatWithValue(SIZE2);
EXPECT_EQ(SIZE2, result.st_size); EXPECT_EQ(SIZE2, result.st_size);
} }
TEST_F(FuseLstatTest, ReturnedDirSizeIsCorrect3) { TEST_F(FuseLstatReturnPropertySizeTest, ReturnedDirSizeIsCorrect3) {
struct ::stat result = CallDirLstatWithSize(SIZE3); struct ::stat result = CallDirLstatWithValue(SIZE3);
EXPECT_EQ(SIZE3, result.st_size); EXPECT_EQ(SIZE3, result.st_size);
} }
TEST_F(FuseLstatTest, ReturnedFileNlinkIsCorrect1) { TEST_F(FuseLstatReturnPropertyNlinkTest, ReturnedFileNlinkIsCorrect1) {
struct ::stat result = CallFileLstatWithNlink(NLINK1); struct ::stat result = CallDirLstatWithValue(NLINK1);
EXPECT_EQ(NLINK1, result.st_nlink); EXPECT_EQ(NLINK1, result.st_nlink);
} }
TEST_F(FuseLstatTest, ReturnedFileNlinkIsCorrect2) { TEST_F(FuseLstatReturnPropertyNlinkTest, ReturnedFileNlinkIsCorrect2) {
struct ::stat result = CallFileLstatWithNlink(NLINK2); struct ::stat result = CallDirLstatWithValue(NLINK2);
EXPECT_EQ(NLINK2, result.st_nlink); EXPECT_EQ(NLINK2, result.st_nlink);
} }
TEST_F(FuseLstatTest, ReturnedDirNlinkIsCorrect1) { TEST_F(FuseLstatReturnPropertyNlinkTest, ReturnedDirNlinkIsCorrect1) {
struct ::stat result = CallDirLstatWithNlink(NLINK1); struct ::stat result = CallDirLstatWithValue(NLINK1);
EXPECT_EQ(NLINK1, result.st_nlink); EXPECT_EQ(NLINK1, result.st_nlink);
} }
TEST_F(FuseLstatTest, ReturnedDirNlinkIsCorrect2) { TEST_F(FuseLstatReturnPropertyNlinkTest, ReturnedDirNlinkIsCorrect2) {
struct ::stat result = CallDirLstatWithNlink(NLINK2); struct ::stat result = CallDirLstatWithValue(NLINK2);
EXPECT_EQ(NLINK2, result.st_nlink); EXPECT_EQ(NLINK2, result.st_nlink);
} }