| 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))); | 284 DestroyChannel(); |
| 288 base::CloseProcessHandle(server_process); | |
| 289 } | 285 } |
| 290 | 286 |
| 291 // This test uses a payload that is smaller than expected. | 287 // 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 | 288 // 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 | 289 // 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 | 290 // sure framing is working properly. |
| 295 // properly. | |
| 296 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 291 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 297 TEST_F(IPCFuzzingTest, MsgBadPayloadShort) { | 292 TEST_F(IPCFuzzingTest, MsgBadPayloadShort) { |
| 293 Init("FuzzServerClient"); |
| 294 |
| 298 FuzzerClientListener listener; | 295 FuzzerClientListener listener; |
| 299 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER, | 296 CreateChannel(&listener); |
| 300 &listener); | 297 listener.Init(channel()); |
| 301 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan); | 298 ASSERT_TRUE(ConnectChannel()); |
| 302 ASSERT_TRUE(server_process); | 299 ASSERT_TRUE(StartClient()); |
| 303 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | |
| 304 ASSERT_TRUE(chan.Connect()); | |
| 305 listener.Init(&chan); | |
| 306 | 300 |
| 307 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID, | 301 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID, |
| 308 IPC::Message::PRIORITY_NORMAL); | 302 IPC::Message::PRIORITY_NORMAL); |
| 309 msg->WriteInt(666); | 303 msg->WriteInt(666); |
| 310 chan.Send(msg); | 304 sender()->Send(msg); |
| 311 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID)); | 305 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID)); |
| 312 | 306 |
| 313 msg = new MsgClassSI(L"expect one", 1); | 307 msg = new MsgClassSI(L"expect one", 1); |
| 314 chan.Send(msg); | 308 sender()->Send(msg); |
| 315 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID)); | 309 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID)); |
| 316 | 310 |
| 317 EXPECT_TRUE(base::WaitForSingleProcess( | 311 EXPECT_TRUE(WaitForClientShutdown()); |
| 318 server_process, base::TimeDelta::FromSeconds(5))); | 312 DestroyChannel(); |
| 319 base::CloseProcessHandle(server_process); | |
| 320 } | 313 } |
| 321 #endif | 314 #endif |
| 322 | 315 |
| 323 // This test uses a payload that has too many arguments, but so the payload | 316 // 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 | 317 // is big enough so the unpacking routine does not generate an error as in the |
| 325 // in the case of MsgBadPayloadShort test. | 318 // 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 | 319 // 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) { | 320 TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) { |
| 321 Init("FuzzServerClient"); |
| 322 |
| 329 FuzzerClientListener listener; | 323 FuzzerClientListener listener; |
| 330 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER, | 324 CreateChannel(&listener); |
| 331 &listener); | 325 listener.Init(channel()); |
| 332 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan); | 326 ASSERT_TRUE(ConnectChannel()); |
| 333 ASSERT_TRUE(server_process); | 327 ASSERT_TRUE(StartClient()); |
| 334 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | |
| 335 ASSERT_TRUE(chan.Connect()); | |
| 336 listener.Init(&chan); | |
| 337 | 328 |
| 338 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID, | 329 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID, |
| 339 IPC::Message::PRIORITY_NORMAL); | 330 IPC::Message::PRIORITY_NORMAL); |
| 340 msg->WriteWString(L"d"); | 331 msg->WriteWString(L"d"); |
| 341 msg->WriteInt(0); | 332 msg->WriteInt(0); |
| 342 msg->WriteInt(0x65); // Extra argument. | 333 msg->WriteInt(0x65); // Extra argument. |
| 343 | 334 |
| 344 chan.Send(msg); | 335 sender()->Send(msg); |
| 345 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID)); | 336 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID)); |
| 346 | 337 |
| 347 // Now send a well formed message to make sure the receiver wasn't | 338 // Now send a well formed message to make sure the receiver wasn't |
| 348 // thrown out of sync by the extra argument. | 339 // thrown out of sync by the extra argument. |
| 349 msg = new MsgClassIS(3, L"expect three"); | 340 msg = new MsgClassIS(3, L"expect three"); |
| 350 chan.Send(msg); | 341 sender()->Send(msg); |
| 351 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID)); | 342 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID)); |
| 352 | 343 |
| 353 EXPECT_TRUE(base::WaitForSingleProcess( | 344 EXPECT_TRUE(WaitForClientShutdown()); |
| 354 server_process, base::TimeDelta::FromSeconds(5))); | 345 DestroyChannel(); |
| 355 base::CloseProcessHandle(server_process); | |
| 356 } | 346 } |
| 357 | 347 |
| 358 // This class is for testing the IPC_BEGIN_MESSAGE_MAP_EX macros. | 348 // This class is for testing the IPC_BEGIN_MESSAGE_MAP_EX macros. |
| 359 class ServerMacroExTest { | 349 class ServerMacroExTest { |
| 360 public: | 350 public: |
| 361 ServerMacroExTest() : unhandled_msgs_(0) { | 351 ServerMacroExTest() : unhandled_msgs_(0) { |
| 362 } | 352 } |
| 363 | 353 |
| 364 virtual ~ServerMacroExTest() { | 354 virtual ~ServerMacroExTest() { |
| 365 } | 355 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 msg->WriteInt(0x64); | 404 msg->WriteInt(0x64); |
| 415 msg->WriteInt(0x32); | 405 msg->WriteInt(0x32); |
| 416 EXPECT_FALSE(server.OnMessageReceived(*msg)); | 406 EXPECT_FALSE(server.OnMessageReceived(*msg)); |
| 417 delete msg; | 407 delete msg; |
| 418 | 408 |
| 419 EXPECT_EQ(0, server.unhandled_msgs()); | 409 EXPECT_EQ(0, server.unhandled_msgs()); |
| 420 #endif | 410 #endif |
| 421 } | 411 } |
| 422 | 412 |
| 423 } // namespace | 413 } // namespace |
| OLD | NEW |