Index: base/file_util_unittest.cc |
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc |
index e7c43ff4533ff42c5b057b5b2a29b3be5b5ca65a..e410c6463ec05a04fcbdfeeb7908473b6ab08aae 100644 |
--- a/base/file_util_unittest.cc |
+++ b/base/file_util_unittest.cc |
@@ -12,6 +12,12 @@ |
#include <winioctl.h> |
#endif |
+#if defined(OS_POSIX) |
+#include <errno.h> |
+#include <fcntl.h> |
+#include <unistd.h> |
+#endif |
+ |
#include <algorithm> |
#include <fstream> |
#include <set> |
@@ -2463,6 +2469,43 @@ TEST_F(FileUtilTest, NonExistentContentUriTest) { |
} |
#endif |
+TEST(ScopedFD, ScopedFDDoesClose) { |
+ int fds[2]; |
+ char c = 0; |
+ ASSERT_EQ(0, pipe(fds)); |
+ const int write_end = fds[1]; |
+ file_util::ScopedFDCloser read_end_closer(fds); |
+ { |
+ file_util::ScopedFDCloser write_end_closer(fds + 1); |
+ } |
+ // This is the only thread. This file descriptor should no longer be valid. |
+ int ret = close(write_end); |
+ EXPECT_EQ(-1, ret); |
+ EXPECT_EQ(EBADF, errno); |
+ // Make sure read(2) won't block. |
+ ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK)); |
+ // Reading the pipe should EOF. |
+ EXPECT_EQ(0, read(fds[0], &c, 1)); |
+} |
+ |
+#if defined(GTEST_HAS_DEATH_TEST) |
+void CloseWithScopedFD(int fd) { |
+ file_util::ScopedFDCloser fd_closer(&fd); |
+} |
+#endif |
+ |
+TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) { |
+ int fds[2]; |
+ ASSERT_EQ(0, pipe(fds)); |
+ file_util::ScopedFDCloser read_end_closer(fds); |
+ EXPECT_EQ(0, IGNORE_EINTR(close(fds[1]))); |
+#if defined(GTEST_HAS_DEATH_TEST) |
+ // This is the only thread. This file descriptor should no longer be valid. |
+ // Trying to close it should crash. This is important for security. |
+ EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); |
+#endif |
+} |
+ |
#endif // defined(OS_POSIX) |
} // namespace |