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_message.h"
18 #include "ipc/ipc_test_base.h"
19 #include "ipc/ipc_test_channel_listener.h"
23 class IPCChannelTest
: public IPCTestBase
{
26 // TODO(viettrungluu): Move to a separate IPCMessageTest.
27 TEST_F(IPCChannelTest
, BasicMessageTest
) {
29 std::string
v2("foobar");
30 base::string16
v3(base::ASCIIToUTF16("hello world"));
32 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
33 EXPECT_TRUE(m
.WriteInt(v1
));
34 EXPECT_TRUE(m
.WriteString(v2
));
35 EXPECT_TRUE(m
.WriteString16(v3
));
37 PickleIterator
iter(m
);
43 EXPECT_TRUE(iter
.ReadInt(&vi
));
46 EXPECT_TRUE(iter
.ReadString(&vs
));
49 EXPECT_TRUE(iter
.ReadString16(&vs16
));
53 EXPECT_FALSE(iter
.ReadInt(&vi
));
54 EXPECT_FALSE(iter
.ReadString(&vs
));
55 EXPECT_FALSE(iter
.ReadString16(&vs16
));
58 TEST_F(IPCChannelTest
, ChannelTest
) {
59 Init("GenericClient");
61 // Set up IPC channel and start client.
62 IPC::TestChannelListener listener
;
63 CreateChannel(&listener
);
64 listener
.Init(sender());
65 ASSERT_TRUE(ConnectChannel());
66 ASSERT_TRUE(StartClient());
68 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
71 base::MessageLoop::current()->Run();
73 // Close the channel so the client's OnChannelError() gets fired.
76 EXPECT_TRUE(WaitForClientShutdown());
80 // TODO(viettrungluu): Move to a separate IPCChannelWinTest.
82 TEST_F(IPCChannelTest
, ChannelTestExistingPipe
) {
83 Init("GenericClient");
85 // Create pipe manually using the standard Chromium name and set up IPC
87 IPC::TestChannelListener listener
;
88 std::string
name("\\\\.\\pipe\\chrome.");
89 name
.append(GetChannelName("GenericClient"));
90 HANDLE pipe
= CreateNamedPipeA(name
.c_str(),
91 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
|
92 FILE_FLAG_FIRST_PIPE_INSTANCE
,
93 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
,
99 CreateChannelFromChannelHandle(IPC::ChannelHandle(pipe
), &listener
);
100 CloseHandle(pipe
); // The channel duplicates the handle.
101 listener
.Init(sender());
103 // Connect to channel and start client.
104 ASSERT_TRUE(ConnectChannel());
105 ASSERT_TRUE(StartClient());
107 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
110 base::MessageLoop::current()->Run();
112 // Close the channel so the client's OnChannelError() gets fired.
115 EXPECT_TRUE(WaitForClientShutdown());
118 #endif // defined (OS_WIN)
120 TEST_F(IPCChannelTest
, ChannelProxyTest
) {
121 Init("GenericClient");
123 base::Thread
thread("ChannelProxyTestServer");
124 base::Thread::Options options
;
125 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
126 thread
.StartWithOptions(options
);
128 // Set up IPC channel proxy.
129 IPC::TestChannelListener listener
;
130 CreateChannelProxy(&listener
, thread
.task_runner().get());
131 listener
.Init(sender());
133 ASSERT_TRUE(StartClient());
135 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
138 base::MessageLoop::current()->Run();
140 EXPECT_TRUE(WaitForClientShutdown());
142 // Destroy the channel proxy before shutting down the thread.
143 DestroyChannelProxy();
147 class ChannelListenerWithOnConnectedSend
: public IPC::TestChannelListener
{
149 ChannelListenerWithOnConnectedSend() {}
150 ~ChannelListenerWithOnConnectedSend() override
{}
152 void OnChannelConnected(int32 peer_pid
) override
{
158 // Acting flakey in Windows. http://crbug.com/129595
159 #define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
161 #define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
163 // This tests the case of a listener sending back an event in its
164 // OnChannelConnected handler.
165 TEST_F(IPCChannelTest
, MAYBE_SendMessageInChannelConnected
) {
166 Init("GenericClient");
168 // Set up IPC channel and start client.
169 ChannelListenerWithOnConnectedSend listener
;
170 CreateChannel(&listener
);
171 listener
.Init(sender());
172 ASSERT_TRUE(ConnectChannel());
173 ASSERT_TRUE(StartClient());
175 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
178 base::MessageLoop::current()->Run();
180 // Close the channel so the client's OnChannelError() gets fired.
183 EXPECT_TRUE(WaitForClientShutdown());
187 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient
) {
188 base::MessageLoopForIO main_message_loop
;
189 IPC::TestChannelListener listener
;
191 // Set up IPC channel.
192 scoped_ptr
<IPC::Channel
> channel(IPC::Channel::CreateClient(
193 IPCTestBase::GetChannelName("GenericClient"),
195 CHECK(channel
->Connect());
196 listener
.Init(channel
.get());
197 IPC::TestChannelListener::SendOneMessage(channel
.get(), "hello from child");
199 base::MessageLoop::current()->Run();