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