OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 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" |
| 18 #include "base/message_loop.h" |
| 19 #include "base/scoped_ptr.h" |
| 20 #include "base/test/multiprocess_test.h" |
| 21 #include "base/test/test_timeouts.h" |
| 22 #include "testing/multiprocess_func_list.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 enum { |
| 27 QUIT_MESSAGE = 47 |
| 28 }; |
| 29 |
| 30 class IPCChannelPosixTestListener : public IPC::Channel::Listener { |
| 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 |
| 46 virtual void OnMessageReceived(const IPC::Message& message) { |
| 47 EXPECT_EQ(message.type(), QUIT_MESSAGE); |
| 48 status_ = MESSAGE_RECEIVED; |
| 49 QuitRunLoop(); |
| 50 } |
| 51 |
| 52 virtual void OnChannelConnected(int32 peer_pid) { |
| 53 status_ = CONNECTED; |
| 54 if (!quit_only_on_message_) { |
| 55 QuitRunLoop(); |
| 56 } |
| 57 } |
| 58 |
| 59 virtual void OnChannelError() { |
| 60 status_ = CHANNEL_ERROR; |
| 61 if (!quit_only_on_message_) { |
| 62 QuitRunLoop(); |
| 63 } |
| 64 } |
| 65 |
| 66 virtual void OnChannelDenied() { |
| 67 status_ = DENIED; |
| 68 if (!quit_only_on_message_) { |
| 69 QuitRunLoop(); |
| 70 } |
| 71 } |
| 72 |
| 73 virtual void OnChannelListenError() { |
| 74 status_ = LISTEN_ERROR; |
| 75 if (!quit_only_on_message_) { |
| 76 QuitRunLoop(); |
| 77 } |
| 78 } |
| 79 |
| 80 STATUS status() { return status_; } |
| 81 |
| 82 void QuitRunLoop() { |
| 83 MessageLoopForIO::current()->QuitNow(); |
| 84 } |
| 85 |
| 86 private: |
| 87 // The current status of the listener. |
| 88 STATUS status_; |
| 89 // If |quit_only_on_message_| then the listener will only break out of |
| 90 // the run loop when the QUIT_MESSAGE is received. |
| 91 bool quit_only_on_message_; |
| 92 }; |
| 93 |
| 94 } // namespace |
| 95 |
| 96 class IPCChannelPosixTest : public base::MultiProcessTest { |
| 97 public: |
| 98 static const char kConnectionSocketTestName[]; |
| 99 static void SetUpSocket(IPC::ChannelHandle *handle, |
| 100 IPC::Channel::Mode mode); |
| 101 static void SpinRunLoop(int milliseconds); |
| 102 |
| 103 protected: |
| 104 virtual void SetUp(); |
| 105 virtual void TearDown(); |
| 106 |
| 107 private: |
| 108 scoped_ptr<MessageLoopForIO> message_loop_; |
| 109 }; |
| 110 |
| 111 const char IPCChannelPosixTest::kConnectionSocketTestName[] = |
| 112 "/var/tmp/chrome_IPCChannelPosixTest__ConnectionSocket"; |
| 113 |
| 114 void IPCChannelPosixTest::SetUp() { |
| 115 MultiProcessTest::SetUp(); |
| 116 // Construct a fresh IO Message loop for the duration of each test. |
| 117 message_loop_.reset(new MessageLoopForIO()); |
| 118 } |
| 119 |
| 120 void IPCChannelPosixTest::TearDown() { |
| 121 message_loop_.reset(NULL); |
| 122 MultiProcessTest::TearDown(); |
| 123 } |
| 124 |
| 125 // Create up a socket and bind and listen to it, or connect it |
| 126 // depending on the |mode|. |
| 127 void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle, |
| 128 IPC::Channel::Mode mode) { |
| 129 const std::string& name = handle->name; |
| 130 |
| 131 int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 132 ASSERT_GE(socket_fd, 0) << name; |
| 133 ASSERT_GE(fcntl(socket_fd, F_SETFL, O_NONBLOCK), 0); |
| 134 struct sockaddr_un server_address = { 0 }; |
| 135 memset(&server_address, 0, sizeof(server_address)); |
| 136 server_address.sun_family = AF_UNIX; |
| 137 int path_len = snprintf(server_address.sun_path, IPC::kMaxPipeNameLength, |
| 138 "%s", name.c_str()); |
| 139 DCHECK_EQ(static_cast<int>(name.length()), path_len); |
| 140 size_t server_address_len = offsetof(struct sockaddr_un, |
| 141 sun_path) + path_len + 1; |
| 142 |
| 143 if (mode == IPC::Channel::MODE_NAMED_SERVER) { |
| 144 // Only one server at a time. Cleanup garbage if it exists. |
| 145 unlink(name.c_str()); |
| 146 // Make sure the path we need exists. |
| 147 FilePath path(name); |
| 148 FilePath dir_path = path.DirName(); |
| 149 ASSERT_TRUE(file_util::CreateDirectory(dir_path)); |
| 150 ASSERT_GE(bind(socket_fd, |
| 151 reinterpret_cast<struct sockaddr *>(&server_address), |
| 152 server_address_len), 0) << server_address.sun_path |
| 153 << ": " << strerror(errno) |
| 154 << "(" << errno << ")"; |
| 155 ASSERT_GE(listen(socket_fd, SOMAXCONN), 0) << server_address.sun_path |
| 156 << ": " << strerror(errno) |
| 157 << "(" << errno << ")"; |
| 158 } else if (mode == IPC::Channel::MODE_NAMED_CLIENT) { |
| 159 ASSERT_GE(connect(socket_fd, |
| 160 reinterpret_cast<struct sockaddr *>(&server_address), |
| 161 server_address_len), 0) << server_address.sun_path |
| 162 << ": " << strerror(errno) |
| 163 << "(" << errno << ")"; |
| 164 } else { |
| 165 FAIL() << "Unknown mode " << mode; |
| 166 } |
| 167 handle->socket.fd = socket_fd; |
| 168 } |
| 169 |
| 170 void IPCChannelPosixTest::SpinRunLoop(int milliseconds) { |
| 171 MessageLoopForIO *loop = MessageLoopForIO::current(); |
| 172 // Post a quit task so that this loop eventually ends and we don't hang |
| 173 // in the case of a bad test. Usually, the run loop will quit sooner than |
| 174 // that because all tests use a IPCChannelPosixTestListener which quits the |
| 175 // current run loop on any channel activity. |
| 176 loop->PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), milliseconds); |
| 177 loop->Run(); |
| 178 } |
| 179 |
| 180 TEST_F(IPCChannelPosixTest, BasicListen) { |
| 181 // Test creating a socket that is listening. |
| 182 IPC::ChannelHandle handle("/var/tmp/IPCChannelPosixTest_BasicListen"); |
| 183 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); |
| 184 unlink(handle.name.c_str()); |
| 185 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL); |
| 186 ASSERT_TRUE(channel.Connect()); |
| 187 ASSERT_TRUE(channel.AcceptsConnections()); |
| 188 ASSERT_FALSE(channel.HasAcceptedConnection()); |
| 189 channel.ResetToAcceptingConnectionState(); |
| 190 ASSERT_FALSE(channel.HasAcceptedConnection()); |
| 191 } |
| 192 |
| 193 TEST_F(IPCChannelPosixTest, BasicConnected) { |
| 194 // Test creating a socket that is connected. |
| 195 int pipe_fds[2]; |
| 196 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds)); |
| 197 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected"); |
| 198 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0); |
| 199 |
| 200 base::FileDescriptor fd(pipe_fds[0], false); |
| 201 IPC::ChannelHandle handle(socket_name, fd); |
| 202 IPC::Channel channel(handle, IPC::Channel::MODE_SERVER, NULL); |
| 203 ASSERT_TRUE(channel.Connect()); |
| 204 ASSERT_FALSE(channel.AcceptsConnections()); |
| 205 channel.Close(); |
| 206 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds[1])) == 0); |
| 207 |
| 208 // Make sure that we can use the socket that is created for us by |
| 209 // a standard channel. |
| 210 IPC::Channel channel2(socket_name, IPC::Channel::MODE_SERVER, NULL); |
| 211 ASSERT_TRUE(channel2.Connect()); |
| 212 ASSERT_FALSE(channel2.AcceptsConnections()); |
| 213 } |
| 214 |
| 215 TEST_F(IPCChannelPosixTest, AdvancedConnected) { |
| 216 // Test creating a connection to an external process. |
| 217 IPCChannelPosixTestListener listener(false); |
| 218 IPC::ChannelHandle chan_handle(kConnectionSocketTestName); |
| 219 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
| 220 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); |
| 221 ASSERT_TRUE(channel.Connect()); |
| 222 ASSERT_TRUE(channel.AcceptsConnections()); |
| 223 ASSERT_FALSE(channel.HasAcceptedConnection()); |
| 224 |
| 225 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", |
| 226 false); |
| 227 ASSERT_TRUE(handle); |
| 228 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); |
| 229 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
| 230 ASSERT_TRUE(channel.HasAcceptedConnection()); |
| 231 IPC::Message* message = new IPC::Message(0, // routing_id |
| 232 QUIT_MESSAGE, // message type |
| 233 IPC::Message::PRIORITY_NORMAL); |
| 234 channel.Send(message); |
| 235 SpinRunLoop(TestTimeouts::action_timeout_ms()); |
| 236 int exit_code = 0; |
| 237 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
| 238 EXPECT_EQ(0, exit_code); |
| 239 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 240 ASSERT_FALSE(channel.HasAcceptedConnection()); |
| 241 } |
| 242 |
| 243 TEST_F(IPCChannelPosixTest, ResetState) { |
| 244 // Test creating a connection to an external process. Close the connection, |
| 245 // but continue to listen and make sure another external process can connect |
| 246 // to us. |
| 247 IPCChannelPosixTestListener listener(false); |
| 248 IPC::ChannelHandle chan_handle(kConnectionSocketTestName); |
| 249 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
| 250 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); |
| 251 ASSERT_TRUE(channel.Connect()); |
| 252 ASSERT_TRUE(channel.AcceptsConnections()); |
| 253 ASSERT_FALSE(channel.HasAcceptedConnection()); |
| 254 |
| 255 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", |
| 256 false); |
| 257 ASSERT_TRUE(handle); |
| 258 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); |
| 259 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
| 260 ASSERT_TRUE(channel.HasAcceptedConnection()); |
| 261 channel.ResetToAcceptingConnectionState(); |
| 262 ASSERT_FALSE(channel.HasAcceptedConnection()); |
| 263 |
| 264 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc", |
| 265 false); |
| 266 ASSERT_TRUE(handle2); |
| 267 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); |
| 268 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
| 269 ASSERT_TRUE(channel.HasAcceptedConnection()); |
| 270 IPC::Message* message = new IPC::Message(0, // routing_id |
| 271 QUIT_MESSAGE, // message type |
| 272 IPC::Message::PRIORITY_NORMAL); |
| 273 channel.Send(message); |
| 274 SpinRunLoop(TestTimeouts::action_timeout_ms()); |
| 275 EXPECT_TRUE(base::KillProcess(handle, 0, false)); |
| 276 int exit_code = 0; |
| 277 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); |
| 278 EXPECT_EQ(0, exit_code); |
| 279 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 280 ASSERT_FALSE(channel.HasAcceptedConnection()); |
| 281 } |
| 282 |
| 283 TEST_F(IPCChannelPosixTest, MultiConnection) { |
| 284 // Test setting up a connection to an external process, and then have |
| 285 // another external process attempt to connect to us. |
| 286 IPCChannelPosixTestListener listener(false); |
| 287 IPC::ChannelHandle chan_handle(kConnectionSocketTestName); |
| 288 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
| 289 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); |
| 290 ASSERT_TRUE(channel.Connect()); |
| 291 ASSERT_TRUE(channel.AcceptsConnections()); |
| 292 ASSERT_FALSE(channel.HasAcceptedConnection()); |
| 293 |
| 294 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", |
| 295 false); |
| 296 ASSERT_TRUE(handle); |
| 297 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); |
| 298 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
| 299 ASSERT_TRUE(channel.HasAcceptedConnection()); |
| 300 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc", |
| 301 false); |
| 302 ASSERT_TRUE(handle2); |
| 303 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); |
| 304 int exit_code = 0; |
| 305 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); |
| 306 EXPECT_EQ(exit_code, 0); |
| 307 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); |
| 308 ASSERT_TRUE(channel.HasAcceptedConnection()); |
| 309 IPC::Message* message = new IPC::Message(0, // routing_id |
| 310 QUIT_MESSAGE, // message type |
| 311 IPC::Message::PRIORITY_NORMAL); |
| 312 channel.Send(message); |
| 313 SpinRunLoop(TestTimeouts::action_timeout_ms()); |
| 314 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
| 315 EXPECT_EQ(exit_code, 0); |
| 316 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 317 ASSERT_FALSE(channel.HasAcceptedConnection()); |
| 318 } |
| 319 |
| 320 // A long running process that connects to us |
| 321 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { |
| 322 MessageLoopForIO message_loop; |
| 323 IPCChannelPosixTestListener listener(true); |
| 324 IPC::ChannelHandle handle(IPCChannelPosixTest::kConnectionSocketTestName); |
| 325 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); |
| 326 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); |
| 327 EXPECT_TRUE(channel.Connect()); |
| 328 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms()); |
| 329 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status()); |
| 330 return 0; |
| 331 } |
| 332 |
| 333 // Simple external process that shouldn't be able to connect to us. |
| 334 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) { |
| 335 MessageLoopForIO message_loop; |
| 336 IPCChannelPosixTestListener listener(false); |
| 337 IPC::ChannelHandle handle(IPCChannelPosixTest::kConnectionSocketTestName); |
| 338 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); |
| 339 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); |
| 340 |
| 341 // In this case connect may succeed or fail depending on if the packet |
| 342 // actually gets sent at sendmsg. Since we never delay on send, we may not |
| 343 // see the error. However even if connect succeeds, eventually we will get an |
| 344 // error back since the channel will be closed when we attempt to read from |
| 345 // it. |
| 346 bool connected = channel.Connect(); |
| 347 if (connected) { |
| 348 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms()); |
| 349 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 350 } else { |
| 351 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); |
| 352 } |
| 353 return 0; |
| 354 } |
| 355 |
OLD | NEW |