File: FuseTest.cpp

package info (click to toggle)
cryfs 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,412 kB
  • sloc: cpp: 150,187; asm: 10,493; python: 1,455; javascript: 65; sh: 50; makefile: 17; xml: 7
file content (149 lines) | stat: -rw-r--r-- 6,208 bytes parent folder | download
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "FuseTest.h"

using ::testing::Eq;
using ::testing::Return;
using ::testing::Throw;
using ::testing::Action;
using ::testing::Invoke;
using std::make_shared;
using std::shared_ptr;

using cpputils::unique_ref;
using cpputils::make_unique_ref;

namespace bf = boost::filesystem;

using namespace fspp::fuse;

MockFilesystem::MockFilesystem() {}
MockFilesystem::~MockFilesystem() {}

FuseTest::FuseTest(): fsimpl(make_shared<MockFilesystem>()), _context(boost::none) {
  using ::testing::_;
  auto defaultAction = Throw(FuseErrnoException(EIO));
  ON_CALL(*fsimpl, openFile(_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, closeFile(_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, lstat(_,_)).WillByDefault(Throw(FuseErrnoException(ENOENT)));
  ON_CALL(*fsimpl, truncate(_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, read(_,_,_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, write(_,_,_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, fsync(_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, fdatasync(_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, access(_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, createAndOpenFile(_,_,_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, mkdir(_,_,_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, rmdir(_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, unlink(_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, rename(_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, readDir(_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, utimens(_,_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, statfs(_)).WillByDefault(Invoke([](struct statvfs *result) {
      ::statvfs("/", result); // As dummy value take the values from the root filesystem
  }));
  ON_CALL(*fsimpl, chmod(_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, chown(_,_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, createSymlink(_,_,_,_)).WillByDefault(defaultAction);
  ON_CALL(*fsimpl, readSymlink(_,_,_)).WillByDefault(defaultAction);

  EXPECT_CALL(*fsimpl, access(_,_)).WillRepeatedly(Return());

  ON_CALL(*fsimpl, setContext(_)).WillByDefault(Invoke([this] (fspp::Context context) {
      _context = std::move(context);
  }));

  ReturnIsDirOnLstat("/");
  ReturnDoesntExistOnLstat("/.Trash");
  ReturnDoesntExistOnLstat("/.Trash-1000");
}

unique_ref<FuseTest::TempTestFS> FuseTest::TestFS(const std::vector<std::string>& fuseOptions) {
  return make_unique_ref<TempTestFS>(fsimpl, fuseOptions);
}

FuseTest::TempTestFS::TempTestFS(shared_ptr<MockFilesystem> fsimpl, const std::vector<std::string>& fuseOptions)
 :_mountDir(),
  _fuse([fsimpl] (Fuse*) {return fsimpl;}, []{}, "fusetest", boost::none), _fuse_thread(&_fuse) {

  _fuse_thread.start(_mountDir.path(), fuseOptions);
}

FuseTest::TempTestFS::~TempTestFS() {
  _fuse_thread.stop();
}

const bf::path &FuseTest::TempTestFS::mountDir() const {
  return _mountDir.path();
}

Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> FuseTest::ReturnIsFileWithSize(fspp::num_bytes_t size) {
  return Invoke([size](const boost::filesystem::path&, fspp::fuse::STAT* result) {
    result->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
    result->st_nlink = 1;
    result->st_size = size.value();
  });
}

Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> FuseTest::ReturnIsFileWithSizeIfFlagIsSet(fspp::num_bytes_t size, const bool* flag) {
  return Invoke([size, flag](const boost::filesystem::path&, fspp::fuse::STAT* result) {
    if (*flag) {
      result->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
      result->st_nlink = 1;
      result->st_size = size.value();
    } else {
      throw fspp::fuse::FuseErrnoException(ENOENT);
    }
  });
}

//TODO Combine ReturnIsFile and ReturnIsFileFstat. This should be possible in gmock by either (a) using ::testing::Undefined as parameter type or (b) using action macros
Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> FuseTest::ReturnIsFile = ReturnIsFileWithSize(fspp::num_bytes_t(0));

Action<void(int, fspp::fuse::STAT*)> FuseTest::ReturnIsFileFstat =
  Invoke([](int, fspp::fuse::STAT* result) {
    result->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
    result->st_nlink = 1;
  });

Action<void(int, fspp::fuse::STAT*)> FuseTest::ReturnIsFileFstatWithSize(fspp::num_bytes_t size) {
  return Invoke([size](int, struct ::stat *result) {
      result->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
      result->st_nlink = 1;
      result->st_size = size.value();
  });
}

Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> FuseTest::ReturnIsDir =
  Invoke([](const boost::filesystem::path&, fspp::fuse::STAT* result) {
    result->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH;
    result->st_nlink = 1;
  });

Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> FuseTest::ReturnDoesntExist = Throw(fspp::fuse::FuseErrnoException(ENOENT));

void FuseTest::OnOpenReturnFileDescriptor(const char *filename, int descriptor) {
  EXPECT_CALL(*fsimpl, openFile(Eq(filename), testing::_)).Times(1).WillOnce(Return(descriptor));
}

void FuseTest::ReturnIsFileOnLstat(const bf::path &path) {
  EXPECT_CALL(*fsimpl, lstat(Eq(path), ::testing::_)).WillRepeatedly(ReturnIsFile);
}

void FuseTest::ReturnIsFileOnLstatIfFlagIsSet(const bf::path &path, const bool* created) {
  ReturnIsFileOnLstatWithSizeIfFlagIsSet(path, fspp::num_bytes_t(0), created);
}

void FuseTest::ReturnIsFileOnLstatWithSizeIfFlagIsSet(const bf::path &path, const fspp::num_bytes_t size, const bool* created) {
  EXPECT_CALL(*fsimpl, lstat(Eq(path), ::testing::_)).WillRepeatedly(ReturnIsFileWithSizeIfFlagIsSet(size, created));
}

void FuseTest::ReturnIsFileOnLstatWithSize(const bf::path &path, const fspp::num_bytes_t size) {
  EXPECT_CALL(*fsimpl, lstat(Eq(path), ::testing::_)).WillRepeatedly(ReturnIsFileWithSize(size));
}

void FuseTest::ReturnIsDirOnLstat(const bf::path &path) {
  EXPECT_CALL(*fsimpl, lstat(Eq(path), ::testing::_)).WillRepeatedly(ReturnIsDir);
}

void FuseTest::ReturnDoesntExistOnLstat(const bf::path &path) {
  EXPECT_CALL(*fsimpl, lstat(Eq(path), ::testing::_)).WillRepeatedly(ReturnDoesntExist);
}