blob: 7f24fc52ee8c8f41eb64f77e55f3c2e075e677d3 [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"
22#include "base/string_util.h"
23#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090024#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090025
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090026// This macro helps avoid wrapped lines in the test structs.
27#define FPL(x) FILE_PATH_LITERAL(x)
28
initial.commit3f4a7322008-07-27 06:49:38 +090029namespace {
30
erikkay@google.comf2406842008-08-21 00:59:49 +090031// file_util winds up using autoreleased objects on the Mac, so this needs
32// to be a PlatformTest
33class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +090034 protected:
35 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +090036 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +090037 // Name a subdirectory of the temp directory.
38 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +090039 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +090040
41 // Create a fresh, empty copy of this directory.
42 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +090043 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +090044 }
45 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +090046 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +090047 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +090048 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +090049 ASSERT_FALSE(file_util::PathExists(test_dir_));
50 }
51
52 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +090053 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +090054};
55
56// Collects all the results from the given file enumerator, and provides an
57// interface to query whether a given file is present.
58class FindResultCollector {
59 public:
60 FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +090061 FilePath cur_file;
62 while (!(cur_file = enumerator.Next()).value().empty()) {
63 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +090064 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +090065 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +090066 << "Same file returned twice";
67
68 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +090069 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +090070 }
71 }
72
73 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +090074 bool HasFile(const FilePath& file) const {
75 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +090076 }
evanm@google.com874d1672008-10-31 08:54:04 +090077
erikkay@google.comdfb51b22008-08-16 02:32:10 +090078 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +090079 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +090080 }
initial.commit3f4a7322008-07-27 06:49:38 +090081
82 private:
evanm@google.com874d1672008-10-31 08:54:04 +090083 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +090084};
85
86// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +090087void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +090088 const std::wstring& contents) {
89 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +090090 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +090091 ASSERT_TRUE(file.is_open());
92 file << contents;
93 file.close();
94}
95
96// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +090097std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +090098 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +090099 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900100 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900101 EXPECT_TRUE(file.is_open());
102 file.getline(contents, 64);
103 file.close();
104 return std::wstring(contents);
105}
106
erikkay@google.com014161d2008-08-16 02:45:13 +0900107#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900108uint64 FileTimeAsUint64(const FILETIME& ft) {
109 ULARGE_INTEGER u;
110 u.LowPart = ft.dwLowDateTime;
111 u.HighPart = ft.dwHighDateTime;
112 return u.QuadPart;
113}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900114#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900115
116const struct append_case {
117 const wchar_t* path;
118 const wchar_t* ending;
119 const wchar_t* result;
120} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900121#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900122 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
123 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
124 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
125 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
126 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
127 {L"", L"path", L"\\path"},
128 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900129#elif defined(OS_POSIX)
130 {L"/foo/bar", L"path", L"/foo/bar/path"},
131 {L"/foo/bar/", L"path", L"/foo/bar/path"},
132 {L"/foo/bar//", L"path", L"/foo/bar//path"},
133 {L"/foo/bar/", L"", L"/foo/bar/"},
134 {L"/foo/bar", L"", L"/foo/bar/"},
135 {L"", L"path", L"/path"},
136 {L"", L"", L"/"},
137#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900138};
139
initial.commit3f4a7322008-07-27 06:49:38 +0900140TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900141 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900142 const append_case& value = append_cases[i];
143 std::wstring result = value.path;
144 file_util::AppendToPath(&result, value.ending);
145 EXPECT_EQ(value.result, result);
146 }
147
148#ifdef NDEBUG
149 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
150#endif
151}
152
153static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900154 const FilePath::CharType* path;
155 const FilePath::CharType* suffix;
156 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900157} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900158 {FPL(""), FPL(""), FPL("")},
159 {FPL(""), FPL("txt"), FPL("txt")},
160 {FPL("."), FPL("txt"), FPL("txt.")},
161 {FPL("."), FPL(""), FPL(".")},
162 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
163 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
164 {FPL("foo"), FPL("txt"), FPL("footxt")},
165 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
166 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
167 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
168 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
169 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
170 {FPL("foo"), FPL(""), FPL("foo")},
171 {FPL("foo"), FPL("."), FPL("foo.")},
172 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
173 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900174#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900175 {FPL("\\"), FPL(""), FPL("\\")},
176 {FPL("\\"), FPL("txt"), FPL("\\txt")},
177 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
178 {FPL("\\."), FPL(""), FPL("\\.")},
179 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
180 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
181 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
182 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
183 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
184 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
185 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
186 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
187 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900188#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900189 {FPL("/"), FPL(""), FPL("/")},
190 {FPL("/"), FPL("txt"), FPL("/txt")},
191 {FPL("/."), FPL("txt"), FPL("/txt.")},
192 {FPL("/."), FPL(""), FPL("/.")},
193 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
194 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
195 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
196 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
197 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
198 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
199 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
200 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900201#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900202};
203
204TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900205 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900206 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900207 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900208 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900209 }
210}
211
212static const struct filename_case {
213 const wchar_t* path;
214 const wchar_t* filename;
215} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900216#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900217 {L"c:\\colon\\backslash", L"backslash"},
218 {L"c:\\colon\\backslash\\", L""},
219 {L"\\\\filename.exe", L"filename.exe"},
220 {L"filename.exe", L"filename.exe"},
221 {L"", L""},
222 {L"\\\\\\", L""},
223 {L"c:/colon/backslash", L"backslash"},
224 {L"c:/colon/backslash/", L""},
225 {L"//////", L""},
226 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900227#elif defined(OS_POSIX)
228 {L"/foo/bar", L"bar"},
229 {L"/foo/bar/", L""},
230 {L"/filename.exe", L"filename.exe"},
231 {L"filename.exe", L"filename.exe"},
232 {L"", L""},
233 {L"/", L""},
234#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900235};
236
237TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900238 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900239 const filename_case& value = filename_cases[i];
240 std::wstring result = file_util::GetFilenameFromPath(value.path);
241 EXPECT_EQ(value.filename, result);
242 }
243}
244
245// Test finding the file type from a path name
246static const struct extension_case {
247 const wchar_t* path;
248 const wchar_t* extension;
249} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900250#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900251 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
252 {L"C:\\colon\\backslash\\filename.", L""},
253 {L"C:\\colon\\backslash\\filename", L""},
254 {L"C:\\colon\\backslash\\", L""},
255 {L"C:\\colon\\backslash.\\", L""},
256 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900257#elif defined(OS_POSIX)
258 {L"/foo/bar/filename.extension", L"extension"},
259 {L"/foo/bar/filename.", L""},
260 {L"/foo/bar/filename", L""},
261 {L"/foo/bar/", L""},
262 {L"/foo/bar./", L""},
263 {L"/foo/bar/filename.extension.extension2", L"extension2"},
264 {L".", L""},
265 {L"..", L""},
266 {L"./foo", L""},
267 {L"./foo.extension", L"extension"},
268 {L"/foo.extension1/bar.extension2", L"extension2"},
269#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900270};
271
272TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900273 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900274 const extension_case& ext = extension_cases[i];
275 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
276 EXPECT_EQ(ext.extension, fext);
277 }
278}
279
280// Test finding the directory component of a path
281static const struct dir_case {
282 const wchar_t* full_path;
283 const wchar_t* directory;
284} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900285#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900286 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
287 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
288 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
289 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
290 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
291 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
292 {L"C:\\", L"C:"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900293#elif defined(OS_POSIX)
294 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
295 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
296 {L"/foo/bar/", L"/foo/bar"},
297 {L"/foo/bar//", L"/foo/bar"},
298 {L"/foo/bar", L"/foo"},
299 {L"/foo/bar./", L"/foo/bar."},
300 {L"/", L"/"},
301 {L".", L"."},
302 {L"..", L"."}, // yes, ".." technically lives in "."
303#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900304};
305
306TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900307 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900308 const dir_case& dir = dir_cases[i];
309 const std::wstring parent =
310 file_util::GetDirectoryFromPath(dir.full_path);
311 EXPECT_EQ(dir.directory, parent);
312 }
313}
314
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900315// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900316#if defined OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +0900317TEST_F(FileUtilTest, CountFilesCreatedAfter) {
318 // Create old file (that we don't want to count)
erikkay@google.com3c580142009-04-16 01:58:27 +0900319 FilePath old_file_name = test_dir_.Append(L"Old File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900320 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
321
322 // Age to perfection
323 Sleep(100);
324
325 // Establish our cutoff time
erikkay@google.com3c580142009-04-16 01:58:27 +0900326 FILETIME test_start_time;
327 GetSystemTimeAsFileTime(&test_start_time);
328 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(),
329 test_start_time));
initial.commit3f4a7322008-07-27 06:49:38 +0900330
331 // Create a new file (that we do want to count)
erikkay@google.com3c580142009-04-16 01:58:27 +0900332 FilePath new_file_name = test_dir_.Append(L"New File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900333 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
334
335 // We should see only the new file.
erikkay@google.com3c580142009-04-16 01:58:27 +0900336 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_.value(),
337 test_start_time));
initial.commit3f4a7322008-07-27 06:49:38 +0900338
339 // Delete new file, we should see no files after cutoff now
340 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com3c580142009-04-16 01:58:27 +0900341 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(),
342 test_start_time));
initial.commit3f4a7322008-07-27 06:49:38 +0900343}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900344#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900345
346// Tests that the Delete function works as expected, especially
347// the recursion flag. Also coincidentally tests PathExists.
348TEST_F(FileUtilTest, Delete) {
349 // Create a file
evanm@google.com874d1672008-10-31 08:54:04 +0900350 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900351 CreateTextFile(file_name, L"I'm cannon fodder.");
352
353 ASSERT_TRUE(file_util::PathExists(file_name));
354
evanm@google.com874d1672008-10-31 08:54:04 +0900355 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
356 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900357
358 ASSERT_TRUE(file_util::PathExists(subdir_path));
359
evanm@google.com874d1672008-10-31 08:54:04 +0900360 FilePath directory_contents = test_dir_;
erikkay@google.com014161d2008-08-16 02:45:13 +0900361#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900362 // TODO(erikkay): see if anyone's actually using this feature of the API
evanm@google.com874d1672008-10-31 08:54:04 +0900363 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900364 // Delete non-recursively and check that only the file is deleted
365 ASSERT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900366 EXPECT_FALSE(file_util::PathExists(file_name));
367 EXPECT_TRUE(file_util::PathExists(subdir_path));
368#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900369
370 // Delete recursively and make sure all contents are deleted
371 ASSERT_TRUE(file_util::Delete(directory_contents, true));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900372 EXPECT_FALSE(file_util::PathExists(file_name));
373 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900374}
375
376TEST_F(FileUtilTest, Move) {
377 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900378 FilePath dir_name_from =
379 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
380 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900381 ASSERT_TRUE(file_util::PathExists(dir_name_from));
382
383 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900384 FilePath file_name_from =
385 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900386 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
387 ASSERT_TRUE(file_util::PathExists(file_name_from));
388
389 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900390 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
391 FilePath file_name_to =
392 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900393
394 ASSERT_FALSE(file_util::PathExists(dir_name_to));
395
396 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
397
398 // Check everything has been moved.
399 EXPECT_FALSE(file_util::PathExists(dir_name_from));
400 EXPECT_FALSE(file_util::PathExists(file_name_from));
401 EXPECT_TRUE(file_util::PathExists(dir_name_to));
402 EXPECT_TRUE(file_util::PathExists(file_name_to));
403}
404
405TEST_F(FileUtilTest, CopyDirectoryRecursively) {
406 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900407 FilePath dir_name_from =
408 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
409 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900410 ASSERT_TRUE(file_util::PathExists(dir_name_from));
411
412 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900413 FilePath file_name_from =
414 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900415 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
416 ASSERT_TRUE(file_util::PathExists(file_name_from));
417
418 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900419 FilePath subdir_name_from =
420 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
421 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900422 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
423
424 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900425 FilePath file_name2_from =
426 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900427 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
428 ASSERT_TRUE(file_util::PathExists(file_name2_from));
429
430 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900431 FilePath dir_name_to =
432 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
433 FilePath file_name_to =
434 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
435 FilePath subdir_name_to =
436 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
437 FilePath file_name2_to =
438 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900439
440 ASSERT_FALSE(file_util::PathExists(dir_name_to));
441
442 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
443
444 // Check everything has been copied.
445 EXPECT_TRUE(file_util::PathExists(dir_name_from));
446 EXPECT_TRUE(file_util::PathExists(file_name_from));
447 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
448 EXPECT_TRUE(file_util::PathExists(file_name2_from));
449 EXPECT_TRUE(file_util::PathExists(dir_name_to));
450 EXPECT_TRUE(file_util::PathExists(file_name_to));
451 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
452 EXPECT_TRUE(file_util::PathExists(file_name2_to));
453}
454
455TEST_F(FileUtilTest, CopyDirectory) {
456 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900457 FilePath dir_name_from =
458 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
459 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900460 ASSERT_TRUE(file_util::PathExists(dir_name_from));
461
462 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900463 FilePath file_name_from =
464 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900465 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
466 ASSERT_TRUE(file_util::PathExists(file_name_from));
467
468 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900469 FilePath subdir_name_from =
470 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
471 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900472 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
473
474 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900475 FilePath file_name2_from =
476 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900477 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
478 ASSERT_TRUE(file_util::PathExists(file_name2_from));
479
480 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900481 FilePath dir_name_to =
482 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
483 FilePath file_name_to =
484 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
485 FilePath subdir_name_to =
486 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +0900487
488 ASSERT_FALSE(file_util::PathExists(dir_name_to));
489
490 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
491
492 // Check everything has been copied.
493 EXPECT_TRUE(file_util::PathExists(dir_name_from));
494 EXPECT_TRUE(file_util::PathExists(file_name_from));
495 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
496 EXPECT_TRUE(file_util::PathExists(file_name2_from));
497 EXPECT_TRUE(file_util::PathExists(dir_name_to));
498 EXPECT_TRUE(file_util::PathExists(file_name_to));
499 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
500}
501
502TEST_F(FileUtilTest, CopyFile) {
503 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900504 FilePath dir_name_from =
505 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
506 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900507 ASSERT_TRUE(file_util::PathExists(dir_name_from));
508
509 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900510 FilePath file_name_from =
511 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900512 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
513 CreateTextFile(file_name_from, file_contents);
514 ASSERT_TRUE(file_util::PathExists(file_name_from));
515
516 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900517 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900518 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900519
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900520 // Copy the file to another location using '..' in the path.
evanm@google.com874d1672008-10-31 08:54:04 +0900521 std::wstring dest_file2(dir_name_from.ToWStringHack());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900522 file_util::AppendToPath(&dest_file2, L"..");
523 file_util::AppendToPath(&dest_file2, L"DestFile.txt");
evanm@google.com874d1672008-10-31 08:54:04 +0900524 ASSERT_TRUE(file_util::CopyFile(file_name_from,
525 FilePath::FromWStringHack(dest_file2)));
526 std::wstring dest_file2_test(dir_name_from.ToWStringHack());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900527 file_util::UpOneDirectory(&dest_file2_test);
528 file_util::AppendToPath(&dest_file2_test, L"DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900529
530 // Check everything has been copied.
531 EXPECT_TRUE(file_util::PathExists(file_name_from));
532 EXPECT_TRUE(file_util::PathExists(dest_file));
533 const std::wstring read_contents = ReadTextFile(dest_file);
534 EXPECT_EQ(file_contents, read_contents);
evanm@google.com874d1672008-10-31 08:54:04 +0900535 EXPECT_TRUE(file_util::PathExists(
536 FilePath::FromWStringHack(dest_file2_test)));
537 EXPECT_TRUE(file_util::PathExists(FilePath::FromWStringHack(dest_file2)));
initial.commit3f4a7322008-07-27 06:49:38 +0900538}
539
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900540// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900541#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900542TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +0900543 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900544
545 SYSTEMTIME start_time;
546 GetLocalTime(&start_time);
547 Sleep(100);
548 CreateTextFile(file_name, L"New file!");
549 Sleep(100);
550 SYSTEMTIME end_time;
551 GetLocalTime(&end_time);
552
553 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +0900554 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +0900555
556 FILETIME start_filetime;
557 SystemTimeToFileTime(&start_time, &start_filetime);
558 FILETIME end_filetime;
559 SystemTimeToFileTime(&end_time, &end_filetime);
560 FILETIME file_creation_filetime;
561 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
562
563 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
564 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
565 "creation time: " << FileTimeAsUint64(file_creation_filetime);
566
567 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
568 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
569 "end time: " << FileTimeAsUint64(end_filetime);
570
evanm@google.com874d1672008-10-31 08:54:04 +0900571 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +0900572}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900573#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900574
erikkay@google.comf2406842008-08-21 00:59:49 +0900575// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +0900576// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +0900577typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +0900578
erikkay@google.comf2406842008-08-21 00:59:49 +0900579TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +0900580 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +0900581 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +0900582 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
583 .Append(FILE_PATH_LITERAL("data"))
584 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900585 ASSERT_TRUE(file_util::PathExists(data_dir));
586
evanm@google.com874d1672008-10-31 08:54:04 +0900587 FilePath original_file =
588 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
589 FilePath same_file =
590 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
591 FilePath same_length_file =
592 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
593 FilePath different_file =
594 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
595 FilePath different_first_file =
596 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
597 FilePath different_last_file =
598 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
599 FilePath empty1_file =
600 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
601 FilePath empty2_file =
602 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
603 FilePath shortened_file =
604 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
605 FilePath binary_file =
606 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
607 FilePath binary_file_same =
608 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
609 FilePath binary_file_diff =
610 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +0900611
612 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
613 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
614 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
615 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
616 EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname"));
617 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
618 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
619 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
620 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
621 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
622 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
623 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
624}
625
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900626// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +0900627#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900628TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900629 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900630 CreateTextFile(target_file, L"This is the target.");
631
evanm@google.com874d1672008-10-31 08:54:04 +0900632 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900633
634 HRESULT result;
635 IShellLink *shell = NULL;
636 IPersistFile *persist = NULL;
637
638 CoInitialize(NULL);
639 // Temporarily create a shortcut for test
640 result = CoCreateInstance(CLSID_ShellLink, NULL,
641 CLSCTX_INPROC_SERVER, IID_IShellLink,
642 reinterpret_cast<LPVOID*>(&shell));
643 EXPECT_TRUE(SUCCEEDED(result));
644 result = shell->QueryInterface(IID_IPersistFile,
645 reinterpret_cast<LPVOID*>(&persist));
646 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900647 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900648 EXPECT_TRUE(SUCCEEDED(result));
649 result = shell->SetDescription(L"ResolveShortcutTest");
650 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900651 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +0900652 EXPECT_TRUE(SUCCEEDED(result));
653 if (persist)
654 persist->Release();
655 if (shell)
656 shell->Release();
657
658 bool is_solved;
evanm@google.com874d1672008-10-31 08:54:04 +0900659 std::wstring link_file_str = link_file.value();
660 is_solved = file_util::ResolveShortcut(&link_file_str);
initial.commit3f4a7322008-07-27 06:49:38 +0900661 EXPECT_TRUE(is_solved);
662 std::wstring contents;
evanm@google.com874d1672008-10-31 08:54:04 +0900663 contents = ReadTextFile(FilePath(link_file_str));
initial.commit3f4a7322008-07-27 06:49:38 +0900664 EXPECT_EQ(L"This is the target.", contents);
665
ericroman@google.comdbff4f52008-08-19 01:00:38 +0900666 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +0900667 DeleteFile(target_file.value().c_str());
668 DeleteFile(link_file_str.c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900669 CoUninitialize();
670}
671
672TEST_F(FileUtilTest, CreateShortcutTest) {
673 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +0900674 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900675 CreateTextFile(target_file, file_contents);
676
evanm@google.com874d1672008-10-31 08:54:04 +0900677 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900678
679 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +0900680 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
681 link_file.value().c_str(),
initial.commit3f4a7322008-07-27 06:49:38 +0900682 NULL, NULL, NULL, NULL, 0));
evanm@google.com874d1672008-10-31 08:54:04 +0900683 std::wstring resolved_name = link_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900684 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evanm@google.com874d1672008-10-31 08:54:04 +0900685 std::wstring read_contents = ReadTextFile(FilePath(resolved_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900686 EXPECT_EQ(file_contents, read_contents);
687
evanm@google.com874d1672008-10-31 08:54:04 +0900688 DeleteFile(target_file.value().c_str());
689 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900690 CoUninitialize();
691}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +0900692
693TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
694 // Create a directory
695 FilePath dir_name_from =
696 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
697 file_util::CreateDirectory(dir_name_from);
698 ASSERT_TRUE(file_util::PathExists(dir_name_from));
699
700 // Create a file under the directory
701 FilePath file_name_from =
702 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
703 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
704 ASSERT_TRUE(file_util::PathExists(file_name_from));
705
706 // Move the directory by using CopyAndDeleteDirectory
707 FilePath dir_name_to = test_dir_.Append(
708 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
709 FilePath file_name_to =
710 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
711
712 ASSERT_FALSE(file_util::PathExists(dir_name_to));
713
714 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
715
716 // Check everything has been moved.
717 EXPECT_FALSE(file_util::PathExists(dir_name_from));
718 EXPECT_FALSE(file_util::PathExists(file_name_from));
719 EXPECT_TRUE(file_util::PathExists(dir_name_to));
720 EXPECT_TRUE(file_util::PathExists(file_name_to));
721}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900722#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900723
724TEST_F(FileUtilTest, CreateTemporaryFileNameTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900725 std::wstring temp_files[3];
726 for (int i = 0; i < 3; i++) {
727 ASSERT_TRUE(file_util::CreateTemporaryFileName(&(temp_files[i])));
728 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
729 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
730 }
731 for (int i = 0; i < 3; i++)
732 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
733 for (int i = 0; i < 3; i++)
734 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
735}
736
737TEST_F(FileUtilTest, CreateAndOpenTemporaryFileNameTest) {
738 FilePath names[3];
739 FILE *fps[3];
740 int i;
741
742 // Create; make sure they are open and exist.
743 for (i = 0; i < 3; ++i) {
744 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
745 ASSERT_TRUE(fps[i]);
746 EXPECT_TRUE(file_util::PathExists(names[i]));
747 }
748
749 // Make sure all names are unique.
750 for (i = 0; i < 3; ++i) {
751 EXPECT_FALSE(names[i] == names[(i+1)%3]);
752 }
753
754 // Close and delete.
755 for (i = 0; i < 3; ++i) {
756 EXPECT_TRUE(file_util::CloseFile(fps[i]));
757 EXPECT_TRUE(file_util::Delete(names[i], false));
758 }
initial.commit3f4a7322008-07-27 06:49:38 +0900759}
760
761TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
762 std::wstring temp_dir;
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900763 ASSERT_TRUE(file_util::CreateNewTempDirectory(std::wstring(), &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900764 EXPECT_TRUE(file_util::PathExists(temp_dir));
765 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +0900766}
767
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900768TEST_F(FileUtilTest, GetShmemTempDirTest) {
769 FilePath dir;
770 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
771 EXPECT_TRUE(file_util::DirectoryExists(dir));
772}
773
initial.commit3f4a7322008-07-27 06:49:38 +0900774TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900775 FilePath test_root =
776 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +0900777#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +0900778 FilePath test_path =
779 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900780#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +0900781 FilePath test_path =
782 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900783#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +0900784
785 EXPECT_FALSE(file_util::PathExists(test_path));
786 EXPECT_TRUE(file_util::CreateDirectory(test_path));
787 EXPECT_TRUE(file_util::PathExists(test_path));
788 // CreateDirectory returns true if the DirectoryExists returns true.
789 EXPECT_TRUE(file_util::CreateDirectory(test_path));
790
791 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +0900792 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900793 EXPECT_FALSE(file_util::PathExists(test_path));
794 CreateTextFile(test_path, L"test file");
795 EXPECT_TRUE(file_util::PathExists(test_path));
796 EXPECT_FALSE(file_util::CreateDirectory(test_path));
797
798 EXPECT_TRUE(file_util::Delete(test_root, true));
799 EXPECT_FALSE(file_util::PathExists(test_root));
800 EXPECT_FALSE(file_util::PathExists(test_path));
801}
802
803TEST_F(FileUtilTest, DetectDirectoryTest) {
804 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900805 FilePath test_root =
806 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900807 EXPECT_FALSE(file_util::PathExists(test_root));
808 EXPECT_TRUE(file_util::CreateDirectory(test_root));
809 EXPECT_TRUE(file_util::PathExists(test_root));
810 EXPECT_TRUE(file_util::DirectoryExists(test_root));
811
812 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +0900813 FilePath test_path =
814 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900815 EXPECT_FALSE(file_util::PathExists(test_path));
816 CreateTextFile(test_path, L"test file");
817 EXPECT_TRUE(file_util::PathExists(test_path));
818 EXPECT_FALSE(file_util::DirectoryExists(test_path));
819 EXPECT_TRUE(file_util::Delete(test_path, false));
820
821 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +0900822}
823
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900824static const struct goodbad_pair {
initial.commit3f4a7322008-07-27 06:49:38 +0900825 std::wstring bad_name;
826 std::wstring good_name;
827} kIllegalCharacterCases[] = {
828 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
829 {L"**********::::.txt", L"--------------.txt"},
initial.commit3f4a7322008-07-27 06:49:38 +0900830 // We can't use UCNs (universal character names) for C0/C1 characters and
831 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
832 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
erikkay@google.com014161d2008-08-16 02:45:13 +0900833#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900834 {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
initial.commit3f4a7322008-07-27 06:49:38 +0900835 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
836 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900837#elif defined(OS_POSIX)
838 {L"bad*file?name.jpg", L"bad-file-name.jpg"},
839 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"},
840 {L"bad\uFFFFfile-name.jpg ", L"bad-file-name.jpg"},
841#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900842 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
843 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
844 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
845 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
846 // Unassigned codepoints are ok.
847 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
848};
849
850TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900851 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900852 std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
853 file_util::ReplaceIllegalCharacters(&bad_name, L'-');
854 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
855 }
856}
857
858static const struct ReplaceExtensionCase {
859 std::wstring file_name;
estade@chromium.org63343202008-12-05 05:46:06 +0900860 FilePath::StringType extension;
initial.commit3f4a7322008-07-27 06:49:38 +0900861 std::wstring result;
862} kReplaceExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +0900863 {L"", FILE_PATH_LITERAL(""), L""},
864 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
865 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
866 {L".", FILE_PATH_LITERAL(""), L""},
867 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
868 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
869 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
870 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
871 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
872 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
873 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
874 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
875 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
876 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
877 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
878 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commit3f4a7322008-07-27 06:49:38 +0900879};
880
881TEST_F(FileUtilTest, ReplaceExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900882 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +0900883 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
884 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
885 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +0900886 }
887}
888
sky@google.com71e7c6f2008-09-20 02:32:18 +0900889// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
890// the directory names of the path.
891TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
estade@chromium.org63343202008-12-05 05:46:06 +0900892 FilePath path;
893 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
894 path = path.Append(FILE_PATH_LITERAL("foo"));
sky@google.com71e7c6f2008-09-20 02:32:18 +0900895 // '/foo.bar/foo' with extension '.baz'
estade@chromium.org63343202008-12-05 05:46:06 +0900896 FilePath result_path = path;
897 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900898 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
899 result_path.value());
sky@google.com71e7c6f2008-09-20 02:32:18 +0900900}
901
initial.commit3f4a7322008-07-27 06:49:38 +0900902TEST_F(FileUtilTest, FileEnumeratorTest) {
903 // Test an empty directory.
avi@google.com5cb79352008-12-11 23:55:12 +0900904 file_util::FileEnumerator f0(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900905 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +0900906 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
907 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +0900908
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900909 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +0900910 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900911 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +0900912 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900913 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +0900914 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900915 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +0900916
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900917 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +0900918 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900919 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900920 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900921 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900922 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900923 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900924 FilePath file2_rel =
925 dir2.Append(FilePath::kParentDirectory)
926 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900927 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900928 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900929
930 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +0900931 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900932 file_util::FileEnumerator::FILES);
933 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900934 EXPECT_TRUE(c1.HasFile(file1));
935 EXPECT_TRUE(c1.HasFile(file2_abs));
936 EXPECT_TRUE(c1.HasFile(dir2file));
937 EXPECT_TRUE(c1.HasFile(dir2innerfile));
938 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +0900939
940 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +0900941 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900942 file_util::FileEnumerator::DIRECTORIES);
943 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900944 EXPECT_TRUE(c2.HasFile(dir1));
945 EXPECT_TRUE(c2.HasFile(dir2));
946 EXPECT_TRUE(c2.HasFile(dir2inner));
947 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +0900948
tim@chromium.org989d0972008-10-16 11:42:45 +0900949 // Only enumerate directories non-recursively.
950 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +0900951 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +0900952 FindResultCollector c2_non_recursive(f2_non_recursive);
953 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
954 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
955 EXPECT_EQ(c2_non_recursive.size(), 2);
956
initial.commit3f4a7322008-07-27 06:49:38 +0900957 // Enumerate files and directories.
avi@google.com5cb79352008-12-11 23:55:12 +0900958 file_util::FileEnumerator f3(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900959 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
960 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900961 EXPECT_TRUE(c3.HasFile(dir1));
962 EXPECT_TRUE(c3.HasFile(dir2));
963 EXPECT_TRUE(c3.HasFile(file1));
964 EXPECT_TRUE(c3.HasFile(file2_abs));
965 EXPECT_TRUE(c3.HasFile(dir2file));
966 EXPECT_TRUE(c3.HasFile(dir2inner));
967 EXPECT_TRUE(c3.HasFile(dir2innerfile));
968 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +0900969
970 // Non-recursive operation.
avi@google.com5cb79352008-12-11 23:55:12 +0900971 file_util::FileEnumerator f4(test_dir_, false,
initial.commit3f4a7322008-07-27 06:49:38 +0900972 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
973 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900974 EXPECT_TRUE(c4.HasFile(dir2));
975 EXPECT_TRUE(c4.HasFile(dir2));
976 EXPECT_TRUE(c4.HasFile(file1));
977 EXPECT_TRUE(c4.HasFile(file2_abs));
978 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +0900979
980 // Enumerate with a pattern.
avi@google.com5cb79352008-12-11 23:55:12 +0900981 file_util::FileEnumerator f5(test_dir_, true,
982 file_util::FileEnumerator::FILES_AND_DIRECTORIES,
983 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900984 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900985 EXPECT_TRUE(c5.HasFile(dir1));
986 EXPECT_TRUE(c5.HasFile(dir2));
987 EXPECT_TRUE(c5.HasFile(dir2file));
988 EXPECT_TRUE(c5.HasFile(dir2inner));
989 EXPECT_TRUE(c5.HasFile(dir2innerfile));
990 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +0900991
992 // Make sure the destructor closes the find handle while in the middle of a
993 // query to allow TearDown to delete the directory.
avi@google.com5cb79352008-12-11 23:55:12 +0900994 file_util::FileEnumerator f6(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900995 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +0900996 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
997 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +0900998}
license.botf003cfe2008-08-24 09:55:55 +0900999
estade@chromium.org97e37822008-11-27 13:03:57 +09001000
1001void PathComponents(const std::wstring& path,
1002 std::vector<std::wstring>* components) {
1003 DCHECK(components != NULL);
1004 if (components == NULL)
1005 return;
1006 std::wstring::size_type start = 0;
1007 std::wstring::size_type end = path.find('/', start);
1008
1009 // Special case the "/" or "\" directory. On Windows with a drive letter,
1010 // this code path won't hit, but the right thing should still happen.
1011 // "E:\foo" will turn into "E:","foo".
1012 if (end == start) {
1013 components->push_back(std::wstring(path, 0, 1));
1014 start = end + 1;
1015 end = path.find('/', start);
1016 }
1017 while (end != std::wstring::npos) {
1018 std::wstring component = std::wstring(path, start, end - start);
1019 components->push_back(component);
1020 start = end + 1;
1021 end = path.find('/', start);
1022 }
1023 std::wstring component = std::wstring(path, start);
1024 components->push_back(component);
1025}
1026
1027static const struct PathComponentsCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001028 const FilePath::CharType* path;
1029 const FilePath::CharType* result;
estade@chromium.org97e37822008-11-27 13:03:57 +09001030} kPathComponents[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001031 {FILE_PATH_LITERAL("/foo/bar/baz/"), FILE_PATH_LITERAL("/|foo|bar|baz|")},
1032 {FILE_PATH_LITERAL("/foo/bar/baz"), FILE_PATH_LITERAL("/|foo|bar|baz")},
1033 {FILE_PATH_LITERAL("e:/foo"), FILE_PATH_LITERAL("e:|foo")},
estade@chromium.org97e37822008-11-27 13:03:57 +09001034};
1035
1036TEST_F(FileUtilTest, PathComponentsTest) {
1037 for (size_t i = 0; i < arraysize(kPathComponents); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001038 FilePath path(kPathComponents[i].path);
estade@chromium.org97e37822008-11-27 13:03:57 +09001039 std::vector<FilePath::StringType> comps;
1040 file_util::PathComponents(path, &comps);
1041
1042 FilePath::StringType result;
1043 for (size_t j = 0; j < comps.size(); ++j) {
1044 result.append(comps[j]);
1045 if (j < comps.size() - 1)
1046 result.append(FILE_PATH_LITERAL("|"), 1);
1047 }
1048 EXPECT_EQ(kPathComponents[i].result, result);
1049 }
1050}
1051
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001052TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001053 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001054
1055 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001056 if (file_util::PathExists(data_dir)) {
1057 ASSERT_TRUE(file_util::Delete(data_dir, true));
1058 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001059 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1060
1061 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1062 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1063 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1064 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1065
1066 // Annoyingly, the directories must actually exist in order for realpath(),
1067 // which Contains() relies on in posix, to work.
1068 ASSERT_TRUE(file_util::CreateDirectory(foo));
1069 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001070 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1071 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1072 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001073
1074 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1075 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1076 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1077 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1078
1079// Platform-specific concerns
1080 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1081#if defined(OS_WIN)
1082 EXPECT_TRUE(file_util::ContainsPath(foo,
1083 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001084 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001085 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
1086#elif defined(OS_LINUX)
1087 EXPECT_FALSE(file_util::ContainsPath(foo,
1088 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
1089#else
1090 // We can't really do this test on osx since the case-sensitivity of the
1091 // filesystem is configurable.
1092#endif
1093}
1094
mark@chromium.org17684802008-09-10 09:16:28 +09001095} // namespace