libcryfs/test/impl/FuseOpenFileListTest.cpp

129 lines
3.1 KiB
C++
Raw Normal View History

2015-02-17 00:48:49 +01:00
#include "google/gtest/gtest.h"
#include "google/gmock/gmock.h"
2015-02-17 00:48:49 +01:00
#include "../../impl/FuseOpenFileList.h"
2014-11-16 02:42:34 +01:00
#include <stdexcept>
using std::unique_ptr;
using std::make_unique;
using namespace fspp;
2014-11-16 02:42:34 +01:00
class MockOpenFile: public OpenFile {
public:
2014-11-16 02:42:34 +01:00
MockOpenFile(int fileid_, int flags_): fileid(fileid_), flags(flags_), destructed(false) {}
int fileid, flags;
bool destructed;
~MockOpenFile() {destructed = true;}
MOCK_CONST_METHOD1(stat, void(struct ::stat*));
MOCK_CONST_METHOD1(truncate, void(off_t));
2015-03-11 01:05:16 +01:00
MOCK_METHOD3(read, ssize_t(void*, size_t, off_t));
2014-11-16 02:42:34 +01:00
MOCK_METHOD3(write, void(const void*, size_t, off_t));
2014-11-21 01:11:24 +01:00
MOCK_METHOD0(flush, void());
2014-11-16 02:42:34 +01:00
MOCK_METHOD0(fsync, void());
MOCK_METHOD0(fdatasync, void());
};
2014-11-16 03:01:41 +01:00
struct FuseOpenFileListTest: public ::testing::Test {
const int FILEID1 = 4;
const int FLAGS1 = 5;
const int FILEID2 = 6;
const int FLAGS2 = 7;
const int FILEID3 = 8;
const int FLAGS3 = 9;
2014-11-16 02:42:34 +01:00
FuseOpenFileList list;
2014-11-16 03:01:41 +01:00
int open(int fileid, int flags) {
return list.open(make_unique<MockOpenFile>(fileid, flags));
2014-11-16 03:01:41 +01:00
}
int open() {
return open(FILEID1, FILEID2);
}
void check(int id, int fileid, int flags) {
MockOpenFile *openFile = dynamic_cast<MockOpenFile*>(list.get(id));
EXPECT_EQ(fileid, openFile->fileid);
EXPECT_EQ(flags, openFile->flags);
}
};
TEST_F(FuseOpenFileListTest, EmptyList1) {
2014-11-16 02:42:34 +01:00
ASSERT_THROW(list.get(0), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, EmptyList2) {
2014-11-16 02:42:34 +01:00
ASSERT_THROW(list.get(3), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, InvalidId) {
int valid_id = open();
2014-11-16 02:42:34 +01:00
int invalid_id = valid_id + 1;
ASSERT_THROW(list.get(invalid_id), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, Open1AndGet) {
int id = open(FILEID1, FLAGS1);
check(id, FILEID1, FLAGS1);
2014-11-16 02:42:34 +01:00
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, Open2AndGet) {
int id1 = open(FILEID1, FLAGS1);
int id2 = open(FILEID2, FLAGS2);
2014-11-16 02:42:34 +01:00
2014-11-16 03:01:41 +01:00
check(id1, FILEID1, FLAGS1);
check(id2, FILEID2, FLAGS2);
2014-11-16 02:42:34 +01:00
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, Open3AndGet) {
int id1 = open(FILEID1, FLAGS1);
int id2 = open(FILEID2, FLAGS2);
int id3 = open(FILEID3, FLAGS3);
2014-11-16 02:42:34 +01:00
2014-11-16 03:01:41 +01:00
check(id1, FILEID1, FLAGS1);
check(id3, FILEID3, FLAGS3);
check(id2, FILEID2, FLAGS2);
2014-11-16 02:42:34 +01:00
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, DestructOnClose) {
int id = open();
2014-11-16 02:42:34 +01:00
MockOpenFile *openFile = dynamic_cast<MockOpenFile*>(list.get(id));
EXPECT_FALSE(openFile->destructed);
list.close(id);
EXPECT_TRUE(openFile->destructed);
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, GetClosedItemOnEmptyList) {
int id = open();
2014-11-16 02:42:34 +01:00
ASSERT_NO_THROW(list.get(id));
list.close(id);
ASSERT_THROW(list.get(id), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, GetClosedItemOnNonEmptyList) {
int id = open();
open();
2014-11-16 02:42:34 +01:00
ASSERT_NO_THROW(list.get(id));
list.close(id);
ASSERT_THROW(list.get(id), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, CloseOnEmptyList1) {
2014-11-16 02:42:34 +01:00
ASSERT_THROW(list.close(0), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, CloseOnEmptyList2) {
2014-11-16 02:42:34 +01:00
ASSERT_THROW(list.close(4), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(FuseOpenFileListTest, RemoveInvalidId) {
int valid_id = open();
2014-11-16 02:42:34 +01:00
int invalid_id = valid_id + 1;
ASSERT_THROW(list.close(invalid_id), std::out_of_range);
}