1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include "testutils/FuseCreateAndOpenTest.h"
using ::testing::Eq;
using ::testing::WithParamInterface;
using ::testing::Values;
using ::testing::Invoke;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
class FuseCreateAndOpenFileDescriptorTest: public FuseCreateAndOpenTest, public WithParamInterface<int> {
public:
void CreateAndOpenAndReadFile(const char *filename) {
auto fs = TestFS();
auto fd = CreateAndOpenFile(fs.get(), filename);
ReadFile(fd->fd());
}
private:
unique_ref<OpenFileHandle> CreateAndOpenFile(const TempTestFS *fs, const char *filename) {
auto realpath = fs->mountDir() / filename;
auto fd = make_unique_ref<OpenFileHandle>(realpath.string().c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
EXPECT_GE(fd->fd(), 0) << "Creating file failed";
return fd;
}
void ReadFile(int fd) {
uint8_t buf = 0;
const int retval = ::read(fd, &buf, 1);
EXPECT_EQ(1, retval) << "Reading file failed";
}
};
INSTANTIATE_TEST_SUITE_P(FuseCreateAndOpenFileDescriptorTest, FuseCreateAndOpenFileDescriptorTest, Values(0, 2, 5, 1000, 1024*1024*1024));
TEST_P(FuseCreateAndOpenFileDescriptorTest, TestReturnedFileDescriptor) {
bool created = false;
ReturnIsFileOnLstatWithSizeIfFlagIsSet(FILENAME, fspp::num_bytes_t(100), &created);
EXPECT_CALL(*fsimpl, createAndOpenFile(Eq(FILENAME), testing::_, testing::_, testing::_))
.Times(1).WillOnce(Invoke([&] () {
ASSERT(!created, "called createAndOpenFile multiple times");
created = true;
return GetParam();
}));
EXPECT_CALL(*fsimpl, read(Eq(GetParam()), testing::_, testing::_, testing::_)).Times(1).WillOnce(Invoke([] () {
return fspp::num_bytes_t(1);
}));
CreateAndOpenAndReadFile(FILENAME);
}
|