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

Side by Side Diff: base/file_util_unittest.cc

Issue 197873014: Revert of Implement ScopedFD in terms of ScopedGeneric. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « base/file_util_posix.cc ('k') | base/files/scoped_file.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>
11 #include <tchar.h> 11 #include <tchar.h>
12 #include <winioctl.h> 12 #include <winioctl.h>
13 #endif 13 #endif
14 14
15 #if defined(OS_POSIX) 15 #if defined(OS_POSIX)
16 #include <errno.h> 16 #include <errno.h>
17 #include <fcntl.h> 17 #include <fcntl.h>
18 #include <unistd.h> 18 #include <unistd.h>
19 #endif 19 #endif
20 20
21 #include <algorithm> 21 #include <algorithm>
22 #include <fstream> 22 #include <fstream>
23 #include <set> 23 #include <set>
24 24
25 #include "base/base_paths.h" 25 #include "base/base_paths.h"
26 #include "base/file_util.h" 26 #include "base/file_util.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/scoped_file.h"
30 #include "base/files/scoped_temp_dir.h" 29 #include "base/files/scoped_temp_dir.h"
31 #include "base/path_service.h" 30 #include "base/path_service.h"
32 #include "base/strings/utf_string_conversions.h" 31 #include "base/strings/utf_string_conversions.h"
33 #include "base/test/test_file_util.h" 32 #include "base/test/test_file_util.h"
34 #include "base/threading/platform_thread.h" 33 #include "base/threading/platform_thread.h"
35 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
36 #include "testing/platform_test.h" 35 #include "testing/platform_test.h"
37 36
38 #if defined(OS_WIN) 37 #if defined(OS_WIN)
39 #include "base/win/scoped_handle.h" 38 #include "base/win/scoped_handle.h"
(...skipping 2427 matching lines...) Expand 10 before | Expand all | Expand 10 after
2467 int fd = OpenContentUriForRead(path); 2466 int fd = OpenContentUriForRead(path);
2468 EXPECT_EQ(-1, fd); 2467 EXPECT_EQ(-1, fd);
2469 } 2468 }
2470 #endif 2469 #endif
2471 2470
2472 TEST(ScopedFD, ScopedFDDoesClose) { 2471 TEST(ScopedFD, ScopedFDDoesClose) {
2473 int fds[2]; 2472 int fds[2];
2474 char c = 0; 2473 char c = 0;
2475 ASSERT_EQ(0, pipe(fds)); 2474 ASSERT_EQ(0, pipe(fds));
2476 const int write_end = fds[1]; 2475 const int write_end = fds[1];
2477 base::ScopedFD read_end_closer(fds[0]); 2476 file_util::ScopedFDCloser read_end_closer(fds);
2478 { 2477 {
2479 base::ScopedFD write_end_closer(fds[1]); 2478 file_util::ScopedFDCloser write_end_closer(fds + 1);
2480 } 2479 }
2481 // This is the only thread. This file descriptor should no longer be valid. 2480 // This is the only thread. This file descriptor should no longer be valid.
2482 int ret = close(write_end); 2481 int ret = close(write_end);
2483 EXPECT_EQ(-1, ret); 2482 EXPECT_EQ(-1, ret);
2484 EXPECT_EQ(EBADF, errno); 2483 EXPECT_EQ(EBADF, errno);
2485 // Make sure read(2) won't block. 2484 // Make sure read(2) won't block.
2486 ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK)); 2485 ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK));
2487 // Reading the pipe should EOF. 2486 // Reading the pipe should EOF.
2488 EXPECT_EQ(0, read(fds[0], &c, 1)); 2487 EXPECT_EQ(0, read(fds[0], &c, 1));
2489 } 2488 }
2490 2489
2491 #if defined(GTEST_HAS_DEATH_TEST) 2490 #if defined(GTEST_HAS_DEATH_TEST)
2492 void CloseWithScopedFD(int fd) { 2491 void CloseWithScopedFD(int fd) {
2493 base::ScopedFD fd_closer(fd); 2492 file_util::ScopedFDCloser fd_closer(&fd);
2494 } 2493 }
2495 #endif 2494 #endif
2496 2495
2497 TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) { 2496 TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) {
2498 int fds[2]; 2497 int fds[2];
2499 ASSERT_EQ(0, pipe(fds)); 2498 ASSERT_EQ(0, pipe(fds));
2500 base::ScopedFD read_end_closer(fds[0]); 2499 file_util::ScopedFDCloser read_end_closer(fds);
2501 EXPECT_EQ(0, IGNORE_EINTR(close(fds[1]))); 2500 EXPECT_EQ(0, IGNORE_EINTR(close(fds[1])));
2502 #if defined(GTEST_HAS_DEATH_TEST) 2501 #if defined(GTEST_HAS_DEATH_TEST)
2503 // This is the only thread. This file descriptor should no longer be valid. 2502 // This is the only thread. This file descriptor should no longer be valid.
2504 // Trying to close it should crash. This is important for security. 2503 // Trying to close it should crash. This is important for security.
2505 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); 2504 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2506 #endif 2505 #endif
2507 } 2506 }
2508 2507
2509 #endif // defined(OS_POSIX) 2508 #endif // defined(OS_POSIX)
2510 2509
2511 } // namespace 2510 } // namespace
2512 2511
2513 } // namespace base 2512 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util_posix.cc ('k') | base/files/scoped_file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698