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