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

Unified Diff: base/file_util_unittest.cc

Issue 183953004: POSIX: CHECK() that file_util::ScopedFD fulfills promise. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix IOS compile failure. Created 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/file_util.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « base/file_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698