OLD | NEW |
---|---|
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) | |
16 #include <errno.h> | |
17 #include <fcntl.h> | |
18 #include <unistd.h> | |
19 #endif | |
20 | |
15 #include <algorithm> | 21 #include <algorithm> |
16 #include <fstream> | 22 #include <fstream> |
17 #include <set> | 23 #include <set> |
18 | 24 |
19 #include "base/base_paths.h" | 25 #include "base/base_paths.h" |
20 #include "base/file_util.h" | 26 #include "base/file_util.h" |
21 #include "base/files/file_enumerator.h" | 27 #include "base/files/file_enumerator.h" |
22 #include "base/files/file_path.h" | 28 #include "base/files/file_path.h" |
23 #include "base/files/scoped_temp_dir.h" | 29 #include "base/files/scoped_temp_dir.h" |
24 #include "base/path_service.h" | 30 #include "base/path_service.h" |
(...skipping 2431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2456 // Size should be smaller than 0. | 2462 // Size should be smaller than 0. |
2457 int64 size; | 2463 int64 size; |
2458 EXPECT_FALSE(GetFileSize(path, &size)); | 2464 EXPECT_FALSE(GetFileSize(path, &size)); |
2459 | 2465 |
2460 // We should not be able to read the file. | 2466 // We should not be able to read the file. |
2461 int fd = OpenContentUriForRead(path); | 2467 int fd = OpenContentUriForRead(path); |
2462 EXPECT_EQ(-1, fd); | 2468 EXPECT_EQ(-1, fd); |
2463 } | 2469 } |
2464 #endif | 2470 #endif |
2465 | 2471 |
2472 TEST(ScopedFD, ScopedFDDoesClose) { | |
2473 int fds[2]; | |
2474 char c = 0; | |
2475 ASSERT_EQ(0, pipe(fds)); | |
2476 const int write_end = fds[1]; | |
2477 file_util::ScopedFDCloser read_end_closer(fds); | |
2478 { | |
2479 file_util::ScopedFDCloser write_end_closer(fds + 1); | |
2480 } | |
2481 // This is the only thread. This file descriptor should no longer be valid. | |
2482 int ret = close(write_end); | |
2483 EXPECT_EQ(-1, ret); | |
2484 EXPECT_EQ(EBADF, errno); | |
2485 // Make sure read(2) won't block. | |
2486 ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK)); | |
2487 // Reading the pipe should EOF. | |
2488 EXPECT_EQ(0, read(fds[0], &c, 1)); | |
2489 } | |
2490 | |
2491 void CloseWithScopedFD(int fd) { | |
2492 file_util::ScopedFDCloser fd_closer(&fd); | |
2493 } | |
2494 | |
2495 TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) { | |
2496 int fds[2]; | |
2497 ASSERT_EQ(0, pipe(fds)); | |
2498 file_util::ScopedFDCloser read_end_closer(fds); | |
2499 CHECK_EQ(0, IGNORE_EINTR(close(fds[1]))); | |
willchan no longer on Chromium
2014/02/28 01:48:48
Shouldn't this be EXPECT_EQ? Is there an advantage
jln (very slow on Chromium)
2014/02/28 02:01:21
You're right, a fd leak is probably not worth a cr
| |
2500 #if defined(GTEST_HAS_DEATH_TEST) | |
2501 // This is the only thread. This file descriptor should no longer be valid. | |
2502 // Trying to close it should crash. This is important for security. | |
2503 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); | |
2504 #endif | |
2505 } | |
2506 | |
2466 #endif // defined(OS_POSIX) | 2507 #endif // defined(OS_POSIX) |
2467 | 2508 |
2468 } // namespace | 2509 } // namespace |
2469 | 2510 |
2470 } // namespace base | 2511 } // namespace base |
OLD | NEW |