Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(679)

Side by Side Diff: base/files/file_unittest.cc

Issue 2567383002: Add FLAG_CAN_DELETE_ON_CLOSE and DeleteOnClose for use on Windows. (Closed)
Patch Set: clang compile fix Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/file.h ('k') | base/files/file_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file.h" 5 #include "base/files/file.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/files/memory_mapped_file.h"
12 #include "base/files/scoped_temp_dir.h" 13 #include "base/files/scoped_temp_dir.h"
13 #include "base/time/time.h" 14 #include "base/time/time.h"
14 #include "build/build_config.h" 15 #include "build/build_config.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 using base::File; 18 using base::File;
18 using base::FilePath; 19 using base::FilePath;
19 20
20 TEST(FileTest, Create) { 21 TEST(FileTest, Create) {
21 base::ScopedTempDir temp_dir; 22 base::ScopedTempDir temp_dir;
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. 511 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
511 NULL)); 512 NULL));
512 ASSERT_TRUE(dir.IsValid()); 513 ASSERT_TRUE(dir.IsValid());
513 514
514 base::File::Info info; 515 base::File::Info info;
515 EXPECT_TRUE(dir.GetInfo(&info)); 516 EXPECT_TRUE(dir.GetInfo(&info));
516 EXPECT_TRUE(info.is_directory); 517 EXPECT_TRUE(info.is_directory);
517 EXPECT_FALSE(info.is_symbolic_link); 518 EXPECT_FALSE(info.is_symbolic_link);
518 EXPECT_EQ(0, info.size); 519 EXPECT_EQ(0, info.size);
519 } 520 }
521
522 TEST(FileTest, DeleteNoop) {
523 base::ScopedTempDir temp_dir;
524 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
525 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
526
527 // Creating and closing a file with DELETE perms should do nothing special.
528 File file(file_path,
529 (base::File::FLAG_CREATE | base::File::FLAG_READ |
530 base::File::FLAG_WRITE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
531 ASSERT_TRUE(file.IsValid());
532 file.Close();
533 ASSERT_TRUE(base::PathExists(file_path));
534 }
535
536 TEST(FileTest, Delete) {
537 base::ScopedTempDir temp_dir;
538 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
539 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
540
541 // Creating a file with DELETE and then marking for delete on close should
542 // delete it.
543 File file(file_path,
544 (base::File::FLAG_CREATE | base::File::FLAG_READ |
545 base::File::FLAG_WRITE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
546 ASSERT_TRUE(file.IsValid());
547 ASSERT_TRUE(file.DeleteOnClose(true));
548 file.Close();
549 ASSERT_FALSE(base::PathExists(file_path));
550 }
551
552 TEST(FileTest, DeleteThenRevoke) {
553 base::ScopedTempDir temp_dir;
554 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
555 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
556
557 // Creating a file with DELETE, marking it for delete, then clearing delete on
558 // close should not delete it.
559 File file(file_path,
560 (base::File::FLAG_CREATE | base::File::FLAG_READ |
561 base::File::FLAG_WRITE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
562 ASSERT_TRUE(file.IsValid());
563 ASSERT_TRUE(file.DeleteOnClose(true));
564 ASSERT_TRUE(file.DeleteOnClose(false));
565 file.Close();
566 ASSERT_TRUE(base::PathExists(file_path));
567 }
568
569 TEST(FileTest, IrrevokableDeleteOnClose) {
570 base::ScopedTempDir temp_dir;
571 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
572 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
573
574 // DELETE_ON_CLOSE cannot be revoked by this opener.
575 File file(
576 file_path,
577 (base::File::FLAG_CREATE | base::File::FLAG_READ |
578 base::File::FLAG_WRITE | base::File::FLAG_DELETE_ON_CLOSE |
579 base::File::FLAG_SHARE_DELETE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
580 ASSERT_TRUE(file.IsValid());
581 // https://msdn.microsoft.com/library/windows/desktop/aa364221.aspx says that
582 // setting the dispositon has no effect if the handle was opened with
583 // FLAG_DELETE_ON_CLOSE. Do not make the test's success dependent on whether
584 // or not SetFileInformationByHandle indicates success or failure. (It happens
585 // to indicate success on Windows 10.)
586 file.DeleteOnClose(false);
587 file.Close();
588 ASSERT_FALSE(base::PathExists(file_path));
589 }
590
591 TEST(FileTest, IrrevokableDeleteOnCloseOther) {
592 base::ScopedTempDir temp_dir;
593 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
594 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
595
596 // DELETE_ON_CLOSE cannot be revoked by another opener.
597 File file(
598 file_path,
599 (base::File::FLAG_CREATE | base::File::FLAG_READ |
600 base::File::FLAG_WRITE | base::File::FLAG_DELETE_ON_CLOSE |
601 base::File::FLAG_SHARE_DELETE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
602 ASSERT_TRUE(file.IsValid());
603
604 File file2(
605 file_path,
606 (base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE |
607 base::File::FLAG_SHARE_DELETE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
608 ASSERT_TRUE(file2.IsValid());
609
610 file2.DeleteOnClose(false);
611 file2.Close();
612 ASSERT_TRUE(base::PathExists(file_path));
613 file.Close();
614 ASSERT_FALSE(base::PathExists(file_path));
615 }
616
617 TEST(FileTest, DeleteWithoutPermission) {
618 base::ScopedTempDir temp_dir;
619 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
620 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
621
622 // It should not be possible to mark a file for deletion when it was not
623 // created/opened with DELETE.
624 File file(file_path, (base::File::FLAG_CREATE | base::File::FLAG_READ |
625 base::File::FLAG_WRITE));
626 ASSERT_TRUE(file.IsValid());
627 ASSERT_FALSE(file.DeleteOnClose(true));
628 file.Close();
629 ASSERT_TRUE(base::PathExists(file_path));
630 }
631
632 TEST(FileTest, UnsharedDeleteOnClose) {
633 base::ScopedTempDir temp_dir;
634 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
635 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
636
637 // Opening with DELETE_ON_CLOSE when a previous opener hasn't enabled sharing
638 // will fail.
639 File file(file_path, (base::File::FLAG_CREATE | base::File::FLAG_READ |
640 base::File::FLAG_WRITE));
641 ASSERT_TRUE(file.IsValid());
642 File file2(
643 file_path,
644 (base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE |
645 base::File::FLAG_DELETE_ON_CLOSE | base::File::FLAG_SHARE_DELETE));
646 ASSERT_FALSE(file2.IsValid());
647
648 file.Close();
649 ASSERT_TRUE(base::PathExists(file_path));
650 }
651
652 TEST(FileTest, NoDeleteOnCloseWithMappedFile) {
653 base::ScopedTempDir temp_dir;
654 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
655 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
656
657 // Mapping a file into memory blocks DeleteOnClose.
658 File file(file_path,
659 (base::File::FLAG_CREATE | base::File::FLAG_READ |
660 base::File::FLAG_WRITE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
661 ASSERT_TRUE(file.IsValid());
662 ASSERT_EQ(5, file.WriteAtCurrentPos("12345", 5));
663
664 {
665 base::MemoryMappedFile mapping;
666 ASSERT_TRUE(mapping.Initialize(file.Duplicate()));
667 ASSERT_EQ(5U, mapping.length());
668
669 EXPECT_FALSE(file.DeleteOnClose(true));
670 }
671
672 file.Close();
673 ASSERT_TRUE(base::PathExists(file_path));
674 }
520 #endif // defined(OS_WIN) 675 #endif // defined(OS_WIN)
OLDNEW
« no previous file with comments | « base/files/file.h ('k') | base/files/file_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698