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" | |
14 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
15 #include "base/pickle.h" | 14 #include "base/pickle.h" |
16 #include "base/posix/unix_domain_socket_linux.h" | 15 #include "base/posix/unix_domain_socket_linux.h" |
17 #include "base/single_thread_task_runner.h" | |
18 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
19 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
21 | 19 |
22 namespace base { | 20 namespace base { |
23 | 21 |
24 namespace { | 22 namespace { |
25 | 23 |
26 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { | 24 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { |
27 Thread message_thread("UnixDomainSocketTest"); | 25 Thread message_thread("UnixDomainSocketTest"); |
28 ASSERT_TRUE(message_thread.Start()); | 26 ASSERT_TRUE(message_thread.Start()); |
29 | 27 |
30 int fds[2]; | 28 int fds[2]; |
31 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 29 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
32 ScopedFD scoped_fd0(fds[0]); | 30 ScopedFD scoped_fd0(fds[0]); |
33 ScopedFD scoped_fd1(fds[1]); | 31 ScopedFD scoped_fd1(fds[1]); |
34 | 32 |
35 // Have the thread send a synchronous message via the socket. | 33 // Have the thread send a synchronous message via the socket. |
36 Pickle request; | 34 Pickle request; |
37 message_thread.task_runner()->PostTask( | 35 message_thread.message_loop()->PostTask( |
38 FROM_HERE, | 36 FROM_HERE, |
39 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), fds[1], | 37 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), |
40 static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), request)); | 38 fds[1], static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), |
| 39 request)); |
41 | 40 |
42 // Receive the message. | 41 // Receive the message. |
43 ScopedVector<base::ScopedFD> message_fds; | 42 ScopedVector<base::ScopedFD> message_fds; |
44 uint8_t buffer[16]; | 43 uint8_t buffer[16]; |
45 ASSERT_EQ(static_cast<int>(request.size()), | 44 ASSERT_EQ(static_cast<int>(request.size()), |
46 UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer), | 45 UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer), |
47 &message_fds)); | 46 &message_fds)); |
48 ASSERT_EQ(1U, message_fds.size()); | 47 ASSERT_EQ(1U, message_fds.size()); |
49 | 48 |
50 // Close the reply FD. | 49 // Close the reply FD. |
51 message_fds.clear(); | 50 message_fds.clear(); |
52 | 51 |
53 // Check that the thread didn't get blocked. | 52 // Check that the thread didn't get blocked. |
54 WaitableEvent event(false, false); | 53 WaitableEvent event(false, false); |
55 message_thread.task_runner()->PostTask( | 54 message_thread.message_loop()->PostTask( |
56 FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event))); | 55 FROM_HERE, |
| 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 |