license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | f35d39b | 2008-08-15 17:50:10 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 7 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | #include <windows.h> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include <shellapi.h> |
| 10 | #include <shlobj.h> |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 11 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | |
| 13 | #include <fstream> |
| 14 | #include <iostream> |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 15 | #include <set> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | |
| 17 | #include "base/base_paths.h" |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 18 | #include "base/file_path.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | #include "base/file_util.h" |
| 20 | #include "base/logging.h" |
| 21 | #include "base/path_service.h" |
| 22 | #include "base/string_util.h" |
| 23 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 24 | #include "testing/platform_test.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 28 | // file_util winds up using autoreleased objects on the Mac, so this needs |
| 29 | // to be a PlatformTest |
| 30 | class FileUtilTest : public PlatformTest { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 31 | protected: |
| 32 | virtual void SetUp() { |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 33 | PlatformTest::SetUp(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 34 | // Name a subdirectory of the temp directory. |
| 35 | ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 36 | test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 37 | |
| 38 | // Create a fresh, empty copy of this directory. |
| 39 | file_util::Delete(test_dir_, true); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 40 | file_util::CreateDirectory(test_dir_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 41 | } |
| 42 | virtual void TearDown() { |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 43 | PlatformTest::TearDown(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 44 | // Clean up test directory |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 45 | ASSERT_TRUE(file_util::Delete(test_dir_, true)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 46 | ASSERT_FALSE(file_util::PathExists(test_dir_)); |
| 47 | } |
| 48 | |
| 49 | // the path to temporary directory used to contain the test operations |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 50 | FilePath test_dir_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | // Collects all the results from the given file enumerator, and provides an |
| 54 | // interface to query whether a given file is present. |
| 55 | class FindResultCollector { |
| 56 | public: |
| 57 | FindResultCollector(file_util::FileEnumerator& enumerator) { |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 58 | FilePath cur_file; |
| 59 | while (!(cur_file = enumerator.Next()).value().empty()) { |
| 60 | FilePath::StringType path = cur_file.value(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 61 | // The file should not be returned twice. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 62 | EXPECT_TRUE(files_.end() == files_.find(path)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 63 | << "Same file returned twice"; |
| 64 | |
| 65 | // Save for later. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 66 | files_.insert(path); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | // Returns true if the enumerator found the file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 71 | bool HasFile(const FilePath& file) const { |
| 72 | return files_.find(file.value()) != files_.end(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 73 | } |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 74 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 75 | int size() { |
[email protected] | f35d39b | 2008-08-15 17:50:10 | [diff] [blame] | 76 | return static_cast<int>(files_.size()); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 77 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 78 | |
| 79 | private: |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 80 | std::set<FilePath::StringType> files_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | // Simple function to dump some text into a new file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 84 | void CreateTextFile(const FilePath& filename, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 85 | const std::wstring& contents) { |
| 86 | std::ofstream file; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 87 | file.open(WideToUTF8(filename.ToWStringHack()).c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 88 | ASSERT_TRUE(file.is_open()); |
| 89 | file << contents; |
| 90 | file.close(); |
| 91 | } |
| 92 | |
| 93 | // Simple function to take out some text from a file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 94 | std::wstring ReadTextFile(const FilePath& filename) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 95 | wchar_t contents[64]; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 96 | std::wifstream file; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 97 | file.open(WideToUTF8(filename.ToWStringHack()).c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 98 | EXPECT_TRUE(file.is_open()); |
| 99 | file.getline(contents, 64); |
| 100 | file.close(); |
| 101 | return std::wstring(contents); |
| 102 | } |
| 103 | |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 104 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | uint64 FileTimeAsUint64(const FILETIME& ft) { |
| 106 | ULARGE_INTEGER u; |
| 107 | u.LowPart = ft.dwLowDateTime; |
| 108 | u.HighPart = ft.dwHighDateTime; |
| 109 | return u.QuadPart; |
| 110 | } |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 111 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 112 | |
| 113 | const struct append_case { |
| 114 | const wchar_t* path; |
| 115 | const wchar_t* ending; |
| 116 | const wchar_t* result; |
| 117 | } append_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 118 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 119 | {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"}, |
| 120 | {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"}, |
| 121 | {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"}, |
| 122 | {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"}, |
| 123 | {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"}, |
| 124 | {L"", L"path", L"\\path"}, |
| 125 | {L"", L"", L"\\"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 126 | #elif defined(OS_POSIX) |
| 127 | {L"/foo/bar", L"path", L"/foo/bar/path"}, |
| 128 | {L"/foo/bar/", L"path", L"/foo/bar/path"}, |
| 129 | {L"/foo/bar//", L"path", L"/foo/bar//path"}, |
| 130 | {L"/foo/bar/", L"", L"/foo/bar/"}, |
| 131 | {L"/foo/bar", L"", L"/foo/bar/"}, |
| 132 | {L"", L"path", L"/path"}, |
| 133 | {L"", L"", L"/"}, |
| 134 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 135 | }; |
| 136 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 137 | TEST_F(FileUtilTest, AppendToPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 138 | for (unsigned int i = 0; i < arraysize(append_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 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; |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 152 | FilePath::StringType suffix; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 153 | std::wstring result; |
| 154 | } kInsertBeforeExtension[] = { |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 155 | {L"", FILE_PATH_LITERAL(""), L""}, |
| 156 | {L"", FILE_PATH_LITERAL("txt"), L"txt"}, |
| 157 | {L".", FILE_PATH_LITERAL("txt"), L"txt."}, |
| 158 | {L".", FILE_PATH_LITERAL(""), L"."}, |
| 159 | {L"foo.dll", FILE_PATH_LITERAL("txt"), L"footxt.dll"}, |
| 160 | {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt.dll"}, |
| 161 | {L"foo", FILE_PATH_LITERAL("txt"), L"footxt"}, |
| 162 | {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"}, |
| 163 | {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baztxt.dll"}, |
| 164 | {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt.dll"}, |
| 165 | {L"foo.dll", FILE_PATH_LITERAL(""), L"foo.dll"}, |
| 166 | {L"foo.dll", FILE_PATH_LITERAL("."), L"foo..dll"}, |
| 167 | {L"foo", FILE_PATH_LITERAL(""), L"foo"}, |
| 168 | {L"foo", FILE_PATH_LITERAL("."), L"foo."}, |
| 169 | {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz.dll"}, |
| 170 | {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz..dll"}, |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 171 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 172 | {L"\\", L"", L"\\"}, |
| 173 | {L"\\", L"txt", L"\\txt"}, |
| 174 | {L"\\.", L"txt", L"\\txt."}, |
| 175 | {L"\\.", L"", L"\\."}, |
| 176 | {L"C:\\bar\\foo.dll", L"txt", L"C:\\bar\\footxt.dll"}, |
| 177 | {L"C:\\bar.baz\\foodll", L"txt", L"C:\\bar.baz\\foodlltxt"}, |
| 178 | {L"C:\\bar.baz\\foo.dll", L"txt", L"C:\\bar.baz\\footxt.dll"}, |
| 179 | {L"C:\\bar.baz\\foo.dll.exe", L"txt", L"C:\\bar.baz\\foo.dlltxt.exe"}, |
| 180 | {L"C:\\bar.baz\\foo", L"", L"C:\\bar.baz\\foo"}, |
| 181 | {L"C:\\bar.baz\\foo.exe", L"", L"C:\\bar.baz\\foo.exe"}, |
| 182 | {L"C:\\bar.baz\\foo.dll.exe", L"", L"C:\\bar.baz\\foo.dll.exe"}, |
| 183 | {L"C:\\bar\\baz\\foo.exe", L" (1)", L"C:\\bar\\baz\\foo (1).exe"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 184 | #elif defined(OS_POSIX) |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 185 | {L"/", "", L"/"}, |
| 186 | {L"/", "txt", L"/txt"}, |
| 187 | {L"/.", "txt", L"/txt."}, |
| 188 | {L"/.", "", L"/."}, |
| 189 | {L"/bar/foo.dll", "txt", L"/bar/footxt.dll"}, |
| 190 | {L"/bar.baz/foodll", "txt", L"/bar.baz/foodlltxt"}, |
| 191 | {L"/bar.baz/foo.dll", "txt", L"/bar.baz/footxt.dll"}, |
| 192 | {L"/bar.baz/foo.dll.exe", "txt", L"/bar.baz/foo.dlltxt.exe"}, |
| 193 | {L"/bar.baz/foo", "", L"/bar.baz/foo"}, |
| 194 | {L"/bar.baz/foo.exe", "", L"/bar.baz/foo.exe"}, |
| 195 | {L"/bar.baz/foo.dll.exe", "", L"/bar.baz/foo.dll.exe"}, |
| 196 | {L"/bar/baz/foo.exe", " (1)", L"/bar/baz/foo (1).exe"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 197 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | TEST_F(FileUtilTest, InsertBeforeExtensionTest) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 201 | for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) { |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 202 | FilePath path = FilePath::FromWStringHack(kInsertBeforeExtension[i].path); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 203 | file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix); |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 204 | EXPECT_EQ(kInsertBeforeExtension[i].result, path.ToWStringHack()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | static const struct filename_case { |
| 209 | const wchar_t* path; |
| 210 | const wchar_t* filename; |
| 211 | } filename_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 212 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 213 | {L"c:\\colon\\backslash", L"backslash"}, |
| 214 | {L"c:\\colon\\backslash\\", L""}, |
| 215 | {L"\\\\filename.exe", L"filename.exe"}, |
| 216 | {L"filename.exe", L"filename.exe"}, |
| 217 | {L"", L""}, |
| 218 | {L"\\\\\\", L""}, |
| 219 | {L"c:/colon/backslash", L"backslash"}, |
| 220 | {L"c:/colon/backslash/", L""}, |
| 221 | {L"//////", L""}, |
| 222 | {L"///filename.exe", L"filename.exe"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 223 | #elif defined(OS_POSIX) |
| 224 | {L"/foo/bar", L"bar"}, |
| 225 | {L"/foo/bar/", L""}, |
| 226 | {L"/filename.exe", L"filename.exe"}, |
| 227 | {L"filename.exe", L"filename.exe"}, |
| 228 | {L"", L""}, |
| 229 | {L"/", L""}, |
| 230 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | TEST_F(FileUtilTest, GetFilenameFromPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 234 | for (unsigned int i = 0; i < arraysize(filename_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 235 | const filename_case& value = filename_cases[i]; |
| 236 | std::wstring result = file_util::GetFilenameFromPath(value.path); |
| 237 | EXPECT_EQ(value.filename, result); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Test finding the file type from a path name |
| 242 | static const struct extension_case { |
| 243 | const wchar_t* path; |
| 244 | const wchar_t* extension; |
| 245 | } extension_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 246 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 247 | {L"C:\\colon\\backslash\\filename.extension", L"extension"}, |
| 248 | {L"C:\\colon\\backslash\\filename.", L""}, |
| 249 | {L"C:\\colon\\backslash\\filename", L""}, |
| 250 | {L"C:\\colon\\backslash\\", L""}, |
| 251 | {L"C:\\colon\\backslash.\\", L""}, |
| 252 | {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 253 | #elif defined(OS_POSIX) |
| 254 | {L"/foo/bar/filename.extension", L"extension"}, |
| 255 | {L"/foo/bar/filename.", L""}, |
| 256 | {L"/foo/bar/filename", L""}, |
| 257 | {L"/foo/bar/", L""}, |
| 258 | {L"/foo/bar./", L""}, |
| 259 | {L"/foo/bar/filename.extension.extension2", L"extension2"}, |
| 260 | {L".", L""}, |
| 261 | {L"..", L""}, |
| 262 | {L"./foo", L""}, |
| 263 | {L"./foo.extension", L"extension"}, |
| 264 | {L"/foo.extension1/bar.extension2", L"extension2"}, |
| 265 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | TEST_F(FileUtilTest, GetFileExtensionFromPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 269 | for (unsigned int i = 0; i < arraysize(extension_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 270 | const extension_case& ext = extension_cases[i]; |
| 271 | const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path); |
| 272 | EXPECT_EQ(ext.extension, fext); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Test finding the directory component of a path |
| 277 | static const struct dir_case { |
| 278 | const wchar_t* full_path; |
| 279 | const wchar_t* directory; |
| 280 | } dir_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 281 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 282 | {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"}, |
| 283 | {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"}, |
| 284 | {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"}, |
| 285 | {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"}, |
| 286 | {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"}, |
| 287 | {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."}, |
| 288 | {L"C:\\", L"C:"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 289 | #elif defined(OS_POSIX) |
| 290 | {L"/foo/bar/gdi32.dll", L"/foo/bar"}, |
| 291 | {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"}, |
| 292 | {L"/foo/bar/", L"/foo/bar"}, |
| 293 | {L"/foo/bar//", L"/foo/bar"}, |
| 294 | {L"/foo/bar", L"/foo"}, |
| 295 | {L"/foo/bar./", L"/foo/bar."}, |
| 296 | {L"/", L"/"}, |
| 297 | {L".", L"."}, |
| 298 | {L"..", L"."}, // yes, ".." technically lives in "." |
| 299 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | TEST_F(FileUtilTest, GetDirectoryFromPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 303 | for (unsigned int i = 0; i < arraysize(dir_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 304 | const dir_case& dir = dir_cases[i]; |
| 305 | const std::wstring parent = |
| 306 | file_util::GetDirectoryFromPath(dir.full_path); |
| 307 | EXPECT_EQ(dir.directory, parent); |
| 308 | } |
| 309 | } |
| 310 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 311 | // TODO(erikkay): implement |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 312 | #if defined OS_WIN |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 313 | TEST_F(FileUtilTest, CountFilesCreatedAfter) { |
| 314 | // Create old file (that we don't want to count) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 315 | FilePath old_file_name = test_dir_.Append(L"Old File.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 316 | CreateTextFile(old_file_name, L"Just call me Mr. Creakybits"); |
| 317 | |
| 318 | // Age to perfection |
| 319 | Sleep(100); |
| 320 | |
| 321 | // Establish our cutoff time |
| 322 | FILETIME test_start_time; |
| 323 | GetSystemTimeAsFileTime(&test_start_time); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 324 | EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(), |
| 325 | test_start_time)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 326 | |
| 327 | // Create a new file (that we do want to count) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 328 | FilePath new_file_name = test_dir_.Append(L"New File.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 329 | CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah."); |
| 330 | |
| 331 | // We should see only the new file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 332 | EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_.value(), |
| 333 | test_start_time)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 334 | |
| 335 | // Delete new file, we should see no files after cutoff now |
| 336 | EXPECT_TRUE(file_util::Delete(new_file_name, false)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 337 | EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(), |
| 338 | test_start_time)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 339 | } |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 340 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 341 | |
| 342 | // Tests that the Delete function works as expected, especially |
| 343 | // the recursion flag. Also coincidentally tests PathExists. |
| 344 | TEST_F(FileUtilTest, Delete) { |
| 345 | // Create a file |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 346 | FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 347 | CreateTextFile(file_name, L"I'm cannon fodder."); |
| 348 | |
| 349 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 350 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 351 | FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory")); |
| 352 | file_util::CreateDirectory(subdir_path); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 353 | |
| 354 | ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 355 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 356 | FilePath directory_contents = test_dir_; |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 357 | #if defined(OS_WIN) |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 358 | // TODO(erikkay): see if anyone's actually using this feature of the API |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 359 | directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 360 | // Delete non-recursively and check that only the file is deleted |
| 361 | ASSERT_TRUE(file_util::Delete(directory_contents, false)); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 362 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 363 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 364 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 365 | |
| 366 | // Delete recursively and make sure all contents are deleted |
| 367 | ASSERT_TRUE(file_util::Delete(directory_contents, true)); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 368 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 369 | EXPECT_FALSE(file_util::PathExists(subdir_path)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | TEST_F(FileUtilTest, Move) { |
| 373 | // Create a directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 374 | FilePath dir_name_from = |
| 375 | test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir")); |
| 376 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 377 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 378 | |
| 379 | // Create a file under the directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 380 | FilePath file_name_from = |
| 381 | dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 382 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 383 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 384 | |
| 385 | // Move the directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 386 | FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir")); |
| 387 | FilePath file_name_to = |
| 388 | dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 389 | |
| 390 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 391 | |
| 392 | EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to)); |
| 393 | |
| 394 | // Check everything has been moved. |
| 395 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 396 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 397 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 398 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 399 | } |
| 400 | |
| 401 | TEST_F(FileUtilTest, CopyDirectoryRecursively) { |
| 402 | // Create a directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 403 | FilePath dir_name_from = |
| 404 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 405 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 406 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 407 | |
| 408 | // Create a file under the directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 409 | FilePath file_name_from = |
| 410 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 411 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 412 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 413 | |
| 414 | // Create a subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 415 | FilePath subdir_name_from = |
| 416 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 417 | file_util::CreateDirectory(subdir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 418 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 419 | |
| 420 | // Create a file under the subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 421 | FilePath file_name2_from = |
| 422 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 423 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 424 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 425 | |
| 426 | // Copy the directory recursively. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 427 | FilePath dir_name_to = |
| 428 | test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
| 429 | FilePath file_name_to = |
| 430 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 431 | FilePath subdir_name_to = |
| 432 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 433 | FilePath file_name2_to = |
| 434 | subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 435 | |
| 436 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 437 | |
| 438 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true)); |
| 439 | |
| 440 | // Check everything has been copied. |
| 441 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 442 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 443 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 444 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 445 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 446 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 447 | EXPECT_TRUE(file_util::PathExists(subdir_name_to)); |
| 448 | EXPECT_TRUE(file_util::PathExists(file_name2_to)); |
| 449 | } |
| 450 | |
| 451 | TEST_F(FileUtilTest, CopyDirectory) { |
| 452 | // Create a directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 453 | FilePath dir_name_from = |
| 454 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 455 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 456 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 457 | |
| 458 | // Create a file under the directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 459 | FilePath file_name_from = |
| 460 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 461 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 462 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 463 | |
| 464 | // Create a subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 465 | FilePath subdir_name_from = |
| 466 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 467 | file_util::CreateDirectory(subdir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 468 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 469 | |
| 470 | // Create a file under the subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 471 | FilePath file_name2_from = |
| 472 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 473 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 474 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 475 | |
| 476 | // Copy the directory not recursively. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 477 | FilePath dir_name_to = |
| 478 | test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
| 479 | FilePath file_name_to = |
| 480 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 481 | FilePath subdir_name_to = |
| 482 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 483 | |
| 484 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 485 | |
| 486 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false)); |
| 487 | |
| 488 | // Check everything has been copied. |
| 489 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 490 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 491 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 492 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 493 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 494 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 495 | EXPECT_FALSE(file_util::PathExists(subdir_name_to)); |
| 496 | } |
| 497 | |
| 498 | TEST_F(FileUtilTest, CopyFile) { |
| 499 | // Create a directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 500 | FilePath dir_name_from = |
| 501 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 502 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 503 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 504 | |
| 505 | // Create a file under the directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 506 | FilePath file_name_from = |
| 507 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 508 | const std::wstring file_contents(L"Gooooooooooooooooooooogle"); |
| 509 | CreateTextFile(file_name_from, file_contents); |
| 510 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 511 | |
| 512 | // Copy the file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 513 | FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 514 | ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file)); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 515 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 516 | // Copy the file to another location using '..' in the path. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 517 | std::wstring dest_file2(dir_name_from.ToWStringHack()); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 518 | file_util::AppendToPath(&dest_file2, L".."); |
| 519 | file_util::AppendToPath(&dest_file2, L"DestFile.txt"); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 520 | ASSERT_TRUE(file_util::CopyFile(file_name_from, |
| 521 | FilePath::FromWStringHack(dest_file2))); |
| 522 | std::wstring dest_file2_test(dir_name_from.ToWStringHack()); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 523 | file_util::UpOneDirectory(&dest_file2_test); |
| 524 | file_util::AppendToPath(&dest_file2_test, L"DestFile.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 525 | |
| 526 | // Check everything has been copied. |
| 527 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 528 | EXPECT_TRUE(file_util::PathExists(dest_file)); |
| 529 | const std::wstring read_contents = ReadTextFile(dest_file); |
| 530 | EXPECT_EQ(file_contents, read_contents); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 531 | EXPECT_TRUE(file_util::PathExists( |
| 532 | FilePath::FromWStringHack(dest_file2_test))); |
| 533 | EXPECT_TRUE(file_util::PathExists(FilePath::FromWStringHack(dest_file2))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 534 | } |
| 535 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 536 | // TODO(erikkay): implement |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 537 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 538 | TEST_F(FileUtilTest, GetFileCreationLocalTime) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 539 | FilePath file_name = test_dir_.Append(L"Test File.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 540 | |
| 541 | SYSTEMTIME start_time; |
| 542 | GetLocalTime(&start_time); |
| 543 | Sleep(100); |
| 544 | CreateTextFile(file_name, L"New file!"); |
| 545 | Sleep(100); |
| 546 | SYSTEMTIME end_time; |
| 547 | GetLocalTime(&end_time); |
| 548 | |
| 549 | SYSTEMTIME file_creation_time; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 550 | file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 551 | |
| 552 | FILETIME start_filetime; |
| 553 | SystemTimeToFileTime(&start_time, &start_filetime); |
| 554 | FILETIME end_filetime; |
| 555 | SystemTimeToFileTime(&end_time, &end_filetime); |
| 556 | FILETIME file_creation_filetime; |
| 557 | SystemTimeToFileTime(&file_creation_time, &file_creation_filetime); |
| 558 | |
| 559 | EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) << |
| 560 | "start time: " << FileTimeAsUint64(start_filetime) << ", " << |
| 561 | "creation time: " << FileTimeAsUint64(file_creation_filetime); |
| 562 | |
| 563 | EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) << |
| 564 | "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " << |
| 565 | "end time: " << FileTimeAsUint64(end_filetime); |
| 566 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 567 | ASSERT_TRUE(DeleteFile(file_name.value().c_str())); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 568 | } |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 569 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 570 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 571 | // file_util winds up using autoreleased objects on the Mac, so this needs |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 572 | // to be a PlatformTest. |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 573 | typedef PlatformTest ReadOnlyFileUtilTest; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 574 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 575 | TEST_F(ReadOnlyFileUtilTest, ContentsEqual) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 576 | FilePath data_dir; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 577 | ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 578 | data_dir = data_dir.Append(FILE_PATH_LITERAL("base")) |
| 579 | .Append(FILE_PATH_LITERAL("data")) |
| 580 | .Append(FILE_PATH_LITERAL("file_util_unittest")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 581 | ASSERT_TRUE(file_util::PathExists(data_dir)); |
| 582 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 583 | FilePath original_file = |
| 584 | data_dir.Append(FILE_PATH_LITERAL("original.txt")); |
| 585 | FilePath same_file = |
| 586 | data_dir.Append(FILE_PATH_LITERAL("same.txt")); |
| 587 | FilePath same_length_file = |
| 588 | data_dir.Append(FILE_PATH_LITERAL("same_length.txt")); |
| 589 | FilePath different_file = |
| 590 | data_dir.Append(FILE_PATH_LITERAL("different.txt")); |
| 591 | FilePath different_first_file = |
| 592 | data_dir.Append(FILE_PATH_LITERAL("different_first.txt")); |
| 593 | FilePath different_last_file = |
| 594 | data_dir.Append(FILE_PATH_LITERAL("different_last.txt")); |
| 595 | FilePath empty1_file = |
| 596 | data_dir.Append(FILE_PATH_LITERAL("empty1.txt")); |
| 597 | FilePath empty2_file = |
| 598 | data_dir.Append(FILE_PATH_LITERAL("empty2.txt")); |
| 599 | FilePath shortened_file = |
| 600 | data_dir.Append(FILE_PATH_LITERAL("shortened.txt")); |
| 601 | FilePath binary_file = |
| 602 | data_dir.Append(FILE_PATH_LITERAL("binary_file.bin")); |
| 603 | FilePath binary_file_same = |
| 604 | data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin")); |
| 605 | FilePath binary_file_diff = |
| 606 | data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 607 | |
| 608 | EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file)); |
| 609 | EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file)); |
| 610 | EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file)); |
| 611 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file)); |
| 612 | EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname")); |
| 613 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file)); |
| 614 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file)); |
| 615 | EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file)); |
| 616 | EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file)); |
| 617 | EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file)); |
| 618 | EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same)); |
| 619 | EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff)); |
| 620 | } |
| 621 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 622 | // We don't need equivalent functionality outside of Windows. |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 623 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 624 | TEST_F(FileUtilTest, ResolveShortcutTest) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 625 | FilePath target_file = test_dir_.Append(L"Target.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 626 | CreateTextFile(target_file, L"This is the target."); |
| 627 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 628 | FilePath link_file = test_dir_.Append(L"Link.lnk"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 629 | |
| 630 | HRESULT result; |
| 631 | IShellLink *shell = NULL; |
| 632 | IPersistFile *persist = NULL; |
| 633 | |
| 634 | CoInitialize(NULL); |
| 635 | // Temporarily create a shortcut for test |
| 636 | result = CoCreateInstance(CLSID_ShellLink, NULL, |
| 637 | CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 638 | reinterpret_cast<LPVOID*>(&shell)); |
| 639 | EXPECT_TRUE(SUCCEEDED(result)); |
| 640 | result = shell->QueryInterface(IID_IPersistFile, |
| 641 | reinterpret_cast<LPVOID*>(&persist)); |
| 642 | EXPECT_TRUE(SUCCEEDED(result)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 643 | result = shell->SetPath(target_file.value().c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 644 | EXPECT_TRUE(SUCCEEDED(result)); |
| 645 | result = shell->SetDescription(L"ResolveShortcutTest"); |
| 646 | EXPECT_TRUE(SUCCEEDED(result)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 647 | result = persist->Save(link_file.value().c_str(), TRUE); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 648 | EXPECT_TRUE(SUCCEEDED(result)); |
| 649 | if (persist) |
| 650 | persist->Release(); |
| 651 | if (shell) |
| 652 | shell->Release(); |
| 653 | |
| 654 | bool is_solved; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 655 | std::wstring link_file_str = link_file.value(); |
| 656 | is_solved = file_util::ResolveShortcut(&link_file_str); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 657 | EXPECT_TRUE(is_solved); |
| 658 | std::wstring contents; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 659 | contents = ReadTextFile(FilePath(link_file_str)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 660 | EXPECT_EQ(L"This is the target.", contents); |
| 661 | |
[email protected] | d324ab33 | 2008-08-18 16:00:38 | [diff] [blame] | 662 | // Cleaning |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 663 | DeleteFile(target_file.value().c_str()); |
| 664 | DeleteFile(link_file_str.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 665 | CoUninitialize(); |
| 666 | } |
| 667 | |
| 668 | TEST_F(FileUtilTest, CreateShortcutTest) { |
| 669 | const wchar_t file_contents[] = L"This is another target."; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 670 | FilePath target_file = test_dir_.Append(L"Target1.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 671 | CreateTextFile(target_file, file_contents); |
| 672 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 673 | FilePath link_file = test_dir_.Append(L"Link1.lnk"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 674 | |
| 675 | CoInitialize(NULL); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 676 | EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(), |
| 677 | link_file.value().c_str(), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 678 | NULL, NULL, NULL, NULL, 0)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 679 | std::wstring resolved_name = link_file.value(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 680 | EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 681 | std::wstring read_contents = ReadTextFile(FilePath(resolved_name)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 682 | EXPECT_EQ(file_contents, read_contents); |
| 683 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 684 | DeleteFile(target_file.value().c_str()); |
| 685 | DeleteFile(link_file.value().c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 686 | CoUninitialize(); |
| 687 | } |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 688 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 689 | |
| 690 | TEST_F(FileUtilTest, CreateTemporaryFileNameTest) { |
| 691 | std::wstring temp_file; |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 692 | ASSERT_TRUE(file_util::CreateTemporaryFileName(&temp_file)); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 693 | EXPECT_TRUE(file_util::PathExists(temp_file)); |
| 694 | EXPECT_TRUE(file_util::Delete(temp_file, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { |
| 698 | std::wstring temp_dir; |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 699 | ASSERT_TRUE(file_util::CreateNewTempDirectory(std::wstring(), &temp_dir)); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 700 | EXPECT_TRUE(file_util::PathExists(temp_dir)); |
| 701 | EXPECT_TRUE(file_util::Delete(temp_dir, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | TEST_F(FileUtilTest, CreateDirectoryTest) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 705 | FilePath test_root = |
| 706 | test_dir_.Append(FILE_PATH_LITERAL("create_directory_test")); |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 707 | #if defined(OS_WIN) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 708 | FilePath test_path = |
| 709 | test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 710 | #elif defined(OS_POSIX) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 711 | FilePath test_path = |
| 712 | test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 713 | #endif |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 714 | |
| 715 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 716 | EXPECT_TRUE(file_util::CreateDirectory(test_path)); |
| 717 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 718 | // CreateDirectory returns true if the DirectoryExists returns true. |
| 719 | EXPECT_TRUE(file_util::CreateDirectory(test_path)); |
| 720 | |
| 721 | // Doesn't work to create it on top of a non-dir |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 722 | test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt")); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 723 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 724 | CreateTextFile(test_path, L"test file"); |
| 725 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 726 | EXPECT_FALSE(file_util::CreateDirectory(test_path)); |
| 727 | |
| 728 | EXPECT_TRUE(file_util::Delete(test_root, true)); |
| 729 | EXPECT_FALSE(file_util::PathExists(test_root)); |
| 730 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 731 | } |
| 732 | |
| 733 | TEST_F(FileUtilTest, DetectDirectoryTest) { |
| 734 | // Check a directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 735 | FilePath test_root = |
| 736 | test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test")); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 737 | EXPECT_FALSE(file_util::PathExists(test_root)); |
| 738 | EXPECT_TRUE(file_util::CreateDirectory(test_root)); |
| 739 | EXPECT_TRUE(file_util::PathExists(test_root)); |
| 740 | EXPECT_TRUE(file_util::DirectoryExists(test_root)); |
| 741 | |
| 742 | // Check a file |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 743 | FilePath test_path = |
| 744 | test_root.Append(FILE_PATH_LITERAL("foobar.txt")); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 745 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 746 | CreateTextFile(test_path, L"test file"); |
| 747 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 748 | EXPECT_FALSE(file_util::DirectoryExists(test_path)); |
| 749 | EXPECT_TRUE(file_util::Delete(test_path, false)); |
| 750 | |
| 751 | EXPECT_TRUE(file_util::Delete(test_root, true)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 752 | } |
| 753 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 754 | static const struct goodbad_pair { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 755 | std::wstring bad_name; |
| 756 | std::wstring good_name; |
| 757 | } kIllegalCharacterCases[] = { |
| 758 | {L"bad*file:name?.jpg", L"bad-file-name-.jpg"}, |
| 759 | {L"**********::::.txt", L"--------------.txt"}, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 760 | // We can't use UCNs (universal character names) for C0/C1 characters and |
| 761 | // U+007F, but \x escape is interpreted by MSVC and gcc as we intend. |
| 762 | {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"}, |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 763 | #if defined(OS_WIN) |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 764 | {L"bad*file\\name.jpg", L"bad-file-name.jpg"}, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 765 | {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"}, |
| 766 | {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 767 | #elif defined(OS_POSIX) |
| 768 | {L"bad*file?name.jpg", L"bad-file-name.jpg"}, |
| 769 | {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"}, |
| 770 | {L"bad\uFFFFfile-name.jpg ", L"bad-file-name.jpg"}, |
| 771 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 772 | {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"}, |
| 773 | {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"}, |
| 774 | {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"}, |
| 775 | {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"}, |
| 776 | // Unassigned codepoints are ok. |
| 777 | {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"}, |
| 778 | }; |
| 779 | |
| 780 | TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 781 | for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 782 | std::wstring bad_name(kIllegalCharacterCases[i].bad_name); |
| 783 | file_util::ReplaceIllegalCharacters(&bad_name, L'-'); |
| 784 | EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | static const struct ReplaceExtensionCase { |
| 789 | std::wstring file_name; |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 790 | FilePath::StringType extension; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 791 | std::wstring result; |
| 792 | } kReplaceExtension[] = { |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 793 | {L"", FILE_PATH_LITERAL(""), L""}, |
| 794 | {L"", FILE_PATH_LITERAL("txt"), L".txt"}, |
| 795 | {L".", FILE_PATH_LITERAL("txt"), L".txt"}, |
| 796 | {L".", FILE_PATH_LITERAL(""), L""}, |
| 797 | {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"}, |
| 798 | {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"}, |
| 799 | {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"}, |
| 800 | {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"}, |
| 801 | {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"}, |
| 802 | {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"}, |
| 803 | {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"}, |
| 804 | {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"}, |
| 805 | {L"foo", FILE_PATH_LITERAL(""), L"foo"}, |
| 806 | {L"foo", FILE_PATH_LITERAL("."), L"foo"}, |
| 807 | {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"}, |
| 808 | {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"}, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 809 | }; |
| 810 | |
| 811 | TEST_F(FileUtilTest, ReplaceExtensionTest) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 812 | for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) { |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 813 | FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name); |
| 814 | file_util::ReplaceExtension(&path, kReplaceExtension[i].extension); |
| 815 | EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
[email protected] | b988fe4d | 2008-09-19 17:32:18 | [diff] [blame] | 819 | // Make sure ReplaceExtension doesn't replace an extension that occurs as one of |
| 820 | // the directory names of the path. |
| 821 | TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) { |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 822 | FilePath path; |
| 823 | path = path.Append(FILE_PATH_LITERAL("foo.bar")); |
| 824 | path = path.Append(FILE_PATH_LITERAL("foo")); |
[email protected] | b988fe4d | 2008-09-19 17:32:18 | [diff] [blame] | 825 | // '/foo.bar/foo' with extension '.baz' |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 826 | FilePath result_path = path; |
| 827 | file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz")); |
| 828 | EXPECT_EQ(path.ToWStringHack() + L".baz", result_path.ToWStringHack()); |
[email protected] | b988fe4d | 2008-09-19 17:32:18 | [diff] [blame] | 829 | } |
| 830 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 831 | TEST_F(FileUtilTest, FileEnumeratorTest) { |
| 832 | // Test an empty directory. |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 833 | file_util::FileEnumerator f0(test_dir_, true, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 834 | file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 835 | EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); |
| 836 | EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 837 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 838 | // create the directories |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 839 | FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 840 | EXPECT_TRUE(file_util::CreateDirectory(dir1)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 841 | FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 842 | EXPECT_TRUE(file_util::CreateDirectory(dir2)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 843 | FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 844 | EXPECT_TRUE(file_util::CreateDirectory(dir2inner)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 845 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 846 | // create the files |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 847 | FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 848 | CreateTextFile(dir2file, L""); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 849 | FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 850 | CreateTextFile(dir2innerfile, L""); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 851 | FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 852 | CreateTextFile(file1, L""); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 853 | FilePath file2_rel = |
| 854 | dir2.Append(FilePath::kParentDirectory) |
| 855 | .Append(FILE_PATH_LITERAL("file2.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 856 | CreateTextFile(file2_rel, L""); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 857 | FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 858 | |
| 859 | // Only enumerate files. |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 860 | file_util::FileEnumerator f1(test_dir_, true, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 861 | file_util::FileEnumerator::FILES); |
| 862 | FindResultCollector c1(f1); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 863 | EXPECT_TRUE(c1.HasFile(file1)); |
| 864 | EXPECT_TRUE(c1.HasFile(file2_abs)); |
| 865 | EXPECT_TRUE(c1.HasFile(dir2file)); |
| 866 | EXPECT_TRUE(c1.HasFile(dir2innerfile)); |
| 867 | EXPECT_EQ(c1.size(), 4); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 868 | |
| 869 | // Only enumerate directories. |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 870 | file_util::FileEnumerator f2(test_dir_, true, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 871 | file_util::FileEnumerator::DIRECTORIES); |
| 872 | FindResultCollector c2(f2); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 873 | EXPECT_TRUE(c2.HasFile(dir1)); |
| 874 | EXPECT_TRUE(c2.HasFile(dir2)); |
| 875 | EXPECT_TRUE(c2.HasFile(dir2inner)); |
| 876 | EXPECT_EQ(c2.size(), 3); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 877 | |
[email protected] | 3bc9ffaf | 2008-10-16 02:42:45 | [diff] [blame] | 878 | // Only enumerate directories non-recursively. |
| 879 | file_util::FileEnumerator f2_non_recursive( |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 880 | test_dir_, false, file_util::FileEnumerator::DIRECTORIES); |
[email protected] | 3bc9ffaf | 2008-10-16 02:42:45 | [diff] [blame] | 881 | FindResultCollector c2_non_recursive(f2_non_recursive); |
| 882 | EXPECT_TRUE(c2_non_recursive.HasFile(dir1)); |
| 883 | EXPECT_TRUE(c2_non_recursive.HasFile(dir2)); |
| 884 | EXPECT_EQ(c2_non_recursive.size(), 2); |
| 885 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 886 | // Enumerate files and directories. |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 887 | file_util::FileEnumerator f3(test_dir_, true, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 888 | file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
| 889 | FindResultCollector c3(f3); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 890 | EXPECT_TRUE(c3.HasFile(dir1)); |
| 891 | EXPECT_TRUE(c3.HasFile(dir2)); |
| 892 | EXPECT_TRUE(c3.HasFile(file1)); |
| 893 | EXPECT_TRUE(c3.HasFile(file2_abs)); |
| 894 | EXPECT_TRUE(c3.HasFile(dir2file)); |
| 895 | EXPECT_TRUE(c3.HasFile(dir2inner)); |
| 896 | EXPECT_TRUE(c3.HasFile(dir2innerfile)); |
| 897 | EXPECT_EQ(c3.size(), 7); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 898 | |
| 899 | // Non-recursive operation. |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 900 | file_util::FileEnumerator f4(test_dir_, false, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 901 | file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
| 902 | FindResultCollector c4(f4); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 903 | EXPECT_TRUE(c4.HasFile(dir2)); |
| 904 | EXPECT_TRUE(c4.HasFile(dir2)); |
| 905 | EXPECT_TRUE(c4.HasFile(file1)); |
| 906 | EXPECT_TRUE(c4.HasFile(file2_abs)); |
| 907 | EXPECT_EQ(c4.size(), 4); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 908 | |
| 909 | // Enumerate with a pattern. |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 910 | file_util::FileEnumerator f5(test_dir_, true, |
| 911 | file_util::FileEnumerator::FILES_AND_DIRECTORIES, |
| 912 | FILE_PATH_LITERAL("dir*")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 913 | FindResultCollector c5(f5); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 914 | EXPECT_TRUE(c5.HasFile(dir1)); |
| 915 | EXPECT_TRUE(c5.HasFile(dir2)); |
| 916 | EXPECT_TRUE(c5.HasFile(dir2file)); |
| 917 | EXPECT_TRUE(c5.HasFile(dir2inner)); |
| 918 | EXPECT_TRUE(c5.HasFile(dir2innerfile)); |
| 919 | EXPECT_EQ(c5.size(), 5); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 920 | |
| 921 | // Make sure the destructor closes the find handle while in the middle of a |
| 922 | // query to allow TearDown to delete the directory. |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 923 | file_util::FileEnumerator f6(test_dir_, true, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 924 | file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 925 | EXPECT_FALSE(f6.Next().value().empty()); // Should have found something |
| 926 | // (we don't care what). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 927 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 928 | |
[email protected] | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 929 | |
| 930 | void PathComponents(const std::wstring& path, |
| 931 | std::vector<std::wstring>* components) { |
| 932 | DCHECK(components != NULL); |
| 933 | if (components == NULL) |
| 934 | return; |
| 935 | std::wstring::size_type start = 0; |
| 936 | std::wstring::size_type end = path.find('/', start); |
| 937 | |
| 938 | // Special case the "/" or "\" directory. On Windows with a drive letter, |
| 939 | // this code path won't hit, but the right thing should still happen. |
| 940 | // "E:\foo" will turn into "E:","foo". |
| 941 | if (end == start) { |
| 942 | components->push_back(std::wstring(path, 0, 1)); |
| 943 | start = end + 1; |
| 944 | end = path.find('/', start); |
| 945 | } |
| 946 | while (end != std::wstring::npos) { |
| 947 | std::wstring component = std::wstring(path, start, end - start); |
| 948 | components->push_back(component); |
| 949 | start = end + 1; |
| 950 | end = path.find('/', start); |
| 951 | } |
| 952 | std::wstring component = std::wstring(path, start); |
| 953 | components->push_back(component); |
| 954 | } |
| 955 | |
| 956 | static const struct PathComponentsCase { |
| 957 | std::wstring path; |
| 958 | FilePath::StringType result; |
| 959 | } kPathComponents[] = { |
| 960 | {L"/foo/bar/baz/", FILE_PATH_LITERAL("/|foo|bar|baz|")}, |
| 961 | {L"/foo/bar/baz", FILE_PATH_LITERAL("/|foo|bar|baz")}, |
| 962 | {L"e:/foo", FILE_PATH_LITERAL("e:|foo")}, |
| 963 | }; |
| 964 | |
| 965 | TEST_F(FileUtilTest, PathComponentsTest) { |
| 966 | for (size_t i = 0; i < arraysize(kPathComponents); ++i) { |
| 967 | FilePath path = FilePath::FromWStringHack(kPathComponents[i].path); |
| 968 | std::vector<FilePath::StringType> comps; |
| 969 | file_util::PathComponents(path, &comps); |
| 970 | |
| 971 | FilePath::StringType result; |
| 972 | for (size_t j = 0; j < comps.size(); ++j) { |
| 973 | result.append(comps[j]); |
| 974 | if (j < comps.size() - 1) |
| 975 | result.append(FILE_PATH_LITERAL("|"), 1); |
| 976 | } |
| 977 | EXPECT_EQ(kPathComponents[i].result, result); |
| 978 | } |
| 979 | } |
| 980 | |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 981 | TEST_F(FileUtilTest, Contains) { |
| 982 | FilePath data_dir; |
| 983 | ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &data_dir)); |
| 984 | data_dir = data_dir.Append(FILE_PATH_LITERAL("FilePathTest")); |
| 985 | |
| 986 | // Create a fresh, empty copy of this directory. |
| 987 | ASSERT_TRUE(file_util::Delete(data_dir, true)); |
| 988 | ASSERT_TRUE(file_util::CreateDirectory(data_dir)); |
| 989 | |
| 990 | FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo"))); |
| 991 | FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt"))); |
| 992 | FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt"))); |
| 993 | FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
| 994 | |
| 995 | // Annoyingly, the directories must actually exist in order for realpath(), |
| 996 | // which Contains() relies on in posix, to work. |
| 997 | ASSERT_TRUE(file_util::CreateDirectory(foo)); |
| 998 | std::string data("hello"); |
| 999 | ASSERT_TRUE(file_util::WriteFile(bar.ToWStringHack(), data.c_str(), |
| 1000 | data.length())); |
| 1001 | ASSERT_TRUE(file_util::WriteFile(baz.ToWStringHack(), data.c_str(), |
| 1002 | data.length())); |
| 1003 | ASSERT_TRUE(file_util::WriteFile(foobar.ToWStringHack(), data.c_str(), |
| 1004 | data.length())); |
| 1005 | |
| 1006 | EXPECT_TRUE(file_util::ContainsPath(foo, bar)); |
| 1007 | EXPECT_FALSE(file_util::ContainsPath(foo, baz)); |
| 1008 | EXPECT_FALSE(file_util::ContainsPath(foo, foobar)); |
| 1009 | EXPECT_FALSE(file_util::ContainsPath(foo, foo)); |
| 1010 | |
| 1011 | // Platform-specific concerns |
| 1012 | FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO"))); |
| 1013 | #if defined(OS_WIN) |
| 1014 | EXPECT_TRUE(file_util::ContainsPath(foo, |
| 1015 | foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
| 1016 | EXPECT_TRUE(file_util::ContainsPath(foo, |
| 1017 | FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt")))); |
| 1018 | #elif defined(OS_LINUX) |
| 1019 | EXPECT_FALSE(file_util::ContainsPath(foo, |
| 1020 | foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
| 1021 | #else |
| 1022 | // We can't really do this test on osx since the case-sensitivity of the |
| 1023 | // filesystem is configurable. |
| 1024 | #endif |
| 1025 | } |
| 1026 | |
[email protected] | 11b901ee | 2008-09-10 00:16:28 | [diff] [blame] | 1027 | } // namespace |