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