summaryrefslogtreecommitdiff
path: root/chromium/third_party/nearby/src/internal/platform/implementation/shared/file_test.cc
blob: 367e9ff011be0dba6d6764de7b6d222c16fb6769 (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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "internal/platform/implementation/shared/file.h"

#include <cstring>
#include <fstream>
#include <memory>
#include <ostream>

#include "file/util/temp_path.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"

namespace location {
namespace nearby {
namespace shared {

class FileTest : public ::testing::Test {
 protected:
  void SetUp() override {
    temp_path_ = std::make_unique<TempPath>(TempPath::Local);
    path_ = temp_path_->path() + "/file.txt";
    std::ofstream output_file(path_);
    file_ = std::fstream(path_, std::fstream::in | std::fstream::out);
  }

  void WriteToFile(absl::string_view text) {
    file_ << text;
    file_.flush();
    size_ += text.size();
  }

  size_t GetSize() const { return size_; }

  void AssertEquals(const ExceptionOr<ByteArray>& bytes,
                    const std::string& expected) {
    EXPECT_TRUE(bytes.ok());
    EXPECT_EQ(std::string(bytes.result()), expected);
  }

  void AssertEmpty(const ExceptionOr<ByteArray>& bytes) {
    EXPECT_TRUE(bytes.ok());
    EXPECT_TRUE(bytes.result().Empty());
  }

  static constexpr int64_t kMaxSize = 3;

  std::unique_ptr<TempPath> temp_path_;
  std::string path_;
  std::fstream file_;
  size_t size_ = 0;
};

TEST_F(FileTest, IOFile_NonExistentPathInput) {
  auto io_file =
      shared::IOFile::CreateInputFile("/not/a/valid/path.txt", GetSize());
  ExceptionOr<ByteArray> read_result = io_file->Read(kMaxSize);
  EXPECT_FALSE(read_result.ok());
  EXPECT_TRUE(read_result.GetException().Raised(Exception::kIo));
}

TEST_F(FileTest, IOFile_GetFilePath) {
  auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
  EXPECT_EQ(io_file->GetFilePath(), path_);
}

TEST_F(FileTest, IOFile_EmptyFileEOF) {
  auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
  AssertEmpty(io_file->Read(kMaxSize));
}

TEST_F(FileTest, IOFile_ReadWorks) {
  WriteToFile("abc");
  auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
  io_file->Read(kMaxSize);
  SUCCEED();
}

TEST_F(FileTest, IOFile_ReadUntilEOF) {
  WriteToFile("abc");
  auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
  AssertEquals(io_file->Read(kMaxSize), "abc");
  AssertEmpty(io_file->Read(kMaxSize));
}

TEST_F(FileTest, IOFile_ReadWithSize) {
  WriteToFile("abc");
  auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
  AssertEquals(io_file->Read(2), "ab");
  AssertEquals(io_file->Read(1), "c");
  AssertEmpty(io_file->Read(kMaxSize));
}

TEST_F(FileTest, IOFile_GetTotalSize) {
  WriteToFile("abc");
  auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
  EXPECT_EQ(io_file->GetTotalSize(), 3);
  AssertEquals(io_file->Read(1), "a");
  EXPECT_EQ(io_file->GetTotalSize(), 3);
}

TEST_F(FileTest, IOFile_CloseInput) {
  WriteToFile("abc");
  auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
  io_file->Close();
  ExceptionOr<ByteArray> read_result = io_file->Read(kMaxSize);
  EXPECT_FALSE(read_result.ok());
  EXPECT_TRUE(read_result.GetException().Raised(Exception::kIo));
}

TEST_F(FileTest, IOFile_NonExistentPathOutput) {
  auto io_file = shared::IOFile::CreateOutputFile("/not/a/valid/path.txt");
  ByteArray bytes("a", 1);
  EXPECT_TRUE(io_file->Write(bytes).Raised(Exception::kIo));
}

TEST_F(FileTest, IOFile_Write) {
  auto io_file_output = shared::IOFile::CreateOutputFile(path_);
  ByteArray bytes1("a");
  ByteArray bytes2("bc");
  EXPECT_EQ(io_file_output->Write(bytes1), Exception{Exception::kSuccess});
  EXPECT_EQ(io_file_output->Write(bytes2), Exception{Exception::kSuccess});
  auto io_file_input = shared::IOFile::CreateInputFile(path_, GetSize());
  AssertEquals(io_file_input->Read(kMaxSize), "abc");
}

TEST_F(FileTest, IOFile_CloseOutput) {
  auto io_file = shared::IOFile::CreateOutputFile(path_);
  io_file->Close();
  ByteArray bytes("a");
  EXPECT_EQ(io_file->Write(bytes), Exception{Exception::kIo});
}

}  // namespace shared
}  // namespace nearby
}  // namespace location