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