| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <sys/socket.h> | 5 #include <sys/socket.h> |
| 6 #include <sys/types.h> | 6 #include <sys/types.h> |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/scoped_file.h" | |
| 13 #include "base/pickle.h" | 12 #include "base/pickle.h" |
| 14 #include "base/posix/unix_domain_socket_linux.h" | 13 #include "base/posix/unix_domain_socket_linux.h" |
| 15 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 namespace base { | 18 namespace base { |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { | 22 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { |
| 24 Thread message_thread("UnixDomainSocketTest"); | 23 Thread message_thread("UnixDomainSocketTest"); |
| 25 ASSERT_TRUE(message_thread.Start()); | 24 ASSERT_TRUE(message_thread.Start()); |
| 26 | 25 |
| 27 int fds[2]; | 26 int fds[2]; |
| 28 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 27 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 29 ScopedFD scoped_fd0(fds[0]); | 28 file_util::ScopedFD scoped_fd0(&fds[0]); |
| 30 ScopedFD scoped_fd1(fds[1]); | 29 file_util::ScopedFD scoped_fd1(&fds[1]); |
| 31 | 30 |
| 32 // Have the thread send a synchronous message via the socket. | 31 // Have the thread send a synchronous message via the socket. |
| 33 Pickle request; | 32 Pickle request; |
| 34 message_thread.message_loop()->PostTask( | 33 message_thread.message_loop()->PostTask( |
| 35 FROM_HERE, | 34 FROM_HERE, |
| 36 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), | 35 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), |
| 37 fds[1], static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), | 36 fds[1], static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), |
| 38 request)); | 37 request)); |
| 39 | 38 |
| 40 // Receive the message. | 39 // Receive the message. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 56 ASSERT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(5000))); | 55 ASSERT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(5000))); |
| 57 } | 56 } |
| 58 | 57 |
| 59 TEST(UnixDomainSocketTest, SendRecvMsgAvoidsSIGPIPE) { | 58 TEST(UnixDomainSocketTest, SendRecvMsgAvoidsSIGPIPE) { |
| 60 // Make sure SIGPIPE isn't being ignored. | 59 // Make sure SIGPIPE isn't being ignored. |
| 61 struct sigaction act = {}, oldact; | 60 struct sigaction act = {}, oldact; |
| 62 act.sa_handler = SIG_DFL; | 61 act.sa_handler = SIG_DFL; |
| 63 ASSERT_EQ(0, sigaction(SIGPIPE, &act, &oldact)); | 62 ASSERT_EQ(0, sigaction(SIGPIPE, &act, &oldact)); |
| 64 int fds[2]; | 63 int fds[2]; |
| 65 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 64 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 66 ScopedFD scoped_fd1(fds[1]); | 65 file_util::ScopedFD scoped_fd1(&fds[1]); |
| 67 ASSERT_EQ(0, IGNORE_EINTR(close(fds[0]))); | 66 ASSERT_EQ(0, IGNORE_EINTR(close(fds[0]))); |
| 68 | 67 |
| 69 // Have the thread send a synchronous message via the socket. Unless the | 68 // Have the thread send a synchronous message via the socket. Unless the |
| 70 // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE. | 69 // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE. |
| 71 Pickle request; | 70 Pickle request; |
| 72 ASSERT_EQ(-1, | 71 ASSERT_EQ(-1, |
| 73 UnixDomainSocket::SendRecvMsg(fds[1], static_cast<uint8_t*>(NULL), | 72 UnixDomainSocket::SendRecvMsg(fds[1], static_cast<uint8_t*>(NULL), |
| 74 0U, static_cast<int*>(NULL), request)); | 73 0U, static_cast<int*>(NULL), request)); |
| 75 ASSERT_EQ(EPIPE, errno); | 74 ASSERT_EQ(EPIPE, errno); |
| 76 // Restore the SIGPIPE handler. | 75 // Restore the SIGPIPE handler. |
| 77 ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL)); | 76 ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL)); |
| 78 } | 77 } |
| 79 | 78 |
| 80 } // namespace | 79 } // namespace |
| 81 | 80 |
| 82 } // namespace base | 81 } // namespace base |
| OLD | NEW |