[email protected] | d0767cb54 | 2009-10-08 17:38:30 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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] | 7d95aae | 2009-10-09 07:33:39 | [diff] [blame^] | 11 | #include <tchar.h> |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 12 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | |
| 14 | #include <fstream> |
| 15 | #include <iostream> |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 16 | #include <set> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | |
| 18 | #include "base/base_paths.h" |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 19 | #include "base/file_path.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 20 | #include "base/file_util.h" |
| 21 | #include "base/logging.h" |
| 22 | #include "base/path_service.h" |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 23 | #include "base/platform_thread.h" |
[email protected] | 85786f9 | 2009-04-18 00:42:48 | [diff] [blame] | 24 | #include "base/time.h" |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 25 | #include "base/utf_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 26 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 27 | #include "testing/platform_test.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 28 | |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 29 | // This macro helps avoid wrapped lines in the test structs. |
| 30 | #define FPL(x) FILE_PATH_LITERAL(x) |
| 31 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | namespace { |
| 33 | |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 34 | const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES = |
| 35 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 36 | file_util::FileEnumerator::FILES | |
| 37 | file_util::FileEnumerator::DIRECTORIES); |
| 38 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 39 | // file_util winds up using autoreleased objects on the Mac, so this needs |
| 40 | // to be a PlatformTest |
| 41 | class FileUtilTest : public PlatformTest { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 42 | protected: |
| 43 | virtual void SetUp() { |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 44 | PlatformTest::SetUp(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 45 | // Name a subdirectory of the temp directory. |
| 46 | ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 47 | test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 48 | |
| 49 | // Create a fresh, empty copy of this directory. |
| 50 | file_util::Delete(test_dir_, true); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 51 | file_util::CreateDirectory(test_dir_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 52 | } |
| 53 | virtual void TearDown() { |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 54 | PlatformTest::TearDown(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 55 | // Clean up test directory |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 56 | ASSERT_TRUE(file_util::Delete(test_dir_, true)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 57 | ASSERT_FALSE(file_util::PathExists(test_dir_)); |
| 58 | } |
| 59 | |
| 60 | // the path to temporary directory used to contain the test operations |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 61 | FilePath test_dir_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | // Collects all the results from the given file enumerator, and provides an |
| 65 | // interface to query whether a given file is present. |
| 66 | class FindResultCollector { |
| 67 | public: |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 68 | explicit FindResultCollector(file_util::FileEnumerator& enumerator) { |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 69 | FilePath cur_file; |
| 70 | while (!(cur_file = enumerator.Next()).value().empty()) { |
| 71 | FilePath::StringType path = cur_file.value(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 72 | // The file should not be returned twice. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 73 | EXPECT_TRUE(files_.end() == files_.find(path)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 74 | << "Same file returned twice"; |
| 75 | |
| 76 | // Save for later. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 77 | files_.insert(path); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | // Returns true if the enumerator found the file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 82 | bool HasFile(const FilePath& file) const { |
| 83 | return files_.find(file.value()) != files_.end(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 84 | } |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 85 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 86 | int size() { |
[email protected] | f35d39b | 2008-08-15 17:50:10 | [diff] [blame] | 87 | return static_cast<int>(files_.size()); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 88 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 89 | |
| 90 | private: |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 91 | std::set<FilePath::StringType> files_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | // Simple function to dump some text into a new file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 95 | void CreateTextFile(const FilePath& filename, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 96 | const std::wstring& contents) { |
| 97 | std::ofstream file; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 98 | file.open(WideToUTF8(filename.ToWStringHack()).c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 99 | ASSERT_TRUE(file.is_open()); |
| 100 | file << contents; |
| 101 | file.close(); |
| 102 | } |
| 103 | |
| 104 | // Simple function to take out some text from a file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 105 | std::wstring ReadTextFile(const FilePath& filename) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 106 | wchar_t contents[64]; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 107 | std::wifstream file; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 108 | file.open(WideToUTF8(filename.ToWStringHack()).c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 109 | EXPECT_TRUE(file.is_open()); |
| 110 | file.getline(contents, 64); |
| 111 | file.close(); |
| 112 | return std::wstring(contents); |
| 113 | } |
| 114 | |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 115 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | uint64 FileTimeAsUint64(const FILETIME& ft) { |
| 117 | ULARGE_INTEGER u; |
| 118 | u.LowPart = ft.dwLowDateTime; |
| 119 | u.HighPart = ft.dwHighDateTime; |
| 120 | return u.QuadPart; |
| 121 | } |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 122 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 123 | |
| 124 | const struct append_case { |
| 125 | const wchar_t* path; |
| 126 | const wchar_t* ending; |
| 127 | const wchar_t* result; |
| 128 | } append_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 129 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 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"path", L"c:\\colon\\backslash\\\\path"}, |
| 133 | {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"}, |
| 134 | {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"}, |
| 135 | {L"", L"path", L"\\path"}, |
| 136 | {L"", L"", L"\\"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 137 | #elif defined(OS_POSIX) |
| 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"path", L"/foo/bar//path"}, |
| 141 | {L"/foo/bar/", L"", L"/foo/bar/"}, |
| 142 | {L"/foo/bar", L"", L"/foo/bar/"}, |
| 143 | {L"", L"path", L"/path"}, |
| 144 | {L"", L"", L"/"}, |
| 145 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 146 | }; |
| 147 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 148 | TEST_F(FileUtilTest, AppendToPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 149 | for (unsigned int i = 0; i < arraysize(append_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 150 | const append_case& value = append_cases[i]; |
| 151 | std::wstring result = value.path; |
| 152 | file_util::AppendToPath(&result, value.ending); |
| 153 | EXPECT_EQ(value.result, result); |
| 154 | } |
| 155 | |
| 156 | #ifdef NDEBUG |
| 157 | file_util::AppendToPath(NULL, L"path"); // asserts in debug mode |
| 158 | #endif |
| 159 | } |
| 160 | |
| 161 | static const struct InsertBeforeExtensionCase { |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 162 | const FilePath::CharType* path; |
| 163 | const FilePath::CharType* suffix; |
| 164 | const FilePath::CharType* result; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 165 | } kInsertBeforeExtension[] = { |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 166 | {FPL(""), FPL(""), FPL("")}, |
| 167 | {FPL(""), FPL("txt"), FPL("txt")}, |
| 168 | {FPL("."), FPL("txt"), FPL("txt.")}, |
| 169 | {FPL("."), FPL(""), FPL(".")}, |
| 170 | {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")}, |
| 171 | {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")}, |
| 172 | {FPL("foo"), FPL("txt"), FPL("footxt")}, |
| 173 | {FPL("foo"), FPL(".txt"), FPL("foo.txt")}, |
| 174 | {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")}, |
| 175 | {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")}, |
| 176 | {FPL("foo.dll"), FPL(""), FPL("foo.dll")}, |
| 177 | {FPL("foo.dll"), FPL("."), FPL("foo..dll")}, |
| 178 | {FPL("foo"), FPL(""), FPL("foo")}, |
| 179 | {FPL("foo"), FPL("."), FPL("foo.")}, |
| 180 | {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")}, |
| 181 | {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")}, |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 182 | #if defined(OS_WIN) |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 183 | {FPL("\\"), FPL(""), FPL("\\")}, |
| 184 | {FPL("\\"), FPL("txt"), FPL("\\txt")}, |
| 185 | {FPL("\\."), FPL("txt"), FPL("\\txt.")}, |
| 186 | {FPL("\\."), FPL(""), FPL("\\.")}, |
| 187 | {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")}, |
| 188 | {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")}, |
| 189 | {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")}, |
| 190 | {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"), |
| 191 | FPL("C:\\bar.baz\\foo.dlltxt.exe")}, |
| 192 | {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")}, |
| 193 | {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")}, |
| 194 | {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")}, |
| 195 | {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] | 196 | #elif defined(OS_POSIX) |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 197 | {FPL("/"), FPL(""), FPL("/")}, |
| 198 | {FPL("/"), FPL("txt"), FPL("/txt")}, |
| 199 | {FPL("/."), FPL("txt"), FPL("/txt.")}, |
| 200 | {FPL("/."), FPL(""), FPL("/.")}, |
| 201 | {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")}, |
| 202 | {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")}, |
| 203 | {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")}, |
| 204 | {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")}, |
| 205 | {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")}, |
| 206 | {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")}, |
| 207 | {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")}, |
| 208 | {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 209 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | TEST_F(FileUtilTest, InsertBeforeExtensionTest) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 213 | for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) { |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 214 | FilePath path(kInsertBeforeExtension[i].path); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 215 | file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix); |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 216 | EXPECT_EQ(kInsertBeforeExtension[i].result, path.value()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | static const struct filename_case { |
| 221 | const wchar_t* path; |
| 222 | const wchar_t* filename; |
| 223 | } filename_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 224 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 225 | {L"c:\\colon\\backslash", L"backslash"}, |
| 226 | {L"c:\\colon\\backslash\\", L""}, |
| 227 | {L"\\\\filename.exe", L"filename.exe"}, |
| 228 | {L"filename.exe", L"filename.exe"}, |
| 229 | {L"", L""}, |
| 230 | {L"\\\\\\", L""}, |
| 231 | {L"c:/colon/backslash", L"backslash"}, |
| 232 | {L"c:/colon/backslash/", L""}, |
| 233 | {L"//////", L""}, |
| 234 | {L"///filename.exe", L"filename.exe"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 235 | #elif defined(OS_POSIX) |
| 236 | {L"/foo/bar", L"bar"}, |
| 237 | {L"/foo/bar/", L""}, |
| 238 | {L"/filename.exe", L"filename.exe"}, |
| 239 | {L"filename.exe", L"filename.exe"}, |
| 240 | {L"", L""}, |
| 241 | {L"/", L""}, |
| 242 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | TEST_F(FileUtilTest, GetFilenameFromPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 246 | for (unsigned int i = 0; i < arraysize(filename_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 247 | const filename_case& value = filename_cases[i]; |
| 248 | std::wstring result = file_util::GetFilenameFromPath(value.path); |
| 249 | EXPECT_EQ(value.filename, result); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Test finding the file type from a path name |
| 254 | static const struct extension_case { |
| 255 | const wchar_t* path; |
| 256 | const wchar_t* extension; |
| 257 | } extension_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 258 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 259 | {L"C:\\colon\\backslash\\filename.extension", L"extension"}, |
| 260 | {L"C:\\colon\\backslash\\filename.", L""}, |
| 261 | {L"C:\\colon\\backslash\\filename", L""}, |
| 262 | {L"C:\\colon\\backslash\\", L""}, |
| 263 | {L"C:\\colon\\backslash.\\", L""}, |
| 264 | {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 265 | #elif defined(OS_POSIX) |
| 266 | {L"/foo/bar/filename.extension", L"extension"}, |
| 267 | {L"/foo/bar/filename.", L""}, |
| 268 | {L"/foo/bar/filename", L""}, |
| 269 | {L"/foo/bar/", L""}, |
| 270 | {L"/foo/bar./", L""}, |
| 271 | {L"/foo/bar/filename.extension.extension2", L"extension2"}, |
| 272 | {L".", L""}, |
| 273 | {L"..", L""}, |
| 274 | {L"./foo", L""}, |
| 275 | {L"./foo.extension", L"extension"}, |
| 276 | {L"/foo.extension1/bar.extension2", L"extension2"}, |
| 277 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | TEST_F(FileUtilTest, GetFileExtensionFromPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 281 | for (unsigned int i = 0; i < arraysize(extension_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 282 | const extension_case& ext = extension_cases[i]; |
| 283 | const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path); |
| 284 | EXPECT_EQ(ext.extension, fext); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // Test finding the directory component of a path |
| 289 | static const struct dir_case { |
| 290 | const wchar_t* full_path; |
| 291 | const wchar_t* directory; |
| 292 | } dir_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 293 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 294 | {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"}, |
| 295 | {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"}, |
| 296 | {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"}, |
| 297 | {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"}, |
| 298 | {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"}, |
| 299 | {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."}, |
| 300 | {L"C:\\", L"C:"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 301 | #elif defined(OS_POSIX) |
| 302 | {L"/foo/bar/gdi32.dll", L"/foo/bar"}, |
| 303 | {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"}, |
| 304 | {L"/foo/bar/", L"/foo/bar"}, |
| 305 | {L"/foo/bar//", L"/foo/bar"}, |
| 306 | {L"/foo/bar", L"/foo"}, |
| 307 | {L"/foo/bar./", L"/foo/bar."}, |
| 308 | {L"/", L"/"}, |
| 309 | {L".", L"."}, |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 310 | {L"..", L"."}, // yes, ".." technically lives in "." |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 311 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | TEST_F(FileUtilTest, GetDirectoryFromPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 315 | for (unsigned int i = 0; i < arraysize(dir_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 316 | const dir_case& dir = dir_cases[i]; |
| 317 | const std::wstring parent = |
| 318 | file_util::GetDirectoryFromPath(dir.full_path); |
| 319 | EXPECT_EQ(dir.directory, parent); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | TEST_F(FileUtilTest, CountFilesCreatedAfter) { |
| 324 | // Create old file (that we don't want to count) |
[email protected] | 85786f9 | 2009-04-18 00:42:48 | [diff] [blame] | 325 | FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 326 | CreateTextFile(old_file_name, L"Just call me Mr. Creakybits"); |
| 327 | |
| 328 | // Age to perfection |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 329 | #if defined(OS_WIN) |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 330 | PlatformThread::Sleep(100); |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 331 | #elif defined(OS_POSIX) |
| 332 | // We need to wait at least one second here because the precision of |
| 333 | // file creation time is one second. |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 334 | PlatformThread::Sleep(1500); |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 335 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 336 | |
| 337 | // Establish our cutoff time |
[email protected] | 85786f9 | 2009-04-18 00:42:48 | [diff] [blame] | 338 | base::Time now(base::Time::NowFromSystemTime()); |
| 339 | EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 340 | |
| 341 | // Create a new file (that we do want to count) |
[email protected] | 85786f9 | 2009-04-18 00:42:48 | [diff] [blame] | 342 | FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 343 | CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah."); |
| 344 | |
| 345 | // We should see only the new file. |
[email protected] | 85786f9 | 2009-04-18 00:42:48 | [diff] [blame] | 346 | EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 347 | |
| 348 | // Delete new file, we should see no files after cutoff now |
| 349 | EXPECT_TRUE(file_util::Delete(new_file_name, false)); |
[email protected] | 85786f9 | 2009-04-18 00:42:48 | [diff] [blame] | 350 | EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // Tests that the Delete function works as expected, especially |
| 354 | // the recursion flag. Also coincidentally tests PathExists. |
| 355 | TEST_F(FileUtilTest, Delete) { |
| 356 | // Create a file |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 357 | FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 358 | CreateTextFile(file_name, L"I'm cannon fodder."); |
| 359 | |
| 360 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 361 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 362 | FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory")); |
| 363 | file_util::CreateDirectory(subdir_path); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 364 | |
| 365 | ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 366 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 367 | FilePath directory_contents = test_dir_; |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 368 | #if defined(OS_WIN) |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 369 | // TODO(erikkay): see if anyone's actually using this feature of the API |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 370 | directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 371 | // Delete non-recursively and check that only the file is deleted |
| 372 | ASSERT_TRUE(file_util::Delete(directory_contents, false)); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 373 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 374 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 375 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 376 | |
| 377 | // Delete recursively and make sure all contents are deleted |
| 378 | ASSERT_TRUE(file_util::Delete(directory_contents, true)); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 379 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 380 | EXPECT_FALSE(file_util::PathExists(subdir_path)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | TEST_F(FileUtilTest, Move) { |
| 384 | // Create a directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 385 | FilePath dir_name_from = |
| 386 | test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir")); |
| 387 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 388 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 389 | |
| 390 | // Create a file under the directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 391 | FilePath file_name_from = |
| 392 | dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 393 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 394 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 395 | |
| 396 | // Move the directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 397 | FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir")); |
| 398 | FilePath file_name_to = |
| 399 | dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 400 | |
| 401 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 402 | |
| 403 | EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to)); |
| 404 | |
| 405 | // Check everything has been moved. |
| 406 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 407 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 408 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 409 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 410 | } |
| 411 | |
| 412 | TEST_F(FileUtilTest, CopyDirectoryRecursively) { |
| 413 | // Create a directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 414 | FilePath dir_name_from = |
| 415 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 416 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 417 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 418 | |
| 419 | // Create a file under the directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 420 | FilePath file_name_from = |
| 421 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 422 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 423 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 424 | |
| 425 | // Create a subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 426 | FilePath subdir_name_from = |
| 427 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 428 | file_util::CreateDirectory(subdir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 429 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 430 | |
| 431 | // Create a file under the subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 432 | FilePath file_name2_from = |
| 433 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 434 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 435 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 436 | |
| 437 | // Copy the directory recursively. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 438 | FilePath dir_name_to = |
| 439 | test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
| 440 | FilePath file_name_to = |
| 441 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 442 | FilePath subdir_name_to = |
| 443 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 444 | FilePath file_name2_to = |
| 445 | subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 446 | |
| 447 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 448 | |
| 449 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true)); |
| 450 | |
| 451 | // Check everything has been copied. |
| 452 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 453 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 454 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 455 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 456 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 457 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 458 | EXPECT_TRUE(file_util::PathExists(subdir_name_to)); |
| 459 | EXPECT_TRUE(file_util::PathExists(file_name2_to)); |
| 460 | } |
| 461 | |
| 462 | TEST_F(FileUtilTest, CopyDirectory) { |
| 463 | // Create a directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 464 | FilePath dir_name_from = |
| 465 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 466 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 467 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 468 | |
| 469 | // Create a file under the directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 470 | FilePath file_name_from = |
| 471 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 472 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 473 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 474 | |
| 475 | // Create a subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 476 | FilePath subdir_name_from = |
| 477 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 478 | file_util::CreateDirectory(subdir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 479 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 480 | |
| 481 | // Create a file under the subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 482 | FilePath file_name2_from = |
| 483 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 484 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 485 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 486 | |
| 487 | // Copy the directory not recursively. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 488 | FilePath dir_name_to = |
| 489 | test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
| 490 | FilePath file_name_to = |
| 491 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 492 | FilePath subdir_name_to = |
| 493 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 494 | |
| 495 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 496 | |
| 497 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false)); |
| 498 | |
| 499 | // Check everything has been copied. |
| 500 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 501 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 502 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 503 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 504 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 505 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 506 | EXPECT_FALSE(file_util::PathExists(subdir_name_to)); |
| 507 | } |
| 508 | |
| 509 | TEST_F(FileUtilTest, CopyFile) { |
| 510 | // Create a directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 511 | FilePath dir_name_from = |
| 512 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 513 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 514 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 515 | |
| 516 | // Create a file under the directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 517 | FilePath file_name_from = |
| 518 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 519 | const std::wstring file_contents(L"Gooooooooooooooooooooogle"); |
| 520 | CreateTextFile(file_name_from, file_contents); |
| 521 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 522 | |
| 523 | // Copy the file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 524 | FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 525 | ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file)); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 526 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 527 | // Copy the file to another location using '..' in the path. |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 528 | FilePath dest_file2(dir_name_from); |
| 529 | dest_file2 = dest_file2.AppendASCII(".."); |
| 530 | dest_file2 = dest_file2.AppendASCII("DestFile.txt"); |
| 531 | ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2)); |
| 532 | |
| 533 | FilePath dest_file2_test(dir_name_from); |
| 534 | dest_file2_test = dest_file2_test.DirName(); |
| 535 | dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 536 | |
| 537 | // Check everything has been copied. |
| 538 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 539 | EXPECT_TRUE(file_util::PathExists(dest_file)); |
| 540 | const std::wstring read_contents = ReadTextFile(dest_file); |
| 541 | EXPECT_EQ(file_contents, read_contents); |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 542 | EXPECT_TRUE(file_util::PathExists(dest_file2_test)); |
| 543 | EXPECT_TRUE(file_util::PathExists(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] | 7d95aae | 2009-10-09 07:33:39 | [diff] [blame^] | 777 | |
| 778 | TEST_F(FileUtilTest, GetTempDirTest) { |
| 779 | static const TCHAR* kTmpKey = _T("TMP"); |
| 780 | static const TCHAR* kTmpValues[] = { |
| 781 | _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\") |
| 782 | }; |
| 783 | // Save the original $TMP. |
| 784 | size_t original_tmp_size; |
| 785 | TCHAR* original_tmp; |
| 786 | ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey)); |
| 787 | // original_tmp may be NULL. |
| 788 | |
| 789 | for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) { |
| 790 | FilePath path; |
| 791 | ::_tputenv_s(kTmpKey, kTmpValues[i]); |
| 792 | file_util::GetTempDir(&path); |
| 793 | EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] << |
| 794 | " result=" << path.value(); |
| 795 | } |
| 796 | |
| 797 | // Restore the original $TMP. |
| 798 | if (original_tmp) { |
| 799 | ::_tputenv_s(kTmpKey, original_tmp); |
| 800 | free(original_tmp); |
| 801 | } else { |
| 802 | ::_tputenv_s(kTmpKey, _T("")); |
| 803 | } |
| 804 | } |
| 805 | #endif // OS_WIN |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 806 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 807 | TEST_F(FileUtilTest, CreateTemporaryFileTest) { |
| 808 | FilePath temp_files[3]; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 809 | for (int i = 0; i < 3; i++) { |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 810 | ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i]))); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 811 | EXPECT_TRUE(file_util::PathExists(temp_files[i])); |
| 812 | EXPECT_FALSE(file_util::DirectoryExists(temp_files[i])); |
| 813 | } |
| 814 | for (int i = 0; i < 3; i++) |
| 815 | EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); |
| 816 | for (int i = 0; i < 3; i++) |
| 817 | EXPECT_TRUE(file_util::Delete(temp_files[i], false)); |
| 818 | } |
| 819 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 820 | TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) { |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 821 | FilePath names[3]; |
| 822 | FILE *fps[3]; |
| 823 | int i; |
| 824 | |
| 825 | // Create; make sure they are open and exist. |
| 826 | for (i = 0; i < 3; ++i) { |
| 827 | fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i])); |
| 828 | ASSERT_TRUE(fps[i]); |
| 829 | EXPECT_TRUE(file_util::PathExists(names[i])); |
| 830 | } |
| 831 | |
| 832 | // Make sure all names are unique. |
| 833 | for (i = 0; i < 3; ++i) { |
| 834 | EXPECT_FALSE(names[i] == names[(i+1)%3]); |
| 835 | } |
| 836 | |
| 837 | // Close and delete. |
| 838 | for (i = 0; i < 3; ++i) { |
| 839 | EXPECT_TRUE(file_util::CloseFile(fps[i])); |
| 840 | EXPECT_TRUE(file_util::Delete(names[i], false)); |
| 841 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 845 | FilePath temp_dir; |
| 846 | ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(), |
| 847 | &temp_dir)); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 848 | EXPECT_TRUE(file_util::PathExists(temp_dir)); |
| 849 | EXPECT_TRUE(file_util::Delete(temp_dir, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 850 | } |
| 851 | |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 852 | TEST_F(FileUtilTest, GetShmemTempDirTest) { |
| 853 | FilePath dir; |
| 854 | EXPECT_TRUE(file_util::GetShmemTempDir(&dir)); |
| 855 | EXPECT_TRUE(file_util::DirectoryExists(dir)); |
| 856 | } |
| 857 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 858 | TEST_F(FileUtilTest, CreateDirectoryTest) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 859 | FilePath test_root = |
| 860 | test_dir_.Append(FILE_PATH_LITERAL("create_directory_test")); |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 861 | #if defined(OS_WIN) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 862 | FilePath test_path = |
| 863 | test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 864 | #elif defined(OS_POSIX) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 865 | FilePath test_path = |
| 866 | test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 867 | #endif |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 868 | |
| 869 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 870 | EXPECT_TRUE(file_util::CreateDirectory(test_path)); |
| 871 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 872 | // CreateDirectory returns true if the DirectoryExists returns true. |
| 873 | EXPECT_TRUE(file_util::CreateDirectory(test_path)); |
| 874 | |
| 875 | // Doesn't work to create it on top of a non-dir |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 876 | test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt")); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 877 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 878 | CreateTextFile(test_path, L"test file"); |
| 879 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 880 | EXPECT_FALSE(file_util::CreateDirectory(test_path)); |
| 881 | |
| 882 | EXPECT_TRUE(file_util::Delete(test_root, true)); |
| 883 | EXPECT_FALSE(file_util::PathExists(test_root)); |
| 884 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 885 | } |
| 886 | |
| 887 | TEST_F(FileUtilTest, DetectDirectoryTest) { |
| 888 | // Check a directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 889 | FilePath test_root = |
| 890 | test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test")); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 891 | EXPECT_FALSE(file_util::PathExists(test_root)); |
| 892 | EXPECT_TRUE(file_util::CreateDirectory(test_root)); |
| 893 | EXPECT_TRUE(file_util::PathExists(test_root)); |
| 894 | EXPECT_TRUE(file_util::DirectoryExists(test_root)); |
| 895 | |
| 896 | // Check a file |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 897 | FilePath test_path = |
| 898 | test_root.Append(FILE_PATH_LITERAL("foobar.txt")); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 899 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 900 | CreateTextFile(test_path, L"test file"); |
| 901 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 902 | EXPECT_FALSE(file_util::DirectoryExists(test_path)); |
| 903 | EXPECT_TRUE(file_util::Delete(test_path, false)); |
| 904 | |
| 905 | EXPECT_TRUE(file_util::Delete(test_root, true)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 906 | } |
| 907 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 908 | static const struct ReplaceExtensionCase { |
| 909 | std::wstring file_name; |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 910 | FilePath::StringType extension; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 911 | std::wstring result; |
| 912 | } kReplaceExtension[] = { |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 913 | {L"", FILE_PATH_LITERAL(""), L""}, |
| 914 | {L"", FILE_PATH_LITERAL("txt"), L".txt"}, |
| 915 | {L".", FILE_PATH_LITERAL("txt"), L".txt"}, |
| 916 | {L".", FILE_PATH_LITERAL(""), L""}, |
| 917 | {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"}, |
| 918 | {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"}, |
| 919 | {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"}, |
| 920 | {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"}, |
| 921 | {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"}, |
| 922 | {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"}, |
| 923 | {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"}, |
| 924 | {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"}, |
| 925 | {L"foo", FILE_PATH_LITERAL(""), L"foo"}, |
| 926 | {L"foo", FILE_PATH_LITERAL("."), L"foo"}, |
| 927 | {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"}, |
| 928 | {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"}, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 929 | }; |
| 930 | |
| 931 | TEST_F(FileUtilTest, ReplaceExtensionTest) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 932 | for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) { |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 933 | FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name); |
| 934 | file_util::ReplaceExtension(&path, kReplaceExtension[i].extension); |
| 935 | EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
[email protected] | b988fe4d | 2008-09-19 17:32:18 | [diff] [blame] | 939 | // Make sure ReplaceExtension doesn't replace an extension that occurs as one of |
| 940 | // the directory names of the path. |
| 941 | TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) { |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 942 | FilePath path; |
| 943 | path = path.Append(FILE_PATH_LITERAL("foo.bar")); |
| 944 | path = path.Append(FILE_PATH_LITERAL("foo")); |
[email protected] | b988fe4d | 2008-09-19 17:32:18 | [diff] [blame] | 945 | // '/foo.bar/foo' with extension '.baz' |
[email protected] | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 946 | FilePath result_path = path; |
| 947 | file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz")); |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 948 | EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"), |
| 949 | result_path.value()); |
[email protected] | b988fe4d | 2008-09-19 17:32:18 | [diff] [blame] | 950 | } |
| 951 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 952 | TEST_F(FileUtilTest, FileEnumeratorTest) { |
| 953 | // Test an empty directory. |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 954 | file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES); |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 955 | EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); |
| 956 | EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 957 | |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 958 | // Test an empty directory, non-recursively, including "..". |
| 959 | file_util::FileEnumerator f0_dotdot(test_dir_, false, |
| 960 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 961 | FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT)); |
| 962 | EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(), |
| 963 | f0_dotdot.Next().value()); |
| 964 | EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 965 | f0_dotdot.Next().value()); |
| 966 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 967 | // create the directories |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 968 | FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 969 | EXPECT_TRUE(file_util::CreateDirectory(dir1)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 970 | FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 971 | EXPECT_TRUE(file_util::CreateDirectory(dir2)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 972 | FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 973 | EXPECT_TRUE(file_util::CreateDirectory(dir2inner)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 974 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 975 | // create the files |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 976 | FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 977 | CreateTextFile(dir2file, L""); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 978 | FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 979 | CreateTextFile(dir2innerfile, L""); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 980 | FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 981 | CreateTextFile(file1, L""); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 982 | FilePath file2_rel = |
| 983 | dir2.Append(FilePath::kParentDirectory) |
| 984 | .Append(FILE_PATH_LITERAL("file2.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 985 | CreateTextFile(file2_rel, L""); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 986 | FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 987 | |
| 988 | // Only enumerate files. |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 989 | file_util::FileEnumerator f1(test_dir_, true, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 990 | file_util::FileEnumerator::FILES); |
| 991 | FindResultCollector c1(f1); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 992 | EXPECT_TRUE(c1.HasFile(file1)); |
| 993 | EXPECT_TRUE(c1.HasFile(file2_abs)); |
| 994 | EXPECT_TRUE(c1.HasFile(dir2file)); |
| 995 | EXPECT_TRUE(c1.HasFile(dir2innerfile)); |
| 996 | EXPECT_EQ(c1.size(), 4); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 997 | |
| 998 | // Only enumerate directories. |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 999 | file_util::FileEnumerator f2(test_dir_, true, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1000 | file_util::FileEnumerator::DIRECTORIES); |
| 1001 | FindResultCollector c2(f2); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1002 | EXPECT_TRUE(c2.HasFile(dir1)); |
| 1003 | EXPECT_TRUE(c2.HasFile(dir2)); |
| 1004 | EXPECT_TRUE(c2.HasFile(dir2inner)); |
| 1005 | EXPECT_EQ(c2.size(), 3); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1006 | |
[email protected] | 3bc9ffaf | 2008-10-16 02:42:45 | [diff] [blame] | 1007 | // Only enumerate directories non-recursively. |
| 1008 | file_util::FileEnumerator f2_non_recursive( |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 1009 | test_dir_, false, file_util::FileEnumerator::DIRECTORIES); |
[email protected] | 3bc9ffaf | 2008-10-16 02:42:45 | [diff] [blame] | 1010 | FindResultCollector c2_non_recursive(f2_non_recursive); |
| 1011 | EXPECT_TRUE(c2_non_recursive.HasFile(dir1)); |
| 1012 | EXPECT_TRUE(c2_non_recursive.HasFile(dir2)); |
| 1013 | EXPECT_EQ(c2_non_recursive.size(), 2); |
| 1014 | |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1015 | // Only enumerate directories, non-recursively, including "..". |
| 1016 | file_util::FileEnumerator f2_dotdot( |
| 1017 | test_dir_, false, |
| 1018 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 1019 | file_util::FileEnumerator::DIRECTORIES | |
| 1020 | file_util::FileEnumerator::INCLUDE_DOT_DOT)); |
| 1021 | FindResultCollector c2_dotdot(f2_dotdot); |
| 1022 | EXPECT_TRUE(c2_dotdot.HasFile(dir1)); |
| 1023 | EXPECT_TRUE(c2_dotdot.HasFile(dir2)); |
| 1024 | EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL("..")))); |
| 1025 | EXPECT_EQ(c2_dotdot.size(), 3); |
| 1026 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1027 | // Enumerate files and directories. |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1028 | file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1029 | FindResultCollector c3(f3); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1030 | EXPECT_TRUE(c3.HasFile(dir1)); |
| 1031 | EXPECT_TRUE(c3.HasFile(dir2)); |
| 1032 | EXPECT_TRUE(c3.HasFile(file1)); |
| 1033 | EXPECT_TRUE(c3.HasFile(file2_abs)); |
| 1034 | EXPECT_TRUE(c3.HasFile(dir2file)); |
| 1035 | EXPECT_TRUE(c3.HasFile(dir2inner)); |
| 1036 | EXPECT_TRUE(c3.HasFile(dir2innerfile)); |
| 1037 | EXPECT_EQ(c3.size(), 7); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1038 | |
| 1039 | // Non-recursive operation. |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1040 | file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1041 | FindResultCollector c4(f4); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1042 | EXPECT_TRUE(c4.HasFile(dir2)); |
| 1043 | EXPECT_TRUE(c4.HasFile(dir2)); |
| 1044 | EXPECT_TRUE(c4.HasFile(file1)); |
| 1045 | EXPECT_TRUE(c4.HasFile(file2_abs)); |
| 1046 | EXPECT_EQ(c4.size(), 4); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1047 | |
| 1048 | // Enumerate with a pattern. |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1049 | file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES, |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 1050 | FILE_PATH_LITERAL("dir*")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1051 | FindResultCollector c5(f5); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1052 | EXPECT_TRUE(c5.HasFile(dir1)); |
| 1053 | EXPECT_TRUE(c5.HasFile(dir2)); |
| 1054 | EXPECT_TRUE(c5.HasFile(dir2file)); |
| 1055 | EXPECT_TRUE(c5.HasFile(dir2inner)); |
| 1056 | EXPECT_TRUE(c5.HasFile(dir2innerfile)); |
| 1057 | EXPECT_EQ(c5.size(), 5); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1058 | |
| 1059 | // Make sure the destructor closes the find handle while in the middle of a |
| 1060 | // query to allow TearDown to delete the directory. |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1061 | file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES); |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 1062 | EXPECT_FALSE(f6.Next().value().empty()); // Should have found something |
| 1063 | // (we don't care what). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1064 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1065 | |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1066 | TEST_F(FileUtilTest, Contains) { |
[email protected] | 11e53f6 | 2009-03-10 18:20:44 | [diff] [blame] | 1067 | FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest")); |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1068 | |
| 1069 | // Create a fresh, empty copy of this directory. |
[email protected] | a92d2fd | 2009-01-31 01:19:57 | [diff] [blame] | 1070 | if (file_util::PathExists(data_dir)) { |
| 1071 | ASSERT_TRUE(file_util::Delete(data_dir, true)); |
| 1072 | } |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1073 | ASSERT_TRUE(file_util::CreateDirectory(data_dir)); |
| 1074 | |
| 1075 | FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo"))); |
| 1076 | FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt"))); |
| 1077 | FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt"))); |
| 1078 | FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
| 1079 | |
| 1080 | // Annoyingly, the directories must actually exist in order for realpath(), |
| 1081 | // which Contains() relies on in posix, to work. |
| 1082 | ASSERT_TRUE(file_util::CreateDirectory(foo)); |
| 1083 | std::string data("hello"); |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 1084 | ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length())); |
| 1085 | ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length())); |
| 1086 | ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1087 | |
| 1088 | EXPECT_TRUE(file_util::ContainsPath(foo, bar)); |
| 1089 | EXPECT_FALSE(file_util::ContainsPath(foo, baz)); |
| 1090 | EXPECT_FALSE(file_util::ContainsPath(foo, foobar)); |
| 1091 | EXPECT_FALSE(file_util::ContainsPath(foo, foo)); |
| 1092 | |
| 1093 | // Platform-specific concerns |
| 1094 | FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO"))); |
| 1095 | #if defined(OS_WIN) |
| 1096 | EXPECT_TRUE(file_util::ContainsPath(foo, |
| 1097 | foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 1098 | EXPECT_TRUE(file_util::ContainsPath(foo, |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1099 | FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt")))); |
| 1100 | #elif defined(OS_LINUX) |
| 1101 | EXPECT_FALSE(file_util::ContainsPath(foo, |
| 1102 | foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
| 1103 | #else |
| 1104 | // We can't really do this test on osx since the case-sensitivity of the |
| 1105 | // filesystem is configurable. |
| 1106 | #endif |
| 1107 | } |
| 1108 | |
[email protected] | 11b901ee | 2008-09-10 00:16:28 | [diff] [blame] | 1109 | } // namespace |