blob: e0884f7cd18533d2303df01d4691b93c31dbb457 [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
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900632// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +0900633#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900634TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900635 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900636 CreateTextFile(target_file, L"This is the target.");
637
evanm@google.com874d1672008-10-31 08:54:04 +0900638 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900639
640 HRESULT result;
641 IShellLink *shell = NULL;
642 IPersistFile *persist = NULL;
643
644 CoInitialize(NULL);
645 // Temporarily create a shortcut for test
646 result = CoCreateInstance(CLSID_ShellLink, NULL,
647 CLSCTX_INPROC_SERVER, IID_IShellLink,
648 reinterpret_cast<LPVOID*>(&shell));
649 EXPECT_TRUE(SUCCEEDED(result));
650 result = shell->QueryInterface(IID_IPersistFile,
651 reinterpret_cast<LPVOID*>(&persist));
652 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900653 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900654 EXPECT_TRUE(SUCCEEDED(result));
655 result = shell->SetDescription(L"ResolveShortcutTest");
656 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900657 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +0900658 EXPECT_TRUE(SUCCEEDED(result));
659 if (persist)
660 persist->Release();
661 if (shell)
662 shell->Release();
663
664 bool is_solved;
evanm@google.com874d1672008-10-31 08:54:04 +0900665 std::wstring link_file_str = link_file.value();
666 is_solved = file_util::ResolveShortcut(&link_file_str);
initial.commit3f4a7322008-07-27 06:49:38 +0900667 EXPECT_TRUE(is_solved);
668 std::wstring contents;
evanm@google.com874d1672008-10-31 08:54:04 +0900669 contents = ReadTextFile(FilePath(link_file_str));
initial.commit3f4a7322008-07-27 06:49:38 +0900670 EXPECT_EQ(L"This is the target.", contents);
671
ericroman@google.comdbff4f52008-08-19 01:00:38 +0900672 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +0900673 DeleteFile(target_file.value().c_str());
674 DeleteFile(link_file_str.c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900675 CoUninitialize();
676}
677
678TEST_F(FileUtilTest, CreateShortcutTest) {
679 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +0900680 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900681 CreateTextFile(target_file, file_contents);
682
evanm@google.com874d1672008-10-31 08:54:04 +0900683 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900684
685 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +0900686 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
687 link_file.value().c_str(),
initial.commit3f4a7322008-07-27 06:49:38 +0900688 NULL, NULL, NULL, NULL, 0));
evanm@google.com874d1672008-10-31 08:54:04 +0900689 std::wstring resolved_name = link_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900690 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evanm@google.com874d1672008-10-31 08:54:04 +0900691 std::wstring read_contents = ReadTextFile(FilePath(resolved_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900692 EXPECT_EQ(file_contents, read_contents);
693
evanm@google.com874d1672008-10-31 08:54:04 +0900694 DeleteFile(target_file.value().c_str());
695 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900696 CoUninitialize();
697}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +0900698
699TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
700 // Create a directory
701 FilePath dir_name_from =
702 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
703 file_util::CreateDirectory(dir_name_from);
704 ASSERT_TRUE(file_util::PathExists(dir_name_from));
705
706 // Create a file under the directory
707 FilePath file_name_from =
708 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
709 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
710 ASSERT_TRUE(file_util::PathExists(file_name_from));
711
712 // Move the directory by using CopyAndDeleteDirectory
713 FilePath dir_name_to = test_dir_.Append(
714 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
715 FilePath file_name_to =
716 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
717
718 ASSERT_FALSE(file_util::PathExists(dir_name_to));
719
720 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
721
722 // Check everything has been moved.
723 EXPECT_FALSE(file_util::PathExists(dir_name_from));
724 EXPECT_FALSE(file_util::PathExists(file_name_from));
725 EXPECT_TRUE(file_util::PathExists(dir_name_to));
726 EXPECT_TRUE(file_util::PathExists(file_name_to));
727}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900728#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900729
730TEST_F(FileUtilTest, CreateTemporaryFileNameTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900731 std::wstring temp_files[3];
732 for (int i = 0; i < 3; i++) {
733 ASSERT_TRUE(file_util::CreateTemporaryFileName(&(temp_files[i])));
734 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
735 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
736 }
737 for (int i = 0; i < 3; i++)
738 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
739 for (int i = 0; i < 3; i++)
740 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
741}
742
743TEST_F(FileUtilTest, CreateAndOpenTemporaryFileNameTest) {
744 FilePath names[3];
745 FILE *fps[3];
746 int i;
747
748 // Create; make sure they are open and exist.
749 for (i = 0; i < 3; ++i) {
750 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
751 ASSERT_TRUE(fps[i]);
752 EXPECT_TRUE(file_util::PathExists(names[i]));
753 }
754
755 // Make sure all names are unique.
756 for (i = 0; i < 3; ++i) {
757 EXPECT_FALSE(names[i] == names[(i+1)%3]);
758 }
759
760 // Close and delete.
761 for (i = 0; i < 3; ++i) {
762 EXPECT_TRUE(file_util::CloseFile(fps[i]));
763 EXPECT_TRUE(file_util::Delete(names[i], false));
764 }
initial.commit3f4a7322008-07-27 06:49:38 +0900765}
766
767TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
768 std::wstring temp_dir;
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900769 ASSERT_TRUE(file_util::CreateNewTempDirectory(std::wstring(), &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900770 EXPECT_TRUE(file_util::PathExists(temp_dir));
771 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +0900772}
773
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900774TEST_F(FileUtilTest, GetShmemTempDirTest) {
775 FilePath dir;
776 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
777 EXPECT_TRUE(file_util::DirectoryExists(dir));
778}
779
initial.commit3f4a7322008-07-27 06:49:38 +0900780TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900781 FilePath test_root =
782 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +0900783#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +0900784 FilePath test_path =
785 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900786#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +0900787 FilePath test_path =
788 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900789#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +0900790
791 EXPECT_FALSE(file_util::PathExists(test_path));
792 EXPECT_TRUE(file_util::CreateDirectory(test_path));
793 EXPECT_TRUE(file_util::PathExists(test_path));
794 // CreateDirectory returns true if the DirectoryExists returns true.
795 EXPECT_TRUE(file_util::CreateDirectory(test_path));
796
797 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +0900798 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900799 EXPECT_FALSE(file_util::PathExists(test_path));
800 CreateTextFile(test_path, L"test file");
801 EXPECT_TRUE(file_util::PathExists(test_path));
802 EXPECT_FALSE(file_util::CreateDirectory(test_path));
803
804 EXPECT_TRUE(file_util::Delete(test_root, true));
805 EXPECT_FALSE(file_util::PathExists(test_root));
806 EXPECT_FALSE(file_util::PathExists(test_path));
807}
808
809TEST_F(FileUtilTest, DetectDirectoryTest) {
810 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900811 FilePath test_root =
812 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900813 EXPECT_FALSE(file_util::PathExists(test_root));
814 EXPECT_TRUE(file_util::CreateDirectory(test_root));
815 EXPECT_TRUE(file_util::PathExists(test_root));
816 EXPECT_TRUE(file_util::DirectoryExists(test_root));
817
818 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +0900819 FilePath test_path =
820 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900821 EXPECT_FALSE(file_util::PathExists(test_path));
822 CreateTextFile(test_path, L"test file");
823 EXPECT_TRUE(file_util::PathExists(test_path));
824 EXPECT_FALSE(file_util::DirectoryExists(test_path));
825 EXPECT_TRUE(file_util::Delete(test_path, false));
826
827 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +0900828}
829
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900830static const struct goodbad_pair {
initial.commit3f4a7322008-07-27 06:49:38 +0900831 std::wstring bad_name;
832 std::wstring good_name;
833} kIllegalCharacterCases[] = {
834 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
835 {L"**********::::.txt", L"--------------.txt"},
initial.commit3f4a7322008-07-27 06:49:38 +0900836 // We can't use UCNs (universal character names) for C0/C1 characters and
837 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
838 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
erikkay@google.com014161d2008-08-16 02:45:13 +0900839#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900840 {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
initial.commit3f4a7322008-07-27 06:49:38 +0900841 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
842 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900843#elif defined(OS_POSIX)
844 {L"bad*file?name.jpg", L"bad-file-name.jpg"},
845 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"},
846 {L"bad\uFFFFfile-name.jpg ", L"bad-file-name.jpg"},
847#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900848 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
849 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
850 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
851 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
852 // Unassigned codepoints are ok.
853 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
854};
855
856TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900857 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900858 std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
859 file_util::ReplaceIllegalCharacters(&bad_name, L'-');
860 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
861 }
862}
863
864static const struct ReplaceExtensionCase {
865 std::wstring file_name;
estade@chromium.org63343202008-12-05 05:46:06 +0900866 FilePath::StringType extension;
initial.commit3f4a7322008-07-27 06:49:38 +0900867 std::wstring result;
868} kReplaceExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +0900869 {L"", FILE_PATH_LITERAL(""), L""},
870 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
871 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
872 {L".", FILE_PATH_LITERAL(""), L""},
873 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
874 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
875 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
876 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
877 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
878 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
879 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
880 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
881 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
882 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
883 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
884 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commit3f4a7322008-07-27 06:49:38 +0900885};
886
887TEST_F(FileUtilTest, ReplaceExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900888 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +0900889 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
890 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
891 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +0900892 }
893}
894
sky@google.com71e7c6f2008-09-20 02:32:18 +0900895// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
896// the directory names of the path.
897TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
estade@chromium.org63343202008-12-05 05:46:06 +0900898 FilePath path;
899 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
900 path = path.Append(FILE_PATH_LITERAL("foo"));
sky@google.com71e7c6f2008-09-20 02:32:18 +0900901 // '/foo.bar/foo' with extension '.baz'
estade@chromium.org63343202008-12-05 05:46:06 +0900902 FilePath result_path = path;
903 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900904 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
905 result_path.value());
sky@google.com71e7c6f2008-09-20 02:32:18 +0900906}
907
initial.commit3f4a7322008-07-27 06:49:38 +0900908TEST_F(FileUtilTest, FileEnumeratorTest) {
909 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900910 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +0900911 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
912 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +0900913
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900914 // Test an empty directory, non-recursively, including "..".
915 file_util::FileEnumerator f0_dotdot(test_dir_, false,
916 static_cast<file_util::FileEnumerator::FILE_TYPE>(
917 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
918 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
919 f0_dotdot.Next().value());
920 EXPECT_EQ(FILE_PATH_LITERAL(""),
921 f0_dotdot.Next().value());
922
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900923 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +0900924 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900925 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +0900926 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900927 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +0900928 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900929 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +0900930
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900931 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +0900932 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900933 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900934 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900935 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900936 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900937 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900938 FilePath file2_rel =
939 dir2.Append(FilePath::kParentDirectory)
940 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900941 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900942 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900943
944 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +0900945 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900946 file_util::FileEnumerator::FILES);
947 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900948 EXPECT_TRUE(c1.HasFile(file1));
949 EXPECT_TRUE(c1.HasFile(file2_abs));
950 EXPECT_TRUE(c1.HasFile(dir2file));
951 EXPECT_TRUE(c1.HasFile(dir2innerfile));
952 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +0900953
954 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +0900955 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900956 file_util::FileEnumerator::DIRECTORIES);
957 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900958 EXPECT_TRUE(c2.HasFile(dir1));
959 EXPECT_TRUE(c2.HasFile(dir2));
960 EXPECT_TRUE(c2.HasFile(dir2inner));
961 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +0900962
tim@chromium.org989d0972008-10-16 11:42:45 +0900963 // Only enumerate directories non-recursively.
964 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +0900965 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +0900966 FindResultCollector c2_non_recursive(f2_non_recursive);
967 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
968 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
969 EXPECT_EQ(c2_non_recursive.size(), 2);
970
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900971 // Only enumerate directories, non-recursively, including "..".
972 file_util::FileEnumerator f2_dotdot(
973 test_dir_, false,
974 static_cast<file_util::FileEnumerator::FILE_TYPE>(
975 file_util::FileEnumerator::DIRECTORIES |
976 file_util::FileEnumerator::INCLUDE_DOT_DOT));
977 FindResultCollector c2_dotdot(f2_dotdot);
978 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
979 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
980 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
981 EXPECT_EQ(c2_dotdot.size(), 3);
982
initial.commit3f4a7322008-07-27 06:49:38 +0900983 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900984 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +0900985 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900986 EXPECT_TRUE(c3.HasFile(dir1));
987 EXPECT_TRUE(c3.HasFile(dir2));
988 EXPECT_TRUE(c3.HasFile(file1));
989 EXPECT_TRUE(c3.HasFile(file2_abs));
990 EXPECT_TRUE(c3.HasFile(dir2file));
991 EXPECT_TRUE(c3.HasFile(dir2inner));
992 EXPECT_TRUE(c3.HasFile(dir2innerfile));
993 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +0900994
995 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900996 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +0900997 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900998 EXPECT_TRUE(c4.HasFile(dir2));
999 EXPECT_TRUE(c4.HasFile(dir2));
1000 EXPECT_TRUE(c4.HasFile(file1));
1001 EXPECT_TRUE(c4.HasFile(file2_abs));
1002 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001003
1004 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001005 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001006 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001007 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001008 EXPECT_TRUE(c5.HasFile(dir1));
1009 EXPECT_TRUE(c5.HasFile(dir2));
1010 EXPECT_TRUE(c5.HasFile(dir2file));
1011 EXPECT_TRUE(c5.HasFile(dir2inner));
1012 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1013 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001014
1015 // Make sure the destructor closes the find handle while in the middle of a
1016 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001017 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001018 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1019 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001020}
license.botf003cfe2008-08-24 09:55:55 +09001021
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001022TEST_F(FileUtilTest, FileEnumeratorOrderTest) {
1023 FilePath fileA = test_dir_.Append(FILE_PATH_LITERAL("a"));
1024 FilePath fileB = test_dir_.Append(FILE_PATH_LITERAL("B"));
1025 FilePath dirC = test_dir_.Append(FILE_PATH_LITERAL("C"));
1026 FilePath dirD = test_dir_.Append(FILE_PATH_LITERAL("d"));
1027 FilePath dirE = test_dir_.Append(FILE_PATH_LITERAL("e"));
1028 FilePath fileF = test_dir_.Append(FILE_PATH_LITERAL("f"));
1029
1030 // Create files/directories in near random order.
1031 CreateTextFile(fileF, L"");
1032 CreateTextFile(fileA, L"");
1033 CreateTextFile(fileB, L"");
1034 EXPECT_TRUE(file_util::CreateDirectory(dirE));
1035 EXPECT_TRUE(file_util::CreateDirectory(dirC));
1036 EXPECT_TRUE(file_util::CreateDirectory(dirD));
1037
estade@chromium.org868ecbc2009-06-24 12:29:26 +09001038 // On Windows, files and directories are enumerated in the lexicographical
1039 // order, ignoring case and whether they are files or directories. On posix,
1040 // we order directories before files.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001041 file_util::FileEnumerator enumerator(test_dir_, false, FILES_AND_DIRECTORIES);
1042 FilePath cur_file = enumerator.Next();
estade@chromium.org868ecbc2009-06-24 12:29:26 +09001043#if defined(OS_WIN)
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001044 EXPECT_EQ(fileA.value(), cur_file.value());
1045 cur_file = enumerator.Next();
1046 EXPECT_EQ(fileB.value(), cur_file.value());
1047 cur_file = enumerator.Next();
1048 EXPECT_EQ(dirC.value(), cur_file.value());
1049 cur_file = enumerator.Next();
1050 EXPECT_EQ(dirD.value(), cur_file.value());
1051 cur_file = enumerator.Next();
1052 EXPECT_EQ(dirE.value(), cur_file.value());
1053 cur_file = enumerator.Next();
1054 EXPECT_EQ(fileF.value(), cur_file.value());
1055 cur_file = enumerator.Next();
estade@chromium.org868ecbc2009-06-24 12:29:26 +09001056#elif defined(OS_POSIX)
1057 EXPECT_EQ(dirC.value(), cur_file.value());
1058 cur_file = enumerator.Next();
1059 EXPECT_EQ(dirD.value(), cur_file.value());
1060 cur_file = enumerator.Next();
1061 EXPECT_EQ(dirE.value(), cur_file.value());
1062 cur_file = enumerator.Next();
1063 EXPECT_EQ(fileA.value(), cur_file.value());
1064 cur_file = enumerator.Next();
1065 EXPECT_EQ(fileB.value(), cur_file.value());
1066 cur_file = enumerator.Next();
1067 EXPECT_EQ(fileF.value(), cur_file.value());
1068 cur_file = enumerator.Next();
1069#endif
1070
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001071 EXPECT_EQ(FILE_PATH_LITERAL(""), cur_file.value());
1072}
estade@chromium.org97e37822008-11-27 13:03:57 +09001073
1074void PathComponents(const std::wstring& path,
1075 std::vector<std::wstring>* components) {
1076 DCHECK(components != NULL);
1077 if (components == NULL)
1078 return;
1079 std::wstring::size_type start = 0;
1080 std::wstring::size_type end = path.find('/', start);
1081
1082 // Special case the "/" or "\" directory. On Windows with a drive letter,
1083 // this code path won't hit, but the right thing should still happen.
1084 // "E:\foo" will turn into "E:","foo".
1085 if (end == start) {
1086 components->push_back(std::wstring(path, 0, 1));
1087 start = end + 1;
1088 end = path.find('/', start);
1089 }
1090 while (end != std::wstring::npos) {
1091 std::wstring component = std::wstring(path, start, end - start);
1092 components->push_back(component);
1093 start = end + 1;
1094 end = path.find('/', start);
1095 }
1096 std::wstring component = std::wstring(path, start);
1097 components->push_back(component);
1098}
1099
1100static const struct PathComponentsCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001101 const FilePath::CharType* path;
1102 const FilePath::CharType* result;
estade@chromium.org97e37822008-11-27 13:03:57 +09001103} kPathComponents[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001104 {FILE_PATH_LITERAL("/foo/bar/baz/"), FILE_PATH_LITERAL("/|foo|bar|baz|")},
1105 {FILE_PATH_LITERAL("/foo/bar/baz"), FILE_PATH_LITERAL("/|foo|bar|baz")},
1106 {FILE_PATH_LITERAL("e:/foo"), FILE_PATH_LITERAL("e:|foo")},
estade@chromium.org97e37822008-11-27 13:03:57 +09001107};
1108
1109TEST_F(FileUtilTest, PathComponentsTest) {
1110 for (size_t i = 0; i < arraysize(kPathComponents); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001111 FilePath path(kPathComponents[i].path);
estade@chromium.org97e37822008-11-27 13:03:57 +09001112 std::vector<FilePath::StringType> comps;
1113 file_util::PathComponents(path, &comps);
1114
1115 FilePath::StringType result;
1116 for (size_t j = 0; j < comps.size(); ++j) {
1117 result.append(comps[j]);
1118 if (j < comps.size() - 1)
1119 result.append(FILE_PATH_LITERAL("|"), 1);
1120 }
1121 EXPECT_EQ(kPathComponents[i].result, result);
1122 }
1123}
1124
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