| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 ~MessageCountFilter() override {} | 225 ~MessageCountFilter() override {} |
| 226 | 226 |
| 227 size_t messages_received_; | 227 size_t messages_received_; |
| 228 uint32_t supported_message_class_; | 228 uint32_t supported_message_class_; |
| 229 bool is_global_filter_; | 229 bool is_global_filter_; |
| 230 | 230 |
| 231 FilterEvent last_filter_event_; | 231 FilterEvent last_filter_event_; |
| 232 bool message_filtering_enabled_; | 232 bool message_filtering_enabled_; |
| 233 }; | 233 }; |
| 234 | 234 |
| 235 class IPCChannelProxyTest : public IPCTestBase { | 235 class IPCChannelProxyTest : public IPCChannelMojoTestBase { |
| 236 public: | 236 public: |
| 237 IPCChannelProxyTest() {} | 237 IPCChannelProxyTest() {} |
| 238 ~IPCChannelProxyTest() override {} | 238 ~IPCChannelProxyTest() override {} |
| 239 | 239 |
| 240 void SetUp() override { | 240 void SetUp() override { |
| 241 IPCTestBase::SetUp(); | 241 IPCChannelMojoTestBase::SetUp(); |
| 242 | 242 |
| 243 Init("ChannelProxyClient"); | 243 Init("ChannelProxyClient"); |
| 244 | 244 |
| 245 thread_.reset(new base::Thread("ChannelProxyTestServerThread")); | 245 thread_.reset(new base::Thread("ChannelProxyTestServerThread")); |
| 246 base::Thread::Options options; | 246 base::Thread::Options options; |
| 247 options.message_loop_type = base::MessageLoop::TYPE_IO; | 247 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 248 thread_->StartWithOptions(options); | 248 thread_->StartWithOptions(options); |
| 249 | 249 |
| 250 listener_.reset(new QuitListener()); | 250 listener_.reset(new QuitListener()); |
| 251 CreateChannelProxy(listener_.get(), thread_->task_runner().get()); | 251 channel_proxy_ = IPC::ChannelProxy::Create( |
| 252 | 252 TakeHandle().release(), IPC::Channel::MODE_SERVER, listener_.get(), |
| 253 ASSERT_TRUE(StartClient()); | 253 thread_->task_runner()); |
| 254 } | 254 } |
| 255 | 255 |
| 256 void TearDown() override { | 256 void TearDown() override { |
| 257 DestroyChannelProxy(); | 257 channel_proxy_.reset(); |
| 258 thread_.reset(); | 258 thread_.reset(); |
| 259 listener_.reset(); | 259 listener_.reset(); |
| 260 IPCTestBase::TearDown(); | 260 IPCChannelMojoTestBase::TearDown(); |
| 261 } | 261 } |
| 262 | 262 |
| 263 void SendQuitMessageAndWaitForIdle() { | 263 void SendQuitMessageAndWaitForIdle() { |
| 264 sender()->Send(new WorkerMsg_Quit); | 264 sender()->Send(new WorkerMsg_Quit); |
| 265 base::RunLoop().Run(); | 265 base::RunLoop().Run(); |
| 266 EXPECT_TRUE(WaitForClientShutdown()); | 266 EXPECT_TRUE(WaitForClientShutdown()); |
| 267 } | 267 } |
| 268 | 268 |
| 269 bool DidListenerGetBadMessage() { | 269 bool DidListenerGetBadMessage() { |
| 270 return listener_->bad_message_received_; | 270 return listener_->bad_message_received_; |
| 271 } | 271 } |
| 272 | 272 |
| 273 IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); } |
| 274 IPC::Sender* sender() { return channel_proxy_.get(); } |
| 275 |
| 273 private: | 276 private: |
| 274 std::unique_ptr<base::Thread> thread_; | 277 std::unique_ptr<base::Thread> thread_; |
| 275 std::unique_ptr<QuitListener> listener_; | 278 std::unique_ptr<QuitListener> listener_; |
| 279 std::unique_ptr<IPC::ChannelProxy> channel_proxy_; |
| 276 }; | 280 }; |
| 277 | 281 |
| 278 TEST_F(IPCChannelProxyTest, MessageClassFilters) { | 282 TEST_F(IPCChannelProxyTest, MessageClassFilters) { |
| 279 // Construct a filter per message class. | 283 // Construct a filter per message class. |
| 280 std::vector<scoped_refptr<MessageCountFilter> > class_filters; | 284 std::vector<scoped_refptr<MessageCountFilter> > class_filters; |
| 281 class_filters.push_back(make_scoped_refptr( | 285 class_filters.push_back(make_scoped_refptr( |
| 282 new MessageCountFilter(TestMsgStart))); | 286 new MessageCountFilter(TestMsgStart))); |
| 283 class_filters.push_back(make_scoped_refptr( | 287 class_filters.push_back(make_scoped_refptr( |
| 284 new MessageCountFilter(UtilityMsgStart))); | 288 new MessageCountFilter(UtilityMsgStart))); |
| 285 for (size_t i = 0; i < class_filters.size(); ++i) | 289 for (size_t i = 0; i < class_filters.size(); ++i) |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 // Ensure that the filters were removed and did not receive any messages. | 351 // Ensure that the filters were removed and did not receive any messages. |
| 348 SendQuitMessageAndWaitForIdle(); | 352 SendQuitMessageAndWaitForIdle(); |
| 349 EXPECT_EQ(MessageCountFilter::FILTER_REMOVED, | 353 EXPECT_EQ(MessageCountFilter::FILTER_REMOVED, |
| 350 global_filter->last_filter_event()); | 354 global_filter->last_filter_event()); |
| 351 EXPECT_EQ(MessageCountFilter::FILTER_REMOVED, | 355 EXPECT_EQ(MessageCountFilter::FILTER_REMOVED, |
| 352 class_filter->last_filter_event()); | 356 class_filter->last_filter_event()); |
| 353 EXPECT_EQ(0U, class_filter->messages_received()); | 357 EXPECT_EQ(0U, class_filter->messages_received()); |
| 354 EXPECT_EQ(0U, global_filter->messages_received()); | 358 EXPECT_EQ(0U, global_filter->messages_received()); |
| 355 } | 359 } |
| 356 | 360 |
| 357 // The test that follow trigger DCHECKS in debug build. | |
| 358 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | |
| 359 | |
| 360 TEST_F(IPCChannelProxyTest, BadMessageOnListenerThread) { | 361 TEST_F(IPCChannelProxyTest, BadMessageOnListenerThread) { |
| 361 scoped_refptr<MessageCountFilter> class_filter( | 362 scoped_refptr<MessageCountFilter> class_filter( |
| 362 new MessageCountFilter(TestMsgStart)); | 363 new MessageCountFilter(TestMsgStart)); |
| 363 class_filter->set_message_filtering_enabled(false); | 364 class_filter->set_message_filtering_enabled(false); |
| 364 channel_proxy()->AddFilter(class_filter.get()); | 365 channel_proxy()->AddFilter(class_filter.get()); |
| 365 | 366 |
| 366 sender()->Send(new TestMsg_SendBadMessage()); | 367 sender()->Send(new TestMsg_SendBadMessage()); |
| 367 | 368 |
| 368 SendQuitMessageAndWaitForIdle(); | 369 SendQuitMessageAndWaitForIdle(); |
| 369 EXPECT_TRUE(DidListenerGetBadMessage()); | 370 EXPECT_TRUE(DidListenerGetBadMessage()); |
| 370 } | 371 } |
| 371 | 372 |
| 372 TEST_F(IPCChannelProxyTest, BadMessageOnIPCThread) { | 373 TEST_F(IPCChannelProxyTest, BadMessageOnIPCThread) { |
| 373 scoped_refptr<MessageCountFilter> class_filter( | 374 scoped_refptr<MessageCountFilter> class_filter( |
| 374 new MessageCountFilter(TestMsgStart)); | 375 new MessageCountFilter(TestMsgStart)); |
| 375 class_filter->set_message_filtering_enabled(true); | 376 class_filter->set_message_filtering_enabled(true); |
| 376 channel_proxy()->AddFilter(class_filter.get()); | 377 channel_proxy()->AddFilter(class_filter.get()); |
| 377 | 378 |
| 378 sender()->Send(new TestMsg_SendBadMessage()); | 379 sender()->Send(new TestMsg_SendBadMessage()); |
| 379 | 380 |
| 380 SendQuitMessageAndWaitForIdle(); | 381 SendQuitMessageAndWaitForIdle(); |
| 381 EXPECT_TRUE(DidListenerGetBadMessage()); | 382 EXPECT_TRUE(DidListenerGetBadMessage()); |
| 382 } | 383 } |
| 383 | 384 |
| 384 class IPCChannelBadMessageTest : public IPCTestBase { | 385 class IPCChannelBadMessageTest : public IPCChannelMojoTestBase { |
| 385 public: | 386 public: |
| 386 void SetUp() override { | 387 void SetUp() override { |
| 387 IPCTestBase::SetUp(); | 388 IPCChannelMojoTestBase::SetUp(); |
| 388 | 389 |
| 389 Init("ChannelProxyClient"); | 390 Init("ChannelProxyClient"); |
| 390 | 391 |
| 391 listener_.reset(new QuitListener()); | 392 listener_.reset(new QuitListener()); |
| 392 CreateChannel(listener_.get()); | 393 CreateChannel(listener_.get()); |
| 393 ASSERT_TRUE(ConnectChannel()); | 394 ASSERT_TRUE(ConnectChannel()); |
| 394 | |
| 395 ASSERT_TRUE(StartClient()); | |
| 396 } | 395 } |
| 397 | 396 |
| 398 void TearDown() override { | 397 void TearDown() override { |
| 398 IPCChannelMojoTestBase::TearDown(); |
| 399 listener_.reset(); | 399 listener_.reset(); |
| 400 IPCTestBase::TearDown(); | |
| 401 } | 400 } |
| 402 | 401 |
| 403 void SendQuitMessageAndWaitForIdle() { | 402 void SendQuitMessageAndWaitForIdle() { |
| 404 sender()->Send(new WorkerMsg_Quit); | 403 sender()->Send(new WorkerMsg_Quit); |
| 405 base::RunLoop().Run(); | 404 base::RunLoop().Run(); |
| 406 EXPECT_TRUE(WaitForClientShutdown()); | 405 EXPECT_TRUE(WaitForClientShutdown()); |
| 407 } | 406 } |
| 408 | 407 |
| 409 bool DidListenerGetBadMessage() { | 408 bool DidListenerGetBadMessage() { |
| 410 return listener_->bad_message_received_; | 409 return listener_->bad_message_received_; |
| 411 } | 410 } |
| 412 | 411 |
| 413 private: | 412 private: |
| 414 std::unique_ptr<QuitListener> listener_; | 413 std::unique_ptr<QuitListener> listener_; |
| 415 }; | 414 }; |
| 416 | 415 |
| 417 #if !defined(OS_WIN) | 416 #if !defined(OS_WIN) |
| 418 // TODO(jam): for some reason this is flaky on win buildbots. | 417 // TODO(jam): for some reason this is flaky on win buildbots. |
| 419 TEST_F(IPCChannelBadMessageTest, BadMessage) { | 418 TEST_F(IPCChannelBadMessageTest, BadMessage) { |
| 420 sender()->Send(new TestMsg_SendBadMessage()); | 419 sender()->Send(new TestMsg_SendBadMessage()); |
| 421 SendQuitMessageAndWaitForIdle(); | 420 SendQuitMessageAndWaitForIdle(); |
| 422 EXPECT_TRUE(DidListenerGetBadMessage()); | 421 EXPECT_TRUE(DidListenerGetBadMessage()); |
| 423 } | 422 } |
| 424 #endif | 423 #endif |
| 425 | 424 |
| 426 #endif | 425 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ChannelProxyClient) { |
| 427 | |
| 428 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ChannelProxyClient) { | |
| 429 base::MessageLoopForIO main_message_loop; | |
| 430 ChannelReflectorListener listener; | 426 ChannelReflectorListener listener; |
| 431 std::unique_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( | 427 Connect(&listener); |
| 432 IPCTestBase::GetChannelName("ChannelProxyClient"), &listener, | 428 listener.Init(channel()); |
| 433 main_message_loop.task_runner())); | |
| 434 CHECK(channel->Connect()); | |
| 435 listener.Init(channel.get()); | |
| 436 | 429 |
| 437 base::RunLoop().Run(); | 430 base::RunLoop().Run(); |
| 438 return 0; | 431 Close(); |
| 439 } | 432 } |
| 440 | 433 |
| 441 } // namespace | 434 } // namespace |
| OLD | NEW |