| 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/location.h" |
| 14 #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" | 16 #include "base/single_thread_task_runner.h" |
| 18 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
| 19 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 20 |
| 22 namespace base { | 21 namespace base { |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { | 25 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { |
| 27 Thread message_thread("UnixDomainSocketTest"); | 26 Thread message_thread("UnixDomainSocketTest"); |
| 28 ASSERT_TRUE(message_thread.Start()); | 27 ASSERT_TRUE(message_thread.Start()); |
| 29 | 28 |
| 30 int fds[2]; | 29 int fds[2]; |
| 31 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 30 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 32 ScopedFD scoped_fd0(fds[0]); | 31 ScopedFD scoped_fd0(fds[0]); |
| 33 ScopedFD scoped_fd1(fds[1]); | 32 ScopedFD scoped_fd1(fds[1]); |
| 34 | 33 |
| 35 // Have the thread send a synchronous message via the socket. | 34 // Have the thread send a synchronous message via the socket. |
| 36 Pickle request; | 35 Pickle request; |
| 37 message_thread.task_runner()->PostTask( | 36 message_thread.task_runner()->PostTask( |
| 38 FROM_HERE, | 37 FROM_HERE, |
| 39 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), fds[1], | 38 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), fds[1], |
| 40 static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), request)); | 39 static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), request)); |
| 41 | 40 |
| 42 // Receive the message. | 41 // Receive the message. |
| 43 ScopedVector<ScopedFD> message_fds; | 42 std::vector<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. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 87 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| 89 | 88 |
| 90 static const char kHello[] = "hello"; | 89 static const char kHello[] = "hello"; |
| 91 ASSERT_TRUE(UnixDomainSocket::SendMsg( | 90 ASSERT_TRUE(UnixDomainSocket::SendMsg( |
| 92 send_sock.get(), kHello, sizeof(kHello), std::vector<int>())); | 91 send_sock.get(), kHello, sizeof(kHello), std::vector<int>())); |
| 93 | 92 |
| 94 // Extra receiving buffer space to make sure we really received only | 93 // Extra receiving buffer space to make sure we really received only |
| 95 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. | 94 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. |
| 96 char buf[sizeof(kHello) + 1]; | 95 char buf[sizeof(kHello) + 1]; |
| 97 ProcessId sender_pid; | 96 ProcessId sender_pid; |
| 98 ScopedVector<ScopedFD> fd_vec; | 97 std::vector<ScopedFD> fd_vec; |
| 99 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( | 98 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( |
| 100 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid); | 99 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid); |
| 101 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); | 100 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); |
| 102 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | 101 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); |
| 103 ASSERT_EQ(0U, fd_vec.size()); | 102 ASSERT_EQ(0U, fd_vec.size()); |
| 104 | 103 |
| 105 ASSERT_EQ(getpid(), sender_pid); | 104 ASSERT_EQ(getpid(), sender_pid); |
| 106 } | 105 } |
| 107 | 106 |
| 108 // Same as above, but send the max number of file descriptors too. | 107 // Same as above, but send the max number of file descriptors too. |
| 109 TEST(UnixDomainSocketTest, RecvPidWithMaxDescriptors) { | 108 TEST(UnixDomainSocketTest, RecvPidWithMaxDescriptors) { |
| 110 int fds[2]; | 109 int fds[2]; |
| 111 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 110 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 112 ScopedFD recv_sock(fds[0]); | 111 ScopedFD recv_sock(fds[0]); |
| 113 ScopedFD send_sock(fds[1]); | 112 ScopedFD send_sock(fds[1]); |
| 114 | 113 |
| 115 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 114 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| 116 | 115 |
| 117 static const char kHello[] = "hello"; | 116 static const char kHello[] = "hello"; |
| 118 std::vector<int> send_fds(UnixDomainSocket::kMaxFileDescriptors, | 117 std::vector<int> send_fds(UnixDomainSocket::kMaxFileDescriptors, |
| 119 send_sock.get()); | 118 send_sock.get()); |
| 120 ASSERT_TRUE(UnixDomainSocket::SendMsg( | 119 ASSERT_TRUE(UnixDomainSocket::SendMsg( |
| 121 send_sock.get(), kHello, sizeof(kHello), send_fds)); | 120 send_sock.get(), kHello, sizeof(kHello), send_fds)); |
| 122 | 121 |
| 123 // Extra receiving buffer space to make sure we really received only | 122 // Extra receiving buffer space to make sure we really received only |
| 124 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. | 123 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. |
| 125 char buf[sizeof(kHello) + 1]; | 124 char buf[sizeof(kHello) + 1]; |
| 126 ProcessId sender_pid; | 125 ProcessId sender_pid; |
| 127 ScopedVector<ScopedFD> recv_fds; | 126 std::vector<ScopedFD> recv_fds; |
| 128 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( | 127 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( |
| 129 recv_sock.get(), buf, sizeof(buf), &recv_fds, &sender_pid); | 128 recv_sock.get(), buf, sizeof(buf), &recv_fds, &sender_pid); |
| 130 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); | 129 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); |
| 131 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | 130 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); |
| 132 ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors, recv_fds.size()); | 131 ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors, recv_fds.size()); |
| 133 | 132 |
| 134 ASSERT_EQ(getpid(), sender_pid); | 133 ASSERT_EQ(getpid(), sender_pid); |
| 135 } | 134 } |
| 136 | 135 |
| 137 // Check that RecvMsgWithPid doesn't DCHECK fail when reading EOF from a | 136 // Check that RecvMsgWithPid doesn't DCHECK fail when reading EOF from a |
| 138 // disconnected socket. | 137 // disconnected socket. |
| 139 TEST(UnixDomianSocketTest, RecvPidDisconnectedSocket) { | 138 TEST(UnixDomianSocketTest, RecvPidDisconnectedSocket) { |
| 140 int fds[2]; | 139 int fds[2]; |
| 141 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 140 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 142 ScopedFD recv_sock(fds[0]); | 141 ScopedFD recv_sock(fds[0]); |
| 143 ScopedFD send_sock(fds[1]); | 142 ScopedFD send_sock(fds[1]); |
| 144 | 143 |
| 145 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 144 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| 146 | 145 |
| 147 send_sock.reset(); | 146 send_sock.reset(); |
| 148 | 147 |
| 149 char ch; | 148 char ch; |
| 150 ProcessId sender_pid; | 149 ProcessId sender_pid; |
| 151 ScopedVector<ScopedFD> recv_fds; | 150 std::vector<ScopedFD> recv_fds; |
| 152 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( | 151 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( |
| 153 recv_sock.get(), &ch, sizeof(ch), &recv_fds, &sender_pid); | 152 recv_sock.get(), &ch, sizeof(ch), &recv_fds, &sender_pid); |
| 154 ASSERT_EQ(0, nread); | 153 ASSERT_EQ(0, nread); |
| 155 ASSERT_EQ(-1, sender_pid); | 154 ASSERT_EQ(-1, sender_pid); |
| 156 ASSERT_EQ(0U, recv_fds.size()); | 155 ASSERT_EQ(0U, recv_fds.size()); |
| 157 } | 156 } |
| 158 | 157 |
| 159 } // namespace | 158 } // namespace |
| 160 | 159 |
| 161 } // namespace base | 160 } // namespace base |
| OLD | NEW |