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

Side by Side Diff: net/socket/unix_domain_socket_posix_unittest.cc

Issue 376323002: Refactor unix domain socket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Checked uid and gid in unittests. Created 6 years, 5 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/socket/unix_domain_socket_posix.h"
6
5 #include <errno.h> 7 #include <errno.h>
6 #include <fcntl.h> 8 #include <fcntl.h>
7 #include <poll.h> 9 #include <poll.h>
8 #include <sys/socket.h> 10 #include <sys/socket.h>
9 #include <sys/stat.h> 11 #include <sys/stat.h>
10 #include <sys/time.h> 12 #include <sys/time.h>
11 #include <sys/types.h> 13 #include <sys/types.h>
12 #include <sys/un.h> 14 #include <sys/un.h>
13 #include <unistd.h> 15 #include <unistd.h>
14 16
15 #include <cstring> 17 #include <cstring>
16 #include <queue> 18 #include <queue>
17 #include <string> 19 #include <string>
18 20
19 #include "base/bind.h" 21 #include "base/bind.h"
20 #include "base/callback.h" 22 #include "base/callback.h"
21 #include "base/compiler_specific.h" 23 #include "base/compiler_specific.h"
22 #include "base/file_util.h" 24 #include "base/file_util.h"
23 #include "base/files/file_path.h" 25 #include "base/files/file_path.h"
26 #include "base/files/scoped_temp_dir.h"
24 #include "base/memory/ref_counted.h" 27 #include "base/memory/ref_counted.h"
25 #include "base/memory/scoped_ptr.h" 28 #include "base/memory/scoped_ptr.h"
26 #include "base/message_loop/message_loop.h" 29 #include "base/message_loop/message_loop.h"
27 #include "base/posix/eintr_wrapper.h" 30 #include "base/posix/eintr_wrapper.h"
28 #include "base/synchronization/condition_variable.h" 31 #include "base/synchronization/condition_variable.h"
29 #include "base/synchronization/lock.h" 32 #include "base/synchronization/lock.h"
30 #include "base/threading/platform_thread.h" 33 #include "base/threading/platform_thread.h"
31 #include "base/threading/thread.h" 34 #include "base/threading/thread.h"
32 #include "net/socket/socket_descriptor.h" 35 #include "net/socket/socket_descriptor.h"
33 #include "net/socket/unix_domain_socket_posix.h"
34 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
35 37
36 using std::queue; 38 using std::queue;
37 using std::string; 39 using std::string;
38 40
39 namespace net { 41 namespace net {
40 namespace { 42 namespace {
41 43
42 const char kSocketFilename[] = "unix_domain_socket_for_testing"; 44 const char kSocketFilename[] = "unix_domain_socket_for_testing";
43 const char kInvalidSocketPath[] = "/invalid/path"; 45 const char kInvalidSocketPath[] = "/invalid/path";
44 const char kMsg[] = "hello"; 46 const char kMsg[] = "hello";
45 47
46 enum EventType { 48 enum EventType {
47 EVENT_ACCEPT, 49 EVENT_ACCEPT,
48 EVENT_AUTH_DENIED, 50 EVENT_AUTH_DENIED,
49 EVENT_AUTH_GRANTED, 51 EVENT_AUTH_GRANTED,
50 EVENT_CLOSE, 52 EVENT_CLOSE,
51 EVENT_LISTEN, 53 EVENT_LISTEN,
52 EVENT_READ, 54 EVENT_READ,
53 }; 55 };
54 56
55 string MakeSocketPath(const string& socket_file_name) {
56 base::FilePath temp_dir;
57 base::GetTempDir(&temp_dir);
58 return temp_dir.Append(socket_file_name).value();
59 }
60
61 string MakeSocketPath() {
62 return MakeSocketPath(kSocketFilename);
63 }
64
65 class EventManager : public base::RefCounted<EventManager> { 57 class EventManager : public base::RefCounted<EventManager> {
66 public: 58 public:
67 EventManager() : condition_(&mutex_) {} 59 EventManager() : condition_(&mutex_) {}
68 60
69 bool HasPendingEvent() { 61 bool HasPendingEvent() {
70 base::AutoLock lock(mutex_); 62 base::AutoLock lock(mutex_);
71 return !events_.empty(); 63 return !events_.empty();
72 } 64 }
73 65
74 void Notify(EventType event) { 66 void Notify(EventType event) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 }; 136 };
145 137
146 bool UserCanConnectCallback( 138 bool UserCanConnectCallback(
147 bool allow_user, const scoped_refptr<EventManager>& event_manager, 139 bool allow_user, const scoped_refptr<EventManager>& event_manager,
148 uid_t, gid_t) { 140 uid_t, gid_t) {
149 event_manager->Notify( 141 event_manager->Notify(
150 allow_user ? EVENT_AUTH_GRANTED : EVENT_AUTH_DENIED); 142 allow_user ? EVENT_AUTH_GRANTED : EVENT_AUTH_DENIED);
151 return allow_user; 143 return allow_user;
152 } 144 }
153 145
154 class UnixDomainSocketTestHelper : public testing::Test { 146 class UnixDomainListenSocketTestHelper : public testing::Test {
155 public: 147 public:
156 void CreateAndListen() { 148 void CreateAndListen() {
157 socket_ = UnixDomainSocket::CreateAndListen( 149 socket_ = UnixDomainListenSocket::CreateAndListen(
158 file_path_.value(), socket_delegate_.get(), MakeAuthCallback()); 150 file_path_.value(), socket_delegate_.get(), MakeAuthCallback());
159 socket_delegate_->OnListenCompleted(); 151 socket_delegate_->OnListenCompleted();
160 } 152 }
161 153
162 protected: 154 protected:
163 UnixDomainSocketTestHelper(const string& path, bool allow_user) 155 UnixDomainListenSocketTestHelper(const string& path_str, bool allow_user)
164 : file_path_(path), 156 : allow_user_(allow_user) {
165 allow_user_(allow_user) {} 157 file_path_ = base::FilePath(path_str);
158 if (!file_path_.IsAbsolute()) {
159 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
160 file_path_ = GetTempSocketPath(file_path_.value());
161 }
162 // Beware that if path_str is an absolute path, this class doesn't delete
163 // the file. It must be an invalid path and cannot be created by unittests.
164 }
165
166 base::FilePath GetTempSocketPath(const std::string socket_name) {
167 DCHECK(temp_dir_.IsValid());
168 return temp_dir_.path().Append(socket_name);
169 }
166 170
167 virtual void SetUp() OVERRIDE { 171 virtual void SetUp() OVERRIDE {
168 event_manager_ = new EventManager(); 172 event_manager_ = new EventManager();
169 socket_delegate_.reset(new TestListenSocketDelegate(event_manager_)); 173 socket_delegate_.reset(new TestListenSocketDelegate(event_manager_));
170 DeleteSocketFile();
171 } 174 }
172 175
173 virtual void TearDown() OVERRIDE { 176 virtual void TearDown() OVERRIDE {
174 DeleteSocketFile();
175 socket_.reset(); 177 socket_.reset();
176 socket_delegate_.reset(); 178 socket_delegate_.reset();
177 event_manager_ = NULL; 179 event_manager_ = NULL;
178 } 180 }
179 181
180 UnixDomainSocket::AuthCallback MakeAuthCallback() { 182 UnixDomainListenSocket::AuthCallback MakeAuthCallback() {
181 return base::Bind(&UserCanConnectCallback, allow_user_, event_manager_); 183 return base::Bind(&UserCanConnectCallback, allow_user_, event_manager_);
182 } 184 }
183 185
184 void DeleteSocketFile() {
185 ASSERT_FALSE(file_path_.empty());
186 base::DeleteFile(file_path_, false /* not recursive */);
187 }
188
189 SocketDescriptor CreateClientSocket() { 186 SocketDescriptor CreateClientSocket() {
190 const SocketDescriptor sock = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0); 187 const SocketDescriptor sock = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0);
191 if (sock < 0) { 188 if (sock < 0) {
192 LOG(ERROR) << "socket() error"; 189 LOG(ERROR) << "socket() error";
193 return kInvalidSocket; 190 return kInvalidSocket;
194 } 191 }
195 sockaddr_un addr; 192 sockaddr_un addr;
196 memset(&addr, 0, sizeof(addr)); 193 memset(&addr, 0, sizeof(addr));
197 addr.sun_family = AF_UNIX; 194 addr.sun_family = AF_UNIX;
198 socklen_t addr_len; 195 socklen_t addr_len;
199 strncpy(addr.sun_path, file_path_.value().c_str(), sizeof(addr.sun_path)); 196 strncpy(addr.sun_path, file_path_.value().c_str(), sizeof(addr.sun_path));
200 addr_len = sizeof(sockaddr_un); 197 addr_len = sizeof(sockaddr_un);
201 if (connect(sock, reinterpret_cast<sockaddr*>(&addr), addr_len) != 0) { 198 if (connect(sock, reinterpret_cast<sockaddr*>(&addr), addr_len) != 0) {
202 LOG(ERROR) << "connect() error"; 199 LOG(ERROR) << "connect() error: errno=" << errno;
203 return kInvalidSocket; 200 return kInvalidSocket;
204 } 201 }
205 return sock; 202 return sock;
206 } 203 }
207 204
208 scoped_ptr<base::Thread> CreateAndRunServerThread() { 205 scoped_ptr<base::Thread> CreateAndRunServerThread() {
209 base::Thread::Options options; 206 base::Thread::Options options;
210 options.message_loop_type = base::MessageLoop::TYPE_IO; 207 options.message_loop_type = base::MessageLoop::TYPE_IO;
211 scoped_ptr<base::Thread> thread(new base::Thread("socketio_test")); 208 scoped_ptr<base::Thread> thread(new base::Thread("socketio_test"));
212 thread->StartWithOptions(options); 209 thread->StartWithOptions(options);
213 thread->message_loop()->PostTask( 210 thread->message_loop()->PostTask(
214 FROM_HERE, 211 FROM_HERE,
215 base::Bind(&UnixDomainSocketTestHelper::CreateAndListen, 212 base::Bind(&UnixDomainListenSocketTestHelper::CreateAndListen,
216 base::Unretained(this))); 213 base::Unretained(this)));
217 return thread.Pass(); 214 return thread.Pass();
218 } 215 }
219 216
220 const base::FilePath file_path_; 217 base::ScopedTempDir temp_dir_;
218 base::FilePath file_path_;
221 const bool allow_user_; 219 const bool allow_user_;
222 scoped_refptr<EventManager> event_manager_; 220 scoped_refptr<EventManager> event_manager_;
223 scoped_ptr<TestListenSocketDelegate> socket_delegate_; 221 scoped_ptr<TestListenSocketDelegate> socket_delegate_;
224 scoped_ptr<UnixDomainSocket> socket_; 222 scoped_ptr<UnixDomainListenSocket> socket_;
225 }; 223 };
226 224
227 class UnixDomainSocketTest : public UnixDomainSocketTestHelper { 225 class UnixDomainListenSocketTest : public UnixDomainListenSocketTestHelper {
228 protected: 226 protected:
229 UnixDomainSocketTest() 227 UnixDomainListenSocketTest()
230 : UnixDomainSocketTestHelper(MakeSocketPath(), true /* allow user */) {} 228 : UnixDomainListenSocketTestHelper(kSocketFilename,
229 true /* allow user */) {}
231 }; 230 };
232 231
233 class UnixDomainSocketTestWithInvalidPath : public UnixDomainSocketTestHelper { 232 class UnixDomainListenSocketTestWithInvalidPath
233 : public UnixDomainListenSocketTestHelper {
234 protected: 234 protected:
235 UnixDomainSocketTestWithInvalidPath() 235 UnixDomainListenSocketTestWithInvalidPath()
236 : UnixDomainSocketTestHelper(kInvalidSocketPath, true) {} 236 : UnixDomainListenSocketTestHelper(kInvalidSocketPath, true) {}
237 }; 237 };
238 238
239 class UnixDomainSocketTestWithForbiddenUser 239 class UnixDomainListenSocketTestWithForbiddenUser
240 : public UnixDomainSocketTestHelper { 240 : public UnixDomainListenSocketTestHelper {
241 protected: 241 protected:
242 UnixDomainSocketTestWithForbiddenUser() 242 UnixDomainListenSocketTestWithForbiddenUser()
243 : UnixDomainSocketTestHelper(MakeSocketPath(), false /* forbid user */) {} 243 : UnixDomainListenSocketTestHelper(kSocketFilename,
244 false /* forbid user */) {}
244 }; 245 };
245 246
246 TEST_F(UnixDomainSocketTest, CreateAndListen) { 247 TEST_F(UnixDomainListenSocketTest, CreateAndListen) {
247 CreateAndListen(); 248 CreateAndListen();
248 EXPECT_FALSE(socket_.get() == NULL); 249 EXPECT_FALSE(socket_.get() == NULL);
249 } 250 }
250 251
251 TEST_F(UnixDomainSocketTestWithInvalidPath, CreateAndListenWithInvalidPath) { 252 TEST_F(UnixDomainListenSocketTestWithInvalidPath,
253 CreateAndListenWithInvalidPath) {
252 CreateAndListen(); 254 CreateAndListen();
253 EXPECT_TRUE(socket_.get() == NULL); 255 EXPECT_TRUE(socket_.get() == NULL);
254 } 256 }
255 257
256 #ifdef SOCKET_ABSTRACT_NAMESPACE_SUPPORTED 258 #ifdef SOCKET_ABSTRACT_NAMESPACE_SUPPORTED
257 // Test with an invalid path to make sure that the socket is not backed by a 259 // Test with an invalid path to make sure that the socket is not backed by a
258 // file. 260 // file.
259 TEST_F(UnixDomainSocketTestWithInvalidPath, 261 TEST_F(UnixDomainListenSocketTestWithInvalidPath,
260 CreateAndListenWithAbstractNamespace) { 262 CreateAndListenWithAbstractNamespace) {
261 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( 263 socket_ = UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
262 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); 264 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback());
263 EXPECT_FALSE(socket_.get() == NULL); 265 EXPECT_FALSE(socket_.get() == NULL);
264 } 266 }
265 267
266 TEST_F(UnixDomainSocketTest, TestFallbackName) { 268 TEST_F(UnixDomainListenSocketTest, TestFallbackName) {
267 scoped_ptr<UnixDomainSocket> existing_socket = 269 scoped_ptr<UnixDomainListenSocket> existing_socket =
268 UnixDomainSocket::CreateAndListenWithAbstractNamespace( 270 UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
269 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); 271 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback());
270 EXPECT_FALSE(existing_socket.get() == NULL); 272 EXPECT_FALSE(existing_socket.get() == NULL);
271 // First, try to bind socket with the same name with no fallback name. 273 // First, try to bind socket with the same name with no fallback name.
272 socket_ = 274 socket_ =
273 UnixDomainSocket::CreateAndListenWithAbstractNamespace( 275 UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
274 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); 276 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback());
275 EXPECT_TRUE(socket_.get() == NULL); 277 EXPECT_TRUE(socket_.get() == NULL);
276 // Now with a fallback name. 278 // Now with a fallback name.
277 const char kFallbackSocketName[] = "unix_domain_socket_for_testing_2"; 279 const char kFallbackSocketName[] = "unix_domain_socket_for_testing_2";
278 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( 280 socket_ = UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
279 file_path_.value(), 281 file_path_.value(),
280 MakeSocketPath(kFallbackSocketName), 282 GetTempSocketPath(kFallbackSocketName).value(),
281 socket_delegate_.get(), 283 socket_delegate_.get(),
282 MakeAuthCallback()); 284 MakeAuthCallback());
283 EXPECT_FALSE(socket_.get() == NULL); 285 EXPECT_FALSE(socket_.get() == NULL);
284 } 286 }
285 #endif 287 #endif
286 288
287 TEST_F(UnixDomainSocketTest, TestWithClient) { 289 TEST_F(UnixDomainListenSocketTest, TestWithClient) {
288 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); 290 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread();
289 EventType event = event_manager_->WaitForEvent(); 291 EventType event = event_manager_->WaitForEvent();
290 ASSERT_EQ(EVENT_LISTEN, event); 292 ASSERT_EQ(EVENT_LISTEN, event);
291 293
292 // Create the client socket. 294 // Create the client socket.
293 const SocketDescriptor sock = CreateClientSocket(); 295 const SocketDescriptor sock = CreateClientSocket();
294 ASSERT_NE(kInvalidSocket, sock); 296 ASSERT_NE(kInvalidSocket, sock);
295 event = event_manager_->WaitForEvent(); 297 event = event_manager_->WaitForEvent();
296 ASSERT_EQ(EVENT_AUTH_GRANTED, event); 298 ASSERT_EQ(EVENT_AUTH_GRANTED, event);
297 event = event_manager_->WaitForEvent(); 299 event = event_manager_->WaitForEvent();
298 ASSERT_EQ(EVENT_ACCEPT, event); 300 ASSERT_EQ(EVENT_ACCEPT, event);
299 301
300 // Send a message from the client to the server. 302 // Send a message from the client to the server.
301 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); 303 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0));
302 ASSERT_NE(-1, ret); 304 ASSERT_NE(-1, ret);
303 ASSERT_EQ(sizeof(kMsg), static_cast<size_t>(ret)); 305 ASSERT_EQ(sizeof(kMsg), static_cast<size_t>(ret));
304 event = event_manager_->WaitForEvent(); 306 event = event_manager_->WaitForEvent();
305 ASSERT_EQ(EVENT_READ, event); 307 ASSERT_EQ(EVENT_READ, event);
306 ASSERT_EQ(kMsg, socket_delegate_->ReceivedData()); 308 ASSERT_EQ(kMsg, socket_delegate_->ReceivedData());
307 309
308 // Close the client socket. 310 // Close the client socket.
309 ret = IGNORE_EINTR(close(sock)); 311 ret = IGNORE_EINTR(close(sock));
310 event = event_manager_->WaitForEvent(); 312 event = event_manager_->WaitForEvent();
311 ASSERT_EQ(EVENT_CLOSE, event); 313 ASSERT_EQ(EVENT_CLOSE, event);
312 } 314 }
313 315
314 TEST_F(UnixDomainSocketTestWithForbiddenUser, TestWithForbiddenUser) { 316 TEST_F(UnixDomainListenSocketTestWithForbiddenUser, TestWithForbiddenUser) {
315 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); 317 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread();
316 EventType event = event_manager_->WaitForEvent(); 318 EventType event = event_manager_->WaitForEvent();
317 ASSERT_EQ(EVENT_LISTEN, event); 319 ASSERT_EQ(EVENT_LISTEN, event);
318 const SocketDescriptor sock = CreateClientSocket(); 320 const SocketDescriptor sock = CreateClientSocket();
319 ASSERT_NE(kInvalidSocket, sock); 321 ASSERT_NE(kInvalidSocket, sock);
320 322
321 event = event_manager_->WaitForEvent(); 323 event = event_manager_->WaitForEvent();
322 ASSERT_EQ(EVENT_AUTH_DENIED, event); 324 ASSERT_EQ(EVENT_AUTH_DENIED, event);
323 325
324 // Wait until the file descriptor is closed by the server. 326 // Wait until the file descriptor is closed by the server.
325 struct pollfd poll_fd; 327 struct pollfd poll_fd;
326 poll_fd.fd = sock; 328 poll_fd.fd = sock;
327 poll_fd.events = POLLIN; 329 poll_fd.events = POLLIN;
328 poll(&poll_fd, 1, -1 /* rely on GTest for timeout handling */); 330 poll(&poll_fd, 1, -1 /* rely on GTest for timeout handling */);
329 331
330 // Send() must fail. 332 // Send() must fail.
331 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); 333 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0));
332 ASSERT_EQ(-1, ret); 334 ASSERT_EQ(-1, ret);
333 ASSERT_EQ(EPIPE, errno); 335 ASSERT_EQ(EPIPE, errno);
334 ASSERT_FALSE(event_manager_->HasPendingEvent()); 336 ASSERT_FALSE(event_manager_->HasPendingEvent());
335 } 337 }
336 338
337 } // namespace 339 } // namespace
338 } // namespace net 340 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698