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