blob: a00fc0bf4d486ddeba5cb6a037953324870cf677 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
erikkay@google.comc8ec9e92008-08-16 02:50:10 +09005#include "build/build_config.h"
6
erikkay@google.com014161d2008-08-16 02:45:13 +09007#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <windows.h>
initial.commit3f4a7322008-07-27 06:49:38 +09009#include <shellapi.h>
10#include <shlobj.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090011#endif
initial.commit3f4a7322008-07-27 06:49:38 +090012
13#include <fstream>
14#include <iostream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090015#include <set>
initial.commit3f4a7322008-07-27 06:49:38 +090016
17#include "base/base_paths.h"
evanm@google.com874d1672008-10-31 08:54:04 +090018#include "base/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090019#include "base/file_util.h"
20#include "base/logging.h"
21#include "base/path_service.h"
22#include "base/string_util.h"
23#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090024#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090025
26namespace {
27
erikkay@google.comf2406842008-08-21 00:59:49 +090028// file_util winds up using autoreleased objects on the Mac, so this needs
29// to be a PlatformTest
30class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +090031 protected:
32 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +090033 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +090034 // Name a subdirectory of the temp directory.
35 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +090036 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +090037
38 // Create a fresh, empty copy of this directory.
39 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +090040 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +090041 }
42 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +090043 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +090044 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +090045 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +090046 ASSERT_FALSE(file_util::PathExists(test_dir_));
47 }
48
49 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +090050 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +090051};
52
53// Collects all the results from the given file enumerator, and provides an
54// interface to query whether a given file is present.
55class FindResultCollector {
56 public:
57 FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +090058 FilePath cur_file;
59 while (!(cur_file = enumerator.Next()).value().empty()) {
60 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +090061 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +090062 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +090063 << "Same file returned twice";
64
65 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +090066 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +090067 }
68 }
69
70 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +090071 bool HasFile(const FilePath& file) const {
72 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +090073 }
evanm@google.com874d1672008-10-31 08:54:04 +090074
erikkay@google.comdfb51b22008-08-16 02:32:10 +090075 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +090076 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +090077 }
initial.commit3f4a7322008-07-27 06:49:38 +090078
79 private:
evanm@google.com874d1672008-10-31 08:54:04 +090080 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +090081};
82
83// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +090084void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +090085 const std::wstring& contents) {
86 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +090087 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +090088 ASSERT_TRUE(file.is_open());
89 file << contents;
90 file.close();
91}
92
93// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +090094std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +090095 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +090096 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +090097 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +090098 EXPECT_TRUE(file.is_open());
99 file.getline(contents, 64);
100 file.close();
101 return std::wstring(contents);
102}
103
erikkay@google.com014161d2008-08-16 02:45:13 +0900104#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900105uint64 FileTimeAsUint64(const FILETIME& ft) {
106 ULARGE_INTEGER u;
107 u.LowPart = ft.dwLowDateTime;
108 u.HighPart = ft.dwHighDateTime;
109 return u.QuadPart;
110}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900111#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900112
113const struct append_case {
114 const wchar_t* path;
115 const wchar_t* ending;
116 const wchar_t* result;
117} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900118#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900119 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
120 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
121 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
122 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
123 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
124 {L"", L"path", L"\\path"},
125 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900126#elif defined(OS_POSIX)
127 {L"/foo/bar", L"path", L"/foo/bar/path"},
128 {L"/foo/bar/", L"path", L"/foo/bar/path"},
129 {L"/foo/bar//", L"path", L"/foo/bar//path"},
130 {L"/foo/bar/", L"", L"/foo/bar/"},
131 {L"/foo/bar", L"", L"/foo/bar/"},
132 {L"", L"path", L"/path"},
133 {L"", L"", L"/"},
134#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900135};
136
initial.commit3f4a7322008-07-27 06:49:38 +0900137TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900138 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900139 const append_case& value = append_cases[i];
140 std::wstring result = value.path;
141 file_util::AppendToPath(&result, value.ending);
142 EXPECT_EQ(value.result, result);
143 }
144
145#ifdef NDEBUG
146 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
147#endif
148}
149
150static const struct InsertBeforeExtensionCase {
151 std::wstring path;
estade@chromium.org63343202008-12-05 05:46:06 +0900152 FilePath::StringType suffix;
initial.commit3f4a7322008-07-27 06:49:38 +0900153 std::wstring result;
154} kInsertBeforeExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +0900155 {L"", FILE_PATH_LITERAL(""), L""},
156 {L"", FILE_PATH_LITERAL("txt"), L"txt"},
157 {L".", FILE_PATH_LITERAL("txt"), L"txt."},
158 {L".", FILE_PATH_LITERAL(""), L"."},
159 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"footxt.dll"},
160 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt.dll"},
161 {L"foo", FILE_PATH_LITERAL("txt"), L"footxt"},
162 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
163 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baztxt.dll"},
164 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt.dll"},
165 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo.dll"},
166 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo..dll"},
167 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
168 {L"foo", FILE_PATH_LITERAL("."), L"foo."},
169 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz.dll"},
170 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz..dll"},
erikkay@google.com014161d2008-08-16 02:45:13 +0900171#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900172 {L"\\", L"", L"\\"},
173 {L"\\", L"txt", L"\\txt"},
174 {L"\\.", L"txt", L"\\txt."},
175 {L"\\.", L"", L"\\."},
176 {L"C:\\bar\\foo.dll", L"txt", L"C:\\bar\\footxt.dll"},
177 {L"C:\\bar.baz\\foodll", L"txt", L"C:\\bar.baz\\foodlltxt"},
178 {L"C:\\bar.baz\\foo.dll", L"txt", L"C:\\bar.baz\\footxt.dll"},
179 {L"C:\\bar.baz\\foo.dll.exe", L"txt", L"C:\\bar.baz\\foo.dlltxt.exe"},
180 {L"C:\\bar.baz\\foo", L"", L"C:\\bar.baz\\foo"},
181 {L"C:\\bar.baz\\foo.exe", L"", L"C:\\bar.baz\\foo.exe"},
182 {L"C:\\bar.baz\\foo.dll.exe", L"", L"C:\\bar.baz\\foo.dll.exe"},
183 {L"C:\\bar\\baz\\foo.exe", L" (1)", L"C:\\bar\\baz\\foo (1).exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900184#elif defined(OS_POSIX)
estade@chromium.org63343202008-12-05 05:46:06 +0900185 {L"/", "", L"/"},
186 {L"/", "txt", L"/txt"},
187 {L"/.", "txt", L"/txt."},
188 {L"/.", "", L"/."},
189 {L"/bar/foo.dll", "txt", L"/bar/footxt.dll"},
190 {L"/bar.baz/foodll", "txt", L"/bar.baz/foodlltxt"},
191 {L"/bar.baz/foo.dll", "txt", L"/bar.baz/footxt.dll"},
192 {L"/bar.baz/foo.dll.exe", "txt", L"/bar.baz/foo.dlltxt.exe"},
193 {L"/bar.baz/foo", "", L"/bar.baz/foo"},
194 {L"/bar.baz/foo.exe", "", L"/bar.baz/foo.exe"},
195 {L"/bar.baz/foo.dll.exe", "", L"/bar.baz/foo.dll.exe"},
196 {L"/bar/baz/foo.exe", " (1)", L"/bar/baz/foo (1).exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900197#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900198};
199
200TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900201 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +0900202 FilePath path = FilePath::FromWStringHack(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900203 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
estade@chromium.org63343202008-12-05 05:46:06 +0900204 EXPECT_EQ(kInsertBeforeExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +0900205 }
206}
207
208static const struct filename_case {
209 const wchar_t* path;
210 const wchar_t* filename;
211} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900212#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900213 {L"c:\\colon\\backslash", L"backslash"},
214 {L"c:\\colon\\backslash\\", L""},
215 {L"\\\\filename.exe", L"filename.exe"},
216 {L"filename.exe", L"filename.exe"},
217 {L"", L""},
218 {L"\\\\\\", L""},
219 {L"c:/colon/backslash", L"backslash"},
220 {L"c:/colon/backslash/", L""},
221 {L"//////", L""},
222 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900223#elif defined(OS_POSIX)
224 {L"/foo/bar", L"bar"},
225 {L"/foo/bar/", L""},
226 {L"/filename.exe", L"filename.exe"},
227 {L"filename.exe", L"filename.exe"},
228 {L"", L""},
229 {L"/", L""},
230#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900231};
232
233TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900234 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900235 const filename_case& value = filename_cases[i];
236 std::wstring result = file_util::GetFilenameFromPath(value.path);
237 EXPECT_EQ(value.filename, result);
238 }
239}
240
241// Test finding the file type from a path name
242static const struct extension_case {
243 const wchar_t* path;
244 const wchar_t* extension;
245} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900246#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900247 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
248 {L"C:\\colon\\backslash\\filename.", L""},
249 {L"C:\\colon\\backslash\\filename", L""},
250 {L"C:\\colon\\backslash\\", L""},
251 {L"C:\\colon\\backslash.\\", L""},
252 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900253#elif defined(OS_POSIX)
254 {L"/foo/bar/filename.extension", L"extension"},
255 {L"/foo/bar/filename.", L""},
256 {L"/foo/bar/filename", L""},
257 {L"/foo/bar/", L""},
258 {L"/foo/bar./", L""},
259 {L"/foo/bar/filename.extension.extension2", L"extension2"},
260 {L".", L""},
261 {L"..", L""},
262 {L"./foo", L""},
263 {L"./foo.extension", L"extension"},
264 {L"/foo.extension1/bar.extension2", L"extension2"},
265#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900266};
267
268TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900269 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900270 const extension_case& ext = extension_cases[i];
271 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
272 EXPECT_EQ(ext.extension, fext);
273 }
274}
275
276// Test finding the directory component of a path
277static const struct dir_case {
278 const wchar_t* full_path;
279 const wchar_t* directory;
280} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900281#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900282 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
283 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
284 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
285 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
286 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
287 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
288 {L"C:\\", L"C:"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900289#elif defined(OS_POSIX)
290 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
291 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
292 {L"/foo/bar/", L"/foo/bar"},
293 {L"/foo/bar//", L"/foo/bar"},
294 {L"/foo/bar", L"/foo"},
295 {L"/foo/bar./", L"/foo/bar."},
296 {L"/", L"/"},
297 {L".", L"."},
298 {L"..", L"."}, // yes, ".." technically lives in "."
299#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900300};
301
302TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900303 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900304 const dir_case& dir = dir_cases[i];
305 const std::wstring parent =
306 file_util::GetDirectoryFromPath(dir.full_path);
307 EXPECT_EQ(dir.directory, parent);
308 }
309}
310
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900311// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900312#if defined OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +0900313TEST_F(FileUtilTest, CountFilesCreatedAfter) {
314 // Create old file (that we don't want to count)
evanm@google.com874d1672008-10-31 08:54:04 +0900315 FilePath old_file_name = test_dir_.Append(L"Old File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900316 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
317
318 // Age to perfection
319 Sleep(100);
320
321 // Establish our cutoff time
322 FILETIME test_start_time;
323 GetSystemTimeAsFileTime(&test_start_time);
evanm@google.com874d1672008-10-31 08:54:04 +0900324 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(),
325 test_start_time));
initial.commit3f4a7322008-07-27 06:49:38 +0900326
327 // Create a new file (that we do want to count)
evanm@google.com874d1672008-10-31 08:54:04 +0900328 FilePath new_file_name = test_dir_.Append(L"New File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900329 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
330
331 // We should see only the new file.
evanm@google.com874d1672008-10-31 08:54:04 +0900332 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_.value(),
333 test_start_time));
initial.commit3f4a7322008-07-27 06:49:38 +0900334
335 // Delete new file, we should see no files after cutoff now
336 EXPECT_TRUE(file_util::Delete(new_file_name, false));
evanm@google.com874d1672008-10-31 08:54:04 +0900337 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(),
338 test_start_time));
initial.commit3f4a7322008-07-27 06:49:38 +0900339}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900340#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900341
342// Tests that the Delete function works as expected, especially
343// the recursion flag. Also coincidentally tests PathExists.
344TEST_F(FileUtilTest, Delete) {
345 // Create a file
evanm@google.com874d1672008-10-31 08:54:04 +0900346 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900347 CreateTextFile(file_name, L"I'm cannon fodder.");
348
349 ASSERT_TRUE(file_util::PathExists(file_name));
350
evanm@google.com874d1672008-10-31 08:54:04 +0900351 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
352 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900353
354 ASSERT_TRUE(file_util::PathExists(subdir_path));
355
evanm@google.com874d1672008-10-31 08:54:04 +0900356 FilePath directory_contents = test_dir_;
erikkay@google.com014161d2008-08-16 02:45:13 +0900357#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900358 // TODO(erikkay): see if anyone's actually using this feature of the API
evanm@google.com874d1672008-10-31 08:54:04 +0900359 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900360 // Delete non-recursively and check that only the file is deleted
361 ASSERT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900362 EXPECT_FALSE(file_util::PathExists(file_name));
363 EXPECT_TRUE(file_util::PathExists(subdir_path));
364#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900365
366 // Delete recursively and make sure all contents are deleted
367 ASSERT_TRUE(file_util::Delete(directory_contents, true));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900368 EXPECT_FALSE(file_util::PathExists(file_name));
369 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900370}
371
372TEST_F(FileUtilTest, Move) {
373 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900374 FilePath dir_name_from =
375 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
376 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900377 ASSERT_TRUE(file_util::PathExists(dir_name_from));
378
379 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900380 FilePath file_name_from =
381 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900382 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
383 ASSERT_TRUE(file_util::PathExists(file_name_from));
384
385 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900386 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
387 FilePath file_name_to =
388 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900389
390 ASSERT_FALSE(file_util::PathExists(dir_name_to));
391
392 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
393
394 // Check everything has been moved.
395 EXPECT_FALSE(file_util::PathExists(dir_name_from));
396 EXPECT_FALSE(file_util::PathExists(file_name_from));
397 EXPECT_TRUE(file_util::PathExists(dir_name_to));
398 EXPECT_TRUE(file_util::PathExists(file_name_to));
399}
400
401TEST_F(FileUtilTest, CopyDirectoryRecursively) {
402 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900403 FilePath dir_name_from =
404 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
405 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900406 ASSERT_TRUE(file_util::PathExists(dir_name_from));
407
408 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900409 FilePath file_name_from =
410 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900411 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
412 ASSERT_TRUE(file_util::PathExists(file_name_from));
413
414 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900415 FilePath subdir_name_from =
416 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
417 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900418 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
419
420 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900421 FilePath file_name2_from =
422 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900423 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
424 ASSERT_TRUE(file_util::PathExists(file_name2_from));
425
426 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900427 FilePath dir_name_to =
428 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
429 FilePath file_name_to =
430 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
431 FilePath subdir_name_to =
432 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
433 FilePath file_name2_to =
434 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900435
436 ASSERT_FALSE(file_util::PathExists(dir_name_to));
437
438 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
439
440 // Check everything has been copied.
441 EXPECT_TRUE(file_util::PathExists(dir_name_from));
442 EXPECT_TRUE(file_util::PathExists(file_name_from));
443 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
444 EXPECT_TRUE(file_util::PathExists(file_name2_from));
445 EXPECT_TRUE(file_util::PathExists(dir_name_to));
446 EXPECT_TRUE(file_util::PathExists(file_name_to));
447 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
448 EXPECT_TRUE(file_util::PathExists(file_name2_to));
449}
450
451TEST_F(FileUtilTest, CopyDirectory) {
452 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900453 FilePath dir_name_from =
454 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
455 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900456 ASSERT_TRUE(file_util::PathExists(dir_name_from));
457
458 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900459 FilePath file_name_from =
460 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900461 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
462 ASSERT_TRUE(file_util::PathExists(file_name_from));
463
464 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900465 FilePath subdir_name_from =
466 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
467 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900468 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
469
470 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900471 FilePath file_name2_from =
472 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900473 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
474 ASSERT_TRUE(file_util::PathExists(file_name2_from));
475
476 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900477 FilePath dir_name_to =
478 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
479 FilePath file_name_to =
480 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
481 FilePath subdir_name_to =
482 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +0900483
484 ASSERT_FALSE(file_util::PathExists(dir_name_to));
485
486 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
487
488 // Check everything has been copied.
489 EXPECT_TRUE(file_util::PathExists(dir_name_from));
490 EXPECT_TRUE(file_util::PathExists(file_name_from));
491 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
492 EXPECT_TRUE(file_util::PathExists(file_name2_from));
493 EXPECT_TRUE(file_util::PathExists(dir_name_to));
494 EXPECT_TRUE(file_util::PathExists(file_name_to));
495 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
496}
497
498TEST_F(FileUtilTest, CopyFile) {
499 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900500 FilePath dir_name_from =
501 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
502 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900503 ASSERT_TRUE(file_util::PathExists(dir_name_from));
504
505 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900506 FilePath file_name_from =
507 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900508 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
509 CreateTextFile(file_name_from, file_contents);
510 ASSERT_TRUE(file_util::PathExists(file_name_from));
511
512 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900513 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900514 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900515
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900516 // Copy the file to another location using '..' in the path.
evanm@google.com874d1672008-10-31 08:54:04 +0900517 std::wstring dest_file2(dir_name_from.ToWStringHack());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900518 file_util::AppendToPath(&dest_file2, L"..");
519 file_util::AppendToPath(&dest_file2, L"DestFile.txt");
evanm@google.com874d1672008-10-31 08:54:04 +0900520 ASSERT_TRUE(file_util::CopyFile(file_name_from,
521 FilePath::FromWStringHack(dest_file2)));
522 std::wstring dest_file2_test(dir_name_from.ToWStringHack());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900523 file_util::UpOneDirectory(&dest_file2_test);
524 file_util::AppendToPath(&dest_file2_test, L"DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900525
526 // Check everything has been copied.
527 EXPECT_TRUE(file_util::PathExists(file_name_from));
528 EXPECT_TRUE(file_util::PathExists(dest_file));
529 const std::wstring read_contents = ReadTextFile(dest_file);
530 EXPECT_EQ(file_contents, read_contents);
evanm@google.com874d1672008-10-31 08:54:04 +0900531 EXPECT_TRUE(file_util::PathExists(
532 FilePath::FromWStringHack(dest_file2_test)));
533 EXPECT_TRUE(file_util::PathExists(FilePath::FromWStringHack(dest_file2)));
initial.commit3f4a7322008-07-27 06:49:38 +0900534}
535
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900536// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900537#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900538TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +0900539 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900540
541 SYSTEMTIME start_time;
542 GetLocalTime(&start_time);
543 Sleep(100);
544 CreateTextFile(file_name, L"New file!");
545 Sleep(100);
546 SYSTEMTIME end_time;
547 GetLocalTime(&end_time);
548
549 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +0900550 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +0900551
552 FILETIME start_filetime;
553 SystemTimeToFileTime(&start_time, &start_filetime);
554 FILETIME end_filetime;
555 SystemTimeToFileTime(&end_time, &end_filetime);
556 FILETIME file_creation_filetime;
557 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
558
559 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
560 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
561 "creation time: " << FileTimeAsUint64(file_creation_filetime);
562
563 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
564 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
565 "end time: " << FileTimeAsUint64(end_filetime);
566
evanm@google.com874d1672008-10-31 08:54:04 +0900567 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +0900568}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900569#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900570
erikkay@google.comf2406842008-08-21 00:59:49 +0900571// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +0900572// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +0900573typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +0900574
erikkay@google.comf2406842008-08-21 00:59:49 +0900575TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +0900576 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +0900577 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +0900578 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
579 .Append(FILE_PATH_LITERAL("data"))
580 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900581 ASSERT_TRUE(file_util::PathExists(data_dir));
582
evanm@google.com874d1672008-10-31 08:54:04 +0900583 FilePath original_file =
584 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
585 FilePath same_file =
586 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
587 FilePath same_length_file =
588 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
589 FilePath different_file =
590 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
591 FilePath different_first_file =
592 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
593 FilePath different_last_file =
594 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
595 FilePath empty1_file =
596 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
597 FilePath empty2_file =
598 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
599 FilePath shortened_file =
600 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
601 FilePath binary_file =
602 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
603 FilePath binary_file_same =
604 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
605 FilePath binary_file_diff =
606 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +0900607
608 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
609 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
610 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
611 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
612 EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname"));
613 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
614 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
615 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
616 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
617 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
618 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
619 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
620}
621
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900622// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +0900623#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900624TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900625 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900626 CreateTextFile(target_file, L"This is the target.");
627
evanm@google.com874d1672008-10-31 08:54:04 +0900628 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900629
630 HRESULT result;
631 IShellLink *shell = NULL;
632 IPersistFile *persist = NULL;
633
634 CoInitialize(NULL);
635 // Temporarily create a shortcut for test
636 result = CoCreateInstance(CLSID_ShellLink, NULL,
637 CLSCTX_INPROC_SERVER, IID_IShellLink,
638 reinterpret_cast<LPVOID*>(&shell));
639 EXPECT_TRUE(SUCCEEDED(result));
640 result = shell->QueryInterface(IID_IPersistFile,
641 reinterpret_cast<LPVOID*>(&persist));
642 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900643 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900644 EXPECT_TRUE(SUCCEEDED(result));
645 result = shell->SetDescription(L"ResolveShortcutTest");
646 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900647 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +0900648 EXPECT_TRUE(SUCCEEDED(result));
649 if (persist)
650 persist->Release();
651 if (shell)
652 shell->Release();
653
654 bool is_solved;
evanm@google.com874d1672008-10-31 08:54:04 +0900655 std::wstring link_file_str = link_file.value();
656 is_solved = file_util::ResolveShortcut(&link_file_str);
initial.commit3f4a7322008-07-27 06:49:38 +0900657 EXPECT_TRUE(is_solved);
658 std::wstring contents;
evanm@google.com874d1672008-10-31 08:54:04 +0900659 contents = ReadTextFile(FilePath(link_file_str));
initial.commit3f4a7322008-07-27 06:49:38 +0900660 EXPECT_EQ(L"This is the target.", contents);
661
ericroman@google.comdbff4f52008-08-19 01:00:38 +0900662 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +0900663 DeleteFile(target_file.value().c_str());
664 DeleteFile(link_file_str.c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900665 CoUninitialize();
666}
667
668TEST_F(FileUtilTest, CreateShortcutTest) {
669 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +0900670 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900671 CreateTextFile(target_file, file_contents);
672
evanm@google.com874d1672008-10-31 08:54:04 +0900673 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900674
675 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +0900676 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
677 link_file.value().c_str(),
initial.commit3f4a7322008-07-27 06:49:38 +0900678 NULL, NULL, NULL, NULL, 0));
evanm@google.com874d1672008-10-31 08:54:04 +0900679 std::wstring resolved_name = link_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900680 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evanm@google.com874d1672008-10-31 08:54:04 +0900681 std::wstring read_contents = ReadTextFile(FilePath(resolved_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900682 EXPECT_EQ(file_contents, read_contents);
683
evanm@google.com874d1672008-10-31 08:54:04 +0900684 DeleteFile(target_file.value().c_str());
685 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900686 CoUninitialize();
687}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900688#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900689
690TEST_F(FileUtilTest, CreateTemporaryFileNameTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900691 std::wstring temp_files[3];
692 for (int i = 0; i < 3; i++) {
693 ASSERT_TRUE(file_util::CreateTemporaryFileName(&(temp_files[i])));
694 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
695 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
696 }
697 for (int i = 0; i < 3; i++)
698 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
699 for (int i = 0; i < 3; i++)
700 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
701}
702
703TEST_F(FileUtilTest, CreateAndOpenTemporaryFileNameTest) {
704 FilePath names[3];
705 FILE *fps[3];
706 int i;
707
708 // Create; make sure they are open and exist.
709 for (i = 0; i < 3; ++i) {
710 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
711 ASSERT_TRUE(fps[i]);
712 EXPECT_TRUE(file_util::PathExists(names[i]));
713 }
714
715 // Make sure all names are unique.
716 for (i = 0; i < 3; ++i) {
717 EXPECT_FALSE(names[i] == names[(i+1)%3]);
718 }
719
720 // Close and delete.
721 for (i = 0; i < 3; ++i) {
722 EXPECT_TRUE(file_util::CloseFile(fps[i]));
723 EXPECT_TRUE(file_util::Delete(names[i], false));
724 }
initial.commit3f4a7322008-07-27 06:49:38 +0900725}
726
727TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
728 std::wstring temp_dir;
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900729 ASSERT_TRUE(file_util::CreateNewTempDirectory(std::wstring(), &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900730 EXPECT_TRUE(file_util::PathExists(temp_dir));
731 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +0900732}
733
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900734TEST_F(FileUtilTest, GetShmemTempDirTest) {
735 FilePath dir;
736 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
737 EXPECT_TRUE(file_util::DirectoryExists(dir));
738}
739
initial.commit3f4a7322008-07-27 06:49:38 +0900740TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900741 FilePath test_root =
742 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +0900743#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +0900744 FilePath test_path =
745 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900746#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +0900747 FilePath test_path =
748 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900749#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +0900750
751 EXPECT_FALSE(file_util::PathExists(test_path));
752 EXPECT_TRUE(file_util::CreateDirectory(test_path));
753 EXPECT_TRUE(file_util::PathExists(test_path));
754 // CreateDirectory returns true if the DirectoryExists returns true.
755 EXPECT_TRUE(file_util::CreateDirectory(test_path));
756
757 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +0900758 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900759 EXPECT_FALSE(file_util::PathExists(test_path));
760 CreateTextFile(test_path, L"test file");
761 EXPECT_TRUE(file_util::PathExists(test_path));
762 EXPECT_FALSE(file_util::CreateDirectory(test_path));
763
764 EXPECT_TRUE(file_util::Delete(test_root, true));
765 EXPECT_FALSE(file_util::PathExists(test_root));
766 EXPECT_FALSE(file_util::PathExists(test_path));
767}
768
769TEST_F(FileUtilTest, DetectDirectoryTest) {
770 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900771 FilePath test_root =
772 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900773 EXPECT_FALSE(file_util::PathExists(test_root));
774 EXPECT_TRUE(file_util::CreateDirectory(test_root));
775 EXPECT_TRUE(file_util::PathExists(test_root));
776 EXPECT_TRUE(file_util::DirectoryExists(test_root));
777
778 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +0900779 FilePath test_path =
780 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900781 EXPECT_FALSE(file_util::PathExists(test_path));
782 CreateTextFile(test_path, L"test file");
783 EXPECT_TRUE(file_util::PathExists(test_path));
784 EXPECT_FALSE(file_util::DirectoryExists(test_path));
785 EXPECT_TRUE(file_util::Delete(test_path, false));
786
787 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +0900788}
789
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900790static const struct goodbad_pair {
initial.commit3f4a7322008-07-27 06:49:38 +0900791 std::wstring bad_name;
792 std::wstring good_name;
793} kIllegalCharacterCases[] = {
794 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
795 {L"**********::::.txt", L"--------------.txt"},
initial.commit3f4a7322008-07-27 06:49:38 +0900796 // We can't use UCNs (universal character names) for C0/C1 characters and
797 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
798 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
erikkay@google.com014161d2008-08-16 02:45:13 +0900799#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900800 {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
initial.commit3f4a7322008-07-27 06:49:38 +0900801 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
802 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900803#elif defined(OS_POSIX)
804 {L"bad*file?name.jpg", L"bad-file-name.jpg"},
805 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"},
806 {L"bad\uFFFFfile-name.jpg ", L"bad-file-name.jpg"},
807#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900808 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
809 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
810 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
811 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
812 // Unassigned codepoints are ok.
813 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
814};
815
816TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900817 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900818 std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
819 file_util::ReplaceIllegalCharacters(&bad_name, L'-');
820 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
821 }
822}
823
824static const struct ReplaceExtensionCase {
825 std::wstring file_name;
estade@chromium.org63343202008-12-05 05:46:06 +0900826 FilePath::StringType extension;
initial.commit3f4a7322008-07-27 06:49:38 +0900827 std::wstring result;
828} kReplaceExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +0900829 {L"", FILE_PATH_LITERAL(""), L""},
830 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
831 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
832 {L".", FILE_PATH_LITERAL(""), L""},
833 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
834 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
835 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
836 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
837 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
838 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
839 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
840 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
841 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
842 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
843 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
844 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commit3f4a7322008-07-27 06:49:38 +0900845};
846
847TEST_F(FileUtilTest, ReplaceExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900848 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +0900849 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
850 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
851 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +0900852 }
853}
854
sky@google.com71e7c6f2008-09-20 02:32:18 +0900855// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
856// the directory names of the path.
857TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
estade@chromium.org63343202008-12-05 05:46:06 +0900858 FilePath path;
859 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
860 path = path.Append(FILE_PATH_LITERAL("foo"));
sky@google.com71e7c6f2008-09-20 02:32:18 +0900861 // '/foo.bar/foo' with extension '.baz'
estade@chromium.org63343202008-12-05 05:46:06 +0900862 FilePath result_path = path;
863 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
864 EXPECT_EQ(path.ToWStringHack() + L".baz", result_path.ToWStringHack());
sky@google.com71e7c6f2008-09-20 02:32:18 +0900865}
866
initial.commit3f4a7322008-07-27 06:49:38 +0900867TEST_F(FileUtilTest, FileEnumeratorTest) {
868 // Test an empty directory.
avi@google.com5cb79352008-12-11 23:55:12 +0900869 file_util::FileEnumerator f0(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900870 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +0900871 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
872 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +0900873
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900874 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +0900875 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900876 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +0900877 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900878 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +0900879 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900880 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +0900881
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900882 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +0900883 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900884 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900885 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900886 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900887 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900888 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900889 FilePath file2_rel =
890 dir2.Append(FilePath::kParentDirectory)
891 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900892 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +0900893 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900894
895 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +0900896 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900897 file_util::FileEnumerator::FILES);
898 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900899 EXPECT_TRUE(c1.HasFile(file1));
900 EXPECT_TRUE(c1.HasFile(file2_abs));
901 EXPECT_TRUE(c1.HasFile(dir2file));
902 EXPECT_TRUE(c1.HasFile(dir2innerfile));
903 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +0900904
905 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +0900906 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900907 file_util::FileEnumerator::DIRECTORIES);
908 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900909 EXPECT_TRUE(c2.HasFile(dir1));
910 EXPECT_TRUE(c2.HasFile(dir2));
911 EXPECT_TRUE(c2.HasFile(dir2inner));
912 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +0900913
tim@chromium.org989d0972008-10-16 11:42:45 +0900914 // Only enumerate directories non-recursively.
915 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +0900916 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +0900917 FindResultCollector c2_non_recursive(f2_non_recursive);
918 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
919 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
920 EXPECT_EQ(c2_non_recursive.size(), 2);
921
initial.commit3f4a7322008-07-27 06:49:38 +0900922 // Enumerate files and directories.
avi@google.com5cb79352008-12-11 23:55:12 +0900923 file_util::FileEnumerator f3(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900924 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
925 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900926 EXPECT_TRUE(c3.HasFile(dir1));
927 EXPECT_TRUE(c3.HasFile(dir2));
928 EXPECT_TRUE(c3.HasFile(file1));
929 EXPECT_TRUE(c3.HasFile(file2_abs));
930 EXPECT_TRUE(c3.HasFile(dir2file));
931 EXPECT_TRUE(c3.HasFile(dir2inner));
932 EXPECT_TRUE(c3.HasFile(dir2innerfile));
933 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +0900934
935 // Non-recursive operation.
avi@google.com5cb79352008-12-11 23:55:12 +0900936 file_util::FileEnumerator f4(test_dir_, false,
initial.commit3f4a7322008-07-27 06:49:38 +0900937 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
938 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900939 EXPECT_TRUE(c4.HasFile(dir2));
940 EXPECT_TRUE(c4.HasFile(dir2));
941 EXPECT_TRUE(c4.HasFile(file1));
942 EXPECT_TRUE(c4.HasFile(file2_abs));
943 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +0900944
945 // Enumerate with a pattern.
avi@google.com5cb79352008-12-11 23:55:12 +0900946 file_util::FileEnumerator f5(test_dir_, true,
947 file_util::FileEnumerator::FILES_AND_DIRECTORIES,
948 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900949 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900950 EXPECT_TRUE(c5.HasFile(dir1));
951 EXPECT_TRUE(c5.HasFile(dir2));
952 EXPECT_TRUE(c5.HasFile(dir2file));
953 EXPECT_TRUE(c5.HasFile(dir2inner));
954 EXPECT_TRUE(c5.HasFile(dir2innerfile));
955 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +0900956
957 // Make sure the destructor closes the find handle while in the middle of a
958 // query to allow TearDown to delete the directory.
avi@google.com5cb79352008-12-11 23:55:12 +0900959 file_util::FileEnumerator f6(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +0900960 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +0900961 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
962 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +0900963}
license.botf003cfe2008-08-24 09:55:55 +0900964
estade@chromium.org97e37822008-11-27 13:03:57 +0900965
966void PathComponents(const std::wstring& path,
967 std::vector<std::wstring>* components) {
968 DCHECK(components != NULL);
969 if (components == NULL)
970 return;
971 std::wstring::size_type start = 0;
972 std::wstring::size_type end = path.find('/', start);
973
974 // Special case the "/" or "\" directory. On Windows with a drive letter,
975 // this code path won't hit, but the right thing should still happen.
976 // "E:\foo" will turn into "E:","foo".
977 if (end == start) {
978 components->push_back(std::wstring(path, 0, 1));
979 start = end + 1;
980 end = path.find('/', start);
981 }
982 while (end != std::wstring::npos) {
983 std::wstring component = std::wstring(path, start, end - start);
984 components->push_back(component);
985 start = end + 1;
986 end = path.find('/', start);
987 }
988 std::wstring component = std::wstring(path, start);
989 components->push_back(component);
990}
991
992static const struct PathComponentsCase {
993 std::wstring path;
994 FilePath::StringType result;
995} kPathComponents[] = {
996 {L"/foo/bar/baz/", FILE_PATH_LITERAL("/|foo|bar|baz|")},
997 {L"/foo/bar/baz", FILE_PATH_LITERAL("/|foo|bar|baz")},
998 {L"e:/foo", FILE_PATH_LITERAL("e:|foo")},
999};
1000
1001TEST_F(FileUtilTest, PathComponentsTest) {
1002 for (size_t i = 0; i < arraysize(kPathComponents); ++i) {
1003 FilePath path = FilePath::FromWStringHack(kPathComponents[i].path);
1004 std::vector<FilePath::StringType> comps;
1005 file_util::PathComponents(path, &comps);
1006
1007 FilePath::StringType result;
1008 for (size_t j = 0; j < comps.size(); ++j) {
1009 result.append(comps[j]);
1010 if (j < comps.size() - 1)
1011 result.append(FILE_PATH_LITERAL("|"), 1);
1012 }
1013 EXPECT_EQ(kPathComponents[i].result, result);
1014 }
1015}
1016
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001017TEST_F(FileUtilTest, Contains) {
1018 FilePath data_dir;
1019 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &data_dir));
1020 data_dir = data_dir.Append(FILE_PATH_LITERAL("FilePathTest"));
1021
1022 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001023 if (file_util::PathExists(data_dir)) {
1024 ASSERT_TRUE(file_util::Delete(data_dir, true));
1025 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001026 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1027
1028 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1029 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1030 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1031 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1032
1033 // Annoyingly, the directories must actually exist in order for realpath(),
1034 // which Contains() relies on in posix, to work.
1035 ASSERT_TRUE(file_util::CreateDirectory(foo));
1036 std::string data("hello");
1037 ASSERT_TRUE(file_util::WriteFile(bar.ToWStringHack(), data.c_str(),
1038 data.length()));
1039 ASSERT_TRUE(file_util::WriteFile(baz.ToWStringHack(), data.c_str(),
1040 data.length()));
1041 ASSERT_TRUE(file_util::WriteFile(foobar.ToWStringHack(), data.c_str(),
1042 data.length()));
1043
1044 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1045 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1046 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1047 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1048
1049// Platform-specific concerns
1050 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1051#if defined(OS_WIN)
1052 EXPECT_TRUE(file_util::ContainsPath(foo,
1053 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001054 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001055 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
1056#elif defined(OS_LINUX)
1057 EXPECT_FALSE(file_util::ContainsPath(foo,
1058 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
1059#else
1060 // We can't really do this test on osx since the case-sensitivity of the
1061 // filesystem is configurable.
1062#endif
1063}
1064
mark@chromium.org17684802008-09-10 09:16:28 +09001065} // namespace