• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include <stddef.h>
6 #include <stdint.h>
7 #include <sys/socket.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/files/file_util.h"
14 #include "base/files/scoped_file.h"
15 #include "base/location.h"
16 #include "base/pickle.h"
17 #include "base/posix/unix_domain_socket_linux.h"
18 #include "base/single_thread_task_runner.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "base/threading/thread.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 
23 namespace base {
24 
25 namespace {
26 
TEST(UnixDomainSocketTest,SendRecvMsgAbortOnReplyFDClose)27 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) {
28   Thread message_thread("UnixDomainSocketTest");
29   ASSERT_TRUE(message_thread.Start());
30 
31   int fds[2];
32   ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
33   ScopedFD scoped_fd0(fds[0]);
34   ScopedFD scoped_fd1(fds[1]);
35 
36   // Have the thread send a synchronous message via the socket.
37   Pickle request;
38   message_thread.task_runner()->PostTask(
39       FROM_HERE,
40       Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), fds[1],
41            static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), request));
42 
43   // Receive the message.
44   std::vector<ScopedFD> message_fds;
45   uint8_t buffer[16];
46   ASSERT_EQ(static_cast<int>(request.size()),
47             UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer),
48                                       &message_fds));
49   ASSERT_EQ(1U, message_fds.size());
50 
51   // Close the reply FD.
52   message_fds.clear();
53 
54   // Check that the thread didn't get blocked.
55   WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC,
56                       WaitableEvent::InitialState::NOT_SIGNALED);
57   message_thread.task_runner()->PostTask(
58       FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event)));
59   ASSERT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(5000)));
60 }
61 
TEST(UnixDomainSocketTest,SendRecvMsgAvoidsSIGPIPE)62 TEST(UnixDomainSocketTest, SendRecvMsgAvoidsSIGPIPE) {
63   // Make sure SIGPIPE isn't being ignored.
64   struct sigaction act = {}, oldact;
65   act.sa_handler = SIG_DFL;
66   ASSERT_EQ(0, sigaction(SIGPIPE, &act, &oldact));
67   int fds[2];
68   ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
69   ScopedFD scoped_fd1(fds[1]);
70   ASSERT_EQ(0, IGNORE_EINTR(close(fds[0])));
71 
72   // Have the thread send a synchronous message via the socket. Unless the
73   // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE.
74   Pickle request;
75   ASSERT_EQ(-1,
76       UnixDomainSocket::SendRecvMsg(fds[1], static_cast<uint8_t*>(NULL),
77                                     0U, static_cast<int*>(NULL), request));
78   ASSERT_EQ(EPIPE, errno);
79   // Restore the SIGPIPE handler.
80   ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL));
81 }
82 
83 // Simple sanity check within a single process that receiving PIDs works.
TEST(UnixDomainSocketTest,RecvPid)84 TEST(UnixDomainSocketTest, RecvPid) {
85   int fds[2];
86   ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
87   ScopedFD recv_sock(fds[0]);
88   ScopedFD send_sock(fds[1]);
89 
90   ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get()));
91 
92   static const char kHello[] = "hello";
93   ASSERT_TRUE(UnixDomainSocket::SendMsg(
94       send_sock.get(), kHello, sizeof(kHello), std::vector<int>()));
95 
96   // Extra receiving buffer space to make sure we really received only
97   // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer.
98   char buf[sizeof(kHello) + 1];
99   ProcessId sender_pid;
100   std::vector<ScopedFD> fd_vec;
101   const ssize_t nread = UnixDomainSocket::RecvMsgWithPid(
102       recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid);
103   ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread));
104   ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello)));
105   ASSERT_EQ(0U, fd_vec.size());
106 
107   ASSERT_EQ(getpid(), sender_pid);
108 }
109 
110 // Same as above, but send the max number of file descriptors too.
TEST(UnixDomainSocketTest,RecvPidWithMaxDescriptors)111 TEST(UnixDomainSocketTest, RecvPidWithMaxDescriptors) {
112   int fds[2];
113   ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
114   ScopedFD recv_sock(fds[0]);
115   ScopedFD send_sock(fds[1]);
116 
117   ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get()));
118 
119   static const char kHello[] = "hello";
120   std::vector<int> send_fds(UnixDomainSocket::kMaxFileDescriptors,
121                             send_sock.get());
122   ASSERT_TRUE(UnixDomainSocket::SendMsg(
123       send_sock.get(), kHello, sizeof(kHello), send_fds));
124 
125   // Extra receiving buffer space to make sure we really received only
126   // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer.
127   char buf[sizeof(kHello) + 1];
128   ProcessId sender_pid;
129   std::vector<ScopedFD> recv_fds;
130   const ssize_t nread = UnixDomainSocket::RecvMsgWithPid(
131       recv_sock.get(), buf, sizeof(buf), &recv_fds, &sender_pid);
132   ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread));
133   ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello)));
134   ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors, recv_fds.size());
135 
136   ASSERT_EQ(getpid(), sender_pid);
137 }
138 
139 // Check that RecvMsgWithPid doesn't DCHECK fail when reading EOF from a
140 // disconnected socket.
TEST(UnixDomianSocketTest,RecvPidDisconnectedSocket)141 TEST(UnixDomianSocketTest, RecvPidDisconnectedSocket) {
142   int fds[2];
143   ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
144   ScopedFD recv_sock(fds[0]);
145   ScopedFD send_sock(fds[1]);
146 
147   ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get()));
148 
149   send_sock.reset();
150 
151   char ch;
152   ProcessId sender_pid;
153   std::vector<ScopedFD> recv_fds;
154   const ssize_t nread = UnixDomainSocket::RecvMsgWithPid(
155       recv_sock.get(), &ch, sizeof(ch), &recv_fds, &sender_pid);
156   ASSERT_EQ(0, nread);
157   ASSERT_EQ(-1, sender_pid);
158   ASSERT_EQ(0U, recv_fds.size());
159 }
160 
161 }  // namespace
162 
163 }  // namespace base
164