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" | 12 #include "base/files/scoped_file.h" |
| 13 #include "base/memory/scoped_vector.h" |
13 #include "base/pickle.h" | 14 #include "base/pickle.h" |
14 #include "base/posix/unix_domain_socket_linux.h" | 15 #include "base/posix/unix_domain_socket_linux.h" |
15 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { | 24 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { |
24 Thread message_thread("UnixDomainSocketTest"); | 25 Thread message_thread("UnixDomainSocketTest"); |
25 ASSERT_TRUE(message_thread.Start()); | 26 ASSERT_TRUE(message_thread.Start()); |
26 | 27 |
27 int fds[2]; | 28 int fds[2]; |
28 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 29 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
29 ScopedFD scoped_fd0(fds[0]); | 30 ScopedFD scoped_fd0(fds[0]); |
30 ScopedFD scoped_fd1(fds[1]); | 31 ScopedFD scoped_fd1(fds[1]); |
31 | 32 |
32 // Have the thread send a synchronous message via the socket. | 33 // Have the thread send a synchronous message via the socket. |
33 Pickle request; | 34 Pickle request; |
34 message_thread.message_loop()->PostTask( | 35 message_thread.message_loop()->PostTask( |
35 FROM_HERE, | 36 FROM_HERE, |
36 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), | 37 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), |
37 fds[1], static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), | 38 fds[1], static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), |
38 request)); | 39 request)); |
39 | 40 |
40 // Receive the message. | 41 // Receive the message. |
41 std::vector<int> message_fds; | 42 ScopedVector<base::ScopedFD> message_fds; |
42 uint8_t buffer[16]; | 43 uint8_t buffer[16]; |
43 ASSERT_EQ(static_cast<int>(request.size()), | 44 ASSERT_EQ(static_cast<int>(request.size()), |
44 UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer), | 45 UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer), |
45 &message_fds)); | 46 &message_fds)); |
46 ASSERT_EQ(1U, message_fds.size()); | 47 ASSERT_EQ(1U, message_fds.size()); |
47 | 48 |
48 // Close the reply FD. | 49 // Close the reply FD. |
49 ASSERT_EQ(0, IGNORE_EINTR(close(message_fds.front()))); | 50 message_fds.clear(); |
50 | 51 |
51 // Check that the thread didn't get blocked. | 52 // Check that the thread didn't get blocked. |
52 WaitableEvent event(false, false); | 53 WaitableEvent event(false, false); |
53 message_thread.message_loop()->PostTask( | 54 message_thread.message_loop()->PostTask( |
54 FROM_HERE, | 55 FROM_HERE, |
55 Bind(&WaitableEvent::Signal, Unretained(&event))); | 56 Bind(&WaitableEvent::Signal, Unretained(&event))); |
56 ASSERT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(5000))); | 57 ASSERT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(5000))); |
57 } | 58 } |
58 | 59 |
59 TEST(UnixDomainSocketTest, SendRecvMsgAvoidsSIGPIPE) { | 60 TEST(UnixDomainSocketTest, SendRecvMsgAvoidsSIGPIPE) { |
(...skipping 27 matching lines...) Expand all Loading... |
87 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 88 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
88 | 89 |
89 static const char kHello[] = "hello"; | 90 static const char kHello[] = "hello"; |
90 ASSERT_TRUE(UnixDomainSocket::SendMsg( | 91 ASSERT_TRUE(UnixDomainSocket::SendMsg( |
91 send_sock.get(), kHello, sizeof(kHello), std::vector<int>())); | 92 send_sock.get(), kHello, sizeof(kHello), std::vector<int>())); |
92 | 93 |
93 // Extra receiving buffer space to make sure we really received only | 94 // Extra receiving buffer space to make sure we really received only |
94 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. | 95 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. |
95 char buf[sizeof(kHello) + 1]; | 96 char buf[sizeof(kHello) + 1]; |
96 base::ProcessId sender_pid; | 97 base::ProcessId sender_pid; |
97 std::vector<int> fd_vec; | 98 ScopedVector<base::ScopedFD> fd_vec; |
98 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( | 99 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( |
99 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid); | 100 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid); |
100 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); | 101 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); |
101 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | 102 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); |
102 ASSERT_EQ(0U, fd_vec.size()); | 103 ASSERT_EQ(0U, fd_vec.size()); |
103 | 104 |
104 ASSERT_EQ(getpid(), sender_pid); | 105 ASSERT_EQ(getpid(), sender_pid); |
105 } | 106 } |
106 | 107 |
107 // Same as above, but send the max number of file descriptors too. | 108 // Same as above, but send the max number of file descriptors too. |
108 TEST(UnixDomainSocketTest, RecvPidWithMaxDescriptors) { | 109 TEST(UnixDomainSocketTest, RecvPidWithMaxDescriptors) { |
109 int fds[2]; | 110 int fds[2]; |
110 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 111 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
111 base::ScopedFD recv_sock(fds[0]); | 112 base::ScopedFD recv_sock(fds[0]); |
112 base::ScopedFD send_sock(fds[1]); | 113 base::ScopedFD send_sock(fds[1]); |
113 | 114 |
114 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 115 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
115 | 116 |
116 static const char kHello[] = "hello"; | 117 static const char kHello[] = "hello"; |
117 std::vector<int> fd_vec(UnixDomainSocket::kMaxFileDescriptors, | 118 std::vector<int> send_fds(UnixDomainSocket::kMaxFileDescriptors, |
118 send_sock.get()); | 119 send_sock.get()); |
119 ASSERT_TRUE(UnixDomainSocket::SendMsg( | 120 ASSERT_TRUE(UnixDomainSocket::SendMsg( |
120 send_sock.get(), kHello, sizeof(kHello), fd_vec)); | 121 send_sock.get(), kHello, sizeof(kHello), send_fds)); |
121 | 122 |
122 // Extra receiving buffer space to make sure we really received only | 123 // Extra receiving buffer space to make sure we really received only |
123 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. | 124 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. |
124 char buf[sizeof(kHello) + 1]; | 125 char buf[sizeof(kHello) + 1]; |
125 base::ProcessId sender_pid; | 126 base::ProcessId sender_pid; |
| 127 ScopedVector<base::ScopedFD> recv_fds; |
126 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( | 128 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( |
127 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid); | 129 recv_sock.get(), buf, sizeof(buf), &recv_fds, &sender_pid); |
128 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); | 130 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); |
129 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | 131 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); |
130 ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors, fd_vec.size()); | 132 ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors, recv_fds.size()); |
131 for (size_t i = 0; i < UnixDomainSocket::kMaxFileDescriptors; i++) { | |
132 ASSERT_EQ(0, IGNORE_EINTR(close(fd_vec[i]))); | |
133 } | |
134 | 133 |
135 ASSERT_EQ(getpid(), sender_pid); | 134 ASSERT_EQ(getpid(), sender_pid); |
136 } | 135 } |
137 | 136 |
138 } // namespace | 137 } // namespace |
139 | 138 |
140 } // namespace base | 139 } // namespace base |
OLD | NEW |