[email protected] | ec3d145 | 2010-02-18 10:02:26 | [diff] [blame] | 1 | // Copyright (c) 2010 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> |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 9 | #include <winioctl.h> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | #include <shellapi.h> |
| 11 | #include <shlobj.h> |
[email protected] | 7d95aae | 2009-10-09 07:33:39 | [diff] [blame] | 12 | #include <tchar.h> |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 13 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
| 15 | #include <fstream> |
[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" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | #include "base/path_service.h" |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 22 | #include "base/platform_thread.h" |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 23 | #include "base/scoped_handle.h" |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 24 | #include "base/scoped_temp_dir.h" |
[email protected] | 85786f9 | 2009-04-18 00:42:48 | [diff] [blame] | 25 | #include "base/time.h" |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 26 | #include "base/utf_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 27 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 28 | #include "testing/platform_test.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 29 | |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 30 | // This macro helps avoid wrapped lines in the test structs. |
| 31 | #define FPL(x) FILE_PATH_LITERAL(x) |
| 32 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 33 | namespace { |
| 34 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 35 | // To test that file_util::Normalize FilePath() deals with NTFS reparse points |
| 36 | // correctly, we need functions to create and delete reparse points. |
| 37 | #if defined(OS_WIN) |
| 38 | typedef struct _REPARSE_DATA_BUFFER { |
| 39 | ULONG ReparseTag; |
| 40 | USHORT ReparseDataLength; |
| 41 | USHORT Reserved; |
| 42 | union { |
| 43 | struct { |
| 44 | USHORT SubstituteNameOffset; |
| 45 | USHORT SubstituteNameLength; |
| 46 | USHORT PrintNameOffset; |
| 47 | USHORT PrintNameLength; |
| 48 | ULONG Flags; |
| 49 | WCHAR PathBuffer[1]; |
| 50 | } SymbolicLinkReparseBuffer; |
| 51 | struct { |
| 52 | USHORT SubstituteNameOffset; |
| 53 | USHORT SubstituteNameLength; |
| 54 | USHORT PrintNameOffset; |
| 55 | USHORT PrintNameLength; |
| 56 | WCHAR PathBuffer[1]; |
| 57 | } MountPointReparseBuffer; |
| 58 | struct { |
| 59 | UCHAR DataBuffer[1]; |
| 60 | } GenericReparseBuffer; |
| 61 | }; |
| 62 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 63 | |
| 64 | // Sets a reparse point. |source| will now point to |target|. Returns true if |
| 65 | // the call succeeds, false otherwise. |
| 66 | bool SetReparsePoint(HANDLE source, const FilePath& target_path) { |
| 67 | std::wstring kPathPrefix = L"\\??\\"; |
| 68 | std::wstring target_str; |
| 69 | // The juction will not work if the target path does not start with \??\ . |
| 70 | if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size())) |
| 71 | target_str += kPathPrefix; |
| 72 | target_str += target_path.value(); |
| 73 | const wchar_t* target = target_str.c_str(); |
| 74 | USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]); |
| 75 | char buffer[2000] = {0}; |
| 76 | DWORD returned; |
| 77 | |
| 78 | REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer); |
| 79 | |
| 80 | data->ReparseTag = 0xa0000003; |
| 81 | memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2); |
| 82 | |
| 83 | data->MountPointReparseBuffer.SubstituteNameLength = size_target; |
| 84 | data->MountPointReparseBuffer.PrintNameOffset = size_target + 2; |
| 85 | data->ReparseDataLength = size_target + 4 + 8; |
| 86 | |
| 87 | int data_size = data->ReparseDataLength + 8; |
| 88 | |
| 89 | if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size, |
| 90 | NULL, 0, &returned, NULL)) { |
| 91 | return false; |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | // Delete the reparse point referenced by |source|. Returns true if the call |
| 97 | // succeeds, false otherwise. |
| 98 | bool DeleteReparsePoint(HANDLE source) { |
| 99 | DWORD returned; |
| 100 | REPARSE_DATA_BUFFER data = {0}; |
| 101 | data.ReparseTag = 0xa0000003; |
| 102 | if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0, |
| 103 | &returned, NULL)) { |
| 104 | return false; |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | #endif |
| 109 | |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 110 | const wchar_t bogus_content[] = L"I'm cannon fodder."; |
| 111 | |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 112 | const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES = |
| 113 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 114 | file_util::FileEnumerator::FILES | |
| 115 | file_util::FileEnumerator::DIRECTORIES); |
| 116 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 117 | // file_util winds up using autoreleased objects on the Mac, so this needs |
| 118 | // to be a PlatformTest |
| 119 | class FileUtilTest : public PlatformTest { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 120 | protected: |
| 121 | virtual void SetUp() { |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 122 | PlatformTest::SetUp(); |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 123 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 126 | ScopedTempDir temp_dir_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | // Collects all the results from the given file enumerator, and provides an |
| 130 | // interface to query whether a given file is present. |
| 131 | class FindResultCollector { |
| 132 | public: |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 133 | explicit FindResultCollector(file_util::FileEnumerator& enumerator) { |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 134 | FilePath cur_file; |
| 135 | while (!(cur_file = enumerator.Next()).value().empty()) { |
| 136 | FilePath::StringType path = cur_file.value(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 137 | // The file should not be returned twice. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 138 | EXPECT_TRUE(files_.end() == files_.find(path)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 139 | << "Same file returned twice"; |
| 140 | |
| 141 | // Save for later. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 142 | files_.insert(path); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | // Returns true if the enumerator found the file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 147 | bool HasFile(const FilePath& file) const { |
| 148 | return files_.find(file.value()) != files_.end(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 149 | } |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 150 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 151 | int size() { |
[email protected] | f35d39b | 2008-08-15 17:50:10 | [diff] [blame] | 152 | return static_cast<int>(files_.size()); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 153 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 154 | |
| 155 | private: |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 156 | std::set<FilePath::StringType> files_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | // Simple function to dump some text into a new file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 160 | void CreateTextFile(const FilePath& filename, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 161 | const std::wstring& contents) { |
| 162 | std::ofstream file; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 163 | file.open(WideToUTF8(filename.ToWStringHack()).c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 164 | ASSERT_TRUE(file.is_open()); |
| 165 | file << contents; |
| 166 | file.close(); |
| 167 | } |
| 168 | |
| 169 | // Simple function to take out some text from a file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 170 | std::wstring ReadTextFile(const FilePath& filename) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 171 | wchar_t contents[64]; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 172 | std::wifstream file; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 173 | file.open(WideToUTF8(filename.ToWStringHack()).c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 174 | EXPECT_TRUE(file.is_open()); |
| 175 | file.getline(contents, 64); |
| 176 | file.close(); |
| 177 | return std::wstring(contents); |
| 178 | } |
| 179 | |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 180 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 181 | uint64 FileTimeAsUint64(const FILETIME& ft) { |
| 182 | ULARGE_INTEGER u; |
| 183 | u.LowPart = ft.dwLowDateTime; |
| 184 | u.HighPart = ft.dwHighDateTime; |
| 185 | return u.QuadPart; |
| 186 | } |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 187 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 188 | |
| 189 | const struct append_case { |
| 190 | const wchar_t* path; |
| 191 | const wchar_t* ending; |
| 192 | const wchar_t* result; |
| 193 | } append_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 194 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 195 | {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"}, |
| 196 | {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"}, |
| 197 | {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"}, |
| 198 | {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"}, |
| 199 | {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"}, |
| 200 | {L"", L"path", L"\\path"}, |
| 201 | {L"", L"", L"\\"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 202 | #elif defined(OS_POSIX) |
| 203 | {L"/foo/bar", L"path", L"/foo/bar/path"}, |
| 204 | {L"/foo/bar/", L"path", L"/foo/bar/path"}, |
| 205 | {L"/foo/bar//", L"path", L"/foo/bar//path"}, |
| 206 | {L"/foo/bar/", L"", L"/foo/bar/"}, |
| 207 | {L"/foo/bar", L"", L"/foo/bar/"}, |
| 208 | {L"", L"path", L"/path"}, |
| 209 | {L"", L"", L"/"}, |
| 210 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 211 | }; |
| 212 | |
[email protected] | 1840cfc | 2010-02-26 15:11:55 | [diff] [blame] | 213 | #if defined(OS_WIN) |
| 214 | // This function is deprecated, but still used on Windows. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 215 | TEST_F(FileUtilTest, AppendToPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 216 | for (unsigned int i = 0; i < arraysize(append_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 217 | const append_case& value = append_cases[i]; |
| 218 | std::wstring result = value.path; |
| 219 | file_util::AppendToPath(&result, value.ending); |
| 220 | EXPECT_EQ(value.result, result); |
| 221 | } |
| 222 | |
| 223 | #ifdef NDEBUG |
| 224 | file_util::AppendToPath(NULL, L"path"); // asserts in debug mode |
| 225 | #endif |
| 226 | } |
[email protected] | 1840cfc | 2010-02-26 15:11:55 | [diff] [blame] | 227 | #endif // defined(OS_WIN) |
| 228 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 229 | static const struct filename_case { |
| 230 | const wchar_t* path; |
| 231 | const wchar_t* filename; |
| 232 | } filename_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 233 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 234 | {L"c:\\colon\\backslash", L"backslash"}, |
| 235 | {L"c:\\colon\\backslash\\", L""}, |
| 236 | {L"\\\\filename.exe", L"filename.exe"}, |
| 237 | {L"filename.exe", L"filename.exe"}, |
| 238 | {L"", L""}, |
| 239 | {L"\\\\\\", L""}, |
| 240 | {L"c:/colon/backslash", L"backslash"}, |
| 241 | {L"c:/colon/backslash/", L""}, |
| 242 | {L"//////", L""}, |
| 243 | {L"///filename.exe", L"filename.exe"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 244 | #elif defined(OS_POSIX) |
| 245 | {L"/foo/bar", L"bar"}, |
| 246 | {L"/foo/bar/", L""}, |
| 247 | {L"/filename.exe", L"filename.exe"}, |
| 248 | {L"filename.exe", L"filename.exe"}, |
| 249 | {L"", L""}, |
| 250 | {L"/", L""}, |
| 251 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 252 | }; |
| 253 | |
[email protected] | 8d19c7d | 2010-07-01 23:19:02 | [diff] [blame] | 254 | #if defined(OS_WIN) |
| 255 | // This function is deprecated on non-Windows. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 256 | TEST_F(FileUtilTest, GetFilenameFromPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 257 | for (unsigned int i = 0; i < arraysize(filename_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 258 | const filename_case& value = filename_cases[i]; |
| 259 | std::wstring result = file_util::GetFilenameFromPath(value.path); |
| 260 | EXPECT_EQ(value.filename, result); |
| 261 | } |
| 262 | } |
[email protected] | 8d19c7d | 2010-07-01 23:19:02 | [diff] [blame] | 263 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 264 | |
| 265 | // Test finding the file type from a path name |
| 266 | static const struct extension_case { |
| 267 | const wchar_t* path; |
| 268 | const wchar_t* extension; |
| 269 | } extension_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 270 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 271 | {L"C:\\colon\\backslash\\filename.extension", L"extension"}, |
| 272 | {L"C:\\colon\\backslash\\filename.", L""}, |
| 273 | {L"C:\\colon\\backslash\\filename", L""}, |
| 274 | {L"C:\\colon\\backslash\\", L""}, |
| 275 | {L"C:\\colon\\backslash.\\", L""}, |
| 276 | {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 277 | #elif defined(OS_POSIX) |
| 278 | {L"/foo/bar/filename.extension", L"extension"}, |
| 279 | {L"/foo/bar/filename.", L""}, |
| 280 | {L"/foo/bar/filename", L""}, |
| 281 | {L"/foo/bar/", L""}, |
| 282 | {L"/foo/bar./", L""}, |
| 283 | {L"/foo/bar/filename.extension.extension2", L"extension2"}, |
| 284 | {L".", L""}, |
| 285 | {L"..", L""}, |
| 286 | {L"./foo", L""}, |
| 287 | {L"./foo.extension", L"extension"}, |
| 288 | {L"/foo.extension1/bar.extension2", L"extension2"}, |
| 289 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 290 | }; |
| 291 | |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 292 | #if defined(OS_WIN) |
| 293 | // This function has been deprecated on non-Windows. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 294 | TEST_F(FileUtilTest, GetFileExtensionFromPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 295 | for (unsigned int i = 0; i < arraysize(extension_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 296 | const extension_case& ext = extension_cases[i]; |
| 297 | const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path); |
| 298 | EXPECT_EQ(ext.extension, fext); |
| 299 | } |
| 300 | } |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 301 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 302 | |
| 303 | // Test finding the directory component of a path |
| 304 | static const struct dir_case { |
| 305 | const wchar_t* full_path; |
| 306 | const wchar_t* directory; |
| 307 | } dir_cases[] = { |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 308 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 309 | {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"}, |
| 310 | {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"}, |
| 311 | {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"}, |
| 312 | {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"}, |
| 313 | {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"}, |
| 314 | {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."}, |
[email protected] | 2c8088a4 | 2009-10-15 05:00:25 | [diff] [blame] | 315 | {L"C:\\", L"C:\\"}, |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 316 | #elif defined(OS_POSIX) |
| 317 | {L"/foo/bar/gdi32.dll", L"/foo/bar"}, |
| 318 | {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"}, |
| 319 | {L"/foo/bar/", L"/foo/bar"}, |
| 320 | {L"/foo/bar//", L"/foo/bar"}, |
| 321 | {L"/foo/bar", L"/foo"}, |
| 322 | {L"/foo/bar./", L"/foo/bar."}, |
| 323 | {L"/", L"/"}, |
| 324 | {L".", L"."}, |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 325 | {L"..", L"."}, // yes, ".." technically lives in "." |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 326 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 327 | }; |
| 328 | |
[email protected] | a18f79a | 2010-02-23 12:52:11 | [diff] [blame] | 329 | #if defined(OS_WIN) |
| 330 | // This function is deprecated, and only exists on Windows anymore. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 331 | TEST_F(FileUtilTest, GetDirectoryFromPath) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 332 | for (unsigned int i = 0; i < arraysize(dir_cases); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 333 | const dir_case& dir = dir_cases[i]; |
| 334 | const std::wstring parent = |
| 335 | file_util::GetDirectoryFromPath(dir.full_path); |
| 336 | EXPECT_EQ(dir.directory, parent); |
| 337 | } |
| 338 | } |
[email protected] | a18f79a | 2010-02-23 12:52:11 | [diff] [blame] | 339 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 340 | |
[email protected] | 2ec79f7 | 2010-06-10 13:05:26 | [diff] [blame] | 341 | // Flaky, http://crbug.com/46246 |
[email protected] | 552b315 | 2010-06-10 12:40:52 | [diff] [blame] | 342 | TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 343 | // Create old file (that we don't want to count) |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 344 | FilePath old_file_name = |
| 345 | temp_dir_.path().Append(FILE_PATH_LITERAL("Old File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 346 | CreateTextFile(old_file_name, L"Just call me Mr. Creakybits"); |
| 347 | |
| 348 | // Age to perfection |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 349 | #if defined(OS_WIN) |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 350 | PlatformThread::Sleep(100); |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 351 | #elif defined(OS_POSIX) |
| 352 | // We need to wait at least one second here because the precision of |
| 353 | // file creation time is one second. |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 354 | PlatformThread::Sleep(1500); |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 355 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 356 | |
| 357 | // Establish our cutoff time |
[email protected] | 85786f9 | 2009-04-18 00:42:48 | [diff] [blame] | 358 | base::Time now(base::Time::NowFromSystemTime()); |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 359 | EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), now)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 360 | |
| 361 | // Create a new file (that we do want to count) |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 362 | FilePath new_file_name = |
| 363 | temp_dir_.path().Append(FILE_PATH_LITERAL("New File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 364 | CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah."); |
| 365 | |
| 366 | // We should see only the new file. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 367 | EXPECT_EQ(1, file_util::CountFilesCreatedAfter(temp_dir_.path(), now)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 368 | |
| 369 | // Delete new file, we should see no files after cutoff now |
| 370 | EXPECT_TRUE(file_util::Delete(new_file_name, false)); |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 371 | EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), now)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 372 | } |
| 373 | |
[email protected] | c2c132c6 | 2010-03-24 21:56:26 | [diff] [blame] | 374 | TEST_F(FileUtilTest, FileAndDirectorySize) { |
| 375 | // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize |
| 376 | // should return 53 bytes. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 377 | FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt")); |
[email protected] | c2c132c6 | 2010-03-24 21:56:26 | [diff] [blame] | 378 | CreateTextFile(file_01, L"12345678901234567890"); |
| 379 | int64 size_f1 = 0; |
| 380 | ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1)); |
| 381 | EXPECT_EQ(20ll, size_f1); |
| 382 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 383 | FilePath subdir_path = temp_dir_.path().Append(FPL("Level2")); |
[email protected] | c2c132c6 | 2010-03-24 21:56:26 | [diff] [blame] | 384 | file_util::CreateDirectory(subdir_path); |
| 385 | |
| 386 | FilePath file_02 = subdir_path.Append(FPL("The file 02.txt")); |
| 387 | CreateTextFile(file_02, L"123456789012345678901234567890"); |
| 388 | int64 size_f2 = 0; |
| 389 | ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2)); |
| 390 | EXPECT_EQ(30ll, size_f2); |
| 391 | |
| 392 | FilePath subsubdir_path = subdir_path.Append(FPL("Level3")); |
| 393 | file_util::CreateDirectory(subsubdir_path); |
| 394 | |
| 395 | FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt")); |
| 396 | CreateTextFile(file_03, L"123"); |
| 397 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 398 | int64 computed_size = file_util::ComputeDirectorySize(temp_dir_.path()); |
[email protected] | c2c132c6 | 2010-03-24 21:56:26 | [diff] [blame] | 399 | EXPECT_EQ(size_f1 + size_f2 + 3, computed_size); |
[email protected] | a04876b9 | 2010-06-11 22:53:43 | [diff] [blame] | 400 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 401 | computed_size = |
| 402 | file_util::ComputeFilesSize(temp_dir_.path(), FPL("The file*")); |
[email protected] | a04876b9 | 2010-06-11 22:53:43 | [diff] [blame] | 403 | EXPECT_EQ(size_f1, computed_size); |
| 404 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 405 | computed_size = file_util::ComputeFilesSize(temp_dir_.path(), FPL("bla*")); |
[email protected] | a04876b9 | 2010-06-11 22:53:43 | [diff] [blame] | 406 | EXPECT_EQ(0, computed_size); |
[email protected] | c2c132c6 | 2010-03-24 21:56:26 | [diff] [blame] | 407 | } |
| 408 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 409 | TEST_F(FileUtilTest, NormalizeFilePathBasic) { |
| 410 | // Create a directory under the test dir. Because we create it, |
| 411 | // we know it is not a link. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 412 | FilePath file_a_path = temp_dir_.path().Append(FPL("file_a")); |
| 413 | FilePath dir_path = temp_dir_.path().Append(FPL("dir")); |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 414 | FilePath file_b_path = dir_path.Append(FPL("file_b")); |
| 415 | file_util::CreateDirectory(dir_path); |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 416 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 417 | FilePath normalized_file_a_path, normalized_file_b_path; |
| 418 | ASSERT_FALSE(file_util::PathExists(file_a_path)); |
| 419 | ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path, |
| 420 | &normalized_file_a_path)) |
[email protected] | e7afe245 | 2010-08-22 16:19:13 | [diff] [blame] | 421 | << "NormalizeFilePath() should fail on nonexistent paths."; |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 422 | |
| 423 | CreateTextFile(file_a_path, bogus_content); |
| 424 | ASSERT_TRUE(file_util::PathExists(file_a_path)); |
| 425 | ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path, |
| 426 | &normalized_file_a_path)); |
| 427 | |
| 428 | CreateTextFile(file_b_path, bogus_content); |
| 429 | ASSERT_TRUE(file_util::PathExists(file_b_path)); |
| 430 | ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path, |
| 431 | &normalized_file_b_path)); |
| 432 | |
| 433 | // Beacuse this test created |dir_path|, we know it is not a link |
| 434 | // or junction. So, the real path of the directory holding file a |
| 435 | // must be the parent of the path holding file b. |
| 436 | ASSERT_TRUE(normalized_file_a_path.DirName() |
| 437 | .IsParent(normalized_file_b_path.DirName())); |
| 438 | } |
| 439 | |
| 440 | #if defined(OS_WIN) |
| 441 | |
| 442 | TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) { |
| 443 | // Build the following directory structure: |
| 444 | // |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 445 | // temp_dir |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 446 | // |-> base_a |
| 447 | // | |-> sub_a |
| 448 | // | |-> file.txt |
| 449 | // | |-> long_name___... (Very long name.) |
| 450 | // | |-> sub_long |
| 451 | // | |-> deep.txt |
| 452 | // |-> base_b |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 453 | // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a) |
| 454 | // |-> to_base_b (reparse point to temp_dir\base_b) |
| 455 | // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long) |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 456 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 457 | FilePath base_a = temp_dir_.path().Append(FPL("base_a")); |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 458 | ASSERT_TRUE(file_util::CreateDirectory(base_a)); |
| 459 | |
| 460 | FilePath sub_a = base_a.Append(FPL("sub_a")); |
| 461 | ASSERT_TRUE(file_util::CreateDirectory(sub_a)); |
| 462 | |
| 463 | FilePath file_txt = sub_a.Append(FPL("file.txt")); |
| 464 | CreateTextFile(file_txt, bogus_content); |
| 465 | |
| 466 | // Want a directory whose name is long enough to make the path to the file |
| 467 | // inside just under MAX_PATH chars. This will be used to test that when |
| 468 | // a junction expands to a path over MAX_PATH chars in length, |
| 469 | // NormalizeFilePath() fails without crashing. |
| 470 | FilePath sub_long_rel(FPL("sub_long")); |
| 471 | FilePath deep_txt(FPL("deep.txt")); |
| 472 | |
| 473 | int target_length = MAX_PATH; |
| 474 | target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'. |
| 475 | target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1); |
[email protected] | 552b315 | 2010-06-10 12:40:52 | [diff] [blame] | 476 | // Without making the path a bit shorter, CreateDirectory() fails. |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 477 | // the resulting path is still long enough to hit the failing case in |
| 478 | // NormalizePath(). |
| 479 | const int kCreateDirLimit = 4; |
| 480 | target_length -= kCreateDirLimit; |
| 481 | FilePath::StringType long_name_str = FPL("long_name_"); |
| 482 | long_name_str.resize(target_length, '_'); |
| 483 | |
| 484 | FilePath long_name = sub_a.Append(FilePath(long_name_str)); |
| 485 | FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt); |
| 486 | ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length()); |
| 487 | |
| 488 | FilePath sub_long = deep_file.DirName(); |
| 489 | ASSERT_TRUE(file_util::CreateDirectory(sub_long)); |
| 490 | CreateTextFile(deep_file, bogus_content); |
| 491 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 492 | FilePath base_b = temp_dir_.path().Append(FPL("base_b")); |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 493 | ASSERT_TRUE(file_util::CreateDirectory(base_b)); |
| 494 | |
| 495 | FilePath to_sub_a = base_b.Append(FPL("to_sub_a")); |
| 496 | ASSERT_TRUE(file_util::CreateDirectory(to_sub_a)); |
| 497 | ScopedHandle reparse_to_sub_a( |
| 498 | ::CreateFile(to_sub_a.value().c_str(), |
| 499 | FILE_ALL_ACCESS, |
| 500 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 501 | NULL, |
| 502 | OPEN_EXISTING, |
| 503 | FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. |
| 504 | NULL)); |
[email protected] | 094f976 | 2010-08-03 03:51:56 | [diff] [blame] | 505 | ASSERT_TRUE(reparse_to_sub_a.IsValid()); |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 506 | ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a)); |
| 507 | |
| 508 | FilePath to_base_b = base_b.Append(FPL("to_base_b")); |
| 509 | ASSERT_TRUE(file_util::CreateDirectory(to_base_b)); |
| 510 | ScopedHandle reparse_to_base_b( |
| 511 | ::CreateFile(to_base_b.value().c_str(), |
| 512 | FILE_ALL_ACCESS, |
| 513 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 514 | NULL, |
| 515 | OPEN_EXISTING, |
| 516 | FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. |
| 517 | NULL)); |
[email protected] | 094f976 | 2010-08-03 03:51:56 | [diff] [blame] | 518 | ASSERT_TRUE(reparse_to_base_b.IsValid()); |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 519 | ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b)); |
| 520 | |
| 521 | FilePath to_sub_long = base_b.Append(FPL("to_sub_long")); |
| 522 | ASSERT_TRUE(file_util::CreateDirectory(to_sub_long)); |
| 523 | ScopedHandle reparse_to_sub_long( |
| 524 | ::CreateFile(to_sub_long.value().c_str(), |
| 525 | FILE_ALL_ACCESS, |
| 526 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 527 | NULL, |
| 528 | OPEN_EXISTING, |
| 529 | FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. |
| 530 | NULL)); |
[email protected] | 094f976 | 2010-08-03 03:51:56 | [diff] [blame] | 531 | ASSERT_TRUE(reparse_to_sub_long.IsValid()); |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 532 | ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long)); |
| 533 | |
| 534 | // Normalize a junction free path: base_a\sub_a\file.txt . |
| 535 | FilePath normalized_path; |
| 536 | ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path)); |
| 537 | ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str()); |
| 538 | |
| 539 | // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude |
| 540 | // the junction to_sub_a. |
| 541 | ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")), |
| 542 | &normalized_path)); |
| 543 | ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str()); |
| 544 | |
| 545 | // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be |
| 546 | // normalized to exclude junctions to_base_b and to_sub_a . |
| 547 | ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b")) |
| 548 | .Append(FPL("to_base_b")) |
| 549 | .Append(FPL("to_sub_a")) |
| 550 | .Append(FPL("file.txt")), |
| 551 | &normalized_path)); |
| 552 | ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str()); |
| 553 | |
| 554 | // A long enough path will cause NormalizeFilePath() to fail. Make a long |
| 555 | // path using to_base_b many times, and check that paths long enough to fail |
| 556 | // do not cause a crash. |
| 557 | FilePath long_path = base_b; |
| 558 | const int kLengthLimit = MAX_PATH + 200; |
| 559 | while (long_path.value().length() <= kLengthLimit) { |
| 560 | long_path = long_path.Append(FPL("to_base_b")); |
| 561 | } |
| 562 | long_path = long_path.Append(FPL("to_sub_a")) |
| 563 | .Append(FPL("file.txt")); |
| 564 | |
| 565 | ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path)); |
| 566 | |
| 567 | // Normalizing the junction to deep.txt should fail, because the expanded |
| 568 | // path to deep.txt is longer than MAX_PATH. |
| 569 | ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt), |
| 570 | &normalized_path)); |
| 571 | |
| 572 | // Delete the reparse points, and see that NormalizeFilePath() fails |
| 573 | // to traverse them. |
| 574 | ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a)); |
| 575 | ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b)); |
| 576 | ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long)); |
| 577 | |
| 578 | ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")), |
| 579 | &normalized_path)); |
| 580 | } |
| 581 | |
| 582 | #endif // defined(OS_WIN) |
| 583 | |
| 584 | // The following test of NormalizeFilePath() require that we create a symlink. |
| 585 | // This can not be done on windows before vista. On vista, creating a symlink |
| 586 | // requires privilege "SeCreateSymbolicLinkPrivilege". |
| 587 | // TODO(skerner): Investigate the possibility of giving base_unittests the |
| 588 | // privileges required to create a symlink. |
| 589 | #if defined(OS_POSIX) |
| 590 | |
| 591 | bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) { |
| 592 | return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0); |
| 593 | } |
| 594 | |
| 595 | TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { |
| 596 | FilePath normalized_path; |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 597 | |
| 598 | // Link one file to another. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 599 | FilePath link_from = temp_dir_.path().Append(FPL("from_file")); |
| 600 | FilePath link_to = temp_dir_.path().Append(FPL("to_file")); |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 601 | CreateTextFile(link_to, bogus_content); |
| 602 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 603 | ASSERT_TRUE(MakeSymlink(link_to, link_from)) |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 604 | << "Failed to create file symlink."; |
| 605 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 606 | // Check that NormalizeFilePath sees the link. |
| 607 | ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path)); |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 608 | ASSERT_TRUE(link_to != link_from); |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 609 | ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); |
| 610 | ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 611 | |
| 612 | // Link to a directory. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 613 | link_from = temp_dir_.path().Append(FPL("from_dir")); |
| 614 | link_to = temp_dir_.path().Append(FPL("to_dir")); |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 615 | file_util::CreateDirectory(link_to); |
| 616 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 617 | ASSERT_TRUE(MakeSymlink(link_to, link_from)) |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 618 | << "Failed to create directory symlink."; |
| 619 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 620 | ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)) |
| 621 | << "Links to directories should return false."; |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 622 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 623 | // Test that a loop in the links causes NormalizeFilePath() to return false. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 624 | link_from = temp_dir_.path().Append(FPL("link_a")); |
| 625 | link_to = temp_dir_.path().Append(FPL("link_b")); |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 626 | ASSERT_TRUE(MakeSymlink(link_to, link_from)) |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 627 | << "Failed to create loop symlink a."; |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 628 | ASSERT_TRUE(MakeSymlink(link_from, link_to)) |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 629 | << "Failed to create loop symlink b."; |
| 630 | |
| 631 | // Infinite loop! |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 632 | ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)); |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 633 | } |
| 634 | #endif // defined(OS_POSIX) |
| 635 | |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 636 | TEST_F(FileUtilTest, DeleteNonExistent) { |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 637 | FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar"); |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 638 | ASSERT_FALSE(file_util::PathExists(non_existent)); |
[email protected] | 3a51cfdd | 2010-05-07 00:05:36 | [diff] [blame] | 639 | |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 640 | EXPECT_TRUE(file_util::Delete(non_existent, false)); |
| 641 | ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 642 | EXPECT_TRUE(file_util::Delete(non_existent, true)); |
| 643 | ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 644 | } |
| 645 | |
| 646 | TEST_F(FileUtilTest, DeleteFile) { |
| 647 | // Create a file |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 648 | FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt")); |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 649 | CreateTextFile(file_name, bogus_content); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 650 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 651 | |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 652 | // Make sure it's deleted |
| 653 | EXPECT_TRUE(file_util::Delete(file_name, false)); |
| 654 | EXPECT_FALSE(file_util::PathExists(file_name)); |
[email protected] | 3a51cfdd | 2010-05-07 00:05:36 | [diff] [blame] | 655 | |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 656 | // Test recursive case, create a new file |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 657 | file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt")); |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 658 | CreateTextFile(file_name, bogus_content); |
| 659 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 660 | |
| 661 | // Make sure it's deleted |
| 662 | EXPECT_TRUE(file_util::Delete(file_name, true)); |
| 663 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 664 | } |
| 665 | |
| 666 | #if defined(OS_WIN) |
| 667 | // Tests that the Delete function works for wild cards, especially |
| 668 | // with the recursion flag. Also coincidentally tests PathExists. |
| 669 | // TODO(erikkay): see if anyone's actually using this feature of the API |
| 670 | TEST_F(FileUtilTest, DeleteWildCard) { |
| 671 | // Create a file and a directory |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 672 | FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt")); |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 673 | CreateTextFile(file_name, bogus_content); |
| 674 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 675 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 676 | FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir")); |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 677 | file_util::CreateDirectory(subdir_path); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 678 | ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 679 | |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 680 | // Create the wildcard path |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 681 | FilePath directory_contents = temp_dir_.path(); |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 682 | directory_contents = directory_contents.Append(FPL("*")); |
| 683 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 684 | // Delete non-recursively and check that only the file is deleted |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 685 | EXPECT_TRUE(file_util::Delete(directory_contents, false)); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 686 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 687 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
[email protected] | dde46b6 | 2010-05-06 21:56:40 | [diff] [blame] | 688 | |
[email protected] | 3a51cfdd | 2010-05-07 00:05:36 | [diff] [blame] | 689 | // Delete recursively and make sure all contents are deleted |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 690 | EXPECT_TRUE(file_util::Delete(directory_contents, true)); |
[email protected] | dde46b6 | 2010-05-06 21:56:40 | [diff] [blame] | 691 | EXPECT_FALSE(file_util::PathExists(file_name)); |
[email protected] | 3a51cfdd | 2010-05-07 00:05:36 | [diff] [blame] | 692 | EXPECT_FALSE(file_util::PathExists(subdir_path)); |
[email protected] | dde46b6 | 2010-05-06 21:56:40 | [diff] [blame] | 693 | } |
| 694 | |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 695 | // TODO(erikkay): see if anyone's actually using this feature of the API |
| 696 | TEST_F(FileUtilTest, DeleteNonExistantWildCard) { |
| 697 | // Create a file and a directory |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 698 | FilePath subdir_path = |
| 699 | temp_dir_.path().Append(FPL("DeleteNonExistantWildCard")); |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 700 | file_util::CreateDirectory(subdir_path); |
| 701 | ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 702 | |
| 703 | // Create the wildcard path |
| 704 | FilePath directory_contents = subdir_path; |
| 705 | directory_contents = directory_contents.Append(FPL("*")); |
| 706 | |
| 707 | // Delete non-recursively and check nothing got deleted |
| 708 | EXPECT_TRUE(file_util::Delete(directory_contents, false)); |
| 709 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 710 | |
| 711 | // Delete recursively and check nothing got deleted |
| 712 | EXPECT_TRUE(file_util::Delete(directory_contents, true)); |
| 713 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 714 | } |
| 715 | #endif |
| 716 | |
| 717 | // Tests non-recursive Delete() for a directory. |
| 718 | TEST_F(FileUtilTest, DeleteDirNonRecursive) { |
| 719 | // Create a subdirectory and put a file and two directories inside. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 720 | FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive")); |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 721 | file_util::CreateDirectory(test_subdir); |
| 722 | ASSERT_TRUE(file_util::PathExists(test_subdir)); |
| 723 | |
| 724 | FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt")); |
| 725 | CreateTextFile(file_name, bogus_content); |
| 726 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 727 | |
| 728 | FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); |
| 729 | file_util::CreateDirectory(subdir_path1); |
| 730 | ASSERT_TRUE(file_util::PathExists(subdir_path1)); |
| 731 | |
| 732 | FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); |
| 733 | file_util::CreateDirectory(subdir_path2); |
| 734 | ASSERT_TRUE(file_util::PathExists(subdir_path2)); |
| 735 | |
| 736 | // Delete non-recursively and check that the empty dir got deleted |
| 737 | EXPECT_TRUE(file_util::Delete(subdir_path2, false)); |
| 738 | EXPECT_FALSE(file_util::PathExists(subdir_path2)); |
| 739 | |
| 740 | // Delete non-recursively and check that nothing got deleted |
| 741 | EXPECT_FALSE(file_util::Delete(test_subdir, false)); |
| 742 | EXPECT_TRUE(file_util::PathExists(test_subdir)); |
| 743 | EXPECT_TRUE(file_util::PathExists(file_name)); |
| 744 | EXPECT_TRUE(file_util::PathExists(subdir_path1)); |
| 745 | } |
| 746 | |
| 747 | // Tests recursive Delete() for a directory. |
| 748 | TEST_F(FileUtilTest, DeleteDirRecursive) { |
| 749 | // Create a subdirectory and put a file and two directories inside. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 750 | FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive")); |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 751 | file_util::CreateDirectory(test_subdir); |
| 752 | ASSERT_TRUE(file_util::PathExists(test_subdir)); |
| 753 | |
| 754 | FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt")); |
| 755 | CreateTextFile(file_name, bogus_content); |
| 756 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 757 | |
| 758 | FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); |
| 759 | file_util::CreateDirectory(subdir_path1); |
| 760 | ASSERT_TRUE(file_util::PathExists(subdir_path1)); |
| 761 | |
| 762 | FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); |
| 763 | file_util::CreateDirectory(subdir_path2); |
| 764 | ASSERT_TRUE(file_util::PathExists(subdir_path2)); |
| 765 | |
| 766 | // Delete recursively and check that the empty dir got deleted |
| 767 | EXPECT_TRUE(file_util::Delete(subdir_path2, true)); |
| 768 | EXPECT_FALSE(file_util::PathExists(subdir_path2)); |
| 769 | |
| 770 | // Delete recursively and check that everything got deleted |
| 771 | EXPECT_TRUE(file_util::Delete(test_subdir, true)); |
| 772 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 773 | EXPECT_FALSE(file_util::PathExists(subdir_path1)); |
| 774 | EXPECT_FALSE(file_util::PathExists(test_subdir)); |
| 775 | } |
| 776 | |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 777 | TEST_F(FileUtilTest, MoveFileNew) { |
| 778 | // Create a file |
| 779 | FilePath file_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 780 | temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 781 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 782 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 783 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 784 | // The destination. |
| 785 | FilePath file_name_to = temp_dir_.path().Append( |
| 786 | FILE_PATH_LITERAL("Move_Test_File_Destination.txt")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 787 | ASSERT_FALSE(file_util::PathExists(file_name_to)); |
| 788 | |
| 789 | EXPECT_TRUE(file_util::Move(file_name_from, file_name_to)); |
| 790 | |
| 791 | // Check everything has been moved. |
| 792 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 793 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 794 | } |
| 795 | |
| 796 | TEST_F(FileUtilTest, MoveFileExists) { |
| 797 | // Create a file |
| 798 | FilePath file_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 799 | temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 800 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 801 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 802 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 803 | // The destination name. |
| 804 | FilePath file_name_to = temp_dir_.path().Append( |
| 805 | FILE_PATH_LITERAL("Move_Test_File_Destination.txt")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 806 | CreateTextFile(file_name_to, L"Old file content"); |
| 807 | ASSERT_TRUE(file_util::PathExists(file_name_to)); |
| 808 | |
| 809 | EXPECT_TRUE(file_util::Move(file_name_from, file_name_to)); |
| 810 | |
| 811 | // Check everything has been moved. |
| 812 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 813 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 814 | EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to)); |
| 815 | } |
| 816 | |
| 817 | TEST_F(FileUtilTest, MoveFileDirExists) { |
| 818 | // Create a file |
| 819 | FilePath file_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 820 | temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 821 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 822 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 823 | |
| 824 | // The destination directory |
| 825 | FilePath dir_name_to = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 826 | temp_dir_.path().Append(FILE_PATH_LITERAL("Destination")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 827 | file_util::CreateDirectory(dir_name_to); |
| 828 | ASSERT_TRUE(file_util::PathExists(dir_name_to)); |
| 829 | |
| 830 | EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to)); |
| 831 | } |
| 832 | |
| 833 | |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 834 | TEST_F(FileUtilTest, MoveNew) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 835 | // Create a directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 836 | FilePath dir_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 837 | temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir")); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 838 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 839 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 840 | |
| 841 | // Create a file under the directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 842 | FilePath file_name_from = |
| 843 | dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 844 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 845 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 846 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 847 | // Move the directory. |
| 848 | FilePath dir_name_to = |
| 849 | temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir")); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 850 | FilePath file_name_to = |
| 851 | dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 852 | |
| 853 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 854 | |
| 855 | EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to)); |
| 856 | |
| 857 | // Check everything has been moved. |
| 858 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 859 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 860 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 861 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 862 | } |
| 863 | |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 864 | TEST_F(FileUtilTest, MoveExist) { |
| 865 | // Create a directory |
| 866 | FilePath dir_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 867 | temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir")); |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 868 | file_util::CreateDirectory(dir_name_from); |
| 869 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 870 | |
| 871 | // Create a file under the directory |
| 872 | FilePath file_name_from = |
| 873 | dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 874 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 875 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 876 | |
| 877 | // Move the directory |
| 878 | FilePath dir_name_exists = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 879 | temp_dir_.path().Append(FILE_PATH_LITERAL("Destination")); |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 880 | |
| 881 | FilePath dir_name_to = |
| 882 | dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir")); |
| 883 | FilePath file_name_to = |
| 884 | dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 885 | |
| 886 | // Create the destination directory. |
| 887 | file_util::CreateDirectory(dir_name_exists); |
| 888 | ASSERT_TRUE(file_util::PathExists(dir_name_exists)); |
| 889 | |
| 890 | EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to)); |
| 891 | |
| 892 | // Check everything has been moved. |
| 893 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 894 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 895 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 896 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 897 | } |
| 898 | |
| 899 | TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 900 | // Create a directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 901 | FilePath dir_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 902 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 903 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 904 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 905 | |
| 906 | // Create a file under the directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 907 | FilePath file_name_from = |
| 908 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 909 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 910 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 911 | |
| 912 | // Create a subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 913 | FilePath subdir_name_from = |
| 914 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 915 | file_util::CreateDirectory(subdir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 916 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 917 | |
| 918 | // Create a file under the subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 919 | FilePath file_name2_from = |
| 920 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 921 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 922 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 923 | |
| 924 | // Copy the directory recursively. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 925 | FilePath dir_name_to = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 926 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 927 | FilePath file_name_to = |
| 928 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 929 | FilePath subdir_name_to = |
| 930 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 931 | FilePath file_name2_to = |
| 932 | subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 933 | |
| 934 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 935 | |
| 936 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true)); |
| 937 | |
| 938 | // Check everything has been copied. |
| 939 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 940 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 941 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 942 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 943 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 944 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 945 | EXPECT_TRUE(file_util::PathExists(subdir_name_to)); |
| 946 | EXPECT_TRUE(file_util::PathExists(file_name2_to)); |
| 947 | } |
| 948 | |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 949 | TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) { |
| 950 | // Create a directory. |
| 951 | FilePath dir_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 952 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 953 | file_util::CreateDirectory(dir_name_from); |
| 954 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 955 | |
| 956 | // Create a file under the directory. |
| 957 | FilePath file_name_from = |
| 958 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 959 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 960 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 961 | |
| 962 | // Create a subdirectory. |
| 963 | FilePath subdir_name_from = |
| 964 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 965 | file_util::CreateDirectory(subdir_name_from); |
| 966 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 967 | |
| 968 | // Create a file under the subdirectory. |
| 969 | FilePath file_name2_from = |
| 970 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 971 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 972 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 973 | |
| 974 | // Copy the directory recursively. |
| 975 | FilePath dir_name_exists = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 976 | temp_dir_.path().Append(FILE_PATH_LITERAL("Destination")); |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 977 | |
| 978 | FilePath dir_name_to = |
| 979 | dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 980 | FilePath file_name_to = |
| 981 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 982 | FilePath subdir_name_to = |
| 983 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 984 | FilePath file_name2_to = |
| 985 | subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 986 | |
| 987 | // Create the destination directory. |
| 988 | file_util::CreateDirectory(dir_name_exists); |
| 989 | ASSERT_TRUE(file_util::PathExists(dir_name_exists)); |
| 990 | |
| 991 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true)); |
| 992 | |
| 993 | // Check everything has been copied. |
| 994 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 995 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 996 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 997 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 998 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 999 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 1000 | EXPECT_TRUE(file_util::PathExists(subdir_name_to)); |
| 1001 | EXPECT_TRUE(file_util::PathExists(file_name2_to)); |
| 1002 | } |
| 1003 | |
| 1004 | TEST_F(FileUtilTest, CopyDirectoryNew) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1005 | // Create a directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1006 | FilePath dir_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1007 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1008 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1009 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 1010 | |
| 1011 | // Create a file under the directory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1012 | FilePath file_name_from = |
| 1013 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1014 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 1015 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 1016 | |
| 1017 | // Create a subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1018 | FilePath subdir_name_from = |
| 1019 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 1020 | file_util::CreateDirectory(subdir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1021 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 1022 | |
| 1023 | // Create a file under the subdirectory. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1024 | FilePath file_name2_from = |
| 1025 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1026 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 1027 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 1028 | |
| 1029 | // Copy the directory not recursively. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1030 | FilePath dir_name_to = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1031 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1032 | FilePath file_name_to = |
| 1033 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 1034 | FilePath subdir_name_to = |
| 1035 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1036 | |
| 1037 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 1038 | |
| 1039 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false)); |
| 1040 | |
| 1041 | // Check everything has been copied. |
| 1042 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 1043 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 1044 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 1045 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 1046 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 1047 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 1048 | EXPECT_FALSE(file_util::PathExists(subdir_name_to)); |
| 1049 | } |
| 1050 | |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 1051 | TEST_F(FileUtilTest, CopyDirectoryExists) { |
| 1052 | // Create a directory. |
| 1053 | FilePath dir_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1054 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 1055 | file_util::CreateDirectory(dir_name_from); |
| 1056 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 1057 | |
| 1058 | // Create a file under the directory. |
| 1059 | FilePath file_name_from = |
| 1060 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 1061 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 1062 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 1063 | |
| 1064 | // Create a subdirectory. |
| 1065 | FilePath subdir_name_from = |
| 1066 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 1067 | file_util::CreateDirectory(subdir_name_from); |
| 1068 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 1069 | |
| 1070 | // Create a file under the subdirectory. |
| 1071 | FilePath file_name2_from = |
| 1072 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 1073 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 1074 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 1075 | |
| 1076 | // Copy the directory not recursively. |
| 1077 | FilePath dir_name_to = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1078 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 1079 | FilePath file_name_to = |
| 1080 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 1081 | FilePath subdir_name_to = |
| 1082 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 1083 | |
| 1084 | // Create the destination directory. |
| 1085 | file_util::CreateDirectory(dir_name_to); |
| 1086 | ASSERT_TRUE(file_util::PathExists(dir_name_to)); |
| 1087 | |
| 1088 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false)); |
| 1089 | |
| 1090 | // Check everything has been copied. |
| 1091 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 1092 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 1093 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 1094 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 1095 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 1096 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 1097 | EXPECT_FALSE(file_util::PathExists(subdir_name_to)); |
| 1098 | } |
| 1099 | |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 1100 | TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) { |
| 1101 | // Create a file |
| 1102 | FilePath file_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1103 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 1104 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 1105 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 1106 | |
| 1107 | // The destination name |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1108 | FilePath file_name_to = temp_dir_.path().Append( |
| 1109 | FILE_PATH_LITERAL("Copy_Test_File_Destination.txt")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 1110 | ASSERT_FALSE(file_util::PathExists(file_name_to)); |
| 1111 | |
| 1112 | EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true)); |
| 1113 | |
| 1114 | // Check the has been copied |
| 1115 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 1116 | } |
| 1117 | |
| 1118 | TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) { |
| 1119 | // Create a file |
| 1120 | FilePath file_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1121 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 1122 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 1123 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 1124 | |
| 1125 | // The destination name |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1126 | FilePath file_name_to = temp_dir_.path().Append( |
| 1127 | FILE_PATH_LITERAL("Copy_Test_File_Destination.txt")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 1128 | CreateTextFile(file_name_to, L"Old file content"); |
| 1129 | ASSERT_TRUE(file_util::PathExists(file_name_to)); |
| 1130 | |
| 1131 | EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true)); |
| 1132 | |
| 1133 | // Check the has been copied |
| 1134 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 1135 | EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to)); |
| 1136 | } |
| 1137 | |
| 1138 | TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) { |
| 1139 | // Create a file |
| 1140 | FilePath file_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1141 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 1142 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 1143 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 1144 | |
| 1145 | // The destination |
| 1146 | FilePath dir_name_to = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1147 | temp_dir_.path().Append(FILE_PATH_LITERAL("Destination")); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 1148 | file_util::CreateDirectory(dir_name_to); |
| 1149 | ASSERT_TRUE(file_util::PathExists(dir_name_to)); |
| 1150 | FilePath file_name_to = |
| 1151 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 1152 | |
| 1153 | EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true)); |
| 1154 | |
| 1155 | // Check the has been copied |
| 1156 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 1157 | } |
| 1158 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1159 | TEST_F(FileUtilTest, CopyFile) { |
| 1160 | // Create a directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1161 | FilePath dir_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1162 | temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1163 | file_util::CreateDirectory(dir_name_from); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1164 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 1165 | |
| 1166 | // Create a file under the directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1167 | FilePath file_name_from = |
| 1168 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1169 | const std::wstring file_contents(L"Gooooooooooooooooooooogle"); |
| 1170 | CreateTextFile(file_name_from, file_contents); |
| 1171 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 1172 | |
| 1173 | // Copy the file. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1174 | FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1175 | ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file)); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 1176 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1177 | // Copy the file to another location using '..' in the path. |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 1178 | FilePath dest_file2(dir_name_from); |
| 1179 | dest_file2 = dest_file2.AppendASCII(".."); |
| 1180 | dest_file2 = dest_file2.AppendASCII("DestFile.txt"); |
| 1181 | ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2)); |
| 1182 | |
| 1183 | FilePath dest_file2_test(dir_name_from); |
| 1184 | dest_file2_test = dest_file2_test.DirName(); |
| 1185 | dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1186 | |
| 1187 | // Check everything has been copied. |
| 1188 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 1189 | EXPECT_TRUE(file_util::PathExists(dest_file)); |
| 1190 | const std::wstring read_contents = ReadTextFile(dest_file); |
| 1191 | EXPECT_EQ(file_contents, read_contents); |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 1192 | EXPECT_TRUE(file_util::PathExists(dest_file2_test)); |
| 1193 | EXPECT_TRUE(file_util::PathExists(dest_file2)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1194 | } |
| 1195 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1196 | // TODO(erikkay): implement |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 1197 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1198 | TEST_F(FileUtilTest, GetFileCreationLocalTime) { |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1199 | FilePath file_name = temp_dir_.path().Append(L"Test File.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1200 | |
| 1201 | SYSTEMTIME start_time; |
| 1202 | GetLocalTime(&start_time); |
| 1203 | Sleep(100); |
| 1204 | CreateTextFile(file_name, L"New file!"); |
| 1205 | Sleep(100); |
| 1206 | SYSTEMTIME end_time; |
| 1207 | GetLocalTime(&end_time); |
| 1208 | |
| 1209 | SYSTEMTIME file_creation_time; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1210 | file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1211 | |
| 1212 | FILETIME start_filetime; |
| 1213 | SystemTimeToFileTime(&start_time, &start_filetime); |
| 1214 | FILETIME end_filetime; |
| 1215 | SystemTimeToFileTime(&end_time, &end_filetime); |
| 1216 | FILETIME file_creation_filetime; |
| 1217 | SystemTimeToFileTime(&file_creation_time, &file_creation_filetime); |
| 1218 | |
| 1219 | EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) << |
| 1220 | "start time: " << FileTimeAsUint64(start_filetime) << ", " << |
| 1221 | "creation time: " << FileTimeAsUint64(file_creation_filetime); |
| 1222 | |
| 1223 | EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) << |
| 1224 | "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " << |
| 1225 | "end time: " << FileTimeAsUint64(end_filetime); |
| 1226 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1227 | ASSERT_TRUE(DeleteFile(file_name.value().c_str())); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1228 | } |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1229 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1230 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 1231 | // file_util winds up using autoreleased objects on the Mac, so this needs |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1232 | // to be a PlatformTest. |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 1233 | typedef PlatformTest ReadOnlyFileUtilTest; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1234 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 1235 | TEST_F(ReadOnlyFileUtilTest, ContentsEqual) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1236 | FilePath data_dir; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1237 | ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1238 | data_dir = data_dir.Append(FILE_PATH_LITERAL("base")) |
| 1239 | .Append(FILE_PATH_LITERAL("data")) |
| 1240 | .Append(FILE_PATH_LITERAL("file_util_unittest")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1241 | ASSERT_TRUE(file_util::PathExists(data_dir)); |
| 1242 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1243 | FilePath original_file = |
| 1244 | data_dir.Append(FILE_PATH_LITERAL("original.txt")); |
| 1245 | FilePath same_file = |
| 1246 | data_dir.Append(FILE_PATH_LITERAL("same.txt")); |
| 1247 | FilePath same_length_file = |
| 1248 | data_dir.Append(FILE_PATH_LITERAL("same_length.txt")); |
| 1249 | FilePath different_file = |
| 1250 | data_dir.Append(FILE_PATH_LITERAL("different.txt")); |
| 1251 | FilePath different_first_file = |
| 1252 | data_dir.Append(FILE_PATH_LITERAL("different_first.txt")); |
| 1253 | FilePath different_last_file = |
| 1254 | data_dir.Append(FILE_PATH_LITERAL("different_last.txt")); |
| 1255 | FilePath empty1_file = |
| 1256 | data_dir.Append(FILE_PATH_LITERAL("empty1.txt")); |
| 1257 | FilePath empty2_file = |
| 1258 | data_dir.Append(FILE_PATH_LITERAL("empty2.txt")); |
| 1259 | FilePath shortened_file = |
| 1260 | data_dir.Append(FILE_PATH_LITERAL("shortened.txt")); |
| 1261 | FilePath binary_file = |
| 1262 | data_dir.Append(FILE_PATH_LITERAL("binary_file.bin")); |
| 1263 | FilePath binary_file_same = |
| 1264 | data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin")); |
| 1265 | FilePath binary_file_diff = |
| 1266 | data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1267 | |
| 1268 | EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file)); |
| 1269 | EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file)); |
| 1270 | EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file)); |
| 1271 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file)); |
[email protected] | 3f4b571 | 2009-12-01 22:14:22 | [diff] [blame] | 1272 | EXPECT_FALSE(file_util::ContentsEqual( |
| 1273 | FilePath(FILE_PATH_LITERAL("bogusname")), |
| 1274 | FilePath(FILE_PATH_LITERAL("bogusname")))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1275 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file)); |
| 1276 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file)); |
| 1277 | EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file)); |
| 1278 | EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file)); |
| 1279 | EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file)); |
| 1280 | EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same)); |
| 1281 | EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff)); |
| 1282 | } |
| 1283 | |
[email protected] | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 1284 | TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) { |
| 1285 | FilePath data_dir; |
| 1286 | ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
| 1287 | data_dir = data_dir.Append(FILE_PATH_LITERAL("base")) |
| 1288 | .Append(FILE_PATH_LITERAL("data")) |
| 1289 | .Append(FILE_PATH_LITERAL("file_util_unittest")); |
| 1290 | ASSERT_TRUE(file_util::PathExists(data_dir)); |
| 1291 | |
| 1292 | FilePath original_file = |
| 1293 | data_dir.Append(FILE_PATH_LITERAL("original.txt")); |
| 1294 | FilePath same_file = |
| 1295 | data_dir.Append(FILE_PATH_LITERAL("same.txt")); |
| 1296 | FilePath crlf_file = |
| 1297 | data_dir.Append(FILE_PATH_LITERAL("crlf.txt")); |
| 1298 | FilePath shortened_file = |
| 1299 | data_dir.Append(FILE_PATH_LITERAL("shortened.txt")); |
| 1300 | FilePath different_file = |
| 1301 | data_dir.Append(FILE_PATH_LITERAL("different.txt")); |
| 1302 | FilePath different_first_file = |
| 1303 | data_dir.Append(FILE_PATH_LITERAL("different_first.txt")); |
| 1304 | FilePath different_last_file = |
| 1305 | data_dir.Append(FILE_PATH_LITERAL("different_last.txt")); |
| 1306 | FilePath first1_file = |
| 1307 | data_dir.Append(FILE_PATH_LITERAL("first1.txt")); |
| 1308 | FilePath first2_file = |
| 1309 | data_dir.Append(FILE_PATH_LITERAL("first2.txt")); |
| 1310 | FilePath empty1_file = |
| 1311 | data_dir.Append(FILE_PATH_LITERAL("empty1.txt")); |
| 1312 | FilePath empty2_file = |
| 1313 | data_dir.Append(FILE_PATH_LITERAL("empty2.txt")); |
| 1314 | FilePath blank_line_file = |
| 1315 | data_dir.Append(FILE_PATH_LITERAL("blank_line.txt")); |
| 1316 | FilePath blank_line_crlf_file = |
| 1317 | data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt")); |
| 1318 | |
| 1319 | EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file)); |
| 1320 | EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file)); |
| 1321 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file)); |
| 1322 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file)); |
| 1323 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, |
| 1324 | different_first_file)); |
| 1325 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, |
| 1326 | different_last_file)); |
| 1327 | EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file)); |
| 1328 | EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file)); |
| 1329 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file)); |
| 1330 | EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file, |
| 1331 | blank_line_crlf_file)); |
| 1332 | } |
| 1333 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1334 | // We don't need equivalent functionality outside of Windows. |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 1335 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1336 | TEST_F(FileUtilTest, ResolveShortcutTest) { |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1337 | FilePath target_file = temp_dir_.path().Append(L"Target.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1338 | CreateTextFile(target_file, L"This is the target."); |
| 1339 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1340 | FilePath link_file = temp_dir_.path().Append(L"Link.lnk"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1341 | |
| 1342 | HRESULT result; |
| 1343 | IShellLink *shell = NULL; |
| 1344 | IPersistFile *persist = NULL; |
| 1345 | |
| 1346 | CoInitialize(NULL); |
| 1347 | // Temporarily create a shortcut for test |
| 1348 | result = CoCreateInstance(CLSID_ShellLink, NULL, |
| 1349 | CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 1350 | reinterpret_cast<LPVOID*>(&shell)); |
| 1351 | EXPECT_TRUE(SUCCEEDED(result)); |
| 1352 | result = shell->QueryInterface(IID_IPersistFile, |
| 1353 | reinterpret_cast<LPVOID*>(&persist)); |
| 1354 | EXPECT_TRUE(SUCCEEDED(result)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1355 | result = shell->SetPath(target_file.value().c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1356 | EXPECT_TRUE(SUCCEEDED(result)); |
| 1357 | result = shell->SetDescription(L"ResolveShortcutTest"); |
| 1358 | EXPECT_TRUE(SUCCEEDED(result)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1359 | result = persist->Save(link_file.value().c_str(), TRUE); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1360 | EXPECT_TRUE(SUCCEEDED(result)); |
| 1361 | if (persist) |
| 1362 | persist->Release(); |
| 1363 | if (shell) |
| 1364 | shell->Release(); |
| 1365 | |
| 1366 | bool is_solved; |
[email protected] | fd061a6 | 2009-08-25 01:51:44 | [diff] [blame] | 1367 | is_solved = file_util::ResolveShortcut(&link_file); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1368 | EXPECT_TRUE(is_solved); |
| 1369 | std::wstring contents; |
[email protected] | fd061a6 | 2009-08-25 01:51:44 | [diff] [blame] | 1370 | contents = ReadTextFile(link_file); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1371 | EXPECT_EQ(L"This is the target.", contents); |
| 1372 | |
[email protected] | d324ab33 | 2008-08-18 16:00:38 | [diff] [blame] | 1373 | // Cleaning |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1374 | DeleteFile(target_file.value().c_str()); |
[email protected] | fd061a6 | 2009-08-25 01:51:44 | [diff] [blame] | 1375 | DeleteFile(link_file.value().c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1376 | CoUninitialize(); |
| 1377 | } |
| 1378 | |
| 1379 | TEST_F(FileUtilTest, CreateShortcutTest) { |
| 1380 | const wchar_t file_contents[] = L"This is another target."; |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1381 | FilePath target_file = temp_dir_.path().Append(L"Target1.txt"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1382 | CreateTextFile(target_file, file_contents); |
| 1383 | |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1384 | FilePath link_file = temp_dir_.path().Append(L"Link1.lnk"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1385 | |
| 1386 | CoInitialize(NULL); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1387 | EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(), |
| 1388 | link_file.value().c_str(), |
[email protected] | 86b5401 | 2009-11-19 09:18:50 | [diff] [blame] | 1389 | NULL, NULL, NULL, NULL, 0, NULL)); |
[email protected] | fd061a6 | 2009-08-25 01:51:44 | [diff] [blame] | 1390 | FilePath resolved_name = link_file; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1391 | EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name)); |
[email protected] | fd061a6 | 2009-08-25 01:51:44 | [diff] [blame] | 1392 | std::wstring read_contents = ReadTextFile(resolved_name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1393 | EXPECT_EQ(file_contents, read_contents); |
| 1394 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1395 | DeleteFile(target_file.value().c_str()); |
| 1396 | DeleteFile(link_file.value().c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1397 | CoUninitialize(); |
| 1398 | } |
[email protected] | 2c59af7dca | 2009-03-11 18:37:48 | [diff] [blame] | 1399 | |
| 1400 | TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) { |
| 1401 | // Create a directory |
| 1402 | FilePath dir_name_from = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1403 | temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir")); |
[email protected] | 2c59af7dca | 2009-03-11 18:37:48 | [diff] [blame] | 1404 | file_util::CreateDirectory(dir_name_from); |
| 1405 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 1406 | |
| 1407 | // Create a file under the directory |
| 1408 | FilePath file_name_from = |
| 1409 | dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt")); |
| 1410 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 1411 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 1412 | |
| 1413 | // Move the directory by using CopyAndDeleteDirectory |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1414 | FilePath dir_name_to = temp_dir_.path().Append( |
[email protected] | 2c59af7dca | 2009-03-11 18:37:48 | [diff] [blame] | 1415 | FILE_PATH_LITERAL("CopyAndDelete_To_Subdir")); |
| 1416 | FilePath file_name_to = |
| 1417 | dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt")); |
| 1418 | |
| 1419 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 1420 | |
| 1421 | EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to)); |
| 1422 | |
| 1423 | // Check everything has been moved. |
| 1424 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 1425 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 1426 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 1427 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 1428 | } |
[email protected] | 7d95aae | 2009-10-09 07:33:39 | [diff] [blame] | 1429 | |
| 1430 | TEST_F(FileUtilTest, GetTempDirTest) { |
| 1431 | static const TCHAR* kTmpKey = _T("TMP"); |
| 1432 | static const TCHAR* kTmpValues[] = { |
| 1433 | _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\") |
| 1434 | }; |
| 1435 | // Save the original $TMP. |
| 1436 | size_t original_tmp_size; |
| 1437 | TCHAR* original_tmp; |
| 1438 | ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey)); |
| 1439 | // original_tmp may be NULL. |
| 1440 | |
| 1441 | for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) { |
| 1442 | FilePath path; |
| 1443 | ::_tputenv_s(kTmpKey, kTmpValues[i]); |
| 1444 | file_util::GetTempDir(&path); |
| 1445 | EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] << |
| 1446 | " result=" << path.value(); |
| 1447 | } |
| 1448 | |
| 1449 | // Restore the original $TMP. |
| 1450 | if (original_tmp) { |
| 1451 | ::_tputenv_s(kTmpKey, original_tmp); |
| 1452 | free(original_tmp); |
| 1453 | } else { |
| 1454 | ::_tputenv_s(kTmpKey, _T("")); |
| 1455 | } |
| 1456 | } |
| 1457 | #endif // OS_WIN |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1458 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 1459 | TEST_F(FileUtilTest, CreateTemporaryFileTest) { |
| 1460 | FilePath temp_files[3]; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 1461 | for (int i = 0; i < 3; i++) { |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 1462 | ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i]))); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 1463 | EXPECT_TRUE(file_util::PathExists(temp_files[i])); |
| 1464 | EXPECT_FALSE(file_util::DirectoryExists(temp_files[i])); |
| 1465 | } |
| 1466 | for (int i = 0; i < 3; i++) |
| 1467 | EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); |
| 1468 | for (int i = 0; i < 3; i++) |
| 1469 | EXPECT_TRUE(file_util::Delete(temp_files[i], false)); |
| 1470 | } |
| 1471 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 1472 | TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) { |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 1473 | FilePath names[3]; |
| 1474 | FILE *fps[3]; |
| 1475 | int i; |
| 1476 | |
| 1477 | // Create; make sure they are open and exist. |
| 1478 | for (i = 0; i < 3; ++i) { |
| 1479 | fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i])); |
| 1480 | ASSERT_TRUE(fps[i]); |
| 1481 | EXPECT_TRUE(file_util::PathExists(names[i])); |
| 1482 | } |
| 1483 | |
| 1484 | // Make sure all names are unique. |
| 1485 | for (i = 0; i < 3; ++i) { |
| 1486 | EXPECT_FALSE(names[i] == names[(i+1)%3]); |
| 1487 | } |
| 1488 | |
| 1489 | // Close and delete. |
| 1490 | for (i = 0; i < 3; ++i) { |
| 1491 | EXPECT_TRUE(file_util::CloseFile(fps[i])); |
| 1492 | EXPECT_TRUE(file_util::Delete(names[i], false)); |
| 1493 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 1497 | FilePath temp_dir; |
| 1498 | ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(), |
| 1499 | &temp_dir)); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 1500 | EXPECT_TRUE(file_util::PathExists(temp_dir)); |
| 1501 | EXPECT_TRUE(file_util::Delete(temp_dir, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1502 | } |
| 1503 | |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 1504 | TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) { |
| 1505 | FilePath new_dir; |
| 1506 | ASSERT_TRUE(file_util::CreateTemporaryDirInDir( |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1507 | temp_dir_.path(), |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 1508 | FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"), |
[email protected] | 046062e8 | 2010-06-30 07:19:11 | [diff] [blame] | 1509 | &new_dir)); |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 1510 | EXPECT_TRUE(file_util::PathExists(new_dir)); |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1511 | EXPECT_TRUE(temp_dir_.path().IsParent(new_dir)); |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 1512 | EXPECT_TRUE(file_util::Delete(new_dir, false)); |
| 1513 | } |
| 1514 | |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 1515 | TEST_F(FileUtilTest, GetShmemTempDirTest) { |
| 1516 | FilePath dir; |
| 1517 | EXPECT_TRUE(file_util::GetShmemTempDir(&dir)); |
| 1518 | EXPECT_TRUE(file_util::DirectoryExists(dir)); |
| 1519 | } |
| 1520 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1521 | TEST_F(FileUtilTest, CreateDirectoryTest) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1522 | FilePath test_root = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1523 | temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test")); |
[email protected] | 8541bb8 | 2008-08-15 17:45:13 | [diff] [blame] | 1524 | #if defined(OS_WIN) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1525 | FilePath test_path = |
| 1526 | test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1527 | #elif defined(OS_POSIX) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1528 | FilePath test_path = |
| 1529 | test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1530 | #endif |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 1531 | |
| 1532 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 1533 | EXPECT_TRUE(file_util::CreateDirectory(test_path)); |
| 1534 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 1535 | // CreateDirectory returns true if the DirectoryExists returns true. |
| 1536 | EXPECT_TRUE(file_util::CreateDirectory(test_path)); |
| 1537 | |
| 1538 | // Doesn't work to create it on top of a non-dir |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1539 | test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt")); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 1540 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 1541 | CreateTextFile(test_path, L"test file"); |
| 1542 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 1543 | EXPECT_FALSE(file_util::CreateDirectory(test_path)); |
| 1544 | |
| 1545 | EXPECT_TRUE(file_util::Delete(test_root, true)); |
| 1546 | EXPECT_FALSE(file_util::PathExists(test_root)); |
| 1547 | EXPECT_FALSE(file_util::PathExists(test_path)); |
[email protected] | 40676ab | 2009-11-27 14:54:41 | [diff] [blame] | 1548 | |
| 1549 | // Verify assumptions made by the Windows implementation: |
| 1550 | // 1. The current directory always exists. |
| 1551 | // 2. The root directory always exists. |
| 1552 | ASSERT_TRUE(file_util::DirectoryExists( |
| 1553 | FilePath(FilePath::kCurrentDirectory))); |
| 1554 | FilePath top_level = test_root; |
| 1555 | while (top_level != top_level.DirName()) { |
| 1556 | top_level = top_level.DirName(); |
| 1557 | } |
| 1558 | ASSERT_TRUE(file_util::DirectoryExists(top_level)); |
| 1559 | |
| 1560 | // Given these assumptions hold, it should be safe to |
| 1561 | // test that "creating" these directories succeeds. |
| 1562 | EXPECT_TRUE(file_util::CreateDirectory( |
| 1563 | FilePath(FilePath::kCurrentDirectory))); |
| 1564 | EXPECT_TRUE(file_util::CreateDirectory(top_level)); |
[email protected] | 89b9ae09 | 2009-12-17 20:42:40 | [diff] [blame] | 1565 | |
| 1566 | #if defined(OS_WIN) |
| 1567 | FilePath invalid_drive(FILE_PATH_LITERAL("o:\\")); |
| 1568 | FilePath invalid_path = |
| 1569 | invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir")); |
| 1570 | if (!file_util::PathExists(invalid_drive)) { |
| 1571 | EXPECT_FALSE(file_util::CreateDirectory(invalid_path)); |
| 1572 | } |
| 1573 | #endif |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | TEST_F(FileUtilTest, DetectDirectoryTest) { |
| 1577 | // Check a directory |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1578 | FilePath test_root = |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1579 | temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test")); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 1580 | EXPECT_FALSE(file_util::PathExists(test_root)); |
| 1581 | EXPECT_TRUE(file_util::CreateDirectory(test_root)); |
| 1582 | EXPECT_TRUE(file_util::PathExists(test_root)); |
| 1583 | EXPECT_TRUE(file_util::DirectoryExists(test_root)); |
| 1584 | |
| 1585 | // Check a file |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1586 | FilePath test_path = |
| 1587 | test_root.Append(FILE_PATH_LITERAL("foobar.txt")); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 1588 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 1589 | CreateTextFile(test_path, L"test file"); |
| 1590 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 1591 | EXPECT_FALSE(file_util::DirectoryExists(test_path)); |
| 1592 | EXPECT_TRUE(file_util::Delete(test_path, false)); |
| 1593 | |
| 1594 | EXPECT_TRUE(file_util::Delete(test_root, true)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1595 | } |
| 1596 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1597 | TEST_F(FileUtilTest, FileEnumeratorTest) { |
| 1598 | // Test an empty directory. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1599 | file_util::FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES); |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 1600 | EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); |
| 1601 | EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1602 | |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1603 | // Test an empty directory, non-recursively, including "..". |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1604 | file_util::FileEnumerator f0_dotdot(temp_dir_.path(), false, |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1605 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 1606 | FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT)); |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1607 | EXPECT_EQ(temp_dir_.path().Append(FILE_PATH_LITERAL("..")).value(), |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1608 | f0_dotdot.Next().value()); |
| 1609 | EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 1610 | f0_dotdot.Next().value()); |
| 1611 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1612 | // create the directories |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1613 | FilePath dir1 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir1")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1614 | EXPECT_TRUE(file_util::CreateDirectory(dir1)); |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1615 | FilePath dir2 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir2")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1616 | EXPECT_TRUE(file_util::CreateDirectory(dir2)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1617 | FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1618 | EXPECT_TRUE(file_util::CreateDirectory(dir2inner)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1619 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1620 | // create the files |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1621 | FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1622 | CreateTextFile(dir2file, L""); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1623 | FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1624 | CreateTextFile(dir2innerfile, L""); |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1625 | FilePath file1 = temp_dir_.path().Append(FILE_PATH_LITERAL("file1.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1626 | CreateTextFile(file1, L""); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 1627 | FilePath file2_rel = |
| 1628 | dir2.Append(FilePath::kParentDirectory) |
| 1629 | .Append(FILE_PATH_LITERAL("file2.txt")); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1630 | CreateTextFile(file2_rel, L""); |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1631 | FilePath file2_abs = temp_dir_.path().Append(FILE_PATH_LITERAL("file2.txt")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1632 | |
| 1633 | // Only enumerate files. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1634 | file_util::FileEnumerator f1(temp_dir_.path(), true, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1635 | file_util::FileEnumerator::FILES); |
| 1636 | FindResultCollector c1(f1); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1637 | EXPECT_TRUE(c1.HasFile(file1)); |
| 1638 | EXPECT_TRUE(c1.HasFile(file2_abs)); |
| 1639 | EXPECT_TRUE(c1.HasFile(dir2file)); |
| 1640 | EXPECT_TRUE(c1.HasFile(dir2innerfile)); |
| 1641 | EXPECT_EQ(c1.size(), 4); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1642 | |
| 1643 | // Only enumerate directories. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1644 | file_util::FileEnumerator f2(temp_dir_.path(), true, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1645 | file_util::FileEnumerator::DIRECTORIES); |
| 1646 | FindResultCollector c2(f2); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1647 | EXPECT_TRUE(c2.HasFile(dir1)); |
| 1648 | EXPECT_TRUE(c2.HasFile(dir2)); |
| 1649 | EXPECT_TRUE(c2.HasFile(dir2inner)); |
| 1650 | EXPECT_EQ(c2.size(), 3); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1651 | |
[email protected] | 3bc9ffaf | 2008-10-16 02:42:45 | [diff] [blame] | 1652 | // Only enumerate directories non-recursively. |
| 1653 | file_util::FileEnumerator f2_non_recursive( |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1654 | temp_dir_.path(), false, file_util::FileEnumerator::DIRECTORIES); |
[email protected] | 3bc9ffaf | 2008-10-16 02:42:45 | [diff] [blame] | 1655 | FindResultCollector c2_non_recursive(f2_non_recursive); |
| 1656 | EXPECT_TRUE(c2_non_recursive.HasFile(dir1)); |
| 1657 | EXPECT_TRUE(c2_non_recursive.HasFile(dir2)); |
| 1658 | EXPECT_EQ(c2_non_recursive.size(), 2); |
| 1659 | |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1660 | // Only enumerate directories, non-recursively, including "..". |
| 1661 | file_util::FileEnumerator f2_dotdot( |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1662 | temp_dir_.path(), false, |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1663 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 1664 | file_util::FileEnumerator::DIRECTORIES | |
| 1665 | file_util::FileEnumerator::INCLUDE_DOT_DOT)); |
| 1666 | FindResultCollector c2_dotdot(f2_dotdot); |
| 1667 | EXPECT_TRUE(c2_dotdot.HasFile(dir1)); |
| 1668 | EXPECT_TRUE(c2_dotdot.HasFile(dir2)); |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1669 | EXPECT_TRUE(c2_dotdot.HasFile( |
| 1670 | temp_dir_.path().Append(FILE_PATH_LITERAL("..")))); |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 1671 | EXPECT_EQ(c2_dotdot.size(), 3); |
| 1672 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1673 | // Enumerate files and directories. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1674 | file_util::FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1675 | FindResultCollector c3(f3); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1676 | EXPECT_TRUE(c3.HasFile(dir1)); |
| 1677 | EXPECT_TRUE(c3.HasFile(dir2)); |
| 1678 | EXPECT_TRUE(c3.HasFile(file1)); |
| 1679 | EXPECT_TRUE(c3.HasFile(file2_abs)); |
| 1680 | EXPECT_TRUE(c3.HasFile(dir2file)); |
| 1681 | EXPECT_TRUE(c3.HasFile(dir2inner)); |
| 1682 | EXPECT_TRUE(c3.HasFile(dir2innerfile)); |
| 1683 | EXPECT_EQ(c3.size(), 7); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1684 | |
| 1685 | // Non-recursive operation. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1686 | file_util::FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1687 | FindResultCollector c4(f4); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1688 | EXPECT_TRUE(c4.HasFile(dir2)); |
| 1689 | EXPECT_TRUE(c4.HasFile(dir2)); |
| 1690 | EXPECT_TRUE(c4.HasFile(file1)); |
| 1691 | EXPECT_TRUE(c4.HasFile(file2_abs)); |
| 1692 | EXPECT_EQ(c4.size(), 4); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1693 | |
| 1694 | // Enumerate with a pattern. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1695 | file_util::FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES, |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 1696 | FILE_PATH_LITERAL("dir*")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1697 | FindResultCollector c5(f5); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 1698 | EXPECT_TRUE(c5.HasFile(dir1)); |
| 1699 | EXPECT_TRUE(c5.HasFile(dir2)); |
| 1700 | EXPECT_TRUE(c5.HasFile(dir2file)); |
| 1701 | EXPECT_TRUE(c5.HasFile(dir2inner)); |
| 1702 | EXPECT_TRUE(c5.HasFile(dir2innerfile)); |
| 1703 | EXPECT_EQ(c5.size(), 5); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1704 | |
| 1705 | // Make sure the destructor closes the find handle while in the middle of a |
| 1706 | // query to allow TearDown to delete the directory. |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1707 | file_util::FileEnumerator f6(temp_dir_.path(), true, FILES_AND_DIRECTORIES); |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 1708 | EXPECT_FALSE(f6.Next().value().empty()); // Should have found something |
| 1709 | // (we don't care what). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1710 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1711 | |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1712 | TEST_F(FileUtilTest, Contains) { |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1713 | FilePath data_dir = |
| 1714 | temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1715 | |
| 1716 | // Create a fresh, empty copy of this directory. |
[email protected] | a92d2fd | 2009-01-31 01:19:57 | [diff] [blame] | 1717 | if (file_util::PathExists(data_dir)) { |
| 1718 | ASSERT_TRUE(file_util::Delete(data_dir, true)); |
| 1719 | } |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1720 | ASSERT_TRUE(file_util::CreateDirectory(data_dir)); |
| 1721 | |
| 1722 | FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo"))); |
| 1723 | FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt"))); |
| 1724 | FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt"))); |
| 1725 | FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
| 1726 | |
| 1727 | // Annoyingly, the directories must actually exist in order for realpath(), |
| 1728 | // which Contains() relies on in posix, to work. |
| 1729 | ASSERT_TRUE(file_util::CreateDirectory(foo)); |
| 1730 | std::string data("hello"); |
[email protected] | 4e9f6be | 2009-04-03 17:17:58 | [diff] [blame] | 1731 | ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length())); |
| 1732 | ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length())); |
| 1733 | ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1734 | |
| 1735 | EXPECT_TRUE(file_util::ContainsPath(foo, bar)); |
| 1736 | EXPECT_FALSE(file_util::ContainsPath(foo, baz)); |
| 1737 | EXPECT_FALSE(file_util::ContainsPath(foo, foobar)); |
| 1738 | EXPECT_FALSE(file_util::ContainsPath(foo, foo)); |
| 1739 | |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 1740 | // Platform-specific concerns. |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1741 | FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO"))); |
| 1742 | #if defined(OS_WIN) |
| 1743 | EXPECT_TRUE(file_util::ContainsPath(foo, |
| 1744 | foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 1745 | EXPECT_TRUE(file_util::ContainsPath(foo, |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1746 | FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt")))); |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 1747 | #elif defined(OS_MACOSX) |
| 1748 | // We can't really do this test on OS X since the case-sensitivity of the |
| 1749 | // filesystem is configurable. |
| 1750 | #elif defined(OS_POSIX) |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1751 | EXPECT_FALSE(file_util::ContainsPath(foo, |
| 1752 | foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
[email protected] | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 1753 | #endif |
| 1754 | } |
| 1755 | |
[email protected] | 507fb9a | 2010-09-23 23:28:22 | [diff] [blame^] | 1756 | TEST_F(FileUtilTest, TouchFile) { |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1757 | FilePath data_dir = |
| 1758 | temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); |
[email protected] | ec3d145 | 2010-02-18 10:02:26 | [diff] [blame] | 1759 | |
| 1760 | // Create a fresh, empty copy of this directory. |
| 1761 | if (file_util::PathExists(data_dir)) { |
| 1762 | ASSERT_TRUE(file_util::Delete(data_dir, true)); |
| 1763 | } |
| 1764 | ASSERT_TRUE(file_util::CreateDirectory(data_dir)); |
| 1765 | |
| 1766 | FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
| 1767 | std::string data("hello"); |
| 1768 | ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); |
| 1769 | |
[email protected] | 507fb9a | 2010-09-23 23:28:22 | [diff] [blame^] | 1770 | base::Time access_time; |
| 1771 | // This timestamp is divisible by one day (in local timezone), |
| 1772 | // to make it work on FAT too. |
| 1773 | ASSERT_TRUE(base::Time::FromString(L"Wed, 16 Nov 1994, 00:00:00", |
| 1774 | &access_time)); |
| 1775 | |
[email protected] | ec3d145 | 2010-02-18 10:02:26 | [diff] [blame] | 1776 | base::Time modification_time; |
| 1777 | // Note that this timestamp is divisible by two (seconds) - FAT stores |
| 1778 | // modification times with 2s resolution. |
| 1779 | ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT", |
| 1780 | &modification_time)); |
[email protected] | 507fb9a | 2010-09-23 23:28:22 | [diff] [blame^] | 1781 | |
| 1782 | ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time)); |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 1783 | base::PlatformFileInfo file_info; |
[email protected] | ec3d145 | 2010-02-18 10:02:26 | [diff] [blame] | 1784 | ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info)); |
[email protected] | 507fb9a | 2010-09-23 23:28:22 | [diff] [blame^] | 1785 | EXPECT_EQ(file_info.last_accessed.ToInternalValue(), |
| 1786 | access_time.ToInternalValue()); |
| 1787 | EXPECT_EQ(file_info.last_modified.ToInternalValue(), |
| 1788 | modification_time.ToInternalValue()); |
[email protected] | ec3d145 | 2010-02-18 10:02:26 | [diff] [blame] | 1789 | } |
| 1790 | |
[email protected] | b33f1d9 | 2010-05-26 01:40:12 | [diff] [blame] | 1791 | TEST_F(FileUtilTest, IsDirectoryEmpty) { |
[email protected] | 90314c0a8 | 2010-09-15 20:40:47 | [diff] [blame] | 1792 | FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir")); |
[email protected] | b33f1d9 | 2010-05-26 01:40:12 | [diff] [blame] | 1793 | |
| 1794 | ASSERT_FALSE(file_util::PathExists(empty_dir)); |
| 1795 | |
| 1796 | ASSERT_TRUE(file_util::CreateDirectory(empty_dir)); |
| 1797 | |
| 1798 | EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir)); |
| 1799 | |
| 1800 | FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt"))); |
| 1801 | std::string bar("baz"); |
| 1802 | ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length())); |
| 1803 | |
| 1804 | EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir)); |
| 1805 | } |
| 1806 | |
[email protected] | 11b901ee | 2008-09-10 00:16:28 | [diff] [blame] | 1807 | } // namespace |