Chromium Code Reviews| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE. | 70 // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE. |
| 71 Pickle request; | 71 Pickle request; |
| 72 ASSERT_EQ(-1, | 72 ASSERT_EQ(-1, |
| 73 UnixDomainSocket::SendRecvMsg(fds[1], static_cast<uint8_t*>(NULL), | 73 UnixDomainSocket::SendRecvMsg(fds[1], static_cast<uint8_t*>(NULL), |
| 74 0U, static_cast<int*>(NULL), request)); | 74 0U, static_cast<int*>(NULL), request)); |
| 75 ASSERT_EQ(EPIPE, errno); | 75 ASSERT_EQ(EPIPE, errno); |
| 76 // Restore the SIGPIPE handler. | 76 // Restore the SIGPIPE handler. |
| 77 ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL)); | 77 ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 // Simple sanity check within a single process that receiving PIDs works. | |
| 81 // Additional tests are in sandbox/linux/tests/unix_domain_socket_unittest.cc. | |
|
jln (very slow on Chromium)
2014/04/24 18:32:58
Remove this line, as base/ should not be tested by
mdempsky
2014/04/24 19:00:34
Done.
| |
| 82 TEST(UnixDomainSocketTest, RecvPid) { | |
| 83 int fds[2]; | |
| 84 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | |
| 85 base::ScopedFD recv_sock(fds[0]); | |
| 86 base::ScopedFD send_sock(fds[1]); | |
| 87 | |
| 88 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | |
| 89 | |
| 90 static const char kHello[] = "hello"; | |
| 91 ASSERT_TRUE(UnixDomainSocket::SendMsg( | |
| 92 send_sock.get(), kHello, sizeof(kHello), std::vector<int>())); | |
| 93 | |
| 94 char buf[sizeof(kHello) + 1]; | |
|
jln (very slow on Chromium)
2014/04/24 18:32:58
Did you add the + 1 to be able to test that you on
mdempsky
2014/04/24 19:00:34
Correct.
| |
| 95 base::ProcessId sender_pid; | |
| 96 std::vector<int> fd_vec; | |
| 97 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( | |
| 98 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid); | |
| 99 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); | |
| 100 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | |
| 101 ASSERT_EQ(0U, fd_vec.size()); | |
| 102 | |
| 103 ASSERT_EQ(getpid(), sender_pid); | |
| 104 } | |
| 105 | |
| 106 // Same as above, but send the max number of file descriptors too. | |
| 107 TEST(UnixDomainSocketTest, RecvPidWithMaxDescriptors) { | |
| 108 int fds[2]; | |
| 109 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | |
| 110 base::ScopedFD recv_sock(fds[0]); | |
| 111 base::ScopedFD send_sock(fds[1]); | |
| 112 | |
| 113 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | |
| 114 | |
| 115 static const char kHello[] = "hello"; | |
| 116 std::vector<int> fd_vec(UnixDomainSocket::kMaxFileDescriptors, | |
| 117 send_sock.get()); | |
| 118 ASSERT_TRUE(UnixDomainSocket::SendMsg( | |
| 119 send_sock.get(), kHello, sizeof(kHello), fd_vec)); | |
| 120 | |
| 121 char buf[sizeof(kHello) + 1]; | |
| 122 base::ProcessId sender_pid; | |
| 123 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid( | |
| 124 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid); | |
| 125 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread)); | |
| 126 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | |
| 127 ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors, fd_vec.size()); | |
| 128 for (size_t i = 0; i < UnixDomainSocket::kMaxFileDescriptors; i++) { | |
| 129 ASSERT_EQ(0, IGNORE_EINTR(close(fd_vec[i]))); | |
| 130 } | |
| 131 | |
| 132 ASSERT_EQ(getpid(), sender_pid); | |
| 133 } | |
| 134 | |
| 80 } // namespace | 135 } // namespace |
| 81 | 136 |
| 82 } // namespace base | 137 } // namespace base |
| OLD | NEW |