aboutsummaryrefslogtreecommitdiff
path: root/fcp/tensorflow/file_descriptor_filesystem_test.cc
blob: 669f82272afba71b8daca34c5d790aa45f6a1034 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

#include "fcp/tensorflow/file_descriptor_filesystem.h"

#include <fcntl.h>

#include <memory>
#include <vector>

#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "android-base/file.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "tensorflow/core/lib/core/status_test_util.h"

namespace tensorflow {
namespace fcp {
namespace {

using ::android::base::ReadFileToString;
using ::testing::TempDir;

static constexpr char kBadFdPath[] = "fd:///10000";
static constexpr char kFileContent[] = "abcdefgh";
static constexpr int kFileContentLen = 8;

class FileDescriptorFileSystemTest : public ::testing::Test {
 protected:
  void TearDown() override {
    if (fd_ != -1) {
      ASSERT_NE(-1, close(fd_));
    }
  }

  // Writes contents to a temp file and sets fd_path_ to point to it. The opened
  // file descriptor is closed automatically in TearDown(). To be called at most
  // once per test.
  void CreateAndOpenFdForTest(std::string contents) {
    ASSERT_EQ(-1, fd_);  // prevent accidental double-open
    file_name_ = TempDir() + "/fdtest";
    android::base::WriteStringToFile(contents, file_name_);

    fd_ = open(file_name_.c_str(), O_RDONLY);
    ASSERT_NE(-1, fd_);

    fd_path_ = absl::StrCat("fd:///", fd_);
  }

  void CreateTempFdForTest() {
    ASSERT_EQ(-1, fd_);  // prevent accidental double-open

    file_name_ = TempDir() + "/fdtest";
    android::base::WriteStringToFile("", file_name_);

    fd_ = open(file_name_.c_str(), O_WRONLY);
    ASSERT_NE(-1, fd_);

    fd_path_ = absl::StrCat("fd:///", fd_);
  }

  FileDescriptorFileSystem fd_fs_;
  string fd_path_;
  string file_name_;

