Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: base/files/file_util_unittest.cc

Issue 935333002: Update from https://crrev.com/316786 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/file_util_linux.cc ('k') | base/i18n/time_formatting.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shellapi.h> 9 #include <shellapi.h>
10 #include <shlobj.h> 10 #include <shlobj.h>
(...skipping 12 matching lines...) Expand all
23 #include <set> 23 #include <set>
24 #include <vector> 24 #include <vector>
25 25
26 #include "base/base_paths.h" 26 #include "base/base_paths.h"
27 #include "base/files/file_enumerator.h" 27 #include "base/files/file_enumerator.h"
28 #include "base/files/file_path.h" 28 #include "base/files/file_path.h"
29 #include "base/files/file_util.h" 29 #include "base/files/file_util.h"
30 #include "base/files/scoped_file.h" 30 #include "base/files/scoped_file.h"
31 #include "base/files/scoped_temp_dir.h" 31 #include "base/files/scoped_temp_dir.h"
32 #include "base/path_service.h" 32 #include "base/path_service.h"
33 #include "base/strings/string_util.h"
33 #include "base/strings/utf_string_conversions.h" 34 #include "base/strings/utf_string_conversions.h"
34 #include "base/test/test_file_util.h" 35 #include "base/test/test_file_util.h"
35 #include "base/threading/platform_thread.h" 36 #include "base/threading/platform_thread.h"
36 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
37 #include "testing/platform_test.h" 38 #include "testing/platform_test.h"
38 39
39 #if defined(OS_WIN) 40 #if defined(OS_WIN)
40 #include "base/win/scoped_handle.h" 41 #include "base/win/scoped_handle.h"
41 #include "base/win/windows_version.h" 42 #include "base/win/windows_version.h"
42 #endif 43 #endif
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 // | |-> file.txt 320 // | |-> file.txt
320 // | |-> long_name___... (Very long name.) 321 // | |-> long_name___... (Very long name.)
321 // | |-> sub_long 322 // | |-> sub_long
322 // | |-> deep.txt 323 // | |-> deep.txt
323 // |-> base_b 324 // |-> base_b
324 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a) 325 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
325 // |-> to_base_b (reparse point to temp_dir\base_b) 326 // |-> to_base_b (reparse point to temp_dir\base_b)
326 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long) 327 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
327 328
328 FilePath base_a = temp_dir_.path().Append(FPL("base_a")); 329 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
330 #if defined(OS_WIN)
331 // TEMP can have a lower case drive letter.
332 string16 temp_base_a = base_a.value();
333 ASSERT_FALSE(temp_base_a.empty());
334 *temp_base_a.begin() = base::ToUpperASCII(*temp_base_a.begin());
335 base_a = FilePath(temp_base_a);
336 #endif
329 ASSERT_TRUE(CreateDirectory(base_a)); 337 ASSERT_TRUE(CreateDirectory(base_a));
330 338
331 FilePath sub_a = base_a.Append(FPL("sub_a")); 339 FilePath sub_a = base_a.Append(FPL("sub_a"));
332 ASSERT_TRUE(CreateDirectory(sub_a)); 340 ASSERT_TRUE(CreateDirectory(sub_a));
333 341
334 FilePath file_txt = sub_a.Append(FPL("file.txt")); 342 FilePath file_txt = sub_a.Append(FPL("file.txt"));
335 CreateTextFile(file_txt, bogus_content); 343 CreateTextFile(file_txt, bogus_content);
336 344
337 // Want a directory whose name is long enough to make the path to the file 345 // Want a directory whose name is long enough to make the path to the file
338 // inside just under MAX_PATH chars. This will be used to test that when 346 // inside just under MAX_PATH chars. This will be used to test that when
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 // to traverse them. 429 // to traverse them.
422 } 430 }
423 431
424 ASSERT_FALSE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")), 432 ASSERT_FALSE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
425 &normalized_path)); 433 &normalized_path));
426 } 434 }
427 435
428 TEST_F(FileUtilTest, DevicePathToDriveLetter) { 436 TEST_F(FileUtilTest, DevicePathToDriveLetter) {
429 // Get a drive letter. 437 // Get a drive letter.
430 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2); 438 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
439 StringToUpperASCII(&real_drive_letter);
431 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) { 440 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
432 LOG(ERROR) << "Can't get a drive letter to test with."; 441 LOG(ERROR) << "Can't get a drive letter to test with.";
433 return; 442 return;
434 } 443 }
435 444
436 // Get the NT style path to that drive. 445 // Get the NT style path to that drive.
437 wchar_t device_path[MAX_PATH] = {'\0'}; 446 wchar_t device_path[MAX_PATH] = {'\0'};
438 ASSERT_TRUE( 447 ASSERT_TRUE(
439 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH)); 448 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH));
440 FilePath actual_device_path(device_path); 449 FilePath actual_device_path(device_path);
(...skipping 940 matching lines...) Expand 10 before | Expand all | Expand 10 after
1381 // Sets the source file to read-only. 1390 // Sets the source file to read-only.
1382 void SetReadOnly(const FilePath& path, bool read_only) { 1391 void SetReadOnly(const FilePath& path, bool read_only) {
1383 #if defined(OS_WIN) 1392 #if defined(OS_WIN)
1384 // On Windows, it involves setting/removing the 'readonly' bit. 1393 // On Windows, it involves setting/removing the 'readonly' bit.
1385 DWORD attrs = GetFileAttributes(path.value().c_str()); 1394 DWORD attrs = GetFileAttributes(path.value().c_str());
1386 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs); 1395 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1387 ASSERT_TRUE(SetFileAttributes( 1396 ASSERT_TRUE(SetFileAttributes(
1388 path.value().c_str(), 1397 path.value().c_str(),
1389 read_only ? (attrs | FILE_ATTRIBUTE_READONLY) : 1398 read_only ? (attrs | FILE_ATTRIBUTE_READONLY) :
1390 (attrs & ~FILE_ATTRIBUTE_READONLY))); 1399 (attrs & ~FILE_ATTRIBUTE_READONLY)));
1391 // Files in the temporary directory should not be indexed ever. If this 1400
1392 // assumption change, fix this unit test accordingly.
1393 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1394 DWORD expected = read_only ? 1401 DWORD expected = read_only ?
1395 ((attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY)) | 1402 ((attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY)) |
1396 FILE_ATTRIBUTE_READONLY) : 1403 FILE_ATTRIBUTE_READONLY) :
1397 (attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY)); 1404 (attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY));
1398 // TODO(ripp@yandex-team.ru): this seems out of place here. If we really think 1405
1399 // it is important to verify that temp files are not indexed there should be 1406 // Ignore FILE_ATTRIBUTE_NOT_CONTENT_INDEXED if present.
1400 // a dedicated test for that (create a file, inspect the attributes) 1407 attrs = GetFileAttributes(path.value().c_str()) &
1401 if (win::GetVersion() >= win::VERSION_VISTA) 1408 ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1402 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1403 attrs = GetFileAttributes(path.value().c_str());
1404 ASSERT_EQ(expected, attrs); 1409 ASSERT_EQ(expected, attrs);
1405 #else 1410 #else
1406 // On all other platforms, it involves removing/setting the write bit. 1411 // On all other platforms, it involves removing/setting the write bit.
1407 mode_t mode = read_only ? S_IRUSR : (S_IRUSR | S_IWUSR); 1412 mode_t mode = read_only ? S_IRUSR : (S_IRUSR | S_IWUSR);
1408 EXPECT_TRUE(SetPosixFilePermissions( 1413 EXPECT_TRUE(SetPosixFilePermissions(
1409 path, DirectoryExists(path) ? (mode | S_IXUSR) : mode)); 1414 path, DirectoryExists(path) ? (mode | S_IXUSR) : mode));
1410 #endif 1415 #endif
1411 } 1416 }
1412 1417
1413 bool IsReadOnly(const FilePath& path) { 1418 bool IsReadOnly(const FilePath& path) {
(...skipping 1192 matching lines...) Expand 10 before | Expand all | Expand 10 after
2606 // Trying to close it should crash. This is important for security. 2611 // Trying to close it should crash. This is important for security.
2607 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); 2612 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2608 #endif 2613 #endif
2609 } 2614 }
2610 2615
2611 #endif // defined(OS_POSIX) 2616 #endif // defined(OS_POSIX)
2612 2617
2613 } // namespace 2618 } // namespace
2614 2619
2615 } // namespace base 2620 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_util_linux.cc ('k') | base/i18n/time_formatting.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698