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