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
3 // found in the LICENSE file.
5 #include "build/build_config.h"
13 #include "base/pickle.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/thread.h"
17 #include "ipc/ipc_test_base.h"
18 #include "ipc/ipc_test_channel_listener.h"
22 class IPCChannelTest
: public IPCTestBase
{
25 #if defined(OS_ANDROID)
26 #define MAYBE_ChannelTest DISABLED_ChannelTest
28 #define MAYBE_ChannelTest ChannelTest
30 TEST_F(IPCChannelTest
, MAYBE_ChannelTest
) {
31 Init("GenericClient");
33 // Set up IPC channel and start client.
34 IPC::TestChannelListener listener
;
35 CreateChannel(&listener
);
36 listener
.Init(sender());
37 ASSERT_TRUE(ConnectChannel());
38 ASSERT_TRUE(StartClient());
40 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
43 base::MessageLoop::current()->Run();
45 // Close the channel so the client's OnChannelError() gets fired.
48 EXPECT_TRUE(WaitForClientShutdown());
52 // TODO(viettrungluu): Move to a separate IPCChannelWinTest.
54 TEST_F(IPCChannelTest
, ChannelTestExistingPipe
) {
55 Init("GenericClient");
57 // Create pipe manually using the standard Chromium name and set up IPC
59 IPC::TestChannelListener listener
;
60 std::string
name("\\\\.\\pipe\\chrome.");
61 name
.append(GetChannelName("GenericClient"));
62 HANDLE pipe
= CreateNamedPipeA(name
.c_str(),
63 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
|
64 FILE_FLAG_FIRST_PIPE_INSTANCE
,
65 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
,
71 CreateChannelFromChannelHandle(IPC::ChannelHandle(pipe
), &listener
);
72 CloseHandle(pipe
); // The channel duplicates the handle.
73 listener
.Init(sender());
75 // Connect to channel and start client.
76 ASSERT_TRUE(ConnectChannel());
77 ASSERT_TRUE(StartClient());
79 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
82 base::MessageLoop::current()->Run();
84 // Close the channel so the client's OnChannelError() gets fired.
87 EXPECT_TRUE(WaitForClientShutdown());
90 #endif // defined (OS_WIN)
92 #if defined(OS_ANDROID)
93 #define MAYBE_ChannelProxyTest DISABLED_ChannelProxyTest
95 #define MAYBE_ChannelProxyTest ChannelProxyTest
97 TEST_F(IPCChannelTest
, MAYBE_ChannelProxyTest
) {
98 Init("GenericClient");
100 base::Thread
thread("ChannelProxyTestServer");
101 base::Thread::Options options
;
102 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
103 thread
.StartWithOptions(options
);
105 // Set up IPC channel proxy.
106 IPC::TestChannelListener listener
;
107 CreateChannelProxy(&listener
, thread
.task_runner().get());
108 listener
.Init(sender());
110 ASSERT_TRUE(StartClient());
112 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
115 base::MessageLoop::current()->Run();
117 EXPECT_TRUE(WaitForClientShutdown());
119 // Destroy the channel proxy before shutting down the thread.
120 DestroyChannelProxy();
124 class ChannelListenerWithOnConnectedSend
: public IPC::TestChannelListener
{
126 ChannelListenerWithOnConnectedSend() {}
127 ~ChannelListenerWithOnConnectedSend() override
{}
129 void OnChannelConnected(int32_t peer_pid
) override
{
134 #if defined(OS_WIN) || defined(OS_ANDROID)
135 // Acting flakey in Windows. http://crbug.com/129595
136 #define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
138 #define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
140 // This tests the case of a listener sending back an event in its
141 // OnChannelConnected handler.
142 TEST_F(IPCChannelTest
, MAYBE_SendMessageInChannelConnected
) {
143 Init("GenericClient");
145 // Set up IPC channel and start client.
146 ChannelListenerWithOnConnectedSend listener
;
147 CreateChannel(&listener
);
148 listener
.Init(sender());
149 ASSERT_TRUE(ConnectChannel());
150 ASSERT_TRUE(StartClient());
152 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
155 base::MessageLoop::current()->Run();
157 // Close the channel so the client's OnChannelError() gets fired.
160 EXPECT_TRUE(WaitForClientShutdown());
164 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient
) {
165 base::MessageLoopForIO main_message_loop
;
166 IPC::TestChannelListener listener
;
168 // Set up IPC channel.
169 scoped_ptr
<IPC::Channel
> channel(IPC::Channel::CreateClient(
170 IPCTestBase::GetChannelName("GenericClient"), &listener
, nullptr));
171 CHECK(channel
->Connect());
172 listener
.Init(channel
.get());
173 IPC::TestChannelListener::SendOneMessage(channel
.get(), "hello from child");
175 base::MessageLoop::current()->Run();