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
3 // found in the LICENSE file.
5 #include <sys/socket.h>
10 #include "base/bind_helpers.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_file.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/pickle.h"
15 #include "base/posix/unix_domain_socket_linux.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/thread.h"
18 #include "testing/gtest/include/gtest/gtest.h"
24 TEST(UnixDomainSocketTest
, SendRecvMsgAbortOnReplyFDClose
) {
25 Thread
message_thread("UnixDomainSocketTest");
26 ASSERT_TRUE(message_thread
.Start());
29 ASSERT_EQ(0, socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, fds
));
30 ScopedFD
scoped_fd0(fds
[0]);
31 ScopedFD
scoped_fd1(fds
[1]);
33 // Have the thread send a synchronous message via the socket.
35 message_thread
.message_loop()->PostTask(
37 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg
),
38 fds
[1], static_cast<uint8_t*>(NULL
), 0U, static_cast<int*>(NULL
),
41 // Receive the message.
42 ScopedVector
<base::ScopedFD
> message_fds
;
44 ASSERT_EQ(static_cast<int>(request
.size()),
45 UnixDomainSocket::RecvMsg(fds
[0], buffer
, sizeof(buffer
),
47 ASSERT_EQ(1U, message_fds
.size());
49 // Close the reply FD.
52 // Check that the thread didn't get blocked.
53 WaitableEvent
event(false, false);
54 message_thread
.message_loop()->PostTask(
56 Bind(&WaitableEvent::Signal
, Unretained(&event
)));
57 ASSERT_TRUE(event
.TimedWait(TimeDelta::FromMilliseconds(5000)));
60 TEST(UnixDomainSocketTest
, SendRecvMsgAvoidsSIGPIPE
) {
61 // Make sure SIGPIPE isn't being ignored.
62 struct sigaction act
= {}, oldact
;
63 act
.sa_handler
= SIG_DFL
;
64 ASSERT_EQ(0, sigaction(SIGPIPE
, &act
, &oldact
));
66 ASSERT_EQ(0, socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, fds
));
67 ScopedFD
scoped_fd1(fds
[1]);
68 ASSERT_EQ(0, IGNORE_EINTR(close(fds
[0])));
70 // Have the thread send a synchronous message via the socket. Unless the
71 // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE.
74 UnixDomainSocket::SendRecvMsg(fds
[1], static_cast<uint8_t*>(NULL
),
75 0U, static_cast<int*>(NULL
), request
));
76 ASSERT_EQ(EPIPE
, errno
);
77 // Restore the SIGPIPE handler.
78 ASSERT_EQ(0, sigaction(SIGPIPE
, &oldact
, NULL
));
81 // Simple sanity check within a single process that receiving PIDs works.
82 TEST(UnixDomainSocketTest
, RecvPid
) {
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]);
88 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock
.get()));
90 static const char kHello
[] = "hello";
91 ASSERT_TRUE(UnixDomainSocket::SendMsg(
92 send_sock
.get(), kHello
, sizeof(kHello
), std::vector
<int>()));
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.
96 char buf
[sizeof(kHello
) + 1];
97 base::ProcessId sender_pid
;
98 ScopedVector
<base::ScopedFD
> fd_vec
;
99 const ssize_t nread
= UnixDomainSocket::RecvMsgWithPid(
100 recv_sock
.get(), buf
, sizeof(buf
), &fd_vec
, &sender_pid
);
101 ASSERT_EQ(sizeof(kHello
), static_cast<size_t>(nread
));
102 ASSERT_EQ(0, memcmp(buf
, kHello
, sizeof(kHello
)));
103 ASSERT_EQ(0U, fd_vec
.size());
105 ASSERT_EQ(getpid(), sender_pid
);
108 // Same as above, but send the max number of file descriptors too.
109 TEST(UnixDomainSocketTest
, RecvPidWithMaxDescriptors
) {
111 ASSERT_EQ(0, socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, fds
));
112 base::ScopedFD
recv_sock(fds
[0]);
113 base::ScopedFD
send_sock(fds
[1]);
115 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock
.get()));
117 static const char kHello
[] = "hello";
118 std::vector
<int> send_fds(UnixDomainSocket::kMaxFileDescriptors
,
120 ASSERT_TRUE(UnixDomainSocket::SendMsg(
121 send_sock
.get(), kHello
, sizeof(kHello
), send_fds
));
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.
125 char buf
[sizeof(kHello
) + 1];
126 base::ProcessId sender_pid
;
127 ScopedVector
<base::ScopedFD
> recv_fds
;
128 const ssize_t nread
= UnixDomainSocket::RecvMsgWithPid(
129 recv_sock
.get(), buf
, sizeof(buf
), &recv_fds
, &sender_pid
);
130 ASSERT_EQ(sizeof(kHello
), static_cast<size_t>(nread
));
131 ASSERT_EQ(0, memcmp(buf
, kHello
, sizeof(kHello
)));
132 ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors
, recv_fds
.size());
134 ASSERT_EQ(getpid(), sender_pid
);
137 // Check that RecvMsgWithPid doesn't DCHECK fail when reading EOF from a
138 // disconnected socket.
139 TEST(UnixDomianSocketTest
, RecvPidDisconnectedSocket
) {
141 ASSERT_EQ(0, socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, fds
));
142 base::ScopedFD
recv_sock(fds
[0]);
143 base::ScopedFD
send_sock(fds
[1]);
145 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock
.get()));
150 base::ProcessId sender_pid
;
151 ScopedVector
<base::ScopedFD
> recv_fds
;
152 const ssize_t nread
= UnixDomainSocket::RecvMsgWithPid(
153 recv_sock
.get(), &ch
, sizeof(ch
), &recv_fds
, &sender_pid
);
155 ASSERT_EQ(-1, sender_pid
);
156 ASSERT_EQ(0U, recv_fds
.size());