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