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