 private:
  int fd_ = -1;
};

TEST_F(FileDescriptorFileSystemTest, WritableFile) {
  CreateTempFdForTest();

  std::unique_ptr<WritableFile> file;
  TF_ASSERT_OK(fd_fs_.NewWritableFile(fd_path_, &file));
  TF_ASSERT_OK(file->Append(kFileContent));
  TF_ASSERT_OK(file->Close());

  std::string actual_content;
  ASSERT_TRUE(ReadFileToString(file_name_, &actual_content));
  EXPECT_EQ(kFileContent, actual_content);
}

TEST_F(FileDescriptorFileSystemTest, WritableFileFailsOnInvalidFd) {
  std::unique_ptr<WritableFile> file;
  EXPECT_FALSE(fd_fs_.NewWritableFile(kBadFdPath, &file).ok());
}

TEST_F(FileDescriptorFileSystemTest, MalformedPathReturnsInvalidArgument) {
  FileStatistics stats;
  EXPECT_EQ(fd_fs_.Stat("fd://0", &stats).code(), error::INVALID_ARGUMENT);
  EXPECT_EQ(fd_fs_.Stat("fd://authority/0", &stats).code(),
            error::INVALID_ARGUMENT);
  EXPECT_EQ(fd_fs_.Stat("fd:///not-a-number", &stats).code(),
            error::INVALID_ARGUMENT);
}

TEST_F(FileDescriptorFileSystemTest, NewRandomAccessFile) {
  CreateAndOpenFdForTest(kFileContent);

  std::unique_ptr<RandomAccessFile> file;
  TF_ASSERT_OK(fd_fs_.NewRandomAccessFile(fd_path_, &file));
  StringPiece content;
  char scratch[kFileContentLen];
  TF_ASSERT_OK(file->Read(0, kFileContentLen, &content, scratch));

  EXPECT_EQ(kFileContent, content);
}

TEST_F(FileDescriptorFileSystemTest,
       NewRandomAccessFileFailsOnRequestMoreBytes) {
  CreateAndOpenFdForTest(kFileContent);

  std::unique_ptr<RandomAccessFile> file;
  TF_ASSERT_OK(fd_fs_.NewRandomAccessFile(fd_path_, &file));
  StringPiece content;
  char scratch[kFileContentLen];
  auto status = file->Read(0, kFileContentLen + 2, &content, scratch);
  EXPECT_EQ(status.code(), error::OUT_OF_RANGE);
  EXPECT_EQ(status.error_message(),
            "Read fewer bytes than requested. Total read bytes 8");
}

TEST_F(FileDescriptorFileSystemTest, NewRandomAccessFileFailsOnInvalidFd) {
  std::unique_ptr<RandomAccessFile> file;
  EXPECT_FALSE(fd_fs_.NewRandomAccessFile(kBadFdPath, &file).ok());
}

TEST_F(FileDescriptorFileSystemTest, NewRandomAccessFileFailsOnDirectoryFd) {
  int dir_fd = open(TempDir().c_str(), O_RDONLY | O_DIRECTORY);
  ASSERT_NE(-1, dir_fd);
  string dir_fd_path = absl::StrCat("fd:///", dir_fd);

  std::unique_ptr<RandomAccessFile> file;
  EXPECT_FALSE(fd_fs_.NewRandomAccessFile(dir_fd_path, &file).ok());

  close(dir_fd);
}

TEST_F(FileDescriptorFileSystemTest, GetMatchingPaths) {
  CreateAndOpenFdForTest(kFileContent);

  std::vector<string> paths;
  TF_EXPECT_OK(fd_fs_.GetMatchingPaths(fd_path_, &paths));

  ASSERT_EQ(1, paths.size());
  EXPECT_EQ(fd_path_, paths.at(0));
}

TEST_F(FileDescriptorFileSystemTest, GetMatchingPathsReturnsEmptyOnBadFd) {
  std::vector<string> paths;
  TF_EXPECT_OK(fd_fs_.GetMatchingPaths(kBadFdPath, &paths));
  EXPECT_TRUE(paths.empty());
}

TEST_F(FileDescriptorFileSystemTest, Stat) {
  CreateAndOpenFdForTest(kFileContent);

  FileStatistics stats;
  TF_EXPECT_OK(fd_fs_.Stat(fd_path_, &stats));

  EXPECT_EQ(kFileContentLen, stats.length);
  EXPECT_GT(stats.mtime_nsec, 0);
  EXPECT_FALSE(stats.is_directory);
}

TEST_F(FileDescriptorFileSystemTest, StatFailsOnBadFd) {
  FileStatistics stats;
  EXPECT_FALSE(fd_fs_.Stat(kBadFdPath, &stats).ok());
}

TEST_F(FileDescriptorFileSystemTest, GetFileSize) {
  CreateAndOpenFdForTest(kFileContent);

  uint64 size;
  TF_EXPECT_OK(fd_fs_.GetFileSize(fd_path_, &size));

  EXPECT_EQ(kFileContentLen, size);
}

TEST_F(FileDescriptorFileSystemTest, GetFileSizeFailsOnBadFd) {
  uint64 size;
  EXPECT_FALSE(fd_fs_.GetFileSize(kBadFdPath, &size).ok());
}

TEST_F(FileDescriptorFileSystemTest,
       NewReadOnlyMemoryRegionFromFileReturnsUnimplemented) {
  std::unique_ptr<ReadOnlyMemoryRegion> region;
  EXPECT_EQ(fd_fs_.NewReadOnlyMemoryRegionFromFile(kBadFdPath, &region).code(),
            error::UNIMPLEMENTED);
}

TEST_F(FileDescriptorFileSystemTest, FileExistsReturnsUnimplemented) {
  EXPECT_EQ(fd_fs_.FileExists(kBadFdPath).code(), error::UNIMPLEMENTED);
}

TEST_F(FileDescriptorFileSystemTest, GetChildrenReturnsUnimplemented) {
  std::vector<string> paths;
  EXPECT_EQ(fd_fs_.GetChildren(kBadFdPath, &paths).code(),
            error::UNIMPLEMENTED);
}

}  // anonymous namespace
}  // namespace fcp
}  // namespace tensorflow