blob: 8f5ce199e16a27b4ce5a8849364fd76d46a75bc7 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]f35d39b2008-08-15 17:50:105#include "build/build_config.h"
6
[email protected]8541bb82008-08-15 17:45:137#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:388#include <windows.h>
initial.commitd7cae122008-07-26 21:49:389#include <shellapi.h>
10#include <shlobj.h>
[email protected]37088fef2008-08-15 17:32:1011#endif
initial.commitd7cae122008-07-26 21:49:3812
13#include <fstream>
14#include <iostream>
[email protected]37088fef2008-08-15 17:32:1015#include <set>
initial.commitd7cae122008-07-26 21:49:3816
17#include "base/base_paths.h"
[email protected]640517f2008-10-30 23:54:0418#include "base/file_path.h"
initial.commitd7cae122008-07-26 21:49:3819#include "base/file_util.h"
20#include "base/logging.h"
21#include "base/path_service.h"
22#include "base/string_util.h"
23#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1524#include "testing/platform_test.h"
initial.commitd7cae122008-07-26 21:49:3825
26namespace {
27
[email protected]ed2f2332008-08-20 15:59:4928// file_util winds up using autoreleased objects on the Mac, so this needs
29// to be a PlatformTest
30class FileUtilTest : public PlatformTest {
initial.commitd7cae122008-07-26 21:49:3831 protected:
32 virtual void SetUp() {
[email protected]ed2f2332008-08-20 15:59:4933 PlatformTest::SetUp();
initial.commitd7cae122008-07-26 21:49:3834 // Name a subdirectory of the temp directory.
35 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
[email protected]640517f2008-10-30 23:54:0436 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commitd7cae122008-07-26 21:49:3837
38 // Create a fresh, empty copy of this directory.
39 file_util::Delete(test_dir_, true);
[email protected]640517f2008-10-30 23:54:0440 file_util::CreateDirectory(test_dir_);
initial.commitd7cae122008-07-26 21:49:3841 }
42 virtual void TearDown() {
[email protected]ed2f2332008-08-20 15:59:4943 PlatformTest::TearDown();
initial.commitd7cae122008-07-26 21:49:3844 // Clean up test directory
[email protected]37088fef2008-08-15 17:32:1045 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commitd7cae122008-07-26 21:49:3846 ASSERT_FALSE(file_util::PathExists(test_dir_));
47 }
48
49 // the path to temporary directory used to contain the test operations
[email protected]640517f2008-10-30 23:54:0450 FilePath test_dir_;
initial.commitd7cae122008-07-26 21:49:3851};
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) {
[email protected]0b733222008-12-11 14:55:1258 FilePath cur_file;
59 while (!(cur_file = enumerator.Next()).value().empty()) {
60 FilePath::StringType path = cur_file.value();
initial.commitd7cae122008-07-26 21:49:3861 // The file should not be returned twice.
[email protected]640517f2008-10-30 23:54:0462 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commitd7cae122008-07-26 21:49:3863 << "Same file returned twice";
64
65 // Save for later.
[email protected]640517f2008-10-30 23:54:0466 files_.insert(path);
initial.commitd7cae122008-07-26 21:49:3867 }
68 }
69
70 // Returns true if the enumerator found the file.
[email protected]640517f2008-10-30 23:54:0471 bool HasFile(const FilePath& file) const {
72 return files_.find(file.value()) != files_.end();
initial.commitd7cae122008-07-26 21:49:3873 }
[email protected]640517f2008-10-30 23:54:0474
[email protected]37088fef2008-08-15 17:32:1075 int size() {
[email protected]f35d39b2008-08-15 17:50:1076 return static_cast<int>(files_.size());
[email protected]37088fef2008-08-15 17:32:1077 }
initial.commitd7cae122008-07-26 21:49:3878
79 private:
[email protected]640517f2008-10-30 23:54:0480 std::set<FilePath::StringType> files_;
initial.commitd7cae122008-07-26 21:49:3881};
82
83// Simple function to dump some text into a new file.
[email protected]640517f2008-10-30 23:54:0484void CreateTextFile(const FilePath& filename,
initial.commitd7cae122008-07-26 21:49:3885 const std::wstring& contents) {
86 std::ofstream file;
[email protected]640517f2008-10-30 23:54:0487 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commitd7cae122008-07-26 21:49:3888 ASSERT_TRUE(file.is_open());
89 file << contents;
90 file.close();
91}
92
93// Simple function to take out some text from a file.
[email protected]640517f2008-10-30 23:54:0494std::wstring ReadTextFile(const FilePath& filename) {
[email protected]37088fef2008-08-15 17:32:1095 wchar_t contents[64];
initial.commitd7cae122008-07-26 21:49:3896 std::wifstream file;
[email protected]640517f2008-10-30 23:54:0497 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commitd7cae122008-07-26 21:49:3898 EXPECT_TRUE(file.is_open());
99 file.getline(contents, 64);
100 file.close();
101 return std::wstring(contents);
102}
103
[email protected]8541bb82008-08-15 17:45:13104#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38105uint64 FileTimeAsUint64(const FILETIME& ft) {
106 ULARGE_INTEGER u;
107 u.LowPart = ft.dwLowDateTime;
108 u.HighPart = ft.dwHighDateTime;
109 return u.QuadPart;
110}
[email protected]37088fef2008-08-15 17:32:10111#endif
initial.commitd7cae122008-07-26 21:49:38112
113const struct append_case {
114 const wchar_t* path;
115 const wchar_t* ending;
116 const wchar_t* result;
117} append_cases[] = {
[email protected]8541bb82008-08-15 17:45:13118#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38119 {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"\\"},
[email protected]37088fef2008-08-15 17:32:10126#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.commitd7cae122008-07-26 21:49:38135};
136
initial.commitd7cae122008-07-26 21:49:38137TEST_F(FileUtilTest, AppendToPath) {
[email protected]37088fef2008-08-15 17:32:10138 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38139 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;
[email protected]ceeb87e2008-12-04 20:46:06152 FilePath::StringType suffix;
initial.commitd7cae122008-07-26 21:49:38153 std::wstring result;
154} kInsertBeforeExtension[] = {
[email protected]ceeb87e2008-12-04 20:46:06155 {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"},
[email protected]8541bb82008-08-15 17:45:13171#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38172 {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"},
[email protected]37088fef2008-08-15 17:32:10184#elif defined(OS_POSIX)
[email protected]ceeb87e2008-12-04 20:46:06185 {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"},
[email protected]37088fef2008-08-15 17:32:10197#endif
initial.commitd7cae122008-07-26 21:49:38198};
199
200TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
[email protected]37088fef2008-08-15 17:32:10201 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
[email protected]ceeb87e2008-12-04 20:46:06202 FilePath path = FilePath::FromWStringHack(kInsertBeforeExtension[i].path);
initial.commitd7cae122008-07-26 21:49:38203 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
[email protected]ceeb87e2008-12-04 20:46:06204 EXPECT_EQ(kInsertBeforeExtension[i].result, path.ToWStringHack());
initial.commitd7cae122008-07-26 21:49:38205 }
206}
207
208static const struct filename_case {
209 const wchar_t* path;
210 const wchar_t* filename;
211} filename_cases[] = {
[email protected]8541bb82008-08-15 17:45:13212#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38213 {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"},
[email protected]37088fef2008-08-15 17:32:10223#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.commitd7cae122008-07-26 21:49:38231};
232
233TEST_F(FileUtilTest, GetFilenameFromPath) {
[email protected]37088fef2008-08-15 17:32:10234 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38235 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[] = {
[email protected]8541bb82008-08-15 17:45:13246#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38247 {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"},
[email protected]37088fef2008-08-15 17:32:10253#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.commitd7cae122008-07-26 21:49:38266};
267
268TEST_F(FileUtilTest, GetFileExtensionFromPath) {
[email protected]37088fef2008-08-15 17:32:10269 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38270 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[] = {
[email protected]8541bb82008-08-15 17:45:13281#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38282 {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:"},
[email protected]37088fef2008-08-15 17:32:10289#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.commitd7cae122008-07-26 21:49:38300};
301
302TEST_F(FileUtilTest, GetDirectoryFromPath) {
[email protected]37088fef2008-08-15 17:32:10303 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38304 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
[email protected]37088fef2008-08-15 17:32:10311// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:13312#if defined OS_WIN
initial.commitd7cae122008-07-26 21:49:38313TEST_F(FileUtilTest, CountFilesCreatedAfter) {
314 // Create old file (that we don't want to count)
[email protected]640517f2008-10-30 23:54:04315 FilePath old_file_name = test_dir_.Append(L"Old File.txt");
initial.commitd7cae122008-07-26 21:49:38316 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);
[email protected]640517f2008-10-30 23:54:04324 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(),
325 test_start_time));
initial.commitd7cae122008-07-26 21:49:38326
327 // Create a new file (that we do want to count)
[email protected]640517f2008-10-30 23:54:04328 FilePath new_file_name = test_dir_.Append(L"New File.txt");
initial.commitd7cae122008-07-26 21:49:38329 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
330
331 // We should see only the new file.
[email protected]640517f2008-10-30 23:54:04332 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_.value(),
333 test_start_time));
initial.commitd7cae122008-07-26 21:49:38334
335 // Delete new file, we should see no files after cutoff now
336 EXPECT_TRUE(file_util::Delete(new_file_name, false));
[email protected]640517f2008-10-30 23:54:04337 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(),
338 test_start_time));
initial.commitd7cae122008-07-26 21:49:38339}
[email protected]37088fef2008-08-15 17:32:10340#endif
initial.commitd7cae122008-07-26 21:49:38341
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
[email protected]640517f2008-10-30 23:54:04346 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commitd7cae122008-07-26 21:49:38347 CreateTextFile(file_name, L"I'm cannon fodder.");
348
349 ASSERT_TRUE(file_util::PathExists(file_name));
350
[email protected]640517f2008-10-30 23:54:04351 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
352 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38353
354 ASSERT_TRUE(file_util::PathExists(subdir_path));
355
[email protected]640517f2008-10-30 23:54:04356 FilePath directory_contents = test_dir_;
[email protected]8541bb82008-08-15 17:45:13357#if defined(OS_WIN)
[email protected]37088fef2008-08-15 17:32:10358 // TODO(erikkay): see if anyone's actually using this feature of the API
[email protected]640517f2008-10-30 23:54:04359 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commitd7cae122008-07-26 21:49:38360 // Delete non-recursively and check that only the file is deleted
361 ASSERT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10362 EXPECT_FALSE(file_util::PathExists(file_name));
363 EXPECT_TRUE(file_util::PathExists(subdir_path));
364#endif
initial.commitd7cae122008-07-26 21:49:38365
366 // Delete recursively and make sure all contents are deleted
367 ASSERT_TRUE(file_util::Delete(directory_contents, true));
[email protected]37088fef2008-08-15 17:32:10368 EXPECT_FALSE(file_util::PathExists(file_name));
369 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commitd7cae122008-07-26 21:49:38370}
371
372TEST_F(FileUtilTest, Move) {
373 // Create a directory
[email protected]640517f2008-10-30 23:54:04374 FilePath dir_name_from =
375 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
376 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38377 ASSERT_TRUE(file_util::PathExists(dir_name_from));
378
379 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04380 FilePath file_name_from =
381 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38382 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
383 ASSERT_TRUE(file_util::PathExists(file_name_from));
384
385 // Move the directory
[email protected]640517f2008-10-30 23:54:04386 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.commitd7cae122008-07-26 21:49:38389
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.
[email protected]640517f2008-10-30 23:54:04403 FilePath dir_name_from =
404 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
405 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38406 ASSERT_TRUE(file_util::PathExists(dir_name_from));
407
408 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04409 FilePath file_name_from =
410 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38411 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
412 ASSERT_TRUE(file_util::PathExists(file_name_from));
413
414 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04415 FilePath subdir_name_from =
416 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
417 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38418 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
419
420 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04421 FilePath file_name2_from =
422 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38423 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
424 ASSERT_TRUE(file_util::PathExists(file_name2_from));
425
426 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:04427 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.commitd7cae122008-07-26 21:49:38435
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.
[email protected]640517f2008-10-30 23:54:04453 FilePath dir_name_from =
454 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
455 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38456 ASSERT_TRUE(file_util::PathExists(dir_name_from));
457
458 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04459 FilePath file_name_from =
460 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38461 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
462 ASSERT_TRUE(file_util::PathExists(file_name_from));
463
464 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04465 FilePath subdir_name_from =
466 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
467 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38468 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
469
470 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04471 FilePath file_name2_from =
472 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38473 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
474 ASSERT_TRUE(file_util::PathExists(file_name2_from));
475
476 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:04477 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.commitd7cae122008-07-26 21:49:38483
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
[email protected]640517f2008-10-30 23:54:04500 FilePath dir_name_from =
501 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
502 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38503 ASSERT_TRUE(file_util::PathExists(dir_name_from));
504
505 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04506 FilePath file_name_from =
507 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38508 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.
[email protected]640517f2008-10-30 23:54:04513 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:38514 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:11515
[email protected]37088fef2008-08-15 17:32:10516 // Copy the file to another location using '..' in the path.
[email protected]640517f2008-10-30 23:54:04517 std::wstring dest_file2(dir_name_from.ToWStringHack());
[email protected]37088fef2008-08-15 17:32:10518 file_util::AppendToPath(&dest_file2, L"..");
519 file_util::AppendToPath(&dest_file2, L"DestFile.txt");
[email protected]640517f2008-10-30 23:54:04520 ASSERT_TRUE(file_util::CopyFile(file_name_from,
521 FilePath::FromWStringHack(dest_file2)));
522 std::wstring dest_file2_test(dir_name_from.ToWStringHack());
[email protected]37088fef2008-08-15 17:32:10523 file_util::UpOneDirectory(&dest_file2_test);
524 file_util::AppendToPath(&dest_file2_test, L"DestFile.txt");
initial.commitd7cae122008-07-26 21:49:38525
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);
[email protected]640517f2008-10-30 23:54:04531 EXPECT_TRUE(file_util::PathExists(
532 FilePath::FromWStringHack(dest_file2_test)));
533 EXPECT_TRUE(file_util::PathExists(FilePath::FromWStringHack(dest_file2)));
initial.commitd7cae122008-07-26 21:49:38534}
535
[email protected]37088fef2008-08-15 17:32:10536// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:13537#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38538TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]640517f2008-10-30 23:54:04539 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:38540
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;
[email protected]640517f2008-10-30 23:54:04550 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:38551
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
[email protected]640517f2008-10-30 23:54:04567 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:38568}
[email protected]37088fef2008-08-15 17:32:10569#endif
initial.commitd7cae122008-07-26 21:49:38570
[email protected]ed2f2332008-08-20 15:59:49571// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:04572// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:49573typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:38574
[email protected]ed2f2332008-08-20 15:59:49575TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:04576 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:38577 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:04578 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
579 .Append(FILE_PATH_LITERAL("data"))
580 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:38581 ASSERT_TRUE(file_util::PathExists(data_dir));
582
[email protected]640517f2008-10-30 23:54:04583 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.commitd7cae122008-07-26 21:49:38607
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
[email protected]37088fef2008-08-15 17:32:10622// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:13623#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38624TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]640517f2008-10-30 23:54:04625 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:38626 CreateTextFile(target_file, L"This is the target.");
627
[email protected]640517f2008-10-30 23:54:04628 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:38629
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));
[email protected]640517f2008-10-30 23:54:04643 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:38644 EXPECT_TRUE(SUCCEEDED(result));
645 result = shell->SetDescription(L"ResolveShortcutTest");
646 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:04647 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:38648 EXPECT_TRUE(SUCCEEDED(result));
649 if (persist)
650 persist->Release();
651 if (shell)
652 shell->Release();
653
654 bool is_solved;
[email protected]640517f2008-10-30 23:54:04655 std::wstring link_file_str = link_file.value();
656 is_solved = file_util::ResolveShortcut(&link_file_str);
initial.commitd7cae122008-07-26 21:49:38657 EXPECT_TRUE(is_solved);
658 std::wstring contents;
[email protected]640517f2008-10-30 23:54:04659 contents = ReadTextFile(FilePath(link_file_str));
initial.commitd7cae122008-07-26 21:49:38660 EXPECT_EQ(L"This is the target.", contents);
661
[email protected]d324ab332008-08-18 16:00:38662 // Cleaning
[email protected]640517f2008-10-30 23:54:04663 DeleteFile(target_file.value().c_str());
664 DeleteFile(link_file_str.c_str());
initial.commitd7cae122008-07-26 21:49:38665 CoUninitialize();
666}
667
668TEST_F(FileUtilTest, CreateShortcutTest) {
669 const wchar_t file_contents[] = L"This is another target.";
[email protected]640517f2008-10-30 23:54:04670 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:38671 CreateTextFile(target_file, file_contents);
672
[email protected]640517f2008-10-30 23:54:04673 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:38674
675 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:04676 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
677 link_file.value().c_str(),
initial.commitd7cae122008-07-26 21:49:38678 NULL, NULL, NULL, NULL, 0));
[email protected]640517f2008-10-30 23:54:04679 std::wstring resolved_name = link_file.value();
initial.commitd7cae122008-07-26 21:49:38680 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]640517f2008-10-30 23:54:04681 std::wstring read_contents = ReadTextFile(FilePath(resolved_name));
initial.commitd7cae122008-07-26 21:49:38682 EXPECT_EQ(file_contents, read_contents);
683
[email protected]640517f2008-10-30 23:54:04684 DeleteFile(target_file.value().c_str());
685 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:38686 CoUninitialize();
687}
[email protected]37088fef2008-08-15 17:32:10688#endif
initial.commitd7cae122008-07-26 21:49:38689
690TEST_F(FileUtilTest, CreateTemporaryFileNameTest) {
691 std::wstring temp_file;
[email protected]392264c2008-11-11 00:01:38692 ASSERT_TRUE(file_util::CreateTemporaryFileName(&temp_file));
[email protected]806b9c62008-09-11 16:09:11693 EXPECT_TRUE(file_util::PathExists(temp_file));
694 EXPECT_TRUE(file_util::Delete(temp_file, false));
initial.commitd7cae122008-07-26 21:49:38695}
696
697TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
698 std::wstring temp_dir;
[email protected]392264c2008-11-11 00:01:38699 ASSERT_TRUE(file_util::CreateNewTempDirectory(std::wstring(), &temp_dir));
[email protected]806b9c62008-09-11 16:09:11700 EXPECT_TRUE(file_util::PathExists(temp_dir));
701 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:38702}
703
704TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:04705 FilePath test_root =
706 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:13707#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:04708 FilePath test_path =
709 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:10710#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:04711 FilePath test_path =
712 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:10713#endif
[email protected]806b9c62008-09-11 16:09:11714
715 EXPECT_FALSE(file_util::PathExists(test_path));
716 EXPECT_TRUE(file_util::CreateDirectory(test_path));
717 EXPECT_TRUE(file_util::PathExists(test_path));
718 // CreateDirectory returns true if the DirectoryExists returns true.
719 EXPECT_TRUE(file_util::CreateDirectory(test_path));
720
721 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:04722 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:11723 EXPECT_FALSE(file_util::PathExists(test_path));
724 CreateTextFile(test_path, L"test file");
725 EXPECT_TRUE(file_util::PathExists(test_path));
726 EXPECT_FALSE(file_util::CreateDirectory(test_path));
727
728 EXPECT_TRUE(file_util::Delete(test_root, true));
729 EXPECT_FALSE(file_util::PathExists(test_root));
730 EXPECT_FALSE(file_util::PathExists(test_path));
731}
732
733TEST_F(FileUtilTest, DetectDirectoryTest) {
734 // Check a directory
[email protected]640517f2008-10-30 23:54:04735 FilePath test_root =
736 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:11737 EXPECT_FALSE(file_util::PathExists(test_root));
738 EXPECT_TRUE(file_util::CreateDirectory(test_root));
739 EXPECT_TRUE(file_util::PathExists(test_root));
740 EXPECT_TRUE(file_util::DirectoryExists(test_root));
741
742 // Check a file
[email protected]640517f2008-10-30 23:54:04743 FilePath test_path =
744 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:11745 EXPECT_FALSE(file_util::PathExists(test_path));
746 CreateTextFile(test_path, L"test file");
747 EXPECT_TRUE(file_util::PathExists(test_path));
748 EXPECT_FALSE(file_util::DirectoryExists(test_path));
749 EXPECT_TRUE(file_util::Delete(test_path, false));
750
751 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:38752}
753
[email protected]37088fef2008-08-15 17:32:10754static const struct goodbad_pair {
initial.commitd7cae122008-07-26 21:49:38755 std::wstring bad_name;
756 std::wstring good_name;
757} kIllegalCharacterCases[] = {
758 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
759 {L"**********::::.txt", L"--------------.txt"},
initial.commitd7cae122008-07-26 21:49:38760 // We can't use UCNs (universal character names) for C0/C1 characters and
761 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
762 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
[email protected]8541bb82008-08-15 17:45:13763#if defined(OS_WIN)
[email protected]37088fef2008-08-15 17:32:10764 {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
initial.commitd7cae122008-07-26 21:49:38765 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
766 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
[email protected]37088fef2008-08-15 17:32:10767#elif defined(OS_POSIX)
768 {L"bad*file?name.jpg", L"bad-file-name.jpg"},
769 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"},
770 {L"bad\uFFFFfile-name.jpg ", L"bad-file-name.jpg"},
771#endif
initial.commitd7cae122008-07-26 21:49:38772 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
773 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
774 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
775 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
776 // Unassigned codepoints are ok.
777 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
778};
779
780TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) {
[email protected]37088fef2008-08-15 17:32:10781 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
initial.commitd7cae122008-07-26 21:49:38782 std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
783 file_util::ReplaceIllegalCharacters(&bad_name, L'-');
784 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
785 }
786}
787
788static const struct ReplaceExtensionCase {
789 std::wstring file_name;
[email protected]ceeb87e2008-12-04 20:46:06790 FilePath::StringType extension;
initial.commitd7cae122008-07-26 21:49:38791 std::wstring result;
792} kReplaceExtension[] = {
[email protected]ceeb87e2008-12-04 20:46:06793 {L"", FILE_PATH_LITERAL(""), L""},
794 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
795 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
796 {L".", FILE_PATH_LITERAL(""), L""},
797 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
798 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
799 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
800 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
801 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
802 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
803 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
804 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
805 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
806 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
807 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
808 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commitd7cae122008-07-26 21:49:38809};
810
811TEST_F(FileUtilTest, ReplaceExtensionTest) {
[email protected]37088fef2008-08-15 17:32:10812 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
[email protected]ceeb87e2008-12-04 20:46:06813 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
814 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
815 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commitd7cae122008-07-26 21:49:38816 }
817}
818
[email protected]b988fe4d2008-09-19 17:32:18819// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
820// the directory names of the path.
821TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
[email protected]ceeb87e2008-12-04 20:46:06822 FilePath path;
823 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
824 path = path.Append(FILE_PATH_LITERAL("foo"));
[email protected]b988fe4d2008-09-19 17:32:18825 // '/foo.bar/foo' with extension '.baz'
[email protected]ceeb87e2008-12-04 20:46:06826 FilePath result_path = path;
827 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
828 EXPECT_EQ(path.ToWStringHack() + L".baz", result_path.ToWStringHack());
[email protected]b988fe4d2008-09-19 17:32:18829}
830
initial.commitd7cae122008-07-26 21:49:38831TEST_F(FileUtilTest, FileEnumeratorTest) {
832 // Test an empty directory.
[email protected]0b733222008-12-11 14:55:12833 file_util::FileEnumerator f0(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:38834 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:12835 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
836 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:38837
[email protected]37088fef2008-08-15 17:32:10838 // create the directories
[email protected]640517f2008-10-30 23:54:04839 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:10840 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]640517f2008-10-30 23:54:04841 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:10842 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:04843 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:10844 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:04845
[email protected]37088fef2008-08-15 17:32:10846 // create the files
[email protected]640517f2008-10-30 23:54:04847 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:10848 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:04849 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:10850 CreateTextFile(dir2innerfile, L"");
[email protected]640517f2008-10-30 23:54:04851 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:10852 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:04853 FilePath file2_rel =
854 dir2.Append(FilePath::kParentDirectory)
855 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:10856 CreateTextFile(file2_rel, L"");
[email protected]640517f2008-10-30 23:54:04857 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:38858
859 // Only enumerate files.
[email protected]0b733222008-12-11 14:55:12860 file_util::FileEnumerator f1(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:38861 file_util::FileEnumerator::FILES);
862 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:10863 EXPECT_TRUE(c1.HasFile(file1));
864 EXPECT_TRUE(c1.HasFile(file2_abs));
865 EXPECT_TRUE(c1.HasFile(dir2file));
866 EXPECT_TRUE(c1.HasFile(dir2innerfile));
867 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:38868
869 // Only enumerate directories.
[email protected]0b733222008-12-11 14:55:12870 file_util::FileEnumerator f2(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:38871 file_util::FileEnumerator::DIRECTORIES);
872 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:10873 EXPECT_TRUE(c2.HasFile(dir1));
874 EXPECT_TRUE(c2.HasFile(dir2));
875 EXPECT_TRUE(c2.HasFile(dir2inner));
876 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:38877
[email protected]3bc9ffaf2008-10-16 02:42:45878 // Only enumerate directories non-recursively.
879 file_util::FileEnumerator f2_non_recursive(
[email protected]0b733222008-12-11 14:55:12880 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:45881 FindResultCollector c2_non_recursive(f2_non_recursive);
882 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
883 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
884 EXPECT_EQ(c2_non_recursive.size(), 2);
885
initial.commitd7cae122008-07-26 21:49:38886 // Enumerate files and directories.
[email protected]0b733222008-12-11 14:55:12887 file_util::FileEnumerator f3(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:38888 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
889 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:10890 EXPECT_TRUE(c3.HasFile(dir1));
891 EXPECT_TRUE(c3.HasFile(dir2));
892 EXPECT_TRUE(c3.HasFile(file1));
893 EXPECT_TRUE(c3.HasFile(file2_abs));
894 EXPECT_TRUE(c3.HasFile(dir2file));
895 EXPECT_TRUE(c3.HasFile(dir2inner));
896 EXPECT_TRUE(c3.HasFile(dir2innerfile));
897 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:38898
899 // Non-recursive operation.
[email protected]0b733222008-12-11 14:55:12900 file_util::FileEnumerator f4(test_dir_, false,
initial.commitd7cae122008-07-26 21:49:38901 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
902 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:10903 EXPECT_TRUE(c4.HasFile(dir2));
904 EXPECT_TRUE(c4.HasFile(dir2));
905 EXPECT_TRUE(c4.HasFile(file1));
906 EXPECT_TRUE(c4.HasFile(file2_abs));
907 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:38908
909 // Enumerate with a pattern.
[email protected]0b733222008-12-11 14:55:12910 file_util::FileEnumerator f5(test_dir_, true,
911 file_util::FileEnumerator::FILES_AND_DIRECTORIES,
912 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:38913 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:10914 EXPECT_TRUE(c5.HasFile(dir1));
915 EXPECT_TRUE(c5.HasFile(dir2));
916 EXPECT_TRUE(c5.HasFile(dir2file));
917 EXPECT_TRUE(c5.HasFile(dir2inner));
918 EXPECT_TRUE(c5.HasFile(dir2innerfile));
919 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:38920
921 // Make sure the destructor closes the find handle while in the middle of a
922 // query to allow TearDown to delete the directory.
[email protected]0b733222008-12-11 14:55:12923 file_util::FileEnumerator f6(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:38924 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:12925 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
926 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:38927}
license.botbf09a502008-08-24 00:55:55928
[email protected]b9e04f02008-11-27 04:03:57929
930void PathComponents(const std::wstring& path,
931 std::vector<std::wstring>* components) {
932 DCHECK(components != NULL);
933 if (components == NULL)
934 return;
935 std::wstring::size_type start = 0;
936 std::wstring::size_type end = path.find('/', start);
937
938 // Special case the "/" or "\" directory. On Windows with a drive letter,
939 // this code path won't hit, but the right thing should still happen.
940 // "E:\foo" will turn into "E:","foo".
941 if (end == start) {
942 components->push_back(std::wstring(path, 0, 1));
943 start = end + 1;
944 end = path.find('/', start);
945 }
946 while (end != std::wstring::npos) {
947 std::wstring component = std::wstring(path, start, end - start);
948 components->push_back(component);
949 start = end + 1;
950 end = path.find('/', start);
951 }
952 std::wstring component = std::wstring(path, start);
953 components->push_back(component);
954}
955
956static const struct PathComponentsCase {
957 std::wstring path;
958 FilePath::StringType result;
959} kPathComponents[] = {
960 {L"/foo/bar/baz/", FILE_PATH_LITERAL("/|foo|bar|baz|")},
961 {L"/foo/bar/baz", FILE_PATH_LITERAL("/|foo|bar|baz")},
962 {L"e:/foo", FILE_PATH_LITERAL("e:|foo")},
963};
964
965TEST_F(FileUtilTest, PathComponentsTest) {
966 for (size_t i = 0; i < arraysize(kPathComponents); ++i) {
967 FilePath path = FilePath::FromWStringHack(kPathComponents[i].path);
968 std::vector<FilePath::StringType> comps;
969 file_util::PathComponents(path, &comps);
970
971 FilePath::StringType result;
972 for (size_t j = 0; j < comps.size(); ++j) {
973 result.append(comps[j]);
974 if (j < comps.size() - 1)
975 result.append(FILE_PATH_LITERAL("|"), 1);
976 }
977 EXPECT_EQ(kPathComponents[i].result, result);
978 }
979}
980
[email protected]ee5c29da2009-01-09 22:14:27981TEST_F(FileUtilTest, Contains) {
982 FilePath data_dir;
983 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &data_dir));
984 data_dir = data_dir.Append(FILE_PATH_LITERAL("FilePathTest"));
985
986 // Create a fresh, empty copy of this directory.
987 ASSERT_TRUE(file_util::Delete(data_dir, true));
988 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
989
990 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
991 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
992 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
993 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
994
995 // Annoyingly, the directories must actually exist in order for realpath(),
996 // which Contains() relies on in posix, to work.
997 ASSERT_TRUE(file_util::CreateDirectory(foo));
998 std::string data("hello");
999 ASSERT_TRUE(file_util::WriteFile(bar.ToWStringHack(), data.c_str(),
1000 data.length()));
1001 ASSERT_TRUE(file_util::WriteFile(baz.ToWStringHack(), data.c_str(),
1002 data.length()));
1003 ASSERT_TRUE(file_util::WriteFile(foobar.ToWStringHack(), data.c_str(),
1004 data.length()));
1005
1006 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1007 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1008 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1009 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1010
1011// Platform-specific concerns
1012 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1013#if defined(OS_WIN)
1014 EXPECT_TRUE(file_util::ContainsPath(foo,
1015 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
1016 EXPECT_TRUE(file_util::ContainsPath(foo,
1017 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
1018#elif defined(OS_LINUX)
1019 EXPECT_FALSE(file_util::ContainsPath(foo,
1020 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
1021#else
1022 // We can't really do this test on osx since the case-sensitivity of the
1023 // filesystem is configurable.
1024#endif
1025}
1026
[email protected]11b901ee2008-09-10 00:16:281027} // namespace