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 // Disable on host as sparsity is filesystem dependent and some hosts may break test assumptions.
330 TEST_DISABLED_FOR_HOST();
331
332 // Create file with no empty prefix or suffix.
333 std::unique_ptr<art::ScratchFile> src1;
334 ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(/*empty_prefix=*/0, /*empty_suffix=*/0, src1));
335
336 struct stat src1_stat;
337 ASSERT_EQ(fstat(src1->GetFd(), &src1_stat), 0);
338
339 // It has at least as many allocated blocks required to represent the data chunks.
340 EXPECT_GE(src1_stat.st_blocks * kStatBlockSize, kNumChunks * kChunkSize);
341 // It is sparse: it has fewer allocated blocks than would be required if the whole file was data.
342 EXPECT_LT(src1_stat.st_blocks * kStatBlockSize, src1_stat.st_size);
343
344 // Create file with an empty prefix and empty suffix.
345 std::unique_ptr<art::ScratchFile> src2;
346 ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(kChunkSize, kChunkSize, src2));
347
348 // File should have the same number of allocated blocks.
349 struct stat src2_stat;
350 ASSERT_EQ(fstat(src2->GetFd(), &src2_stat), 0);
351 EXPECT_EQ(src2_stat.st_blocks, src1_stat.st_blocks);
352 }
353
354 // Test complete copies of the source file produced by FdFileTest::CreateSparseSourceFile.
TEST_F(FdFileTest,CopySparseFullCopy)355 TEST_F(FdFileTest, CopySparseFullCopy) {
356 // Disable on host as sparsity is filesystem dependent and some hosts may break test assumptions.
357 TEST_DISABLED_FOR_HOST();
358
359 auto verify_fullcopy = [&](size_t empty_prefix, size_t empty_suffix) {
360 SCOPED_TRACE(::testing::Message() << "prefix:" << empty_prefix << ", suffix:" << empty_suffix);
361
362 std::unique_ptr<art::ScratchFile> src;
363 ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(empty_prefix, empty_suffix, src));
364
365 art::ScratchFile dest;
366 ASSERT_TRUE(dest.GetFile()->IsOpened());
367
368 off_t copy_size = src->GetFile()->GetLength();
369 EXPECT_TRUE(dest.GetFile()->Copy(src->GetFile(), /*offset=*/0, copy_size));
370 EXPECT_EQ(dest.GetFile()->Flush(), 0);
371
372 // Test destination length.
373 EXPECT_EQ(dest.GetFile()->GetLength(), copy_size);
374
375 // Test FD offsets.
376 EXPECT_EQ(lseek(dest.GetFd(), /*offset=*/0, SEEK_CUR), dest.GetFile()->GetLength());
377 EXPECT_EQ(lseek(src->GetFd(), /*offset=*/0, SEEK_CUR), src->GetFile()->GetLength());
378
379 // Test output sparsity matches the input sparsity.
380 struct stat src_stat, dest_stat;
381 ASSERT_EQ(fstat(src->GetFd(), &src_stat), 0);
382 ASSERT_EQ(fstat(dest.GetFd(), &dest_stat), 0);
383 EXPECT_EQ(dest_stat.st_blocks, src_stat.st_blocks);
384
385 // Test the resulting data in the destination is correct.
386 ASSERT_NO_FATAL_FAILURE(TestDataMatches(src->GetFile(),
387 dest.GetFile(),
388 /*input_offset=*/0u,
389 /*output_offset=*/0u,
390 copy_size));
391 };
392
393 // Test full copies using different offsets and outer skip regions of sizes [0, 128, 2048, 32768].
394 ASSERT_NO_FATAL_FAILURE(verify_fullcopy(0, 0));
395 for (size_t empty_region_size = 128;
396 empty_region_size <= kChunkSize / 2;
397 empty_region_size <<= 4) {
398 // Empty prefix.
399 ASSERT_NO_FATAL_FAILURE(verify_fullcopy(/*empty_prefix=*/empty_region_size,
400 /*empty_suffix=*/0u));
401 // Empty suffix.
402 ASSERT_NO_FATAL_FAILURE(verify_fullcopy(/*empty_prefix=*/0u,
403 /*empty_suffix=*/empty_region_size));
404 // Both.
405 ASSERT_NO_FATAL_FAILURE(verify_fullcopy(/*empty_prefix=*/empty_region_size,
406 /*empty_suffix=*/empty_region_size));
407 }
408 }
409
410 // Find the filesystem blocksize of the test environment by creating and calling fstat on a
411 // temporary file.
GetFilesystemBlockSize()412 size_t FdFileTest::GetFilesystemBlockSize() {
413 art::ScratchFile tmpfile;
414 if (!tmpfile.GetFile()->IsOpened()) {
415 return 0;
416 }
417 struct stat tmp_stat;
418 if (fstat(tmpfile.GetFd(), &tmp_stat) != 0) {
419 return 0;
420 }
421 return tmp_stat.st_blksize;
422 }
423
424 // Test partial copies of the source file produced by FdFileTest::CreateSparseSourceFile.
TEST_F(FdFileTest,CopySparsePartialCopy)425 TEST_F(FdFileTest, CopySparsePartialCopy) {
426 // Disable on host as sparsity is filesystem dependent and some hosts may break test assumptions.
427 TEST_DISABLED_FOR_HOST();
428
429 size_t blocksize = GetFilesystemBlockSize();
430 ASSERT_GT(blocksize, 0u);
431
432 auto verify_partialcopy = [&](size_t empty_prefix,
433 size_t empty_suffix,
434 size_t copy_start_offset,
435 size_t copy_end_offset) {
436 // The copy starts <copy_start_offset> from the start of the source file.
437 // The copy ends <copy_end_offset> from the end of the source file.
438 SCOPED_TRACE(::testing::Message() << "prefix:" << empty_prefix << ", suffix:" << empty_suffix
439 << ", copy_start_offset:" << copy_start_offset
440 << ", copy_end_offset:" << copy_end_offset);
441
442 std::unique_ptr<art::ScratchFile> src;
443 ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(empty_prefix, empty_suffix, src));
444
445 art::ScratchFile dest;
446 ASSERT_TRUE(dest.GetFile()->IsOpened());
447
448 off_t copy_size = src->GetFile()->GetLength() - copy_start_offset - copy_end_offset;
449 EXPECT_TRUE(dest.GetFile()->Copy(src->GetFile(), copy_start_offset, copy_size));
450 EXPECT_EQ(dest.GetFile()->Flush(), 0);
451
452 // Test destination length.
453 EXPECT_EQ(dest.GetFile()->GetLength(), copy_size);
454
455 // Test FD offsets.
456 EXPECT_EQ(lseek(dest.GetFd(), /*offset=*/0, SEEK_CUR), copy_size);
457 EXPECT_EQ(lseek(src->GetFd(), /*offset=*/0, SEEK_CUR), copy_start_offset + copy_size);
458
459 // Test output sparsity matches the input sparsity, accounting for any discarded blocks.
460 // For simplicity, only reason about the sparsity when there is no empty prefix/suffix, and we
461 // are discarding no more than the first and/or last chunk of data.
462 if (empty_prefix == 0 && empty_suffix == 0 && copy_start_offset <= kChunkSize
463 && copy_end_offset <= kChunkSize) {
464 // Round down to whole filesystem blocks, then convert to fstat blocksize.
465 size_t discarded_blocks = (copy_start_offset / blocksize) + (copy_end_offset / blocksize);
466 discarded_blocks *= (blocksize / kStatBlockSize);
467
468 struct stat src_stat, dest_stat;
469 ASSERT_EQ(fstat(src->GetFd(), &src_stat), 0);
470 ASSERT_EQ(fstat(dest.GetFd(), &dest_stat), 0);
471
472 if (art::IsAlignedParam(copy_start_offset, blocksize)) {
473 // We expect the sparsity to be preserved.
474 EXPECT_EQ(dest_stat.st_blocks, src_stat.st_blocks - discarded_blocks);
475 } else {
476 // As all data chunks are aligned, an non-aligned copy can only decrease the sparsity.
477 EXPECT_GT(dest_stat.st_blocks, src_stat.st_blocks - discarded_blocks);
478 }
479 }
480
481 // Test the resulting data in the destination is correct.
482 ASSERT_NO_FATAL_FAILURE(TestDataMatches(src->GetFile(),
483 dest.GetFile(),
484 /*input_offset=*/copy_start_offset,
485 /*output_offset=*/0u,
486 copy_size));
487 };
488
489 // Test partial copies with outer skip regions.
490 std::vector<size_t> outer_regions = {0, 128, 2 * art::KB, 32 * art::KB};
491 for (const auto& empty : outer_regions) {
492 for (size_t discard = 0; discard <= 8 * art::KB; discard += 1 * art::KB) {
493 // Start copy <discard> bytes after the file start.
494 ASSERT_NO_FATAL_FAILURE(verify_partialcopy(/*empty_prefix=*/empty,
495 /*empty_suffix=*/empty,
496 /*copy_start_offset=*/discard,
497 /*copy_end_offset=*/0u));
498 // End copy <discard> bytes before the file end.
499 ASSERT_NO_FATAL_FAILURE(verify_partialcopy(/*empty_prefix=*/empty,
500 /*empty_suffix=*/empty,
501 /*copy_start_offset=*/0u,
502 /*copy_end_offset=*/discard));
503 // Both.
504 ASSERT_NO_FATAL_FAILURE(verify_partialcopy(/*empty_prefix=*/empty,
505 /*empty_suffix=*/empty,
506 /*copy_start_offset=*/discard,
507 /*copy_end_offset=*/discard));
508 }
509 }
510 }
511
512 // Test the case where the destination file's FD offset is non-zero before the copy.
TEST_F(FdFileTest,CopySparseToNonZeroOffset)513 TEST_F(FdFileTest, CopySparseToNonZeroOffset) {
514 // Disable on host as sparsity is filesystem dependent and some hosts may break test assumptions.
515 TEST_DISABLED_FOR_HOST();
516
517 std::unique_ptr<art::ScratchFile> src;
518 ASSERT_NO_FATAL_FAILURE(CreateSparseSourceFile(/*empty_prefix=*/0u, /*empty_suffix=*/0u, src));
519
520 art::ScratchFile dest;
521 FdFile* dest_file = dest.GetFile();
522 ASSERT_TRUE(dest_file->IsOpened());
523
524 // Move the destination file offset forward before starting the copy.
525 constexpr size_t existing_length = kChunkSize;
526 EXPECT_EQ(lseek(dest.GetFd(), existing_length, SEEK_SET), existing_length);
527 ASSERT_EQ(dest_file->SetLength(existing_length), 0);
528
529 off_t copy_size = src->GetFile()->GetLength();
530 EXPECT_TRUE(dest_file->Copy(src->GetFile(), /*offset=*/0, copy_size));
531 EXPECT_EQ(dest_file->Flush(), 0);
532
533 // Test destination length.
534 EXPECT_EQ(dest_file->GetLength(), existing_length + copy_size);
535
536 // Test FD offsets.
537 EXPECT_EQ(lseek(dest.GetFd(), /*offset=*/0, SEEK_CUR), dest_file->GetLength());
538 EXPECT_EQ(lseek(src->GetFd(), /*offset=*/0, SEEK_CUR), src->GetFile()->GetLength());
539
540 // Test the copied data appended to the destination is correct.
541 ASSERT_NO_FATAL_FAILURE(TestDataMatches(src->GetFile(),
542 dest_file,
543 /*input_offset=*/0u,
544 /*output_offset=*/existing_length,
545 copy_size));
546 }
547 #endif
548
TEST_F(FdFileTest,MoveConstructor)549 TEST_F(FdFileTest, MoveConstructor) {
550 // New scratch file, zero-length.
551 art::ScratchFile tmp;
552 FdFile file(tmp.GetFilename(), O_RDWR, false);
553 ASSERT_TRUE(file.IsOpened());
554 EXPECT_GE(file.Fd(), 0);
555
556 int old_fd = file.Fd();
557
558 FdFile file2(std::move(file));
559 EXPECT_FALSE(file.IsOpened()); // NOLINT - checking file is no longer opened after move
560 EXPECT_TRUE(file2.IsOpened());
561 EXPECT_EQ(old_fd, file2.Fd());
562
563 ASSERT_EQ(file2.Flush(), 0);
564 ASSERT_EQ(file2.Close(), 0);
565 }
566
TEST_F(FdFileTest,OperatorMoveEquals)567 TEST_F(FdFileTest, OperatorMoveEquals) {
568 // Make sure the read_only_ flag is correctly copied
569 // over.
570 art::ScratchFile tmp;
571 FdFile file(tmp.GetFilename(), O_RDONLY, false);
572 ASSERT_TRUE(file.ReadOnlyMode());
573
574 FdFile file2(tmp.GetFilename(), O_RDWR, false);
575 ASSERT_FALSE(file2.ReadOnlyMode());
576
577 file2 = std::move(file);
578 ASSERT_TRUE(file2.ReadOnlyMode());
579 }
580
TEST_F(FdFileTest,EraseWithPathUnlinks)581 TEST_F(FdFileTest, EraseWithPathUnlinks) {
582 // New scratch file, zero-length.
583 art::ScratchFile tmp;
584 std::string filename = tmp.GetFilename();
585 tmp.Close(); // This is required because of the unlink race between the scratch file and the
586 // FdFile, which leads to close-guard breakage.
587 FdFile file(filename, O_RDWR, false);
588 ASSERT_TRUE(file.IsOpened());
589 EXPECT_GE(file.Fd(), 0);
590 uint8_t buffer[16] = { 0 };
591 EXPECT_TRUE(file.WriteFully(&buffer, sizeof(buffer)));
592 EXPECT_EQ(file.Flush(), 0);
593
594 EXPECT_TRUE(file.Erase(true));
595
596 EXPECT_FALSE(file.IsOpened());
597
598 EXPECT_FALSE(art::OS::FileExists(filename.c_str())) << filename;
599 }
600
TEST_F(FdFileTest,Compare)601 TEST_F(FdFileTest, Compare) {
602 std::vector<uint8_t> buffer;
603 constexpr int64_t length = 17 * art::KB;
604 for (size_t i = 0; i < length; ++i) {
605 buffer.push_back(static_cast<uint8_t>(i));
606 }
607
608 auto reset_compare = [&](art::ScratchFile& a, art::ScratchFile& b) {
609 a.GetFile()->ResetOffset();
610 b.GetFile()->ResetOffset();
611 return a.GetFile()->Compare(b.GetFile());
612 };
613
614 art::ScratchFile tmp;
615 EXPECT_TRUE(tmp.GetFile()->WriteFully(&buffer[0], length));
616 EXPECT_EQ(tmp.GetFile()->GetLength(), length);
617
618 art::ScratchFile tmp2;
619 EXPECT_TRUE(tmp2.GetFile()->WriteFully(&buffer[0], length));
620 EXPECT_EQ(tmp2.GetFile()->GetLength(), length);
621
622 // Basic equality check.
623 tmp.GetFile()->ResetOffset();
624 tmp2.GetFile()->ResetOffset();
625 // Files should be the same.
626 EXPECT_EQ(reset_compare(tmp, tmp2), 0);
627
628 // Change a byte near the start.
629 ++buffer[2];
630 art::ScratchFile tmp3;
631 EXPECT_TRUE(tmp3.GetFile()->WriteFully(&buffer[0], length));
632 --buffer[2];
633 EXPECT_NE(reset_compare(tmp, tmp3), 0);
634
635 // Change a byte near the middle.
636 ++buffer[length / 2];
637 art::ScratchFile tmp4;
638 EXPECT_TRUE(tmp4.GetFile()->WriteFully(&buffer[0], length));
639 --buffer[length / 2];
640 EXPECT_NE(reset_compare(tmp, tmp4), 0);
641
642 // Change a byte near the end.
643 ++buffer[length - 5];
644 art::ScratchFile tmp5;
645 EXPECT_TRUE(tmp5.GetFile()->WriteFully(&buffer[0], length));
646 --buffer[length - 5];
647 EXPECT_NE(reset_compare(tmp, tmp5), 0);
648
649 // Reference check
650 art::ScratchFile tmp6;
651 EXPECT_TRUE(tmp6.GetFile()->WriteFully(&buffer[0], length));
652 EXPECT_EQ(reset_compare(tmp, tmp6), 0);
653 }
654
TEST_F(FdFileTest,PipeFlush)655 TEST_F(FdFileTest, PipeFlush) {
656 int pipefd[2];
657 ASSERT_EQ(0, pipe2(pipefd, O_CLOEXEC));
658
659 FdFile file(pipefd[1], true);
660 ASSERT_TRUE(file.WriteFully("foo", 3));
661 ASSERT_EQ(0, file.Flush());
662 ASSERT_EQ(0, file.FlushCloseOrErase());
663 close(pipefd[0]);
664 }
665
666 } // namespace unix_file
667