blob: 02ddc0a42324cbd7293435d2a1945d2517923ebe [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
erikkay@google.comc8ec9e92008-08-16 02:50:10 +09005#include "build/build_config.h"
6
erikkay@google.com014161d2008-08-16 02:45:13 +09007#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <windows.h>
initial.commit3f4a7322008-07-27 06:49:38 +09009#include <shellapi.h>
10#include <shlobj.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090011#endif
initial.commit3f4a7322008-07-27 06:49:38 +090012
13#include <fstream>
14#include <iostream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090015#include <set>
initial.commit3f4a7322008-07-27 06:49:38 +090016
17#include "base/base_paths.h"
evanm@google.com874d1672008-10-31 08:54:04 +090018#include "base/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090019#include "base/file_util.h"
20#include "base/logging.h"
21#include "base/path_service.h"
22#include "base/string_util.h"
erikkay@google.com9ac26762009-04-18 09:42:48 +090023#include "base/time.h"
initial.commit3f4a7322008-07-27 06:49:38 +090024#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090025#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090026
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090027// This macro helps avoid wrapped lines in the test structs.
28#define FPL(x) FILE_PATH_LITERAL(x)
29
initial.commit3f4a7322008-07-27 06:49:38 +090030namespace {
31
erikkay@google.comf2406842008-08-21 00:59:49 +090032// file_util winds up using autoreleased objects on the Mac, so this needs
33// to be a PlatformTest
34class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +090035 protected:
36 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +090037 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +090038 // Name a subdirectory of the temp directory.
39 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +090040 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +090041
42 // Create a fresh, empty copy of this directory.
43 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +090044 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +090045 }
46 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +090047 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +090048 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +090049 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +090050 ASSERT_FALSE(file_util::PathExists(test_dir_));
51 }
52
53 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +090054 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +090055};
56
57// Collects all the results from the given file enumerator, and provides an
58// interface to query whether a given file is present.
59class FindResultCollector {
60 public:
61 FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +090062 FilePath cur_file;
63 while (!(cur_file = enumerator.Next()).value().empty()) {
64 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +090065 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +090066 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +090067 << "Same file returned twice";
68
69 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +090070 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +090071 }
72 }
73
74 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +090075 bool HasFile(const FilePath& file) const {
76 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +090077 }
evanm@google.com874d1672008-10-31 08:54:04 +090078
erikkay@google.comdfb51b22008-08-16 02:32:10 +090079 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +090080 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +090081 }
initial.commit3f4a7322008-07-27 06:49:38 +090082
83 private:
evanm@google.com874d1672008-10-31 08:54:04 +090084 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +090085};
86
87// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +090088void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +090089 const std::wstring& contents) {
90 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +090091 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +090092 ASSERT_TRUE(file.is_open());
93 file << contents;
94 file.close();
95}
96
97// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +090098std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +090099 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900100 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900101 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900102 EXPECT_TRUE(file.is_open());
103 file.getline(contents, 64);
104 file.close();
105 return std::wstring(contents);
106}
107
erikkay@google.com014161d2008-08-16 02:45:13 +0900108#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900109uint64 FileTimeAsUint64(const FILETIME& ft) {
110 ULARGE_INTEGER u;
111 u.LowPart = ft.dwLowDateTime;
112 u.HighPart = ft.dwHighDateTime;
113 return u.QuadPart;
114}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900115#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900116
117const struct append_case {
118 const wchar_t* path;
119 const wchar_t* ending;
120 const wchar_t* result;
121} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900122#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900123 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
124 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
125 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
126 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
127 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
128 {L"", L"path", L"\\path"},
129 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900130#elif defined(OS_POSIX)
131 {L"/foo/bar", L"path", L"/foo/bar/path"},
132 {L"/foo/bar/", L"path", L"/foo/bar/path"},
133 {L"/foo/bar//", L"path", L"/foo/bar//path"},
134 {L"/foo/bar/", L"", L"/foo/bar/"},
135 {L"/foo/bar", L"", L"/foo/bar/"},
136 {L"", L"path", L"/path"},
137 {L"", L"", L"/"},
138#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900139};
140
initial.commit3f4a7322008-07-27 06:49:38 +0900141TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900142 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900143 const append_case& value = append_cases[i];
144 std::wstring result = value.path;
145 file_util::AppendToPath(&result, value.ending);
146 EXPECT_EQ(value.result, result);
147 }
148
149#ifdef NDEBUG
150 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
151#endif
152}
153
154static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900155 const FilePath::CharType* path;
156 const FilePath::CharType* suffix;
157 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900158} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900159 {FPL(""), FPL(""), FPL("")},
160 {FPL(""), FPL("txt"), FPL("txt")},
161 {FPL("."), FPL("txt"), FPL("txt.")},
162 {FPL("."), FPL(""), FPL(".")},
163 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
164 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
165 {FPL("foo"), FPL("txt"), FPL("footxt")},
166 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
167 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
168 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
169 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
170 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
171 {FPL("foo"), FPL(""), FPL("foo")},
172 {FPL("foo"), FPL("."), FPL("foo.")},
173 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
174 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900175#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900176 {FPL("\\"), FPL(""), FPL("\\")},
177 {FPL("\\"), FPL("txt"), FPL("\\txt")},
178 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
179 {FPL("\\."), FPL(""), FPL("\\.")},
180 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
181 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
182 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
183 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
184 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
185 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
186 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
187 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
188 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900189#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900190 {FPL("/"), FPL(""), FPL("/")},
191 {FPL("/"), FPL("txt"), FPL("/txt")},
192 {FPL("/."), FPL("txt"), FPL("/txt.")},
193 {FPL("/."), FPL(""), FPL("/.")},
194 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
195 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
196 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
197 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
198 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
199 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
200 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
201 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900202#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900203};
204
205TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900206 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900207 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900208 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900209 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900210 }
211}
212
213static const struct filename_case {
214 const wchar_t* path;
215 const wchar_t* filename;
216} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900217#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900218 {L"c:\\colon\\backslash", L"backslash"},
219 {L"c:\\colon\\backslash\\", L""},
220 {L"\\\\filename.exe", L"filename.exe"},
221 {L"filename.exe", L"filename.exe"},
222 {L"", L""},
223 {L"\\\\\\", L""},
224 {L"c:/colon/backslash", L"backslash"},
225 {L"c:/colon/backslash/", L""},
226 {L"//////", L""},
227 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900228#elif defined(OS_POSIX)
229 {L"/foo/bar", L"bar"},
230 {L"/foo/bar/", L""},
231 {L"/filename.exe", L"filename.exe"},
232 {L"filename.exe", L"filename.exe"},
233 {L"", L""},
234 {L"/", L""},
235#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900236};
237
238TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900239 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900240 const filename_case& value = filename_cases[i];
241 std::wstring result = file_util::GetFilenameFromPath(value.path);
242 EXPECT_EQ(value.filename, result);
243 }
244}
245
246// Test finding the file type from a path name
247static const struct extension_case {
248 const wchar_t* path;
249 const wchar_t* extension;
250} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900251#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900252 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
253 {L"C:\\colon\\backslash\\filename.", L""},
254 {L"C:\\colon\\backslash\\filename", L""},
255 {L"C:\\colon\\backslash\\", L""},
256 {L"C:\\colon\\backslash.\\", L""},
257 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900258#elif defined(OS_POSIX)
259 {L"/foo/bar/filename.extension", L"extension"},
260 {L"/foo/bar/filename.", L""},
261 {L"/foo/bar/filename", L""},
262 {L"/foo/bar/", L""},
263 {L"/foo/bar./", L""},
264 {L"/foo/bar/filename.extension.extension2", L"extension2"},
265 {L".", L""},
266 {L"..", L""},
267 {L"./foo", L""},
268 {L"./foo.extension", L"extension"},
269 {L"/foo.extension1/bar.extension2", L"extension2"},
270#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900271};
272
273TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900274 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900275 const extension_case& ext = extension_cases[i];
276 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
277 EXPECT_EQ(ext.extension, fext);
278 }
279}
280
281// Test finding the directory component of a path
282static const struct dir_case {
283 const wchar_t* full_path;
284 const wchar_t* directory;
285} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900286#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900287 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
288 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
289 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
290 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
291 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
292 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
293 {L"C:\\", L"C:"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900294#elif defined(OS_POSIX)
295 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
296 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
297 {L"/foo/bar/", L"/foo/bar"},
298 {L"/foo/bar//", L"/foo/bar"},
299 {L"/foo/bar", L"/foo"},
300 {L"/foo/bar./", L"/foo/bar."},
301 {L"/", L"/"},
302 {L".", L"."},
303 {L"..", L"."}, // yes, ".." technically lives in "."
304#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900305};
306
307TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900308 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900309 const dir_case& dir = dir_cases[i];
310 const std::wstring parent =
311 file_util::GetDirectoryFromPath(dir.full_path);
312 EXPECT_EQ(dir.directory, parent);
313 }
314}
315
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900316// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900317#if defined OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +0900318TEST_F(FileUtilTest, CountFilesCreatedAfter) {
319 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900320 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900321 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
322
323 // Age to perfection
324 Sleep(100);
325
326 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900327 base::Time now(base::Time::NowFromSystemTime());
328 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900329
330 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900331 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900332 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
333
334 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900335 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900336
337 // Delete new file, we should see no files after cutoff now
338 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900339 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900340}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900341#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900342
343// Tests that the Delete function works as expected, especially
344// the recursion flag. Also coincidentally tests PathExists.
345TEST_F(FileUtilTest, Delete) {
346 // Create a file
evanm@google.com874d1672008-10-31 08:54:04 +0900347 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900348 CreateTextFile(file_name, L"I'm cannon fodder.");
349
350 ASSERT_TRUE(file_util::PathExists(file_name));
351
evanm@google.com874d1672008-10-31 08:54:04 +0900352 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
353 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900354
355 ASSERT_TRUE(file_util::PathExists(subdir_path));
356
evanm@google.com874d1672008-10-31 08:54:04 +0900357 FilePath directory_contents = test_dir_;
erikkay@google.com014161d2008-08-16 02:45:13 +0900358#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900359 // TODO(erikkay): see if anyone's actually using this feature of the API
evanm@google.com874d1672008-10-31 08:54:04 +0900360 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900361 // Delete non-recursively and check that only the file is deleted
362 ASSERT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900363 EXPECT_FALSE(file_util::PathExists(file_name));
364 EXPECT_TRUE(file_util::PathExists(subdir_path));
365#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900366
367 // Delete recursively and make sure all contents are deleted
368 ASSERT_TRUE(file_util::Delete(directory_contents, true));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900369 EXPECT_FALSE(file_util::PathExists(file_name));
370 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900371}
372
373TEST_F(FileUtilTest, Move) {
374 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900375 FilePath dir_name_from =
376 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
377 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900378 ASSERT_TRUE(file_util::PathExists(dir_name_from));
379
380 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900381 FilePath file_name_from =
382 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900383 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
384 ASSERT_TRUE(file_util::PathExists(file_name_from));
385
386 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900387 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
388 FilePath file_name_to =
389 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900390
391 ASSERT_FALSE(file_util::PathExists(dir_name_to));
392
393 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
394
395 // Check everything has been moved.
396 EXPECT_FALSE(file_util::PathExists(dir_name_from));
397 EXPECT_FALSE(file_util::PathExists(file_name_from));
398 EXPECT_TRUE(file_util::PathExists(dir_name_to));
399 EXPECT_TRUE(file_util::PathExists(file_name_to));
400}
401
402TEST_F(FileUtilTest, CopyDirectoryRecursively) {
403 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900404 FilePath dir_name_from =
405 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
406 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900407 ASSERT_TRUE(file_util::PathExists(dir_name_from));
408
409 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900410 FilePath file_name_from =
411 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900412 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
413 ASSERT_TRUE(file_util::PathExists(file_name_from));
414
415 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900416 FilePath subdir_name_from =
417 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
418 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900419 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
420
421 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900422 FilePath file_name2_from =
423 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900424 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
425 ASSERT_TRUE(file_util::PathExists(file_name2_from));
426
427 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900428 FilePath dir_name_to =
429 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
430 FilePath file_name_to =
431 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
432 FilePath subdir_name_to =
433 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
434 FilePath file_name2_to =
435 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900436
437 ASSERT_FALSE(file_util::PathExists(dir_name_to));
438
439 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
440
441 // Check everything has been copied.
442 EXPECT_TRUE(file_util::PathExists(dir_name_from));
443 EXPECT_TRUE(file_util::PathExists(file_name_from));
444 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
445 EXPECT_TRUE(file_util::PathExists(file_name2_from));
446 EXPECT_TRUE(file_util::PathExists(dir_name_to));
447 EXPECT_TRUE(file_util::PathExists(file_name_to));
448 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
449 EXPECT_TRUE(file_util::PathExists(file_name2_to));
450}
451
452TEST_F(FileUtilTest, CopyDirectory) {
453 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900454 FilePath dir_name_from =
455 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
456 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900457 ASSERT_TRUE(file_util::PathExists(dir_name_from));
458
459 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900460 FilePath file_name_from =
461 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900462 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
463 ASSERT_TRUE(file_util::PathExists(file_name_from));
464
465 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900466 FilePath subdir_name_from =
467 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
468 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900469 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
470
471 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900472 FilePath file_name2_from =
473 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900474 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
475 ASSERT_TRUE(file_util::PathExists(file_name2_from));
476
477 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900478 FilePath dir_name_to =
479 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
480 FilePath file_name_to =
481 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
482 FilePath subdir_name_to =
483 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +0900484
485 ASSERT_FALSE(file_util::PathExists(dir_name_to));
486
487 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
488
489 // Check everything has been copied.
490 EXPECT_TRUE(file_util::PathExists(dir_name_from));
491 EXPECT_TRUE(file_util::PathExists(file_name_from));
492 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
493 EXPECT_TRUE(file_util::PathExists(file_name2_from));
494 EXPECT_TRUE(file_util::PathExists(dir_name_to));
495 EXPECT_TRUE(file_util::PathExists(file_name_to));
496 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
497}
498
499TEST_F(FileUtilTest, CopyFile) {
500 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900501 FilePath dir_name_from =
502 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
503 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900504 ASSERT_TRUE(file_util::PathExists(dir_name_from));
505
506 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900507 FilePath file_name_from =
508 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900509 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
510 CreateTextFile(file_name_from, file_contents);
511 ASSERT_TRUE(file_util::PathExists(file_name_from));
512
513 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900514 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900515 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900516
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900517 // Copy the file to another location using '..' in the path.
evanm@google.com874d1672008-10-31 08:54:04 +0900518 std::wstring dest_file2(dir_name_from.ToWStringHack());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900519 file_util::AppendToPath(&dest_file2, L"..");
520 file_util::AppendToPath(&dest_file2, L"DestFile.txt");
evanm@google.com874d1672008-10-31 08:54:04 +0900521 ASSERT_TRUE(file_util::CopyFile(file_name_from,
522 FilePath::FromWStringHack(dest_file2)));
523 std::wstring dest_file2_test(dir_name_from.ToWStringHack());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900524 file_util::UpOneDirectory(&dest_file2_test);
525 file_util::AppendToPath(&dest_file2_test, L"DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900526
527 // Check everything has been copied.
528 EXPECT_TRUE(file_util::PathExists(file_name_from));
529 EXPECT_TRUE(file_util::PathExists(dest_file));
530 const std::wstring read_contents = ReadTextFile(dest_file);
531 EXPECT_EQ(file_contents, read_contents);
evanm@google.com874d1672008-10-31 08:54:04 +0900532 EXPECT_TRUE(file_util::PathExists(
533 FilePath::FromWStringHack(dest_file2_test)));
534 EXPECT_TRUE(file_util::PathExists(FilePath::FromWStringHack(dest_file2)));
initial.commit3f4a7322008-07-27 06:49:38 +0900535}
536
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900537// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900538#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900539TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +0900540 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900541
542 SYSTEMTIME start_time;
543 GetLocalTime(&start_time);
544 Sleep(100);
545 CreateTextFile(file_name, L"New file!");
546 Sleep(100);
547 SYSTEMTIME end_time;
548 GetLocalTime(&end_time);
549
550 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +0900551 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +0900552
553 FILETIME start_filetime;
554 SystemTimeToFileTime(&start_time, &start_filetime);
555 FILETIME end_filetime;
556 SystemTimeToFileTime(&end_time, &end_filetime);
557 FILETIME file_creation_filetime;
558 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
559
560 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
561 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
562 "creation time: " << FileTimeAsUint64(file_creation_filetime);
563
564 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
565 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
566 "end time: " << FileTimeAsUint64(end_filetime);
567
evanm@google.com874d1672008-10-31 08:54:04 +0900568 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +0900569}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900570#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900571
erikkay@google.comf2406842008-08-21 00:59:49 +0900572// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +0900573// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +0900574typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +0900575
erikkay@google.comf2406842008-08-21 00:59:49 +0900576TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +0900577 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +0900578 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +0900579 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
580 .Append(FILE_PATH_LITERAL("data"))
581 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900582 ASSERT_TRUE(file_util::PathExists(data_dir));
583
evanm@google.com874d1672008-10-31 08:54:04 +0900584 FilePath original_file =
585 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
586 FilePath same_file =
587 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
588 FilePath same_length_file =
589 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
590 FilePath different_file =
591 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
592 FilePath different_first_file =
593 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
594 FilePath different_last_file =
595 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
596 FilePath empty1_file =
597 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
598 FilePath empty2_file =
599 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
600 FilePath shortened_file =
601 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
602 FilePath binary_file =
603 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
604 FilePath binary_file_same =
605 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
606 FilePath binary_file_diff =
607 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +0900608
609 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
610 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
611 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
612 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
613 EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname"));
614 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
615 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
616 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
617 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
618 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
619 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
620 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
621}
622
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900623// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +0900624#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900625TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900626 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900627 CreateTextFile(target_file, L"This is the target.");
628
evanm@google.com874d1672008-10-31 08:54:04 +0900629 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900630
631 HRESULT result;
632 IShellLink *shell = NULL;
633 IPersistFile *persist = NULL;
634
635 CoInitialize(NULL);
636 // Temporarily create a shortcut for test
637 result = CoCreateInstance(CLSID_ShellLink, NULL,
638 CLSCTX_INPROC_SERVER, IID_IShellLink,
639 reinterpret_cast<LPVOID*>(&shell));
640 EXPECT_TRUE(SUCCEEDED(result));
641 result = shell->QueryInterface(IID_IPersistFile,
642 reinterpret_cast<LPVOID*>(&persist));
643 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900644 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900645 EXPECT_TRUE(SUCCEEDED(result));
646 result = shell->SetDescription(L"ResolveShortcutTest");
647 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900648 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +0900649 EXPECT_TRUE(SUCCEEDED(result));
650 if (persist)
651 persist->Release();
652 if (shell)
653 shell->Release();
654
655 bool is_solved;
evanm@google.com874d1672008-10-31 08:54:04 +0900656 std::wstring link_file_str = link_file.value();
657 is_solved = file_util::ResolveShortcut(&link_file_str);
initial.commit3f4a7322008-07-27 06:49:38 +0900658 EXPECT_TRUE(is_solved);
659 std::wstring contents;
evanm@google.com874d1672008-10-31 08:54:04 +0900660 contents = ReadTextFile(FilePath(link_file_str));
initial.commit3f4a7322008-07-27 06:49:38 +0900661 EXPECT_EQ(L"This is the target.", contents);
662
ericroman@google.comdbff4f52008-08-19 01:00:38 +0900663 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +0900664 DeleteFile(target_file.value().c_str());
665 DeleteFile(link_file_str.c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900666 CoUninitialize();
667}
668
669TEST_F(FileUtilTest, CreateShortcutTest) {
670 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +0900671 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900672 CreateTextFile(target_file, file_contents);
673
evanm@google.com874d1672008-10-31 08:54:04 +0900674 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900675
676 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +0900677 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
678 link_file.value().c_str(),
initial.commit3f4a7322008-07-27 06:49:38 +0900679 NULL, NULL, NULL, NULL, 0));
evanm@google.com874d1672008-10-31 08:54:04 +0900680 std::wstring resolved_name = link_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900681 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evanm@google.com874d1672008-10-31 08:54:04 +0900682 std::wstring read_contents = ReadTextFile(FilePath(resolved_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900683 EXPECT_EQ(file_contents, read_contents);
684
evanm@google.com874d1672008-10-31 08:54:04 +0900685 DeleteFile(target_file.value().c_str());
686 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900687 CoUninitialize();
688}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +0900689
690TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
691 // Create a directory
692 FilePath dir_name_from =
693 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
694 file_util::CreateDirectory(dir_name_from);
695 ASSERT_TRUE(file_util::PathExists(dir_name_from));
696
697 // Create a file under the directory
698 FilePath file_name_from =
699 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
700 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
701 ASSERT_TRUE(file_util::PathExists(file_name_from));
702
703 // Move the directory by using CopyAndDeleteDirectory
704 FilePath dir_name_to = test_dir_.Append(
705 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
706 FilePath file_name_to =
707 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
708
709 ASSERT_FALSE(file_util::PathExists(dir_name_to));
710
711 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
712
713 // Check everything has been moved.
714 EXPECT_FALSE(file_util::PathExists(dir_name_from));
715 EXPECT_FALSE(file_util::PathExists(file_name_from));
716 EXPECT_TRUE(file_util::PathExists(dir_name_to));
717 EXPECT_TRUE(file_util::PathExists(file_name_to));
718}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900719#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900720
721TEST_F(FileUtilTest, CreateTemporaryFileNameTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900722 std::wstring temp_files[3];
723 for (int i = 0; i < 3; i++) {
724 ASSERT_TRUE(file_util::CreateTemporaryFileName(&(temp_files[i])));
725 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
726 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
727 }
728 for (int i = 0; i < 3; i++)
729 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
730 for (int i = 0; i < 3; i++)
731 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
732}
733
734TEST_F(FileUtilTest, CreateAndOpenTemporaryFileNameTest) {
735 FilePath names[3];
736 FILE *fps[3];
737 int i;
738
739 // Create; make sure they are open and exist.
740 for (i = 0; i < 3; ++i) {
741 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
742 ASSERT_TRUE(fps[i]);
743 EXPECT_TRUE(file_util::PathExists(names[i]));
744 }
745
746 // Make sure all names are unique.
747 for (i = 0; i < 3; ++i) {
748 EXPECT_FALSE(names[i] == names[(i+1)%3]);
749 }
750
751 // Close and delete.
752 for (i = 0; i < 3; ++i) {
753 EXPECT_TRUE(file_util::CloseFile(fps[i]));
754 EXPECT_TRUE(file_util::Delete(names[i], false));
755 }
initial.commit3f4a7322008-07-27 06:49:38 +0900756}
757
758TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
759 std::wstring temp_dir;
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900760 ASSERT_TRUE(file_util::CreateNewTempDirectory(std::wstring(), &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900761 EXPECT_TRUE(file_util::PathExists(temp_dir));
762 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +0900763}
764
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900765TEST_F(FileUtilTest, GetShmemTempDirTest) {
766 FilePath dir;
767 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
768 EXPECT_TRUE(file_util::DirectoryExists(dir));
769}
770
initial.commit3f4a7322008-07-27 06:49:38 +0900771TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900772 FilePath test_root =
773 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +0900774#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +0900775 FilePath test_path =
776 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900777#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +0900778 FilePath test_path =
779 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900780#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +0900781
782 EXPECT_FALSE(file_util::PathExists(test_path));
783 EXPECT_TRUE(file_util::CreateDirectory(test_path));
784 EXPECT_TRUE(file_util::PathExists(test_path));
785 // CreateDirectory returns true if the DirectoryExists returns true.
786 EXPECT_TRUE(file_util::CreateDirectory(test_path));
787
788 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +0900789 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900790 EXPECT_FALSE(file_util::PathExists(test_path));
791 CreateTextFile(test_path, L"test file");
792 EXPECT_TRUE(file_util::PathExists(test_path));
793 EXPECT_FALSE(file_util::CreateDirectory(test_path));
794
795 EXPECT_TRUE(file_util::Delete(test_root, true));
796 EXPECT_FALSE(file_util::PathExists(test_root));
797 EXPECT_FALSE(file_util::PathExists(test_path));
798}
799
800TEST_F(FileUtilTest, DetectDirectoryTest) {
801 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900802 FilePath test_root =
803 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900804 EXPECT_FALSE(file_util::PathExists(test_root));
805 EXPECT_TRUE(file_util::CreateDirectory(test_root));
806 EXPECT_TRUE(file_util::PathExists(test_root));
807 EXPECT_TRUE(file_util::DirectoryExists(test_root));
808
809 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +0900810 FilePath test_path =
811 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900812 EXPECT_FALSE(file_util::PathExists(test_path));
813 CreateTextFile(test_path, L"test file");
814 EXPECT_TRUE(file_util::PathExists(test_path));
815 EXPECT_FALSE(file_util::DirectoryExists(test_path));
816 EXPECT_TRUE(file_util::Delete(test_path, false));
817
818 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +0900819}
820
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900821static const struct goodbad_pair {
initial.commit3f4a7322008-07-27 06:49:38 +0900822 std::wstring bad_name;
823 std::wstring good_name;
824} kIllegalCharacterCases[] = {
825 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
826 {L"**********::::.txt", L"--------------.txt"},
initial.commit3f4a7322008-07-27 06:49:38 +0900827 // We can't use UCNs (universal character names) for C0/C1 characters and
828 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
829 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
erikkay@google.com014161d2008-08-16 02:45:13 +0900830#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900831 {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
initial.commit3f4a7322008-07-27 06:49:38 +0900832 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
833 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900834#elif defined(OS_POSIX)
835 {L"bad*file?name.jpg", L"bad-file-name.jpg"},
836 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"},
837 {L"bad\uFFFFfile-name.jpg ", L"bad-file-name.jpg"},
838#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900839 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
840 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
841 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
842 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
843 // Unassigned codepoints are ok.
844 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
845};
846
847TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900848 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900849 std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
850 file_util::ReplaceIllegalCharacters(&bad_name, L'-');
851 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
852 }
853}
854
855static const struct ReplaceExtensionCase {
856 std::wstring file_name;
estade@chromium.org63343202008-12-05 05:46:06 +0900857 FilePath::StringType extension;
initial.commit3f4a7322008-07-27 06:49:38 +0900858 std::wstring result;
859} kReplaceExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +0900860 {L"", FILE_PATH_LITERAL(""), L""},
861 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
862 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
863 {L".", FILE_PATH_LITERAL(""), L""},
864 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
865 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
866 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
867 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
868 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
869 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
870 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
871 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
872 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
873 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
874 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
875 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commit3f4a7322008-07-27 06:49:38 +0900876};
877
878TEST_F(FileUtilTest, ReplaceExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900879 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +0900880 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
881 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
882 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +0900883 }
884}
885
sky@google.com71e7c6f2008-09-20 02:32:18 +0900886// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
887// the directory names of the path.
888TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
estade@chromium.org63343202008-12-05 05:46:06 +0900889 FilePath path;
890 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
891 path = path.Append(FILE_PATH_LITERAL("foo"));
sky@google.com71e7c6f2008-09-20 02:32:18 +0900892 // '/foo.bar/foo' with extension '.baz'
estade@chromium.org63343202008-12-05 05:46:06 +0900893 FilePath result_path = path;
894 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900895 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
896 result_path.value());
sky@google.com71e7c6f2008-09-20 02:32:18 +0900897}
898
initial.commit3f4a7322008-07-27 06:49:38 +0900899TEST_F(FileUtilTest, FileEnumeratorTest) {
900 // Test an empty directory.
avi@google.com5cb79352008-12-11 23:55:12 +0900901 file_util::FileEnumerator f0(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900902 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +0900903 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
904 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +0900905
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900906 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +0900907 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900908 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +0900909 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900910 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +0900911 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900912 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +0900913
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900914 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +0900915 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900916 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900917 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900918 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900919 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900920 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900921 FilePath file2_rel =
922 dir2.Append(FilePath::kParentDirectory)
923 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900924 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900925 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900926
927 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +0900928 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900929 file_util::FileEnumerator::FILES);
930 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900931 EXPECT_TRUE(c1.HasFile(file1));
932 EXPECT_TRUE(c1.HasFile(file2_abs));
933 EXPECT_TRUE(c1.HasFile(dir2file));
934 EXPECT_TRUE(c1.HasFile(dir2innerfile));
935 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +0900936
937 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +0900938 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900939 file_util::FileEnumerator::DIRECTORIES);
940 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900941 EXPECT_TRUE(c2.HasFile(dir1));
942 EXPECT_TRUE(c2.HasFile(dir2));
943 EXPECT_TRUE(c2.HasFile(dir2inner));
944 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +0900945
tim@chromium.org989d0972008-10-16 11:42:45 +0900946 // Only enumerate directories non-recursively.
947 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +0900948 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +0900949 FindResultCollector c2_non_recursive(f2_non_recursive);
950 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
951 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
952 EXPECT_EQ(c2_non_recursive.size(), 2);
953
initial.commit3f4a7322008-07-27 06:49:38 +0900954 // Enumerate files and directories.
avi@google.com5cb79352008-12-11 23:55:12 +0900955 file_util::FileEnumerator f3(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900956 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
957 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900958 EXPECT_TRUE(c3.HasFile(dir1));
959 EXPECT_TRUE(c3.HasFile(dir2));
960 EXPECT_TRUE(c3.HasFile(file1));
961 EXPECT_TRUE(c3.HasFile(file2_abs));
962 EXPECT_TRUE(c3.HasFile(dir2file));
963 EXPECT_TRUE(c3.HasFile(dir2inner));
964 EXPECT_TRUE(c3.HasFile(dir2innerfile));
965 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +0900966
967 // Non-recursive operation.
avi@google.com5cb79352008-12-11 23:55:12 +0900968 file_util::FileEnumerator f4(test_dir_, false,
initial.commit3f4a7322008-07-27 06:49:38 +0900969 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
970 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900971 EXPECT_TRUE(c4.HasFile(dir2));
972 EXPECT_TRUE(c4.HasFile(dir2));
973 EXPECT_TRUE(c4.HasFile(file1));
974 EXPECT_TRUE(c4.HasFile(file2_abs));
975 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +0900976
977 // Enumerate with a pattern.
avi@google.com5cb79352008-12-11 23:55:12 +0900978 file_util::FileEnumerator f5(test_dir_, true,
979 file_util::FileEnumerator::FILES_AND_DIRECTORIES,
980 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900981 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900982 EXPECT_TRUE(c5.HasFile(dir1));
983 EXPECT_TRUE(c5.HasFile(dir2));
984 EXPECT_TRUE(c5.HasFile(dir2file));
985 EXPECT_TRUE(c5.HasFile(dir2inner));
986 EXPECT_TRUE(c5.HasFile(dir2innerfile));
987 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +0900988
989 // Make sure the destructor closes the find handle while in the middle of a
990 // query to allow TearDown to delete the directory.
avi@google.com5cb79352008-12-11 23:55:12 +0900991 file_util::FileEnumerator f6(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900992 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +0900993 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
994 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +0900995}
license.botf003cfe2008-08-24 09:55:55 +0900996
estade@chromium.org97e37822008-11-27 13:03:57 +0900997
998void PathComponents(const std::wstring& path,
999 std::vector<std::wstring>* components) {
1000 DCHECK(components != NULL);
1001 if (components == NULL)
1002 return;
1003 std::wstring::size_type start = 0;
1004 std::wstring::size_type end = path.find('/', start);
1005
1006 // Special case the "/" or "\" directory. On Windows with a drive letter,
1007 // this code path won't hit, but the right thing should still happen.
1008 // "E:\foo" will turn into "E:","foo".
1009 if (end == start) {
1010 components->push_back(std::wstring(path, 0, 1));
1011 start = end + 1;
1012 end = path.find('/', start);
1013 }
1014 while (end != std::wstring::npos) {
1015 std::wstring component = std::wstring(path, start, end - start);
1016 components->push_back(component);
1017 start = end + 1;
1018 end = path.find('/', start);
1019 }
1020 std::wstring component = std::wstring(path, start);
1021 components->push_back(component);
1022}
1023
1024static const struct PathComponentsCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001025 const FilePath::CharType* path;
1026 const FilePath::CharType* result;
estade@chromium.org97e37822008-11-27 13:03:57 +09001027} kPathComponents[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001028 {FILE_PATH_LITERAL("/foo/bar/baz/"), FILE_PATH_LITERAL("/|foo|bar|baz|")},
1029 {FILE_PATH_LITERAL("/foo/bar/baz"), FILE_PATH_LITERAL("/|foo|bar|baz")},
1030 {FILE_PATH_LITERAL("e:/foo"), FILE_PATH_LITERAL("e:|foo")},
estade@chromium.org97e37822008-11-27 13:03:57 +09001031};
1032
1033TEST_F(FileUtilTest, PathComponentsTest) {
1034 for (size_t i = 0; i < arraysize(kPathComponents); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001035 FilePath path(kPathComponents[i].path);
estade@chromium.org97e37822008-11-27 13:03:57 +09001036 std::vector<FilePath::StringType> comps;
1037 file_util::PathComponents(path, &comps);
1038
1039 FilePath::StringType result;
1040 for (size_t j = 0; j < comps.size(); ++j) {
1041 result.append(comps[j]);
1042 if (j < comps.size() - 1)
1043 result.append(FILE_PATH_LITERAL("|"), 1);
1044 }
1045 EXPECT_EQ(kPathComponents[i].result, result);
1046 }
1047}
1048
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001049TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001050 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001051
1052 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001053 if (file_util::PathExists(data_dir)) {
1054 ASSERT_TRUE(file_util::Delete(data_dir, true));
1055 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001056 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1057
1058 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1059 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1060 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1061 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1062
1063 // Annoyingly, the directories must actually exist in order for realpath(),
1064 // which Contains() relies on in posix, to work.
1065 ASSERT_TRUE(file_util::CreateDirectory(foo));
1066 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001067 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1068 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1069 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001070
1071 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1072 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1073 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1074 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1075
1076// Platform-specific concerns
1077 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1078#if defined(OS_WIN)
1079 EXPECT_TRUE(file_util::ContainsPath(foo,
1080 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001081 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001082 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
1083#elif defined(OS_LINUX)
1084 EXPECT_FALSE(file_util::ContainsPath(foo,
1085 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
1086#else
1087 // We can't really do this test on osx since the case-sensitivity of the
1088 // filesystem is configurable.
1089#endif
1090}
1091
mark@chromium.org17684802008-09-10 09:16:28 +09001092} // namespace