blob: 5430a2d3106de5cdcf7c90fb326d5c2f4acb5c3f [file] [log] [blame]
[email protected]71b7da72012-01-17 22:44:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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]7d95aae2009-10-09 07:33:3911#include <tchar.h>
[email protected]e0785902011-05-19 23:34:1712#include <winioctl.h>
[email protected]37088fef2008-08-15 17:32:1013#endif
initial.commitd7cae122008-07-26 21:49:3814
[email protected]aead7d32011-10-25 00:08:2815#include <algorithm>
initial.commitd7cae122008-07-26 21:49:3816#include <fstream>
[email protected]37088fef2008-08-15 17:32:1017#include <set>
initial.commitd7cae122008-07-26 21:49:3818
19#include "base/base_paths.h"
[email protected]640517f2008-10-30 23:54:0420#include "base/file_path.h"
initial.commitd7cae122008-07-26 21:49:3821#include "base/file_util.h"
initial.commitd7cae122008-07-26 21:49:3822#include "base/path_service.h"
[email protected]e0785902011-05-19 23:34:1723#include "base/scoped_temp_dir.h"
[email protected]ce072a72010-12-31 20:02:1624#include "base/threading/platform_thread.h"
[email protected]047a03f2009-10-07 02:10:2025#include "base/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3826#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1527#include "testing/platform_test.h"
initial.commitd7cae122008-07-26 21:49:3828
[email protected]b90d7e802011-01-09 16:32:2029#if defined(OS_WIN)
30#include "base/win/scoped_handle.h"
31#endif
32
[email protected]4e9f6be2009-04-03 17:17:5833// This macro helps avoid wrapped lines in the test structs.
34#define FPL(x) FILE_PATH_LITERAL(x)
35
initial.commitd7cae122008-07-26 21:49:3836namespace {
37
[email protected]6f5f4322010-06-09 22:56:4838// To test that file_util::Normalize FilePath() deals with NTFS reparse points
39// correctly, we need functions to create and delete reparse points.
40#if defined(OS_WIN)
41typedef struct _REPARSE_DATA_BUFFER {
42 ULONG ReparseTag;
43 USHORT ReparseDataLength;
44 USHORT Reserved;
45 union {
46 struct {
47 USHORT SubstituteNameOffset;
48 USHORT SubstituteNameLength;
49 USHORT PrintNameOffset;
50 USHORT PrintNameLength;
51 ULONG Flags;
52 WCHAR PathBuffer[1];
53 } SymbolicLinkReparseBuffer;
54 struct {
55 USHORT SubstituteNameOffset;
56 USHORT SubstituteNameLength;
57 USHORT PrintNameOffset;
58 USHORT PrintNameLength;
59 WCHAR PathBuffer[1];
60 } MountPointReparseBuffer;
61 struct {
62 UCHAR DataBuffer[1];
63 } GenericReparseBuffer;
64 };
65} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
66
67// Sets a reparse point. |source| will now point to |target|. Returns true if
68// the call succeeds, false otherwise.
69bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
70 std::wstring kPathPrefix = L"\\??\\";
71 std::wstring target_str;
72 // The juction will not work if the target path does not start with \??\ .
73 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
74 target_str += kPathPrefix;
75 target_str += target_path.value();
76 const wchar_t* target = target_str.c_str();
77 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
78 char buffer[2000] = {0};
79 DWORD returned;
80
81 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
82
83 data->ReparseTag = 0xa0000003;
84 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
85
86 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
87 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
88 data->ReparseDataLength = size_target + 4 + 8;
89
90 int data_size = data->ReparseDataLength + 8;
91
92 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
93 NULL, 0, &returned, NULL)) {
94 return false;
95 }
96 return true;
97}
98
99// Delete the reparse point referenced by |source|. Returns true if the call
100// succeeds, false otherwise.
101bool DeleteReparsePoint(HANDLE source) {
102 DWORD returned;
103 REPARSE_DATA_BUFFER data = {0};
104 data.ReparseTag = 0xa0000003;
105 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
106 &returned, NULL)) {
107 return false;
108 }
109 return true;
110}
111#endif
112
[email protected]73e4c362011-09-22 14:47:18113#if defined(OS_POSIX)
114// Provide a simple way to change the permissions bits on |path| in tests.
115// ASSERT failures will return, but not stop the test. Caller should wrap
116// calls to this function in ASSERT_NO_FATAL_FAILURE().
117void ChangePosixFilePermissions(const FilePath& path,
118 mode_t mode_bits_to_set,
119 mode_t mode_bits_to_clear) {
120 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear)
121 << "Can't set and clear the same bits.";
122
123 struct stat stat_buf;
124 ASSERT_EQ(0, stat(path.value().c_str(), &stat_buf));
125
126 mode_t updated_mode_bits = stat_buf.st_mode;
127 updated_mode_bits |= mode_bits_to_set;
128 updated_mode_bits &= ~mode_bits_to_clear;
129
130 ASSERT_EQ(0, chmod(path.value().c_str(), updated_mode_bits));
131}
132#endif // defined(OS_POSIX)
133
[email protected]83d86252010-05-07 18:58:45134const wchar_t bogus_content[] = L"I'm cannon fodder.";
135
[email protected]58b7c5a62011-08-15 13:09:27136const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES =
137 static_cast<file_util::FileEnumerator::FileType>(
[email protected]8199b3a2009-06-09 05:57:38138 file_util::FileEnumerator::FILES |
139 file_util::FileEnumerator::DIRECTORIES);
140
[email protected]ed2f2332008-08-20 15:59:49141// file_util winds up using autoreleased objects on the Mac, so this needs
142// to be a PlatformTest
143class FileUtilTest : public PlatformTest {
initial.commitd7cae122008-07-26 21:49:38144 protected:
145 virtual void SetUp() {
[email protected]ed2f2332008-08-20 15:59:49146 PlatformTest::SetUp();
[email protected]90314c0a82010-09-15 20:40:47147 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
initial.commitd7cae122008-07-26 21:49:38148 }
149
[email protected]90314c0a82010-09-15 20:40:47150 ScopedTempDir temp_dir_;
initial.commitd7cae122008-07-26 21:49:38151};
152
153// Collects all the results from the given file enumerator, and provides an
154// interface to query whether a given file is present.
155class FindResultCollector {
156 public:
[email protected]53c58042009-08-26 20:00:14157 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
[email protected]0b733222008-12-11 14:55:12158 FilePath cur_file;
159 while (!(cur_file = enumerator.Next()).value().empty()) {
160 FilePath::StringType path = cur_file.value();
initial.commitd7cae122008-07-26 21:49:38161 // The file should not be returned twice.
[email protected]640517f2008-10-30 23:54:04162 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commitd7cae122008-07-26 21:49:38163 << "Same file returned twice";
164
165 // Save for later.
[email protected]640517f2008-10-30 23:54:04166 files_.insert(path);
initial.commitd7cae122008-07-26 21:49:38167 }
168 }
169
170 // Returns true if the enumerator found the file.
[email protected]640517f2008-10-30 23:54:04171 bool HasFile(const FilePath& file) const {
172 return files_.find(file.value()) != files_.end();
initial.commitd7cae122008-07-26 21:49:38173 }
[email protected]640517f2008-10-30 23:54:04174
[email protected]37088fef2008-08-15 17:32:10175 int size() {
[email protected]f35d39b2008-08-15 17:50:10176 return static_cast<int>(files_.size());
[email protected]37088fef2008-08-15 17:32:10177 }
initial.commitd7cae122008-07-26 21:49:38178
179 private:
[email protected]640517f2008-10-30 23:54:04180 std::set<FilePath::StringType> files_;
initial.commitd7cae122008-07-26 21:49:38181};
182
183// Simple function to dump some text into a new file.
[email protected]640517f2008-10-30 23:54:04184void CreateTextFile(const FilePath& filename,
initial.commitd7cae122008-07-26 21:49:38185 const std::wstring& contents) {
[email protected]76c551c2011-03-25 20:49:54186 std::wofstream file;
[email protected]8a205c02011-02-04 20:41:33187 file.open(filename.value().c_str());
initial.commitd7cae122008-07-26 21:49:38188 ASSERT_TRUE(file.is_open());
189 file << contents;
190 file.close();
191}
192
193// Simple function to take out some text from a file.
[email protected]640517f2008-10-30 23:54:04194std::wstring ReadTextFile(const FilePath& filename) {
[email protected]37088fef2008-08-15 17:32:10195 wchar_t contents[64];
initial.commitd7cae122008-07-26 21:49:38196 std::wifstream file;
[email protected]8a205c02011-02-04 20:41:33197 file.open(filename.value().c_str());
initial.commitd7cae122008-07-26 21:49:38198 EXPECT_TRUE(file.is_open());
[email protected]76c551c2011-03-25 20:49:54199 file.getline(contents, arraysize(contents));
initial.commitd7cae122008-07-26 21:49:38200 file.close();
201 return std::wstring(contents);
202}
203
[email protected]8541bb82008-08-15 17:45:13204#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38205uint64 FileTimeAsUint64(const FILETIME& ft) {
206 ULARGE_INTEGER u;
207 u.LowPart = ft.dwLowDateTime;
208 u.HighPart = ft.dwHighDateTime;
209 return u.QuadPart;
210}
[email protected]37088fef2008-08-15 17:32:10211#endif
initial.commitd7cae122008-07-26 21:49:38212
213const struct append_case {
214 const wchar_t* path;
215 const wchar_t* ending;
216 const wchar_t* result;
217} append_cases[] = {
[email protected]8541bb82008-08-15 17:45:13218#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38219 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
220 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
221 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
222 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
223 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
224 {L"", L"path", L"\\path"},
225 {L"", L"", L"\\"},
[email protected]37088fef2008-08-15 17:32:10226#elif defined(OS_POSIX)
227 {L"/foo/bar", L"path", L"/foo/bar/path"},
228 {L"/foo/bar/", L"path", L"/foo/bar/path"},
229 {L"/foo/bar//", L"path", L"/foo/bar//path"},
230 {L"/foo/bar/", L"", L"/foo/bar/"},
231 {L"/foo/bar", L"", L"/foo/bar/"},
232 {L"", L"path", L"/path"},
233 {L"", L"", L"/"},
234#endif
initial.commitd7cae122008-07-26 21:49:38235};
236
initial.commitd7cae122008-07-26 21:49:38237static const struct filename_case {
238 const wchar_t* path;
239 const wchar_t* filename;
240} filename_cases[] = {
[email protected]8541bb82008-08-15 17:45:13241#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38242 {L"c:\\colon\\backslash", L"backslash"},
243 {L"c:\\colon\\backslash\\", L""},
244 {L"\\\\filename.exe", L"filename.exe"},
245 {L"filename.exe", L"filename.exe"},
246 {L"", L""},
247 {L"\\\\\\", L""},
248 {L"c:/colon/backslash", L"backslash"},
249 {L"c:/colon/backslash/", L""},
250 {L"//////", L""},
251 {L"///filename.exe", L"filename.exe"},
[email protected]37088fef2008-08-15 17:32:10252#elif defined(OS_POSIX)
253 {L"/foo/bar", L"bar"},
254 {L"/foo/bar/", L""},
255 {L"/filename.exe", L"filename.exe"},
256 {L"filename.exe", L"filename.exe"},
257 {L"", L""},
258 {L"/", L""},
259#endif
initial.commitd7cae122008-07-26 21:49:38260};
261
initial.commitd7cae122008-07-26 21:49:38262// Test finding the file type from a path name
263static const struct extension_case {
264 const wchar_t* path;
265 const wchar_t* extension;
266} extension_cases[] = {
[email protected]8541bb82008-08-15 17:45:13267#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38268 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
269 {L"C:\\colon\\backslash\\filename.", L""},
270 {L"C:\\colon\\backslash\\filename", L""},
271 {L"C:\\colon\\backslash\\", L""},
272 {L"C:\\colon\\backslash.\\", L""},
273 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
[email protected]37088fef2008-08-15 17:32:10274#elif defined(OS_POSIX)
275 {L"/foo/bar/filename.extension", L"extension"},
276 {L"/foo/bar/filename.", L""},
277 {L"/foo/bar/filename", L""},
278 {L"/foo/bar/", L""},
279 {L"/foo/bar./", L""},
280 {L"/foo/bar/filename.extension.extension2", L"extension2"},
281 {L".", L""},
282 {L"..", L""},
283 {L"./foo", L""},
284 {L"./foo.extension", L"extension"},
285 {L"/foo.extension1/bar.extension2", L"extension2"},
286#endif
initial.commitd7cae122008-07-26 21:49:38287};
288
initial.commitd7cae122008-07-26 21:49:38289// Test finding the directory component of a path
290static const struct dir_case {
291 const wchar_t* full_path;
292 const wchar_t* directory;
293} dir_cases[] = {
[email protected]8541bb82008-08-15 17:45:13294#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38295 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
296 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
297 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
298 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
299 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
300 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
[email protected]2c8088a42009-10-15 05:00:25301 {L"C:\\", L"C:\\"},
[email protected]37088fef2008-08-15 17:32:10302#elif defined(OS_POSIX)
303 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
304 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
305 {L"/foo/bar/", L"/foo/bar"},
306 {L"/foo/bar//", L"/foo/bar"},
307 {L"/foo/bar", L"/foo"},
308 {L"/foo/bar./", L"/foo/bar."},
309 {L"/", L"/"},
310 {L".", L"."},
[email protected]53c58042009-08-26 20:00:14311 {L"..", L"."}, // yes, ".." technically lives in "."
[email protected]37088fef2008-08-15 17:32:10312#endif
initial.commitd7cae122008-07-26 21:49:38313};
314
[email protected]d6407d72012-02-17 03:32:09315TEST_F(FileUtilTest, CountFilesCreatedAfter) {
316 FilePath file_name =
317 temp_dir_.path().Append(FILE_PATH_LITERAL("f.txt"));
318 CreateTextFile(file_name, L"test");
initial.commitd7cae122008-07-26 21:49:38319
[email protected]d6407d72012-02-17 03:32:09320 base::PlatformFileInfo info;
321 file_util::GetFileInfo(file_name, &info);
322 base::Time file_time = info.creation_time;
initial.commitd7cae122008-07-26 21:49:38323
[email protected]d6407d72012-02-17 03:32:09324 base::TimeDelta two_secs = base::TimeDelta::FromSeconds(2);
325 base::Time after = file_time + two_secs;
326 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), after));
initial.commitd7cae122008-07-26 21:49:38327
[email protected]d6407d72012-02-17 03:32:09328 base::Time before = file_time - two_secs;
329 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(temp_dir_.path(), before));
initial.commitd7cae122008-07-26 21:49:38330
[email protected]d6407d72012-02-17 03:32:09331 // After deleting the file, shouldn't find it any more.
332 EXPECT_TRUE(file_util::Delete(file_name, false));
333 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), before));
334 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), after));
initial.commitd7cae122008-07-26 21:49:38335}
336
[email protected]c2c132c62010-03-24 21:56:26337TEST_F(FileUtilTest, FileAndDirectorySize) {
338 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
339 // should return 53 bytes.
[email protected]90314c0a82010-09-15 20:40:47340 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
[email protected]c2c132c62010-03-24 21:56:26341 CreateTextFile(file_01, L"12345678901234567890");
342 int64 size_f1 = 0;
343 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
344 EXPECT_EQ(20ll, size_f1);
345
[email protected]90314c0a82010-09-15 20:40:47346 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
[email protected]c2c132c62010-03-24 21:56:26347 file_util::CreateDirectory(subdir_path);
348
349 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
350 CreateTextFile(file_02, L"123456789012345678901234567890");
351 int64 size_f2 = 0;
352 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
353 EXPECT_EQ(30ll, size_f2);
354
355 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
356 file_util::CreateDirectory(subsubdir_path);
357
358 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
359 CreateTextFile(file_03, L"123");
360
[email protected]90314c0a82010-09-15 20:40:47361 int64 computed_size = file_util::ComputeDirectorySize(temp_dir_.path());
[email protected]c2c132c62010-03-24 21:56:26362 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
[email protected]a04876b92010-06-11 22:53:43363
[email protected]90314c0a82010-09-15 20:40:47364 computed_size =
365 file_util::ComputeFilesSize(temp_dir_.path(), FPL("The file*"));
[email protected]a04876b92010-06-11 22:53:43366 EXPECT_EQ(size_f1, computed_size);
367
[email protected]90314c0a82010-09-15 20:40:47368 computed_size = file_util::ComputeFilesSize(temp_dir_.path(), FPL("bla*"));
[email protected]a04876b92010-06-11 22:53:43369 EXPECT_EQ(0, computed_size);
[email protected]c2c132c62010-03-24 21:56:26370}
371
[email protected]6f5f4322010-06-09 22:56:48372TEST_F(FileUtilTest, NormalizeFilePathBasic) {
373 // Create a directory under the test dir. Because we create it,
374 // we know it is not a link.
[email protected]90314c0a82010-09-15 20:40:47375 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
376 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
[email protected]6f5f4322010-06-09 22:56:48377 FilePath file_b_path = dir_path.Append(FPL("file_b"));
378 file_util::CreateDirectory(dir_path);
[email protected]01e2a1f2010-05-12 15:13:57379
[email protected]6f5f4322010-06-09 22:56:48380 FilePath normalized_file_a_path, normalized_file_b_path;
381 ASSERT_FALSE(file_util::PathExists(file_a_path));
382 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
383 &normalized_file_a_path))
[email protected]e7afe2452010-08-22 16:19:13384 << "NormalizeFilePath() should fail on nonexistent paths.";
[email protected]6f5f4322010-06-09 22:56:48385
386 CreateTextFile(file_a_path, bogus_content);
387 ASSERT_TRUE(file_util::PathExists(file_a_path));
388 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
389 &normalized_file_a_path));
390
391 CreateTextFile(file_b_path, bogus_content);
392 ASSERT_TRUE(file_util::PathExists(file_b_path));
393 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
394 &normalized_file_b_path));
395
396 // Beacuse this test created |dir_path|, we know it is not a link
397 // or junction. So, the real path of the directory holding file a
398 // must be the parent of the path holding file b.
399 ASSERT_TRUE(normalized_file_a_path.DirName()
400 .IsParent(normalized_file_b_path.DirName()));
401}
402
403#if defined(OS_WIN)
404
405TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
406 // Build the following directory structure:
407 //
[email protected]90314c0a82010-09-15 20:40:47408 // temp_dir
[email protected]6f5f4322010-06-09 22:56:48409 // |-> base_a
410 // | |-> sub_a
411 // | |-> file.txt
412 // | |-> long_name___... (Very long name.)
413 // | |-> sub_long
414 // | |-> deep.txt
415 // |-> base_b
[email protected]90314c0a82010-09-15 20:40:47416 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
417 // |-> to_base_b (reparse point to temp_dir\base_b)
418 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
[email protected]6f5f4322010-06-09 22:56:48419
[email protected]90314c0a82010-09-15 20:40:47420 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
[email protected]6f5f4322010-06-09 22:56:48421 ASSERT_TRUE(file_util::CreateDirectory(base_a));
422
423 FilePath sub_a = base_a.Append(FPL("sub_a"));
424 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
425
426 FilePath file_txt = sub_a.Append(FPL("file.txt"));
427 CreateTextFile(file_txt, bogus_content);
428
429 // Want a directory whose name is long enough to make the path to the file
430 // inside just under MAX_PATH chars. This will be used to test that when
431 // a junction expands to a path over MAX_PATH chars in length,
432 // NormalizeFilePath() fails without crashing.
433 FilePath sub_long_rel(FPL("sub_long"));
434 FilePath deep_txt(FPL("deep.txt"));
435
436 int target_length = MAX_PATH;
437 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
438 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
[email protected]552b3152010-06-10 12:40:52439 // Without making the path a bit shorter, CreateDirectory() fails.
[email protected]6f5f4322010-06-09 22:56:48440 // the resulting path is still long enough to hit the failing case in
441 // NormalizePath().
442 const int kCreateDirLimit = 4;
443 target_length -= kCreateDirLimit;
444 FilePath::StringType long_name_str = FPL("long_name_");
445 long_name_str.resize(target_length, '_');
446
447 FilePath long_name = sub_a.Append(FilePath(long_name_str));
448 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
449 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
450
451 FilePath sub_long = deep_file.DirName();
452 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
453 CreateTextFile(deep_file, bogus_content);
454
[email protected]90314c0a82010-09-15 20:40:47455 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
[email protected]6f5f4322010-06-09 22:56:48456 ASSERT_TRUE(file_util::CreateDirectory(base_b));
457
458 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
459 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
[email protected]b90d7e802011-01-09 16:32:20460 base::win::ScopedHandle reparse_to_sub_a(
[email protected]6f5f4322010-06-09 22:56:48461 ::CreateFile(to_sub_a.value().c_str(),
462 FILE_ALL_ACCESS,
463 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
464 NULL,
465 OPEN_EXISTING,
466 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
467 NULL));
[email protected]094f9762010-08-03 03:51:56468 ASSERT_TRUE(reparse_to_sub_a.IsValid());
[email protected]6f5f4322010-06-09 22:56:48469 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
470
471 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
472 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
[email protected]b90d7e802011-01-09 16:32:20473 base::win::ScopedHandle reparse_to_base_b(
[email protected]6f5f4322010-06-09 22:56:48474 ::CreateFile(to_base_b.value().c_str(),
475 FILE_ALL_ACCESS,
476 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
477 NULL,
478 OPEN_EXISTING,
479 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
480 NULL));
[email protected]094f9762010-08-03 03:51:56481 ASSERT_TRUE(reparse_to_base_b.IsValid());
[email protected]6f5f4322010-06-09 22:56:48482 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
483
484 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
485 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
[email protected]b90d7e802011-01-09 16:32:20486 base::win::ScopedHandle reparse_to_sub_long(
[email protected]6f5f4322010-06-09 22:56:48487 ::CreateFile(to_sub_long.value().c_str(),
488 FILE_ALL_ACCESS,
489 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
490 NULL,
491 OPEN_EXISTING,
492 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
493 NULL));
[email protected]094f9762010-08-03 03:51:56494 ASSERT_TRUE(reparse_to_sub_long.IsValid());
[email protected]6f5f4322010-06-09 22:56:48495 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
496
497 // Normalize a junction free path: base_a\sub_a\file.txt .
498 FilePath normalized_path;
499 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
500 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
501
502 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
503 // the junction to_sub_a.
504 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
505 &normalized_path));
506 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
507
508 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
509 // normalized to exclude junctions to_base_b and to_sub_a .
510 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
511 .Append(FPL("to_base_b"))
512 .Append(FPL("to_sub_a"))
513 .Append(FPL("file.txt")),
514 &normalized_path));
515 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
516
517 // A long enough path will cause NormalizeFilePath() to fail. Make a long
518 // path using to_base_b many times, and check that paths long enough to fail
519 // do not cause a crash.
520 FilePath long_path = base_b;
521 const int kLengthLimit = MAX_PATH + 200;
522 while (long_path.value().length() <= kLengthLimit) {
523 long_path = long_path.Append(FPL("to_base_b"));
524 }
525 long_path = long_path.Append(FPL("to_sub_a"))
526 .Append(FPL("file.txt"));
527
528 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
529
530 // Normalizing the junction to deep.txt should fail, because the expanded
531 // path to deep.txt is longer than MAX_PATH.
532 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
533 &normalized_path));
534
535 // Delete the reparse points, and see that NormalizeFilePath() fails
536 // to traverse them.
537 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
538 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
539 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
540
541 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
542 &normalized_path));
543}
544
[email protected]71b7da72012-01-17 22:44:31545TEST_F(FileUtilTest, DevicePathToDriveLetter) {
546 // Get a drive letter.
547 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
548 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
549 LOG(ERROR) << "Can't get a drive letter to test with.";
550 return;
551 }
552
553 // Get the NT style path to that drive.
554 wchar_t device_path[MAX_PATH] = {'\0'};
555 ASSERT_TRUE(
556 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH));
557 FilePath actual_device_path(device_path);
558 FilePath win32_path;
559
560 // Run DevicePathToDriveLetterPath() on the NT style path we got from
561 // QueryDosDevice(). Expect the drive letter we started with.
562 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(actual_device_path,
563 &win32_path));
564 ASSERT_EQ(real_drive_letter, win32_path.value());
565
566 // Add some directories to the path. Expect those extra path componenets
567 // to be preserved.
568 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
569 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(
570 actual_device_path.Append(kRelativePath),
571 &win32_path));
572 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
573 win32_path.value());
574
575 // Deform the real path so that it is invalid by removing the last four
576 // characters. The way windows names devices that are hard disks
577 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
578 // than three characters. The only way the truncated string could be a
579 // real drive is if more than 10^3 disks are mounted:
580 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
581 // Check that DevicePathToDriveLetterPath fails.
582 int path_length = actual_device_path.value().length();
583 int new_length = path_length - 4;
584 ASSERT_LT(0, new_length);
585 FilePath prefix_of_real_device_path(
586 actual_device_path.value().substr(0, new_length));
587 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
588 prefix_of_real_device_path,
589 &win32_path));
590
591 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
592 prefix_of_real_device_path.Append(kRelativePath),
593 &win32_path));
594
595 // Deform the real path so that it is invalid by adding some characters. For
596 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
597 // request for the drive letter whose native path is
598 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
599 // because drives are numbered in order and mounting 112345 hard disks will
600 // never happen.
601 const FilePath::StringType kExtraChars = FPL("12345");
602
603 FilePath real_device_path_plus_numbers(
604 actual_device_path.value() + kExtraChars);
605
606 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
607 real_device_path_plus_numbers,
608 &win32_path));
609
610 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
611 real_device_path_plus_numbers.Append(kRelativePath),
612 &win32_path));
613}
614
[email protected]23b59d22011-12-29 22:59:22615TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) {
616 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test"));
617 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
618 base::win::ScopedHandle dir(
619 ::CreateFile(empty_dir.value().c_str(),
620 FILE_ALL_ACCESS,
621 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
622 NULL,
623 OPEN_EXISTING,
624 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
625 NULL));
626 ASSERT_TRUE(dir.IsValid());
627 base::PlatformFileInfo info;
628 EXPECT_TRUE(base::GetPlatformFileInfo(dir.Get(), &info));
629 EXPECT_TRUE(info.is_directory);
630 EXPECT_FALSE(info.is_symbolic_link);
631 EXPECT_EQ(0, info.size);
632}
633
[email protected]6f5f4322010-06-09 22:56:48634#endif // defined(OS_WIN)
635
[email protected]2e733d102010-11-30 00:43:37636#if defined(OS_POSIX)
637
638TEST_F(FileUtilTest, CreateAndReadSymlinks) {
639 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
640 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
641 CreateTextFile(link_to, bogus_content);
642
643 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
644 << "Failed to create file symlink.";
645
646 // If we created the link properly, we should be able to read the
647 // contents through it.
648 std::wstring contents = ReadTextFile(link_from);
649 ASSERT_EQ(contents, bogus_content);
650
651 FilePath result;
652 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from, &result));
653 ASSERT_EQ(link_to.value(), result.value());
654
655 // Link to a directory.
656 link_from = temp_dir_.path().Append(FPL("from_dir"));
657 link_to = temp_dir_.path().Append(FPL("to_dir"));
658 file_util::CreateDirectory(link_to);
659
660 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
661 << "Failed to create directory symlink.";
662
663 // Test failures.
664 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to));
665 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result));
666 FilePath missing = temp_dir_.path().Append(FPL("missing"));
667 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result));
668}
669
670
[email protected]6f5f4322010-06-09 22:56:48671// The following test of NormalizeFilePath() require that we create a symlink.
[email protected]2e733d102010-11-30 00:43:37672// This can not be done on Windows before Vista. On Vista, creating a symlink
[email protected]6f5f4322010-06-09 22:56:48673// requires privilege "SeCreateSymbolicLinkPrivilege".
674// TODO(skerner): Investigate the possibility of giving base_unittests the
675// privileges required to create a symlink.
[email protected]6f5f4322010-06-09 22:56:48676TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
677 FilePath normalized_path;
[email protected]01e2a1f2010-05-12 15:13:57678
679 // Link one file to another.
[email protected]90314c0a82010-09-15 20:40:47680 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
681 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
[email protected]01e2a1f2010-05-12 15:13:57682 CreateTextFile(link_to, bogus_content);
683
[email protected]2e733d102010-11-30 00:43:37684 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57685 << "Failed to create file symlink.";
686
[email protected]6f5f4322010-06-09 22:56:48687 // Check that NormalizeFilePath sees the link.
688 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57689 ASSERT_TRUE(link_to != link_from);
[email protected]6f5f4322010-06-09 22:56:48690 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
691 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
[email protected]01e2a1f2010-05-12 15:13:57692
693 // Link to a directory.
[email protected]90314c0a82010-09-15 20:40:47694 link_from = temp_dir_.path().Append(FPL("from_dir"));
695 link_to = temp_dir_.path().Append(FPL("to_dir"));
[email protected]01e2a1f2010-05-12 15:13:57696 file_util::CreateDirectory(link_to);
697
[email protected]2e733d102010-11-30 00:43:37698 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57699 << "Failed to create directory symlink.";
700
[email protected]6f5f4322010-06-09 22:56:48701 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
702 << "Links to directories should return false.";
[email protected]01e2a1f2010-05-12 15:13:57703
[email protected]6f5f4322010-06-09 22:56:48704 // Test that a loop in the links causes NormalizeFilePath() to return false.
[email protected]90314c0a82010-09-15 20:40:47705 link_from = temp_dir_.path().Append(FPL("link_a"));
706 link_to = temp_dir_.path().Append(FPL("link_b"));
[email protected]2e733d102010-11-30 00:43:37707 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57708 << "Failed to create loop symlink a.";
[email protected]2e733d102010-11-30 00:43:37709 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from, link_to))
[email protected]01e2a1f2010-05-12 15:13:57710 << "Failed to create loop symlink b.";
711
712 // Infinite loop!
[email protected]6f5f4322010-06-09 22:56:48713 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57714}
715#endif // defined(OS_POSIX)
716
[email protected]83d86252010-05-07 18:58:45717TEST_F(FileUtilTest, DeleteNonExistent) {
[email protected]90314c0a82010-09-15 20:40:47718 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
[email protected]83d86252010-05-07 18:58:45719 ASSERT_FALSE(file_util::PathExists(non_existent));
[email protected]3a51cfdd2010-05-07 00:05:36720
[email protected]83d86252010-05-07 18:58:45721 EXPECT_TRUE(file_util::Delete(non_existent, false));
722 ASSERT_FALSE(file_util::PathExists(non_existent));
723 EXPECT_TRUE(file_util::Delete(non_existent, true));
724 ASSERT_FALSE(file_util::PathExists(non_existent));
725}
726
727TEST_F(FileUtilTest, DeleteFile) {
728 // Create a file
[email protected]90314c0a82010-09-15 20:40:47729 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
[email protected]83d86252010-05-07 18:58:45730 CreateTextFile(file_name, bogus_content);
initial.commitd7cae122008-07-26 21:49:38731 ASSERT_TRUE(file_util::PathExists(file_name));
732
[email protected]83d86252010-05-07 18:58:45733 // Make sure it's deleted
734 EXPECT_TRUE(file_util::Delete(file_name, false));
735 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36736
[email protected]83d86252010-05-07 18:58:45737 // Test recursive case, create a new file
[email protected]90314c0a82010-09-15 20:40:47738 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
[email protected]83d86252010-05-07 18:58:45739 CreateTextFile(file_name, bogus_content);
740 ASSERT_TRUE(file_util::PathExists(file_name));
741
742 // Make sure it's deleted
743 EXPECT_TRUE(file_util::Delete(file_name, true));
744 EXPECT_FALSE(file_util::PathExists(file_name));
745}
746
747#if defined(OS_WIN)
748// Tests that the Delete function works for wild cards, especially
749// with the recursion flag. Also coincidentally tests PathExists.
750// TODO(erikkay): see if anyone's actually using this feature of the API
751TEST_F(FileUtilTest, DeleteWildCard) {
752 // Create a file and a directory
[email protected]90314c0a82010-09-15 20:40:47753 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
[email protected]83d86252010-05-07 18:58:45754 CreateTextFile(file_name, bogus_content);
755 ASSERT_TRUE(file_util::PathExists(file_name));
756
[email protected]90314c0a82010-09-15 20:40:47757 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
[email protected]83d86252010-05-07 18:58:45758 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38759 ASSERT_TRUE(file_util::PathExists(subdir_path));
760
[email protected]83d86252010-05-07 18:58:45761 // Create the wildcard path
[email protected]90314c0a82010-09-15 20:40:47762 FilePath directory_contents = temp_dir_.path();
[email protected]83d86252010-05-07 18:58:45763 directory_contents = directory_contents.Append(FPL("*"));
764
initial.commitd7cae122008-07-26 21:49:38765 // Delete non-recursively and check that only the file is deleted
[email protected]83d86252010-05-07 18:58:45766 EXPECT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10767 EXPECT_FALSE(file_util::PathExists(file_name));
768 EXPECT_TRUE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40769
[email protected]3a51cfdd2010-05-07 00:05:36770 // Delete recursively and make sure all contents are deleted
[email protected]83d86252010-05-07 18:58:45771 EXPECT_TRUE(file_util::Delete(directory_contents, true));
[email protected]dde46b62010-05-06 21:56:40772 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36773 EXPECT_FALSE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40774}
775
[email protected]83d86252010-05-07 18:58:45776// TODO(erikkay): see if anyone's actually using this feature of the API
777TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
778 // Create a file and a directory
[email protected]90314c0a82010-09-15 20:40:47779 FilePath subdir_path =
780 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
[email protected]83d86252010-05-07 18:58:45781 file_util::CreateDirectory(subdir_path);
782 ASSERT_TRUE(file_util::PathExists(subdir_path));
783
784 // Create the wildcard path
785 FilePath directory_contents = subdir_path;
786 directory_contents = directory_contents.Append(FPL("*"));
787
788 // Delete non-recursively and check nothing got deleted
789 EXPECT_TRUE(file_util::Delete(directory_contents, false));
790 EXPECT_TRUE(file_util::PathExists(subdir_path));
791
792 // Delete recursively and check nothing got deleted
793 EXPECT_TRUE(file_util::Delete(directory_contents, true));
794 EXPECT_TRUE(file_util::PathExists(subdir_path));
795}
796#endif
797
798// Tests non-recursive Delete() for a directory.
799TEST_F(FileUtilTest, DeleteDirNonRecursive) {
800 // Create a subdirectory and put a file and two directories inside.
[email protected]90314c0a82010-09-15 20:40:47801 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
[email protected]83d86252010-05-07 18:58:45802 file_util::CreateDirectory(test_subdir);
803 ASSERT_TRUE(file_util::PathExists(test_subdir));
804
805 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
806 CreateTextFile(file_name, bogus_content);
807 ASSERT_TRUE(file_util::PathExists(file_name));
808
809 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
810 file_util::CreateDirectory(subdir_path1);
811 ASSERT_TRUE(file_util::PathExists(subdir_path1));
812
813 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
814 file_util::CreateDirectory(subdir_path2);
815 ASSERT_TRUE(file_util::PathExists(subdir_path2));
816
817 // Delete non-recursively and check that the empty dir got deleted
818 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
819 EXPECT_FALSE(file_util::PathExists(subdir_path2));
820
821 // Delete non-recursively and check that nothing got deleted
822 EXPECT_FALSE(file_util::Delete(test_subdir, false));
823 EXPECT_TRUE(file_util::PathExists(test_subdir));
824 EXPECT_TRUE(file_util::PathExists(file_name));
825 EXPECT_TRUE(file_util::PathExists(subdir_path1));
826}
827
828// Tests recursive Delete() for a directory.
829TEST_F(FileUtilTest, DeleteDirRecursive) {
830 // Create a subdirectory and put a file and two directories inside.
[email protected]90314c0a82010-09-15 20:40:47831 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
[email protected]83d86252010-05-07 18:58:45832 file_util::CreateDirectory(test_subdir);
833 ASSERT_TRUE(file_util::PathExists(test_subdir));
834
835 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
836 CreateTextFile(file_name, bogus_content);
837 ASSERT_TRUE(file_util::PathExists(file_name));
838
839 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
840 file_util::CreateDirectory(subdir_path1);
841 ASSERT_TRUE(file_util::PathExists(subdir_path1));
842
843 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
844 file_util::CreateDirectory(subdir_path2);
845 ASSERT_TRUE(file_util::PathExists(subdir_path2));
846
847 // Delete recursively and check that the empty dir got deleted
848 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
849 EXPECT_FALSE(file_util::PathExists(subdir_path2));
850
851 // Delete recursively and check that everything got deleted
852 EXPECT_TRUE(file_util::Delete(test_subdir, true));
853 EXPECT_FALSE(file_util::PathExists(file_name));
854 EXPECT_FALSE(file_util::PathExists(subdir_path1));
855 EXPECT_FALSE(file_util::PathExists(test_subdir));
856}
857
[email protected]bc6a9012009-10-15 01:11:44858TEST_F(FileUtilTest, MoveFileNew) {
859 // Create a file
860 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47861 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44862 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
863 ASSERT_TRUE(file_util::PathExists(file_name_from));
864
[email protected]90314c0a82010-09-15 20:40:47865 // The destination.
866 FilePath file_name_to = temp_dir_.path().Append(
867 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:44868 ASSERT_FALSE(file_util::PathExists(file_name_to));
869
870 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
871
872 // Check everything has been moved.
873 EXPECT_FALSE(file_util::PathExists(file_name_from));
874 EXPECT_TRUE(file_util::PathExists(file_name_to));
875}
876
877TEST_F(FileUtilTest, MoveFileExists) {
878 // Create a file
879 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47880 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44881 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
882 ASSERT_TRUE(file_util::PathExists(file_name_from));
883
[email protected]90314c0a82010-09-15 20:40:47884 // The destination name.
885 FilePath file_name_to = temp_dir_.path().Append(
886 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:44887 CreateTextFile(file_name_to, L"Old file content");
888 ASSERT_TRUE(file_util::PathExists(file_name_to));
889
890 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
891
892 // Check everything has been moved.
893 EXPECT_FALSE(file_util::PathExists(file_name_from));
894 EXPECT_TRUE(file_util::PathExists(file_name_to));
895 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
896}
897
898TEST_F(FileUtilTest, MoveFileDirExists) {
899 // Create a file
900 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47901 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44902 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
903 ASSERT_TRUE(file_util::PathExists(file_name_from));
904
905 // The destination directory
906 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:47907 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]bc6a9012009-10-15 01:11:44908 file_util::CreateDirectory(dir_name_to);
909 ASSERT_TRUE(file_util::PathExists(dir_name_to));
910
911 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
912}
913
914
[email protected]abbc5732009-10-13 17:57:27915TEST_F(FileUtilTest, MoveNew) {
initial.commitd7cae122008-07-26 21:49:38916 // Create a directory
[email protected]640517f2008-10-30 23:54:04917 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47918 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
[email protected]640517f2008-10-30 23:54:04919 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38920 ASSERT_TRUE(file_util::PathExists(dir_name_from));
921
922 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04923 FilePath file_name_from =
924 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38925 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
926 ASSERT_TRUE(file_util::PathExists(file_name_from));
927
[email protected]90314c0a82010-09-15 20:40:47928 // Move the directory.
929 FilePath dir_name_to =
930 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
[email protected]640517f2008-10-30 23:54:04931 FilePath file_name_to =
932 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38933
934 ASSERT_FALSE(file_util::PathExists(dir_name_to));
935
936 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
937
938 // Check everything has been moved.
939 EXPECT_FALSE(file_util::PathExists(dir_name_from));
940 EXPECT_FALSE(file_util::PathExists(file_name_from));
941 EXPECT_TRUE(file_util::PathExists(dir_name_to));
942 EXPECT_TRUE(file_util::PathExists(file_name_to));
943}
944
[email protected]abbc5732009-10-13 17:57:27945TEST_F(FileUtilTest, MoveExist) {
946 // Create a directory
947 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47948 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:27949 file_util::CreateDirectory(dir_name_from);
950 ASSERT_TRUE(file_util::PathExists(dir_name_from));
951
952 // Create a file under the directory
953 FilePath file_name_from =
954 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
955 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
956 ASSERT_TRUE(file_util::PathExists(file_name_from));
957
958 // Move the directory
959 FilePath dir_name_exists =
[email protected]90314c0a82010-09-15 20:40:47960 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]abbc5732009-10-13 17:57:27961
962 FilePath dir_name_to =
963 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
964 FilePath file_name_to =
965 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
966
967 // Create the destination directory.
968 file_util::CreateDirectory(dir_name_exists);
969 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
970
971 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
972
973 // Check everything has been moved.
974 EXPECT_FALSE(file_util::PathExists(dir_name_from));
975 EXPECT_FALSE(file_util::PathExists(file_name_from));
976 EXPECT_TRUE(file_util::PathExists(dir_name_to));
977 EXPECT_TRUE(file_util::PathExists(file_name_to));
978}
979
980TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commitd7cae122008-07-26 21:49:38981 // Create a directory.
[email protected]640517f2008-10-30 23:54:04982 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47983 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:04984 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38985 ASSERT_TRUE(file_util::PathExists(dir_name_from));
986
987 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04988 FilePath file_name_from =
989 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38990 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
991 ASSERT_TRUE(file_util::PathExists(file_name_from));
992
993 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04994 FilePath subdir_name_from =
995 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
996 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38997 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
998
999 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:041000 FilePath file_name2_from =
1001 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381002 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1003 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1004
1005 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:041006 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471007 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]640517f2008-10-30 23:54:041008 FilePath file_name_to =
1009 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1010 FilePath subdir_name_to =
1011 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1012 FilePath file_name2_to =
1013 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381014
1015 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1016
1017 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
1018
1019 // Check everything has been copied.
1020 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1021 EXPECT_TRUE(file_util::PathExists(file_name_from));
1022 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1023 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1024 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1025 EXPECT_TRUE(file_util::PathExists(file_name_to));
1026 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1027 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1028}
1029
[email protected]abbc5732009-10-13 17:57:271030TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1031 // Create a directory.
1032 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471033 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:271034 file_util::CreateDirectory(dir_name_from);
1035 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1036
1037 // Create a file under the directory.
1038 FilePath file_name_from =
1039 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1040 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1041 ASSERT_TRUE(file_util::PathExists(file_name_from));
1042
1043 // Create a subdirectory.
1044 FilePath subdir_name_from =
1045 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1046 file_util::CreateDirectory(subdir_name_from);
1047 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1048
1049 // Create a file under the subdirectory.
1050 FilePath file_name2_from =
1051 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1052 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1053 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1054
1055 // Copy the directory recursively.
1056 FilePath dir_name_exists =
[email protected]90314c0a82010-09-15 20:40:471057 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]abbc5732009-10-13 17:57:271058
1059 FilePath dir_name_to =
1060 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1061 FilePath file_name_to =
1062 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1063 FilePath subdir_name_to =
1064 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1065 FilePath file_name2_to =
1066 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1067
1068 // Create the destination directory.
1069 file_util::CreateDirectory(dir_name_exists);
1070 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1071
1072 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1073
1074 // Check everything has been copied.
1075 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1076 EXPECT_TRUE(file_util::PathExists(file_name_from));
1077 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1078 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1079 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1080 EXPECT_TRUE(file_util::PathExists(file_name_to));
1081 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1082 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1083}
1084
1085TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commitd7cae122008-07-26 21:49:381086 // Create a directory.
[email protected]640517f2008-10-30 23:54:041087 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471088 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:041089 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381090 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1091
1092 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:041093 FilePath file_name_from =
1094 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381095 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1096 ASSERT_TRUE(file_util::PathExists(file_name_from));
1097
1098 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:041099 FilePath subdir_name_from =
1100 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1101 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:381102 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1103
1104 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:041105 FilePath file_name2_from =
1106 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381107 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1108 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1109
1110 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:041111 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471112 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]640517f2008-10-30 23:54:041113 FilePath file_name_to =
1114 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1115 FilePath subdir_name_to =
1116 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commitd7cae122008-07-26 21:49:381117
1118 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1119
1120 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1121
1122 // Check everything has been copied.
1123 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1124 EXPECT_TRUE(file_util::PathExists(file_name_from));
1125 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1126 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1127 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1128 EXPECT_TRUE(file_util::PathExists(file_name_to));
1129 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1130}
1131
[email protected]abbc5732009-10-13 17:57:271132TEST_F(FileUtilTest, CopyDirectoryExists) {
1133 // Create a directory.
1134 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471135 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:271136 file_util::CreateDirectory(dir_name_from);
1137 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1138
1139 // Create a file under the directory.
1140 FilePath file_name_from =
1141 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1142 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1143 ASSERT_TRUE(file_util::PathExists(file_name_from));
1144
1145 // Create a subdirectory.
1146 FilePath subdir_name_from =
1147 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1148 file_util::CreateDirectory(subdir_name_from);
1149 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1150
1151 // Create a file under the subdirectory.
1152 FilePath file_name2_from =
1153 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1154 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1155 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1156
1157 // Copy the directory not recursively.
1158 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471159 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]abbc5732009-10-13 17:57:271160 FilePath file_name_to =
1161 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1162 FilePath subdir_name_to =
1163 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1164
1165 // Create the destination directory.
1166 file_util::CreateDirectory(dir_name_to);
1167 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1168
1169 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1170
1171 // Check everything has been copied.
1172 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1173 EXPECT_TRUE(file_util::PathExists(file_name_from));
1174 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1175 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1176 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1177 EXPECT_TRUE(file_util::PathExists(file_name_to));
1178 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1179}
1180
[email protected]bc6a9012009-10-15 01:11:441181TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1182 // Create a file
1183 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471184 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441185 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1186 ASSERT_TRUE(file_util::PathExists(file_name_from));
1187
1188 // The destination name
[email protected]90314c0a82010-09-15 20:40:471189 FilePath file_name_to = temp_dir_.path().Append(
1190 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:441191 ASSERT_FALSE(file_util::PathExists(file_name_to));
1192
1193 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1194
1195 // Check the has been copied
1196 EXPECT_TRUE(file_util::PathExists(file_name_to));
1197}
1198
1199TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1200 // Create a file
1201 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471202 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441203 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1204 ASSERT_TRUE(file_util::PathExists(file_name_from));
1205
1206 // The destination name
[email protected]90314c0a82010-09-15 20:40:471207 FilePath file_name_to = temp_dir_.path().Append(
1208 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:441209 CreateTextFile(file_name_to, L"Old file content");
1210 ASSERT_TRUE(file_util::PathExists(file_name_to));
1211
1212 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1213
1214 // Check the has been copied
1215 EXPECT_TRUE(file_util::PathExists(file_name_to));
1216 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1217}
1218
1219TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1220 // Create a file
1221 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471222 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441223 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1224 ASSERT_TRUE(file_util::PathExists(file_name_from));
1225
1226 // The destination
1227 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471228 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]bc6a9012009-10-15 01:11:441229 file_util::CreateDirectory(dir_name_to);
1230 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1231 FilePath file_name_to =
1232 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1233
1234 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1235
1236 // Check the has been copied
1237 EXPECT_TRUE(file_util::PathExists(file_name_to));
1238}
1239
initial.commitd7cae122008-07-26 21:49:381240TEST_F(FileUtilTest, CopyFile) {
1241 // Create a directory
[email protected]640517f2008-10-30 23:54:041242 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471243 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:041244 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381245 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1246
1247 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:041248 FilePath file_name_from =
1249 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381250 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1251 CreateTextFile(file_name_from, file_contents);
1252 ASSERT_TRUE(file_util::PathExists(file_name_from));
1253
1254 // Copy the file.
[email protected]640517f2008-10-30 23:54:041255 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:381256 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:111257
[email protected]37088fef2008-08-15 17:32:101258 // Copy the file to another location using '..' in the path.
[email protected]53c58042009-08-26 20:00:141259 FilePath dest_file2(dir_name_from);
1260 dest_file2 = dest_file2.AppendASCII("..");
1261 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1262 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1263
1264 FilePath dest_file2_test(dir_name_from);
1265 dest_file2_test = dest_file2_test.DirName();
1266 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commitd7cae122008-07-26 21:49:381267
1268 // Check everything has been copied.
1269 EXPECT_TRUE(file_util::PathExists(file_name_from));
1270 EXPECT_TRUE(file_util::PathExists(dest_file));
1271 const std::wstring read_contents = ReadTextFile(dest_file);
1272 EXPECT_EQ(file_contents, read_contents);
[email protected]53c58042009-08-26 20:00:141273 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1274 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commitd7cae122008-07-26 21:49:381275}
1276
[email protected]37088fef2008-08-15 17:32:101277// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:131278#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381279TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]90314c0a82010-09-15 20:40:471280 FilePath file_name = temp_dir_.path().Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:381281
1282 SYSTEMTIME start_time;
1283 GetLocalTime(&start_time);
[email protected]a1b75b942011-12-31 22:53:511284 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
initial.commitd7cae122008-07-26 21:49:381285 CreateTextFile(file_name, L"New file!");
[email protected]a1b75b942011-12-31 22:53:511286 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
initial.commitd7cae122008-07-26 21:49:381287 SYSTEMTIME end_time;
1288 GetLocalTime(&end_time);
1289
1290 SYSTEMTIME file_creation_time;
[email protected]640517f2008-10-30 23:54:041291 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:381292
1293 FILETIME start_filetime;
1294 SystemTimeToFileTime(&start_time, &start_filetime);
1295 FILETIME end_filetime;
1296 SystemTimeToFileTime(&end_time, &end_filetime);
1297 FILETIME file_creation_filetime;
1298 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1299
1300 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1301 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1302 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1303
1304 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1305 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1306 "end time: " << FileTimeAsUint64(end_filetime);
1307
[email protected]640517f2008-10-30 23:54:041308 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:381309}
[email protected]37088fef2008-08-15 17:32:101310#endif
initial.commitd7cae122008-07-26 21:49:381311
[email protected]ed2f2332008-08-20 15:59:491312// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:041313// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:491314typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:381315
[email protected]ed2f2332008-08-20 15:59:491316TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:041317 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:381318 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:041319 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1320 .Append(FILE_PATH_LITERAL("data"))
1321 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:381322 ASSERT_TRUE(file_util::PathExists(data_dir));
1323
[email protected]640517f2008-10-30 23:54:041324 FilePath original_file =
1325 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1326 FilePath same_file =
1327 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1328 FilePath same_length_file =
1329 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1330 FilePath different_file =
1331 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1332 FilePath different_first_file =
1333 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1334 FilePath different_last_file =
1335 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1336 FilePath empty1_file =
1337 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1338 FilePath empty2_file =
1339 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1340 FilePath shortened_file =
1341 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1342 FilePath binary_file =
1343 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1344 FilePath binary_file_same =
1345 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1346 FilePath binary_file_diff =
1347 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commitd7cae122008-07-26 21:49:381348
1349 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1350 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1351 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1352 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
[email protected]3f4b5712009-12-01 22:14:221353 EXPECT_FALSE(file_util::ContentsEqual(
1354 FilePath(FILE_PATH_LITERAL("bogusname")),
1355 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commitd7cae122008-07-26 21:49:381356 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1357 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1358 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1359 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1360 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1361 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1362 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1363}
1364
[email protected]b81637c32009-06-26 21:17:241365TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1366 FilePath data_dir;
1367 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1368 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1369 .Append(FILE_PATH_LITERAL("data"))
1370 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1371 ASSERT_TRUE(file_util::PathExists(data_dir));
1372
1373 FilePath original_file =
1374 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1375 FilePath same_file =
1376 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1377 FilePath crlf_file =
1378 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1379 FilePath shortened_file =
1380 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1381 FilePath different_file =
1382 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1383 FilePath different_first_file =
1384 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1385 FilePath different_last_file =
1386 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1387 FilePath first1_file =
1388 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1389 FilePath first2_file =
1390 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1391 FilePath empty1_file =
1392 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1393 FilePath empty2_file =
1394 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1395 FilePath blank_line_file =
1396 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1397 FilePath blank_line_crlf_file =
1398 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1399
1400 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1401 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1402 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1403 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1404 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1405 different_first_file));
1406 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1407 different_last_file));
1408 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1409 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1410 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1411 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1412 blank_line_crlf_file));
1413}
1414
[email protected]37088fef2008-08-15 17:32:101415// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:131416#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381417TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]90314c0a82010-09-15 20:40:471418 FilePath target_file = temp_dir_.path().Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:381419 CreateTextFile(target_file, L"This is the target.");
1420
[email protected]90314c0a82010-09-15 20:40:471421 FilePath link_file = temp_dir_.path().Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:381422
1423 HRESULT result;
[email protected]b2f28d22012-03-03 01:54:351424 IShellLink* shell = NULL;
1425 IPersistFile* persist = NULL;
initial.commitd7cae122008-07-26 21:49:381426
1427 CoInitialize(NULL);
1428 // Temporarily create a shortcut for test
1429 result = CoCreateInstance(CLSID_ShellLink, NULL,
1430 CLSCTX_INPROC_SERVER, IID_IShellLink,
1431 reinterpret_cast<LPVOID*>(&shell));
1432 EXPECT_TRUE(SUCCEEDED(result));
1433 result = shell->QueryInterface(IID_IPersistFile,
1434 reinterpret_cast<LPVOID*>(&persist));
1435 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041436 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381437 EXPECT_TRUE(SUCCEEDED(result));
1438 result = shell->SetDescription(L"ResolveShortcutTest");
1439 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041440 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:381441 EXPECT_TRUE(SUCCEEDED(result));
1442 if (persist)
1443 persist->Release();
1444 if (shell)
1445 shell->Release();
1446
1447 bool is_solved;
[email protected]fd061a62009-08-25 01:51:441448 is_solved = file_util::ResolveShortcut(&link_file);
initial.commitd7cae122008-07-26 21:49:381449 EXPECT_TRUE(is_solved);
1450 std::wstring contents;
[email protected]fd061a62009-08-25 01:51:441451 contents = ReadTextFile(link_file);
initial.commitd7cae122008-07-26 21:49:381452 EXPECT_EQ(L"This is the target.", contents);
1453
[email protected]d324ab332008-08-18 16:00:381454 // Cleaning
[email protected]640517f2008-10-30 23:54:041455 DeleteFile(target_file.value().c_str());
[email protected]fd061a62009-08-25 01:51:441456 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381457 CoUninitialize();
1458}
1459
1460TEST_F(FileUtilTest, CreateShortcutTest) {
1461 const wchar_t file_contents[] = L"This is another target.";
[email protected]90314c0a82010-09-15 20:40:471462 FilePath target_file = temp_dir_.path().Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:381463 CreateTextFile(target_file, file_contents);
1464
[email protected]90314c0a82010-09-15 20:40:471465 FilePath link_file = temp_dir_.path().Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:381466
1467 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:041468 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1469 link_file.value().c_str(),
[email protected]86b54012009-11-19 09:18:501470 NULL, NULL, NULL, NULL, 0, NULL));
[email protected]fd061a62009-08-25 01:51:441471 FilePath resolved_name = link_file;
initial.commitd7cae122008-07-26 21:49:381472 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]fd061a62009-08-25 01:51:441473 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commitd7cae122008-07-26 21:49:381474 EXPECT_EQ(file_contents, read_contents);
1475
[email protected]640517f2008-10-30 23:54:041476 DeleteFile(target_file.value().c_str());
1477 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381478 CoUninitialize();
1479}
[email protected]2c59af7dca2009-03-11 18:37:481480
1481TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1482 // Create a directory
1483 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471484 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
[email protected]2c59af7dca2009-03-11 18:37:481485 file_util::CreateDirectory(dir_name_from);
1486 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1487
1488 // Create a file under the directory
1489 FilePath file_name_from =
1490 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1491 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1492 ASSERT_TRUE(file_util::PathExists(file_name_from));
1493
1494 // Move the directory by using CopyAndDeleteDirectory
[email protected]90314c0a82010-09-15 20:40:471495 FilePath dir_name_to = temp_dir_.path().Append(
[email protected]2c59af7dca2009-03-11 18:37:481496 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1497 FilePath file_name_to =
1498 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1499
1500 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1501
1502 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1503
1504 // Check everything has been moved.
1505 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1506 EXPECT_FALSE(file_util::PathExists(file_name_from));
1507 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1508 EXPECT_TRUE(file_util::PathExists(file_name_to));
1509}
[email protected]7d95aae2009-10-09 07:33:391510
1511TEST_F(FileUtilTest, GetTempDirTest) {
1512 static const TCHAR* kTmpKey = _T("TMP");
1513 static const TCHAR* kTmpValues[] = {
1514 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1515 };
1516 // Save the original $TMP.
1517 size_t original_tmp_size;
1518 TCHAR* original_tmp;
1519 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1520 // original_tmp may be NULL.
1521
1522 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1523 FilePath path;
1524 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1525 file_util::GetTempDir(&path);
1526 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1527 " result=" << path.value();
1528 }
1529
1530 // Restore the original $TMP.
1531 if (original_tmp) {
1532 ::_tputenv_s(kTmpKey, original_tmp);
1533 free(original_tmp);
1534 } else {
1535 ::_tputenv_s(kTmpKey, _T(""));
1536 }
1537}
1538#endif // OS_WIN
initial.commitd7cae122008-07-26 21:49:381539
[email protected]33edeab2009-08-18 16:07:551540TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1541 FilePath temp_files[3];
[email protected]9e51af92009-02-04 00:58:391542 for (int i = 0; i < 3; i++) {
[email protected]33edeab2009-08-18 16:07:551543 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
[email protected]9e51af92009-02-04 00:58:391544 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1545 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1546 }
1547 for (int i = 0; i < 3; i++)
1548 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1549 for (int i = 0; i < 3; i++)
1550 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1551}
1552
[email protected]33edeab2009-08-18 16:07:551553TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
[email protected]9e51af92009-02-04 00:58:391554 FilePath names[3];
[email protected]b2f28d22012-03-03 01:54:351555 FILE* fps[3];
[email protected]9e51af92009-02-04 00:58:391556 int i;
1557
1558 // Create; make sure they are open and exist.
1559 for (i = 0; i < 3; ++i) {
1560 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1561 ASSERT_TRUE(fps[i]);
1562 EXPECT_TRUE(file_util::PathExists(names[i]));
1563 }
1564
1565 // Make sure all names are unique.
1566 for (i = 0; i < 3; ++i) {
1567 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1568 }
1569
1570 // Close and delete.
1571 for (i = 0; i < 3; ++i) {
1572 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1573 EXPECT_TRUE(file_util::Delete(names[i], false));
1574 }
initial.commitd7cae122008-07-26 21:49:381575}
1576
1577TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
[email protected]53c58042009-08-26 20:00:141578 FilePath temp_dir;
1579 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1580 &temp_dir));
[email protected]806b9c62008-09-11 16:09:111581 EXPECT_TRUE(file_util::PathExists(temp_dir));
1582 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:381583}
1584
[email protected]b0b3abd92010-04-30 17:00:091585TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1586 FilePath new_dir;
1587 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
[email protected]90314c0a82010-09-15 20:40:471588 temp_dir_.path(),
[email protected]b0b3abd92010-04-30 17:00:091589 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
[email protected]046062e82010-06-30 07:19:111590 &new_dir));
[email protected]b0b3abd92010-04-30 17:00:091591 EXPECT_TRUE(file_util::PathExists(new_dir));
[email protected]90314c0a82010-09-15 20:40:471592 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
[email protected]b0b3abd92010-04-30 17:00:091593 EXPECT_TRUE(file_util::Delete(new_dir, false));
1594}
1595
[email protected]9e51af92009-02-04 00:58:391596TEST_F(FileUtilTest, GetShmemTempDirTest) {
1597 FilePath dir;
[email protected]103dccfa2011-12-06 18:07:051598 EXPECT_TRUE(file_util::GetShmemTempDir(&dir, false));
[email protected]9e51af92009-02-04 00:58:391599 EXPECT_TRUE(file_util::DirectoryExists(dir));
1600}
1601
initial.commitd7cae122008-07-26 21:49:381602TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:041603 FilePath test_root =
[email protected]90314c0a82010-09-15 20:40:471604 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:131605#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:041606 FilePath test_path =
1607 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:101608#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:041609 FilePath test_path =
1610 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:101611#endif
[email protected]806b9c62008-09-11 16:09:111612
1613 EXPECT_FALSE(file_util::PathExists(test_path));
1614 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1615 EXPECT_TRUE(file_util::PathExists(test_path));
1616 // CreateDirectory returns true if the DirectoryExists returns true.
1617 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1618
1619 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:041620 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111621 EXPECT_FALSE(file_util::PathExists(test_path));
1622 CreateTextFile(test_path, L"test file");
1623 EXPECT_TRUE(file_util::PathExists(test_path));
1624 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1625
1626 EXPECT_TRUE(file_util::Delete(test_root, true));
1627 EXPECT_FALSE(file_util::PathExists(test_root));
1628 EXPECT_FALSE(file_util::PathExists(test_path));
[email protected]40676ab2009-11-27 14:54:411629
1630 // Verify assumptions made by the Windows implementation:
1631 // 1. The current directory always exists.
1632 // 2. The root directory always exists.
1633 ASSERT_TRUE(file_util::DirectoryExists(
1634 FilePath(FilePath::kCurrentDirectory)));
1635 FilePath top_level = test_root;
1636 while (top_level != top_level.DirName()) {
1637 top_level = top_level.DirName();
1638 }
1639 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1640
1641 // Given these assumptions hold, it should be safe to
1642 // test that "creating" these directories succeeds.
1643 EXPECT_TRUE(file_util::CreateDirectory(
1644 FilePath(FilePath::kCurrentDirectory)));
1645 EXPECT_TRUE(file_util::CreateDirectory(top_level));
[email protected]89b9ae092009-12-17 20:42:401646
1647#if defined(OS_WIN)
1648 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1649 FilePath invalid_path =
1650 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1651 if (!file_util::PathExists(invalid_drive)) {
1652 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1653 }
1654#endif
[email protected]806b9c62008-09-11 16:09:111655}
1656
1657TEST_F(FileUtilTest, DetectDirectoryTest) {
1658 // Check a directory
[email protected]640517f2008-10-30 23:54:041659 FilePath test_root =
[email protected]90314c0a82010-09-15 20:40:471660 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:111661 EXPECT_FALSE(file_util::PathExists(test_root));
1662 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1663 EXPECT_TRUE(file_util::PathExists(test_root));
1664 EXPECT_TRUE(file_util::DirectoryExists(test_root));
[email protected]806b9c62008-09-11 16:09:111665 // Check a file
[email protected]640517f2008-10-30 23:54:041666 FilePath test_path =
1667 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111668 EXPECT_FALSE(file_util::PathExists(test_path));
1669 CreateTextFile(test_path, L"test file");
1670 EXPECT_TRUE(file_util::PathExists(test_path));
1671 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1672 EXPECT_TRUE(file_util::Delete(test_path, false));
1673
1674 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:381675}
1676
initial.commitd7cae122008-07-26 21:49:381677TEST_F(FileUtilTest, FileEnumeratorTest) {
1678 // Test an empty directory.
[email protected]90314c0a82010-09-15 20:40:471679 file_util::FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121680 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1681 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:381682
[email protected]8199b3a2009-06-09 05:57:381683 // Test an empty directory, non-recursively, including "..".
[email protected]90314c0a82010-09-15 20:40:471684 file_util::FileEnumerator f0_dotdot(temp_dir_.path(), false,
[email protected]58b7c5a62011-08-15 13:09:271685 static_cast<file_util::FileEnumerator::FileType>(
[email protected]8199b3a2009-06-09 05:57:381686 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
[email protected]90314c0a82010-09-15 20:40:471687 EXPECT_EQ(temp_dir_.path().Append(FILE_PATH_LITERAL("..")).value(),
[email protected]8199b3a2009-06-09 05:57:381688 f0_dotdot.Next().value());
1689 EXPECT_EQ(FILE_PATH_LITERAL(""),
1690 f0_dotdot.Next().value());
1691
[email protected]37088fef2008-08-15 17:32:101692 // create the directories
[email protected]90314c0a82010-09-15 20:40:471693 FilePath dir1 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:101694 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]90314c0a82010-09-15 20:40:471695 FilePath dir2 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:101696 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:041697 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:101698 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:041699
[email protected]37088fef2008-08-15 17:32:101700 // create the files
[email protected]640517f2008-10-30 23:54:041701 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:101702 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:041703 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:101704 CreateTextFile(dir2innerfile, L"");
[email protected]90314c0a82010-09-15 20:40:471705 FilePath file1 = temp_dir_.path().Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:101706 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:041707 FilePath file2_rel =
1708 dir2.Append(FilePath::kParentDirectory)
1709 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:101710 CreateTextFile(file2_rel, L"");
[email protected]90314c0a82010-09-15 20:40:471711 FilePath file2_abs = temp_dir_.path().Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:381712
1713 // Only enumerate files.
[email protected]90314c0a82010-09-15 20:40:471714 file_util::FileEnumerator f1(temp_dir_.path(), true,
initial.commitd7cae122008-07-26 21:49:381715 file_util::FileEnumerator::FILES);
1716 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:101717 EXPECT_TRUE(c1.HasFile(file1));
1718 EXPECT_TRUE(c1.HasFile(file2_abs));
1719 EXPECT_TRUE(c1.HasFile(dir2file));
1720 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1721 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:381722
1723 // Only enumerate directories.
[email protected]90314c0a82010-09-15 20:40:471724 file_util::FileEnumerator f2(temp_dir_.path(), true,
initial.commitd7cae122008-07-26 21:49:381725 file_util::FileEnumerator::DIRECTORIES);
1726 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:101727 EXPECT_TRUE(c2.HasFile(dir1));
1728 EXPECT_TRUE(c2.HasFile(dir2));
1729 EXPECT_TRUE(c2.HasFile(dir2inner));
1730 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:381731
[email protected]3bc9ffaf2008-10-16 02:42:451732 // Only enumerate directories non-recursively.
1733 file_util::FileEnumerator f2_non_recursive(
[email protected]90314c0a82010-09-15 20:40:471734 temp_dir_.path(), false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:451735 FindResultCollector c2_non_recursive(f2_non_recursive);
1736 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1737 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1738 EXPECT_EQ(c2_non_recursive.size(), 2);
1739
[email protected]8199b3a2009-06-09 05:57:381740 // Only enumerate directories, non-recursively, including "..".
1741 file_util::FileEnumerator f2_dotdot(
[email protected]90314c0a82010-09-15 20:40:471742 temp_dir_.path(), false,
[email protected]58b7c5a62011-08-15 13:09:271743 static_cast<file_util::FileEnumerator::FileType>(
[email protected]8199b3a2009-06-09 05:57:381744 file_util::FileEnumerator::DIRECTORIES |
1745 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1746 FindResultCollector c2_dotdot(f2_dotdot);
1747 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1748 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
[email protected]90314c0a82010-09-15 20:40:471749 EXPECT_TRUE(c2_dotdot.HasFile(
1750 temp_dir_.path().Append(FILE_PATH_LITERAL(".."))));
[email protected]8199b3a2009-06-09 05:57:381751 EXPECT_EQ(c2_dotdot.size(), 3);
1752
initial.commitd7cae122008-07-26 21:49:381753 // Enumerate files and directories.
[email protected]90314c0a82010-09-15 20:40:471754 file_util::FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381755 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:101756 EXPECT_TRUE(c3.HasFile(dir1));
1757 EXPECT_TRUE(c3.HasFile(dir2));
1758 EXPECT_TRUE(c3.HasFile(file1));
1759 EXPECT_TRUE(c3.HasFile(file2_abs));
1760 EXPECT_TRUE(c3.HasFile(dir2file));
1761 EXPECT_TRUE(c3.HasFile(dir2inner));
1762 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1763 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:381764
1765 // Non-recursive operation.
[email protected]90314c0a82010-09-15 20:40:471766 file_util::FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381767 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:101768 EXPECT_TRUE(c4.HasFile(dir2));
1769 EXPECT_TRUE(c4.HasFile(dir2));
1770 EXPECT_TRUE(c4.HasFile(file1));
1771 EXPECT_TRUE(c4.HasFile(file2_abs));
1772 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:381773
1774 // Enumerate with a pattern.
[email protected]90314c0a82010-09-15 20:40:471775 file_util::FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES,
[email protected]0b733222008-12-11 14:55:121776 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:381777 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:101778 EXPECT_TRUE(c5.HasFile(dir1));
1779 EXPECT_TRUE(c5.HasFile(dir2));
1780 EXPECT_TRUE(c5.HasFile(dir2file));
1781 EXPECT_TRUE(c5.HasFile(dir2inner));
1782 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1783 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:381784
1785 // Make sure the destructor closes the find handle while in the middle of a
1786 // query to allow TearDown to delete the directory.
[email protected]90314c0a82010-09-15 20:40:471787 file_util::FileEnumerator f6(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121788 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1789 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:381790}
license.botbf09a502008-08-24 00:55:551791
[email protected]ee5c29da2009-01-09 22:14:271792TEST_F(FileUtilTest, Contains) {
[email protected]90314c0a82010-09-15 20:40:471793 FilePath data_dir =
1794 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ee5c29da2009-01-09 22:14:271795
1796 // Create a fresh, empty copy of this directory.
[email protected]a92d2fd2009-01-31 01:19:571797 if (file_util::PathExists(data_dir)) {
1798 ASSERT_TRUE(file_util::Delete(data_dir, true));
1799 }
[email protected]ee5c29da2009-01-09 22:14:271800 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1801
1802 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1803 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1804 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1805 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1806
1807 // Annoyingly, the directories must actually exist in order for realpath(),
1808 // which Contains() relies on in posix, to work.
1809 ASSERT_TRUE(file_util::CreateDirectory(foo));
1810 std::string data("hello");
[email protected]4e9f6be2009-04-03 17:17:581811 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1812 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1813 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
[email protected]ee5c29da2009-01-09 22:14:271814
1815 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1816 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1817 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1818 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1819
[email protected]e43eddf12009-12-29 00:32:521820 // Platform-specific concerns.
[email protected]ee5c29da2009-01-09 22:14:271821 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1822#if defined(OS_WIN)
1823 EXPECT_TRUE(file_util::ContainsPath(foo,
1824 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]9e51af92009-02-04 00:58:391825 EXPECT_TRUE(file_util::ContainsPath(foo,
[email protected]ee5c29da2009-01-09 22:14:271826 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
[email protected]e43eddf12009-12-29 00:32:521827#elif defined(OS_MACOSX)
1828 // We can't really do this test on OS X since the case-sensitivity of the
1829 // filesystem is configurable.
1830#elif defined(OS_POSIX)
[email protected]ee5c29da2009-01-09 22:14:271831 EXPECT_FALSE(file_util::ContainsPath(foo,
1832 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]ee5c29da2009-01-09 22:14:271833#endif
1834}
1835
[email protected]507fb9a2010-09-23 23:28:221836TEST_F(FileUtilTest, TouchFile) {
[email protected]90314c0a82010-09-15 20:40:471837 FilePath data_dir =
1838 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ec3d1452010-02-18 10:02:261839
1840 // Create a fresh, empty copy of this directory.
1841 if (file_util::PathExists(data_dir)) {
1842 ASSERT_TRUE(file_util::Delete(data_dir, true));
1843 }
1844 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1845
1846 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1847 std::string data("hello");
1848 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1849
[email protected]507fb9a2010-09-23 23:28:221850 base::Time access_time;
1851 // This timestamp is divisible by one day (in local timezone),
1852 // to make it work on FAT too.
[email protected]46470aa2011-08-03 05:28:101853 ASSERT_TRUE(base::Time::FromString("Wed, 16 Nov 1994, 00:00:00",
[email protected]507fb9a2010-09-23 23:28:221854 &access_time));
1855
[email protected]ec3d1452010-02-18 10:02:261856 base::Time modification_time;
1857 // Note that this timestamp is divisible by two (seconds) - FAT stores
1858 // modification times with 2s resolution.
[email protected]46470aa2011-08-03 05:28:101859 ASSERT_TRUE(base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
[email protected]ec3d1452010-02-18 10:02:261860 &modification_time));
[email protected]507fb9a2010-09-23 23:28:221861
1862 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
[email protected]2f0193c22010-09-03 02:28:371863 base::PlatformFileInfo file_info;
[email protected]ec3d1452010-02-18 10:02:261864 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
[email protected]507fb9a2010-09-23 23:28:221865 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
1866 access_time.ToInternalValue());
1867 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
1868 modification_time.ToInternalValue());
[email protected]ec3d1452010-02-18 10:02:261869}
1870
[email protected]b33f1d92010-05-26 01:40:121871TEST_F(FileUtilTest, IsDirectoryEmpty) {
[email protected]90314c0a82010-09-15 20:40:471872 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
[email protected]b33f1d92010-05-26 01:40:121873
1874 ASSERT_FALSE(file_util::PathExists(empty_dir));
1875
1876 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1877
1878 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1879
1880 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1881 std::string bar("baz");
1882 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1883
1884 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1885}
1886
[email protected]73e4c362011-09-22 14:47:181887#if defined(OS_POSIX)
1888
1889// Testing VerifyPathControlledByAdmin() is hard, because there is no
1890// way a test can make a file owned by root, or change file paths
1891// at the root of the file system. VerifyPathControlledByAdmin()
1892// is implemented as a call to VerifyPathControlledByUser, which gives
1893// us the ability to test with paths under the test's temp directory,
1894// using a user id we control.
1895// Pull tests of VerifyPathControlledByUserTest() into a separate test class
1896// with a common SetUp() method.
1897class VerifyPathControlledByUserTest : public FileUtilTest {
1898 protected:
1899 virtual void SetUp() {
1900 FileUtilTest::SetUp();
1901
1902 // Create a basic structure used by each test.
1903 // base_dir_
1904 // |-> sub_dir_
1905 // |-> text_file_
1906
1907 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
1908 ASSERT_TRUE(file_util::CreateDirectory(base_dir_));
1909
1910 sub_dir_ = base_dir_.AppendASCII("sub_dir");
1911 ASSERT_TRUE(file_util::CreateDirectory(sub_dir_));
1912
1913 text_file_ = sub_dir_.AppendASCII("file.txt");
1914 CreateTextFile(text_file_, L"This text file has some text in it.");
1915
[email protected]30fcaa492011-09-26 17:18:431916 // Get the user and group files are created with from |base_dir_|.
1917 struct stat stat_buf;
1918 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
1919 uid_ = stat_buf.st_uid;
[email protected]7312f42a2011-10-17 21:30:291920 ok_gids_.insert(stat_buf.st_gid);
1921 bad_gids_.insert(stat_buf.st_gid + 1);
1922
[email protected]30fcaa492011-09-26 17:18:431923 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
[email protected]73e4c362011-09-22 14:47:181924
1925 // To ensure that umask settings do not cause the initial state
1926 // of permissions to be different from what we expect, explicitly
1927 // set permissions on the directories we create.
1928 // Make all files and directories non-world-writable.
1929 mode_t enabled_permissions =
[email protected]b2f28d22012-03-03 01:54:351930 S_IRWXU | // User can read, write, traverse
1931 S_IRWXG; // Group can read, write, traverse
[email protected]73e4c362011-09-22 14:47:181932 mode_t disabled_permissions =
[email protected]b2f28d22012-03-03 01:54:351933 S_IRWXO; // Other users can't read, write, traverse.
[email protected]73e4c362011-09-22 14:47:181934
1935 ASSERT_NO_FATAL_FAILURE(
1936 ChangePosixFilePermissions(
1937 base_dir_, enabled_permissions, disabled_permissions));
1938 ASSERT_NO_FATAL_FAILURE(
1939 ChangePosixFilePermissions(
1940 sub_dir_, enabled_permissions, disabled_permissions));
1941 }
1942
1943 FilePath base_dir_;
1944 FilePath sub_dir_;
1945 FilePath text_file_;
1946 uid_t uid_;
[email protected]7312f42a2011-10-17 21:30:291947
1948 std::set<gid_t> ok_gids_;
1949 std::set<gid_t> bad_gids_;
[email protected]73e4c362011-09-22 14:47:181950};
1951
[email protected]30fcaa492011-09-26 17:18:431952TEST_F(VerifyPathControlledByUserTest, BadPaths) {
[email protected]73e4c362011-09-22 14:47:181953 // File does not exist.
1954 FilePath does_not_exist = base_dir_.AppendASCII("does")
1955 .AppendASCII("not")
1956 .AppendASCII("exist");
[email protected]73e4c362011-09-22 14:47:181957 EXPECT_FALSE(
1958 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:291959 base_dir_, does_not_exist, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181960
1961 // |base| not a subpath of |path|.
1962 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:291963 file_util::VerifyPathControlledByUser(
1964 sub_dir_, base_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181965
1966 // An empty base path will fail to be a prefix for any path.
1967 FilePath empty;
1968 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:291969 file_util::VerifyPathControlledByUser(
1970 empty, base_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181971
1972 // Finding that a bad call fails proves nothing unless a good call succeeds.
1973 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:291974 file_util::VerifyPathControlledByUser(
1975 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181976}
1977
1978TEST_F(VerifyPathControlledByUserTest, Symlinks) {
1979 // Symlinks in the path should cause failure.
1980
1981 // Symlink to the file at the end of the path.
1982 FilePath file_link = base_dir_.AppendASCII("file_link");
1983 ASSERT_TRUE(file_util::CreateSymbolicLink(text_file_, file_link))
1984 << "Failed to create symlink.";
1985
1986 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:291987 file_util::VerifyPathControlledByUser(
1988 base_dir_, file_link, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181989 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:291990 file_util::VerifyPathControlledByUser(
1991 file_link, file_link, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181992
1993 // Symlink from one directory to another within the path.
1994 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
1995 ASSERT_TRUE(file_util::CreateSymbolicLink(sub_dir_, link_to_sub_dir))
1996 << "Failed to create symlink.";
1997
1998 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
1999 ASSERT_TRUE(file_util::PathExists(file_path_with_link));
2000
2001 EXPECT_FALSE(
2002 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292003 base_dir_, file_path_with_link, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182004
2005 EXPECT_FALSE(
2006 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292007 link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182008
2009 // Symlinks in parents of base path are allowed.
2010 EXPECT_TRUE(
2011 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292012 file_path_with_link, file_path_with_link, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182013}
2014
[email protected]30fcaa492011-09-26 17:18:432015TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
[email protected]73e4c362011-09-22 14:47:182016 // Get a uid that is not the uid of files we create.
2017 uid_t bad_uid = uid_ + 1;
2018
[email protected]73e4c362011-09-22 14:47:182019 // Make all files and directories non-world-writable.
2020 ASSERT_NO_FATAL_FAILURE(
2021 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2022 ASSERT_NO_FATAL_FAILURE(
2023 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2024 ASSERT_NO_FATAL_FAILURE(
2025 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2026
2027 // We control these paths.
2028 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292029 file_util::VerifyPathControlledByUser(
2030 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182031 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292032 file_util::VerifyPathControlledByUser(
2033 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182034 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292035 file_util::VerifyPathControlledByUser(
2036 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182037
2038 // Another user does not control these paths.
2039 EXPECT_FALSE(
2040 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292041 base_dir_, sub_dir_, bad_uid, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182042 EXPECT_FALSE(
2043 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292044 base_dir_, text_file_, bad_uid, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182045 EXPECT_FALSE(
2046 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292047 sub_dir_, text_file_, bad_uid, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182048
2049 // Another group does not control the paths.
2050 EXPECT_FALSE(
2051 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292052 base_dir_, sub_dir_, uid_, bad_gids_));
[email protected]73e4c362011-09-22 14:47:182053 EXPECT_FALSE(
2054 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292055 base_dir_, text_file_, uid_, bad_gids_));
[email protected]73e4c362011-09-22 14:47:182056 EXPECT_FALSE(
2057 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292058 sub_dir_, text_file_, uid_, bad_gids_));
2059}
2060
2061TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
2062 // Make all files and directories writable only by their owner.
2063 ASSERT_NO_FATAL_FAILURE(
2064 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
2065 ASSERT_NO_FATAL_FAILURE(
2066 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
2067 ASSERT_NO_FATAL_FAILURE(
2068 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
2069
2070 // Any group is okay because the path is not group-writable.
2071 EXPECT_TRUE(
2072 file_util::VerifyPathControlledByUser(
2073 base_dir_, sub_dir_, uid_, ok_gids_));
2074 EXPECT_TRUE(
2075 file_util::VerifyPathControlledByUser(
2076 base_dir_, text_file_, uid_, ok_gids_));
2077 EXPECT_TRUE(
2078 file_util::VerifyPathControlledByUser(
2079 sub_dir_, text_file_, uid_, ok_gids_));
2080
2081 EXPECT_TRUE(
2082 file_util::VerifyPathControlledByUser(
2083 base_dir_, sub_dir_, uid_, bad_gids_));
2084 EXPECT_TRUE(
2085 file_util::VerifyPathControlledByUser(
2086 base_dir_, text_file_, uid_, bad_gids_));
2087 EXPECT_TRUE(
2088 file_util::VerifyPathControlledByUser(
2089 sub_dir_, text_file_, uid_, bad_gids_));
2090
2091 // No group is okay, because we don't check the group
2092 // if no group can write.
2093 std::set<gid_t> no_gids; // Empty set of gids.
2094 EXPECT_TRUE(
2095 file_util::VerifyPathControlledByUser(
2096 base_dir_, sub_dir_, uid_, no_gids));
2097 EXPECT_TRUE(
2098 file_util::VerifyPathControlledByUser(
2099 base_dir_, text_file_, uid_, no_gids));
2100 EXPECT_TRUE(
2101 file_util::VerifyPathControlledByUser(
2102 sub_dir_, text_file_, uid_, no_gids));
2103
2104
2105 // Make all files and directories writable by their group.
2106 ASSERT_NO_FATAL_FAILURE(
2107 ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
2108 ASSERT_NO_FATAL_FAILURE(
2109 ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
2110 ASSERT_NO_FATAL_FAILURE(
2111 ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
2112
2113 // Now |ok_gids_| works, but |bad_gids_| fails.
2114 EXPECT_TRUE(
2115 file_util::VerifyPathControlledByUser(
2116 base_dir_, sub_dir_, uid_, ok_gids_));
2117 EXPECT_TRUE(
2118 file_util::VerifyPathControlledByUser(
2119 base_dir_, text_file_, uid_, ok_gids_));
2120 EXPECT_TRUE(
2121 file_util::VerifyPathControlledByUser(
2122 sub_dir_, text_file_, uid_, ok_gids_));
2123
2124 EXPECT_FALSE(
2125 file_util::VerifyPathControlledByUser(
2126 base_dir_, sub_dir_, uid_, bad_gids_));
2127 EXPECT_FALSE(
2128 file_util::VerifyPathControlledByUser(
2129 base_dir_, text_file_, uid_, bad_gids_));
2130 EXPECT_FALSE(
2131 file_util::VerifyPathControlledByUser(
2132 sub_dir_, text_file_, uid_, bad_gids_));
2133
2134 // Because any group in the group set is allowed,
2135 // the union of good and bad gids passes.
2136
2137 std::set<gid_t> multiple_gids;
2138 std::set_union(
2139 ok_gids_.begin(), ok_gids_.end(),
2140 bad_gids_.begin(), bad_gids_.end(),
2141 std::inserter(multiple_gids, multiple_gids.begin()));
2142
2143 EXPECT_TRUE(
2144 file_util::VerifyPathControlledByUser(
2145 base_dir_, sub_dir_, uid_, multiple_gids));
2146 EXPECT_TRUE(
2147 file_util::VerifyPathControlledByUser(
2148 base_dir_, text_file_, uid_, multiple_gids));
2149 EXPECT_TRUE(
2150 file_util::VerifyPathControlledByUser(
2151 sub_dir_, text_file_, uid_, multiple_gids));
[email protected]73e4c362011-09-22 14:47:182152}
2153
[email protected]30fcaa492011-09-26 17:18:432154TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
[email protected]73e4c362011-09-22 14:47:182155 // Make all files and directories non-world-writable.
2156 ASSERT_NO_FATAL_FAILURE(
2157 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2158 ASSERT_NO_FATAL_FAILURE(
2159 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2160 ASSERT_NO_FATAL_FAILURE(
2161 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2162
2163 // Initialy, we control all parts of the path.
2164 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292165 file_util::VerifyPathControlledByUser(
2166 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182167 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292168 file_util::VerifyPathControlledByUser(
2169 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182170 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292171 file_util::VerifyPathControlledByUser(
2172 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182173
[email protected]b2f28d22012-03-03 01:54:352174 // Make base_dir_ world-writable.
[email protected]73e4c362011-09-22 14:47:182175 ASSERT_NO_FATAL_FAILURE(
2176 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
2177 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292178 file_util::VerifyPathControlledByUser(
2179 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182180 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292181 file_util::VerifyPathControlledByUser(
2182 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182183 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292184 file_util::VerifyPathControlledByUser(
2185 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182186
2187 // Make sub_dir_ world writable.
2188 ASSERT_NO_FATAL_FAILURE(
2189 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
2190 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292191 file_util::VerifyPathControlledByUser(
2192 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182193 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292194 file_util::VerifyPathControlledByUser(
2195 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182196 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292197 file_util::VerifyPathControlledByUser(
2198 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182199
2200 // Make text_file_ world writable.
2201 ASSERT_NO_FATAL_FAILURE(
2202 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
2203 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292204 file_util::VerifyPathControlledByUser(
2205 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182206 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292207 file_util::VerifyPathControlledByUser(
2208 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182209 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292210 file_util::VerifyPathControlledByUser(
2211 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182212
2213 // Make sub_dir_ non-world writable.
2214 ASSERT_NO_FATAL_FAILURE(
2215 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2216 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292217 file_util::VerifyPathControlledByUser(
2218 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182219 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292220 file_util::VerifyPathControlledByUser(
2221 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182222 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292223 file_util::VerifyPathControlledByUser(
2224 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182225
2226 // Make base_dir_ non-world-writable.
2227 ASSERT_NO_FATAL_FAILURE(
2228 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2229 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292230 file_util::VerifyPathControlledByUser(
2231 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182232 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292233 file_util::VerifyPathControlledByUser(
2234 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182235 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292236 file_util::VerifyPathControlledByUser(
2237 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182238
2239 // Back to the initial state: Nothing is writable, so every path
2240 // should pass.
2241 ASSERT_NO_FATAL_FAILURE(
2242 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2243 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292244 file_util::VerifyPathControlledByUser(
2245 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182246 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292247 file_util::VerifyPathControlledByUser(
2248 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182249 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292250 file_util::VerifyPathControlledByUser(
2251 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182252}
2253
2254#endif // defined(OS_POSIX)
2255
[email protected]11b901ee2008-09-10 00:16:282256} // namespace