blob: 736b0b5fe50495f544b138de06c77cf50d06546e [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
erikkay@google.comc8ec9e92008-08-16 02:50:10 +09005#include "build/build_config.h"
6
erikkay@google.com014161d2008-08-16 02:45:13 +09007#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <windows.h>
initial.commit3f4a7322008-07-27 06:49:38 +09009#include <shellapi.h>
10#include <shlobj.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090011#endif
initial.commit3f4a7322008-07-27 06:49:38 +090012
13#include <fstream>
14#include <iostream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090015#include <set>
initial.commit3f4a7322008-07-27 06:49:38 +090016
17#include "base/base_paths.h"
evanm@google.com874d1672008-10-31 08:54:04 +090018#include "base/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090019#include "base/file_util.h"
20#include "base/logging.h"
21#include "base/path_service.h"
erikkay@google.com8d133f62009-04-24 00:05:19 +090022#include "base/platform_thread.h"
initial.commit3f4a7322008-07-27 06:49:38 +090023#include "base/string_util.h"
erikkay@google.com9ac26762009-04-18 09:42:48 +090024#include "base/time.h"
initial.commit3f4a7322008-07-27 06:49:38 +090025#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090026#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090027
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090028// This macro helps avoid wrapped lines in the test structs.
29#define FPL(x) FILE_PATH_LITERAL(x)
30
initial.commit3f4a7322008-07-27 06:49:38 +090031namespace {
32
erikkay@google.comf2406842008-08-21 00:59:49 +090033// file_util winds up using autoreleased objects on the Mac, so this needs
34// to be a PlatformTest
35class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +090036 protected:
37 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +090038 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +090039 // Name a subdirectory of the temp directory.
40 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +090041 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +090042
43 // Create a fresh, empty copy of this directory.
44 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +090045 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +090046 }
47 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +090048 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +090049 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +090050 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +090051 ASSERT_FALSE(file_util::PathExists(test_dir_));
52 }
53
54 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +090055 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +090056};
57
58// Collects all the results from the given file enumerator, and provides an
59// interface to query whether a given file is present.
60class FindResultCollector {
61 public:
62 FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +090063 FilePath cur_file;
64 while (!(cur_file = enumerator.Next()).value().empty()) {
65 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +090066 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +090067 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +090068 << "Same file returned twice";
69
70 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +090071 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +090072 }
73 }
74
75 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +090076 bool HasFile(const FilePath& file) const {
77 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +090078 }
evanm@google.com874d1672008-10-31 08:54:04 +090079
erikkay@google.comdfb51b22008-08-16 02:32:10 +090080 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +090081 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +090082 }
initial.commit3f4a7322008-07-27 06:49:38 +090083
84 private:
evanm@google.com874d1672008-10-31 08:54:04 +090085 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +090086};
87
88// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +090089void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +090090 const std::wstring& contents) {
91 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +090092 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +090093 ASSERT_TRUE(file.is_open());
94 file << contents;
95 file.close();
96}
97
98// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +090099std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900100 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900101 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900102 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900103 EXPECT_TRUE(file.is_open());
104 file.getline(contents, 64);
105 file.close();
106 return std::wstring(contents);
107}
108
erikkay@google.com014161d2008-08-16 02:45:13 +0900109#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900110uint64 FileTimeAsUint64(const FILETIME& ft) {
111 ULARGE_INTEGER u;
112 u.LowPart = ft.dwLowDateTime;
113 u.HighPart = ft.dwHighDateTime;
114 return u.QuadPart;
115}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900116#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900117
118const struct append_case {
119 const wchar_t* path;
120 const wchar_t* ending;
121 const wchar_t* result;
122} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900123#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900124 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
125 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
126 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
127 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
128 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
129 {L"", L"path", L"\\path"},
130 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900131#elif defined(OS_POSIX)
132 {L"/foo/bar", L"path", L"/foo/bar/path"},
133 {L"/foo/bar/", L"path", L"/foo/bar/path"},
134 {L"/foo/bar//", L"path", L"/foo/bar//path"},
135 {L"/foo/bar/", L"", L"/foo/bar/"},
136 {L"/foo/bar", L"", L"/foo/bar/"},
137 {L"", L"path", L"/path"},
138 {L"", L"", L"/"},
139#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900140};
141
initial.commit3f4a7322008-07-27 06:49:38 +0900142TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900143 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900144 const append_case& value = append_cases[i];
145 std::wstring result = value.path;
146 file_util::AppendToPath(&result, value.ending);
147 EXPECT_EQ(value.result, result);
148 }
149
150#ifdef NDEBUG
151 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
152#endif
153}
154
155static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900156 const FilePath::CharType* path;
157 const FilePath::CharType* suffix;
158 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900159} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900160 {FPL(""), FPL(""), FPL("")},
161 {FPL(""), FPL("txt"), FPL("txt")},
162 {FPL("."), FPL("txt"), FPL("txt.")},
163 {FPL("."), FPL(""), FPL(".")},
164 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
165 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
166 {FPL("foo"), FPL("txt"), FPL("footxt")},
167 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
168 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
169 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
170 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
171 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
172 {FPL("foo"), FPL(""), FPL("foo")},
173 {FPL("foo"), FPL("."), FPL("foo.")},
174 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
175 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900176#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900177 {FPL("\\"), FPL(""), FPL("\\")},
178 {FPL("\\"), FPL("txt"), FPL("\\txt")},
179 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
180 {FPL("\\."), FPL(""), FPL("\\.")},
181 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
182 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
183 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
184 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
185 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
186 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
187 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
188 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
189 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900190#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900191 {FPL("/"), FPL(""), FPL("/")},
192 {FPL("/"), FPL("txt"), FPL("/txt")},
193 {FPL("/."), FPL("txt"), FPL("/txt.")},
194 {FPL("/."), FPL(""), FPL("/.")},
195 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
196 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
197 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
198 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
199 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
200 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
201 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
202 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900203#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900204};
205
206TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900207 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900208 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900209 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900210 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900211 }
212}
213
214static const struct filename_case {
215 const wchar_t* path;
216 const wchar_t* filename;
217} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900218#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900219 {L"c:\\colon\\backslash", L"backslash"},
220 {L"c:\\colon\\backslash\\", L""},
221 {L"\\\\filename.exe", L"filename.exe"},
222 {L"filename.exe", L"filename.exe"},
223 {L"", L""},
224 {L"\\\\\\", L""},
225 {L"c:/colon/backslash", L"backslash"},
226 {L"c:/colon/backslash/", L""},
227 {L"//////", L""},
228 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900229#elif defined(OS_POSIX)
230 {L"/foo/bar", L"bar"},
231 {L"/foo/bar/", L""},
232 {L"/filename.exe", L"filename.exe"},
233 {L"filename.exe", L"filename.exe"},
234 {L"", L""},
235 {L"/", L""},
236#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900237};
238
239TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900240 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900241 const filename_case& value = filename_cases[i];
242 std::wstring result = file_util::GetFilenameFromPath(value.path);
243 EXPECT_EQ(value.filename, result);
244 }
245}
246
247// Test finding the file type from a path name
248static const struct extension_case {
249 const wchar_t* path;
250 const wchar_t* extension;
251} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900252#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900253 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
254 {L"C:\\colon\\backslash\\filename.", L""},
255 {L"C:\\colon\\backslash\\filename", L""},
256 {L"C:\\colon\\backslash\\", L""},
257 {L"C:\\colon\\backslash.\\", L""},
258 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900259#elif defined(OS_POSIX)
260 {L"/foo/bar/filename.extension", L"extension"},
261 {L"/foo/bar/filename.", L""},
262 {L"/foo/bar/filename", L""},
263 {L"/foo/bar/", L""},
264 {L"/foo/bar./", L""},
265 {L"/foo/bar/filename.extension.extension2", L"extension2"},
266 {L".", L""},
267 {L"..", L""},
268 {L"./foo", L""},
269 {L"./foo.extension", L"extension"},
270 {L"/foo.extension1/bar.extension2", L"extension2"},
271#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900272};
273
274TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900275 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900276 const extension_case& ext = extension_cases[i];
277 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
278 EXPECT_EQ(ext.extension, fext);
279 }
280}
281
282// Test finding the directory component of a path
283static const struct dir_case {
284 const wchar_t* full_path;
285 const wchar_t* directory;
286} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900287#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900288 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
289 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
290 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
291 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
292 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
293 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
294 {L"C:\\", L"C:"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900295#elif defined(OS_POSIX)
296 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
297 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
298 {L"/foo/bar/", L"/foo/bar"},
299 {L"/foo/bar//", L"/foo/bar"},
300 {L"/foo/bar", L"/foo"},
301 {L"/foo/bar./", L"/foo/bar."},
302 {L"/", L"/"},
303 {L".", L"."},
304 {L"..", L"."}, // yes, ".." technically lives in "."
305#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900306};
307
308TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900309 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900310 const dir_case& dir = dir_cases[i];
311 const std::wstring parent =
312 file_util::GetDirectoryFromPath(dir.full_path);
313 EXPECT_EQ(dir.directory, parent);
314 }
315}
316
317TEST_F(FileUtilTest, CountFilesCreatedAfter) {
318 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900319 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("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
evan@chromium.org37301322009-04-21 10:50:39 +0900323#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900324 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900325#elif defined(OS_POSIX)
326 // We need to wait at least one second here because the precision of
327 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900328 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900329#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900330
331 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900332 base::Time now(base::Time::NowFromSystemTime());
333 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900334
335 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900336 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900337 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
338
339 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900340 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900341
342 // Delete new file, we should see no files after cutoff now
343 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900344 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900345}
346
347// Tests that the Delete function works as expected, especially
348// the recursion flag. Also coincidentally tests PathExists.
349TEST_F(FileUtilTest, Delete) {
350 // Create a file
evanm@google.com874d1672008-10-31 08:54:04 +0900351 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900352 CreateTextFile(file_name, L"I'm cannon fodder.");
353
354 ASSERT_TRUE(file_util::PathExists(file_name));
355
evanm@google.com874d1672008-10-31 08:54:04 +0900356 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
357 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900358
359 ASSERT_TRUE(file_util::PathExists(subdir_path));
360
evanm@google.com874d1672008-10-31 08:54:04 +0900361 FilePath directory_contents = test_dir_;
erikkay@google.com014161d2008-08-16 02:45:13 +0900362#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900363 // TODO(erikkay): see if anyone's actually using this feature of the API
evanm@google.com874d1672008-10-31 08:54:04 +0900364 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900365 // Delete non-recursively and check that only the file is deleted
366 ASSERT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900367 EXPECT_FALSE(file_util::PathExists(file_name));
368 EXPECT_TRUE(file_util::PathExists(subdir_path));
369#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900370
371 // Delete recursively and make sure all contents are deleted
372 ASSERT_TRUE(file_util::Delete(directory_contents, true));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900373 EXPECT_FALSE(file_util::PathExists(file_name));
374 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900375}
376
377TEST_F(FileUtilTest, Move) {
378 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900379 FilePath dir_name_from =
380 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
381 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900382 ASSERT_TRUE(file_util::PathExists(dir_name_from));
383
384 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900385 FilePath file_name_from =
386 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900387 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
388 ASSERT_TRUE(file_util::PathExists(file_name_from));
389
390 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900391 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
392 FilePath file_name_to =
393 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900394
395 ASSERT_FALSE(file_util::PathExists(dir_name_to));
396
397 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
398
399 // Check everything has been moved.
400 EXPECT_FALSE(file_util::PathExists(dir_name_from));
401 EXPECT_FALSE(file_util::PathExists(file_name_from));
402 EXPECT_TRUE(file_util::PathExists(dir_name_to));
403 EXPECT_TRUE(file_util::PathExists(file_name_to));
404}
405
406TEST_F(FileUtilTest, CopyDirectoryRecursively) {
407 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900408 FilePath dir_name_from =
409 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
410 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900411 ASSERT_TRUE(file_util::PathExists(dir_name_from));
412
413 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900414 FilePath file_name_from =
415 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900416 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
417 ASSERT_TRUE(file_util::PathExists(file_name_from));
418
419 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900420 FilePath subdir_name_from =
421 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
422 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900423 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
424
425 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900426 FilePath file_name2_from =
427 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900428 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
429 ASSERT_TRUE(file_util::PathExists(file_name2_from));
430
431 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900432 FilePath dir_name_to =
433 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
434 FilePath file_name_to =
435 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
436 FilePath subdir_name_to =
437 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
438 FilePath file_name2_to =
439 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900440
441 ASSERT_FALSE(file_util::PathExists(dir_name_to));
442
443 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
444
445 // Check everything has been copied.
446 EXPECT_TRUE(file_util::PathExists(dir_name_from));
447 EXPECT_TRUE(file_util::PathExists(file_name_from));
448 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
449 EXPECT_TRUE(file_util::PathExists(file_name2_from));
450 EXPECT_TRUE(file_util::PathExists(dir_name_to));
451 EXPECT_TRUE(file_util::PathExists(file_name_to));
452 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
453 EXPECT_TRUE(file_util::PathExists(file_name2_to));
454}
455
456TEST_F(FileUtilTest, CopyDirectory) {
457 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900458 FilePath dir_name_from =
459 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
460 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900461 ASSERT_TRUE(file_util::PathExists(dir_name_from));
462
463 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900464 FilePath file_name_from =
465 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900466 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
467 ASSERT_TRUE(file_util::PathExists(file_name_from));
468
469 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900470 FilePath subdir_name_from =
471 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
472 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900473 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
474
475 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900476 FilePath file_name2_from =
477 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900478 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
479 ASSERT_TRUE(file_util::PathExists(file_name2_from));
480
481 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900482 FilePath dir_name_to =
483 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
484 FilePath file_name_to =
485 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
486 FilePath subdir_name_to =
487 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +0900488
489 ASSERT_FALSE(file_util::PathExists(dir_name_to));
490
491 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
492
493 // Check everything has been copied.
494 EXPECT_TRUE(file_util::PathExists(dir_name_from));
495 EXPECT_TRUE(file_util::PathExists(file_name_from));
496 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
497 EXPECT_TRUE(file_util::PathExists(file_name2_from));
498 EXPECT_TRUE(file_util::PathExists(dir_name_to));
499 EXPECT_TRUE(file_util::PathExists(file_name_to));
500 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
501}
502
503TEST_F(FileUtilTest, CopyFile) {
504 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900505 FilePath dir_name_from =
506 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
507 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900508 ASSERT_TRUE(file_util::PathExists(dir_name_from));
509
510 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900511 FilePath file_name_from =
512 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900513 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
514 CreateTextFile(file_name_from, file_contents);
515 ASSERT_TRUE(file_util::PathExists(file_name_from));
516
517 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900518 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900519 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900520
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900521 // Copy the file to another location using '..' in the path.
evanm@google.com874d1672008-10-31 08:54:04 +0900522 std::wstring dest_file2(dir_name_from.ToWStringHack());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900523 file_util::AppendToPath(&dest_file2, L"..");
524 file_util::AppendToPath(&dest_file2, L"DestFile.txt");
evanm@google.com874d1672008-10-31 08:54:04 +0900525 ASSERT_TRUE(file_util::CopyFile(file_name_from,
526 FilePath::FromWStringHack(dest_file2)));
527 std::wstring dest_file2_test(dir_name_from.ToWStringHack());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900528 file_util::UpOneDirectory(&dest_file2_test);
529 file_util::AppendToPath(&dest_file2_test, L"DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900530
531 // Check everything has been copied.
532 EXPECT_TRUE(file_util::PathExists(file_name_from));
533 EXPECT_TRUE(file_util::PathExists(dest_file));
534 const std::wstring read_contents = ReadTextFile(dest_file);
535 EXPECT_EQ(file_contents, read_contents);
evanm@google.com874d1672008-10-31 08:54:04 +0900536 EXPECT_TRUE(file_util::PathExists(
537 FilePath::FromWStringHack(dest_file2_test)));
538 EXPECT_TRUE(file_util::PathExists(FilePath::FromWStringHack(dest_file2)));
initial.commit3f4a7322008-07-27 06:49:38 +0900539}
540
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900541// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900542#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900543TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +0900544 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900545
546 SYSTEMTIME start_time;
547 GetLocalTime(&start_time);
548 Sleep(100);
549 CreateTextFile(file_name, L"New file!");
550 Sleep(100);
551 SYSTEMTIME end_time;
552 GetLocalTime(&end_time);
553
554 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +0900555 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +0900556
557 FILETIME start_filetime;
558 SystemTimeToFileTime(&start_time, &start_filetime);
559 FILETIME end_filetime;
560 SystemTimeToFileTime(&end_time, &end_filetime);
561 FILETIME file_creation_filetime;
562 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
563
564 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
565 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
566 "creation time: " << FileTimeAsUint64(file_creation_filetime);
567
568 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
569 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
570 "end time: " << FileTimeAsUint64(end_filetime);
571
evanm@google.com874d1672008-10-31 08:54:04 +0900572 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +0900573}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900574#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900575
erikkay@google.comf2406842008-08-21 00:59:49 +0900576// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +0900577// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +0900578typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +0900579
erikkay@google.comf2406842008-08-21 00:59:49 +0900580TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +0900581 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +0900582 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +0900583 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
584 .Append(FILE_PATH_LITERAL("data"))
585 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900586 ASSERT_TRUE(file_util::PathExists(data_dir));
587
evanm@google.com874d1672008-10-31 08:54:04 +0900588 FilePath original_file =
589 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
590 FilePath same_file =
591 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
592 FilePath same_length_file =
593 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
594 FilePath different_file =
595 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
596 FilePath different_first_file =
597 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
598 FilePath different_last_file =
599 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
600 FilePath empty1_file =
601 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
602 FilePath empty2_file =
603 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
604 FilePath shortened_file =
605 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
606 FilePath binary_file =
607 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
608 FilePath binary_file_same =
609 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
610 FilePath binary_file_diff =
611 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +0900612
613 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
614 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
615 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
616 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
617 EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname"));
618 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
619 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
620 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
621 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
622 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
623 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
624 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
625}
626
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900627// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +0900628#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900629TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900630 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900631 CreateTextFile(target_file, L"This is the target.");
632
evanm@google.com874d1672008-10-31 08:54:04 +0900633 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900634
635 HRESULT result;
636 IShellLink *shell = NULL;
637 IPersistFile *persist = NULL;
638
639 CoInitialize(NULL);
640 // Temporarily create a shortcut for test
641 result = CoCreateInstance(CLSID_ShellLink, NULL,
642 CLSCTX_INPROC_SERVER, IID_IShellLink,
643 reinterpret_cast<LPVOID*>(&shell));
644 EXPECT_TRUE(SUCCEEDED(result));
645 result = shell->QueryInterface(IID_IPersistFile,
646 reinterpret_cast<LPVOID*>(&persist));
647 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900648 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900649 EXPECT_TRUE(SUCCEEDED(result));
650 result = shell->SetDescription(L"ResolveShortcutTest");
651 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900652 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +0900653 EXPECT_TRUE(SUCCEEDED(result));
654 if (persist)
655 persist->Release();
656 if (shell)
657 shell->Release();
658
659 bool is_solved;
evanm@google.com874d1672008-10-31 08:54:04 +0900660 std::wstring link_file_str = link_file.value();
661 is_solved = file_util::ResolveShortcut(&link_file_str);
initial.commit3f4a7322008-07-27 06:49:38 +0900662 EXPECT_TRUE(is_solved);
663 std::wstring contents;
evanm@google.com874d1672008-10-31 08:54:04 +0900664 contents = ReadTextFile(FilePath(link_file_str));
initial.commit3f4a7322008-07-27 06:49:38 +0900665 EXPECT_EQ(L"This is the target.", contents);
666
ericroman@google.comdbff4f52008-08-19 01:00:38 +0900667 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +0900668 DeleteFile(target_file.value().c_str());
669 DeleteFile(link_file_str.c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900670 CoUninitialize();
671}
672
673TEST_F(FileUtilTest, CreateShortcutTest) {
674 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +0900675 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900676 CreateTextFile(target_file, file_contents);
677
evanm@google.com874d1672008-10-31 08:54:04 +0900678 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900679
680 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +0900681 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
682 link_file.value().c_str(),
initial.commit3f4a7322008-07-27 06:49:38 +0900683 NULL, NULL, NULL, NULL, 0));
evanm@google.com874d1672008-10-31 08:54:04 +0900684 std::wstring resolved_name = link_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900685 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evanm@google.com874d1672008-10-31 08:54:04 +0900686 std::wstring read_contents = ReadTextFile(FilePath(resolved_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900687 EXPECT_EQ(file_contents, read_contents);
688
evanm@google.com874d1672008-10-31 08:54:04 +0900689 DeleteFile(target_file.value().c_str());
690 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900691 CoUninitialize();
692}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +0900693
694TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
695 // Create a directory
696 FilePath dir_name_from =
697 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
698 file_util::CreateDirectory(dir_name_from);
699 ASSERT_TRUE(file_util::PathExists(dir_name_from));
700
701 // Create a file under the directory
702 FilePath file_name_from =
703 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
704 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
705 ASSERT_TRUE(file_util::PathExists(file_name_from));
706
707 // Move the directory by using CopyAndDeleteDirectory
708 FilePath dir_name_to = test_dir_.Append(
709 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
710 FilePath file_name_to =
711 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
712
713 ASSERT_FALSE(file_util::PathExists(dir_name_to));
714
715 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
716
717 // Check everything has been moved.
718 EXPECT_FALSE(file_util::PathExists(dir_name_from));
719 EXPECT_FALSE(file_util::PathExists(file_name_from));
720 EXPECT_TRUE(file_util::PathExists(dir_name_to));
721 EXPECT_TRUE(file_util::PathExists(file_name_to));
722}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900723#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900724
725TEST_F(FileUtilTest, CreateTemporaryFileNameTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900726 std::wstring temp_files[3];
727 for (int i = 0; i < 3; i++) {
728 ASSERT_TRUE(file_util::CreateTemporaryFileName(&(temp_files[i])));
729 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
730 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
731 }
732 for (int i = 0; i < 3; i++)
733 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
734 for (int i = 0; i < 3; i++)
735 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
736}
737
738TEST_F(FileUtilTest, CreateAndOpenTemporaryFileNameTest) {
739 FilePath names[3];
740 FILE *fps[3];
741 int i;
742
743 // Create; make sure they are open and exist.
744 for (i = 0; i < 3; ++i) {
745 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
746 ASSERT_TRUE(fps[i]);
747 EXPECT_TRUE(file_util::PathExists(names[i]));
748 }
749
750 // Make sure all names are unique.
751 for (i = 0; i < 3; ++i) {
752 EXPECT_FALSE(names[i] == names[(i+1)%3]);
753 }
754
755 // Close and delete.
756 for (i = 0; i < 3; ++i) {
757 EXPECT_TRUE(file_util::CloseFile(fps[i]));
758 EXPECT_TRUE(file_util::Delete(names[i], false));
759 }
initial.commit3f4a7322008-07-27 06:49:38 +0900760}
761
762TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
763 std::wstring temp_dir;
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900764 ASSERT_TRUE(file_util::CreateNewTempDirectory(std::wstring(), &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900765 EXPECT_TRUE(file_util::PathExists(temp_dir));
766 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +0900767}
768
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900769TEST_F(FileUtilTest, GetShmemTempDirTest) {
770 FilePath dir;
771 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
772 EXPECT_TRUE(file_util::DirectoryExists(dir));
773}
774
initial.commit3f4a7322008-07-27 06:49:38 +0900775TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900776 FilePath test_root =
777 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +0900778#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +0900779 FilePath test_path =
780 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900781#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +0900782 FilePath test_path =
783 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900784#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +0900785
786 EXPECT_FALSE(file_util::PathExists(test_path));
787 EXPECT_TRUE(file_util::CreateDirectory(test_path));
788 EXPECT_TRUE(file_util::PathExists(test_path));
789 // CreateDirectory returns true if the DirectoryExists returns true.
790 EXPECT_TRUE(file_util::CreateDirectory(test_path));
791
792 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +0900793 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900794 EXPECT_FALSE(file_util::PathExists(test_path));
795 CreateTextFile(test_path, L"test file");
796 EXPECT_TRUE(file_util::PathExists(test_path));
797 EXPECT_FALSE(file_util::CreateDirectory(test_path));
798
799 EXPECT_TRUE(file_util::Delete(test_root, true));
800 EXPECT_FALSE(file_util::PathExists(test_root));
801 EXPECT_FALSE(file_util::PathExists(test_path));
802}
803
804TEST_F(FileUtilTest, DetectDirectoryTest) {
805 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900806 FilePath test_root =
807 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900808 EXPECT_FALSE(file_util::PathExists(test_root));
809 EXPECT_TRUE(file_util::CreateDirectory(test_root));
810 EXPECT_TRUE(file_util::PathExists(test_root));
811 EXPECT_TRUE(file_util::DirectoryExists(test_root));
812
813 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +0900814 FilePath test_path =
815 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900816 EXPECT_FALSE(file_util::PathExists(test_path));
817 CreateTextFile(test_path, L"test file");
818 EXPECT_TRUE(file_util::PathExists(test_path));
819 EXPECT_FALSE(file_util::DirectoryExists(test_path));
820 EXPECT_TRUE(file_util::Delete(test_path, false));
821
822 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +0900823}
824
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900825static const struct goodbad_pair {
initial.commit3f4a7322008-07-27 06:49:38 +0900826 std::wstring bad_name;
827 std::wstring good_name;
828} kIllegalCharacterCases[] = {
829 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
830 {L"**********::::.txt", L"--------------.txt"},
initial.commit3f4a7322008-07-27 06:49:38 +0900831 // We can't use UCNs (universal character names) for C0/C1 characters and
832 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
833 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
erikkay@google.com014161d2008-08-16 02:45:13 +0900834#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900835 {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
initial.commit3f4a7322008-07-27 06:49:38 +0900836 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
837 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900838#elif defined(OS_POSIX)
839 {L"bad*file?name.jpg", L"bad-file-name.jpg"},
840 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"},
841 {L"bad\uFFFFfile-name.jpg ", L"bad-file-name.jpg"},
842#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900843 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
844 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
845 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
846 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
847 // Unassigned codepoints are ok.
848 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
849};
850
851TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900852 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900853 std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
854 file_util::ReplaceIllegalCharacters(&bad_name, L'-');
855 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
856 }
857}
858
859static const struct ReplaceExtensionCase {
860 std::wstring file_name;
estade@chromium.org63343202008-12-05 05:46:06 +0900861 FilePath::StringType extension;
initial.commit3f4a7322008-07-27 06:49:38 +0900862 std::wstring result;
863} kReplaceExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +0900864 {L"", FILE_PATH_LITERAL(""), L""},
865 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
866 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
867 {L".", FILE_PATH_LITERAL(""), L""},
868 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
869 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
870 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
871 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
872 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
873 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
874 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
875 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
876 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
877 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
878 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
879 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commit3f4a7322008-07-27 06:49:38 +0900880};
881
882TEST_F(FileUtilTest, ReplaceExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900883 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +0900884 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
885 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
886 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +0900887 }
888}
889
sky@google.com71e7c6f2008-09-20 02:32:18 +0900890// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
891// the directory names of the path.
892TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
estade@chromium.org63343202008-12-05 05:46:06 +0900893 FilePath path;
894 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
895 path = path.Append(FILE_PATH_LITERAL("foo"));
sky@google.com71e7c6f2008-09-20 02:32:18 +0900896 // '/foo.bar/foo' with extension '.baz'
estade@chromium.org63343202008-12-05 05:46:06 +0900897 FilePath result_path = path;
898 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900899 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
900 result_path.value());
sky@google.com71e7c6f2008-09-20 02:32:18 +0900901}
902
initial.commit3f4a7322008-07-27 06:49:38 +0900903TEST_F(FileUtilTest, FileEnumeratorTest) {
904 // Test an empty directory.
avi@google.com5cb79352008-12-11 23:55:12 +0900905 file_util::FileEnumerator f0(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900906 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +0900907 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
908 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +0900909
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900910 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +0900911 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900912 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +0900913 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900914 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +0900915 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900916 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +0900917
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900918 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +0900919 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900920 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900921 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900922 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900923 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900924 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900925 FilePath file2_rel =
926 dir2.Append(FilePath::kParentDirectory)
927 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900928 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900929 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900930
931 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +0900932 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900933 file_util::FileEnumerator::FILES);
934 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900935 EXPECT_TRUE(c1.HasFile(file1));
936 EXPECT_TRUE(c1.HasFile(file2_abs));
937 EXPECT_TRUE(c1.HasFile(dir2file));
938 EXPECT_TRUE(c1.HasFile(dir2innerfile));
939 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +0900940
941 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +0900942 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900943 file_util::FileEnumerator::DIRECTORIES);
944 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900945 EXPECT_TRUE(c2.HasFile(dir1));
946 EXPECT_TRUE(c2.HasFile(dir2));
947 EXPECT_TRUE(c2.HasFile(dir2inner));
948 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +0900949
tim@chromium.org989d0972008-10-16 11:42:45 +0900950 // Only enumerate directories non-recursively.
951 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +0900952 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +0900953 FindResultCollector c2_non_recursive(f2_non_recursive);
954 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
955 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
956 EXPECT_EQ(c2_non_recursive.size(), 2);
957
initial.commit3f4a7322008-07-27 06:49:38 +0900958 // Enumerate files and directories.
avi@google.com5cb79352008-12-11 23:55:12 +0900959 file_util::FileEnumerator f3(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900960 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
961 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900962 EXPECT_TRUE(c3.HasFile(dir1));
963 EXPECT_TRUE(c3.HasFile(dir2));
964 EXPECT_TRUE(c3.HasFile(file1));
965 EXPECT_TRUE(c3.HasFile(file2_abs));
966 EXPECT_TRUE(c3.HasFile(dir2file));
967 EXPECT_TRUE(c3.HasFile(dir2inner));
968 EXPECT_TRUE(c3.HasFile(dir2innerfile));
969 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +0900970
971 // Non-recursive operation.
avi@google.com5cb79352008-12-11 23:55:12 +0900972 file_util::FileEnumerator f4(test_dir_, false,
initial.commit3f4a7322008-07-27 06:49:38 +0900973 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
974 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900975 EXPECT_TRUE(c4.HasFile(dir2));
976 EXPECT_TRUE(c4.HasFile(dir2));
977 EXPECT_TRUE(c4.HasFile(file1));
978 EXPECT_TRUE(c4.HasFile(file2_abs));
979 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +0900980
981 // Enumerate with a pattern.
avi@google.com5cb79352008-12-11 23:55:12 +0900982 file_util::FileEnumerator f5(test_dir_, true,
983 file_util::FileEnumerator::FILES_AND_DIRECTORIES,
984 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900985 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900986 EXPECT_TRUE(c5.HasFile(dir1));
987 EXPECT_TRUE(c5.HasFile(dir2));
988 EXPECT_TRUE(c5.HasFile(dir2file));
989 EXPECT_TRUE(c5.HasFile(dir2inner));
990 EXPECT_TRUE(c5.HasFile(dir2innerfile));
991 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +0900992
993 // Make sure the destructor closes the find handle while in the middle of a
994 // query to allow TearDown to delete the directory.
avi@google.com5cb79352008-12-11 23:55:12 +0900995 file_util::FileEnumerator f6(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900996 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +0900997 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
998 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +0900999}
license.botf003cfe2008-08-24 09:55:55 +09001000
estade@chromium.org97e37822008-11-27 13:03:57 +09001001
1002void PathComponents(const std::wstring& path,
1003 std::vector<std::wstring>* components) {
1004 DCHECK(components != NULL);
1005 if (components == NULL)
1006 return;
1007 std::wstring::size_type start = 0;
1008 std::wstring::size_type end = path.find('/', start);
1009
1010 // Special case the "/" or "\" directory. On Windows with a drive letter,
1011 // this code path won't hit, but the right thing should still happen.
1012 // "E:\foo" will turn into "E:","foo".
1013 if (end == start) {
1014 components->push_back(std::wstring(path, 0, 1));
1015 start = end + 1;
1016 end = path.find('/', start);
1017 }
1018 while (end != std::wstring::npos) {
1019 std::wstring component = std::wstring(path, start, end - start);
1020 components->push_back(component);
1021 start = end + 1;
1022 end = path.find('/', start);
1023 }
1024 std::wstring component = std::wstring(path, start);
1025 components->push_back(component);
1026}
1027
1028static const struct PathComponentsCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001029 const FilePath::CharType* path;
1030 const FilePath::CharType* result;
estade@chromium.org97e37822008-11-27 13:03:57 +09001031} kPathComponents[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001032 {FILE_PATH_LITERAL("/foo/bar/baz/"), FILE_PATH_LITERAL("/|foo|bar|baz|")},
1033 {FILE_PATH_LITERAL("/foo/bar/baz"), FILE_PATH_LITERAL("/|foo|bar|baz")},
1034 {FILE_PATH_LITERAL("e:/foo"), FILE_PATH_LITERAL("e:|foo")},
estade@chromium.org97e37822008-11-27 13:03:57 +09001035};
1036
1037TEST_F(FileUtilTest, PathComponentsTest) {
1038 for (size_t i = 0; i < arraysize(kPathComponents); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001039 FilePath path(kPathComponents[i].path);
estade@chromium.org97e37822008-11-27 13:03:57 +09001040 std::vector<FilePath::StringType> comps;
1041 file_util::PathComponents(path, &comps);
1042
1043 FilePath::StringType result;
1044 for (size_t j = 0; j < comps.size(); ++j) {
1045 result.append(comps[j]);
1046 if (j < comps.size() - 1)
1047 result.append(FILE_PATH_LITERAL("|"), 1);
1048 }
1049 EXPECT_EQ(kPathComponents[i].result, result);
1050 }
1051}
1052
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001053TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001054 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001055
1056 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001057 if (file_util::PathExists(data_dir)) {
1058 ASSERT_TRUE(file_util::Delete(data_dir, true));
1059 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001060 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1061
1062 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1063 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1064 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1065 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1066
1067 // Annoyingly, the directories must actually exist in order for realpath(),
1068 // which Contains() relies on in posix, to work.
1069 ASSERT_TRUE(file_util::CreateDirectory(foo));
1070 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001071 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1072 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1073 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001074
1075 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1076 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1077 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1078 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1079
1080// Platform-specific concerns
1081 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1082#if defined(OS_WIN)
1083 EXPECT_TRUE(file_util::ContainsPath(foo,
1084 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001085 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001086 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
1087#elif defined(OS_LINUX)
1088 EXPECT_FALSE(file_util::ContainsPath(foo,
1089 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
1090#else
1091 // We can't really do this test on osx since the case-sensitivity of the
1092 // filesystem is configurable.
1093#endif
1094}
1095
mark@chromium.org17684802008-09-10 09:16:28 +09001096} // namespace