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