Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: mojo/edk/embedder/embedder_unittest.cc

Issue 2466433002: Add mojo::edk::ClosePeerConnection. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/embedder/embedder.cc ('k') | mojo/edk/embedder/named_platform_handle_utils_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "mojo/edk/embedder/embedder.h" 5 #include "mojo/edk/embedder/embedder.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <string.h> 9 #include <string.h>
10 10
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/base_paths.h"
13 #include "base/bind.h" 14 #include "base/bind.h"
14 #include "base/command_line.h" 15 #include "base/command_line.h"
15 #include "base/files/file.h" 16 #include "base/files/file.h"
16 #include "base/logging.h" 17 #include "base/logging.h"
17 #include "base/macros.h" 18 #include "base/macros.h"
18 #include "base/memory/shared_memory.h" 19 #include "base/memory/shared_memory.h"
19 #include "base/message_loop/message_loop.h" 20 #include "base/message_loop/message_loop.h"
21 #include "base/path_service.h"
20 #include "base/process/process_handle.h" 22 #include "base/process/process_handle.h"
23 #include "base/run_loop.h"
21 #include "base/synchronization/waitable_event.h" 24 #include "base/synchronization/waitable_event.h"
22 #include "base/test/test_timeouts.h" 25 #include "base/test/test_timeouts.h"
26 #include "mojo/edk/embedder/named_platform_handle.h"
27 #include "mojo/edk/embedder/named_platform_handle_utils.h"
23 #include "mojo/edk/embedder/platform_channel_pair.h" 28 #include "mojo/edk/embedder/platform_channel_pair.h"
24 #include "mojo/edk/embedder/test_embedder.h" 29 #include "mojo/edk/embedder/test_embedder.h"
25 #include "mojo/edk/system/test_utils.h" 30 #include "mojo/edk/system/test_utils.h"
26 #include "mojo/edk/test/mojo_test_base.h" 31 #include "mojo/edk/test/mojo_test_base.h"
32 #include "mojo/edk/test/scoped_ipc_support.h"
27 #include "mojo/public/c/system/core.h" 33 #include "mojo/public/c/system/core.h"
28 #include "mojo/public/cpp/system/handle.h" 34 #include "mojo/public/cpp/system/handle.h"
29 #include "mojo/public/cpp/system/message_pipe.h" 35 #include "mojo/public/cpp/system/message_pipe.h"
30 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
31 37
32 namespace mojo { 38 namespace mojo {
33 namespace edk { 39 namespace edk {
34 namespace { 40 namespace {
35 41
36 const MojoHandleSignals kSignalReadadableWritable = 42 const MojoHandleSignals kSignalReadadableWritable =
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 WriteMessage(client_mp, "bye"); 556 WriteMessage(client_mp, "bye");
551 } 557 }
552 558
553 #endif // defined(OS_MACOSX) && !defined(OS_IOS) 559 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
554 560
555 // TODO(vtl): Test immediate write & close. 561 // TODO(vtl): Test immediate write & close.
556 // TODO(vtl): Test broken-connection cases. 562 // TODO(vtl): Test broken-connection cases.
557 563
558 #endif // !defined(OS_IOS) 564 #endif // !defined(OS_IOS)
559 565
566 NamedPlatformHandle GenerateChannelName() {
567 #if defined(OS_POSIX)
568 base::FilePath temp_dir;
569 CHECK(base::PathService::Get(base::DIR_TEMP, &temp_dir));
570 return NamedPlatformHandle(
571 temp_dir.AppendASCII(GenerateRandomToken()).value());
572 #else
573 return NamedPlatformHandle(GenerateRandomToken());
574 #endif
575 }
576
577 void CreateClientHandleOnIoThread(const NamedPlatformHandle& named_handle,
578 ScopedPlatformHandle* output) {
579 *output = CreateClientHandle(named_handle);
580 }
581
582 TEST_F(EmbedderTest, ClosePendingPeerConnection) {
583 NamedPlatformHandle named_handle = GenerateChannelName();
584 std::string peer_token = GenerateRandomToken();
585 ScopedMessagePipeHandle server_pipe =
586 ConnectToPeerProcess(CreateServerHandle(named_handle), peer_token);
587 ClosePeerConnection(peer_token);
588 EXPECT_EQ(MOJO_RESULT_OK,
589 Wait(server_pipe.get(), MOJO_HANDLE_SIGNAL_PEER_CLOSED,
590 MOJO_DEADLINE_INDEFINITE, nullptr));
591 base::MessageLoop message_loop;
592 base::RunLoop run_loop;
593 ScopedPlatformHandle client_handle;
594 // Closing the channel involves posting a task to the IO thread to do the
595 // work. By the time the local message pipe has been observerd as closed,
596 // that task will have been posted. Therefore, a task to create the client
597 // connection should be handled after the channel is closed.
598 test::GetIoTaskRunner()->PostTaskAndReply(
599 FROM_HERE,
600 base::Bind(&CreateClientHandleOnIoThread, named_handle, &client_handle),
601 run_loop.QuitClosure());
602 run_loop.Run();
603 EXPECT_FALSE(client_handle.is_valid());
604 }
605
606 #if !defined(OS_IOS)
607
608 TEST_F(EmbedderTest, ClosePipeToConnectedPeer) {
609 set_launch_type(LaunchType::PEER);
610 auto& controller = StartClient("ClosePipeToConnectedPeerClient");
611 MojoHandle server_mp = controller.pipe();
612 // 1. Write a message to |server_mp| (attaching nothing).
613 WriteMessage(server_mp, "hello");
614
615 // 2. Read a message from |server_mp|.
616 EXPECT_EQ("world!", ReadMessage(server_mp));
617
618 controller.ClosePeerConnection();
619
620 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(server_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
621 MOJO_DEADLINE_INDEFINITE, nullptr));
622
623 EXPECT_EQ(0, controller.WaitForShutdown());
624 }
625
626 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ClosePipeToConnectedPeerClient, EmbedderTest,
627 client_mp) {
628 // 1. Read the first message from |client_mp|.
629 EXPECT_EQ("hello", ReadMessage(client_mp));
630
631 // 2. Write a message to |client_mp| (attaching nothing).
632 WriteMessage(client_mp, "world!");
633
634 ASSERT_EQ(MOJO_RESULT_OK,
635 MojoWait(client_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
636 MOJO_DEADLINE_INDEFINITE, nullptr));
637 }
638
639 TEST_F(EmbedderTest, ClosePipeToConnectingPeer) {
640 set_launch_type(LaunchType::PEER);
641 auto& controller = StartClient("ClosePipeToConnectingPeerClient");
642 controller.ClosePeerConnection();
643
644 MojoHandle server_mp = controller.pipe();
645
646 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(server_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
647 MOJO_DEADLINE_INDEFINITE, nullptr));
648
649 EXPECT_EQ(0, controller.WaitForShutdown());
650 }
651
652 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ClosePipeToConnectingPeerClient, EmbedderTest,
653 client_mp) {
654 ASSERT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
655 MOJO_DEADLINE_INDEFINITE, nullptr));
656 }
657
658 #endif // !defined(OS_IOS)
659
560 } // namespace 660 } // namespace
561 } // namespace edk 661 } // namespace edk
562 } // namespace mojo 662 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/embedder/embedder.cc ('k') | mojo/edk/embedder/named_platform_handle_utils_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698