File: FuseWriteErrorTest.cpp

package info (click to toggle)
cryfs 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,404 kB
  • sloc: cpp: 150,188; asm: 10,493; python: 1,455; javascript: 65; sh: 50; makefile: 17; xml: 7
file content (60 lines) | stat: -rw-r--r-- 2,472 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
#include <cstddef>

#include "testutils/FuseWriteTest.h"

#include "fspp/fs_interface/FuseErrnoException.h"

using ::testing::WithParamInterface;
using ::testing::Values;
using ::testing::Eq;
using ::testing::Ne;
using ::testing::Invoke;
using ::testing::Throw;

using namespace fspp::fuse;

class FuseWriteErrorTest: public FuseWriteTest, public WithParamInterface<int> {
public:
  fspp::num_bytes_t FILESIZE = fspp::num_bytes_t(64*1024*1024);
  fspp::num_bytes_t WRITECOUNT = fspp::num_bytes_t(32*1024*1024);

  void SetUp() override {
    //Make the file size big enough that fuse should issue at least two writes
    ReturnIsFileOnLstatWithSize(FILENAME, FILESIZE);
    OnOpenReturnFileDescriptor(FILENAME, 0);
  }
};
INSTANTIATE_TEST_SUITE_P(FuseWriteErrorTest, FuseWriteErrorTest, Values(EAGAIN, EBADF, EDESTADDRREQ, EDQUOT, EFAULT, EFBIG, EINTR, EINVAL, EIO, ENOSPC, EPIPE, EOVERFLOW, ESPIPE, ENXIO));


TEST_P(FuseWriteErrorTest, ReturnErrorOnFirstWriteCall) {
  EXPECT_CALL(*fsimpl, write(0, testing::_, testing::_, testing::_))
    .WillRepeatedly(Throw(FuseErrnoException(GetParam())));

  char *buf = new char[WRITECOUNT.value()];
  auto retval = WriteFileReturnError(FILENAME, buf, WRITECOUNT, fspp::num_bytes_t(0));
  EXPECT_EQ(GetParam(), retval.error);
  delete[] buf;
}

TEST_P(FuseWriteErrorTest, ReturnErrorOnSecondWriteCall) {
  // The first write request is from the beginning of the file and works, but the later ones fail.
  // We store the number of bytes the first call could successfully write and check later that our
  // write syscall returns exactly this number of bytes
  fspp::num_bytes_t successfullyWrittenBytes = fspp::num_bytes_t(-1);
  EXPECT_CALL(*fsimpl, write(0, testing::_, testing::_, Eq(fspp::num_bytes_t(0))))
    .Times(1)
    .WillOnce(Invoke([&successfullyWrittenBytes](int, const void*, fspp::num_bytes_t count, fspp::num_bytes_t) {
      // Store the number of successfully written bytes
      successfullyWrittenBytes = count;
    }));
  EXPECT_CALL(*fsimpl, write(0, testing::_, testing::_, Ne(fspp::num_bytes_t(0))))
    .WillRepeatedly(Throw(FuseErrnoException(GetParam())));

  char *buf = new char[WRITECOUNT.value()];
  auto retval = WriteFileReturnError(FILENAME, buf, WRITECOUNT, fspp::num_bytes_t(0));
  EXPECT_EQ(0, retval.error);
  EXPECT_EQ(successfullyWrittenBytes, retval.written_bytes); // Check that we're getting the number of successfully written bytes (the first write call) returned
  delete[] buf;
}