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/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
| 13 #include "base/location.h" |
13 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
14 #include "base/pickle.h" | 15 #include "base/pickle.h" |
15 #include "base/posix/unix_domain_socket_linux.h" | 16 #include "base/posix/unix_domain_socket_linux.h" |
| 17 #include "base/single_thread_task_runner.h" |
16 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
17 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
20 namespace base { | 22 namespace base { |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { | 26 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { |
25 Thread message_thread("UnixDomainSocketTest"); | 27 Thread message_thread("UnixDomainSocketTest"); |
26 ASSERT_TRUE(message_thread.Start()); | 28 ASSERT_TRUE(message_thread.Start()); |
27 | 29 |
28 int fds[2]; | 30 int fds[2]; |
29 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 31 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
30 ScopedFD scoped_fd0(fds[0]); | 32 ScopedFD scoped_fd0(fds[0]); |
31 ScopedFD scoped_fd1(fds[1]); | 33 ScopedFD scoped_fd1(fds[1]); |
32 | 34 |
33 // Have the thread send a synchronous message via the socket. | 35 // Have the thread send a synchronous message via the socket. |
34 Pickle request; | 36 Pickle request; |
35 message_thread.message_loop()->PostTask( | 37 message_thread.task_runner()->PostTask( |
36 FROM_HERE, | 38 FROM_HERE, |
37 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), | 39 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), fds[1], |
38 fds[1], static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), | 40 static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), request)); |
39 request)); | |
40 | 41 |
41 // Receive the message. | 42 // Receive the message. |
42 ScopedVector<base::ScopedFD> message_fds; | 43 ScopedVector<base::ScopedFD> message_fds; |
43 uint8_t buffer[16]; | 44 uint8_t buffer[16]; |
44 ASSERT_EQ(static_cast<int>(request.size()), | 45 ASSERT_EQ(static_cast<int>(request.size()), |
45 UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer), | 46 UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer), |
46 &message_fds)); | 47 &message_fds)); |
47 ASSERT_EQ(1U, message_fds.size()); | 48 ASSERT_EQ(1U, message_fds.size()); |
48 | 49 |
49 // Close the reply FD. | 50 // Close the reply FD. |
50 message_fds.clear(); | 51 message_fds.clear(); |
51 | 52 |
52 // Check that the thread didn't get blocked. | 53 // Check that the thread didn't get blocked. |
53 WaitableEvent event(false, false); | 54 WaitableEvent event(false, false); |
54 message_thread.message_loop()->PostTask( | 55 message_thread.task_runner()->PostTask( |
55 FROM_HERE, | 56 FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event))); |
56 Bind(&WaitableEvent::Signal, Unretained(&event))); | |
57 ASSERT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(5000))); | 57 ASSERT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(5000))); |
58 } | 58 } |
59 | 59 |
60 TEST(UnixDomainSocketTest, SendRecvMsgAvoidsSIGPIPE) { | 60 TEST(UnixDomainSocketTest, SendRecvMsgAvoidsSIGPIPE) { |
61 // Make sure SIGPIPE isn't being ignored. | 61 // Make sure SIGPIPE isn't being ignored. |
62 struct sigaction act = {}, oldact; | 62 struct sigaction act = {}, oldact; |
63 act.sa_handler = SIG_DFL; | 63 act.sa_handler = SIG_DFL; |
64 ASSERT_EQ(0, sigaction(SIGPIPE, &act, &oldact)); | 64 ASSERT_EQ(0, sigaction(SIGPIPE, &act, &oldact)); |
65 int fds[2]; | 65 int fds[2]; |
66 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 66 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( | 152 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( |
153 recv_sock.get(), &ch, sizeof(ch), &recv_fds, &sender_pid); | 153 recv_sock.get(), &ch, sizeof(ch), &recv_fds, &sender_pid); |
154 ASSERT_EQ(0, nread); | 154 ASSERT_EQ(0, nread); |
155 ASSERT_EQ(-1, sender_pid); | 155 ASSERT_EQ(-1, sender_pid); |
156 ASSERT_EQ(0U, recv_fds.size()); | 156 ASSERT_EQ(0U, recv_fds.size()); |
157 } | 157 } |
158 | 158 |
159 } // namespace | 159 } // namespace |
160 | 160 |
161 } // namespace base | 161 } // namespace base |
OLD | NEW |