blob: 7dff8689a8633ff26799026dccdbb9aaa441a49c [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commitd7cae122008-07-26 21:49:384
[email protected]f35d39b2008-08-15 17:50:105#include "build/build_config.h"
6
[email protected]8541bb82008-08-15 17:45:137#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:388#include <windows.h>
initial.commitd7cae122008-07-26 21:49:389#include <shellapi.h>
10#include <shlobj.h>
[email protected]37088fef2008-08-15 17:32:1011#endif
initial.commitd7cae122008-07-26 21:49:3812
13#include <fstream>
14#include <iostream>
[email protected]37088fef2008-08-15 17:32:1015#include <set>
initial.commitd7cae122008-07-26 21:49:3816
17#include "base/base_paths.h"
[email protected]640517f2008-10-30 23:54:0418#include "base/file_path.h"
initial.commitd7cae122008-07-26 21:49:3819#include "base/file_util.h"
20#include "base/logging.h"
21#include "base/path_service.h"
[email protected]2126577b2009-04-23 15:05:1922#include "base/platform_thread.h"
initial.commitd7cae122008-07-26 21:49:3823#include "base/string_util.h"
[email protected]85786f92009-04-18 00:42:4824#include "base/time.h"
initial.commitd7cae122008-07-26 21:49:3825#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1526#include "testing/platform_test.h"
initial.commitd7cae122008-07-26 21:49:3827
[email protected]4e9f6be2009-04-03 17:17:5828// This macro helps avoid wrapped lines in the test structs.
29#define FPL(x) FILE_PATH_LITERAL(x)
30
initial.commitd7cae122008-07-26 21:49:3831namespace {
32
[email protected]8199b3a2009-06-09 05:57:3833const 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
[email protected]ed2f2332008-08-20 15:59:4938// file_util winds up using autoreleased objects on the Mac, so this needs
39// to be a PlatformTest
40class FileUtilTest : public PlatformTest {
initial.commitd7cae122008-07-26 21:49:3841 protected:
42 virtual void SetUp() {
[email protected]ed2f2332008-08-20 15:59:4943 PlatformTest::SetUp();
initial.commitd7cae122008-07-26 21:49:3844 // Name a subdirectory of the temp directory.
45 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
[email protected]640517f2008-10-30 23:54:0446 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commitd7cae122008-07-26 21:49:3847
48 // Create a fresh, empty copy of this directory.
49 file_util::Delete(test_dir_, true);
[email protected]640517f2008-10-30 23:54:0450 file_util::CreateDirectory(test_dir_);
initial.commitd7cae122008-07-26 21:49:3851 }
52 virtual void TearDown() {
[email protected]ed2f2332008-08-20 15:59:4953 PlatformTest::TearDown();
initial.commitd7cae122008-07-26 21:49:3854 // Clean up test directory
[email protected]37088fef2008-08-15 17:32:1055 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commitd7cae122008-07-26 21:49:3856 ASSERT_FALSE(file_util::PathExists(test_dir_));
57 }
58
59 // the path to temporary directory used to contain the test operations
[email protected]640517f2008-10-30 23:54:0460 FilePath test_dir_;
initial.commitd7cae122008-07-26 21:49:3861};
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) {
[email protected]0b733222008-12-11 14:55:1268 FilePath cur_file;
69 while (!(cur_file = enumerator.Next()).value().empty()) {
70 FilePath::StringType path = cur_file.value();
initial.commitd7cae122008-07-26 21:49:3871 // The file should not be returned twice.
[email protected]640517f2008-10-30 23:54:0472 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commitd7cae122008-07-26 21:49:3873 << "Same file returned twice";
74
75 // Save for later.
[email protected]640517f2008-10-30 23:54:0476 files_.insert(path);
initial.commitd7cae122008-07-26 21:49:3877 }
78 }
79
80 // Returns true if the enumerator found the file.
[email protected]640517f2008-10-30 23:54:0481 bool HasFile(const FilePath& file) const {
82 return files_.find(file.value()) != files_.end();
initial.commitd7cae122008-07-26 21:49:3883 }
[email protected]640517f2008-10-30 23:54:0484
[email protected]37088fef2008-08-15 17:32:1085 int size() {
[email protected]f35d39b2008-08-15 17:50:1086 return static_cast<int>(files_.size());
[email protected]37088fef2008-08-15 17:32:1087 }
initial.commitd7cae122008-07-26 21:49:3888
89 private:
[email protected]640517f2008-10-30 23:54:0490 std::set<FilePath::StringType> files_;
initial.commitd7cae122008-07-26 21:49:3891};
92
93// Simple function to dump some text into a new file.
[email protected]640517f2008-10-30 23:54:0494void CreateTextFile(const FilePath& filename,
initial.commitd7cae122008-07-26 21:49:3895 const std::wstring& contents) {
96 std::ofstream file;
[email protected]640517f2008-10-30 23:54:0497 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commitd7cae122008-07-26 21:49:3898 ASSERT_TRUE(file.is_open());
99 file << contents;
100 file.close();
101}
102
103// Simple function to take out some text from a file.
[email protected]640517f2008-10-30 23:54:04104std::wstring ReadTextFile(const FilePath& filename) {
[email protected]37088fef2008-08-15 17:32:10105 wchar_t contents[64];
initial.commitd7cae122008-07-26 21:49:38106 std::wifstream file;
[email protected]640517f2008-10-30 23:54:04107 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commitd7cae122008-07-26 21:49:38108 EXPECT_TRUE(file.is_open());
109 file.getline(contents, 64);
110 file.close();
111 return std::wstring(contents);
112}
113
[email protected]8541bb82008-08-15 17:45:13114#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38115uint64 FileTimeAsUint64(const FILETIME& ft) {
116 ULARGE_INTEGER u;
117 u.LowPart = ft.dwLowDateTime;
118 u.HighPart = ft.dwHighDateTime;
119 return u.QuadPart;
120}
[email protected]37088fef2008-08-15 17:32:10121#endif
initial.commitd7cae122008-07-26 21:49:38122
123const struct append_case {
124 const wchar_t* path;
125 const wchar_t* ending;
126 const wchar_t* result;
127} append_cases[] = {
[email protected]8541bb82008-08-15 17:45:13128#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38129 {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"\\"},
[email protected]37088fef2008-08-15 17:32:10136#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.commitd7cae122008-07-26 21:49:38145};
146
initial.commitd7cae122008-07-26 21:49:38147TEST_F(FileUtilTest, AppendToPath) {
[email protected]37088fef2008-08-15 17:32:10148 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38149 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 {
[email protected]4e9f6be2009-04-03 17:17:58161 const FilePath::CharType* path;
162 const FilePath::CharType* suffix;
163 const FilePath::CharType* result;
initial.commitd7cae122008-07-26 21:49:38164} kInsertBeforeExtension[] = {
[email protected]4e9f6be2009-04-03 17:17:58165 {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")},
[email protected]8541bb82008-08-15 17:45:13181#if defined(OS_WIN)
[email protected]4e9f6be2009-04-03 17:17:58182 {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")},
[email protected]37088fef2008-08-15 17:32:10195#elif defined(OS_POSIX)
[email protected]4e9f6be2009-04-03 17:17:58196 {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")},
[email protected]37088fef2008-08-15 17:32:10208#endif
initial.commitd7cae122008-07-26 21:49:38209};
210
211TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
[email protected]37088fef2008-08-15 17:32:10212 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
[email protected]4e9f6be2009-04-03 17:17:58213 FilePath path(kInsertBeforeExtension[i].path);
initial.commitd7cae122008-07-26 21:49:38214 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
[email protected]4e9f6be2009-04-03 17:17:58215 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commitd7cae122008-07-26 21:49:38216 }
217}
218
219static const struct filename_case {
220 const wchar_t* path;
221 const wchar_t* filename;
222} filename_cases[] = {
[email protected]8541bb82008-08-15 17:45:13223#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38224 {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"},
[email protected]37088fef2008-08-15 17:32:10234#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.commitd7cae122008-07-26 21:49:38242};
243
244TEST_F(FileUtilTest, GetFilenameFromPath) {
[email protected]37088fef2008-08-15 17:32:10245 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38246 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[] = {
[email protected]8541bb82008-08-15 17:45:13257#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38258 {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"},
[email protected]37088fef2008-08-15 17:32:10264#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.commitd7cae122008-07-26 21:49:38277};
278
279TEST_F(FileUtilTest, GetFileExtensionFromPath) {
[email protected]37088fef2008-08-15 17:32:10280 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38281 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[] = {
[email protected]8541bb82008-08-15 17:45:13292#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38293 {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:"},
[email protected]37088fef2008-08-15 17:32:10300#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.commitd7cae122008-07-26 21:49:38311};
312
313TEST_F(FileUtilTest, GetDirectoryFromPath) {
[email protected]37088fef2008-08-15 17:32:10314 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38315 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)
[email protected]85786f92009-04-18 00:42:48324 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commitd7cae122008-07-26 21:49:38325 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
326
327 // Age to perfection
[email protected]4b7743de2009-04-21 01:50:39328#if defined(OS_WIN)
[email protected]2126577b2009-04-23 15:05:19329 PlatformThread::Sleep(100);
[email protected]4b7743de2009-04-21 01:50:39330#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.
[email protected]2126577b2009-04-23 15:05:19333 PlatformThread::Sleep(1500);
[email protected]4b7743de2009-04-21 01:50:39334#endif
initial.commitd7cae122008-07-26 21:49:38335
336 // Establish our cutoff time
[email protected]85786f92009-04-18 00:42:48337 base::Time now(base::Time::NowFromSystemTime());
338 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38339
340 // Create a new file (that we do want to count)
[email protected]85786f92009-04-18 00:42:48341 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commitd7cae122008-07-26 21:49:38342 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
343
344 // We should see only the new file.
[email protected]85786f92009-04-18 00:42:48345 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38346
347 // Delete new file, we should see no files after cutoff now
348 EXPECT_TRUE(file_util::Delete(new_file_name, false));
[email protected]85786f92009-04-18 00:42:48349 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38350}
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
[email protected]640517f2008-10-30 23:54:04356 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commitd7cae122008-07-26 21:49:38357 CreateTextFile(file_name, L"I'm cannon fodder.");
358
359 ASSERT_TRUE(file_util::PathExists(file_name));
360
[email protected]640517f2008-10-30 23:54:04361 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
362 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38363
364 ASSERT_TRUE(file_util::PathExists(subdir_path));
365
[email protected]640517f2008-10-30 23:54:04366 FilePath directory_contents = test_dir_;
[email protected]8541bb82008-08-15 17:45:13367#if defined(OS_WIN)
[email protected]37088fef2008-08-15 17:32:10368 // TODO(erikkay): see if anyone's actually using this feature of the API
[email protected]640517f2008-10-30 23:54:04369 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commitd7cae122008-07-26 21:49:38370 // Delete non-recursively and check that only the file is deleted
371 ASSERT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10372 EXPECT_FALSE(file_util::PathExists(file_name));
373 EXPECT_TRUE(file_util::PathExists(subdir_path));
374#endif
initial.commitd7cae122008-07-26 21:49:38375
376 // Delete recursively and make sure all contents are deleted
377 ASSERT_TRUE(file_util::Delete(directory_contents, true));
[email protected]37088fef2008-08-15 17:32:10378 EXPECT_FALSE(file_util::PathExists(file_name));
379 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commitd7cae122008-07-26 21:49:38380}
381
382TEST_F(FileUtilTest, Move) {
383 // Create a directory
[email protected]640517f2008-10-30 23:54:04384 FilePath dir_name_from =
385 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
386 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38387 ASSERT_TRUE(file_util::PathExists(dir_name_from));
388
389 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04390 FilePath file_name_from =
391 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38392 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
393 ASSERT_TRUE(file_util::PathExists(file_name_from));
394
395 // Move the directory
[email protected]640517f2008-10-30 23:54:04396 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.commitd7cae122008-07-26 21:49:38399
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.
[email protected]640517f2008-10-30 23:54:04413 FilePath dir_name_from =
414 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
415 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38416 ASSERT_TRUE(file_util::PathExists(dir_name_from));
417
418 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04419 FilePath file_name_from =
420 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38421 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
422 ASSERT_TRUE(file_util::PathExists(file_name_from));
423
424 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04425 FilePath subdir_name_from =
426 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
427 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38428 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
429
430 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04431 FilePath file_name2_from =
432 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38433 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
434 ASSERT_TRUE(file_util::PathExists(file_name2_from));
435
436 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:04437 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.commitd7cae122008-07-26 21:49:38445
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.
[email protected]640517f2008-10-30 23:54:04463 FilePath dir_name_from =
464 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
465 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38466 ASSERT_TRUE(file_util::PathExists(dir_name_from));
467
468 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04469 FilePath file_name_from =
470 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38471 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
472 ASSERT_TRUE(file_util::PathExists(file_name_from));
473
474 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04475 FilePath subdir_name_from =
476 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
477 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38478 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
479
480 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04481 FilePath file_name2_from =
482 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38483 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
484 ASSERT_TRUE(file_util::PathExists(file_name2_from));
485
486 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:04487 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.commitd7cae122008-07-26 21:49:38493
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
[email protected]640517f2008-10-30 23:54:04510 FilePath dir_name_from =
511 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
512 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38513 ASSERT_TRUE(file_util::PathExists(dir_name_from));
514
515 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04516 FilePath file_name_from =
517 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38518 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.
[email protected]640517f2008-10-30 23:54:04523 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:38524 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:11525
[email protected]37088fef2008-08-15 17:32:10526 // Copy the file to another location using '..' in the path.
[email protected]640517f2008-10-30 23:54:04527 std::wstring dest_file2(dir_name_from.ToWStringHack());
[email protected]37088fef2008-08-15 17:32:10528 file_util::AppendToPath(&dest_file2, L"..");
529 file_util::AppendToPath(&dest_file2, L"DestFile.txt");
[email protected]640517f2008-10-30 23:54:04530 ASSERT_TRUE(file_util::CopyFile(file_name_from,
531 FilePath::FromWStringHack(dest_file2)));
532 std::wstring dest_file2_test(dir_name_from.ToWStringHack());
[email protected]37088fef2008-08-15 17:32:10533 file_util::UpOneDirectory(&dest_file2_test);
534 file_util::AppendToPath(&dest_file2_test, L"DestFile.txt");
initial.commitd7cae122008-07-26 21:49:38535
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);
[email protected]640517f2008-10-30 23:54:04541 EXPECT_TRUE(file_util::PathExists(
542 FilePath::FromWStringHack(dest_file2_test)));
543 EXPECT_TRUE(file_util::PathExists(FilePath::FromWStringHack(dest_file2)));
initial.commitd7cae122008-07-26 21:49:38544}
545
[email protected]37088fef2008-08-15 17:32:10546// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:13547#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38548TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]640517f2008-10-30 23:54:04549 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:38550
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;
[email protected]640517f2008-10-30 23:54:04560 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:38561
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
[email protected]640517f2008-10-30 23:54:04577 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:38578}
[email protected]37088fef2008-08-15 17:32:10579#endif
initial.commitd7cae122008-07-26 21:49:38580
[email protected]ed2f2332008-08-20 15:59:49581// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:04582// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:49583typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:38584
[email protected]ed2f2332008-08-20 15:59:49585TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:04586 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:38587 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:04588 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
589 .Append(FILE_PATH_LITERAL("data"))
590 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:38591 ASSERT_TRUE(file_util::PathExists(data_dir));
592
[email protected]640517f2008-10-30 23:54:04593 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.commitd7cae122008-07-26 21:49:38617
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
[email protected]b81637c32009-06-26 21:17:24632TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
633 FilePath data_dir;
634 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
635 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
636 .Append(FILE_PATH_LITERAL("data"))
637 .Append(FILE_PATH_LITERAL("file_util_unittest"));
638 ASSERT_TRUE(file_util::PathExists(data_dir));
639
640 FilePath original_file =
641 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
642 FilePath same_file =
643 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
644 FilePath crlf_file =
645 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
646 FilePath shortened_file =
647 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
648 FilePath different_file =
649 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
650 FilePath different_first_file =
651 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
652 FilePath different_last_file =
653 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
654 FilePath first1_file =
655 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
656 FilePath first2_file =
657 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
658 FilePath empty1_file =
659 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
660 FilePath empty2_file =
661 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
662 FilePath blank_line_file =
663 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
664 FilePath blank_line_crlf_file =
665 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
666
667 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
668 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
669 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
670 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
671 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
672 different_first_file));
673 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
674 different_last_file));
675 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
676 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
677 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
678 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
679 blank_line_crlf_file));
680}
681
[email protected]37088fef2008-08-15 17:32:10682// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:13683#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38684TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]640517f2008-10-30 23:54:04685 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:38686 CreateTextFile(target_file, L"This is the target.");
687
[email protected]640517f2008-10-30 23:54:04688 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:38689
690 HRESULT result;
691 IShellLink *shell = NULL;
692 IPersistFile *persist = NULL;
693
694 CoInitialize(NULL);
695 // Temporarily create a shortcut for test
696 result = CoCreateInstance(CLSID_ShellLink, NULL,
697 CLSCTX_INPROC_SERVER, IID_IShellLink,
698 reinterpret_cast<LPVOID*>(&shell));
699 EXPECT_TRUE(SUCCEEDED(result));
700 result = shell->QueryInterface(IID_IPersistFile,
701 reinterpret_cast<LPVOID*>(&persist));
702 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:04703 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:38704 EXPECT_TRUE(SUCCEEDED(result));
705 result = shell->SetDescription(L"ResolveShortcutTest");
706 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:04707 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:38708 EXPECT_TRUE(SUCCEEDED(result));
709 if (persist)
710 persist->Release();
711 if (shell)
712 shell->Release();
713
714 bool is_solved;
[email protected]640517f2008-10-30 23:54:04715 std::wstring link_file_str = link_file.value();
716 is_solved = file_util::ResolveShortcut(&link_file_str);
initial.commitd7cae122008-07-26 21:49:38717 EXPECT_TRUE(is_solved);
718 std::wstring contents;
[email protected]640517f2008-10-30 23:54:04719 contents = ReadTextFile(FilePath(link_file_str));
initial.commitd7cae122008-07-26 21:49:38720 EXPECT_EQ(L"This is the target.", contents);
721
[email protected]d324ab332008-08-18 16:00:38722 // Cleaning
[email protected]640517f2008-10-30 23:54:04723 DeleteFile(target_file.value().c_str());
724 DeleteFile(link_file_str.c_str());
initial.commitd7cae122008-07-26 21:49:38725 CoUninitialize();
726}
727
728TEST_F(FileUtilTest, CreateShortcutTest) {
729 const wchar_t file_contents[] = L"This is another target.";
[email protected]640517f2008-10-30 23:54:04730 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:38731 CreateTextFile(target_file, file_contents);
732
[email protected]640517f2008-10-30 23:54:04733 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:38734
735 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:04736 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
737 link_file.value().c_str(),
initial.commitd7cae122008-07-26 21:49:38738 NULL, NULL, NULL, NULL, 0));
[email protected]640517f2008-10-30 23:54:04739 std::wstring resolved_name = link_file.value();
initial.commitd7cae122008-07-26 21:49:38740 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]640517f2008-10-30 23:54:04741 std::wstring read_contents = ReadTextFile(FilePath(resolved_name));
initial.commitd7cae122008-07-26 21:49:38742 EXPECT_EQ(file_contents, read_contents);
743
[email protected]640517f2008-10-30 23:54:04744 DeleteFile(target_file.value().c_str());
745 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:38746 CoUninitialize();
747}
[email protected]2c59af7dca2009-03-11 18:37:48748
749TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
750 // Create a directory
751 FilePath dir_name_from =
752 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
753 file_util::CreateDirectory(dir_name_from);
754 ASSERT_TRUE(file_util::PathExists(dir_name_from));
755
756 // Create a file under the directory
757 FilePath file_name_from =
758 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
759 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
760 ASSERT_TRUE(file_util::PathExists(file_name_from));
761
762 // Move the directory by using CopyAndDeleteDirectory
763 FilePath dir_name_to = test_dir_.Append(
764 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
765 FilePath file_name_to =
766 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
767
768 ASSERT_FALSE(file_util::PathExists(dir_name_to));
769
770 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
771
772 // Check everything has been moved.
773 EXPECT_FALSE(file_util::PathExists(dir_name_from));
774 EXPECT_FALSE(file_util::PathExists(file_name_from));
775 EXPECT_TRUE(file_util::PathExists(dir_name_to));
776 EXPECT_TRUE(file_util::PathExists(file_name_to));
777}
[email protected]37088fef2008-08-15 17:32:10778#endif
initial.commitd7cae122008-07-26 21:49:38779
780TEST_F(FileUtilTest, CreateTemporaryFileNameTest) {
[email protected]9e51af92009-02-04 00:58:39781 std::wstring temp_files[3];
782 for (int i = 0; i < 3; i++) {
783 ASSERT_TRUE(file_util::CreateTemporaryFileName(&(temp_files[i])));
784 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
785 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
786 }
787 for (int i = 0; i < 3; i++)
788 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
789 for (int i = 0; i < 3; i++)
790 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
791}
792
793TEST_F(FileUtilTest, CreateAndOpenTemporaryFileNameTest) {
794 FilePath names[3];
795 FILE *fps[3];
796 int i;
797
798 // Create; make sure they are open and exist.
799 for (i = 0; i < 3; ++i) {
800 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
801 ASSERT_TRUE(fps[i]);
802 EXPECT_TRUE(file_util::PathExists(names[i]));
803 }
804
805 // Make sure all names are unique.
806 for (i = 0; i < 3; ++i) {
807 EXPECT_FALSE(names[i] == names[(i+1)%3]);
808 }
809
810 // Close and delete.
811 for (i = 0; i < 3; ++i) {
812 EXPECT_TRUE(file_util::CloseFile(fps[i]));
813 EXPECT_TRUE(file_util::Delete(names[i], false));
814 }
initial.commitd7cae122008-07-26 21:49:38815}
816
817TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
818 std::wstring temp_dir;
[email protected]392264c2008-11-11 00:01:38819 ASSERT_TRUE(file_util::CreateNewTempDirectory(std::wstring(), &temp_dir));
[email protected]806b9c62008-09-11 16:09:11820 EXPECT_TRUE(file_util::PathExists(temp_dir));
821 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:38822}
823
[email protected]9e51af92009-02-04 00:58:39824TEST_F(FileUtilTest, GetShmemTempDirTest) {
825 FilePath dir;
826 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
827 EXPECT_TRUE(file_util::DirectoryExists(dir));
828}
829
initial.commitd7cae122008-07-26 21:49:38830TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:04831 FilePath test_root =
832 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:13833#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:04834 FilePath test_path =
835 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:10836#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:04837 FilePath test_path =
838 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:10839#endif
[email protected]806b9c62008-09-11 16:09:11840
841 EXPECT_FALSE(file_util::PathExists(test_path));
842 EXPECT_TRUE(file_util::CreateDirectory(test_path));
843 EXPECT_TRUE(file_util::PathExists(test_path));
844 // CreateDirectory returns true if the DirectoryExists returns true.
845 EXPECT_TRUE(file_util::CreateDirectory(test_path));
846
847 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:04848 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:11849 EXPECT_FALSE(file_util::PathExists(test_path));
850 CreateTextFile(test_path, L"test file");
851 EXPECT_TRUE(file_util::PathExists(test_path));
852 EXPECT_FALSE(file_util::CreateDirectory(test_path));
853
854 EXPECT_TRUE(file_util::Delete(test_root, true));
855 EXPECT_FALSE(file_util::PathExists(test_root));
856 EXPECT_FALSE(file_util::PathExists(test_path));
857}
858
859TEST_F(FileUtilTest, DetectDirectoryTest) {
860 // Check a directory
[email protected]640517f2008-10-30 23:54:04861 FilePath test_root =
862 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:11863 EXPECT_FALSE(file_util::PathExists(test_root));
864 EXPECT_TRUE(file_util::CreateDirectory(test_root));
865 EXPECT_TRUE(file_util::PathExists(test_root));
866 EXPECT_TRUE(file_util::DirectoryExists(test_root));
867
868 // Check a file
[email protected]640517f2008-10-30 23:54:04869 FilePath test_path =
870 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:11871 EXPECT_FALSE(file_util::PathExists(test_path));
872 CreateTextFile(test_path, L"test file");
873 EXPECT_TRUE(file_util::PathExists(test_path));
874 EXPECT_FALSE(file_util::DirectoryExists(test_path));
875 EXPECT_TRUE(file_util::Delete(test_path, false));
876
877 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:38878}
879
[email protected]37088fef2008-08-15 17:32:10880static const struct goodbad_pair {
initial.commitd7cae122008-07-26 21:49:38881 std::wstring bad_name;
882 std::wstring good_name;
883} kIllegalCharacterCases[] = {
884 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
885 {L"**********::::.txt", L"--------------.txt"},
initial.commitd7cae122008-07-26 21:49:38886 // We can't use UCNs (universal character names) for C0/C1 characters and
887 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
888 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
[email protected]8541bb82008-08-15 17:45:13889#if defined(OS_WIN)
[email protected]37088fef2008-08-15 17:32:10890 {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
initial.commitd7cae122008-07-26 21:49:38891 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
[email protected]37088fef2008-08-15 17:32:10892#elif defined(OS_POSIX)
893 {L"bad*file?name.jpg", L"bad-file-name.jpg"},
894 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"},
[email protected]37088fef2008-08-15 17:32:10895#endif
initial.commitd7cae122008-07-26 21:49:38896 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
897 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
898 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
899 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
900 // Unassigned codepoints are ok.
901 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
[email protected]8df44a02009-06-24 16:44:49902 // Non-characters are not allowed.
903 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
904 {L"bad\uFDD0file\uFDEFname.jpg ", L"bad-file-name.jpg"},
initial.commitd7cae122008-07-26 21:49:38905};
906
907TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) {
[email protected]37088fef2008-08-15 17:32:10908 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
initial.commitd7cae122008-07-26 21:49:38909 std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
910 file_util::ReplaceIllegalCharacters(&bad_name, L'-');
911 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
912 }
913}
914
915static const struct ReplaceExtensionCase {
916 std::wstring file_name;
[email protected]ceeb87e2008-12-04 20:46:06917 FilePath::StringType extension;
initial.commitd7cae122008-07-26 21:49:38918 std::wstring result;
919} kReplaceExtension[] = {
[email protected]ceeb87e2008-12-04 20:46:06920 {L"", FILE_PATH_LITERAL(""), L""},
921 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
922 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
923 {L".", FILE_PATH_LITERAL(""), L""},
924 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
925 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
926 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
927 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
928 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
929 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
930 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
931 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
932 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
933 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
934 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
935 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commitd7cae122008-07-26 21:49:38936};
937
938TEST_F(FileUtilTest, ReplaceExtensionTest) {
[email protected]37088fef2008-08-15 17:32:10939 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
[email protected]ceeb87e2008-12-04 20:46:06940 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
941 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
942 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commitd7cae122008-07-26 21:49:38943 }
944}
945
[email protected]b988fe4d2008-09-19 17:32:18946// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
947// the directory names of the path.
948TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
[email protected]ceeb87e2008-12-04 20:46:06949 FilePath path;
950 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
951 path = path.Append(FILE_PATH_LITERAL("foo"));
[email protected]b988fe4d2008-09-19 17:32:18952 // '/foo.bar/foo' with extension '.baz'
[email protected]ceeb87e2008-12-04 20:46:06953 FilePath result_path = path;
954 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
[email protected]4e9f6be2009-04-03 17:17:58955 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
956 result_path.value());
[email protected]b988fe4d2008-09-19 17:32:18957}
958
initial.commitd7cae122008-07-26 21:49:38959TEST_F(FileUtilTest, FileEnumeratorTest) {
960 // Test an empty directory.
[email protected]8199b3a2009-06-09 05:57:38961 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:12962 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
963 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:38964
[email protected]8199b3a2009-06-09 05:57:38965 // Test an empty directory, non-recursively, including "..".
966 file_util::FileEnumerator f0_dotdot(test_dir_, false,
967 static_cast<file_util::FileEnumerator::FILE_TYPE>(
968 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
969 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
970 f0_dotdot.Next().value());
971 EXPECT_EQ(FILE_PATH_LITERAL(""),
972 f0_dotdot.Next().value());
973
[email protected]37088fef2008-08-15 17:32:10974 // create the directories
[email protected]640517f2008-10-30 23:54:04975 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:10976 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]640517f2008-10-30 23:54:04977 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:10978 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:04979 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:10980 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:04981
[email protected]37088fef2008-08-15 17:32:10982 // create the files
[email protected]640517f2008-10-30 23:54:04983 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:10984 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:04985 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:10986 CreateTextFile(dir2innerfile, L"");
[email protected]640517f2008-10-30 23:54:04987 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:10988 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:04989 FilePath file2_rel =
990 dir2.Append(FilePath::kParentDirectory)
991 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:10992 CreateTextFile(file2_rel, L"");
[email protected]640517f2008-10-30 23:54:04993 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:38994
995 // Only enumerate files.
[email protected]0b733222008-12-11 14:55:12996 file_util::FileEnumerator f1(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:38997 file_util::FileEnumerator::FILES);
998 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:10999 EXPECT_TRUE(c1.HasFile(file1));
1000 EXPECT_TRUE(c1.HasFile(file2_abs));
1001 EXPECT_TRUE(c1.HasFile(dir2file));
1002 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1003 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:381004
1005 // Only enumerate directories.
[email protected]0b733222008-12-11 14:55:121006 file_util::FileEnumerator f2(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:381007 file_util::FileEnumerator::DIRECTORIES);
1008 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:101009 EXPECT_TRUE(c2.HasFile(dir1));
1010 EXPECT_TRUE(c2.HasFile(dir2));
1011 EXPECT_TRUE(c2.HasFile(dir2inner));
1012 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:381013
[email protected]3bc9ffaf2008-10-16 02:42:451014 // Only enumerate directories non-recursively.
1015 file_util::FileEnumerator f2_non_recursive(
[email protected]0b733222008-12-11 14:55:121016 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:451017 FindResultCollector c2_non_recursive(f2_non_recursive);
1018 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1019 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1020 EXPECT_EQ(c2_non_recursive.size(), 2);
1021
[email protected]8199b3a2009-06-09 05:57:381022 // Only enumerate directories, non-recursively, including "..".
1023 file_util::FileEnumerator f2_dotdot(
1024 test_dir_, false,
1025 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1026 file_util::FileEnumerator::DIRECTORIES |
1027 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1028 FindResultCollector c2_dotdot(f2_dotdot);
1029 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1030 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1031 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1032 EXPECT_EQ(c2_dotdot.size(), 3);
1033
initial.commitd7cae122008-07-26 21:49:381034 // Enumerate files and directories.
[email protected]8199b3a2009-06-09 05:57:381035 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381036 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:101037 EXPECT_TRUE(c3.HasFile(dir1));
1038 EXPECT_TRUE(c3.HasFile(dir2));
1039 EXPECT_TRUE(c3.HasFile(file1));
1040 EXPECT_TRUE(c3.HasFile(file2_abs));
1041 EXPECT_TRUE(c3.HasFile(dir2file));
1042 EXPECT_TRUE(c3.HasFile(dir2inner));
1043 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1044 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:381045
1046 // Non-recursive operation.
[email protected]8199b3a2009-06-09 05:57:381047 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381048 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:101049 EXPECT_TRUE(c4.HasFile(dir2));
1050 EXPECT_TRUE(c4.HasFile(dir2));
1051 EXPECT_TRUE(c4.HasFile(file1));
1052 EXPECT_TRUE(c4.HasFile(file2_abs));
1053 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:381054
1055 // Enumerate with a pattern.
[email protected]8199b3a2009-06-09 05:57:381056 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
[email protected]0b733222008-12-11 14:55:121057 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:381058 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:101059 EXPECT_TRUE(c5.HasFile(dir1));
1060 EXPECT_TRUE(c5.HasFile(dir2));
1061 EXPECT_TRUE(c5.HasFile(dir2file));
1062 EXPECT_TRUE(c5.HasFile(dir2inner));
1063 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1064 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:381065
1066 // Make sure the destructor closes the find handle while in the middle of a
1067 // query to allow TearDown to delete the directory.
[email protected]8199b3a2009-06-09 05:57:381068 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121069 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1070 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:381071}
license.botbf09a502008-08-24 00:55:551072
[email protected]8199b3a2009-06-09 05:57:381073TEST_F(FileUtilTest, FileEnumeratorOrderTest) {
1074 FilePath fileA = test_dir_.Append(FILE_PATH_LITERAL("a"));
1075 FilePath fileB = test_dir_.Append(FILE_PATH_LITERAL("B"));
1076 FilePath dirC = test_dir_.Append(FILE_PATH_LITERAL("C"));
1077 FilePath dirD = test_dir_.Append(FILE_PATH_LITERAL("d"));
1078 FilePath dirE = test_dir_.Append(FILE_PATH_LITERAL("e"));
1079 FilePath fileF = test_dir_.Append(FILE_PATH_LITERAL("f"));
1080
1081 // Create files/directories in near random order.
1082 CreateTextFile(fileF, L"");
1083 CreateTextFile(fileA, L"");
1084 CreateTextFile(fileB, L"");
1085 EXPECT_TRUE(file_util::CreateDirectory(dirE));
1086 EXPECT_TRUE(file_util::CreateDirectory(dirC));
1087 EXPECT_TRUE(file_util::CreateDirectory(dirD));
1088
[email protected]172c5502009-06-24 03:29:261089 // On Windows, files and directories are enumerated in the lexicographical
1090 // order, ignoring case and whether they are files or directories. On posix,
1091 // we order directories before files.
[email protected]8199b3a2009-06-09 05:57:381092 file_util::FileEnumerator enumerator(test_dir_, false, FILES_AND_DIRECTORIES);
1093 FilePath cur_file = enumerator.Next();
[email protected]172c5502009-06-24 03:29:261094#if defined(OS_WIN)
[email protected]8199b3a2009-06-09 05:57:381095 EXPECT_EQ(fileA.value(), cur_file.value());
1096 cur_file = enumerator.Next();
1097 EXPECT_EQ(fileB.value(), cur_file.value());
1098 cur_file = enumerator.Next();
1099 EXPECT_EQ(dirC.value(), cur_file.value());
1100 cur_file = enumerator.Next();
1101 EXPECT_EQ(dirD.value(), cur_file.value());
1102 cur_file = enumerator.Next();
1103 EXPECT_EQ(dirE.value(), cur_file.value());
1104 cur_file = enumerator.Next();
1105 EXPECT_EQ(fileF.value(), cur_file.value());
1106 cur_file = enumerator.Next();
[email protected]172c5502009-06-24 03:29:261107#elif defined(OS_POSIX)
1108 EXPECT_EQ(dirC.value(), cur_file.value());
1109 cur_file = enumerator.Next();
1110 EXPECT_EQ(dirD.value(), cur_file.value());
1111 cur_file = enumerator.Next();
1112 EXPECT_EQ(dirE.value(), cur_file.value());
1113 cur_file = enumerator.Next();
1114 EXPECT_EQ(fileA.value(), cur_file.value());
1115 cur_file = enumerator.Next();
1116 EXPECT_EQ(fileB.value(), cur_file.value());
1117 cur_file = enumerator.Next();
1118 EXPECT_EQ(fileF.value(), cur_file.value());
1119 cur_file = enumerator.Next();
1120#endif
1121
[email protected]8199b3a2009-06-09 05:57:381122 EXPECT_EQ(FILE_PATH_LITERAL(""), cur_file.value());
1123}
[email protected]b9e04f02008-11-27 04:03:571124
[email protected]ee5c29da2009-01-09 22:14:271125TEST_F(FileUtilTest, Contains) {
[email protected]11e53f62009-03-10 18:20:441126 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ee5c29da2009-01-09 22:14:271127
1128 // Create a fresh, empty copy of this directory.
[email protected]a92d2fd2009-01-31 01:19:571129 if (file_util::PathExists(data_dir)) {
1130 ASSERT_TRUE(file_util::Delete(data_dir, true));
1131 }
[email protected]ee5c29da2009-01-09 22:14:271132 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");
[email protected]4e9f6be2009-04-03 17:17:581143 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()));
[email protected]ee5c29da2009-01-09 22:14:271146
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"))));
[email protected]9e51af92009-02-04 00:58:391157 EXPECT_TRUE(file_util::ContainsPath(foo,
[email protected]ee5c29da2009-01-09 22:14:271158 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
[email protected]11b901ee2008-09-10 00:16:281168} // namespace