blob: b1f78222f27c564a99144597234b74a533a56872 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
erikkay@google.comc8ec9e92008-08-16 02:50:10 +09005#include "build/build_config.h"
6
erikkay@google.com014161d2008-08-16 02:45:13 +09007#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <windows.h>
initial.commit3f4a7322008-07-27 06:49:38 +09009#include <shellapi.h>
10#include <shlobj.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090011#endif
initial.commit3f4a7322008-07-27 06:49:38 +090012
13#include <fstream>
14#include <iostream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090015#include <set>
initial.commit3f4a7322008-07-27 06:49:38 +090016
17#include "base/base_paths.h"
evanm@google.com874d1672008-10-31 08:54:04 +090018#include "base/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090019#include "base/file_util.h"
20#include "base/logging.h"
21#include "base/path_service.h"
erikkay@google.com8d133f62009-04-24 00:05:19 +090022#include "base/platform_thread.h"
initial.commit3f4a7322008-07-27 06:49:38 +090023#include "base/string_util.h"
erikkay@google.com9ac26762009-04-18 09:42:48 +090024#include "base/time.h"
initial.commit3f4a7322008-07-27 06:49:38 +090025#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090026#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090027
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090028// This macro helps avoid wrapped lines in the test structs.
29#define FPL(x) FILE_PATH_LITERAL(x)
30
initial.commit3f4a7322008-07-27 06:49:38 +090031namespace {
32
yuzo@chromium.org2da0f822009-06-09 14:57:38 +090033const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
34 static_cast<file_util::FileEnumerator::FILE_TYPE>(
35 file_util::FileEnumerator::FILES |
36 file_util::FileEnumerator::DIRECTORIES);
37
erikkay@google.comf2406842008-08-21 00:59:49 +090038// file_util winds up using autoreleased objects on the Mac, so this needs
39// to be a PlatformTest
40class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +090041 protected:
42 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +090043 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +090044 // Name a subdirectory of the temp directory.
45 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +090046 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +090047
48 // Create a fresh, empty copy of this directory.
49 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +090050 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +090051 }
52 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +090053 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +090054 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +090055 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +090056 ASSERT_FALSE(file_util::PathExists(test_dir_));
57 }
58
59 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +090060 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +090061};
62
63// Collects all the results from the given file enumerator, and provides an
64// interface to query whether a given file is present.
65class FindResultCollector {
66 public:
67 FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +090068 FilePath cur_file;
69 while (!(cur_file = enumerator.Next()).value().empty()) {
70 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +090071 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +090072 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +090073 << "Same file returned twice";
74
75 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +090076 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +090077 }
78 }
79
80 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +090081 bool HasFile(const FilePath& file) const {
82 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +090083 }
evanm@google.com874d1672008-10-31 08:54:04 +090084
erikkay@google.comdfb51b22008-08-16 02:32:10 +090085 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +090086 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +090087 }
initial.commit3f4a7322008-07-27 06:49:38 +090088
89 private:
evanm@google.com874d1672008-10-31 08:54:04 +090090 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +090091};
92
93// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +090094void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +090095 const std::wstring& contents) {
96 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +090097 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +090098 ASSERT_TRUE(file.is_open());
99 file << contents;
100 file.close();
101}
102
103// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900104std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900105 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900106 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900107 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900108 EXPECT_TRUE(file.is_open());
109 file.getline(contents, 64);
110 file.close();
111 return std::wstring(contents);
112}
113
erikkay@google.com014161d2008-08-16 02:45:13 +0900114#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900115uint64 FileTimeAsUint64(const FILETIME& ft) {
116 ULARGE_INTEGER u;
117 u.LowPart = ft.dwLowDateTime;
118 u.HighPart = ft.dwHighDateTime;
119 return u.QuadPart;
120}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900121#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900122
123const struct append_case {
124 const wchar_t* path;
125 const wchar_t* ending;
126 const wchar_t* result;
127} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900128#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900129 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
130 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
131 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
132 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
133 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
134 {L"", L"path", L"\\path"},
135 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900136#elif defined(OS_POSIX)
137 {L"/foo/bar", L"path", L"/foo/bar/path"},
138 {L"/foo/bar/", L"path", L"/foo/bar/path"},
139 {L"/foo/bar//", L"path", L"/foo/bar//path"},
140 {L"/foo/bar/", L"", L"/foo/bar/"},
141 {L"/foo/bar", L"", L"/foo/bar/"},
142 {L"", L"path", L"/path"},
143 {L"", L"", L"/"},
144#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900145};
146
initial.commit3f4a7322008-07-27 06:49:38 +0900147TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900148 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900149 const append_case& value = append_cases[i];
150 std::wstring result = value.path;
151 file_util::AppendToPath(&result, value.ending);
152 EXPECT_EQ(value.result, result);
153 }
154
155#ifdef NDEBUG
156 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
157#endif
158}
159
160static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900161 const FilePath::CharType* path;
162 const FilePath::CharType* suffix;
163 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900164} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900165 {FPL(""), FPL(""), FPL("")},
166 {FPL(""), FPL("txt"), FPL("txt")},
167 {FPL("."), FPL("txt"), FPL("txt.")},
168 {FPL("."), FPL(""), FPL(".")},
169 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
170 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
171 {FPL("foo"), FPL("txt"), FPL("footxt")},
172 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
173 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
174 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
175 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
176 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
177 {FPL("foo"), FPL(""), FPL("foo")},
178 {FPL("foo"), FPL("."), FPL("foo.")},
179 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
180 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900181#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900182 {FPL("\\"), FPL(""), FPL("\\")},
183 {FPL("\\"), FPL("txt"), FPL("\\txt")},
184 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
185 {FPL("\\."), FPL(""), FPL("\\.")},
186 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
187 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
188 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
189 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
190 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
191 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
192 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
193 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
194 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900195#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900196 {FPL("/"), FPL(""), FPL("/")},
197 {FPL("/"), FPL("txt"), FPL("/txt")},
198 {FPL("/."), FPL("txt"), FPL("/txt.")},
199 {FPL("/."), FPL(""), FPL("/.")},
200 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
201 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
202 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
203 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
204 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
205 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
206 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
207 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900208#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900209};
210
211TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900212 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900213 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900214 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900215 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900216 }
217}
218
219static const struct filename_case {
220 const wchar_t* path;
221 const wchar_t* filename;
222} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900223#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900224 {L"c:\\colon\\backslash", L"backslash"},
225 {L"c:\\colon\\backslash\\", L""},
226 {L"\\\\filename.exe", L"filename.exe"},
227 {L"filename.exe", L"filename.exe"},
228 {L"", L""},
229 {L"\\\\\\", L""},
230 {L"c:/colon/backslash", L"backslash"},
231 {L"c:/colon/backslash/", L""},
232 {L"//////", L""},
233 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900234#elif defined(OS_POSIX)
235 {L"/foo/bar", L"bar"},
236 {L"/foo/bar/", L""},
237 {L"/filename.exe", L"filename.exe"},
238 {L"filename.exe", L"filename.exe"},
239 {L"", L""},
240 {L"/", L""},
241#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900242};
243
244TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900245 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900246 const filename_case& value = filename_cases[i];
247 std::wstring result = file_util::GetFilenameFromPath(value.path);
248 EXPECT_EQ(value.filename, result);
249 }
250}
251
252// Test finding the file type from a path name
253static const struct extension_case {
254 const wchar_t* path;
255 const wchar_t* extension;
256} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900257#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900258 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
259 {L"C:\\colon\\backslash\\filename.", L""},
260 {L"C:\\colon\\backslash\\filename", L""},
261 {L"C:\\colon\\backslash\\", L""},
262 {L"C:\\colon\\backslash.\\", L""},
263 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900264#elif defined(OS_POSIX)
265 {L"/foo/bar/filename.extension", L"extension"},
266 {L"/foo/bar/filename.", L""},
267 {L"/foo/bar/filename", L""},
268 {L"/foo/bar/", L""},
269 {L"/foo/bar./", L""},
270 {L"/foo/bar/filename.extension.extension2", L"extension2"},
271 {L".", L""},
272 {L"..", L""},
273 {L"./foo", L""},
274 {L"./foo.extension", L"extension"},
275 {L"/foo.extension1/bar.extension2", L"extension2"},
276#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900277};
278
279TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900280 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900281 const extension_case& ext = extension_cases[i];
282 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
283 EXPECT_EQ(ext.extension, fext);
284 }
285}
286
287// Test finding the directory component of a path
288static const struct dir_case {
289 const wchar_t* full_path;
290 const wchar_t* directory;
291} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900292#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900293 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
294 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
295 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
296 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
297 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
298 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
299 {L"C:\\", L"C:"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900300#elif defined(OS_POSIX)
301 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
302 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
303 {L"/foo/bar/", L"/foo/bar"},
304 {L"/foo/bar//", L"/foo/bar"},
305 {L"/foo/bar", L"/foo"},
306 {L"/foo/bar./", L"/foo/bar."},
307 {L"/", L"/"},
308 {L".", L"."},
309 {L"..", L"."}, // yes, ".." technically lives in "."
310#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900311};
312
313TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900314 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900315 const dir_case& dir = dir_cases[i];
316 const std::wstring parent =
317 file_util::GetDirectoryFromPath(dir.full_path);
318 EXPECT_EQ(dir.directory, parent);
319 }
320}
321
322TEST_F(FileUtilTest, CountFilesCreatedAfter) {
323 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900324 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900325 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
326
327 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900328#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900329 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900330#elif defined(OS_POSIX)
331 // We need to wait at least one second here because the precision of
332 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900333 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900334#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900335
336 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900337 base::Time now(base::Time::NowFromSystemTime());
338 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900339
340 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900341 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900342 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
343
344 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900345 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900346
347 // Delete new file, we should see no files after cutoff now
348 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900349 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900350}
351
352// Tests that the Delete function works as expected, especially
353// the recursion flag. Also coincidentally tests PathExists.
354TEST_F(FileUtilTest, Delete) {
355 // Create a file
evanm@google.com874d1672008-10-31 08:54:04 +0900356 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900357 CreateTextFile(file_name, L"I'm cannon fodder.");
358
359 ASSERT_TRUE(file_util::PathExists(file_name));
360
evanm@google.com874d1672008-10-31 08:54:04 +0900361 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
362 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900363
364 ASSERT_TRUE(file_util::PathExists(subdir_path));
365
evanm@google.com874d1672008-10-31 08:54:04 +0900366 FilePath directory_contents = test_dir_;
erikkay@google.com014161d2008-08-16 02:45:13 +0900367#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900368 // TODO(erikkay): see if anyone's actually using this feature of the API
evanm@google.com874d1672008-10-31 08:54:04 +0900369 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900370 // Delete non-recursively and check that only the file is deleted
371 ASSERT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900372 EXPECT_FALSE(file_util::PathExists(file_name));
373 EXPECT_TRUE(file_util::PathExists(subdir_path));
374#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900375
376 // Delete recursively and make sure all contents are deleted
377 ASSERT_TRUE(file_util::Delete(directory_contents, true));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900378 EXPECT_FALSE(file_util::PathExists(file_name));
379 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900380}
381
382TEST_F(FileUtilTest, Move) {
383 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900384 FilePath dir_name_from =
385 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
386 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900387 ASSERT_TRUE(file_util::PathExists(dir_name_from));
388
389 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900390 FilePath file_name_from =
391 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900392 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
393 ASSERT_TRUE(file_util::PathExists(file_name_from));
394
395 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900396 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
397 FilePath file_name_to =
398 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900399
400 ASSERT_FALSE(file_util::PathExists(dir_name_to));
401
402 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
403
404 // Check everything has been moved.
405 EXPECT_FALSE(file_util::PathExists(dir_name_from));
406 EXPECT_FALSE(file_util::PathExists(file_name_from));
407 EXPECT_TRUE(file_util::PathExists(dir_name_to));
408 EXPECT_TRUE(file_util::PathExists(file_name_to));
409}
410
411TEST_F(FileUtilTest, CopyDirectoryRecursively) {
412 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900413 FilePath dir_name_from =
414 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
415 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900416 ASSERT_TRUE(file_util::PathExists(dir_name_from));
417
418 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900419 FilePath file_name_from =
420 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900421 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
422 ASSERT_TRUE(file_util::PathExists(file_name_from));
423
424 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900425 FilePath subdir_name_from =
426 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
427 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900428 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
429
430 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900431 FilePath file_name2_from =
432 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900433 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
434 ASSERT_TRUE(file_util::PathExists(file_name2_from));
435
436 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900437 FilePath dir_name_to =
438 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
439 FilePath file_name_to =
440 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
441 FilePath subdir_name_to =
442 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
443 FilePath file_name2_to =
444 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900445
446 ASSERT_FALSE(file_util::PathExists(dir_name_to));
447
448 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
449
450 // Check everything has been copied.
451 EXPECT_TRUE(file_util::PathExists(dir_name_from));
452 EXPECT_TRUE(file_util::PathExists(file_name_from));
453 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
454 EXPECT_TRUE(file_util::PathExists(file_name2_from));
455 EXPECT_TRUE(file_util::PathExists(dir_name_to));
456 EXPECT_TRUE(file_util::PathExists(file_name_to));
457 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
458 EXPECT_TRUE(file_util::PathExists(file_name2_to));
459}
460
461TEST_F(FileUtilTest, CopyDirectory) {
462 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900463 FilePath dir_name_from =
464 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
465 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900466 ASSERT_TRUE(file_util::PathExists(dir_name_from));
467
468 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900469 FilePath file_name_from =
470 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900471 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
472 ASSERT_TRUE(file_util::PathExists(file_name_from));
473
474 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900475 FilePath subdir_name_from =
476 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
477 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900478 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
479
480 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900481 FilePath file_name2_from =
482 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900483 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
484 ASSERT_TRUE(file_util::PathExists(file_name2_from));
485
486 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900487 FilePath dir_name_to =
488 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
489 FilePath file_name_to =
490 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
491 FilePath subdir_name_to =
492 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +0900493
494 ASSERT_FALSE(file_util::PathExists(dir_name_to));
495
496 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
497
498 // Check everything has been copied.
499 EXPECT_TRUE(file_util::PathExists(dir_name_from));
500 EXPECT_TRUE(file_util::PathExists(file_name_from));
501 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
502 EXPECT_TRUE(file_util::PathExists(file_name2_from));
503 EXPECT_TRUE(file_util::PathExists(dir_name_to));
504 EXPECT_TRUE(file_util::PathExists(file_name_to));
505 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
506}
507
508TEST_F(FileUtilTest, CopyFile) {
509 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900510 FilePath dir_name_from =
511 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
512 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900513 ASSERT_TRUE(file_util::PathExists(dir_name_from));
514
515 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900516 FilePath file_name_from =
517 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900518 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
519 CreateTextFile(file_name_from, file_contents);
520 ASSERT_TRUE(file_util::PathExists(file_name_from));
521
522 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900523 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900524 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900525
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900526 // Copy the file to another location using '..' in the path.
evanm@google.com874d1672008-10-31 08:54:04 +0900527 std::wstring dest_file2(dir_name_from.ToWStringHack());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900528 file_util::AppendToPath(&dest_file2, L"..");
529 file_util::AppendToPath(&dest_file2, L"DestFile.txt");
evanm@google.com874d1672008-10-31 08:54:04 +0900530 ASSERT_TRUE(file_util::CopyFile(file_name_from,
531 FilePath::FromWStringHack(dest_file2)));
532 std::wstring dest_file2_test(dir_name_from.ToWStringHack());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900533 file_util::UpOneDirectory(&dest_file2_test);
534 file_util::AppendToPath(&dest_file2_test, L"DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900535
536 // Check everything has been copied.
537 EXPECT_TRUE(file_util::PathExists(file_name_from));
538 EXPECT_TRUE(file_util::PathExists(dest_file));
539 const std::wstring read_contents = ReadTextFile(dest_file);
540 EXPECT_EQ(file_contents, read_contents);
evanm@google.com874d1672008-10-31 08:54:04 +0900541 EXPECT_TRUE(file_util::PathExists(
542 FilePath::FromWStringHack(dest_file2_test)));
543 EXPECT_TRUE(file_util::PathExists(FilePath::FromWStringHack(dest_file2)));
initial.commit3f4a7322008-07-27 06:49:38 +0900544}
545
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900546// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900547#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900548TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +0900549 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900550
551 SYSTEMTIME start_time;
552 GetLocalTime(&start_time);
553 Sleep(100);
554 CreateTextFile(file_name, L"New file!");
555 Sleep(100);
556 SYSTEMTIME end_time;
557 GetLocalTime(&end_time);
558
559 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +0900560 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +0900561
562 FILETIME start_filetime;
563 SystemTimeToFileTime(&start_time, &start_filetime);
564 FILETIME end_filetime;
565 SystemTimeToFileTime(&end_time, &end_filetime);
566 FILETIME file_creation_filetime;
567 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
568
569 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
570 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
571 "creation time: " << FileTimeAsUint64(file_creation_filetime);
572
573 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
574 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
575 "end time: " << FileTimeAsUint64(end_filetime);
576
evanm@google.com874d1672008-10-31 08:54:04 +0900577 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +0900578}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900579#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900580
erikkay@google.comf2406842008-08-21 00:59:49 +0900581// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +0900582// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +0900583typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +0900584
erikkay@google.comf2406842008-08-21 00:59:49 +0900585TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +0900586 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +0900587 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +0900588 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
589 .Append(FILE_PATH_LITERAL("data"))
590 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900591 ASSERT_TRUE(file_util::PathExists(data_dir));
592
evanm@google.com874d1672008-10-31 08:54:04 +0900593 FilePath original_file =
594 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
595 FilePath same_file =
596 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
597 FilePath same_length_file =
598 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
599 FilePath different_file =
600 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
601 FilePath different_first_file =
602 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
603 FilePath different_last_file =
604 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
605 FilePath empty1_file =
606 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
607 FilePath empty2_file =
608 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
609 FilePath shortened_file =
610 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
611 FilePath binary_file =
612 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
613 FilePath binary_file_same =
614 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
615 FilePath binary_file_diff =
616 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +0900617
618 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
619 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
620 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
621 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
622 EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname"));
623 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
624 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
625 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
626 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
627 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
628 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
629 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
630}
631
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900632TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
633 FilePath data_dir;
634 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
635 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
636 .Append(FILE_PATH_LITERAL("data"))
637 .Append(FILE_PATH_LITERAL("file_util_unittest"));
638 ASSERT_TRUE(file_util::PathExists(data_dir));
639
640 FilePath original_file =
641 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
642 FilePath same_file =
643 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
644 FilePath crlf_file =
645 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
646 FilePath shortened_file =
647 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
648 FilePath different_file =
649 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
650 FilePath different_first_file =
651 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
652 FilePath different_last_file =
653 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
654 FilePath first1_file =
655 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
656 FilePath first2_file =
657 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
658 FilePath empty1_file =
659 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
660 FilePath empty2_file =
661 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
662 FilePath blank_line_file =
663 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
664 FilePath blank_line_crlf_file =
665 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
666
667 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
668 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
669 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
670 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
671 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
672 different_first_file));
673 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
674 different_last_file));
675 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
676 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
677 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
678 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
679 blank_line_crlf_file));
680}
681
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900682// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +0900683#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900684TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900685 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900686 CreateTextFile(target_file, L"This is the target.");
687
evanm@google.com874d1672008-10-31 08:54:04 +0900688 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900689
690 HRESULT result;
691 IShellLink *shell = NULL;
692 IPersistFile *persist = NULL;
693
694 CoInitialize(NULL);
695 // Temporarily create a shortcut for test
696 result = CoCreateInstance(CLSID_ShellLink, NULL,
697 CLSCTX_INPROC_SERVER, IID_IShellLink,
698 reinterpret_cast<LPVOID*>(&shell));
699 EXPECT_TRUE(SUCCEEDED(result));
700 result = shell->QueryInterface(IID_IPersistFile,
701 reinterpret_cast<LPVOID*>(&persist));
702 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900703 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900704 EXPECT_TRUE(SUCCEEDED(result));
705 result = shell->SetDescription(L"ResolveShortcutTest");
706 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900707 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +0900708 EXPECT_TRUE(SUCCEEDED(result));
709 if (persist)
710 persist->Release();
711 if (shell)
712 shell->Release();
713
714 bool is_solved;
evanm@google.com874d1672008-10-31 08:54:04 +0900715 std::wstring link_file_str = link_file.value();
716 is_solved = file_util::ResolveShortcut(&link_file_str);
initial.commit3f4a7322008-07-27 06:49:38 +0900717 EXPECT_TRUE(is_solved);
718 std::wstring contents;
evanm@google.com874d1672008-10-31 08:54:04 +0900719 contents = ReadTextFile(FilePath(link_file_str));
initial.commit3f4a7322008-07-27 06:49:38 +0900720 EXPECT_EQ(L"This is the target.", contents);
721
ericroman@google.comdbff4f52008-08-19 01:00:38 +0900722 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +0900723 DeleteFile(target_file.value().c_str());
724 DeleteFile(link_file_str.c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900725 CoUninitialize();
726}
727
728TEST_F(FileUtilTest, CreateShortcutTest) {
729 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +0900730 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900731 CreateTextFile(target_file, file_contents);
732
evanm@google.com874d1672008-10-31 08:54:04 +0900733 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900734
735 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +0900736 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
737 link_file.value().c_str(),
initial.commit3f4a7322008-07-27 06:49:38 +0900738 NULL, NULL, NULL, NULL, 0));
evanm@google.com874d1672008-10-31 08:54:04 +0900739 std::wstring resolved_name = link_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900740 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evanm@google.com874d1672008-10-31 08:54:04 +0900741 std::wstring read_contents = ReadTextFile(FilePath(resolved_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900742 EXPECT_EQ(file_contents, read_contents);
743
evanm@google.com874d1672008-10-31 08:54:04 +0900744 DeleteFile(target_file.value().c_str());
745 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900746 CoUninitialize();
747}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +0900748
749TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
750 // Create a directory
751 FilePath dir_name_from =
752 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
753 file_util::CreateDirectory(dir_name_from);
754 ASSERT_TRUE(file_util::PathExists(dir_name_from));
755
756 // Create a file under the directory
757 FilePath file_name_from =
758 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
759 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
760 ASSERT_TRUE(file_util::PathExists(file_name_from));
761
762 // Move the directory by using CopyAndDeleteDirectory
763 FilePath dir_name_to = test_dir_.Append(
764 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
765 FilePath file_name_to =
766 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
767
768 ASSERT_FALSE(file_util::PathExists(dir_name_to));
769
770 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
771
772 // Check everything has been moved.
773 EXPECT_FALSE(file_util::PathExists(dir_name_from));
774 EXPECT_FALSE(file_util::PathExists(file_name_from));
775 EXPECT_TRUE(file_util::PathExists(dir_name_to));
776 EXPECT_TRUE(file_util::PathExists(file_name_to));
777}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900778#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900779
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900780TEST_F(FileUtilTest, CreateTemporaryFileTest) {
781 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900782 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900783 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900784 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
785 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
786 }
787 for (int i = 0; i < 3; i++)
788 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
789 for (int i = 0; i < 3; i++)
790 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
791}
792
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900793TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900794 FilePath names[3];
795 FILE *fps[3];
796 int i;
797
798 // Create; make sure they are open and exist.
799 for (i = 0; i < 3; ++i) {
800 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
801 ASSERT_TRUE(fps[i]);
802 EXPECT_TRUE(file_util::PathExists(names[i]));
803 }
804
805 // Make sure all names are unique.
806 for (i = 0; i < 3; ++i) {
807 EXPECT_FALSE(names[i] == names[(i+1)%3]);
808 }
809
810 // Close and delete.
811 for (i = 0; i < 3; ++i) {
812 EXPECT_TRUE(file_util::CloseFile(fps[i]));
813 EXPECT_TRUE(file_util::Delete(names[i], false));
814 }
initial.commit3f4a7322008-07-27 06:49:38 +0900815}
816
817TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
818 std::wstring temp_dir;
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900819 ASSERT_TRUE(file_util::CreateNewTempDirectory(std::wstring(), &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900820 EXPECT_TRUE(file_util::PathExists(temp_dir));
821 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +0900822}
823
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900824TEST_F(FileUtilTest, GetShmemTempDirTest) {
825 FilePath dir;
826 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
827 EXPECT_TRUE(file_util::DirectoryExists(dir));
828}
829
initial.commit3f4a7322008-07-27 06:49:38 +0900830TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900831 FilePath test_root =
832 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +0900833#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +0900834 FilePath test_path =
835 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900836#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +0900837 FilePath test_path =
838 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900839#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +0900840
841 EXPECT_FALSE(file_util::PathExists(test_path));
842 EXPECT_TRUE(file_util::CreateDirectory(test_path));
843 EXPECT_TRUE(file_util::PathExists(test_path));
844 // CreateDirectory returns true if the DirectoryExists returns true.
845 EXPECT_TRUE(file_util::CreateDirectory(test_path));
846
847 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +0900848 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900849 EXPECT_FALSE(file_util::PathExists(test_path));
850 CreateTextFile(test_path, L"test file");
851 EXPECT_TRUE(file_util::PathExists(test_path));
852 EXPECT_FALSE(file_util::CreateDirectory(test_path));
853
854 EXPECT_TRUE(file_util::Delete(test_root, true));
855 EXPECT_FALSE(file_util::PathExists(test_root));
856 EXPECT_FALSE(file_util::PathExists(test_path));
857}
858
859TEST_F(FileUtilTest, DetectDirectoryTest) {
860 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900861 FilePath test_root =
862 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900863 EXPECT_FALSE(file_util::PathExists(test_root));
864 EXPECT_TRUE(file_util::CreateDirectory(test_root));
865 EXPECT_TRUE(file_util::PathExists(test_root));
866 EXPECT_TRUE(file_util::DirectoryExists(test_root));
867
868 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +0900869 FilePath test_path =
870 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900871 EXPECT_FALSE(file_util::PathExists(test_path));
872 CreateTextFile(test_path, L"test file");
873 EXPECT_TRUE(file_util::PathExists(test_path));
874 EXPECT_FALSE(file_util::DirectoryExists(test_path));
875 EXPECT_TRUE(file_util::Delete(test_path, false));
876
877 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +0900878}
879
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900880static const struct goodbad_pair {
initial.commit3f4a7322008-07-27 06:49:38 +0900881 std::wstring bad_name;
882 std::wstring good_name;
883} kIllegalCharacterCases[] = {
884 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
885 {L"**********::::.txt", L"--------------.txt"},
initial.commit3f4a7322008-07-27 06:49:38 +0900886 // We can't use UCNs (universal character names) for C0/C1 characters and
887 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
888 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
erikkay@google.com014161d2008-08-16 02:45:13 +0900889#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900890 {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
initial.commit3f4a7322008-07-27 06:49:38 +0900891 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900892#elif defined(OS_POSIX)
893 {L"bad*file?name.jpg", L"bad-file-name.jpg"},
894 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900895#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900896 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
897 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
898 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
899 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
900 // Unassigned codepoints are ok.
901 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
jshin@chromium.orgfdaedbf2009-06-25 01:44:49 +0900902 // Non-characters are not allowed.
903 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
904 {L"bad\uFDD0file\uFDEFname.jpg ", L"bad-file-name.jpg"},
initial.commit3f4a7322008-07-27 06:49:38 +0900905};
906
907TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900908 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900909 std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
910 file_util::ReplaceIllegalCharacters(&bad_name, L'-');
911 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
912 }
913}
914
915static const struct ReplaceExtensionCase {
916 std::wstring file_name;
estade@chromium.org63343202008-12-05 05:46:06 +0900917 FilePath::StringType extension;
initial.commit3f4a7322008-07-27 06:49:38 +0900918 std::wstring result;
919} kReplaceExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +0900920 {L"", FILE_PATH_LITERAL(""), L""},
921 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
922 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
923 {L".", FILE_PATH_LITERAL(""), L""},
924 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
925 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
926 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
927 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
928 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
929 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
930 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
931 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
932 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
933 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
934 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
935 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commit3f4a7322008-07-27 06:49:38 +0900936};
937
938TEST_F(FileUtilTest, ReplaceExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900939 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +0900940 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
941 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
942 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +0900943 }
944}
945
sky@google.com71e7c6f2008-09-20 02:32:18 +0900946// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
947// the directory names of the path.
948TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
estade@chromium.org63343202008-12-05 05:46:06 +0900949 FilePath path;
950 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
951 path = path.Append(FILE_PATH_LITERAL("foo"));
sky@google.com71e7c6f2008-09-20 02:32:18 +0900952 // '/foo.bar/foo' with extension '.baz'
estade@chromium.org63343202008-12-05 05:46:06 +0900953 FilePath result_path = path;
954 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900955 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
956 result_path.value());
sky@google.com71e7c6f2008-09-20 02:32:18 +0900957}
958
initial.commit3f4a7322008-07-27 06:49:38 +0900959TEST_F(FileUtilTest, FileEnumeratorTest) {
960 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900961 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +0900962 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
963 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +0900964
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900965 // Test an empty directory, non-recursively, including "..".
966 file_util::FileEnumerator f0_dotdot(test_dir_, false,
967 static_cast<file_util::FileEnumerator::FILE_TYPE>(
968 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
969 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
970 f0_dotdot.Next().value());
971 EXPECT_EQ(FILE_PATH_LITERAL(""),
972 f0_dotdot.Next().value());
973
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900974 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +0900975 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900976 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +0900977 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900978 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +0900979 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900980 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +0900981
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900982 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +0900983 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900984 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900985 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900986 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900987 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900988 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900989 FilePath file2_rel =
990 dir2.Append(FilePath::kParentDirectory)
991 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900992 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900993 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900994
995 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +0900996 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900997 file_util::FileEnumerator::FILES);
998 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900999 EXPECT_TRUE(c1.HasFile(file1));
1000 EXPECT_TRUE(c1.HasFile(file2_abs));
1001 EXPECT_TRUE(c1.HasFile(dir2file));
1002 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1003 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001004
1005 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001006 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001007 file_util::FileEnumerator::DIRECTORIES);
1008 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001009 EXPECT_TRUE(c2.HasFile(dir1));
1010 EXPECT_TRUE(c2.HasFile(dir2));
1011 EXPECT_TRUE(c2.HasFile(dir2inner));
1012 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001013
tim@chromium.org989d0972008-10-16 11:42:45 +09001014 // Only enumerate directories non-recursively.
1015 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001016 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001017 FindResultCollector c2_non_recursive(f2_non_recursive);
1018 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1019 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1020 EXPECT_EQ(c2_non_recursive.size(), 2);
1021
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001022 // Only enumerate directories, non-recursively, including "..".
1023 file_util::FileEnumerator f2_dotdot(
1024 test_dir_, false,
1025 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1026 file_util::FileEnumerator::DIRECTORIES |
1027 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1028 FindResultCollector c2_dotdot(f2_dotdot);
1029 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1030 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1031 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1032 EXPECT_EQ(c2_dotdot.size(), 3);
1033
initial.commit3f4a7322008-07-27 06:49:38 +09001034 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001035 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001036 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001037 EXPECT_TRUE(c3.HasFile(dir1));
1038 EXPECT_TRUE(c3.HasFile(dir2));
1039 EXPECT_TRUE(c3.HasFile(file1));
1040 EXPECT_TRUE(c3.HasFile(file2_abs));
1041 EXPECT_TRUE(c3.HasFile(dir2file));
1042 EXPECT_TRUE(c3.HasFile(dir2inner));
1043 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1044 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001045
1046 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001047 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001048 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001049 EXPECT_TRUE(c4.HasFile(dir2));
1050 EXPECT_TRUE(c4.HasFile(dir2));
1051 EXPECT_TRUE(c4.HasFile(file1));
1052 EXPECT_TRUE(c4.HasFile(file2_abs));
1053 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001054
1055 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001056 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001057 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001058 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001059 EXPECT_TRUE(c5.HasFile(dir1));
1060 EXPECT_TRUE(c5.HasFile(dir2));
1061 EXPECT_TRUE(c5.HasFile(dir2file));
1062 EXPECT_TRUE(c5.HasFile(dir2inner));
1063 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1064 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001065
1066 // Make sure the destructor closes the find handle while in the middle of a
1067 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001068 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001069 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1070 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001071}
license.botf003cfe2008-08-24 09:55:55 +09001072
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001073TEST_F(FileUtilTest, FileEnumeratorOrderTest) {
1074 FilePath fileA = test_dir_.Append(FILE_PATH_LITERAL("a"));
1075 FilePath fileB = test_dir_.Append(FILE_PATH_LITERAL("B"));
1076 FilePath dirC = test_dir_.Append(FILE_PATH_LITERAL("C"));
1077 FilePath dirD = test_dir_.Append(FILE_PATH_LITERAL("d"));
1078 FilePath dirE = test_dir_.Append(FILE_PATH_LITERAL("e"));
1079 FilePath fileF = test_dir_.Append(FILE_PATH_LITERAL("f"));
1080
1081 // Create files/directories in near random order.
1082 CreateTextFile(fileF, L"");
1083 CreateTextFile(fileA, L"");
1084 CreateTextFile(fileB, L"");
1085 EXPECT_TRUE(file_util::CreateDirectory(dirE));
1086 EXPECT_TRUE(file_util::CreateDirectory(dirC));
1087 EXPECT_TRUE(file_util::CreateDirectory(dirD));
1088
estade@chromium.org868ecbc2009-06-24 12:29:26 +09001089 // On Windows, files and directories are enumerated in the lexicographical
1090 // order, ignoring case and whether they are files or directories. On posix,
1091 // we order directories before files.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001092 file_util::FileEnumerator enumerator(test_dir_, false, FILES_AND_DIRECTORIES);
1093 FilePath cur_file = enumerator.Next();
estade@chromium.org868ecbc2009-06-24 12:29:26 +09001094#if defined(OS_WIN)
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001095 EXPECT_EQ(fileA.value(), cur_file.value());
1096 cur_file = enumerator.Next();
1097 EXPECT_EQ(fileB.value(), cur_file.value());
1098 cur_file = enumerator.Next();
1099 EXPECT_EQ(dirC.value(), cur_file.value());
1100 cur_file = enumerator.Next();
1101 EXPECT_EQ(dirD.value(), cur_file.value());
1102 cur_file = enumerator.Next();
1103 EXPECT_EQ(dirE.value(), cur_file.value());
1104 cur_file = enumerator.Next();
1105 EXPECT_EQ(fileF.value(), cur_file.value());
1106 cur_file = enumerator.Next();
estade@chromium.org868ecbc2009-06-24 12:29:26 +09001107#elif defined(OS_POSIX)
1108 EXPECT_EQ(dirC.value(), cur_file.value());
1109 cur_file = enumerator.Next();
1110 EXPECT_EQ(dirD.value(), cur_file.value());
1111 cur_file = enumerator.Next();
1112 EXPECT_EQ(dirE.value(), cur_file.value());
1113 cur_file = enumerator.Next();
1114 EXPECT_EQ(fileA.value(), cur_file.value());
1115 cur_file = enumerator.Next();
1116 EXPECT_EQ(fileB.value(), cur_file.value());
1117 cur_file = enumerator.Next();
1118 EXPECT_EQ(fileF.value(), cur_file.value());
1119 cur_file = enumerator.Next();
1120#endif
1121
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001122 EXPECT_EQ(FILE_PATH_LITERAL(""), cur_file.value());
1123}
estade@chromium.org97e37822008-11-27 13:03:57 +09001124
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001125TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001126 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001127
1128 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001129 if (file_util::PathExists(data_dir)) {
1130 ASSERT_TRUE(file_util::Delete(data_dir, true));
1131 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001132 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1133
1134 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1135 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1136 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1137 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1138
1139 // Annoyingly, the directories must actually exist in order for realpath(),
1140 // which Contains() relies on in posix, to work.
1141 ASSERT_TRUE(file_util::CreateDirectory(foo));
1142 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001143 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1144 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1145 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001146
1147 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1148 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1149 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1150 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1151
1152// Platform-specific concerns
1153 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1154#if defined(OS_WIN)
1155 EXPECT_TRUE(file_util::ContainsPath(foo,
1156 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001157 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001158 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
1159#elif defined(OS_LINUX)
1160 EXPECT_FALSE(file_util::ContainsPath(foo,
1161 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
1162#else
1163 // We can't really do this test on osx since the case-sensitivity of the
1164 // filesystem is configurable.
1165#endif
1166}
1167
mark@chromium.org17684802008-09-10 09:16:28 +09001168} // namespace