1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/zucchini/mapped_file.h"
6
7 #include <utility>
8
9 #include "base/files/file.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace zucchini {
16
17 class MappedFileWriterTest : public testing::Test {
18 protected:
19 MappedFileWriterTest() = default;
SetUp()20 void SetUp() override {
21 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
22 file_path_ = temp_dir_.GetPath().AppendASCII("test-file");
23 }
24
25 base::FilePath file_path_;
26
27 private:
28 base::ScopedTempDir temp_dir_;
29 };
30
TEST_F(MappedFileWriterTest,Keep)31 TEST_F(MappedFileWriterTest, Keep) {
32 EXPECT_FALSE(base::PathExists(file_path_));
33 {
34 using base::File;
35 File file(file_path_, File::FLAG_CREATE_ALWAYS | File::FLAG_READ |
36 File::FLAG_WRITE | File::FLAG_WIN_SHARE_DELETE |
37 File::FLAG_CAN_DELETE_ON_CLOSE);
38 MappedFileWriter file_writer(file_path_, std::move(file), 10);
39 EXPECT_FALSE(file_writer.HasError());
40 EXPECT_TRUE(file_writer.Keep());
41 EXPECT_FALSE(file_writer.HasError());
42 EXPECT_TRUE(file_writer.error().empty());
43 }
44 EXPECT_TRUE(base::PathExists(file_path_));
45 }
46
TEST_F(MappedFileWriterTest,DeleteOnClose)47 TEST_F(MappedFileWriterTest, DeleteOnClose) {
48 EXPECT_FALSE(base::PathExists(file_path_));
49 {
50 using base::File;
51 File file(file_path_, File::FLAG_CREATE_ALWAYS | File::FLAG_READ |
52 File::FLAG_WRITE | File::FLAG_WIN_SHARE_DELETE |
53 File::FLAG_CAN_DELETE_ON_CLOSE);
54 MappedFileWriter file_writer(file_path_, std::move(file), 10);
55 EXPECT_FALSE(file_writer.HasError());
56 EXPECT_TRUE(file_writer.error().empty());
57 }
58 EXPECT_FALSE(base::PathExists(file_path_));
59 }
60
61 } // namespace zucchini
62