1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/installer/util/create_dir_work_item.h"
#include <windows.h>
#include <memory>
#include "base/base_paths.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/string_util.h"
#include "chrome/installer/util/work_item.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class CreateDirWorkItemTest : public testing::Test {
protected:
void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
base::ScopedTempDir temp_dir_;
};
} // namespace
TEST_F(CreateDirWorkItemTest, CreatePath) {
base::FilePath parent_dir(temp_dir_.GetPath());
parent_dir = parent_dir.AppendASCII("a");
base::CreateDirectory(parent_dir);
ASSERT_TRUE(base::PathExists(parent_dir));
base::FilePath top_dir_to_create(parent_dir);
top_dir_to_create = top_dir_to_create.AppendASCII("b");
base::FilePath dir_to_create(top_dir_to_create);
dir_to_create = dir_to_create.AppendASCII("c");
dir_to_create = dir_to_create.AppendASCII("d");
std::unique_ptr<CreateDirWorkItem> work_item(
WorkItem::CreateCreateDirWorkItem(dir_to_create));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(dir_to_create));
work_item->Rollback();
// Rollback should delete all the paths up to top_dir_to_create.
EXPECT_FALSE(base::PathExists(top_dir_to_create));
EXPECT_TRUE(base::PathExists(parent_dir));
}
TEST_F(CreateDirWorkItemTest, CreateExistingPath) {
base::FilePath dir_to_create(temp_dir_.GetPath());
dir_to_create = dir_to_create.AppendASCII("aa");
base::CreateDirectory(dir_to_create);
ASSERT_TRUE(base::PathExists(dir_to_create));
std::unique_ptr<CreateDirWorkItem> work_item(
WorkItem::CreateCreateDirWorkItem(dir_to_create));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(dir_to_create));
work_item->Rollback();
// Rollback should not remove the path since it exists before
// the CreateDirWorkItem is called.
EXPECT_TRUE(base::PathExists(dir_to_create));
}
TEST_F(CreateDirWorkItemTest, CreateSharedPath) {
base::FilePath dir_to_create_1(temp_dir_.GetPath());
dir_to_create_1 = dir_to_create_1.AppendASCII("aaa");
base::FilePath dir_to_create_2(dir_to_create_1);
dir_to_create_2 = dir_to_create_2.AppendASCII("bbb");
base::FilePath dir_to_create_3(dir_to_create_2);
dir_to_create_3 = dir_to_create_3.AppendASCII("ccc");
std::unique_ptr<CreateDirWorkItem> work_item(
WorkItem::CreateCreateDirWorkItem(dir_to_create_3));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(dir_to_create_3));
// Create another directory under dir_to_create_2
base::FilePath dir_to_create_4(dir_to_create_2);
dir_to_create_4 = dir_to_create_4.AppendASCII("ddd");
base::CreateDirectory(dir_to_create_4);
ASSERT_TRUE(base::PathExists(dir_to_create_4));
work_item->Rollback();
// Rollback should delete dir_to_create_3.
EXPECT_FALSE(base::PathExists(dir_to_create_3));
// Rollback should not delete dir_to_create_2 as it is shared.
EXPECT_TRUE(base::PathExists(dir_to_create_2));
EXPECT_TRUE(base::PathExists(dir_to_create_4));
}
TEST_F(CreateDirWorkItemTest, RollbackWithMissingDir) {
base::FilePath dir_to_create_1(temp_dir_.GetPath());
dir_to_create_1 = dir_to_create_1.AppendASCII("aaaa");
base::FilePath dir_to_create_2(dir_to_create_1);
dir_to_create_2 = dir_to_create_2.AppendASCII("bbbb");
base::FilePath dir_to_create_3(dir_to_create_2);
dir_to_create_3 = dir_to_create_3.AppendASCII("cccc");
std::unique_ptr<CreateDirWorkItem> work_item(
WorkItem::CreateCreateDirWorkItem(dir_to_create_3));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(dir_to_create_3));
RemoveDirectory(dir_to_create_3.value().c_str());
ASSERT_FALSE(base::PathExists(dir_to_create_3));
work_item->Rollback();
// dir_to_create_3 has already been deleted, Rollback should delete
// the rest.
EXPECT_FALSE(base::PathExists(dir_to_create_1));
}
|