| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/process_util.h" | |
| 11 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| 12 #include "ipc/ipc_channel.h" | |
| 13 #include "ipc/ipc_channel_proxy.h" | |
| 14 #include "ipc/ipc_multiprocess_test.h" | |
| 15 #include "ipc/ipc_test_base.h" | 11 #include "ipc/ipc_test_base.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 13 |
| 18 // IPC messages for testing ---------------------------------------------------- | 14 // IPC messages for testing ---------------------------------------------------- |
| 19 | 15 |
| 20 #define IPC_MESSAGE_IMPL | 16 #define IPC_MESSAGE_IMPL |
| 21 #include "ipc/ipc_message_macros.h" | 17 #include "ipc/ipc_message_macros.h" |
| 22 | 18 |
| 23 #define IPC_MESSAGE_START TestMsgStart | 19 #define IPC_MESSAGE_START TestMsgStart |
| 24 | 20 |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 if (NULL == last_msg_) | 235 if (NULL == last_msg_) |
| 240 return false; | 236 return false; |
| 241 if (FUZZER_ROUTING_ID != last_msg_->routing_id()) | 237 if (FUZZER_ROUTING_ID != last_msg_->routing_id()) |
| 242 return false; | 238 return false; |
| 243 return (type_id == last_msg_->type()); | 239 return (type_id == last_msg_->type()); |
| 244 }; | 240 }; |
| 245 | 241 |
| 246 IPC::Message* last_msg_; | 242 IPC::Message* last_msg_; |
| 247 }; | 243 }; |
| 248 | 244 |
| 249 // Runs the fuzzing server child mode. Returns when the preset number | 245 // Runs the fuzzing server child mode. Returns when the preset number of |
| 250 // of messages have been received. | 246 // messages have been received. |
| 251 MULTIPROCESS_IPC_TEST_MAIN(RunFuzzServer) { | 247 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) { |
| 252 MessageLoopForIO main_message_loop; | 248 MessageLoopForIO main_message_loop; |
| 253 FuzzerServerListener listener; | 249 FuzzerServerListener listener; |
| 254 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_CLIENT, &listener); | 250 IPC::Channel channel(IPCTestBase::GetChannelName("FuzzServerClient"), |
| 255 CHECK(chan.Connect()); | 251 IPC::Channel::MODE_CLIENT, |
| 256 listener.Init(&chan); | 252 &listener); |
| 253 CHECK(channel.Connect()); |
| 254 listener.Init(&channel); |
| 257 MessageLoop::current()->Run(); | 255 MessageLoop::current()->Run(); |
| 258 return 0; | 256 return 0; |
| 259 } | 257 } |
| 260 | 258 |
| 261 class IPCFuzzingTest : public IPCTestBase { | 259 class IPCFuzzingTest : public IPCTestBase { |
| 262 }; | 260 }; |
| 263 | 261 |
| 264 // This test makes sure that the FuzzerClientListener and FuzzerServerListener | 262 // This test makes sure that the FuzzerClientListener and FuzzerServerListener |
| 265 // are working properly by generating two well formed IPC calls. | 263 // are working properly by generating two well formed IPC calls. |
| 266 TEST_F(IPCFuzzingTest, SanityTest) { | 264 TEST_F(IPCFuzzingTest, SanityTest) { |
| 265 Init("FuzzServerClient"); |
| 266 |
| 267 FuzzerClientListener listener; | 267 FuzzerClientListener listener; |
| 268 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER, | 268 CreateChannel(&listener); |
| 269 &listener); | 269 listener.Init(channel()); |
| 270 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan); | 270 ASSERT_TRUE(ConnectChannel()); |
| 271 ASSERT_TRUE(server_process); | 271 ASSERT_TRUE(StartClient()); |
| 272 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | |
| 273 ASSERT_TRUE(chan.Connect()); | |
| 274 listener.Init(&chan); | |
| 275 | 272 |
| 276 IPC::Message* msg = NULL; | 273 IPC::Message* msg = NULL; |
| 277 int value = 43; | 274 int value = 43; |
| 278 msg = new MsgClassIS(value, L"expect 43"); | 275 msg = new MsgClassIS(value, L"expect 43"); |
| 279 chan.Send(msg); | 276 sender()->Send(msg); |
| 280 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID)); | 277 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID)); |
| 281 | 278 |
| 282 msg = new MsgClassSI(L"expect 44", ++value); | 279 msg = new MsgClassSI(L"expect 44", ++value); |
| 283 chan.Send(msg); | 280 sender()->Send(msg); |
| 284 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID)); | 281 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID)); |
| 285 | 282 |
| 286 EXPECT_TRUE(base::WaitForSingleProcess( | 283 EXPECT_TRUE(WaitForClientShutdown()); |
| 287 server_process, base::TimeDelta::FromSeconds(5))); | |
| 288 base::CloseProcessHandle(server_process); | |
| 289 } | 284 } |
| 290 | 285 |
| 291 // This test uses a payload that is smaller than expected. | 286 // This test uses a payload that is smaller than expected. This generates an |
| 292 // This generates an error while unpacking the IPC buffer which in | 287 // error while unpacking the IPC buffer which in debug trigger an assertion and |
| 293 // In debug this triggers an assertion and in release it is ignored(!!). Right | 288 // in release is ignored (!). Right after we generate another valid IPC to make |
| 294 // after we generate another valid IPC to make sure framing is working | 289 // sure framing is working properly. |
| 295 // properly. | |
| 296 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 290 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 297 TEST_F(IPCFuzzingTest, MsgBadPayloadShort) { | 291 TEST_F(IPCFuzzingTest, MsgBadPayloadShort) { |
| 292 Init("FuzzServerClient"); |
| 293 |
| 298 FuzzerClientListener listener; | 294 FuzzerClientListener listener; |
| 299 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER, | 295 CreateChannel(&listener); |
| 300 &listener); | 296 listener.Init(channel()); |
| 301 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan); | 297 ASSERT_TRUE(ConnectChannel()); |
| 302 ASSERT_TRUE(server_process); | 298 ASSERT_TRUE(StartClient()); |
| 303 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | |
| 304 ASSERT_TRUE(chan.Connect()); | |
| 305 listener.Init(&chan); | |
| 306 | 299 |
| 307 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID, | 300 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID, |
| 308 IPC::Message::PRIORITY_NORMAL); | 301 IPC::Message::PRIORITY_NORMAL); |
| 309 msg->WriteInt(666); | 302 msg->WriteInt(666); |
| 310 chan.Send(msg); | 303 sender()->Send(msg); |
| 311 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID)); | 304 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID)); |
| 312 | 305 |
| 313 msg = new MsgClassSI(L"expect one", 1); | 306 msg = new MsgClassSI(L"expect one", 1); |
| 314 chan.Send(msg); | 307 sender()->Send(msg); |
| 315 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID)); | 308 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID)); |
| 316 | 309 |
| 317 EXPECT_TRUE(base::WaitForSingleProcess( | 310 EXPECT_TRUE(WaitForClientShutdown()); |
| 318 server_process, base::TimeDelta::FromSeconds(5))); | |
| 319 base::CloseProcessHandle(server_process); | |
| 320 } | 311 } |
| 321 #endif | 312 #endif |
| 322 | 313 |
| 323 // This test uses a payload that has too many arguments, but so the payload | 314 // This test uses a payload that has too many arguments, but so the payload size |
| 324 // size is big enough so the unpacking routine does not generate an error as | 315 // is big enough so the unpacking routine does not generate an error as in the |
| 325 // in the case of MsgBadPayloadShort test. | 316 // case of MsgBadPayloadShort test. This test does not pinpoint a flaw (per se) |
| 326 // This test does not pinpoint a flaw (per se) as by design we don't carry | 317 // as by design we don't carry type information on the IPC message. |
| 327 // type information on the IPC message. | |
| 328 TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) { | 318 TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) { |
| 319 Init("FuzzServerClient"); |
| 320 |
| 329 FuzzerClientListener listener; | 321 FuzzerClientListener listener; |
| 330 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER, | 322 CreateChannel(&listener); |
| 331 &listener); | 323 listener.Init(channel()); |
| 332 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan); | 324 ASSERT_TRUE(ConnectChannel()); |
| 333 ASSERT_TRUE(server_process); | 325 ASSERT_TRUE(StartClient()); |
| 334 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | |
| 335 ASSERT_TRUE(chan.Connect()); | |
| 336 listener.Init(&chan); | |
| 337 | 326 |
| 338 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID, | 327 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID, |
| 339 IPC::Message::PRIORITY_NORMAL); | 328 IPC::Message::PRIORITY_NORMAL); |
| 340 msg->WriteWString(L"d"); | 329 msg->WriteWString(L"d"); |
| 341 msg->WriteInt(0); | 330 msg->WriteInt(0); |
| 342 msg->WriteInt(0x65); // Extra argument. | 331 msg->WriteInt(0x65); // Extra argument. |
| 343 | 332 |
| 344 chan.Send(msg); | 333 sender()->Send(msg); |
| 345 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID)); | 334 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID)); |
| 346 | 335 |
| 347 // Now send a well formed message to make sure the receiver wasn't | 336 // Now send a well formed message to make sure the receiver wasn't |
| 348 // thrown out of sync by the extra argument. | 337 // thrown out of sync by the extra argument. |
| 349 msg = new MsgClassIS(3, L"expect three"); | 338 msg = new MsgClassIS(3, L"expect three"); |
| 350 chan.Send(msg); | 339 sender()->Send(msg); |
| 351 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID)); | 340 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID)); |
| 352 | 341 |
| 353 EXPECT_TRUE(base::WaitForSingleProcess( | 342 EXPECT_TRUE(WaitForClientShutdown()); |
| 354 server_process, base::TimeDelta::FromSeconds(5))); | |
| 355 base::CloseProcessHandle(server_process); | |
| 356 } | 343 } |
| 357 | 344 |
| 358 // This class is for testing the IPC_BEGIN_MESSAGE_MAP_EX macros. | 345 // This class is for testing the IPC_BEGIN_MESSAGE_MAP_EX macros. |
| 359 class ServerMacroExTest { | 346 class ServerMacroExTest { |
| 360 public: | 347 public: |
| 361 ServerMacroExTest() : unhandled_msgs_(0) { | 348 ServerMacroExTest() : unhandled_msgs_(0) { |
| 362 } | 349 } |
| 363 | 350 |
| 364 virtual ~ServerMacroExTest() { | 351 virtual ~ServerMacroExTest() { |
| 365 } | 352 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 msg->WriteInt(0x64); | 401 msg->WriteInt(0x64); |
| 415 msg->WriteInt(0x32); | 402 msg->WriteInt(0x32); |
| 416 EXPECT_FALSE(server.OnMessageReceived(*msg)); | 403 EXPECT_FALSE(server.OnMessageReceived(*msg)); |
| 417 delete msg; | 404 delete msg; |
| 418 | 405 |
| 419 EXPECT_EQ(0, server.unhandled_msgs()); | 406 EXPECT_EQ(0, server.unhandled_msgs()); |
| 420 #endif | 407 #endif |
| 421 } | 408 } |
| 422 | 409 |
| 423 } // namespace | 410 } // namespace |
| OLD | NEW |