1 // Copyright (c) 2011 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.
7 #include "base/base_paths.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_util.h"
12 #include "chrome/installer/util/create_dir_work_item.h"
13 #include "chrome/installer/util/work_item.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 class CreateDirWorkItemTest
: public testing::Test
{
19 void SetUp() override
{ ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir()); }
21 base::ScopedTempDir temp_dir_
;
25 TEST_F(CreateDirWorkItemTest
, CreatePath
) {
26 base::FilePath
parent_dir(temp_dir_
.path());
27 parent_dir
= parent_dir
.AppendASCII("a");
28 base::CreateDirectory(parent_dir
);
29 ASSERT_TRUE(base::PathExists(parent_dir
));
31 base::FilePath
top_dir_to_create(parent_dir
);
32 top_dir_to_create
= top_dir_to_create
.AppendASCII("b");
34 base::FilePath
dir_to_create(top_dir_to_create
);
35 dir_to_create
= dir_to_create
.AppendASCII("c");
36 dir_to_create
= dir_to_create
.AppendASCII("d");
38 scoped_ptr
<CreateDirWorkItem
> work_item(
39 WorkItem::CreateCreateDirWorkItem(dir_to_create
));
41 EXPECT_TRUE(work_item
->Do());
43 EXPECT_TRUE(base::PathExists(dir_to_create
));
45 work_item
->Rollback();
47 // Rollback should delete all the paths up to top_dir_to_create.
48 EXPECT_FALSE(base::PathExists(top_dir_to_create
));
49 EXPECT_TRUE(base::PathExists(parent_dir
));
52 TEST_F(CreateDirWorkItemTest
, CreateExistingPath
) {
53 base::FilePath
dir_to_create(temp_dir_
.path());
54 dir_to_create
= dir_to_create
.AppendASCII("aa");
55 base::CreateDirectory(dir_to_create
);
56 ASSERT_TRUE(base::PathExists(dir_to_create
));
58 scoped_ptr
<CreateDirWorkItem
> work_item(
59 WorkItem::CreateCreateDirWorkItem(dir_to_create
));
61 EXPECT_TRUE(work_item
->Do());
63 EXPECT_TRUE(base::PathExists(dir_to_create
));
65 work_item
->Rollback();
67 // Rollback should not remove the path since it exists before
68 // the CreateDirWorkItem is called.
69 EXPECT_TRUE(base::PathExists(dir_to_create
));
72 TEST_F(CreateDirWorkItemTest
, CreateSharedPath
) {
73 base::FilePath
dir_to_create_1(temp_dir_
.path());
74 dir_to_create_1
= dir_to_create_1
.AppendASCII("aaa");
76 base::FilePath
dir_to_create_2(dir_to_create_1
);
77 dir_to_create_2
= dir_to_create_2
.AppendASCII("bbb");
79 base::FilePath
dir_to_create_3(dir_to_create_2
);
80 dir_to_create_3
= dir_to_create_3
.AppendASCII("ccc");
82 scoped_ptr
<CreateDirWorkItem
> work_item(
83 WorkItem::CreateCreateDirWorkItem(dir_to_create_3
));
85 EXPECT_TRUE(work_item
->Do());
87 EXPECT_TRUE(base::PathExists(dir_to_create_3
));
89 // Create another directory under dir_to_create_2
90 base::FilePath
dir_to_create_4(dir_to_create_2
);
91 dir_to_create_4
= dir_to_create_4
.AppendASCII("ddd");
92 base::CreateDirectory(dir_to_create_4
);
93 ASSERT_TRUE(base::PathExists(dir_to_create_4
));
95 work_item
->Rollback();
97 // Rollback should delete dir_to_create_3.
98 EXPECT_FALSE(base::PathExists(dir_to_create_3
));
100 // Rollback should not delete dir_to_create_2 as it is shared.
101 EXPECT_TRUE(base::PathExists(dir_to_create_2
));
102 EXPECT_TRUE(base::PathExists(dir_to_create_4
));
105 TEST_F(CreateDirWorkItemTest
, RollbackWithMissingDir
) {
106 base::FilePath
dir_to_create_1(temp_dir_
.path());
107 dir_to_create_1
= dir_to_create_1
.AppendASCII("aaaa");
109 base::FilePath
dir_to_create_2(dir_to_create_1
);
110 dir_to_create_2
= dir_to_create_2
.AppendASCII("bbbb");
112 base::FilePath
dir_to_create_3(dir_to_create_2
);
113 dir_to_create_3
= dir_to_create_3
.AppendASCII("cccc");
115 scoped_ptr
<CreateDirWorkItem
> work_item(
116 WorkItem::CreateCreateDirWorkItem(dir_to_create_3
));
118 EXPECT_TRUE(work_item
->Do());
120 EXPECT_TRUE(base::PathExists(dir_to_create_3
));
122 RemoveDirectory(dir_to_create_3
.value().c_str());
123 ASSERT_FALSE(base::PathExists(dir_to_create_3
));
125 work_item
->Rollback();
127 // dir_to_create_3 has already been deleted, Rollback should delete
129 EXPECT_FALSE(base::PathExists(dir_to_create_1
));