• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/mem_map.h"
20 #include "gtest/gtest.h"
21 #include "fd_file.h"
22 #include "random_access_file_test.h"
23 
24 #include "sys/mman.h"
25 
26 namespace unix_file {
27 
28 class FdFileTest : public RandomAccessFileTest {
29  protected:
MakeTestFile()30   RandomAccessFile* MakeTestFile() override {
31     FILE* tmp = tmpfile();
32     int fd = art::DupCloexec(fileno(tmp));
33     fclose(tmp);
34     return new FdFile(fd, false);
35   }
36   // Variables and functions related to sparse copy and move (rename) tests.
37   void TestDataMatches(const FdFile* src,
38                        const FdFile* dest,
39                        size_t input_offset,
40                        size_t output_offset,
41                        size_t copy_length);
42 #ifdef __linux__
43   static constexpr int kNumChunks = 8;
44   static constexpr size_t kChunkSize = 64 * art::KB;
45   static constexpr size_t kStatBlockSize = 512;
46   void CreateSparseSourceFile(size_t empty_prefix,
47                               size_t empty_suffix,
48                               /*out*/ std::unique_ptr<art::ScratchFile>& out_file);
49   size_t GetFilesystemBlockSize();
50 #endif
51 };
52 
TEST_F(FdFileTest,Read)53 TEST_F(FdFileTest, Read) {
54   TestRead();
55 }
56 
TEST_F(FdFileTest,SetLength)57 TEST_F(FdFileTest, SetLength) {
58   TestSetLength();
59 }
60 
TEST_F(FdFileTest,Write)61 TEST_F(FdFileTest, Write) {
62   TestWrite();
63 }
64 
TEST_F(FdFileTest,UnopenedFile)65 TEST_F(FdFileTest, UnopenedFile) {
66   FdFile file;
67   EXPECT_EQ(FdFile::kInvalidFd, file.Fd());
68   EXPECT_FALSE(file.IsOpened());
69   EXPECT_TRUE(file.GetPath().empty());
70 }
71 
TEST_F(FdFileTest,IsOpenFd)72 TEST_F(FdFileTest, IsOpenFd) {
73   art::ScratchFile scratch_file;
74   FdFile* file = scratch_file.GetFile();
75   ASSERT_TRUE(file->IsOpened());
76   EXPECT_GE(file->Fd(), 0);
77   EXPECT_NE(file->Fd(), FdFile::kInvalidFd);
78   EXPECT_TRUE(FdFile::IsOpenFd(file->Fd()));
79   int old_fd = file->Fd();
80   ASSERT_TRUE(file != nullptr);
81   ASSERT_EQ(file->FlushClose(), 0);
82   EXPECT_FALSE(file->IsOpened());
83   EXPECT_FALSE(FdFile::IsOpenFd(old_fd));
84 }
85 
TEST_F(FdFileTest,OpenClose)86 TEST_F(FdFileTest, OpenClose) {
87   std::string good_path(GetTmpPath("some-file.txt"));
88   FdFile file(good_path, O_CREAT | O_WRONLY, true);
89   ASSERT_TRUE(file.IsOpened());
90   EXPECT_GE(file.Fd(), 0);
91   EXPECT_TRUE(file.IsOpened());
92   EXPECT_FALSE(file.ReadOnlyMode());
93   EXPECT_EQ(0, file.Flush());
94   EXPECT_EQ(0, file.Close());
95   EXPECT_EQ(FdFile::kInvalidFd, file.Fd());
96   EXPECT_FALSE(file.IsOpened());
97   FdFile file2(good_path, O_RDONLY, true);
98   EXPECT_TRUE(file2.IsOpened());
99   EXPECT_TRUE(file2.ReadOnlyMode());
100   EXPECT_GE(file2.Fd(), 0);
101 
102   ASSERT_EQ(file2.Close(), 0);
103   ASSERT_EQ(unlink(good_path.c_str()), 0);
104 }
105 
TEST_F(FdFileTest,ReadFullyEmptyFile)106 TEST_F(FdFileTest, ReadFullyEmptyFile) {
107   // New scratch file, zero-length.
108   art::ScratchFile tmp;
109   FdFile file(tmp.GetFilename(), O_RDONLY, false);
110   ASSERT_TRUE(file.IsOpened());
111   EXPECT_TRUE(file.ReadOnlyMode());
112   EXPECT_GE(file.Fd(), 0);
113   uint8_t buffer[16];
114   EXPECT_FALSE(file.ReadFully(&buffer, 4));
115 }
116 
117 template <size_t Size>
NullTerminateCharArray(char (& array)[Size])118 static void NullTerminateCharArray(char (&array)[Size]) {
119   array[Size - 1] = '\0';
120 }
121 
TEST_F(FdFileTest,ReadFullyWithOffset)122 TEST_F(FdFileTest, ReadFullyWithOffset) {
123   // New scratch file, zero-length.
124   art::ScratchFile tmp;
125   FdFile file(tmp.GetFilename(), O_RDWR, false);
126   ASSERT_TRUE(file.IsOpened());
127   EXPECT_GE(file.Fd(), 0);
128   EXPECT_FALSE(file.ReadOnlyMode());
129 
130   char ignore_prefix[20] = {'a', };
131   NullTerminateCharArray(ignore_prefix);
132   char read_suffix[10] = {'b', };
133   NullTerminateCharArray(read_suffix);
134 
135   off_t offset = 0;
136   // Write scratch data to file that we can read back into.
137   EXPECT_TRUE(file.Write(ignore_prefix, sizeof(ignore_prefix), offset));
138   offset += sizeof(ignore_prefix);
139   EXPECT_TRUE(file.Write(read_suffix, sizeof(read_suffix), offset));
140 
141   ASSERT_EQ(file.Flush(), 0);
142 
143   // Reading at an offset should only produce 'bbbb...', since we ignore the 'aaa...' prefix.
144   char buffer[sizeof(read_suffix)];
145   EXPECT_TRUE(file.PreadFully(buffer, sizeof(read_suffix), offset));
146   EXPECT_STREQ(&read_suffix[0], &buffer[0]);
147 
148   ASSERT_EQ(file.Close(), 0);
149 }
150 
TEST_F(FdFileTest,ReadWriteFullyWithOffset)151 TEST_F(FdFileTest, ReadWriteFullyWithOffset) {
152   // New scratch file, zero-length.
153   art::ScratchFile tmp;
154   FdFile file(tmp.GetFilename(), O_RDWR, false);
155   ASSERT_GE(file.Fd(), 0);
156   EXPECT_TRUE(file.IsOpened());
157   EXPECT_FALSE(file.ReadOnlyMode());
158 
159   const char* test_string = "This is a test string";
160   size_t length = strlen(test_string) + 1;
161   const size_t offset = 12;
162   std::unique_ptr<char[]> offset_read_string(new char[length]);
163   std::unique_ptr<char[]> read_string(new char[length]);
164 
165   // Write scratch data to file that we can read back into.
166   EXPECT_TRUE(file.PwriteFully(test_string, length, offset));
167   ASSERT_EQ(file.Flush(), 0);
168 
169   // Test reading both the offsets.
170   EXPECT_TRUE(file.PreadFully(&offset_read_string[0], length, offset));
171   EXPECT_STREQ(test_string, &offset_read_string[0]);
172 
173   EXPECT_TRUE(file.PreadFully(&read_string[0], length, 0u));
174   EXPECT_NE(memcmp(&read_string[0], test_string, length), 0);
175 
176   ASSERT_EQ(file.Close(), 0);
177 }
178 
179 // Create a sparse file and return a pointer to it via the 'out_file' argument, necessary because
180 // gtest assertions require the function to return void.
CreateSparseSourceFile(size_t empty_prefix,size_t empty_suffix,std::unique_ptr<art::ScratchFile> & out_file)181 void FdFileTest::CreateSparseSourceFile(size_t empty_prefix,
182                                         size_t empty_suffix,
183                                         /*out*/ std::unique_ptr<art::ScratchFile>& out_file) {
184 /*
185    * Layout of the source file:
186    *   [ optional <empty_prefix> empty region ]
187    *   [ <kChunkSize> data chunk              ]  -\
188    *   [ <kChunkSize> empty chunk             ]   |
189    *   [ <kChunkSize> data chunk              ]   |
190    *   [ <kChunkSize> empty chunk             ]    > (2 * kNumChunks - 1) kChunkSize chunks
191    *   [ <kChunkSize> data chunk              ]   |
192    *   [   ...                                ]   |
193    *   [ <kChunkSize> data chunk              ]  -/
194    *   [ optional <empty_suffix> empty region ]
195    */
196   out_file = std::make_unique<art::ScratchFile>();
197   FdFile* src = out_file->GetFile();
198   ASSERT_TRUE(src->IsOpened());
199 
200   ASSERT_EQ(lseek(src->Fd(), empty_prefix, SEEK_CUR), empty_prefix);
201 
202   std::vector<int8_t> data_buffer(/*n=*/kChunkSize, /*val=*/1);
203 
204   ASSERT_TRUE(src->WriteFully(data_buffer.data(), kChunkSize));
205   for (size_t i = 0; i < kNumChunks - 1; i++) {
206     // Leave a chunk size of unwritten space between each data chunk.
207     ASSERT_GT(lseek(src->Fd(), kChunkSize, SEEK_CUR), 0);
208     ASSERT_TRUE(src->WriteFully(data_buffer.data(), kChunkSize));
209   }
210   ASSERT_EQ(src->SetLength(src->GetLength() + empty_suffix), 0);
211   ASSERT_EQ(src->Flush(), 0);
212 
213   size_t expected_length = (2 * kNumChunks - 1) * kChunkSize + empty_prefix + empty_suffix;
214   ASSERT_EQ(src->GetLength(), expected_length);
215 }
216 
TEST_F(FdFileTest,Rename)217 TEST_F(FdFileTest, Rename) {
218   // To test that rename preserves sparsity (on systems that support file sparsity), create a sparse
219   // source file.
220   std::unique_ptr<art::ScratchFile> src;
221   ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(/*empty_prefix=*/0, /*empty_suffix=*/0, src));
222 
223   size_t src_offset = lseek(src->GetFd(), /*offset=*/0, SEEK_CUR);
224   size_t source_length = src->GetFile()->GetLength();
225   struct stat src_stat;
226   ASSERT_EQ(fstat(src->GetFd(), &src_stat), 0);
227 
228   // Move the file via a rename.
229   art::ScratchFile dest;
230   const std::string& new_filename = dest.GetFilename();
231   const std::string& old_filename = src->GetFilename();
232   ASSERT_TRUE(src->GetFile()->Rename(new_filename));
233 
234   // Confirm the FdFile path has correctly updated.
235   EXPECT_EQ(src->GetFile()->GetPath(), new_filename);
236   // Check the offset of the moved file has not been modified.
237   EXPECT_EQ(lseek(src->GetFd(), /*offset=*/0, SEEK_CUR), src_offset);
238 
239   // Test that the file no longer exists in the old location, and there is a file at the new
240   // location with the expected length.
241   EXPECT_FALSE(art::OS::FileExists(old_filename.c_str()));
242   FdFile dest_file(new_filename, O_RDONLY, /*check_usage=*/false);
243   ASSERT_TRUE(dest_file.IsOpened());
244   EXPECT_EQ(dest_file.GetLength(), source_length);
245 
246   // Confirm the file at the new location has the same number of allocated data blocks as the source
247   // file. If the source file was a sparse file, this confirms that the sparsity was preserved
248   // by the move.
249   struct stat dest_stat;
250   ASSERT_EQ(fstat(dest_file.Fd(), &dest_stat), 0);
251   EXPECT_EQ(dest_stat.st_blocks, src_stat.st_blocks);
252 
253   // And it is exactly the same file in the new location, with the same contents.
254   EXPECT_EQ(dest_stat.st_dev, src_stat.st_dev);
255   EXPECT_EQ(dest_stat.st_ino, src_stat.st_ino);
256 
257   ASSERT_NO_FATAL_FAILURE(TestDataMatches(src->GetFile(),
258                                           &dest_file,
259                                           /*input_offset=*/0u,
260                                           /*output_offset=*/0u,
261                                           source_length));
262   src->Close();
263 }
264 
TEST_F(FdFileTest,Copy)265 TEST_F(FdFileTest, Copy) {
266   art::ScratchFile src_tmp;
267   FdFile src(src_tmp.GetFilename(), O_RDWR, false);
268   ASSERT_GE(src.Fd(), 0);
269   ASSERT_TRUE(src.IsOpened());
270 
271   char src_data[] = "Some test data.";
272   ASSERT_TRUE(src.WriteFully(src_data, sizeof(src_data)));  // Including the zero terminator.
273   ASSERT_EQ(0, src.Flush());
274   ASSERT_EQ(static_cast<int64_t>(sizeof(src_data)), src.GetLength());
275 
276   art::ScratchFile dest_tmp;
277   FdFile dest(dest_tmp.GetFilename(), O_RDWR, false);
278   ASSERT_GE(dest.Fd(), 0);
279   ASSERT_TRUE(dest.IsOpened());
280 
281   ASSERT_TRUE(dest.Copy(&src, 0, sizeof(src_data)));
282   ASSERT_EQ(0, dest.Flush());
283   ASSERT_EQ(static_cast<int64_t>(sizeof(src_data)), dest.GetLength());
284 
285   char check_data[sizeof(src_data)];
286   ASSERT_TRUE(dest.PreadFully(check_data, sizeof(src_data), 0u));
287   CHECK_EQ(0, memcmp(check_data, src_data, sizeof(src_data)));
288 
289   ASSERT_EQ(0, dest.Close());
290   ASSERT_EQ(0, src.Close());
291 }
292 
293 // Helper to assert correctness of the copied data.
TestDataMatches(const FdFile * src,const FdFile * dest,size_t input_offset,size_t output_offset,size_t copy_length)294 void FdFileTest::TestDataMatches(const FdFile* src,
295                                  const FdFile* dest,
296                                  size_t input_offset,
297                                  size_t output_offset,
298                                  size_t copy_length) {
299   art::MemMap::Init();
300   std::string error_msg;
301   art::MemMap src_mmap = art::MemMap::MapFile(copy_length + input_offset,
302                                               PROT_READ,
303                                               MAP_PRIVATE,
304                                               src->Fd(),
305                                               /*start=*/0,
306                                               /*low_4gb=*/false,
307                                               src->GetPath().c_str(),
308                                               &error_msg);
309   ASSERT_TRUE(src_mmap.IsValid()) << error_msg;
310   art::MemMap dest_mmap = art::MemMap::MapFile(copy_length + output_offset,
311                                                PROT_READ,
312                                                MAP_PRIVATE,
313                                                dest->Fd(),
314                                                /*start=*/0,
315                                                /*low_4gb=*/false,
316                                                dest->GetPath().c_str(),
317                                                &error_msg);
318   ASSERT_TRUE(dest_mmap.IsValid()) << error_msg;
319 
320   EXPECT_EQ(0, memcmp(src_mmap.Begin() + input_offset,
321                       dest_mmap.Begin() + output_offset,
322                       copy_length));
323 }
324 
325 #ifdef __linux__
326 // Test that the file created by FdFileTest::CreateSparseSourceFile is sparse on the test
327 // environment.
TEST_F(FdFileTest,CopySparseCreateSparseFile)328 TEST_F(FdFileTest, CopySparseCreateSparseFile) {
329   // Create file with no empty prefix or suffix.
330   std::unique_ptr<art::ScratchFile> src1;
331   ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(/*empty_prefix=*/0, /*empty_suffix=*/0, src1));
332 
333   struct stat src1_stat;
334   ASSERT_EQ(fstat(src1->GetFd(), &src1_stat), 0);
335 
336   // It has at least as many allocated blocks required to represent the data chunks.
337   EXPECT_GE(src1_stat.st_blocks * kStatBlockSize, kNumChunks * kChunkSize);
338   // It is sparse: it has fewer allocated blocks than would be required if the whole file was data.
339   EXPECT_LT(src1_stat.st_blocks * kStatBlockSize, src1_stat.st_size);
340 
341   // Create file with an empty prefix and empty suffix.
342   std::unique_ptr<art::ScratchFile> src2;
343   ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(kChunkSize, kChunkSize, src2));
344 
345   // File should have the same number of allocated blocks.
346   struct stat src2_stat;
347   ASSERT_EQ(fstat(src2->GetFd(), &src2_stat), 0);
348   EXPECT_EQ(src2_stat.st_blocks, src1_stat.st_blocks);
349 }
350 
351 // Test complete copies of the source file produced by FdFileTest::CreateSparseSourceFile.
TEST_F(FdFileTest,CopySparseFullCopy)352 TEST_F(FdFileTest, CopySparseFullCopy) {
353   auto verify_fullcopy = [&](size_t empty_prefix, size_t empty_suffix) {
354     SCOPED_TRACE(testing::Message() << "prefix:" << empty_prefix << ", suffix:" << empty_suffix);
355 
356     std::unique_ptr<art::ScratchFile> src;
357     ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(empty_prefix, empty_suffix, src));
358 
359     art::ScratchFile dest;
360     ASSERT_TRUE(dest.GetFile()->IsOpened());
361 
362     off_t copy_size = src->GetFile()->GetLength();
363     EXPECT_TRUE(dest.GetFile()->Copy(src->GetFile(), /*offset=*/0, copy_size));
364     EXPECT_EQ(dest.GetFile()->Flush(), 0);
365 
366     // Test destination length.
367     EXPECT_EQ(dest.GetFile()->GetLength(), copy_size);
368 
369     // Test FD offsets.
370     EXPECT_EQ(lseek(dest.GetFd(), /*offset=*/0, SEEK_CUR), dest.GetFile()->GetLength());
371     EXPECT_EQ(lseek(src->GetFd(), /*offset=*/0, SEEK_CUR), src->GetFile()->GetLength());
372 
373     // Test output sparsity matches the input sparsity.
374     struct stat src_stat, dest_stat;
375     ASSERT_EQ(fstat(src->GetFd(), &src_stat), 0);
376     ASSERT_EQ(fstat(dest.GetFd(), &dest_stat), 0);
377     EXPECT_EQ(dest_stat.st_blocks, src_stat.st_blocks);
378 
379     // Test the resulting data in the destination is correct.
380     ASSERT_NO_FATAL_FAILURE(TestDataMatches(src->GetFile(),
381                                             dest.GetFile(),
382                                             /*input_offset=*/0u,
383                                             /*output_offset=*/0u,
384                                             copy_size));
385   };
386 
387   // Test full copies using different offsets and outer skip regions of sizes [0, 128, 2048, 32768].
388   ASSERT_NO_FATAL_FAILURE(verify_fullcopy(0, 0));
389   for (size_t empty_region_size = 128;
390        empty_region_size <= kChunkSize / 2;
391        empty_region_size <<= 4) {
392     // Empty prefix.
393     ASSERT_NO_FATAL_FAILURE(verify_fullcopy(/*empty_prefix=*/empty_region_size,
394                                             /*empty_suffix=*/0u));
395     // Empty suffix.
396     ASSERT_NO_FATAL_FAILURE(verify_fullcopy(/*empty_prefix=*/0u,
397                                             /*empty_suffix=*/empty_region_size));
398     // Both.
399     ASSERT_NO_FATAL_FAILURE(verify_fullcopy(/*empty_prefix=*/empty_region_size,
400                                             /*empty_suffix=*/empty_region_size));
401   }
402 }
403 
404 // Find the filesystem blocksize of the test environment by creating and calling fstat on a
405 // temporary file.
GetFilesystemBlockSize()406 size_t FdFileTest::GetFilesystemBlockSize() {
407   art::ScratchFile tmpfile;
408   if (!tmpfile.GetFile()->IsOpened()) {
409     return 0;
410   }
411   struct stat tmp_stat;
412   if (fstat(tmpfile.GetFd(), &tmp_stat) != 0) {
413     return 0;
414   }
415   return tmp_stat.st_blksize;
416 }
417 
418 // Test partial copies of the source file produced by FdFileTest::CreateSparseSourceFile.
TEST_F(FdFileTest,CopySparsePartialCopy)419 TEST_F(FdFileTest, CopySparsePartialCopy) {
420   size_t blocksize = GetFilesystemBlockSize();
421   ASSERT_GT(blocksize, 0u);
422 
423   auto verify_partialcopy = [&](size_t empty_prefix,
424                                 size_t empty_suffix,
425                                 size_t copy_start_offset,
426                                 size_t copy_end_offset) {
427     // The copy starts <copy_start_offset> from the start of the source file.
428     // The copy ends <copy_end_offset> from the end of the source file.
429     SCOPED_TRACE(testing::Message() << "prefix:" << empty_prefix << ", suffix:" << empty_suffix
430                  << ", copy_start_offset:" << copy_start_offset << ", copy_end_offset:"
431                  << copy_end_offset);
432 
433     std::unique_ptr<art::ScratchFile> src;
434     ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(empty_prefix, empty_suffix, src));
435 
436     art::ScratchFile dest;
437     ASSERT_TRUE(dest.GetFile()->IsOpened());
438 
439     off_t copy_size = src->GetFile()->GetLength() - copy_start_offset - copy_end_offset;
440     EXPECT_TRUE(dest.GetFile()->Copy(src->GetFile(), copy_start_offset, copy_size));
441     EXPECT_EQ(dest.GetFile()->Flush(), 0);
442 
443     // Test destination length.
444     EXPECT_EQ(dest.GetFile()->GetLength(), copy_size);
445 
446     // Test FD offsets.
447     EXPECT_EQ(lseek(dest.GetFd(), /*offset=*/0, SEEK_CUR), copy_size);
448     EXPECT_EQ(lseek(src->GetFd(), /*offset=*/0, SEEK_CUR), copy_start_offset + copy_size);
449 
450     // Test output sparsity matches the input sparsity, accounting for any discarded blocks.
451     // For simplicity, only reason about the sparsity when there is no empty prefix/suffix, and we
452     // are discarding no more than the first and/or last chunk of data.
453     if (empty_prefix == 0 && empty_suffix == 0 && copy_start_offset <= kChunkSize
454         && copy_end_offset <= kChunkSize) {
455       // Round down to whole filesystem blocks, then convert to fstat blocksize.
456       size_t discarded_blocks = (copy_start_offset / blocksize) + (copy_end_offset / blocksize);
457       discarded_blocks *= (blocksize / kStatBlockSize);
458 
459       struct stat src_stat, dest_stat;
460       ASSERT_EQ(fstat(src->GetFd(), &src_stat), 0);
461       ASSERT_EQ(fstat(dest.GetFd(), &dest_stat), 0);
462 
463       if (art::IsAlignedParam(copy_start_offset, blocksize)) {
464         // We expect the sparsity to be preserved.
465         EXPECT_EQ(dest_stat.st_blocks, src_stat.st_blocks - discarded_blocks);
466       } else {
467         // As all data chunks are aligned, an non-aligned copy can only decrease the sparsity.
468         EXPECT_GT(dest_stat.st_blocks, src_stat.st_blocks - discarded_blocks);
469       }
470     }
471 
472     // Test the resulting data in the destination is correct.
473     ASSERT_NO_FATAL_FAILURE(TestDataMatches(src->GetFile(),
474                                             dest.GetFile(),
475                                             /*input_offset=*/copy_start_offset,
476                                             /*output_offset=*/0u,
477                                             copy_size));
478   };
479 
480   // Test partial copies with outer skip regions.
481   std::vector<size_t> outer_regions = {0, 128, 2 * art::KB, 32 * art::KB};
482   for (const auto& empty : outer_regions) {
483     for (size_t discard = 0; discard <= 8 * art::KB; discard += 1 * art::KB) {
484       // Start copy <discard> bytes after the file start.
485       ASSERT_NO_FATAL_FAILURE(verify_partialcopy(/*empty_prefix=*/empty,
486                                                  /*empty_suffix=*/empty,
487                                                  /*copy_start_offset=*/discard,
488                                                  /*copy_end_offset=*/0u));
489       // End copy <discard> bytes before the file end.
490       ASSERT_NO_FATAL_FAILURE(verify_partialcopy(/*empty_prefix=*/empty,
491                                                  /*empty_suffix=*/empty,
492                                                  /*copy_start_offset=*/0u,
493                                                  /*copy_end_offset=*/discard));
494       // Both.
495       ASSERT_NO_FATAL_FAILURE(verify_partialcopy(/*empty_prefix=*/empty,
496                                                  /*empty_suffix=*/empty,
497                                                  /*copy_start_offset=*/discard,
498                                                  /*copy_end_offset=*/discard));
499     }
500   }
501 }
502 
503 // Test the case where the destination file's FD offset is non-zero before the copy.
TEST_F(FdFileTest,CopySparseToNonZeroOffset)504 TEST_F(FdFileTest, CopySparseToNonZeroOffset) {
505   std::unique_ptr<art::ScratchFile> src;
506   ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(/*empty_prefix=*/0u, /*empty_suffix=*/0u, src));
507 
508   art::ScratchFile dest;
509   FdFile* dest_file = dest.GetFile();
510   ASSERT_TRUE(dest_file->IsOpened());
511 
512   // Move the destination file offset forward before starting the copy.
513   constexpr size_t existing_length = kChunkSize;
514   EXPECT_EQ(lseek(dest.GetFd(), existing_length, SEEK_SET), existing_length);
515   ASSERT_EQ(dest_file->SetLength(existing_length), 0);
516 
517   off_t copy_size = src->GetFile()->GetLength();
518   EXPECT_TRUE(dest_file->Copy(src->GetFile(), /*offset=*/0, copy_size));
519   EXPECT_EQ(dest_file->Flush(), 0);
520 
521   // Test destination length.
522   EXPECT_EQ(dest_file->GetLength(), existing_length + copy_size);
523 
524   // Test FD offsets.
525   EXPECT_EQ(lseek(dest.GetFd(), /*offset=*/0, SEEK_CUR), dest_file->GetLength());
526   EXPECT_EQ(lseek(src->GetFd(), /*offset=*/0, SEEK_CUR), src->GetFile()->GetLength());
527 
528   // Test the copied data appended to the destination is correct.
529   ASSERT_NO_FATAL_FAILURE(TestDataMatches(src->GetFile(),
530                                           dest_file,
531                                           /*input_offset=*/0u,
532                                           /*output_offset=*/existing_length,
533                                           copy_size));
534 }
535 #endif
536 
TEST_F(FdFileTest,MoveConstructor)537 TEST_F(FdFileTest, MoveConstructor) {
538   // New scratch file, zero-length.
539   art::ScratchFile tmp;
540   FdFile file(tmp.GetFilename(), O_RDWR, false);
541   ASSERT_TRUE(file.IsOpened());
542   EXPECT_GE(file.Fd(), 0);
543 
544   int old_fd = file.Fd();
545 
546   FdFile file2(std::move(file));
547   EXPECT_FALSE(file.IsOpened());  // NOLINT - checking file is no longer opened after move
548   EXPECT_TRUE(file2.IsOpened());
549   EXPECT_EQ(old_fd, file2.Fd());
550 
551   ASSERT_EQ(file2.Flush(), 0);
552   ASSERT_EQ(file2.Close(), 0);
553 }
554 
TEST_F(FdFileTest,OperatorMoveEquals)555 TEST_F(FdFileTest, OperatorMoveEquals) {
556   // Make sure the read_only_ flag is correctly copied
557   // over.
558   art::ScratchFile tmp;
559   FdFile file(tmp.GetFilename(), O_RDONLY, false);
560   ASSERT_TRUE(file.ReadOnlyMode());
561 
562   FdFile file2(tmp.GetFilename(), O_RDWR, false);
563   ASSERT_FALSE(file2.ReadOnlyMode());
564 
565   file2 = std::move(file);
566   ASSERT_TRUE(file2.ReadOnlyMode());
567 }
568 
TEST_F(FdFileTest,EraseWithPathUnlinks)569 TEST_F(FdFileTest, EraseWithPathUnlinks) {
570   // New scratch file, zero-length.
571   art::ScratchFile tmp;
572   std::string filename = tmp.GetFilename();
573   tmp.Close();  // This is required because of the unlink race between the scratch file and the
574                 // FdFile, which leads to close-guard breakage.
575   FdFile file(filename, O_RDWR, false);
576   ASSERT_TRUE(file.IsOpened());
577   EXPECT_GE(file.Fd(), 0);
578   uint8_t buffer[16] = { 0 };
579   EXPECT_TRUE(file.WriteFully(&buffer, sizeof(buffer)));
580   EXPECT_EQ(file.Flush(), 0);
581 
582   EXPECT_TRUE(file.Erase(true));
583 
584   EXPECT_FALSE(file.IsOpened());
585 
586   EXPECT_FALSE(art::OS::FileExists(filename.c_str())) << filename;
587 }
588 
TEST_F(FdFileTest,Compare)589 TEST_F(FdFileTest, Compare) {
590   std::vector<uint8_t> buffer;
591   constexpr int64_t length = 17 * art::KB;
592   for (size_t i = 0; i < length; ++i) {
593     buffer.push_back(static_cast<uint8_t>(i));
594   }
595 
596   auto reset_compare = [&](art::ScratchFile& a, art::ScratchFile& b) {
597     a.GetFile()->ResetOffset();
598     b.GetFile()->ResetOffset();
599     return a.GetFile()->Compare(b.GetFile());
600   };
601 
602   art::ScratchFile tmp;
603   EXPECT_TRUE(tmp.GetFile()->WriteFully(&buffer[0], length));
604   EXPECT_EQ(tmp.GetFile()->GetLength(), length);
605 
606   art::ScratchFile tmp2;
607   EXPECT_TRUE(tmp2.GetFile()->WriteFully(&buffer[0], length));
608   EXPECT_EQ(tmp2.GetFile()->GetLength(), length);
609 
610   // Basic equality check.
611   tmp.GetFile()->ResetOffset();
612   tmp2.GetFile()->ResetOffset();
613   // Files should be the same.
614   EXPECT_EQ(reset_compare(tmp, tmp2), 0);
615 
616   // Change a byte near the start.
617   ++buffer[2];
618   art::ScratchFile tmp3;
619   EXPECT_TRUE(tmp3.GetFile()->WriteFully(&buffer[0], length));
620   --buffer[2];
621   EXPECT_NE(reset_compare(tmp, tmp3), 0);
622 
623   // Change a byte near the middle.
624   ++buffer[length / 2];
625   art::ScratchFile tmp4;
626   EXPECT_TRUE(tmp4.GetFile()->WriteFully(&buffer[0], length));
627   --buffer[length / 2];
628   EXPECT_NE(reset_compare(tmp, tmp4), 0);
629 
630   // Change a byte near the end.
631   ++buffer[length - 5];
632   art::ScratchFile tmp5;
633   EXPECT_TRUE(tmp5.GetFile()->WriteFully(&buffer[0], length));
634   --buffer[length - 5];
635   EXPECT_NE(reset_compare(tmp, tmp5), 0);
636 
637   // Reference check
638   art::ScratchFile tmp6;
639   EXPECT_TRUE(tmp6.GetFile()->WriteFully(&buffer[0], length));
640   EXPECT_EQ(reset_compare(tmp, tmp6), 0);
641 }
642 
TEST_F(FdFileTest,PipeFlush)643 TEST_F(FdFileTest, PipeFlush) {
644   int pipefd[2];
645   ASSERT_EQ(0, pipe2(pipefd, O_CLOEXEC));
646 
647   FdFile file(pipefd[1], true);
648   ASSERT_TRUE(file.WriteFully("foo", 3));
649   ASSERT_EQ(0, file.Flush());
650   ASSERT_EQ(0, file.FlushCloseOrErase());
651   close(pipefd[0]);
652 }
653 
654 }  // namespace unix_file
655