Speculative fix for the white/stale tab issue on Windows.
[chromium-blink-merge.git] / base / posix / unix_domain_socket_linux_unittest.cc
blob1343555b330919d62a10f5de604cc72749dd1289
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>
6 #include <sys/types.h>
7 #include <unistd.h>
9 #include "base/bind.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"
18 namespace base {
20 namespace {
22 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) {
23 Thread message_thread("UnixDomainSocketTest");
24 ASSERT_TRUE(message_thread.Start());
26 int fds[2];
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.
32 Pickle request;
33 message_thread.message_loop()->PostTask(
34 FROM_HERE,
35 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg),
36 fds[1], static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL),
37 request));
39 // Receive the message.
40 std::vector<int> message_fds;
41 uint8_t buffer[16];
42 ASSERT_EQ(static_cast<int>(request.size()),
43 UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer),
44 &message_fds));
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(
53 FROM_HERE,
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));
63 int fds[2];
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.
70 Pickle request;
71 ASSERT_EQ(-1,
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));
79 } // namespace
81 } // namespace base