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