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