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