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