File: FuseTest.h

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 (97 lines) | stat: -rw-r--r-- 5,250 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
#pragma once
#ifndef MESSMER_FSPP_TEST_TESTUTILS_FUSETEST_H_
#define MESSMER_FSPP_TEST_TESTUTILS_FUSETEST_H_

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include "fspp/fuse/Filesystem.h"
#include "fspp/fs_interface/FuseErrnoException.h"
#include "fspp/fuse/Fuse.h"
#include "fspp/fs_interface/Dir.h"

#include <boost/filesystem.hpp>

#include <cpp-utils/tempfile/TempDir.h>
#include "FuseThread.h"

class MockFilesystem: public fspp::fuse::Filesystem {
public:
  MockFilesystem();
  ~MockFilesystem() override;

  MOCK_METHOD(void, setContext, (fspp::Context&&), (override));
  MOCK_METHOD(int, openFile, (const boost::filesystem::path&, int), (override));
  MOCK_METHOD(void, closeFile, (int), (override));
  MOCK_METHOD(void, lstat, (const boost::filesystem::path&, fspp::fuse::STAT*), (override));
  MOCK_METHOD(void, truncate, (const boost::filesystem::path&, fspp::num_bytes_t), (override));
  MOCK_METHOD(fspp::num_bytes_t, read, (int, void*, fspp::num_bytes_t, fspp::num_bytes_t), (override));
  MOCK_METHOD(void, write, (int, const void*, fspp::num_bytes_t, fspp::num_bytes_t), (override));
  MOCK_METHOD(void, flush, (int), (override));
  MOCK_METHOD(void, fsync, (int), (override));
  MOCK_METHOD(void, fdatasync, (int), (override));
  MOCK_METHOD(void, access, (const boost::filesystem::path&, int), (override));
  MOCK_METHOD(int, createAndOpenFile, (const boost::filesystem::path&, mode_t, uid_t, gid_t), (override));
  MOCK_METHOD(void, mkdir, (const boost::filesystem::path&, mode_t, uid_t, gid_t), (override));
  MOCK_METHOD(void, rmdir, (const boost::filesystem::path&), (override));
  MOCK_METHOD(void, unlink, (const boost::filesystem::path&), (override));
  MOCK_METHOD(void, rename, (const boost::filesystem::path&, const boost::filesystem::path&), (override));
  MOCK_METHOD(std::vector<fspp::Dir::Entry>, readDir, (const boost::filesystem::path &path), (override));
  MOCK_METHOD(void, utimens, (const boost::filesystem::path&, timespec, timespec), (override));
  MOCK_METHOD(void, statfs, (struct statvfs*), (override));
  MOCK_METHOD(void, chmod, (const boost::filesystem::path&, mode_t), (override));
  MOCK_METHOD(void, chown, (const boost::filesystem::path&, uid_t, gid_t), (override));
  MOCK_METHOD(void, createSymlink, (const boost::filesystem::path&, const boost::filesystem::path&, uid_t, gid_t), (override));
  MOCK_METHOD(void, readSymlink, (const boost::filesystem::path&, char*, fspp::num_bytes_t), (override));
};

class FuseTest: public ::testing::Test {
public:
  static constexpr const char* FILENAME = "/myfile";

  FuseTest();

  class TempTestFS {
  public:
    TempTestFS(std::shared_ptr<MockFilesystem> fsimpl, const std::vector<std::string>& fuseOptions = {});
    virtual ~TempTestFS();
  public:
    const boost::filesystem::path &mountDir() const;
  private:
    cpputils::TempDir _mountDir;
    fspp::fuse::Fuse _fuse;
    FuseThread _fuse_thread;
  };

  cpputils::unique_ref<TempTestFS> TestFS(const std::vector<std::string>& fuseOptions = {});

  std::shared_ptr<MockFilesystem> fsimpl;

  const fspp::Context& context() const {
      ASSERT(_context != boost::none, "Context wasn't correctly initialized");
      return *_context;
  }
private:
  boost::optional<fspp::Context> _context;

public:

  //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
  static ::testing::Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> ReturnIsFile; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
  static ::testing::Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> ReturnIsFileWithSize(fspp::num_bytes_t size); // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
  static ::testing::Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> ReturnIsFileWithSizeIfFlagIsSet(fspp::num_bytes_t size, const bool* flag); // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
  static ::testing::Action<void(int, fspp::fuse::STAT*)> ReturnIsFileFstat; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
  static ::testing::Action<void(int, fspp::fuse::STAT*)> ReturnIsFileFstatWithSize(fspp::num_bytes_t size); // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
  static ::testing::Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> ReturnIsDir; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
  static ::testing::Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> ReturnDoesntExist; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

  void ReturnIsFileOnLstat(const boost::filesystem::path &path);
  void ReturnIsFileOnLstatIfFlagIsSet(const boost::filesystem::path &path, const bool *created);
  void ReturnIsFileOnLstatWithSizeIfFlagIsSet(const boost::filesystem::path &path, const fspp::num_bytes_t size, const bool* created);
  void ReturnIsFileOnLstatWithSize(const boost::filesystem::path &path, fspp::num_bytes_t size);
  void ReturnIsDirOnLstat(const boost::filesystem::path &path);
  void ReturnDoesntExistOnLstat(const boost::filesystem::path &path);
  void OnOpenReturnFileDescriptor(const char *filename, int descriptor);
};

#endif