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/message_loop/message_loop.h"
14 #include "base/pickle.h"
15 #include "base/strings/string16.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/thread.h"
18 #include "ipc/ipc_message.h"
19 #include "ipc/ipc_test_base.h"
20 #include "ipc/ipc_test_channel_listener.h"
24 class IPCChannelTest
: public IPCTestBase
{
27 // TODO(viettrungluu): Move to a separate IPCMessageTest.
28 TEST_F(IPCChannelTest
, BasicMessageTest
) {
30 std::string
v2("foobar");
31 base::string16
v3(base::ASCIIToUTF16("hello world"));
33 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
34 EXPECT_TRUE(m
.WriteInt(v1
));
35 EXPECT_TRUE(m
.WriteString(v2
));
36 EXPECT_TRUE(m
.WriteString16(v3
));
38 PickleIterator
iter(m
);
44 EXPECT_TRUE(iter
.ReadInt(&vi
));
47 EXPECT_TRUE(iter
.ReadString(&vs
));
50 EXPECT_TRUE(iter
.ReadString16(&vs16
));
54 EXPECT_FALSE(iter
.ReadInt(&vi
));
55 EXPECT_FALSE(iter
.ReadString(&vs
));
56 EXPECT_FALSE(iter
.ReadString16(&vs16
));
59 TEST_F(IPCChannelTest
, ChannelTest
) {
60 Init("GenericClient");
62 // Set up IPC channel and start client.
63 IPC::TestChannelListener listener
;
64 CreateChannel(&listener
);
65 listener
.Init(sender());
66 ASSERT_TRUE(ConnectChannel());
67 ASSERT_TRUE(StartClient());
69 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
72 base::MessageLoop::current()->Run();
74 // Close the channel so the client's OnChannelError() gets fired.
77 EXPECT_TRUE(WaitForClientShutdown());
81 // TODO(viettrungluu): Move to a separate IPCChannelWinTest.
83 TEST_F(IPCChannelTest
, ChannelTestExistingPipe
) {
84 Init("GenericClient");
86 // Create pipe manually using the standard Chromium name and set up IPC
88 IPC::TestChannelListener listener
;
89 std::string
name("\\\\.\\pipe\\chrome.");
90 name
.append(GetChannelName("GenericClient"));
91 HANDLE pipe
= CreateNamedPipeA(name
.c_str(),
92 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
|
93 FILE_FLAG_FIRST_PIPE_INSTANCE
,
94 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
,
100 CreateChannelFromChannelHandle(IPC::ChannelHandle(pipe
), &listener
);
101 CloseHandle(pipe
); // The channel duplicates the handle.
102 listener
.Init(sender());
104 // Connect to channel and start client.
105 ASSERT_TRUE(ConnectChannel());
106 ASSERT_TRUE(StartClient());
108 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
111 base::MessageLoop::current()->Run();
113 // Close the channel so the client's OnChannelError() gets fired.
116 EXPECT_TRUE(WaitForClientShutdown());
119 #endif // defined (OS_WIN)
121 TEST_F(IPCChannelTest
, ChannelProxyTest
) {
122 Init("GenericClient");
124 base::Thread
thread("ChannelProxyTestServer");
125 base::Thread::Options options
;
126 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
127 thread
.StartWithOptions(options
);
129 // Set up IPC channel proxy.
130 IPC::TestChannelListener listener
;
131 CreateChannelProxy(&listener
, thread
.message_loop_proxy().get());
132 listener
.Init(sender());
134 ASSERT_TRUE(StartClient());
136 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
139 base::MessageLoop::current()->Run();
141 EXPECT_TRUE(WaitForClientShutdown());
143 // Destroy the channel proxy before shutting down the thread.
144 DestroyChannelProxy();
148 class ChannelListenerWithOnConnectedSend
: public IPC::TestChannelListener
{
150 ChannelListenerWithOnConnectedSend() {}
151 ~ChannelListenerWithOnConnectedSend() override
{}
153 void OnChannelConnected(int32 peer_pid
) override
{
159 // Acting flakey in Windows. http://crbug.com/129595
160 #define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
162 #define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
164 // This tests the case of a listener sending back an event in its
165 // OnChannelConnected handler.
166 TEST_F(IPCChannelTest
, MAYBE_SendMessageInChannelConnected
) {
167 Init("GenericClient");
169 // Set up IPC channel and start client.
170 ChannelListenerWithOnConnectedSend listener
;
171 CreateChannel(&listener
);
172 listener
.Init(sender());
173 ASSERT_TRUE(ConnectChannel());
174 ASSERT_TRUE(StartClient());
176 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
179 base::MessageLoop::current()->Run();
181 // Close the channel so the client's OnChannelError() gets fired.
184 EXPECT_TRUE(WaitForClientShutdown());
188 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient
) {
189 base::MessageLoopForIO main_message_loop
;
190 IPC::TestChannelListener listener
;
192 // Set up IPC channel.
193 scoped_ptr
<IPC::Channel
> channel(IPC::Channel::CreateClient(
194 IPCTestBase::GetChannelName("GenericClient"),
196 CHECK(channel
->Connect());
197 listener
.Init(channel
.get());
198 IPC::TestChannelListener::SendOneMessage(channel
.get(), "hello from child");
200 base::MessageLoop::current()->Run();