initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame^] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include <windows.h> |
| 31 | #include <set> |
| 32 | #include <shellapi.h> |
| 33 | #include <shlobj.h> |
| 34 | |
| 35 | #include <fstream> |
| 36 | #include <iostream> |
| 37 | |
| 38 | #include "base/base_paths.h" |
| 39 | #include "base/file_util.h" |
| 40 | #include "base/logging.h" |
| 41 | #include "base/path_service.h" |
| 42 | #include "base/string_util.h" |
| 43 | #include "testing/gtest/include/gtest/gtest.h" |
| 44 | |
| 45 | namespace { |
| 46 | |
| 47 | class FileUtilTest : public testing::Test { |
| 48 | protected: |
| 49 | virtual void SetUp() { |
| 50 | // Name a subdirectory of the temp directory. |
| 51 | ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
| 52 | file_util::AppendToPath(&test_dir_, L"FileUtilTest"); |
| 53 | |
| 54 | // Create a fresh, empty copy of this directory. |
| 55 | file_util::Delete(test_dir_, true); |
| 56 | CreateDirectory(test_dir_.c_str(), NULL); |
| 57 | } |
| 58 | virtual void TearDown() { |
| 59 | // Clean up test directory |
| 60 | ASSERT_TRUE(file_util::Delete(test_dir_, false)); |
| 61 | ASSERT_FALSE(file_util::PathExists(test_dir_)); |
| 62 | } |
| 63 | |
| 64 | // the path to temporary directory used to contain the test operations |
| 65 | std::wstring test_dir_; |
| 66 | }; |
| 67 | |
| 68 | // Collects all the results from the given file enumerator, and provides an |
| 69 | // interface to query whether a given file is present. |
| 70 | class FindResultCollector { |
| 71 | public: |
| 72 | FindResultCollector(file_util::FileEnumerator& enumerator) { |
| 73 | std::wstring cur_file; |
| 74 | while (!(cur_file = enumerator.Next()).empty()) { |
| 75 | // The file should not be returned twice. |
| 76 | EXPECT_TRUE(files_.end() == files_.find(cur_file)) |
| 77 | << "Same file returned twice"; |
| 78 | |
| 79 | // Save for later. |
| 80 | files_.insert(cur_file); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Returns true if the enumerator found the file. |
| 85 | bool HasFile(const std::wstring& file) const { |
| 86 | return files_.find(file) != files_.end(); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | std::set<std::wstring> files_; |
| 91 | }; |
| 92 | |
| 93 | // Simple function to dump some text into a new file. |
| 94 | void CreateTextFile(const std::wstring& filename, |
| 95 | const std::wstring& contents) { |
| 96 | std::ofstream file; |
| 97 | file.open(filename.c_str()); |
| 98 | ASSERT_TRUE(file.is_open()); |
| 99 | file << contents; |
| 100 | file.close(); |
| 101 | } |
| 102 | |
| 103 | // Simple function to take out some text from a file. |
| 104 | std::wstring ReadTextFile(const std::wstring& filename) { |
| 105 | WCHAR contents[64]; |
| 106 | std::wifstream file; |
| 107 | file.open(filename.c_str()); |
| 108 | EXPECT_TRUE(file.is_open()); |
| 109 | file.getline(contents, 64); |
| 110 | file.close(); |
| 111 | return std::wstring(contents); |
| 112 | } |
| 113 | |
| 114 | uint64 FileTimeAsUint64(const FILETIME& ft) { |
| 115 | ULARGE_INTEGER u; |
| 116 | u.LowPart = ft.dwLowDateTime; |
| 117 | u.HighPart = ft.dwHighDateTime; |
| 118 | return u.QuadPart; |
| 119 | } |
| 120 | |
| 121 | const struct append_case { |
| 122 | const wchar_t* path; |
| 123 | const wchar_t* ending; |
| 124 | const wchar_t* result; |
| 125 | } append_cases[] = { |
| 126 | {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"}, |
| 127 | {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"}, |
| 128 | {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"}, |
| 129 | {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"}, |
| 130 | {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"}, |
| 131 | {L"", L"path", L"\\path"}, |
| 132 | {L"", L"", L"\\"}, |
| 133 | }; |
| 134 | |
| 135 | } // namespace |
| 136 | |
| 137 | TEST_F(FileUtilTest, AppendToPath) { |
| 138 | for (int i = 0; i < arraysize(append_cases); ++i) { |
| 139 | const append_case& value = append_cases[i]; |
| 140 | std::wstring result = value.path; |
| 141 | file_util::AppendToPath(&result, value.ending); |
| 142 | EXPECT_EQ(value.result, result); |
| 143 | } |
| 144 | |
| 145 | #ifdef NDEBUG |
| 146 | file_util::AppendToPath(NULL, L"path"); // asserts in debug mode |
| 147 | #endif |
| 148 | } |
| 149 | |
| 150 | static const struct InsertBeforeExtensionCase { |
| 151 | std::wstring path; |
| 152 | std::wstring suffix; |
| 153 | std::wstring result; |
| 154 | } kInsertBeforeExtension[] = { |
| 155 | {L"", L"", L""}, |
| 156 | {L"", L"txt", L"txt"}, |
| 157 | {L".", L"txt", L"txt."}, |
| 158 | {L".", L"", L"."}, |
| 159 | {L"foo.dll", L"txt", L"footxt.dll"}, |
| 160 | {L"foo.dll", L".txt", L"foo.txt.dll"}, |
| 161 | {L"foo", L"txt", L"footxt"}, |
| 162 | {L"foo", L".txt", L"foo.txt"}, |
| 163 | {L"foo.baz.dll", L"txt", L"foo.baztxt.dll"}, |
| 164 | {L"foo.baz.dll", L".txt", L"foo.baz.txt.dll"}, |
| 165 | {L"foo.dll", L"", L"foo.dll"}, |
| 166 | {L"foo.dll", L".", L"foo..dll"}, |
| 167 | {L"foo", L"", L"foo"}, |
| 168 | {L"foo", L".", L"foo."}, |
| 169 | {L"foo.baz.dll", L"", L"foo.baz.dll"}, |
| 170 | {L"foo.baz.dll", L".", L"foo.baz..dll"}, |
| 171 | {L"\\", L"", L"\\"}, |
| 172 | {L"\\", L"txt", L"\\txt"}, |
| 173 | {L"\\.", L"txt", L"\\txt."}, |
| 174 | {L"\\.", L"", L"\\."}, |
| 175 | {L"C:\\bar\\foo.dll", L"txt", L"C:\\bar\\footxt.dll"}, |
| 176 | {L"C:\\bar.baz\\foodll", L"txt", L"C:\\bar.baz\\foodlltxt"}, |
| 177 | {L"C:\\bar.baz\\foo.dll", L"txt", L"C:\\bar.baz\\footxt.dll"}, |
| 178 | {L"C:\\bar.baz\\foo.dll.exe", L"txt", L"C:\\bar.baz\\foo.dlltxt.exe"}, |
| 179 | {L"C:\\bar.baz\\foo", L"", L"C:\\bar.baz\\foo"}, |
| 180 | {L"C:\\bar.baz\\foo.exe", L"", L"C:\\bar.baz\\foo.exe"}, |
| 181 | {L"C:\\bar.baz\\foo.dll.exe", L"", L"C:\\bar.baz\\foo.dll.exe"}, |
| 182 | {L"C:\\bar\\baz\\foo.exe", L" (1)", L"C:\\bar\\baz\\foo (1).exe"}, |
| 183 | }; |
| 184 | |
| 185 | TEST_F(FileUtilTest, InsertBeforeExtensionTest) { |
| 186 | for (int i = 0; i < arraysize(kInsertBeforeExtension); ++i) { |
| 187 | std::wstring path(kInsertBeforeExtension[i].path); |
| 188 | file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix); |
| 189 | EXPECT_EQ(path, kInsertBeforeExtension[i].result); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | static const struct filename_case { |
| 194 | const wchar_t* path; |
| 195 | const wchar_t* filename; |
| 196 | } filename_cases[] = { |
| 197 | {L"c:\\colon\\backslash", L"backslash"}, |
| 198 | {L"c:\\colon\\backslash\\", L""}, |
| 199 | {L"\\\\filename.exe", L"filename.exe"}, |
| 200 | {L"filename.exe", L"filename.exe"}, |
| 201 | {L"", L""}, |
| 202 | {L"\\\\\\", L""}, |
| 203 | {L"c:/colon/backslash", L"backslash"}, |
| 204 | {L"c:/colon/backslash/", L""}, |
| 205 | {L"//////", L""}, |
| 206 | {L"///filename.exe", L"filename.exe"}, |
| 207 | }; |
| 208 | |
| 209 | TEST_F(FileUtilTest, GetFilenameFromPath) { |
| 210 | for (int i = 0; i < arraysize(filename_cases); ++i) { |
| 211 | const filename_case& value = filename_cases[i]; |
| 212 | std::wstring result = file_util::GetFilenameFromPath(value.path); |
| 213 | EXPECT_EQ(value.filename, result); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Test finding the file type from a path name |
| 218 | static const struct extension_case { |
| 219 | const wchar_t* path; |
| 220 | const wchar_t* extension; |
| 221 | } extension_cases[] = { |
| 222 | {L"C:\\colon\\backslash\\filename.extension", L"extension"}, |
| 223 | {L"C:\\colon\\backslash\\filename.", L""}, |
| 224 | {L"C:\\colon\\backslash\\filename", L""}, |
| 225 | {L"C:\\colon\\backslash\\", L""}, |
| 226 | {L"C:\\colon\\backslash.\\", L""}, |
| 227 | {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"}, |
| 228 | }; |
| 229 | |
| 230 | TEST_F(FileUtilTest, GetFileExtensionFromPath) { |
| 231 | for (int i = 0; i < arraysize(extension_cases); ++i) { |
| 232 | const extension_case& ext = extension_cases[i]; |
| 233 | const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path); |
| 234 | EXPECT_EQ(ext.extension, fext); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Test finding the directory component of a path |
| 239 | static const struct dir_case { |
| 240 | const wchar_t* full_path; |
| 241 | const wchar_t* directory; |
| 242 | } dir_cases[] = { |
| 243 | {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"}, |
| 244 | {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"}, |
| 245 | {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"}, |
| 246 | {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"}, |
| 247 | {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"}, |
| 248 | {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."}, |
| 249 | {L"C:\\", L"C:"}, |
| 250 | }; |
| 251 | |
| 252 | TEST_F(FileUtilTest, GetDirectoryFromPath) { |
| 253 | for (int i = 0; i < arraysize(dir_cases); ++i) { |
| 254 | const dir_case& dir = dir_cases[i]; |
| 255 | const std::wstring parent = |
| 256 | file_util::GetDirectoryFromPath(dir.full_path); |
| 257 | EXPECT_EQ(dir.directory, parent); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | TEST_F(FileUtilTest, CountFilesCreatedAfter) { |
| 262 | // Create old file (that we don't want to count) |
| 263 | std::wstring old_file_name = test_dir_; |
| 264 | file_util::AppendToPath(&old_file_name, L"Old File.txt"); |
| 265 | CreateTextFile(old_file_name, L"Just call me Mr. Creakybits"); |
| 266 | |
| 267 | // Age to perfection |
| 268 | Sleep(100); |
| 269 | |
| 270 | // Establish our cutoff time |
| 271 | FILETIME test_start_time; |
| 272 | GetSystemTimeAsFileTime(&test_start_time); |
| 273 | EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, test_start_time)); |
| 274 | |
| 275 | // Create a new file (that we do want to count) |
| 276 | std::wstring new_file_name = test_dir_; |
| 277 | file_util::AppendToPath(&new_file_name, L"New File.txt"); |
| 278 | CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah."); |
| 279 | |
| 280 | // We should see only the new file. |
| 281 | EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, test_start_time)); |
| 282 | |
| 283 | // Delete new file, we should see no files after cutoff now |
| 284 | EXPECT_TRUE(file_util::Delete(new_file_name, false)); |
| 285 | EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, test_start_time)); |
| 286 | } |
| 287 | |
| 288 | // Tests that the Delete function works as expected, especially |
| 289 | // the recursion flag. Also coincidentally tests PathExists. |
| 290 | TEST_F(FileUtilTest, Delete) { |
| 291 | // Create a file |
| 292 | std::wstring file_name = test_dir_; |
| 293 | file_util::AppendToPath(&file_name, L"Test File.txt"); |
| 294 | CreateTextFile(file_name, L"I'm cannon fodder."); |
| 295 | |
| 296 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 297 | |
| 298 | std::wstring subdir_path = test_dir_; |
| 299 | file_util::AppendToPath(&subdir_path, L"Subdirectory"); |
| 300 | CreateDirectory(subdir_path.c_str(), NULL); |
| 301 | |
| 302 | ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 303 | |
| 304 | std::wstring directory_contents = test_dir_; |
| 305 | file_util::AppendToPath(&directory_contents, L"*"); |
| 306 | |
| 307 | // Delete non-recursively and check that only the file is deleted |
| 308 | ASSERT_TRUE(file_util::Delete(directory_contents, false)); |
| 309 | ASSERT_FALSE(file_util::PathExists(file_name)); |
| 310 | ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 311 | |
| 312 | // Delete recursively and make sure all contents are deleted |
| 313 | ASSERT_TRUE(file_util::Delete(directory_contents, true)); |
| 314 | ASSERT_FALSE(file_util::PathExists(file_name)); |
| 315 | ASSERT_FALSE(file_util::PathExists(subdir_path)); |
| 316 | } |
| 317 | |
| 318 | TEST_F(FileUtilTest, Move) { |
| 319 | // Create a directory |
| 320 | std::wstring dir_name_from(test_dir_); |
| 321 | file_util::AppendToPath(&dir_name_from, L"Move_From_Subdir"); |
| 322 | CreateDirectory(dir_name_from.c_str(), NULL); |
| 323 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 324 | |
| 325 | // Create a file under the directory |
| 326 | std::wstring file_name_from(dir_name_from); |
| 327 | file_util::AppendToPath(&file_name_from, L"Move_Test_File.txt"); |
| 328 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 329 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 330 | |
| 331 | // Move the directory |
| 332 | std::wstring dir_name_to(test_dir_); |
| 333 | file_util::AppendToPath(&dir_name_to, L"Move_To_Subdir"); |
| 334 | std::wstring file_name_to(dir_name_to); |
| 335 | file_util::AppendToPath(&file_name_to, L"Move_Test_File.txt"); |
| 336 | |
| 337 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 338 | |
| 339 | EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to)); |
| 340 | |
| 341 | // Check everything has been moved. |
| 342 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 343 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 344 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 345 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 346 | } |
| 347 | |
| 348 | TEST_F(FileUtilTest, CopyDirectoryRecursively) { |
| 349 | // Create a directory. |
| 350 | std::wstring dir_name_from(test_dir_); |
| 351 | file_util::AppendToPath(&dir_name_from, L"Copy_From_Subdir"); |
| 352 | CreateDirectory(dir_name_from.c_str(), NULL); |
| 353 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 354 | |
| 355 | // Create a file under the directory. |
| 356 | std::wstring file_name_from(dir_name_from); |
| 357 | file_util::AppendToPath(&file_name_from, L"Copy_Test_File.txt"); |
| 358 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 359 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 360 | |
| 361 | // Create a subdirectory. |
| 362 | std::wstring subdir_name_from(dir_name_from); |
| 363 | file_util::AppendToPath(&subdir_name_from, L"Subdir"); |
| 364 | CreateDirectory(subdir_name_from.c_str(), NULL); |
| 365 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 366 | |
| 367 | // Create a file under the subdirectory. |
| 368 | std::wstring file_name2_from(subdir_name_from); |
| 369 | file_util::AppendToPath(&file_name2_from, L"Copy_Test_File.txt"); |
| 370 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 371 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 372 | |
| 373 | // Copy the directory recursively. |
| 374 | std::wstring dir_name_to(test_dir_); |
| 375 | file_util::AppendToPath(&dir_name_to, L"Copy_To_Subdir"); |
| 376 | std::wstring file_name_to(dir_name_to); |
| 377 | file_util::AppendToPath(&file_name_to, L"Copy_Test_File.txt"); |
| 378 | std::wstring subdir_name_to(dir_name_to); |
| 379 | file_util::AppendToPath(&subdir_name_to, L"Subdir"); |
| 380 | std::wstring file_name2_to(subdir_name_to); |
| 381 | file_util::AppendToPath(&file_name2_to, L"Copy_Test_File.txt"); |
| 382 | |
| 383 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 384 | |
| 385 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true)); |
| 386 | |
| 387 | // Check everything has been copied. |
| 388 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 389 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 390 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 391 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 392 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 393 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 394 | EXPECT_TRUE(file_util::PathExists(subdir_name_to)); |
| 395 | EXPECT_TRUE(file_util::PathExists(file_name2_to)); |
| 396 | } |
| 397 | |
| 398 | TEST_F(FileUtilTest, CopyDirectory) { |
| 399 | // Create a directory. |
| 400 | std::wstring dir_name_from(test_dir_); |
| 401 | file_util::AppendToPath(&dir_name_from, L"Copy_From_Subdir"); |
| 402 | CreateDirectory(dir_name_from.c_str(), NULL); |
| 403 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 404 | |
| 405 | // Create a file under the directory. |
| 406 | std::wstring file_name_from(dir_name_from); |
| 407 | file_util::AppendToPath(&file_name_from, L"Copy_Test_File.txt"); |
| 408 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 409 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 410 | |
| 411 | // Create a subdirectory. |
| 412 | std::wstring subdir_name_from(dir_name_from); |
| 413 | file_util::AppendToPath(&subdir_name_from, L"Subdir"); |
| 414 | CreateDirectory(subdir_name_from.c_str(), NULL); |
| 415 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 416 | |
| 417 | // Create a file under the subdirectory. |
| 418 | std::wstring file_name2_from(subdir_name_from); |
| 419 | file_util::AppendToPath(&file_name2_from, L"Copy_Test_File.txt"); |
| 420 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 421 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 422 | |
| 423 | // Copy the directory not recursively. |
| 424 | std::wstring dir_name_to(test_dir_); |
| 425 | file_util::AppendToPath(&dir_name_to, L"Copy_To_Subdir"); |
| 426 | std::wstring file_name_to(dir_name_to); |
| 427 | file_util::AppendToPath(&file_name_to, L"Copy_Test_File.txt"); |
| 428 | std::wstring subdir_name_to(dir_name_to); |
| 429 | file_util::AppendToPath(&subdir_name_to, L"Subdir"); |
| 430 | |
| 431 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 432 | |
| 433 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false)); |
| 434 | |
| 435 | // Check everything has been copied. |
| 436 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 437 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 438 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 439 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 440 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 441 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 442 | EXPECT_FALSE(file_util::PathExists(subdir_name_to)); |
| 443 | } |
| 444 | |
| 445 | TEST_F(FileUtilTest, CopyFile) { |
| 446 | // Create a directory |
| 447 | std::wstring dir_name_from(test_dir_); |
| 448 | file_util::AppendToPath(&dir_name_from, L"Copy_From_Subdir"); |
| 449 | CreateDirectory(dir_name_from.c_str(), NULL); |
| 450 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 451 | |
| 452 | // Create a file under the directory |
| 453 | std::wstring file_name_from(dir_name_from); |
| 454 | file_util::AppendToPath(&file_name_from, L"Copy_Test_File.txt"); |
| 455 | const std::wstring file_contents(L"Gooooooooooooooooooooogle"); |
| 456 | CreateTextFile(file_name_from, file_contents); |
| 457 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 458 | |
| 459 | // Copy the file. |
| 460 | std::wstring dest_file(dir_name_from); |
| 461 | file_util::AppendToPath(&dest_file, L"DestFile.txt"); |
| 462 | ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file)); |
| 463 | |
| 464 | // Check everything has been copied. |
| 465 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 466 | EXPECT_TRUE(file_util::PathExists(dest_file)); |
| 467 | const std::wstring read_contents = ReadTextFile(dest_file); |
| 468 | EXPECT_EQ(file_contents, read_contents); |
| 469 | } |
| 470 | |
| 471 | TEST_F(FileUtilTest, GetFileCreationLocalTime) { |
| 472 | std::wstring file_name = test_dir_; |
| 473 | file_util::AppendToPath(&file_name, L"Test File.txt"); |
| 474 | |
| 475 | SYSTEMTIME start_time; |
| 476 | GetLocalTime(&start_time); |
| 477 | Sleep(100); |
| 478 | CreateTextFile(file_name, L"New file!"); |
| 479 | Sleep(100); |
| 480 | SYSTEMTIME end_time; |
| 481 | GetLocalTime(&end_time); |
| 482 | |
| 483 | SYSTEMTIME file_creation_time; |
| 484 | file_util::GetFileCreationLocalTime(file_name, &file_creation_time); |
| 485 | |
| 486 | FILETIME start_filetime; |
| 487 | SystemTimeToFileTime(&start_time, &start_filetime); |
| 488 | FILETIME end_filetime; |
| 489 | SystemTimeToFileTime(&end_time, &end_filetime); |
| 490 | FILETIME file_creation_filetime; |
| 491 | SystemTimeToFileTime(&file_creation_time, &file_creation_filetime); |
| 492 | |
| 493 | EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) << |
| 494 | "start time: " << FileTimeAsUint64(start_filetime) << ", " << |
| 495 | "creation time: " << FileTimeAsUint64(file_creation_filetime); |
| 496 | |
| 497 | EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) << |
| 498 | "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " << |
| 499 | "end time: " << FileTimeAsUint64(end_filetime); |
| 500 | |
| 501 | ASSERT_TRUE(DeleteFile(file_name.c_str())); |
| 502 | } |
| 503 | |
| 504 | typedef testing::Test ReadOnlyFileUtilTest; |
| 505 | |
| 506 | TEST(ReadOnlyFileUtilTest, ContentsEqual) { |
| 507 | std::wstring data_dir; |
| 508 | ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
| 509 | file_util::AppendToPath(&data_dir, L"base"); |
| 510 | file_util::AppendToPath(&data_dir, L"data"); |
| 511 | file_util::AppendToPath(&data_dir, L"file_util_unittest"); |
| 512 | ASSERT_TRUE(file_util::PathExists(data_dir)); |
| 513 | |
| 514 | std::wstring original_file = data_dir; |
| 515 | file_util::AppendToPath(&original_file, L"original.txt"); |
| 516 | std::wstring same_file = data_dir; |
| 517 | file_util::AppendToPath(&same_file, L"same.txt"); |
| 518 | std::wstring same_length_file = data_dir; |
| 519 | file_util::AppendToPath(&same_length_file, L"same_length.txt"); |
| 520 | std::wstring different_file = data_dir; |
| 521 | file_util::AppendToPath(&different_file, L"different.txt"); |
| 522 | std::wstring different_first_file = data_dir; |
| 523 | file_util::AppendToPath(&different_first_file, L"different_first.txt"); |
| 524 | std::wstring different_last_file = data_dir; |
| 525 | file_util::AppendToPath(&different_last_file, L"different_last.txt"); |
| 526 | std::wstring empty1_file = data_dir; |
| 527 | file_util::AppendToPath(&empty1_file, L"empty1.txt"); |
| 528 | std::wstring empty2_file = data_dir; |
| 529 | file_util::AppendToPath(&empty2_file, L"empty2.txt"); |
| 530 | std::wstring shortened_file = data_dir; |
| 531 | file_util::AppendToPath(&shortened_file, L"shortened.txt"); |
| 532 | std::wstring binary_file = data_dir; |
| 533 | file_util::AppendToPath(&binary_file, L"binary_file.bin"); |
| 534 | std::wstring binary_file_same = data_dir; |
| 535 | file_util::AppendToPath(&binary_file_same, L"binary_file_same.bin"); |
| 536 | std::wstring binary_file_diff = data_dir; |
| 537 | file_util::AppendToPath(&binary_file_diff, L"binary_file_diff.bin"); |
| 538 | |
| 539 | EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file)); |
| 540 | EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file)); |
| 541 | EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file)); |
| 542 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file)); |
| 543 | EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname")); |
| 544 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file)); |
| 545 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file)); |
| 546 | EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file)); |
| 547 | EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file)); |
| 548 | EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file)); |
| 549 | EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same)); |
| 550 | EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff)); |
| 551 | } |
| 552 | |
| 553 | TEST_F(FileUtilTest, ResolveShortcutTest) { |
| 554 | std::wstring target_file = test_dir_; |
| 555 | file_util::AppendToPath(&target_file, L"Target.txt"); |
| 556 | CreateTextFile(target_file, L"This is the target."); |
| 557 | |
| 558 | std::wstring link_file = test_dir_; |
| 559 | file_util::AppendToPath(&link_file, L"Link.lnk"); |
| 560 | |
| 561 | HRESULT result; |
| 562 | IShellLink *shell = NULL; |
| 563 | IPersistFile *persist = NULL; |
| 564 | |
| 565 | CoInitialize(NULL); |
| 566 | // Temporarily create a shortcut for test |
| 567 | result = CoCreateInstance(CLSID_ShellLink, NULL, |
| 568 | CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 569 | reinterpret_cast<LPVOID*>(&shell)); |
| 570 | EXPECT_TRUE(SUCCEEDED(result)); |
| 571 | result = shell->QueryInterface(IID_IPersistFile, |
| 572 | reinterpret_cast<LPVOID*>(&persist)); |
| 573 | EXPECT_TRUE(SUCCEEDED(result)); |
| 574 | result = shell->SetPath(target_file.c_str()); |
| 575 | EXPECT_TRUE(SUCCEEDED(result)); |
| 576 | result = shell->SetDescription(L"ResolveShortcutTest"); |
| 577 | EXPECT_TRUE(SUCCEEDED(result)); |
| 578 | result = persist->Save(link_file.c_str(), TRUE); |
| 579 | EXPECT_TRUE(SUCCEEDED(result)); |
| 580 | if (persist) |
| 581 | persist->Release(); |
| 582 | if (shell) |
| 583 | shell->Release(); |
| 584 | |
| 585 | bool is_solved; |
| 586 | is_solved = file_util::ResolveShortcut(&link_file); |
| 587 | EXPECT_TRUE(is_solved); |
| 588 | std::wstring contents; |
| 589 | contents = ReadTextFile(link_file); |
| 590 | EXPECT_EQ(L"This is the target.", contents); |
| 591 | |
| 592 | // Cleanning |
| 593 | DeleteFile(target_file.c_str()); |
| 594 | DeleteFile(link_file.c_str()); |
| 595 | CoUninitialize(); |
| 596 | } |
| 597 | |
| 598 | TEST_F(FileUtilTest, CreateShortcutTest) { |
| 599 | const wchar_t file_contents[] = L"This is another target."; |
| 600 | std::wstring target_file = test_dir_; |
| 601 | file_util::AppendToPath(&target_file, L"Target1.txt"); |
| 602 | CreateTextFile(target_file, file_contents); |
| 603 | |
| 604 | std::wstring link_file = test_dir_; |
| 605 | file_util::AppendToPath(&link_file, L"Link1.lnk"); |
| 606 | |
| 607 | CoInitialize(NULL); |
| 608 | EXPECT_TRUE(file_util::CreateShortcutLink(target_file.c_str(), |
| 609 | link_file.c_str(), |
| 610 | NULL, NULL, NULL, NULL, 0)); |
| 611 | std::wstring resolved_name = link_file; |
| 612 | EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name)); |
| 613 | std::wstring read_contents = ReadTextFile(resolved_name); |
| 614 | EXPECT_EQ(file_contents, read_contents); |
| 615 | |
| 616 | DeleteFile(target_file.c_str()); |
| 617 | DeleteFile(link_file.c_str()); |
| 618 | CoUninitialize(); |
| 619 | } |
| 620 | |
| 621 | TEST_F(FileUtilTest, CreateTemporaryFileNameTest) { |
| 622 | std::wstring temp_file; |
| 623 | file_util::CreateTemporaryFileName(&temp_file); |
| 624 | EXPECT_EQ(file_util::PathExists(temp_file), true); |
| 625 | } |
| 626 | |
| 627 | TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { |
| 628 | std::wstring temp_dir; |
| 629 | file_util::CreateNewTempDirectory(std::wstring(), &temp_dir); |
| 630 | EXPECT_EQ(file_util::PathExists(temp_dir), true); |
| 631 | } |
| 632 | |
| 633 | TEST_F(FileUtilTest, CreateDirectoryTest) { |
| 634 | std::wstring test_root = test_dir_; |
| 635 | file_util::AppendToPath(&test_root, L"create_directory_test"); |
| 636 | std::wstring test_path(test_root); |
| 637 | file_util::AppendToPath(&test_path, L"dir\\tree\\likely\\doesnt\\exist\\"); |
| 638 | |
| 639 | EXPECT_EQ(file_util::PathExists(test_path), false); |
| 640 | EXPECT_EQ(file_util::CreateDirectory(test_path), true); |
| 641 | EXPECT_EQ(file_util::PathExists(test_path), true); |
| 642 | EXPECT_EQ(file_util::Delete(test_root, true), true); |
| 643 | EXPECT_EQ(file_util::PathExists(test_root), false); |
| 644 | EXPECT_EQ(file_util::PathExists(test_path), false); |
| 645 | } |
| 646 | |
| 647 | static const struct { |
| 648 | std::wstring bad_name; |
| 649 | std::wstring good_name; |
| 650 | } kIllegalCharacterCases[] = { |
| 651 | {L"bad*file:name?.jpg", L"bad-file-name-.jpg"}, |
| 652 | {L"**********::::.txt", L"--------------.txt"}, |
| 653 | {L"bad*file\\name.jpg", L"bad-file-name.jpg"}, |
| 654 | // We can't use UCNs (universal character names) for C0/C1 characters and |
| 655 | // U+007F, but \x escape is interpreted by MSVC and gcc as we intend. |
| 656 | {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"}, |
| 657 | {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"}, |
| 658 | {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"}, |
| 659 | {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"}, |
| 660 | {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"}, |
| 661 | {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"}, |
| 662 | {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"}, |
| 663 | // Unassigned codepoints are ok. |
| 664 | {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"}, |
| 665 | }; |
| 666 | |
| 667 | TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) { |
| 668 | for (int i = 0; i < arraysize(kIllegalCharacterCases); ++i) { |
| 669 | std::wstring bad_name(kIllegalCharacterCases[i].bad_name); |
| 670 | file_util::ReplaceIllegalCharacters(&bad_name, L'-'); |
| 671 | EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | static const struct ReplaceExtensionCase { |
| 676 | std::wstring file_name; |
| 677 | std::wstring extension; |
| 678 | std::wstring result; |
| 679 | } kReplaceExtension[] = { |
| 680 | {L"", L"", L""}, |
| 681 | {L"", L"txt", L".txt"}, |
| 682 | {L".", L"txt", L".txt"}, |
| 683 | {L".", L"", L""}, |
| 684 | {L"foo.dll", L"txt", L"foo.txt"}, |
| 685 | {L"foo.dll", L".txt", L"foo.txt"}, |
| 686 | {L"foo", L"txt", L"foo.txt"}, |
| 687 | {L"foo", L".txt", L"foo.txt"}, |
| 688 | {L"foo.baz.dll", L"txt", L"foo.baz.txt"}, |
| 689 | {L"foo.baz.dll", L".txt", L"foo.baz.txt"}, |
| 690 | {L"foo.dll", L"", L"foo"}, |
| 691 | {L"foo.dll", L".", L"foo"}, |
| 692 | {L"foo", L"", L"foo"}, |
| 693 | {L"foo", L".", L"foo"}, |
| 694 | {L"foo.baz.dll", L"", L"foo.baz"}, |
| 695 | {L"foo.baz.dll", L".", L"foo.baz"}, |
| 696 | }; |
| 697 | |
| 698 | TEST_F(FileUtilTest, ReplaceExtensionTest) { |
| 699 | for (int i = 0; i < arraysize(kReplaceExtension); ++i) { |
| 700 | std::wstring file_name(kReplaceExtension[i].file_name); |
| 701 | file_util::ReplaceExtension(&file_name, kReplaceExtension[i].extension); |
| 702 | EXPECT_EQ(file_name, kReplaceExtension[i].result); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | TEST_F(FileUtilTest, FileEnumeratorTest) { |
| 707 | // Test an empty directory. |
| 708 | file_util::FileEnumerator f0(test_dir_, true, |
| 709 | file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
| 710 | EXPECT_EQ(f0.Next(), L""); |
| 711 | EXPECT_EQ(f0.Next(), L""); |
| 712 | |
| 713 | // Populate the test dir. |
| 714 | file_util::CreateDirectory(test_dir_ + L"\\dir1"); |
| 715 | file_util::CreateDirectory(test_dir_ + L"\\dir2"); |
| 716 | CreateTextFile(test_dir_ + L"\\dir2\\dir2file.txt", L""); |
| 717 | file_util::CreateDirectory(test_dir_ + L"\\dir2\\inner"); |
| 718 | CreateTextFile(test_dir_ + L"\\dir2\\inner\\innerfile.txt", L""); |
| 719 | CreateTextFile(test_dir_ + L"\\file1.txt", L""); |
| 720 | CreateTextFile(test_dir_ + L"\\file2.txt", L""); |
| 721 | |
| 722 | // Only enumerate files. |
| 723 | file_util::FileEnumerator f1(test_dir_, true, |
| 724 | file_util::FileEnumerator::FILES); |
| 725 | FindResultCollector c1(f1); |
| 726 | EXPECT_TRUE(c1.HasFile(test_dir_ + L"\\file1.txt")); |
| 727 | EXPECT_TRUE(c1.HasFile(test_dir_ + L"\\file2.txt")); |
| 728 | EXPECT_TRUE(c1.HasFile(test_dir_ + L"\\dir2\\dir2file.txt")); |
| 729 | EXPECT_TRUE(c1.HasFile(test_dir_ + L"\\dir2\\inner\\innerfile.txt")); |
| 730 | |
| 731 | // Only enumerate directories. |
| 732 | file_util::FileEnumerator f2(test_dir_, true, |
| 733 | file_util::FileEnumerator::DIRECTORIES); |
| 734 | FindResultCollector c2(f2); |
| 735 | EXPECT_TRUE(c2.HasFile(test_dir_ + L"\\dir1")); |
| 736 | EXPECT_TRUE(c2.HasFile(test_dir_ + L"\\dir2")); |
| 737 | EXPECT_TRUE(c2.HasFile(test_dir_ + L"\\dir2\\inner")); |
| 738 | |
| 739 | // Enumerate files and directories. |
| 740 | file_util::FileEnumerator f3(test_dir_, true, |
| 741 | file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
| 742 | FindResultCollector c3(f3); |
| 743 | EXPECT_TRUE(c3.HasFile(test_dir_ + L"\\dir1")); |
| 744 | EXPECT_TRUE(c3.HasFile(test_dir_ + L"\\dir2")); |
| 745 | EXPECT_TRUE(c3.HasFile(test_dir_ + L"\\file1.txt")); |
| 746 | EXPECT_TRUE(c3.HasFile(test_dir_ + L"\\file2.txt")); |
| 747 | EXPECT_TRUE(c3.HasFile(test_dir_ + L"\\dir2\\dir2file.txt")); |
| 748 | EXPECT_TRUE(c3.HasFile(test_dir_ + L"\\dir2\\dir2file.txt")); |
| 749 | EXPECT_TRUE(c3.HasFile(test_dir_ + L"\\dir2\\inner")); |
| 750 | EXPECT_TRUE(c3.HasFile(test_dir_ + L"\\dir2\\inner\\innerfile.txt")); |
| 751 | |
| 752 | // Non-recursive operation. |
| 753 | file_util::FileEnumerator f4(test_dir_, false, |
| 754 | file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
| 755 | FindResultCollector c4(f4); |
| 756 | EXPECT_TRUE(c4.HasFile(test_dir_ + L"\\dir1")); |
| 757 | EXPECT_TRUE(c4.HasFile(test_dir_ + L"\\dir2")); |
| 758 | EXPECT_TRUE(c4.HasFile(test_dir_ + L"\\file1.txt")); |
| 759 | EXPECT_TRUE(c4.HasFile(test_dir_ + L"\\file2.txt")); |
| 760 | |
| 761 | // Enumerate with a pattern. |
| 762 | file_util::FileEnumerator f5(test_dir_, true, |
| 763 | file_util::FileEnumerator::FILES_AND_DIRECTORIES, L"dir*"); |
| 764 | FindResultCollector c5(f5); |
| 765 | EXPECT_TRUE(c5.HasFile(test_dir_ + L"\\dir1")); |
| 766 | EXPECT_TRUE(c5.HasFile(test_dir_ + L"\\dir2")); |
| 767 | EXPECT_TRUE(c5.HasFile(test_dir_ + L"\\dir2\\dir2file.txt")); |
| 768 | EXPECT_TRUE(c5.HasFile(test_dir_ + L"\\dir2\\inner")); |
| 769 | EXPECT_TRUE(c5.HasFile(test_dir_ + L"\\dir2\\inner\\innerfile.txt")); |
| 770 | |
| 771 | // Make sure the destructor closes the find handle while in the middle of a |
| 772 | // query to allow TearDown to delete the directory. |
| 773 | file_util::FileEnumerator f6(test_dir_, true, |
| 774 | file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
| 775 | EXPECT_FALSE(f6.Next().empty()); // Should have found something |
| 776 | // (we don't care what). |
| 777 | } |