#include "platform_v2/impl/shared/file.h" #include #include #include #include #include "file/util/temp_path.h" #include "platform_v2/base/byte_array.h" #include "gtest/gtest.h" #include "absl/strings/string_view.h" namespace location { namespace nearby { namespace shared { class FileTest : public ::testing::Test { protected: void SetUp() override { temp_path_ = std::make_unique(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& bytes, const std::string& expected) { EXPECT_TRUE(bytes.ok()); EXPECT_EQ(std::string(bytes.result()), expected); } void AssertEmpty(const ExceptionOr& bytes) { EXPECT_TRUE(bytes.ok()); EXPECT_TRUE(bytes.result().Empty()); } static constexpr int64_t kMaxSize = 3; std::unique_ptr temp_path_; std::string path_; std::fstream file_; size_t size_ = 0; }; TEST_F(FileTest, InputFile_NonExistentPath) { InputFile input_file("/not/a/valid/path.txt", GetSize()); ExceptionOr read_result = input_file.Read(kMaxSize); EXPECT_FALSE(read_result.ok()); EXPECT_TRUE(read_result.GetException().Raised(Exception::kIo)); } TEST_F(FileTest, InputFile_GetFilePath) { InputFile input_file(path_, GetSize()); EXPECT_EQ(input_file.GetFilePath(), path_); } TEST_F(FileTest, InputFile_EmptyFileEOF) { InputFile input_file(path_, GetSize()); AssertEmpty(input_file.Read(kMaxSize)); } TEST_F(FileTest, InputFile_ReadWorks) { WriteToFile("abc"); InputFile input_file(path_, GetSize()); input_file.Read(kMaxSize); SUCCEED(); } TEST_F(FileTest, InputFile_ReadUntilEOF) { WriteToFile("abc"); InputFile input_file(path_, GetSize()); AssertEquals(input_file.Read(kMaxSize), "abc"); AssertEmpty(input_file.Read(kMaxSize)); } TEST_F(FileTest, InputFile_ReadWithSize) { WriteToFile("abc"); InputFile input_file(path_, GetSize()); AssertEquals(input_file.Read(2), "ab"); AssertEquals(input_file.Read(1), "c"); AssertEmpty(input_file.Read(kMaxSize)); } TEST_F(FileTest, InputFile_GetTotalSize) { WriteToFile("abc"); InputFile input_file(path_, GetSize()); EXPECT_EQ(input_file.GetTotalSize(), 3); AssertEquals(input_file.Read(1), "a"); EXPECT_EQ(input_file.GetTotalSize(), 3); } TEST_F(FileTest, InputFile_Close) { WriteToFile("abc"); InputFile input_file(path_, GetSize()); input_file.Close(); ExceptionOr read_result = input_file.Read(kMaxSize); EXPECT_FALSE(read_result.ok()); EXPECT_TRUE(read_result.GetException().Raised(Exception::kIo)); } TEST_F(FileTest, OutputFile_NonExistentPath) { OutputFile output_file("/not/a/valid/path.txt"); ByteArray bytes("a", 1); EXPECT_TRUE(output_file.Write(bytes).Raised(Exception::kIo)); } TEST_F(FileTest, OutputFile_Write) { OutputFile output_file(path_); ByteArray bytes1("a"); ByteArray bytes2("bc"); EXPECT_EQ(output_file.Write(bytes1), Exception{Exception::kSuccess}); EXPECT_EQ(output_file.Write(bytes2), Exception{Exception::kSuccess}); InputFile input_file(path_, GetSize()); AssertEquals(input_file.Read(kMaxSize), "abc"); } TEST_F(FileTest, OutputFile_Close) { OutputFile output_file(path_); output_file.Close(); ByteArray bytes("a"); EXPECT_EQ(output_file.Write(bytes), Exception{Exception::kIo}); } } // namespace shared } // namespace nearby } // namespace location