| 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" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 ScopedFD scoped_fd1(fds[1]); | 33 ScopedFD scoped_fd1(fds[1]); |
| 34 | 34 |
| 35 // Have the thread send a synchronous message via the socket. | 35 // Have the thread send a synchronous message via the socket. |
| 36 Pickle request; | 36 Pickle request; |
| 37 message_thread.task_runner()->PostTask( | 37 message_thread.task_runner()->PostTask( |
| 38 FROM_HERE, | 38 FROM_HERE, |
| 39 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), fds[1], | 39 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), fds[1], |
| 40 static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), request)); | 40 static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), request)); |
| 41 | 41 |
| 42 // Receive the message. | 42 // Receive the message. |
| 43 ScopedVector<base::ScopedFD> message_fds; | 43 ScopedVector<ScopedFD> message_fds; |
| 44 uint8_t buffer[16]; | 44 uint8_t buffer[16]; |
| 45 ASSERT_EQ(static_cast<int>(request.size()), | 45 ASSERT_EQ(static_cast<int>(request.size()), |
| 46 UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer), | 46 UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer), |
| 47 &message_fds)); | 47 &message_fds)); |
| 48 ASSERT_EQ(1U, message_fds.size()); | 48 ASSERT_EQ(1U, message_fds.size()); |
| 49 | 49 |
| 50 // Close the reply FD. | 50 // Close the reply FD. |
| 51 message_fds.clear(); | 51 message_fds.clear(); |
| 52 | 52 |
| 53 // Check that the thread didn't get blocked. | 53 // Check that the thread didn't get blocked. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 75 0U, static_cast<int*>(NULL), request)); | 75 0U, static_cast<int*>(NULL), request)); |
| 76 ASSERT_EQ(EPIPE, errno); | 76 ASSERT_EQ(EPIPE, errno); |
| 77 // Restore the SIGPIPE handler. | 77 // Restore the SIGPIPE handler. |
| 78 ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL)); | 78 ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL)); |
| 79 } | 79 } |
| 80 | 80 |
| 81 // Simple sanity check within a single process that receiving PIDs works. | 81 // Simple sanity check within a single process that receiving PIDs works. |
| 82 TEST(UnixDomainSocketTest, RecvPid) { | 82 TEST(UnixDomainSocketTest, RecvPid) { |
| 83 int fds[2]; | 83 int fds[2]; |
| 84 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 84 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 85 base::ScopedFD recv_sock(fds[0]); | 85 ScopedFD recv_sock(fds[0]); |
| 86 base::ScopedFD send_sock(fds[1]); | 86 ScopedFD send_sock(fds[1]); |
| 87 | 87 |
| 88 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 88 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| 89 | 89 |
| 90 static const char kHello[] = "hello"; | 90 static const char kHello[] = "hello"; |
| 91 ASSERT_TRUE(UnixDomainSocket::SendMsg( | 91 ASSERT_TRUE(UnixDomainSocket::SendMsg( |
| 92 send_sock.get(), kHello, sizeof(kHello), std::vector<int>())); | 92 send_sock.get(), kHello, sizeof(kHello), std::vector<int>())); |
| 93 | 93 |
| 94 // Extra receiving buffer space to make sure we really received only | 94 // 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. | 95 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. |
| 96 char buf[sizeof(kHello) + 1]; | 96 char buf[sizeof(kHello) + 1]; |
| 97 base::ProcessId sender_pid; | 97 ProcessId sender_pid; |
| 98 ScopedVector<base::ScopedFD> fd_vec; | 98 ScopedVector<ScopedFD> fd_vec; |
| 99 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( | 99 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( |
| 100 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid); | 100 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid); |
| 101 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); | 101 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); |
| 102 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | 102 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); |
| 103 ASSERT_EQ(0U, fd_vec.size()); | 103 ASSERT_EQ(0U, fd_vec.size()); |
| 104 | 104 |
| 105 ASSERT_EQ(getpid(), sender_pid); | 105 ASSERT_EQ(getpid(), sender_pid); |
| 106 } | 106 } |
| 107 | 107 |
| 108 // 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. |
| 109 TEST(UnixDomainSocketTest, RecvPidWithMaxDescriptors) { | 109 TEST(UnixDomainSocketTest, RecvPidWithMaxDescriptors) { |
| 110 int fds[2]; | 110 int fds[2]; |
| 111 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 111 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 112 base::ScopedFD recv_sock(fds[0]); | 112 ScopedFD recv_sock(fds[0]); |
| 113 base::ScopedFD send_sock(fds[1]); | 113 ScopedFD send_sock(fds[1]); |
| 114 | 114 |
| 115 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 115 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| 116 | 116 |
| 117 static const char kHello[] = "hello"; | 117 static const char kHello[] = "hello"; |
| 118 std::vector<int> send_fds(UnixDomainSocket::kMaxFileDescriptors, | 118 std::vector<int> send_fds(UnixDomainSocket::kMaxFileDescriptors, |
| 119 send_sock.get()); | 119 send_sock.get()); |
| 120 ASSERT_TRUE(UnixDomainSocket::SendMsg( | 120 ASSERT_TRUE(UnixDomainSocket::SendMsg( |
| 121 send_sock.get(), kHello, sizeof(kHello), send_fds)); | 121 send_sock.get(), kHello, sizeof(kHello), send_fds)); |
| 122 | 122 |
| 123 // Extra receiving buffer space to make sure we really received only | 123 // 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. | 124 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. |
| 125 char buf[sizeof(kHello) + 1]; | 125 char buf[sizeof(kHello) + 1]; |
| 126 base::ProcessId sender_pid; | 126 ProcessId sender_pid; |
| 127 ScopedVector<base::ScopedFD> recv_fds; | 127 ScopedVector<ScopedFD> recv_fds; |
| 128 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( | 128 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( |
| 129 recv_sock.get(), buf, sizeof(buf), &recv_fds, &sender_pid); | 129 recv_sock.get(), buf, sizeof(buf), &recv_fds, &sender_pid); |
| 130 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); | 130 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); |
| 131 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | 131 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); |
| 132 ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors, recv_fds.size()); | 132 ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors, recv_fds.size()); |
| 133 | 133 |
| 134 ASSERT_EQ(getpid(), sender_pid); | 134 ASSERT_EQ(getpid(), sender_pid); |
| 135 } | 135 } |
| 136 | 136 |
| 137 // Check that RecvMsgWithPid doesn't DCHECK fail when reading EOF from a | 137 // Check that RecvMsgWithPid doesn't DCHECK fail when reading EOF from a |
| 138 // disconnected socket. | 138 // disconnected socket. |
| 139 TEST(UnixDomianSocketTest, RecvPidDisconnectedSocket) { | 139 TEST(UnixDomianSocketTest, RecvPidDisconnectedSocket) { |
| 140 int fds[2]; | 140 int fds[2]; |
| 141 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 141 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 142 base::ScopedFD recv_sock(fds[0]); | 142 ScopedFD recv_sock(fds[0]); |
| 143 base::ScopedFD send_sock(fds[1]); | 143 ScopedFD send_sock(fds[1]); |
| 144 | 144 |
| 145 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 145 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| 146 | 146 |
| 147 send_sock.reset(); | 147 send_sock.reset(); |
| 148 | 148 |
| 149 char ch; | 149 char ch; |
| 150 base::ProcessId sender_pid; | 150 ProcessId sender_pid; |
| 151 ScopedVector<base::ScopedFD> recv_fds; | 151 ScopedVector<ScopedFD> recv_fds; |
| 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 |