tedvessenes@gmail.com | 56b3370 | 2012-03-07 13:41:40 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // These tests are POSIX only. |
| 6 | |
| 7 | #include "ipc/ipc_channel_posix.h" |
| 8 | |
| 9 | #include <fcntl.h> |
| 10 | #include <sys/socket.h> |
| 11 | #include <sys/un.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #include "base/basictypes.h" |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
thestig | df416e1 | 2014-09-13 10:02:11 +0900 | [diff] [blame] | 16 | #include "base/files/file_util.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 17 | #include "base/memory/scoped_ptr.h" |
avi@chromium.org | a29af56 | 2013-07-18 08:00:30 +0900 | [diff] [blame] | 18 | #include "base/message_loop/message_loop.h" |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 19 | #include "base/path_service.h" |
brettw@chromium.org | b1788fb | 2012-11-15 05:54:35 +0900 | [diff] [blame] | 20 | #include "base/posix/eintr_wrapper.h" |
rsesek@chromium.org | 1931971 | 2013-07-24 14:15:24 +0900 | [diff] [blame] | 21 | #include "base/process/kill.h" |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 22 | #include "base/test/multiprocess_test.h" |
| 23 | #include "base/test/test_timeouts.h" |
brettw@chromium.org | f947ed0 | 2012-06-12 07:35:26 +0900 | [diff] [blame] | 24 | #include "ipc/ipc_listener.h" |
jeremya@chromium.org | f14bfab | 2013-03-13 13:23:10 +0900 | [diff] [blame] | 25 | #include "ipc/unix_domain_socket_util.h" |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 26 | #include "testing/multiprocess_func_list.h" |
| 27 | |
| 28 | namespace { |
| 29 | |
pkasting@chromium.org | c4f2de2 | 2011-09-01 09:46:33 +0900 | [diff] [blame] | 30 | static const uint32 kQuitMessage = 47; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 31 | |
brettw@chromium.org | f947ed0 | 2012-06-12 07:35:26 +0900 | [diff] [blame] | 32 | class IPCChannelPosixTestListener : public IPC::Listener { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 33 | public: |
| 34 | enum STATUS { |
| 35 | DISCONNECTED, |
| 36 | MESSAGE_RECEIVED, |
| 37 | CHANNEL_ERROR, |
| 38 | CONNECTED, |
| 39 | DENIED, |
| 40 | LISTEN_ERROR |
| 41 | }; |
| 42 | |
| 43 | IPCChannelPosixTestListener(bool quit_only_on_message) |
hubbe@chromium.org | a53c97c | 2014-02-21 09:18:29 +0900 | [diff] [blame] | 44 | : status_(DISCONNECTED), |
| 45 | quit_only_on_message_(quit_only_on_message) { |
| 46 | } |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 47 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 48 | ~IPCChannelPosixTestListener() override {} |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 49 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 50 | bool OnMessageReceived(const IPC::Message& message) override { |
pkasting@chromium.org | c4f2de2 | 2011-09-01 09:46:33 +0900 | [diff] [blame] | 51 | EXPECT_EQ(message.type(), kQuitMessage); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 52 | status_ = MESSAGE_RECEIVED; |
| 53 | QuitRunLoop(); |
jam@chromium.org | 8a2c784 | 2010-12-24 15:19:28 +0900 | [diff] [blame] | 54 | return true; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 55 | } |
| 56 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 57 | void OnChannelConnected(int32 peer_pid) override { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 58 | status_ = CONNECTED; |
| 59 | if (!quit_only_on_message_) { |
| 60 | QuitRunLoop(); |
| 61 | } |
| 62 | } |
| 63 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 64 | void OnChannelError() override { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 65 | status_ = CHANNEL_ERROR; |
hubbe@chromium.org | a53c97c | 2014-02-21 09:18:29 +0900 | [diff] [blame] | 66 | QuitRunLoop(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 67 | } |
| 68 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 69 | void OnChannelDenied() override { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 70 | status_ = DENIED; |
| 71 | if (!quit_only_on_message_) { |
| 72 | QuitRunLoop(); |
| 73 | } |
| 74 | } |
| 75 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 76 | void OnChannelListenError() override { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 77 | status_ = LISTEN_ERROR; |
| 78 | if (!quit_only_on_message_) { |
| 79 | QuitRunLoop(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | STATUS status() { return status_; } |
| 84 | |
| 85 | void QuitRunLoop() { |
hubbe@chromium.org | a53c97c | 2014-02-21 09:18:29 +0900 | [diff] [blame] | 86 | base::MessageLoopForIO* loop = base::MessageLoopForIO::current(); |
| 87 | if (loop->is_running()) { |
| 88 | loop->QuitNow(); |
| 89 | } else { |
| 90 | // Die as soon as Run is called. |
| 91 | loop->PostTask(FROM_HERE, loop->QuitClosure()); |
| 92 | } |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private: |
| 96 | // The current status of the listener. |
| 97 | STATUS status_; |
| 98 | // If |quit_only_on_message_| then the listener will only break out of |
pkasting@chromium.org | c4f2de2 | 2011-09-01 09:46:33 +0900 | [diff] [blame] | 99 | // the run loop when kQuitMessage is received. |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 100 | bool quit_only_on_message_; |
| 101 | }; |
| 102 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 103 | class IPCChannelPosixTest : public base::MultiProcessTest { |
| 104 | public: |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 105 | static void SetUpSocket(IPC::ChannelHandle *handle, |
| 106 | IPC::Channel::Mode mode); |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 107 | static void SpinRunLoop(base::TimeDelta delay); |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 108 | static const std::string GetConnectionSocketName(); |
| 109 | static const std::string GetChannelDirName(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 110 | |
| 111 | protected: |
dcheng | cd52576 | 2014-12-24 04:59:59 +0900 | [diff] [blame^] | 112 | void SetUp() override; |
| 113 | void TearDown() override; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 114 | |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 115 | private: |
| 116 | scoped_ptr<base::MessageLoopForIO> message_loop_; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 117 | }; |
| 118 | |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 119 | const std::string IPCChannelPosixTest::GetChannelDirName() { |
jrg@chromium.org | 2eb192e | 2011-11-04 09:14:16 +0900 | [diff] [blame] | 120 | #if defined(OS_ANDROID) |
brettw@chromium.org | 22b3fda | 2013-02-10 13:49:30 +0900 | [diff] [blame] | 121 | base::FilePath tmp_dir; |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 122 | PathService::Get(base::DIR_CACHE, &tmp_dir); |
| 123 | return tmp_dir.value(); |
jrg@chromium.org | 2eb192e | 2011-11-04 09:14:16 +0900 | [diff] [blame] | 124 | #else |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 125 | return "/var/tmp"; |
jrg@chromium.org | 2eb192e | 2011-11-04 09:14:16 +0900 | [diff] [blame] | 126 | #endif |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | const std::string IPCChannelPosixTest::GetConnectionSocketName() { |
| 130 | return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket"; |
| 131 | } |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 132 | |
| 133 | void IPCChannelPosixTest::SetUp() { |
| 134 | MultiProcessTest::SetUp(); |
| 135 | // Construct a fresh IO Message loop for the duration of each test. |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 136 | message_loop_.reset(new base::MessageLoopForIO()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void IPCChannelPosixTest::TearDown() { |
| 140 | message_loop_.reset(NULL); |
| 141 | MultiProcessTest::TearDown(); |
| 142 | } |
| 143 | |
| 144 | // Create up a socket and bind and listen to it, or connect it |
| 145 | // depending on the |mode|. |
| 146 | void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle, |
| 147 | IPC::Channel::Mode mode) { |
| 148 | const std::string& name = handle->name; |
| 149 | |
| 150 | int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 151 | ASSERT_GE(socket_fd, 0) << name; |
| 152 | ASSERT_GE(fcntl(socket_fd, F_SETFL, O_NONBLOCK), 0); |
| 153 | struct sockaddr_un server_address = { 0 }; |
| 154 | memset(&server_address, 0, sizeof(server_address)); |
| 155 | server_address.sun_family = AF_UNIX; |
jeremya@chromium.org | f14bfab | 2013-03-13 13:23:10 +0900 | [diff] [blame] | 156 | int path_len = snprintf(server_address.sun_path, IPC::kMaxSocketNameLength, |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 157 | "%s", name.c_str()); |
| 158 | DCHECK_EQ(static_cast<int>(name.length()), path_len); |
| 159 | size_t server_address_len = offsetof(struct sockaddr_un, |
| 160 | sun_path) + path_len + 1; |
| 161 | |
| 162 | if (mode == IPC::Channel::MODE_NAMED_SERVER) { |
| 163 | // Only one server at a time. Cleanup garbage if it exists. |
| 164 | unlink(name.c_str()); |
pkasting@chromium.org | c4f2de2 | 2011-09-01 09:46:33 +0900 | [diff] [blame] | 165 | // Make sure the path we need exists. |
brettw@chromium.org | 22b3fda | 2013-02-10 13:49:30 +0900 | [diff] [blame] | 166 | base::FilePath path(name); |
| 167 | base::FilePath dir_path = path.DirName(); |
brettw@chromium.org | 738669a | 2013-12-04 05:08:54 +0900 | [diff] [blame] | 168 | ASSERT_TRUE(base::CreateDirectory(dir_path)); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 169 | ASSERT_GE(bind(socket_fd, |
| 170 | reinterpret_cast<struct sockaddr *>(&server_address), |
| 171 | server_address_len), 0) << server_address.sun_path |
| 172 | << ": " << strerror(errno) |
| 173 | << "(" << errno << ")"; |
| 174 | ASSERT_GE(listen(socket_fd, SOMAXCONN), 0) << server_address.sun_path |
| 175 | << ": " << strerror(errno) |
| 176 | << "(" << errno << ")"; |
| 177 | } else if (mode == IPC::Channel::MODE_NAMED_CLIENT) { |
| 178 | ASSERT_GE(connect(socket_fd, |
| 179 | reinterpret_cast<struct sockaddr *>(&server_address), |
| 180 | server_address_len), 0) << server_address.sun_path |
| 181 | << ": " << strerror(errno) |
| 182 | << "(" << errno << ")"; |
| 183 | } else { |
| 184 | FAIL() << "Unknown mode " << mode; |
| 185 | } |
| 186 | handle->socket.fd = socket_fd; |
| 187 | } |
| 188 | |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 189 | void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay) { |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 190 | base::MessageLoopForIO* loop = base::MessageLoopForIO::current(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 191 | // Post a quit task so that this loop eventually ends and we don't hang |
| 192 | // in the case of a bad test. Usually, the run loop will quit sooner than |
| 193 | // that because all tests use a IPCChannelPosixTestListener which quits the |
| 194 | // current run loop on any channel activity. |
hubbe@chromium.org | a53c97c | 2014-02-21 09:18:29 +0900 | [diff] [blame] | 195 | loop->PostDelayedTask(FROM_HERE, loop->QuitClosure(), delay); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 196 | loop->Run(); |
| 197 | } |
| 198 | |
| 199 | TEST_F(IPCChannelPosixTest, BasicListen) { |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 200 | const std::string kChannelName = |
| 201 | GetChannelDirName() + "/IPCChannelPosixTest_BasicListen"; |
jrg@chromium.org | 2eb192e | 2011-11-04 09:14:16 +0900 | [diff] [blame] | 202 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 203 | // Test creating a socket that is listening. |
jrg@chromium.org | 2eb192e | 2011-11-04 09:14:16 +0900 | [diff] [blame] | 204 | IPC::ChannelHandle handle(kChannelName); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 205 | SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); |
| 206 | unlink(handle.name.c_str()); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 207 | scoped_ptr<IPC::ChannelPosix> channel( |
| 208 | new IPC::ChannelPosix(handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 209 | ASSERT_TRUE(channel->Connect()); |
| 210 | ASSERT_TRUE(channel->AcceptsConnections()); |
| 211 | ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 212 | channel->ResetToAcceptingConnectionState(); |
| 213 | ASSERT_FALSE(channel->HasAcceptedConnection()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | TEST_F(IPCChannelPosixTest, BasicConnected) { |
| 217 | // Test creating a socket that is connected. |
| 218 | int pipe_fds[2]; |
| 219 | ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds)); |
| 220 | std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected"); |
| 221 | ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0); |
| 222 | |
| 223 | base::FileDescriptor fd(pipe_fds[0], false); |
| 224 | IPC::ChannelHandle handle(socket_name, fd); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 225 | scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 226 | handle, IPC::Channel::MODE_SERVER, NULL)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 227 | ASSERT_TRUE(channel->Connect()); |
| 228 | ASSERT_FALSE(channel->AcceptsConnections()); |
| 229 | channel->Close(); |
mark@chromium.org | fa5a0f9 | 2013-12-03 23:10:59 +0900 | [diff] [blame] | 230 | ASSERT_TRUE(IGNORE_EINTR(close(pipe_fds[1])) == 0); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 231 | |
| 232 | // Make sure that we can use the socket that is created for us by |
| 233 | // a standard channel. |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 234 | scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix( |
| 235 | socket_name, IPC::Channel::MODE_SERVER, NULL)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 236 | ASSERT_TRUE(channel2->Connect()); |
| 237 | ASSERT_FALSE(channel2->AcceptsConnections()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 238 | } |
| 239 | |
hubbe@chromium.org | a53c97c | 2014-02-21 09:18:29 +0900 | [diff] [blame] | 240 | // If a connection closes right before a Send() call, we may end up closing |
| 241 | // the connection without notifying the listener, which can cause hangs in |
| 242 | // sync_message_filter and others. Make sure the listener is notified. |
| 243 | TEST_F(IPCChannelPosixTest, SendHangTest) { |
| 244 | IPCChannelPosixTestListener out_listener(true); |
| 245 | IPCChannelPosixTestListener in_listener(true); |
| 246 | IPC::ChannelHandle in_handle("IN"); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 247 | scoped_ptr<IPC::ChannelPosix> in_chan(new IPC::ChannelPosix( |
| 248 | in_handle, IPC::Channel::MODE_SERVER, &in_listener)); |
morrita | 13e514d | 2014-10-21 08:53:25 +0900 | [diff] [blame] | 249 | IPC::ChannelHandle out_handle( |
| 250 | "OUT", base::FileDescriptor(in_chan->TakeClientFileDescriptor())); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 251 | scoped_ptr<IPC::ChannelPosix> out_chan(new IPC::ChannelPosix( |
| 252 | out_handle, IPC::Channel::MODE_CLIENT, &out_listener)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 253 | ASSERT_TRUE(in_chan->Connect()); |
| 254 | ASSERT_TRUE(out_chan->Connect()); |
| 255 | in_chan->Close(); // simulate remote process dying at an unfortunate time. |
hubbe@chromium.org | a53c97c | 2014-02-21 09:18:29 +0900 | [diff] [blame] | 256 | // Send will fail, because it cannot write the message. |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 257 | ASSERT_FALSE(out_chan->Send(new IPC::Message( |
hubbe@chromium.org | a53c97c | 2014-02-21 09:18:29 +0900 | [diff] [blame] | 258 | 0, // routing_id |
| 259 | kQuitMessage, // message type |
| 260 | IPC::Message::PRIORITY_NORMAL))); |
| 261 | SpinRunLoop(TestTimeouts::action_max_timeout()); |
| 262 | ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status()); |
| 263 | } |
| 264 | |
| 265 | // If a connection closes right before a Connect() call, we may end up closing |
| 266 | // the connection without notifying the listener, which can cause hangs in |
| 267 | // sync_message_filter and others. Make sure the listener is notified. |
| 268 | TEST_F(IPCChannelPosixTest, AcceptHangTest) { |
| 269 | IPCChannelPosixTestListener out_listener(true); |
| 270 | IPCChannelPosixTestListener in_listener(true); |
| 271 | IPC::ChannelHandle in_handle("IN"); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 272 | scoped_ptr<IPC::ChannelPosix> in_chan(new IPC::ChannelPosix( |
| 273 | in_handle, IPC::Channel::MODE_SERVER, &in_listener)); |
morrita | 13e514d | 2014-10-21 08:53:25 +0900 | [diff] [blame] | 274 | IPC::ChannelHandle out_handle( |
| 275 | "OUT", base::FileDescriptor(in_chan->TakeClientFileDescriptor())); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 276 | scoped_ptr<IPC::ChannelPosix> out_chan(new IPC::ChannelPosix( |
| 277 | out_handle, IPC::Channel::MODE_CLIENT, &out_listener)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 278 | ASSERT_TRUE(in_chan->Connect()); |
| 279 | in_chan->Close(); // simulate remote process dying at an unfortunate time. |
| 280 | ASSERT_FALSE(out_chan->Connect()); |
hubbe@chromium.org | a53c97c | 2014-02-21 09:18:29 +0900 | [diff] [blame] | 281 | SpinRunLoop(TestTimeouts::action_max_timeout()); |
| 282 | ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status()); |
| 283 | } |
| 284 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 285 | TEST_F(IPCChannelPosixTest, AdvancedConnected) { |
| 286 | // Test creating a connection to an external process. |
| 287 | IPCChannelPosixTestListener listener(false); |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 288 | IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 289 | SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 290 | scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 291 | chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 292 | ASSERT_TRUE(channel->Connect()); |
| 293 | ASSERT_TRUE(channel->AcceptsConnections()); |
| 294 | ASSERT_FALSE(channel->HasAcceptedConnection()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 295 | |
viettrungluu@chromium.org | ceed605 | 2014-03-09 09:59:31 +0900 | [diff] [blame] | 296 | base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 297 | ASSERT_TRUE(handle); |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 298 | SpinRunLoop(TestTimeouts::action_max_timeout()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 299 | ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 300 | ASSERT_TRUE(channel->HasAcceptedConnection()); |
bbudge@chromium.org | ab4c6bc | 2013-11-05 07:28:12 +0900 | [diff] [blame] | 301 | IPC::Message* message = new IPC::Message(0, // routing_id |
| 302 | kQuitMessage, // message type |
| 303 | IPC::Message::PRIORITY_NORMAL); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 304 | channel->Send(message); |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 305 | SpinRunLoop(TestTimeouts::action_timeout()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 306 | int exit_code = 0; |
| 307 | EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
| 308 | EXPECT_EQ(0, exit_code); |
| 309 | ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 310 | ASSERT_FALSE(channel->HasAcceptedConnection()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | TEST_F(IPCChannelPosixTest, ResetState) { |
| 314 | // Test creating a connection to an external process. Close the connection, |
| 315 | // but continue to listen and make sure another external process can connect |
| 316 | // to us. |
| 317 | IPCChannelPosixTestListener listener(false); |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 318 | IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 319 | SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 320 | scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 321 | chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 322 | ASSERT_TRUE(channel->Connect()); |
| 323 | ASSERT_TRUE(channel->AcceptsConnections()); |
| 324 | ASSERT_FALSE(channel->HasAcceptedConnection()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 325 | |
viettrungluu@chromium.org | ceed605 | 2014-03-09 09:59:31 +0900 | [diff] [blame] | 326 | base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 327 | ASSERT_TRUE(handle); |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 328 | SpinRunLoop(TestTimeouts::action_max_timeout()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 329 | ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 330 | ASSERT_TRUE(channel->HasAcceptedConnection()); |
| 331 | channel->ResetToAcceptingConnectionState(); |
| 332 | ASSERT_FALSE(channel->HasAcceptedConnection()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 333 | |
viettrungluu@chromium.org | ceed605 | 2014-03-09 09:59:31 +0900 | [diff] [blame] | 334 | base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc"); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 335 | ASSERT_TRUE(handle2); |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 336 | SpinRunLoop(TestTimeouts::action_max_timeout()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 337 | ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 338 | ASSERT_TRUE(channel->HasAcceptedConnection()); |
bbudge@chromium.org | ab4c6bc | 2013-11-05 07:28:12 +0900 | [diff] [blame] | 339 | IPC::Message* message = new IPC::Message(0, // routing_id |
| 340 | kQuitMessage, // message type |
| 341 | IPC::Message::PRIORITY_NORMAL); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 342 | channel->Send(message); |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 343 | SpinRunLoop(TestTimeouts::action_timeout()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 344 | EXPECT_TRUE(base::KillProcess(handle, 0, false)); |
| 345 | int exit_code = 0; |
| 346 | EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); |
| 347 | EXPECT_EQ(0, exit_code); |
| 348 | ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 349 | ASSERT_FALSE(channel->HasAcceptedConnection()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 350 | } |
| 351 | |
dmaclach@chromium.org | 344a3d5 | 2011-08-25 23:14:05 +0900 | [diff] [blame] | 352 | TEST_F(IPCChannelPosixTest, BadChannelName) { |
| 353 | // Test empty name |
| 354 | IPC::ChannelHandle handle(""); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 355 | scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 356 | handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 357 | ASSERT_FALSE(channel->Connect()); |
dmaclach@chromium.org | 344a3d5 | 2011-08-25 23:14:05 +0900 | [diff] [blame] | 358 | |
| 359 | // Test name that is too long. |
| 360 | const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement" |
| 361 | "client-centered_synergy_through_top-line" |
| 362 | "platforms_Phosfluorescently_disintermediate_" |
| 363 | "clicks-and-mortar_best_practices_without_" |
| 364 | "future-proof_growth_strategies_Continually" |
| 365 | "pontificate_proactive_potentialities_before" |
| 366 | "leading-edge_processes"; |
jeremya@chromium.org | f14bfab | 2013-03-13 13:23:10 +0900 | [diff] [blame] | 367 | EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength); |
dmaclach@chromium.org | 344a3d5 | 2011-08-25 23:14:05 +0900 | [diff] [blame] | 368 | IPC::ChannelHandle handle2(kTooLongName); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 369 | scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix( |
| 370 | handle2, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 371 | EXPECT_FALSE(channel2->Connect()); |
dmaclach@chromium.org | 344a3d5 | 2011-08-25 23:14:05 +0900 | [diff] [blame] | 372 | } |
| 373 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 374 | TEST_F(IPCChannelPosixTest, MultiConnection) { |
| 375 | // Test setting up a connection to an external process, and then have |
| 376 | // another external process attempt to connect to us. |
| 377 | IPCChannelPosixTestListener listener(false); |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 378 | IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 379 | SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 380 | scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 381 | chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 382 | ASSERT_TRUE(channel->Connect()); |
| 383 | ASSERT_TRUE(channel->AcceptsConnections()); |
| 384 | ASSERT_FALSE(channel->HasAcceptedConnection()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 385 | |
viettrungluu@chromium.org | ceed605 | 2014-03-09 09:59:31 +0900 | [diff] [blame] | 386 | base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 387 | ASSERT_TRUE(handle); |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 388 | SpinRunLoop(TestTimeouts::action_max_timeout()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 389 | ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 390 | ASSERT_TRUE(channel->HasAcceptedConnection()); |
viettrungluu@chromium.org | ceed605 | 2014-03-09 09:59:31 +0900 | [diff] [blame] | 391 | base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc"); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 392 | ASSERT_TRUE(handle2); |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 393 | SpinRunLoop(TestTimeouts::action_max_timeout()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 394 | int exit_code = 0; |
| 395 | EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); |
| 396 | EXPECT_EQ(exit_code, 0); |
| 397 | ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 398 | ASSERT_TRUE(channel->HasAcceptedConnection()); |
bbudge@chromium.org | ab4c6bc | 2013-11-05 07:28:12 +0900 | [diff] [blame] | 399 | IPC::Message* message = new IPC::Message(0, // routing_id |
| 400 | kQuitMessage, // message type |
| 401 | IPC::Message::PRIORITY_NORMAL); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 402 | channel->Send(message); |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 403 | SpinRunLoop(TestTimeouts::action_timeout()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 404 | EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
| 405 | EXPECT_EQ(exit_code, 0); |
| 406 | ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 407 | ASSERT_FALSE(channel->HasAcceptedConnection()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 408 | } |
| 409 | |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 410 | TEST_F(IPCChannelPosixTest, DoubleServer) { |
| 411 | // Test setting up two servers with the same name. |
| 412 | IPCChannelPosixTestListener listener(false); |
| 413 | IPCChannelPosixTestListener listener2(false); |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 414 | IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 415 | scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 416 | chan_handle, IPC::Channel::MODE_SERVER, &listener)); |
| 417 | scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix( |
| 418 | chan_handle, IPC::Channel::MODE_SERVER, &listener2)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 419 | ASSERT_TRUE(channel->Connect()); |
| 420 | ASSERT_FALSE(channel2->Connect()); |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | TEST_F(IPCChannelPosixTest, BadMode) { |
| 424 | // Test setting up two servers with a bad mode. |
| 425 | IPCChannelPosixTestListener listener(false); |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 426 | IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 427 | scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 428 | chan_handle, IPC::Channel::MODE_NONE, &listener)); |
| 429 | ASSERT_FALSE(channel->Connect()); |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 430 | } |
| 431 | |
kkania@chromium.org | f37b4e5 | 2011-08-09 15:46:06 +0900 | [diff] [blame] | 432 | TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) { |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 433 | const std::string& connection_socket_name = GetConnectionSocketName(); |
kkania@chromium.org | f37b4e5 | 2011-08-09 15:46:06 +0900 | [diff] [blame] | 434 | IPCChannelPosixTestListener listener(false); |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 435 | IPC::ChannelHandle chan_handle(connection_socket_name); |
brettw@chromium.org | 220b8de | 2013-07-17 04:10:23 +0900 | [diff] [blame] | 436 | ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name), false)); |
kkania@chromium.org | f37b4e5 | 2011-08-09 15:46:06 +0900 | [diff] [blame] | 437 | ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 438 | connection_socket_name)); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 439 | scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 440 | chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
kkania@chromium.org | f37b4e5 | 2011-08-09 15:46:06 +0900 | [diff] [blame] | 441 | ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized( |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 442 | connection_socket_name)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 443 | channel->Close(); |
kkania@chromium.org | f37b4e5 | 2011-08-09 15:46:06 +0900 | [diff] [blame] | 444 | ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 445 | connection_socket_name)); |
kkania@chromium.org | f37b4e5 | 2011-08-09 15:46:06 +0900 | [diff] [blame] | 446 | } |
| 447 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 448 | // A long running process that connects to us |
| 449 | MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 450 | base::MessageLoopForIO message_loop; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 451 | IPCChannelPosixTestListener listener(true); |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 452 | IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 453 | IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 454 | scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 455 | handle, IPC::Channel::MODE_NAMED_CLIENT, &listener)); |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 456 | EXPECT_TRUE(channel->Connect()); |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 457 | IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 458 | EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status()); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | // Simple external process that shouldn't be able to connect to us. |
| 463 | MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) { |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 464 | base::MessageLoopForIO message_loop; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 465 | IPCChannelPosixTestListener listener(false); |
nileshagrawal@chromium.org | 42a2e0d | 2012-05-25 11:56:25 +0900 | [diff] [blame] | 466 | IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 467 | IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); |
morrita@chromium.org | 844f1c3 | 2014-06-07 15:15:53 +0900 | [diff] [blame] | 468 | scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 469 | handle, IPC::Channel::MODE_NAMED_CLIENT, &listener)); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 470 | |
| 471 | // In this case connect may succeed or fail depending on if the packet |
| 472 | // actually gets sent at sendmsg. Since we never delay on send, we may not |
| 473 | // see the error. However even if connect succeeds, eventually we will get an |
| 474 | // error back since the channel will be closed when we attempt to read from |
| 475 | // it. |
morrita@chromium.org | 2ced004 | 2014-05-30 12:58:59 +0900 | [diff] [blame] | 476 | bool connected = channel->Connect(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 477 | if (connected) { |
tedvessenes@gmail.com | 5966b64 | 2012-07-12 00:41:43 +0900 | [diff] [blame] | 478 | IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 479 | EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 480 | } else { |
| 481 | EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); |
| 482 | } |
| 483 | return 0; |
| 484 | } |
viettrungluu@chromium.org | 7ca1913 | 2013-01-12 05:56:22 +0900 | [diff] [blame] | 485 | |
| 486 | } // namespace |