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