Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / posix / unix_domain_socket_linux_unittest.cc
blob175ec52f086c02e55283ecb6e9407b90649cd67c
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/files/file_util.h"
12 #include "base/files/scoped_file.h"
13 #include "base/location.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/pickle.h"
16 #include "base/posix/unix_domain_socket_linux.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/synchronization/waitable_event.h"
19 #include "base/threading/thread.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace base {
24 namespace {
26 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) {
27 Thread message_thread("UnixDomainSocketTest");
28 ASSERT_TRUE(message_thread.Start());
30 int fds[2];
31 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
32 ScopedFD scoped_fd0(fds[0]);
33 ScopedFD scoped_fd1(fds[1]);
35 // Have the thread send a synchronous message via the socket.
36 Pickle request;
37 message_thread.task_runner()->PostTask(
38 FROM_HERE,
39 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), fds[1],
40 static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), request));
42 // Receive the message.
43 ScopedVector<ScopedFD> message_fds;
44 uint8_t buffer[16];
45 ASSERT_EQ(static_cast<int>(request.size()),
46 UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer),
47 &message_fds));
48 ASSERT_EQ(1U, message_fds.size());
50 // Close the reply FD.
51 message_fds.clear();
53 // Check that the thread didn't get blocked.
54 WaitableEvent event(false, false);
55 message_thread.task_runner()->PostTask(
56 FROM_HERE, 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));
65 int fds[2];
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.
72 Pickle request;
73 ASSERT_EQ(-1,
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) {
83 int fds[2];
84 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
85 ScopedFD recv_sock(fds[0]);
86 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 ProcessId sender_pid;
98 ScopedVector<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) {
110 int fds[2];
111 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
112 ScopedFD recv_sock(fds[0]);
113 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,
119 send_sock.get());
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 ProcessId sender_pid;
127 ScopedVector<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) {
140 int fds[2];
141 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
142 ScopedFD recv_sock(fds[0]);
143 ScopedFD send_sock(fds[1]);
145 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get()));
147 send_sock.reset();
149 char ch;
150 ProcessId sender_pid;
151 ScopedVector<ScopedFD> recv_fds;
152 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid(
153 recv_sock.get(), &ch, sizeof(ch), &recv_fds, &sender_pid);
154 ASSERT_EQ(0, nread);
155 ASSERT_EQ(-1, sender_pid);
156 ASSERT_EQ(0U, recv_fds.size());
159 } // namespace
161 } // namespace base