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/file_util.h"
12 #include "base/pickle.h"
13 #include "base/posix/unix_domain_socket_linux.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 TEST(UnixDomainSocketTest
, SendRecvMsgAbortOnReplyFDClose
) {
23 Thread
message_thread("UnixDomainSocketTest");
24 ASSERT_TRUE(message_thread
.Start());
27 ASSERT_EQ(0, socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, fds
));
28 file_util::ScopedFD
scoped_fd0(&fds
[0]);
29 file_util::ScopedFD
scoped_fd1(&fds
[1]);
31 // Have the thread send a synchronous message via the socket.
33 message_thread
.message_loop()->PostTask(
35 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg
),
36 fds
[1], static_cast<uint8_t*>(NULL
), 0U, static_cast<int*>(NULL
),
39 // Receive the message.
40 std::vector
<int> message_fds
;
42 ASSERT_EQ(static_cast<int>(request
.size()),
43 UnixDomainSocket::RecvMsg(fds
[0], buffer
, sizeof(buffer
),
45 ASSERT_EQ(1U, message_fds
.size());
47 // Close the reply FD.
48 ASSERT_EQ(0, HANDLE_EINTR(close(message_fds
.front())));
50 // Check that the thread didn't get blocked.
51 WaitableEvent
event(false, false);
52 message_thread
.message_loop()->PostTask(
54 Bind(&WaitableEvent::Signal
, Unretained(&event
)));
55 ASSERT_TRUE(event
.TimedWait(TimeDelta::FromMilliseconds(5000)));
58 TEST(UnixDomainSocketTest
, SendRecvMsgAvoidsSIGPIPE
) {
59 // Make sure SIGPIPE isn't being ignored.
60 struct sigaction act
= {}, oldact
;
61 act
.sa_handler
= SIG_DFL
;
62 ASSERT_EQ(0, sigaction(SIGPIPE
, &act
, &oldact
));
64 ASSERT_EQ(0, socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, fds
));
65 file_util::ScopedFD
scoped_fd1(&fds
[1]);
66 ASSERT_EQ(0, HANDLE_EINTR(close(fds
[0])));
68 // Have the thread send a synchronous message via the socket. Unless the
69 // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE.
72 UnixDomainSocket::SendRecvMsg(fds
[1], static_cast<uint8_t*>(NULL
),
73 0U, static_cast<int*>(NULL
), request
));
74 ASSERT_EQ(EPIPE
, errno
);
75 // Restore the SIGPIPE handler.
76 ASSERT_EQ(0, sigaction(SIGPIPE
, &oldact
, NULL
));