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

Side by Side Diff: base/file_util_unittest.cc

Issue 191673003: 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
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"
29 #include "base/files/scoped_temp_dir.h" 30 #include "base/files/scoped_temp_dir.h"
30 #include "base/path_service.h" 31 #include "base/path_service.h"
31 #include "base/strings/utf_string_conversions.h" 32 #include "base/strings/utf_string_conversions.h"
32 #include "base/test/test_file_util.h" 33 #include "base/test/test_file_util.h"
33 #include "base/threading/platform_thread.h" 34 #include "base/threading/platform_thread.h"
34 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
35 #include "testing/platform_test.h" 36 #include "testing/platform_test.h"
36 37
37 #if defined(OS_WIN) 38 #if defined(OS_WIN)
38 #include "base/win/scoped_handle.h" 39 #include "base/win/scoped_handle.h"
(...skipping 2427 matching lines...) Expand 10 before | Expand all | Expand 10 after
2466 int fd = OpenContentUriForRead(path); 2467 int fd = OpenContentUriForRead(path);
2467 EXPECT_EQ(-1, fd); 2468 EXPECT_EQ(-1, fd);
2468 } 2469 }
2469 #endif 2470 #endif
2470 2471
2471 TEST(ScopedFD, ScopedFDDoesClose) { 2472 TEST(ScopedFD, ScopedFDDoesClose) {
2472 int fds[2]; 2473 int fds[2];
2473 char c = 0; 2474 char c = 0;
2474 ASSERT_EQ(0, pipe(fds)); 2475 ASSERT_EQ(0, pipe(fds));
2475 const int write_end = fds[1]; 2476 const int write_end = fds[1];
2476 file_util::ScopedFDCloser read_end_closer(fds); 2477 base::ScopedFD read_end_closer(fds[0]);
2477 { 2478 {
2478 file_util::ScopedFDCloser write_end_closer(fds + 1); 2479 base::ScopedFD write_end_closer(fds[1]);
2479 } 2480 }
2480 // This is the only thread. This file descriptor should no longer be valid. 2481 // This is the only thread. This file descriptor should no longer be valid.
2481 int ret = close(write_end); 2482 int ret = close(write_end);
2482 EXPECT_EQ(-1, ret); 2483 EXPECT_EQ(-1, ret);
2483 EXPECT_EQ(EBADF, errno); 2484 EXPECT_EQ(EBADF, errno);
2484 // Make sure read(2) won't block. 2485 // Make sure read(2) won't block.
2485 ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK)); 2486 ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK));
2486 // Reading the pipe should EOF. 2487 // Reading the pipe should EOF.
2487 EXPECT_EQ(0, read(fds[0], &c, 1)); 2488 EXPECT_EQ(0, read(fds[0], &c, 1));
2488 } 2489 }
2489 2490
2490 #if defined(GTEST_HAS_DEATH_TEST) 2491 #if defined(GTEST_HAS_DEATH_TEST)
2491 void CloseWithScopedFD(int fd) { 2492 void CloseWithScopedFD(int fd) {
2492 file_util::ScopedFDCloser fd_closer(&fd); 2493 base::ScopedFD fd_closer(fd);
2493 } 2494 }
2494 #endif 2495 #endif
2495 2496
2496 TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) { 2497 TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) {
2497 int fds[2]; 2498 int fds[2];
2498 ASSERT_EQ(0, pipe(fds)); 2499 ASSERT_EQ(0, pipe(fds));
2499 file_util::ScopedFDCloser read_end_closer(fds); 2500 base::ScopedFD read_end_closer(fds[0]);
2500 EXPECT_EQ(0, IGNORE_EINTR(close(fds[1]))); 2501 EXPECT_EQ(0, IGNORE_EINTR(close(fds[1])));
2501 #if defined(GTEST_HAS_DEATH_TEST) 2502 #if defined(GTEST_HAS_DEATH_TEST)
2502 // This is the only thread. This file descriptor should no longer be valid. 2503 // This is the only thread. This file descriptor should no longer be valid.
2503 // Trying to close it should crash. This is important for security. 2504 // Trying to close it should crash. This is important for security.
2504 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); 2505 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2505 #endif 2506 #endif
2506 } 2507 }
2507 2508
2508 #endif // defined(OS_POSIX) 2509 #endif // defined(OS_POSIX)
2509 2510
2510 } // namespace 2511 } // namespace
2511 2512
2512 } // namespace base 2513 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698