| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <tuple> | 10 #include <tuple> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
| 16 #include "base/memory/shared_memory_handle.h" | 16 #include "base/memory/shared_memory_handle.h" |
| 17 #include "base/run_loop.h" |
| 17 #include "base/win/scoped_handle.h" | 18 #include "base/win/scoped_handle.h" |
| 18 #include "ipc/attachment_broker_privileged_win.h" | 19 #include "ipc/attachment_broker_privileged_win.h" |
| 19 #include "ipc/attachment_broker_unprivileged_win.h" | 20 #include "ipc/attachment_broker_unprivileged_win.h" |
| 20 #include "ipc/handle_attachment_win.h" | 21 #include "ipc/handle_attachment_win.h" |
| 21 #include "ipc/handle_win.h" | 22 #include "ipc/handle_win.h" |
| 22 #include "ipc/ipc_listener.h" | 23 #include "ipc/ipc_listener.h" |
| 23 #include "ipc/ipc_message.h" | 24 #include "ipc/ipc_message.h" |
| 24 #include "ipc/ipc_test_base.h" | 25 #include "ipc/ipc_test_base.h" |
| 25 #include "ipc/ipc_test_messages.h" | 26 #include "ipc/ipc_test_messages.h" |
| 26 | 27 |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 // checks that the file has the same contents in the privileged process. | 349 // checks that the file has the same contents in the privileged process. |
| 349 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandle) { | 350 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandle) { |
| 350 Init("SendHandle"); | 351 Init("SendHandle"); |
| 351 | 352 |
| 352 CommonSetUp(); | 353 CommonSetUp(); |
| 353 ResultListener result_listener; | 354 ResultListener result_listener; |
| 354 get_proxy_listener()->set_listener(&result_listener); | 355 get_proxy_listener()->set_listener(&result_listener); |
| 355 | 356 |
| 356 HANDLE h = CreateTempFile(); | 357 HANDLE h = CreateTempFile(); |
| 357 SendMessageWithAttachment(h); | 358 SendMessageWithAttachment(h); |
| 358 base::MessageLoop::current()->Run(); | 359 base::RunLoop().Run(); |
| 359 | 360 |
| 360 // Check the result. | 361 // Check the result. |
| 361 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 362 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 362 get_proxy_listener()->get_reason()); | 363 get_proxy_listener()->get_reason()); |
| 363 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 364 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 364 | 365 |
| 365 CommonTearDown(); | 366 CommonTearDown(); |
| 366 } | 367 } |
| 367 | 368 |
| 368 // Similar to SendHandle, except the file HANDLE attached to the message has | 369 // Similar to SendHandle, except the file HANDLE attached to the message has |
| 369 // neither read nor write permissions. | 370 // neither read nor write permissions. |
| 370 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, | 371 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, |
| 371 SendHandleWithoutPermissions) { | 372 SendHandleWithoutPermissions) { |
| 372 Init("SendHandleWithoutPermissions"); | 373 Init("SendHandleWithoutPermissions"); |
| 373 | 374 |
| 374 CommonSetUp(); | 375 CommonSetUp(); |
| 375 ResultListener result_listener; | 376 ResultListener result_listener; |
| 376 get_proxy_listener()->set_listener(&result_listener); | 377 get_proxy_listener()->set_listener(&result_listener); |
| 377 | 378 |
| 378 HANDLE h = CreateTempFile(); | 379 HANDLE h = CreateTempFile(); |
| 379 HANDLE h2; | 380 HANDLE h2; |
| 380 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), | 381 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), |
| 381 &h2, 0, FALSE, DUPLICATE_CLOSE_SOURCE); | 382 &h2, 0, FALSE, DUPLICATE_CLOSE_SOURCE); |
| 382 ASSERT_TRUE(result); | 383 ASSERT_TRUE(result); |
| 383 IPC::HandleWin handle_win(h2, IPC::HandleWin::DUPLICATE); | 384 IPC::HandleWin handle_win(h2, IPC::HandleWin::DUPLICATE); |
| 384 IPC::Message* message = new TestHandleWinMsg(100, handle_win, 200); | 385 IPC::Message* message = new TestHandleWinMsg(100, handle_win, 200); |
| 385 sender()->Send(message); | 386 sender()->Send(message); |
| 386 base::MessageLoop::current()->Run(); | 387 base::RunLoop().Run(); |
| 387 | 388 |
| 388 // Check the result. | 389 // Check the result. |
| 389 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 390 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 390 get_proxy_listener()->get_reason()); | 391 get_proxy_listener()->get_reason()); |
| 391 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 392 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 392 | 393 |
| 393 CommonTearDown(); | 394 CommonTearDown(); |
| 394 } | 395 } |
| 395 | 396 |
| 396 // Similar to SendHandle, except the attachment's destination process is this | 397 // Similar to SendHandle, except the attachment's destination process is this |
| 397 // process. This is an unrealistic scenario, but simulates an unprivileged | 398 // process. This is an unrealistic scenario, but simulates an unprivileged |
| 398 // process sending an attachment to another unprivileged process. | 399 // process sending an attachment to another unprivileged process. |
| 399 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleToSelf) { | 400 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleToSelf) { |
| 400 Init("SendHandleToSelf"); | 401 Init("SendHandleToSelf"); |
| 401 | 402 |
| 402 set_broker(new MockBroker); | 403 set_broker(new MockBroker); |
| 403 | 404 |
| 404 PreConnectSetUp(); | 405 PreConnectSetUp(); |
| 405 // Technically, the channel is an endpoint, but we need the proxy listener to | 406 // Technically, the channel is an endpoint, but we need the proxy listener to |
| 406 // receive the messages so that it can quit the message loop. | 407 // receive the messages so that it can quit the message loop. |
| 407 channel()->SetAttachmentBrokerEndpoint(false); | 408 channel()->SetAttachmentBrokerEndpoint(false); |
| 408 PostConnectSetUp(); | 409 PostConnectSetUp(); |
| 409 get_proxy_listener()->set_listener(get_broker()); | 410 get_proxy_listener()->set_listener(get_broker()); |
| 410 | 411 |
| 411 HANDLE h = CreateTempFile(); | 412 HANDLE h = CreateTempFile(); |
| 412 SendMessageWithAttachment(h); | 413 SendMessageWithAttachment(h); |
| 413 base::MessageLoop::current()->Run(); | 414 base::RunLoop().Run(); |
| 414 | 415 |
| 415 // Get the received attachment. | 416 // Get the received attachment. |
| 416 IPC::BrokerableAttachment::AttachmentId* id = get_observer()->get_id(); | 417 IPC::BrokerableAttachment::AttachmentId* id = get_observer()->get_id(); |
| 417 scoped_refptr<IPC::BrokerableAttachment> received_attachment; | 418 scoped_refptr<IPC::BrokerableAttachment> received_attachment; |
| 418 get_broker()->GetAttachmentWithId(*id, &received_attachment); | 419 get_broker()->GetAttachmentWithId(*id, &received_attachment); |
| 419 ASSERT_NE(received_attachment.get(), nullptr); | 420 ASSERT_NE(received_attachment.get(), nullptr); |
| 420 | 421 |
| 421 // Check that it's a different entry in the HANDLE table. | 422 // Check that it's a different entry in the HANDLE table. |
| 422 ScopedHandle h2(GetHandleFromBrokeredAttachment(received_attachment)); | 423 ScopedHandle h2(GetHandleFromBrokeredAttachment(received_attachment)); |
| 423 EXPECT_NE(h2.Get(), h); | 424 EXPECT_NE(h2.Get(), h); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 440 | 441 |
| 441 HANDLE h = CreateTempFile(); | 442 HANDLE h = CreateTempFile(); |
| 442 HANDLE h2; | 443 HANDLE h2; |
| 443 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), | 444 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), |
| 444 &h2, 0, FALSE, DUPLICATE_SAME_ACCESS); | 445 &h2, 0, FALSE, DUPLICATE_SAME_ACCESS); |
| 445 ASSERT_TRUE(result); | 446 ASSERT_TRUE(result); |
| 446 IPC::HandleWin handle_win1(h, IPC::HandleWin::FILE_READ_WRITE); | 447 IPC::HandleWin handle_win1(h, IPC::HandleWin::FILE_READ_WRITE); |
| 447 IPC::HandleWin handle_win2(h2, IPC::HandleWin::FILE_READ_WRITE); | 448 IPC::HandleWin handle_win2(h2, IPC::HandleWin::FILE_READ_WRITE); |
| 448 IPC::Message* message = new TestTwoHandleWinMsg(handle_win1, handle_win2); | 449 IPC::Message* message = new TestTwoHandleWinMsg(handle_win1, handle_win2); |
| 449 sender()->Send(message); | 450 sender()->Send(message); |
| 450 base::MessageLoop::current()->Run(); | 451 base::RunLoop().Run(); |
| 451 | 452 |
| 452 // Check the result. | 453 // Check the result. |
| 453 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 454 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 454 get_proxy_listener()->get_reason()); | 455 get_proxy_listener()->get_reason()); |
| 455 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 456 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 456 | 457 |
| 457 CommonTearDown(); | 458 CommonTearDown(); |
| 458 } | 459 } |
| 459 | 460 |
| 460 // Similar to SendHandle, but sends the same message twice. | 461 // Similar to SendHandle, but sends the same message twice. |
| 461 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleTwice) { | 462 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleTwice) { |
| 462 Init("SendHandleTwice"); | 463 Init("SendHandleTwice"); |
| 463 | 464 |
| 464 CommonSetUp(); | 465 CommonSetUp(); |
| 465 ResultListener result_listener; | 466 ResultListener result_listener; |
| 466 get_proxy_listener()->set_listener(&result_listener); | 467 get_proxy_listener()->set_listener(&result_listener); |
| 467 | 468 |
| 468 HANDLE h = CreateTempFile(); | 469 HANDLE h = CreateTempFile(); |
| 469 HANDLE h2; | 470 HANDLE h2; |
| 470 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), | 471 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), |
| 471 &h2, 0, FALSE, DUPLICATE_SAME_ACCESS); | 472 &h2, 0, FALSE, DUPLICATE_SAME_ACCESS); |
| 472 ASSERT_TRUE(result); | 473 ASSERT_TRUE(result); |
| 473 SendMessageWithAttachment(h); | 474 SendMessageWithAttachment(h); |
| 474 SendMessageWithAttachment(h2); | 475 SendMessageWithAttachment(h2); |
| 475 base::MessageLoop::current()->Run(); | 476 base::RunLoop().Run(); |
| 476 | 477 |
| 477 // Check the result. | 478 // Check the result. |
| 478 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 479 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 479 get_proxy_listener()->get_reason()); | 480 get_proxy_listener()->get_reason()); |
| 480 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 481 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 481 | 482 |
| 482 CommonTearDown(); | 483 CommonTearDown(); |
| 483 } | 484 } |
| 484 | 485 |
| 485 // An unprivileged process makes a shared memory region and sends it to the | 486 // An unprivileged process makes a shared memory region and sends it to the |
| 486 // privileged process. | 487 // privileged process. |
| 487 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendSharedMemoryHandle) { | 488 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendSharedMemoryHandle) { |
| 488 Init("SendSharedMemoryHandle"); | 489 Init("SendSharedMemoryHandle"); |
| 489 | 490 |
| 490 CommonSetUp(); | 491 CommonSetUp(); |
| 491 ResultListener result_listener; | 492 ResultListener result_listener; |
| 492 get_proxy_listener()->set_listener(&result_listener); | 493 get_proxy_listener()->set_listener(&result_listener); |
| 493 | 494 |
| 494 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); | 495 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); |
| 495 shared_memory->CreateAndMapAnonymous(kSharedMemorySize); | 496 shared_memory->CreateAndMapAnonymous(kSharedMemorySize); |
| 496 memcpy(shared_memory->memory(), kDataBuffer, strlen(kDataBuffer)); | 497 memcpy(shared_memory->memory(), kDataBuffer, strlen(kDataBuffer)); |
| 497 sender()->Send(new TestSharedMemoryHandleMsg1(shared_memory->handle())); | 498 sender()->Send(new TestSharedMemoryHandleMsg1(shared_memory->handle())); |
| 498 base::MessageLoop::current()->Run(); | 499 base::RunLoop().Run(); |
| 499 | 500 |
| 500 // Check the result. | 501 // Check the result. |
| 501 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 502 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 502 get_proxy_listener()->get_reason()); | 503 get_proxy_listener()->get_reason()); |
| 503 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 504 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 504 | 505 |
| 505 CommonTearDown(); | 506 CommonTearDown(); |
| 506 } | 507 } |
| 507 | 508 |
| 508 using OnMessageReceivedCallback = void (*)(IPC::Sender* sender, | 509 using OnMessageReceivedCallback = void (*)(IPC::Sender* sender, |
| 509 const IPC::Message& message); | 510 const IPC::Message& message); |
| 510 | 511 |
| 511 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, | 512 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, |
| 512 const char* channel_name) { | 513 const char* channel_name) { |
| 513 LOG(INFO) << "Privileged process start."; | 514 LOG(INFO) << "Privileged process start."; |
| 514 base::MessageLoopForIO main_message_loop; | 515 base::MessageLoopForIO main_message_loop; |
| 515 ProxyListener listener; | 516 ProxyListener listener; |
| 516 | 517 |
| 517 // Set up IPC channel. | 518 // Set up IPC channel. |
| 518 IPC::AttachmentBrokerPrivilegedWin broker; | 519 IPC::AttachmentBrokerPrivilegedWin broker; |
| 519 std::unique_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( | 520 std::unique_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( |
| 520 IPCTestBase::GetChannelName(channel_name), &listener)); | 521 IPCTestBase::GetChannelName(channel_name), &listener)); |
| 521 broker.RegisterCommunicationChannel(channel.get(), nullptr); | 522 broker.RegisterCommunicationChannel(channel.get(), nullptr); |
| 522 CHECK(channel->Connect()); | 523 CHECK(channel->Connect()); |
| 523 | 524 |
| 524 while (true) { | 525 while (true) { |
| 525 LOG(INFO) << "Privileged process spinning run loop."; | 526 LOG(INFO) << "Privileged process spinning run loop."; |
| 526 base::MessageLoop::current()->Run(); | 527 base::RunLoop().Run(); |
| 527 ProxyListener::Reason reason = listener.get_reason(); | 528 ProxyListener::Reason reason = listener.get_reason(); |
| 528 if (reason == ProxyListener::CHANNEL_ERROR) | 529 if (reason == ProxyListener::CHANNEL_ERROR) |
| 529 break; | 530 break; |
| 530 | 531 |
| 531 while (listener.has_message()) { | 532 while (listener.has_message()) { |
| 532 LOG(INFO) << "Privileged process running callback."; | 533 LOG(INFO) << "Privileged process running callback."; |
| 533 callback(channel.get(), listener.get_first_message()); | 534 callback(channel.get(), listener.get_first_message()); |
| 534 LOG(INFO) << "Privileged process finishing callback."; | 535 LOG(INFO) << "Privileged process finishing callback."; |
| 535 listener.pop_first_message(); | 536 listener.pop_first_message(); |
| 536 } | 537 } |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 memcmp(shared_memory->memory(), kDataBuffer, strlen(kDataBuffer)) == 0; | 658 memcmp(shared_memory->memory(), kDataBuffer, strlen(kDataBuffer)) == 0; |
| 658 SendControlMessage(sender, success); | 659 SendControlMessage(sender, success); |
| 659 } | 660 } |
| 660 | 661 |
| 661 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandle) { | 662 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandle) { |
| 662 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleCallback, | 663 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleCallback, |
| 663 "SendSharedMemoryHandle"); | 664 "SendSharedMemoryHandle"); |
| 664 } | 665 } |
| 665 | 666 |
| 666 } // namespace | 667 } // namespace |
| OLD | NEW |