Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

Side by Side Diff: base/posix/unix_domain_socket_linux_unittest.cc

Issue 241863002: Extend UnixDomainSocket::RecvMsg to return sender's PID (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove sandbox unit tests for followup CL Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <sys/socket.h> 5 #include <sys/socket.h>
6 #include <sys/types.h> 6 #include <sys/types.h>
7 #include <unistd.h> 7 #include <unistd.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE. 70 // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE.
71 Pickle request; 71 Pickle request;
72 ASSERT_EQ(-1, 72 ASSERT_EQ(-1,
73 UnixDomainSocket::SendRecvMsg(fds[1], static_cast<uint8_t*>(NULL), 73 UnixDomainSocket::SendRecvMsg(fds[1], static_cast<uint8_t*>(NULL),
74 0U, static_cast<int*>(NULL), request)); 74 0U, static_cast<int*>(NULL), request));
75 ASSERT_EQ(EPIPE, errno); 75 ASSERT_EQ(EPIPE, errno);
76 // Restore the SIGPIPE handler. 76 // Restore the SIGPIPE handler.
77 ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL)); 77 ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL));
78 } 78 }
79 79
80 // Simple sanity check within a single process that receiving PIDs works.
81 TEST(UnixDomainSocketTest, RecvPid) {
82 int fds[2];
83 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
84 base::ScopedFD recv_sock(fds[0]);
85 base::ScopedFD send_sock(fds[1]);
86
87 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get()));
88
89 static const char kHello[] = "hello";
90 ASSERT_TRUE(UnixDomainSocket::SendMsg(
91 send_sock.get(), kHello, sizeof(kHello), std::vector<int>()));
willchan no longer on Chromium 2014/04/24 20:44:04 Generally we prefer arraysize to sizeof for arrays
mdempsky 2014/04/24 20:59:14 Noted, but the APIs used here take the number of b
willchan no longer on Chromium 2014/04/24 21:09:56 Oops, I missed that. Fine as is.
92
93 // Extra receiving buffer space to make sure we really received only
94 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer.
95 char buf[sizeof(kHello) + 1];
96 base::ProcessId sender_pid;
97 std::vector<int> fd_vec;
98 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid(
99 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid);
100 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread));
101 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello)));
102 ASSERT_EQ(0U, fd_vec.size());
103
104 ASSERT_EQ(getpid(), sender_pid);
105 }
106
107 // Same as above, but send the max number of file descriptors too.
108 TEST(UnixDomainSocketTest, RecvPidWithMaxDescriptors) {
109 int fds[2];
110 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
111 base::ScopedFD recv_sock(fds[0]);
112 base::ScopedFD send_sock(fds[1]);
113
114 ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get()));
115
116 static const char kHello[] = "hello";
117 std::vector<int> fd_vec(UnixDomainSocket::kMaxFileDescriptors,
118 send_sock.get());
119 ASSERT_TRUE(UnixDomainSocket::SendMsg(
120 send_sock.get(), kHello, sizeof(kHello), fd_vec));
121
122 // Extra receiving buffer space to make sure we really received only
123 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer.
124 char buf[sizeof(kHello) + 1];
125 base::ProcessId sender_pid;
126 const ssize_t nread = UnixDomainSocket::RecvMsgWithPid(
127 recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid);
128 ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread));
129 ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello)));
130 ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors, fd_vec.size());
131 for (size_t i = 0; i < UnixDomainSocket::kMaxFileDescriptors; i++) {
132 ASSERT_EQ(0, IGNORE_EINTR(close(fd_vec[i])));
133 }
134
135 ASSERT_EQ(getpid(), sender_pid);
136 }
137
80 } // namespace 138 } // namespace
81 139
82 } // namespace base 140 } // namespace base
OLDNEW
« base/posix/unix_domain_socket_linux.h ('K') | « base/posix/unix_domain_socket_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698