xref: /aosp_15_r20/external/private-join-and-compute/private_join_and_compute/util/file_test.cc (revision a6aa18fbfbf9cb5cd47356a9d1b057768998488c)
1 /*
2  * Copyright 2019 Google LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     https://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "private_join_and_compute/util/file.h"
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 
21 #include <memory>
22 #include <string>
23 
24 #include "private_join_and_compute/util/status.inc"
25 
26 namespace private_join_and_compute {
27 namespace {
28 
29 template <typename T1, typename T2>
AssertOkAndHolds(const T1 & expected_value,const StatusOr<T2> & status_or)30 void AssertOkAndHolds(const T1& expected_value, const StatusOr<T2>& status_or) {
31   EXPECT_TRUE(status_or.ok()) << status_or.status();
32   EXPECT_EQ(expected_value, status_or.value());
33 }
34 
35 class FileTest : public testing::Test {
36  public:
FileTest()37   FileTest() : testing::Test(), f_(File::GetFile()) {}
38 
39   std::unique_ptr<File> f_;
40 };
41 
TEST_F(FileTest,WriteDataThenReadTest)42 TEST_F(FileTest, WriteDataThenReadTest) {
43   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp.txt", "wb").ok());
44   EXPECT_TRUE(f_->Write("water", 4).ok());
45   EXPECT_TRUE(f_->Close().ok());
46   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp.txt", "rb").ok());
47   AssertOkAndHolds(true, f_->HasMore());
48   AssertOkAndHolds("wat", f_->Read(3));
49   AssertOkAndHolds(true, f_->HasMore());
50   AssertOkAndHolds("e", f_->Read(1));
51   AssertOkAndHolds(false, f_->HasMore());
52   EXPECT_TRUE(f_->Close().ok());
53 }
54 
TEST_F(FileTest,ReadLineTest)55 TEST_F(FileTest, ReadLineTest) {
56   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp.txt", "wb").ok());
57   EXPECT_TRUE(f_->Write("Line1\nLine2\n\n", 13).ok());
58   EXPECT_TRUE(f_->Close().ok());
59   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp.txt", "r").ok());
60   AssertOkAndHolds(true, f_->HasMore());
61   AssertOkAndHolds("Line1", f_->ReadLine());
62   AssertOkAndHolds(true, f_->HasMore());
63   AssertOkAndHolds("Line2", f_->ReadLine());
64   AssertOkAndHolds(true, f_->HasMore());
65   AssertOkAndHolds("", f_->ReadLine());
66   AssertOkAndHolds(false, f_->HasMore());
67   EXPECT_TRUE(f_->Close().ok());
68 }
69 
TEST_F(FileTest,CannotOpenFileIfAnotherFileIsAlreadyOpened)70 TEST_F(FileTest, CannotOpenFileIfAnotherFileIsAlreadyOpened) {
71   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp.txt", "w").ok());
72   EXPECT_FALSE(f_->Open(testing::TempDir() + "/tmp1.txt", "w").ok());
73   EXPECT_TRUE(f_->Close().ok());
74 }
75 
TEST_F(FileTest,AllOperationsFailWhenThereIsNoOpenedFile)76 TEST_F(FileTest, AllOperationsFailWhenThereIsNoOpenedFile) {
77   EXPECT_FALSE(f_->Close().ok());
78   EXPECT_FALSE(f_->HasMore().ok());
79   EXPECT_FALSE(f_->Read(1).ok());
80   EXPECT_FALSE(f_->ReadLine().ok());
81   EXPECT_FALSE(f_->Write("w", 1).ok());
82 }
83 
TEST_F(FileTest,AllOperationsFailWhenThereIsNoOpenedFileAfterClosing)84 TEST_F(FileTest, AllOperationsFailWhenThereIsNoOpenedFileAfterClosing) {
85   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp.txt", "w").ok());
86   EXPECT_TRUE(f_->Close().ok());
87   EXPECT_FALSE(f_->Close().ok());
88   EXPECT_FALSE(f_->HasMore().ok());
89   EXPECT_FALSE(f_->Read(1).ok());
90   EXPECT_FALSE(f_->ReadLine().ok());
91   EXPECT_FALSE(f_->Write("w", 1).ok());
92 }
93 
TEST_F(FileTest,TestRename)94 TEST_F(FileTest, TestRename) {
95   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp.txt", "w").ok());
96   EXPECT_TRUE(f_->Write("water", 5).ok());
97   EXPECT_TRUE(f_->Close().ok());
98   EXPECT_TRUE(RenameFile(testing::TempDir() + "/tmp.txt",
99                          testing::TempDir() + "/tmp1.txt")
100                   .ok());
101   EXPECT_FALSE(f_->Open(testing::TempDir() + "/tmp.txt", "r").ok());
102   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp1.txt", "r").ok());
103   AssertOkAndHolds(true, f_->HasMore());
104   AssertOkAndHolds("water", f_->Read(5));
105   AssertOkAndHolds(false, f_->HasMore());
106   EXPECT_TRUE(f_->Close().ok());
107 }
108 
TEST_F(FileTest,TestDelete)109 TEST_F(FileTest, TestDelete) {
110   // Create file and delete it.
111   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp.txt", "w").ok());
112   EXPECT_TRUE(f_->Write("water", 5).ok());
113   EXPECT_TRUE(f_->Close().ok());
114   EXPECT_TRUE(DeleteFile(testing::TempDir() + "/tmp.txt").ok());
115   EXPECT_FALSE(f_->Open(testing::TempDir() + "/tmp.txt", "r").ok());
116 
117   // Try to delete nonexistent file.
118   EXPECT_FALSE(DeleteFile(testing::TempDir() + "/tmp2.txt").ok());
119 }
120 
TEST_F(FileTest,JoinPathWithMultipleArgs)121 TEST_F(FileTest, JoinPathWithMultipleArgs) {
122   std::string ret = JoinPath("/tmp", "foo", "bar/", "/baz/");
123   EXPECT_EQ("/tmp/foo.bar.baz", ret);
124 }
125 
TEST_F(FileTest,JoinPathWithMultipleArgsStartingWithEndSlashDir)126 TEST_F(FileTest, JoinPathWithMultipleArgsStartingWithEndSlashDir) {
127   std::string ret = JoinPath("/tmp/", "foo", "bar/", "/baz/");
128   EXPECT_EQ("/tmp/foo.bar.baz", ret);
129 }
130 
TEST_F(FileTest,ReadLineWithCarriageReturnsTest)131 TEST_F(FileTest, ReadLineWithCarriageReturnsTest) {
132   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp.txt", "wb").ok());
133   std::string file_string = "Line1\nLine2\r\nLine3\r\nLine4\n\n";
134   EXPECT_TRUE(f_->Write(file_string, file_string.size()).ok());
135   EXPECT_TRUE(f_->Close().ok());
136   EXPECT_TRUE(f_->Open(testing::TempDir() + "/tmp.txt", "r").ok());
137   AssertOkAndHolds(true, f_->HasMore());
138   AssertOkAndHolds("Line1", f_->ReadLine());
139   AssertOkAndHolds(true, f_->HasMore());
140   AssertOkAndHolds("Line2", f_->ReadLine());
141   AssertOkAndHolds(true, f_->HasMore());
142   AssertOkAndHolds("Line3", f_->ReadLine());
143   AssertOkAndHolds(true, f_->HasMore());
144   AssertOkAndHolds("Line4", f_->ReadLine());
145   AssertOkAndHolds(true, f_->HasMore());
146   AssertOkAndHolds("", f_->ReadLine());
147   AssertOkAndHolds(false, f_->HasMore());
148   EXPECT_TRUE(f_->Close().ok());
149 }
150 
151 }  // namespace
152 }  // namespace private_join_and_compute
153