| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 <windows.h> | 5 #include <windows.h> |
| 6 | 6 |
| 7 #include <fstream> | 7 #include <fstream> |
| 8 | 8 |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/process_util.h" | 13 #include "base/process_util.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "chrome/installer/util/work_item.h" | 15 #include "chrome/installer/util/work_item.h" |
| 16 #include "chrome/installer/util/move_tree_work_item.h" | 16 #include "chrome/installer/util/move_tree_work_item.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 class MoveTreeWorkItemTest : public testing::Test { | 20 class MoveTreeWorkItemTest : public testing::Test { |
| 21 protected: | 21 protected: |
| 22 virtual void SetUp() { | 22 virtual void SetUp() { |
| 23 // Name a subdirectory of the user temp directory. | 23 ASSERT_TRUE(temp_from_dir_.CreateUniqueTempDir()); |
| 24 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | 24 ASSERT_TRUE(temp_to_dir_.CreateUniqueTempDir()); |
| 25 test_dir_ = test_dir_.AppendASCII("MoveTreeWorkItemTest"); | |
| 26 | |
| 27 // Create a fresh, empty copy of this test directory. | |
| 28 file_util::Delete(test_dir_, true); | |
| 29 file_util::CreateDirectoryW(test_dir_); | |
| 30 | |
| 31 // Create a tempory directory under the test directory. | |
| 32 temp_dir_ = test_dir_.AppendASCII("temp"); | |
| 33 file_util::CreateDirectoryW(temp_dir_); | |
| 34 | |
| 35 ASSERT_TRUE(file_util::PathExists(test_dir_)); | |
| 36 ASSERT_TRUE(file_util::PathExists(temp_dir_)); | |
| 37 } | 25 } |
| 38 | 26 |
| 39 virtual void TearDown() { | 27 ScopedTempDir temp_from_dir_; |
| 40 // Clean up test directory | 28 ScopedTempDir temp_to_dir_; |
| 41 ASSERT_TRUE(file_util::Delete(test_dir_, true)); | |
| 42 ASSERT_FALSE(file_util::PathExists(test_dir_)); | |
| 43 } | |
| 44 | |
| 45 // the path to temporary directory used to contain the test operations | |
| 46 FilePath test_dir_; | |
| 47 FilePath temp_dir_; | |
| 48 }; | 29 }; |
| 49 | 30 |
| 50 // Simple function to dump some text into a new file. | 31 // Simple function to dump some text into a new file. |
| 51 void CreateTextFile(const std::wstring& filename, | 32 void CreateTextFile(const std::wstring& filename, |
| 52 const std::wstring& contents) { | 33 const std::wstring& contents) { |
| 53 std::ofstream file; | 34 std::ofstream file; |
| 54 file.open(filename.c_str()); | 35 file.open(filename.c_str()); |
| 55 ASSERT_TRUE(file.is_open()); | 36 ASSERT_TRUE(file.is_open()); |
| 56 file << contents; | 37 file << contents; |
| 57 file.close(); | 38 file.close(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 69 } | 50 } |
| 70 | 51 |
| 71 wchar_t text_content_1[] = L"Gooooooooooooooooooooogle"; | 52 wchar_t text_content_1[] = L"Gooooooooooooooooooooogle"; |
| 72 wchar_t text_content_2[] = L"Overwrite Me"; | 53 wchar_t text_content_2[] = L"Overwrite Me"; |
| 73 }; | 54 }; |
| 74 | 55 |
| 75 // Move one directory from source to destination when destination does not | 56 // Move one directory from source to destination when destination does not |
| 76 // exist. | 57 // exist. |
| 77 TEST_F(MoveTreeWorkItemTest, MoveDirectory) { | 58 TEST_F(MoveTreeWorkItemTest, MoveDirectory) { |
| 78 // Create two level deep source dir | 59 // Create two level deep source dir |
| 79 FilePath from_dir1(test_dir_); | 60 FilePath from_dir1(temp_from_dir_.path()); |
| 80 from_dir1 = from_dir1.AppendASCII("From_Dir1"); | 61 from_dir1 = from_dir1.AppendASCII("From_Dir1"); |
| 81 file_util::CreateDirectory(from_dir1); | 62 file_util::CreateDirectory(from_dir1); |
| 82 ASSERT_TRUE(file_util::PathExists(from_dir1)); | 63 ASSERT_TRUE(file_util::PathExists(from_dir1)); |
| 83 | 64 |
| 84 FilePath from_dir2(from_dir1); | 65 FilePath from_dir2(from_dir1); |
| 85 from_dir2 = from_dir2.AppendASCII("From_Dir2"); | 66 from_dir2 = from_dir2.AppendASCII("From_Dir2"); |
| 86 file_util::CreateDirectory(from_dir2); | 67 file_util::CreateDirectory(from_dir2); |
| 87 ASSERT_TRUE(file_util::PathExists(from_dir2)); | 68 ASSERT_TRUE(file_util::PathExists(from_dir2)); |
| 88 | 69 |
| 89 FilePath from_file(from_dir2); | 70 FilePath from_file(from_dir2); |
| 90 from_file = from_file.AppendASCII("From_File"); | 71 from_file = from_file.AppendASCII("From_File"); |
| 91 CreateTextFile(from_file.value(), text_content_1); | 72 CreateTextFile(from_file.value(), text_content_1); |
| 92 ASSERT_TRUE(file_util::PathExists(from_file)); | 73 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 93 | 74 |
| 94 // Generate destination path | 75 // Generate destination path |
| 95 FilePath to_dir(test_dir_); | 76 FilePath to_dir(temp_from_dir_.path()); |
| 96 to_dir = to_dir.AppendASCII("To_Dir"); | 77 to_dir = to_dir.AppendASCII("To_Dir"); |
| 97 ASSERT_FALSE(file_util::PathExists(to_dir)); | 78 ASSERT_FALSE(file_util::PathExists(to_dir)); |
| 98 | 79 |
| 99 FilePath to_file(to_dir); | 80 FilePath to_file(to_dir); |
| 100 to_file = to_file.AppendASCII("From_Dir2"); | 81 to_file = to_file.AppendASCII("From_Dir2"); |
| 101 to_file = to_file.AppendASCII("From_File"); | 82 to_file = to_file.AppendASCII("From_File"); |
| 102 ASSERT_FALSE(file_util::PathExists(to_file)); | 83 ASSERT_FALSE(file_util::PathExists(to_file)); |
| 103 | 84 |
| 104 // test Do() | 85 // test Do() |
| 105 scoped_ptr<MoveTreeWorkItem> work_item( | 86 scoped_ptr<MoveTreeWorkItem> work_item( |
| 106 WorkItem::CreateMoveTreeWorkItem(from_dir1, to_dir, temp_dir_)); | 87 WorkItem::CreateMoveTreeWorkItem(from_dir1, to_dir, temp_to_dir_.path())); |
| 107 EXPECT_TRUE(work_item->Do()); | 88 EXPECT_TRUE(work_item->Do()); |
| 108 | 89 |
| 109 EXPECT_FALSE(file_util::PathExists(from_dir1)); | 90 EXPECT_FALSE(file_util::PathExists(from_dir1)); |
| 110 EXPECT_TRUE(file_util::PathExists(to_dir)); | 91 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 111 EXPECT_TRUE(file_util::PathExists(to_file)); | 92 EXPECT_TRUE(file_util::PathExists(to_file)); |
| 112 | 93 |
| 113 // test rollback() | 94 // test rollback() |
| 114 work_item->Rollback(); | 95 work_item->Rollback(); |
| 115 | 96 |
| 116 EXPECT_TRUE(file_util::PathExists(from_dir1)); | 97 EXPECT_TRUE(file_util::PathExists(from_dir1)); |
| 117 EXPECT_TRUE(file_util::PathExists(from_file)); | 98 EXPECT_TRUE(file_util::PathExists(from_file)); |
| 118 EXPECT_FALSE(file_util::PathExists(to_dir)); | 99 EXPECT_FALSE(file_util::PathExists(to_dir)); |
| 119 } | 100 } |
| 120 | 101 |
| 121 // Move one directory from source to destination when destination already | 102 // Move one directory from source to destination when destination already |
| 122 // exists. | 103 // exists. |
| 123 TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExists) { | 104 TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExists) { |
| 124 // Create two level deep source dir | 105 // Create two level deep source dir |
| 125 FilePath from_dir1(test_dir_); | 106 FilePath from_dir1(temp_from_dir_.path()); |
| 126 from_dir1 = from_dir1.AppendASCII("From_Dir1"); | 107 from_dir1 = from_dir1.AppendASCII("From_Dir1"); |
| 127 file_util::CreateDirectory(from_dir1); | 108 file_util::CreateDirectory(from_dir1); |
| 128 ASSERT_TRUE(file_util::PathExists(from_dir1)); | 109 ASSERT_TRUE(file_util::PathExists(from_dir1)); |
| 129 | 110 |
| 130 FilePath from_dir2(from_dir1); | 111 FilePath from_dir2(from_dir1); |
| 131 from_dir2 = from_dir2.AppendASCII("From_Dir2"); | 112 from_dir2 = from_dir2.AppendASCII("From_Dir2"); |
| 132 file_util::CreateDirectory(from_dir2); | 113 file_util::CreateDirectory(from_dir2); |
| 133 ASSERT_TRUE(file_util::PathExists(from_dir2)); | 114 ASSERT_TRUE(file_util::PathExists(from_dir2)); |
| 134 | 115 |
| 135 FilePath from_file(from_dir2); | 116 FilePath from_file(from_dir2); |
| 136 from_file = from_file.AppendASCII("From_File"); | 117 from_file = from_file.AppendASCII("From_File"); |
| 137 CreateTextFile(from_file.value(), text_content_1); | 118 CreateTextFile(from_file.value(), text_content_1); |
| 138 ASSERT_TRUE(file_util::PathExists(from_file)); | 119 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 139 | 120 |
| 140 // Create destination path | 121 // Create destination path |
| 141 FilePath to_dir(test_dir_); | 122 FilePath to_dir(temp_from_dir_.path()); |
| 142 to_dir = to_dir.AppendASCII("To_Dir"); | 123 to_dir = to_dir.AppendASCII("To_Dir"); |
| 143 file_util::CreateDirectory(to_dir); | 124 file_util::CreateDirectory(to_dir); |
| 144 ASSERT_TRUE(file_util::PathExists(to_dir)); | 125 ASSERT_TRUE(file_util::PathExists(to_dir)); |
| 145 | 126 |
| 146 FilePath orig_to_file(to_dir); | 127 FilePath orig_to_file(to_dir); |
| 147 orig_to_file = orig_to_file.AppendASCII("To_File"); | 128 orig_to_file = orig_to_file.AppendASCII("To_File"); |
| 148 CreateTextFile(orig_to_file.value(), text_content_2); | 129 CreateTextFile(orig_to_file.value(), text_content_2); |
| 149 ASSERT_TRUE(file_util::PathExists(orig_to_file)); | 130 ASSERT_TRUE(file_util::PathExists(orig_to_file)); |
| 150 | 131 |
| 151 FilePath new_to_file(to_dir); | 132 FilePath new_to_file(to_dir); |
| 152 new_to_file = new_to_file.AppendASCII("From_Dir2"); | 133 new_to_file = new_to_file.AppendASCII("From_Dir2"); |
| 153 new_to_file = new_to_file.AppendASCII("From_File"); | 134 new_to_file = new_to_file.AppendASCII("From_File"); |
| 154 ASSERT_FALSE(file_util::PathExists(new_to_file)); | 135 ASSERT_FALSE(file_util::PathExists(new_to_file)); |
| 155 | 136 |
| 156 // test Do() | 137 // test Do() |
| 157 scoped_ptr<MoveTreeWorkItem> work_item( | 138 scoped_ptr<MoveTreeWorkItem> work_item( |
| 158 WorkItem::CreateMoveTreeWorkItem(from_dir1, to_dir, temp_dir_)); | 139 WorkItem::CreateMoveTreeWorkItem(from_dir1, to_dir, temp_to_dir_.path())); |
| 159 EXPECT_TRUE(work_item->Do()); | 140 EXPECT_TRUE(work_item->Do()); |
| 160 | 141 |
| 161 EXPECT_FALSE(file_util::PathExists(from_dir1)); | 142 EXPECT_FALSE(file_util::PathExists(from_dir1)); |
| 162 EXPECT_TRUE(file_util::PathExists(to_dir)); | 143 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 163 EXPECT_TRUE(file_util::PathExists(new_to_file)); | 144 EXPECT_TRUE(file_util::PathExists(new_to_file)); |
| 164 EXPECT_FALSE(file_util::PathExists(orig_to_file)); | 145 EXPECT_FALSE(file_util::PathExists(orig_to_file)); |
| 165 | 146 |
| 166 // test rollback() | 147 // test rollback() |
| 167 work_item->Rollback(); | 148 work_item->Rollback(); |
| 168 | 149 |
| 169 EXPECT_TRUE(file_util::PathExists(from_dir1)); | 150 EXPECT_TRUE(file_util::PathExists(from_dir1)); |
| 170 EXPECT_TRUE(file_util::PathExists(to_dir)); | 151 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 171 EXPECT_FALSE(file_util::PathExists(new_to_file)); | 152 EXPECT_FALSE(file_util::PathExists(new_to_file)); |
| 172 EXPECT_TRUE(file_util::PathExists(orig_to_file)); | 153 EXPECT_TRUE(file_util::PathExists(orig_to_file)); |
| 173 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(text_content_2)); | 154 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(text_content_2)); |
| 174 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); | 155 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); |
| 175 } | 156 } |
| 176 | 157 |
| 177 // Move one file from source to destination when destination does not | 158 // Move one file from source to destination when destination does not |
| 178 // exist. | 159 // exist. |
| 179 TEST_F(MoveTreeWorkItemTest, MoveAFile) { | 160 TEST_F(MoveTreeWorkItemTest, MoveAFile) { |
| 180 // Create a file inside source dir | 161 // Create a file inside source dir |
| 181 FilePath from_dir(test_dir_); | 162 FilePath from_dir(temp_from_dir_.path()); |
| 182 from_dir = from_dir.AppendASCII("From_Dir"); | 163 from_dir = from_dir.AppendASCII("From_Dir"); |
| 183 file_util::CreateDirectory(from_dir); | 164 file_util::CreateDirectory(from_dir); |
| 184 ASSERT_TRUE(file_util::PathExists(from_dir)); | 165 ASSERT_TRUE(file_util::PathExists(from_dir)); |
| 185 | 166 |
| 186 FilePath from_file(from_dir); | 167 FilePath from_file(from_dir); |
| 187 from_file = from_file.AppendASCII("From_File"); | 168 from_file = from_file.AppendASCII("From_File"); |
| 188 CreateTextFile(from_file.value(), text_content_1); | 169 CreateTextFile(from_file.value(), text_content_1); |
| 189 ASSERT_TRUE(file_util::PathExists(from_file)); | 170 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 190 | 171 |
| 191 // Generate destination file name | 172 // Generate destination file name |
| 192 FilePath to_file(test_dir_); | 173 FilePath to_file(temp_from_dir_.path()); |
| 193 to_file = to_file.AppendASCII("To_File"); | 174 to_file = to_file.AppendASCII("To_File"); |
| 194 ASSERT_FALSE(file_util::PathExists(to_file)); | 175 ASSERT_FALSE(file_util::PathExists(to_file)); |
| 195 | 176 |
| 196 // test Do() | 177 // test Do() |
| 197 scoped_ptr<MoveTreeWorkItem> work_item( | 178 scoped_ptr<MoveTreeWorkItem> work_item( |
| 198 WorkItem::CreateMoveTreeWorkItem(from_file, to_file, temp_dir_)); | 179 WorkItem::CreateMoveTreeWorkItem( |
| 180 from_file, to_file, temp_to_dir_.path())); |
| 199 EXPECT_TRUE(work_item->Do()); | 181 EXPECT_TRUE(work_item->Do()); |
| 200 | 182 |
| 201 EXPECT_TRUE(file_util::PathExists(from_dir)); | 183 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 202 EXPECT_FALSE(file_util::PathExists(from_file)); | 184 EXPECT_FALSE(file_util::PathExists(from_file)); |
| 203 EXPECT_TRUE(file_util::PathExists(to_file)); | 185 EXPECT_TRUE(file_util::PathExists(to_file)); |
| 204 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_1)); | 186 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_1)); |
| 205 | 187 |
| 206 // test rollback() | 188 // test rollback() |
| 207 work_item->Rollback(); | 189 work_item->Rollback(); |
| 208 | 190 |
| 209 EXPECT_TRUE(file_util::PathExists(from_dir)); | 191 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 210 EXPECT_TRUE(file_util::PathExists(from_file)); | 192 EXPECT_TRUE(file_util::PathExists(from_file)); |
| 211 EXPECT_FALSE(file_util::PathExists(to_file)); | 193 EXPECT_FALSE(file_util::PathExists(to_file)); |
| 212 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); | 194 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); |
| 213 } | 195 } |
| 214 | 196 |
| 215 // Move one file from source to destination when destination already | 197 // Move one file from source to destination when destination already |
| 216 // exists. | 198 // exists. |
| 217 TEST_F(MoveTreeWorkItemTest, MoveFileDestExists) { | 199 TEST_F(MoveTreeWorkItemTest, MoveFileDestExists) { |
| 218 // Create a file inside source dir | 200 // Create a file inside source dir |
| 219 FilePath from_dir(test_dir_); | 201 FilePath from_dir(temp_from_dir_.path()); |
| 220 from_dir = from_dir.AppendASCII("From_Dir"); | 202 from_dir = from_dir.AppendASCII("From_Dir"); |
| 221 file_util::CreateDirectory(from_dir); | 203 file_util::CreateDirectory(from_dir); |
| 222 ASSERT_TRUE(file_util::PathExists(from_dir)); | 204 ASSERT_TRUE(file_util::PathExists(from_dir)); |
| 223 | 205 |
| 224 FilePath from_file(from_dir); | 206 FilePath from_file(from_dir); |
| 225 from_file = from_file.AppendASCII("From_File"); | 207 from_file = from_file.AppendASCII("From_File"); |
| 226 CreateTextFile(from_file.value(), text_content_1); | 208 CreateTextFile(from_file.value(), text_content_1); |
| 227 ASSERT_TRUE(file_util::PathExists(from_file)); | 209 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 228 | 210 |
| 229 // Create destination path | 211 // Create destination path |
| 230 FilePath to_dir(test_dir_); | 212 FilePath to_dir(temp_from_dir_.path()); |
| 231 to_dir = to_dir.AppendASCII("To_Dir"); | 213 to_dir = to_dir.AppendASCII("To_Dir"); |
| 232 file_util::CreateDirectory(to_dir); | 214 file_util::CreateDirectory(to_dir); |
| 233 ASSERT_TRUE(file_util::PathExists(to_dir)); | 215 ASSERT_TRUE(file_util::PathExists(to_dir)); |
| 234 | 216 |
| 235 FilePath to_file(to_dir); | 217 FilePath to_file(to_dir); |
| 236 to_file = to_file.AppendASCII("To_File"); | 218 to_file = to_file.AppendASCII("To_File"); |
| 237 CreateTextFile(to_file.value(), text_content_2); | 219 CreateTextFile(to_file.value(), text_content_2); |
| 238 ASSERT_TRUE(file_util::PathExists(to_file)); | 220 ASSERT_TRUE(file_util::PathExists(to_file)); |
| 239 | 221 |
| 240 // test Do() | 222 // test Do() |
| 241 scoped_ptr<MoveTreeWorkItem> work_item( | 223 scoped_ptr<MoveTreeWorkItem> work_item( |
| 242 WorkItem::CreateMoveTreeWorkItem(from_file, to_dir, temp_dir_)); | 224 WorkItem::CreateMoveTreeWorkItem(from_file, to_dir, temp_to_dir_.path())); |
| 243 EXPECT_TRUE(work_item->Do()); | 225 EXPECT_TRUE(work_item->Do()); |
| 244 | 226 |
| 245 EXPECT_TRUE(file_util::PathExists(from_dir)); | 227 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 246 EXPECT_FALSE(file_util::PathExists(from_file)); | 228 EXPECT_FALSE(file_util::PathExists(from_file)); |
| 247 EXPECT_TRUE(file_util::PathExists(to_dir)); | 229 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 248 EXPECT_FALSE(file_util::PathExists(to_file)); | 230 EXPECT_FALSE(file_util::PathExists(to_file)); |
| 249 EXPECT_EQ(0, ReadTextFile(to_dir).compare(text_content_1)); | 231 EXPECT_EQ(0, ReadTextFile(to_dir).compare(text_content_1)); |
| 250 | 232 |
| 251 // test rollback() | 233 // test rollback() |
| 252 work_item->Rollback(); | 234 work_item->Rollback(); |
| 253 | 235 |
| 254 EXPECT_TRUE(file_util::PathExists(from_dir)); | 236 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 255 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); | 237 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); |
| 256 EXPECT_TRUE(file_util::PathExists(to_dir)); | 238 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 257 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_2)); | 239 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_2)); |
| 258 } | 240 } |
| 259 | 241 |
| 260 // Move one file from source to destination when destination already | 242 // Move one file from source to destination when destination already |
| 261 // exists and is in use. | 243 // exists and is in use. |
| 262 TEST_F(MoveTreeWorkItemTest, MoveFileDestInUse) { | 244 TEST_F(MoveTreeWorkItemTest, MoveFileDestInUse) { |
| 263 // Create a file inside source dir | 245 // Create a file inside source dir |
| 264 FilePath from_dir(test_dir_); | 246 FilePath from_dir(temp_from_dir_.path()); |
| 265 from_dir = from_dir.AppendASCII("From_Dir"); | 247 from_dir = from_dir.AppendASCII("From_Dir"); |
| 266 file_util::CreateDirectory(from_dir); | 248 file_util::CreateDirectory(from_dir); |
| 267 ASSERT_TRUE(file_util::PathExists(from_dir)); | 249 ASSERT_TRUE(file_util::PathExists(from_dir)); |
| 268 | 250 |
| 269 FilePath from_file(from_dir); | 251 FilePath from_file(from_dir); |
| 270 from_file = from_file.AppendASCII("From_File"); | 252 from_file = from_file.AppendASCII("From_File"); |
| 271 CreateTextFile(from_file.value(), text_content_1); | 253 CreateTextFile(from_file.value(), text_content_1); |
| 272 ASSERT_TRUE(file_util::PathExists(from_file)); | 254 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 273 | 255 |
| 274 // Create an executable in destination path by copying ourself to it. | 256 // Create an executable in destination path by copying ourself to it. |
| 275 FilePath to_dir(test_dir_); | 257 FilePath to_dir(temp_from_dir_.path()); |
| 276 to_dir = to_dir.AppendASCII("To_Dir"); | 258 to_dir = to_dir.AppendASCII("To_Dir"); |
| 277 file_util::CreateDirectory(to_dir); | 259 file_util::CreateDirectory(to_dir); |
| 278 ASSERT_TRUE(file_util::PathExists(to_dir)); | 260 ASSERT_TRUE(file_util::PathExists(to_dir)); |
| 279 | 261 |
| 280 wchar_t exe_full_path_str[MAX_PATH]; | 262 wchar_t exe_full_path_str[MAX_PATH]; |
| 281 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); | 263 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); |
| 282 FilePath exe_full_path(exe_full_path_str); | 264 FilePath exe_full_path(exe_full_path_str); |
| 283 FilePath to_file(to_dir); | 265 FilePath to_file(to_dir); |
| 284 to_file = to_file.AppendASCII("To_File"); | 266 to_file = to_file.AppendASCII("To_File"); |
| 285 file_util::CopyFile(exe_full_path, to_file); | 267 file_util::CopyFile(exe_full_path, to_file); |
| 286 ASSERT_TRUE(file_util::PathExists(to_file)); | 268 ASSERT_TRUE(file_util::PathExists(to_file)); |
| 287 | 269 |
| 288 // Run the executable in destination path | 270 // Run the executable in destination path |
| 289 STARTUPINFOW si = {sizeof(si)}; | 271 STARTUPINFOW si = {sizeof(si)}; |
| 290 PROCESS_INFORMATION pi = {0}; | 272 PROCESS_INFORMATION pi = {0}; |
| 291 ASSERT_TRUE(::CreateProcess(NULL, | 273 ASSERT_TRUE(::CreateProcess(NULL, |
| 292 const_cast<wchar_t*>(to_file.value().c_str()), | 274 const_cast<wchar_t*>(to_file.value().c_str()), |
| 293 NULL, NULL, FALSE, | 275 NULL, NULL, FALSE, |
| 294 CREATE_NO_WINDOW | CREATE_SUSPENDED, | 276 CREATE_NO_WINDOW | CREATE_SUSPENDED, |
| 295 NULL, NULL, &si, &pi)); | 277 NULL, NULL, &si, &pi)); |
| 296 | 278 |
| 297 // test Do() | 279 // test Do() |
| 298 scoped_ptr<MoveTreeWorkItem> work_item( | 280 scoped_ptr<MoveTreeWorkItem> work_item( |
| 299 WorkItem::CreateMoveTreeWorkItem(from_file, to_file, temp_dir_)); | 281 WorkItem::CreateMoveTreeWorkItem( |
| 282 from_file, to_file, temp_to_dir_.path())); |
| 300 EXPECT_TRUE(work_item->Do()); | 283 EXPECT_TRUE(work_item->Do()); |
| 301 | 284 |
| 302 EXPECT_TRUE(file_util::PathExists(from_dir)); | 285 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 303 EXPECT_FALSE(file_util::PathExists(from_file)); | 286 EXPECT_FALSE(file_util::PathExists(from_file)); |
| 304 EXPECT_TRUE(file_util::PathExists(to_dir)); | 287 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 305 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_1)); | 288 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_1)); |
| 306 | 289 |
| 307 // test rollback() | 290 // test rollback() |
| 308 work_item->Rollback(); | 291 work_item->Rollback(); |
| 309 | 292 |
| 310 EXPECT_TRUE(file_util::PathExists(from_dir)); | 293 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 311 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); | 294 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); |
| 312 EXPECT_TRUE(file_util::PathExists(to_dir)); | 295 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 313 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); | 296 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); |
| 314 | 297 |
| 315 TerminateProcess(pi.hProcess, 0); | 298 TerminateProcess(pi.hProcess, 0); |
| 316 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); | 299 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); |
| 317 CloseHandle(pi.hProcess); | 300 CloseHandle(pi.hProcess); |
| 318 CloseHandle(pi.hThread); | 301 CloseHandle(pi.hThread); |
| 319 } | 302 } |
| 320 | 303 |
| 321 // Move one file that is in use to destination. | 304 // Move one file that is in use to destination. |
| 322 TEST_F(MoveTreeWorkItemTest, MoveFileInUse) { | 305 TEST_F(MoveTreeWorkItemTest, MoveFileInUse) { |
| 323 // Create an executable for source by copying ourself to a new source dir. | 306 // Create an executable for source by copying ourself to a new source dir. |
| 324 FilePath from_dir(test_dir_); | 307 FilePath from_dir(temp_from_dir_.path()); |
| 325 from_dir = from_dir.AppendASCII("From_Dir"); | 308 from_dir = from_dir.AppendASCII("From_Dir"); |
| 326 file_util::CreateDirectory(from_dir); | 309 file_util::CreateDirectory(from_dir); |
| 327 ASSERT_TRUE(file_util::PathExists(from_dir)); | 310 ASSERT_TRUE(file_util::PathExists(from_dir)); |
| 328 | 311 |
| 329 wchar_t exe_full_path_str[MAX_PATH]; | 312 wchar_t exe_full_path_str[MAX_PATH]; |
| 330 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); | 313 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); |
| 331 FilePath exe_full_path(exe_full_path_str); | 314 FilePath exe_full_path(exe_full_path_str); |
| 332 FilePath from_file(from_dir); | 315 FilePath from_file(from_dir); |
| 333 from_file = from_file.AppendASCII("From_File"); | 316 from_file = from_file.AppendASCII("From_File"); |
| 334 file_util::CopyFile(exe_full_path, from_file); | 317 file_util::CopyFile(exe_full_path, from_file); |
| 335 ASSERT_TRUE(file_util::PathExists(from_file)); | 318 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 336 | 319 |
| 337 // Create a destination source dir and generate destination file name. | 320 // Create a destination source dir and generate destination file name. |
| 338 FilePath to_dir(test_dir_); | 321 FilePath to_dir(temp_from_dir_.path()); |
| 339 to_dir = to_dir.AppendASCII("To_Dir"); | 322 to_dir = to_dir.AppendASCII("To_Dir"); |
| 340 file_util::CreateDirectory(to_dir); | 323 file_util::CreateDirectory(to_dir); |
| 341 ASSERT_TRUE(file_util::PathExists(to_dir)); | 324 ASSERT_TRUE(file_util::PathExists(to_dir)); |
| 342 | 325 |
| 343 FilePath to_file(to_dir); | 326 FilePath to_file(to_dir); |
| 344 to_file = to_file.AppendASCII("To_File"); | 327 to_file = to_file.AppendASCII("To_File"); |
| 345 CreateTextFile(to_file.value(), text_content_1); | 328 CreateTextFile(to_file.value(), text_content_1); |
| 346 ASSERT_TRUE(file_util::PathExists(to_file)); | 329 ASSERT_TRUE(file_util::PathExists(to_file)); |
| 347 | 330 |
| 348 // Run the executable in source path | 331 // Run the executable in source path |
| 349 STARTUPINFOW si = {sizeof(si)}; | 332 STARTUPINFOW si = {sizeof(si)}; |
| 350 PROCESS_INFORMATION pi = {0}; | 333 PROCESS_INFORMATION pi = {0}; |
| 351 ASSERT_TRUE(::CreateProcess(NULL, | 334 ASSERT_TRUE(::CreateProcess(NULL, |
| 352 const_cast<wchar_t*>(from_file.value().c_str()), | 335 const_cast<wchar_t*>(from_file.value().c_str()), |
| 353 NULL, NULL, FALSE, | 336 NULL, NULL, FALSE, |
| 354 CREATE_NO_WINDOW | CREATE_SUSPENDED, | 337 CREATE_NO_WINDOW | CREATE_SUSPENDED, |
| 355 NULL, NULL, &si, &pi)); | 338 NULL, NULL, &si, &pi)); |
| 356 | 339 |
| 357 // test Do() | 340 // test Do() |
| 358 scoped_ptr<MoveTreeWorkItem> work_item( | 341 scoped_ptr<MoveTreeWorkItem> work_item( |
| 359 WorkItem::CreateMoveTreeWorkItem(from_file, to_file, temp_dir_)); | 342 WorkItem::CreateMoveTreeWorkItem( |
| 343 from_file, to_file, temp_to_dir_.path())); |
| 360 EXPECT_TRUE(work_item->Do()); | 344 EXPECT_TRUE(work_item->Do()); |
| 361 | 345 |
| 362 EXPECT_TRUE(file_util::PathExists(from_dir)); | 346 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 363 EXPECT_FALSE(file_util::PathExists(from_file)); | 347 EXPECT_FALSE(file_util::PathExists(from_file)); |
| 364 EXPECT_TRUE(file_util::PathExists(to_dir)); | 348 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 365 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); | 349 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); |
| 366 | 350 |
| 367 // Close the process and make sure all the conditions after Do() are | 351 // Close the process and make sure all the conditions after Do() are |
| 368 // still true. | 352 // still true. |
| 369 TerminateProcess(pi.hProcess, 0); | 353 TerminateProcess(pi.hProcess, 0); |
| 370 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); | 354 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); |
| 371 CloseHandle(pi.hProcess); | 355 CloseHandle(pi.hProcess); |
| 372 CloseHandle(pi.hThread); | 356 CloseHandle(pi.hThread); |
| 373 | 357 |
| 374 EXPECT_TRUE(file_util::PathExists(from_dir)); | 358 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 375 EXPECT_FALSE(file_util::PathExists(from_file)); | 359 EXPECT_FALSE(file_util::PathExists(from_file)); |
| 376 EXPECT_TRUE(file_util::PathExists(to_dir)); | 360 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 377 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); | 361 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); |
| 378 | 362 |
| 379 // test rollback() | 363 // test rollback() |
| 380 work_item->Rollback(); | 364 work_item->Rollback(); |
| 381 | 365 |
| 382 EXPECT_TRUE(file_util::PathExists(from_dir)); | 366 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 383 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, from_file)); | 367 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, from_file)); |
| 384 EXPECT_TRUE(file_util::PathExists(to_dir)); | 368 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 385 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_1)); | 369 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_1)); |
| 386 } | 370 } |
| OLD | NEW |