1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "base/common_art_test.h"  // For ScratchFile
18 #include "base/file_utils.h"
19 #include "gtest/gtest.h"
20 #include "fd_file.h"
21 #include "random_access_file_test.h"
22 
23 namespace unix_file {
24 
25 class FdFileTest : public RandomAccessFileTest {
26  protected:
MakeTestFile()27   RandomAccessFile* MakeTestFile() override {
28     FILE* tmp = tmpfile();
29     int fd = art::DupCloexec(fileno(tmp));
30     fclose(tmp);
31     return new FdFile(fd, false);
32   }
33 };
34 
TEST_F(FdFileTest,Read)35 TEST_F(FdFileTest, Read) {
36   TestRead();
37 }
38 
TEST_F(FdFileTest,SetLength)39 TEST_F(FdFileTest, SetLength) {
40   TestSetLength();
41 }
42 
TEST_F(FdFileTest,Write)43 TEST_F(FdFileTest, Write) {
44   TestWrite();
45 }
46 
TEST_F(FdFileTest,UnopenedFile)47 TEST_F(FdFileTest, UnopenedFile) {
48   FdFile file;
49   EXPECT_EQ(FdFile::kInvalidFd, file.Fd());
50   EXPECT_FALSE(file.IsOpened());
51   EXPECT_TRUE(file.GetPath().empty());
52 }
53 
TEST_F(FdFileTest,IsOpenFd)54 TEST_F(FdFileTest, IsOpenFd) {
55   art::ScratchFile scratch_file;
56   FdFile* file = scratch_file.GetFile();
57   ASSERT_TRUE(file->IsOpened());
58   EXPECT_GE(file->Fd(), 0);
59   EXPECT_NE(file->Fd(), FdFile::kInvalidFd);
60   EXPECT_TRUE(FdFile::IsOpenFd(file->Fd()));
61   int old_fd = file->Fd();
62   ASSERT_TRUE(file != nullptr);
63   ASSERT_EQ(file->FlushClose(), 0);
64   EXPECT_FALSE(file->IsOpened());
65   EXPECT_FALSE(FdFile::IsOpenFd(old_fd));
66 }
67 
TEST_F(FdFileTest,OpenClose)68 TEST_F(FdFileTest, OpenClose) {
69   std::string good_path(GetTmpPath("some-file.txt"));
70   FdFile file(good_path, O_CREAT | O_WRONLY, true);
71   ASSERT_TRUE(file.IsOpened());
72   EXPECT_GE(file.Fd(), 0);
73   EXPECT_TRUE(file.IsOpened());
74   EXPECT_FALSE(file.ReadOnlyMode());
75   EXPECT_EQ(0, file.Flush());
76   EXPECT_EQ(0, file.Close());
77   EXPECT_EQ(FdFile::kInvalidFd, file.Fd());
78   EXPECT_FALSE(file.IsOpened());
79   FdFile file2(good_path, O_RDONLY, true);
80   EXPECT_TRUE(file2.IsOpened());
81   EXPECT_TRUE(file2.ReadOnlyMode());
82   EXPECT_GE(file2.Fd(), 0);
83 
84   ASSERT_EQ(file2.Close(), 0);
85   ASSERT_EQ(unlink(good_path.c_str()), 0);
86 }
87 
TEST_F(FdFileTest,ReadFullyEmptyFile)88 TEST_F(FdFileTest, ReadFullyEmptyFile) {
89   // New scratch file, zero-length.
90   art::ScratchFile tmp;
91   FdFile file(tmp.GetFilename(), O_RDONLY, false);
92   ASSERT_TRUE(file.IsOpened());
93   EXPECT_TRUE(file.ReadOnlyMode());
94   EXPECT_GE(file.Fd(), 0);
95   uint8_t buffer[16];
96   EXPECT_FALSE(file.ReadFully(&buffer, 4));
97 }
98 
99 template <size_t Size>
NullTerminateCharArray(char (& array)[Size])100 static void NullTerminateCharArray(char (&array)[Size]) {
101   array[Size - 1] = '\0';
102 }
103 
TEST_F(FdFileTest,ReadFullyWithOffset)104 TEST_F(FdFileTest, ReadFullyWithOffset) {
105   // New scratch file, zero-length.
106   art::ScratchFile tmp;
107   FdFile file(tmp.GetFilename(), O_RDWR, false);
108   ASSERT_TRUE(file.IsOpened());
109   EXPECT_GE(file.Fd(), 0);
110   EXPECT_FALSE(file.ReadOnlyMode());
111 
112   char ignore_prefix[20] = {'a', };
113   NullTerminateCharArray(ignore_prefix);
114   char read_suffix[10] = {'b', };
115   NullTerminateCharArray(read_suffix);
116 
117   off_t offset = 0;
118   // Write scratch data to file that we can read back into.
119   EXPECT_TRUE(file.Write(ignore_prefix, sizeof(ignore_prefix), offset));
120   offset += sizeof(ignore_prefix);
121   EXPECT_TRUE(file.Write(read_suffix, sizeof(read_suffix), offset));
122 
123   ASSERT_EQ(file.Flush(), 0);
124 
125   // Reading at an offset should only produce 'bbbb...', since we ignore the 'aaa...' prefix.
126   char buffer[sizeof(read_suffix)];
127   EXPECT_TRUE(file.PreadFully(buffer, sizeof(read_suffix), offset));
128   EXPECT_STREQ(&read_suffix[0], &buffer[0]);
129 
130   ASSERT_EQ(file.Close(), 0);
131 }
132 
TEST_F(FdFileTest,ReadWriteFullyWithOffset)133 TEST_F(FdFileTest, ReadWriteFullyWithOffset) {
134   // New scratch file, zero-length.
135   art::ScratchFile tmp;
136   FdFile file(tmp.GetFilename(), O_RDWR, false);
137   ASSERT_GE(file.Fd(), 0);
138   EXPECT_TRUE(file.IsOpened());
139   EXPECT_FALSE(file.ReadOnlyMode());
140 
141   const char* test_string = "This is a test string";
142   size_t length = strlen(test_string) + 1;
143   const size_t offset = 12;
144   std::unique_ptr<char[]> offset_read_string(new char[length]);
145   std::unique_ptr<char[]> read_string(new char[length]);
146 
147   // Write scratch data to file that we can read back into.
148   EXPECT_TRUE(file.PwriteFully(test_string, length, offset));
149   ASSERT_EQ(file.Flush(), 0);
150 
151   // Test reading both the offsets.
152   EXPECT_TRUE(file.PreadFully(&offset_read_string[0], length, offset));
153   EXPECT_STREQ(test_string, &offset_read_string[0]);
154 
155   EXPECT_TRUE(file.PreadFully(&read_string[0], length, 0u));
156   EXPECT_NE(memcmp(&read_string[0], test_string, length), 0);
157 
158   ASSERT_EQ(file.Close(), 0);
159 }
160 
TEST_F(FdFileTest,Copy)161 TEST_F(FdFileTest, Copy) {
162   art::ScratchFile src_tmp;
163   FdFile src(src_tmp.GetFilename(), O_RDWR, false);
164   ASSERT_GE(src.Fd(), 0);
165   ASSERT_TRUE(src.IsOpened());
166 
167   char src_data[] = "Some test data.";
168   ASSERT_TRUE(src.WriteFully(src_data, sizeof(src_data)));  // Including the zero terminator.
169   ASSERT_EQ(0, src.Flush());
170   ASSERT_EQ(static_cast<int64_t>(sizeof(src_data)), src.GetLength());
171 
172   art::ScratchFile dest_tmp;
173   FdFile dest(src_tmp.GetFilename(), O_RDWR, false);
174   ASSERT_GE(dest.Fd(), 0);
175   ASSERT_TRUE(dest.IsOpened());
176 
177   ASSERT_TRUE(dest.Copy(&src, 0, sizeof(src_data)));
178   ASSERT_EQ(0, dest.Flush());
179   ASSERT_EQ(static_cast<int64_t>(sizeof(src_data)), dest.GetLength());
180 
181   char check_data[sizeof(src_data)];
182   ASSERT_TRUE(dest.PreadFully(check_data, sizeof(src_data), 0u));
183   CHECK_EQ(0, memcmp(check_data, src_data, sizeof(src_data)));
184 
185   ASSERT_EQ(0, dest.Close());
186   ASSERT_EQ(0, src.Close());
187 }
188 
TEST_F(FdFileTest,MoveConstructor)189 TEST_F(FdFileTest, MoveConstructor) {
190   // New scratch file, zero-length.
191   art::ScratchFile tmp;
192   FdFile file(tmp.GetFilename(), O_RDWR, false);
193   ASSERT_TRUE(file.IsOpened());
194   EXPECT_GE(file.Fd(), 0);
195 
196   int old_fd = file.Fd();
197 
198   FdFile file2(std::move(file));
199   EXPECT_FALSE(file.IsOpened());
200   EXPECT_TRUE(file2.IsOpened());
201   EXPECT_EQ(old_fd, file2.Fd());
202 
203   ASSERT_EQ(file2.Flush(), 0);
204   ASSERT_EQ(file2.Close(), 0);
205 }
206 
TEST_F(FdFileTest,OperatorMoveEquals)207 TEST_F(FdFileTest, OperatorMoveEquals) {
208   // Make sure the read_only_ flag is correctly copied
209   // over.
210   art::ScratchFile tmp;
211   FdFile file(tmp.GetFilename(), O_RDONLY, false);
212   ASSERT_TRUE(file.ReadOnlyMode());
213 
214   FdFile file2(tmp.GetFilename(), O_RDWR, false);
215   ASSERT_FALSE(file2.ReadOnlyMode());
216 
217   file2 = std::move(file);
218   ASSERT_TRUE(file2.ReadOnlyMode());
219 }
220 
TEST_F(FdFileTest,EraseWithPathUnlinks)221 TEST_F(FdFileTest, EraseWithPathUnlinks) {
222   // New scratch file, zero-length.
223   art::ScratchFile tmp;
224   std::string filename = tmp.GetFilename();
225   tmp.Close();  // This is required because of the unlink race between the scratch file and the
226                 // FdFile, which leads to close-guard breakage.
227   FdFile file(filename, O_RDWR, false);
228   ASSERT_TRUE(file.IsOpened());
229   EXPECT_GE(file.Fd(), 0);
230   uint8_t buffer[16] = { 0 };
231   EXPECT_TRUE(file.WriteFully(&buffer, sizeof(buffer)));
232   EXPECT_EQ(file.Flush(), 0);
233 
234   EXPECT_TRUE(file.Erase(true));
235 
236   EXPECT_FALSE(file.IsOpened());
237 
238   EXPECT_FALSE(art::OS::FileExists(filename.c_str())) << filename;
239 }
240 
TEST_F(FdFileTest,Compare)241 TEST_F(FdFileTest, Compare) {
242   std::vector<uint8_t> buffer;
243   constexpr int64_t length = 17 * art::KB;
244   for (size_t i = 0; i < length; ++i) {
245     buffer.push_back(static_cast<uint8_t>(i));
246   }
247 
248   auto reset_compare = [&](art::ScratchFile& a, art::ScratchFile& b) {
249     a.GetFile()->ResetOffset();
250     b.GetFile()->ResetOffset();
251     return a.GetFile()->Compare(b.GetFile());
252   };
253 
254   art::ScratchFile tmp;
255   EXPECT_TRUE(tmp.GetFile()->WriteFully(&buffer[0], length));
256   EXPECT_EQ(tmp.GetFile()->GetLength(), length);
257 
258   art::ScratchFile tmp2;
259   EXPECT_TRUE(tmp2.GetFile()->WriteFully(&buffer[0], length));
260   EXPECT_EQ(tmp2.GetFile()->GetLength(), length);
261 
262   // Basic equality check.
263   tmp.GetFile()->ResetOffset();
264   tmp2.GetFile()->ResetOffset();
265   // Files should be the same.
266   EXPECT_EQ(reset_compare(tmp, tmp2), 0);
267 
268   // Change a byte near the start.
269   ++buffer[2];
270   art::ScratchFile tmp3;
271   EXPECT_TRUE(tmp3.GetFile()->WriteFully(&buffer[0], length));
272   --buffer[2];
273   EXPECT_NE(reset_compare(tmp, tmp3), 0);
274 
275   // Change a byte near the middle.
276   ++buffer[length / 2];
277   art::ScratchFile tmp4;
278   EXPECT_TRUE(tmp4.GetFile()->WriteFully(&buffer[0], length));
279   --buffer[length / 2];
280   EXPECT_NE(reset_compare(tmp, tmp4), 0);
281 
282   // Change a byte near the end.
283   ++buffer[length - 5];
284   art::ScratchFile tmp5;
285   EXPECT_TRUE(tmp5.GetFile()->WriteFully(&buffer[0], length));
286   --buffer[length - 5];
287   EXPECT_NE(reset_compare(tmp, tmp5), 0);
288 
289   // Reference check
290   art::ScratchFile tmp6;
291   EXPECT_TRUE(tmp6.GetFile()->WriteFully(&buffer[0], length));
292   EXPECT_EQ(reset_compare(tmp, tmp6), 0);
293 }
294 
TEST_F(FdFileTest,PipeFlush)295 TEST_F(FdFileTest, PipeFlush) {
296   int pipefd[2];
297   ASSERT_EQ(0, pipe2(pipefd, O_CLOEXEC));
298 
299   FdFile file(pipefd[1], true);
300   ASSERT_TRUE(file.WriteFully("foo", 3));
301   ASSERT_EQ(0, file.Flush());
302   ASSERT_EQ(0, file.FlushCloseOrErase());
303   close(pipefd[0]);
304 }
305 
306 }  // namespace unix_file
307