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

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

Issue 191673003: Implement ScopedFD in terms of ScopedGeneric. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/files/scoped_file.h"
12 #include "base/pickle.h" 13 #include "base/pickle.h"
13 #include "base/posix/unix_domain_socket_linux.h" 14 #include "base/posix/unix_domain_socket_linux.h"
14 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h" 16 #include "base/threading/thread.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace base { 19 namespace base {
19 20
20 namespace { 21 namespace {
21 22
22 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) { 23 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) {
23 Thread message_thread("UnixDomainSocketTest"); 24 Thread message_thread("UnixDomainSocketTest");
24 ASSERT_TRUE(message_thread.Start()); 25 ASSERT_TRUE(message_thread.Start());
25 26
26 int fds[2]; 27 int fds[2];
27 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); 28 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
28 file_util::ScopedFD scoped_fd0(&fds[0]); 29 ScopedFD scoped_fd0(fds[0]);
29 file_util::ScopedFD scoped_fd1(&fds[1]); 30 ScopedFD scoped_fd1(fds[1]);
30 31
31 // Have the thread send a synchronous message via the socket. 32 // Have the thread send a synchronous message via the socket.
32 Pickle request; 33 Pickle request;
33 message_thread.message_loop()->PostTask( 34 message_thread.message_loop()->PostTask(
34 FROM_HERE, 35 FROM_HERE,
35 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg), 36 Bind(IgnoreResult(&UnixDomainSocket::SendRecvMsg),
36 fds[1], static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL), 37 fds[1], static_cast<uint8_t*>(NULL), 0U, static_cast<int*>(NULL),
37 request)); 38 request));
38 39
39 // Receive the message. 40 // Receive the message.
(...skipping 15 matching lines...) Expand all
55 ASSERT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(5000))); 56 ASSERT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(5000)));
56 } 57 }
57 58
58 TEST(UnixDomainSocketTest, SendRecvMsgAvoidsSIGPIPE) { 59 TEST(UnixDomainSocketTest, SendRecvMsgAvoidsSIGPIPE) {
59 // Make sure SIGPIPE isn't being ignored. 60 // Make sure SIGPIPE isn't being ignored.
60 struct sigaction act = {}, oldact; 61 struct sigaction act = {}, oldact;
61 act.sa_handler = SIG_DFL; 62 act.sa_handler = SIG_DFL;
62 ASSERT_EQ(0, sigaction(SIGPIPE, &act, &oldact)); 63 ASSERT_EQ(0, sigaction(SIGPIPE, &act, &oldact));
63 int fds[2]; 64 int fds[2];
64 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); 65 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds));
65 file_util::ScopedFD scoped_fd1(&fds[1]); 66 ScopedFD scoped_fd1(fds[1]);
66 ASSERT_EQ(0, IGNORE_EINTR(close(fds[0]))); 67 ASSERT_EQ(0, IGNORE_EINTR(close(fds[0])));
67 68
68 // Have the thread send a synchronous message via the socket. Unless the 69 // 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 // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE.
70 Pickle request; 71 Pickle request;
71 ASSERT_EQ(-1, 72 ASSERT_EQ(-1,
72 UnixDomainSocket::SendRecvMsg(fds[1], static_cast<uint8_t*>(NULL), 73 UnixDomainSocket::SendRecvMsg(fds[1], static_cast<uint8_t*>(NULL),
73 0U, static_cast<int*>(NULL), request)); 74 0U, static_cast<int*>(NULL), request));
74 ASSERT_EQ(EPIPE, errno); 75 ASSERT_EQ(EPIPE, errno);
75 // Restore the SIGPIPE handler. 76 // Restore the SIGPIPE handler.
76 ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL)); 77 ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, NULL));
77 } 78 }
78 79
79 } // namespace 80 } // namespace
80 81
81 } // namespace base 82 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698