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/threading/thread.h"
16 #include "ipc/ipc_message.h"
17 #include "ipc/ipc_test_base.h"
18 #include "ipc/ipc_test_channel_listener.h"
22 class IPCChannelTest
: public IPCTestBase
{
25 // TODO(viettrungluu): Move to a separate IPCMessageTest.
26 TEST_F(IPCChannelTest
, BasicMessageTest
) {
28 std::string
v2("foobar");
29 std::wstring
v3(L
"hello world");
31 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
32 EXPECT_TRUE(m
.WriteInt(v1
));
33 EXPECT_TRUE(m
.WriteString(v2
));
34 EXPECT_TRUE(m
.WriteWString(v3
));
36 PickleIterator
iter(m
);
42 EXPECT_TRUE(m
.ReadInt(&iter
, &vi
));
45 EXPECT_TRUE(m
.ReadString(&iter
, &vs
));
48 EXPECT_TRUE(m
.ReadWString(&iter
, &vw
));
52 EXPECT_FALSE(m
.ReadInt(&iter
, &vi
));
53 EXPECT_FALSE(m
.ReadString(&iter
, &vs
));
54 EXPECT_FALSE(m
.ReadWString(&iter
, &vw
));
57 TEST_F(IPCChannelTest
, ChannelTest
) {
58 Init("GenericClient");
60 // Set up IPC channel and start client.
61 IPC::TestChannelListener listener
;
62 CreateChannel(&listener
);
63 listener
.Init(sender());
64 ASSERT_TRUE(ConnectChannel());
65 ASSERT_TRUE(StartClient());
67 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
70 base::MessageLoop::current()->Run();
72 // Close the channel so the client's OnChannelError() gets fired.
75 EXPECT_TRUE(WaitForClientShutdown());
79 // TODO(viettrungluu): Move to a separate IPCChannelWinTest.
81 TEST_F(IPCChannelTest
, ChannelTestExistingPipe
) {
82 Init("GenericClient");
84 // Create pipe manually using the standard Chromium name and set up IPC
86 IPC::TestChannelListener listener
;
87 std::string
name("\\\\.\\pipe\\chrome.");
88 name
.append(GetChannelName("GenericClient"));
89 HANDLE pipe
= CreateNamedPipeA(name
.c_str(),
90 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
|
91 FILE_FLAG_FIRST_PIPE_INSTANCE
,
92 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
,
98 CreateChannelFromChannelHandle(IPC::ChannelHandle(pipe
), &listener
);
99 CloseHandle(pipe
); // The channel duplicates the handle.
100 listener
.Init(sender());
102 // Connect to channel and start client.
103 ASSERT_TRUE(ConnectChannel());
104 ASSERT_TRUE(StartClient());
106 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
109 base::MessageLoop::current()->Run();
111 // Close the channel so the client's OnChannelError() gets fired.
114 EXPECT_TRUE(WaitForClientShutdown());
117 #endif // defined (OS_WIN)
119 TEST_F(IPCChannelTest
, ChannelProxyTest
) {
120 Init("GenericClient");
122 base::Thread
thread("ChannelProxyTestServer");
123 base::Thread::Options options
;
124 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
125 thread
.StartWithOptions(options
);
127 // Set up IPC channel proxy.
128 IPC::TestChannelListener listener
;
129 CreateChannelProxy(&listener
, thread
.message_loop_proxy().get());
130 listener
.Init(sender());
132 ASSERT_TRUE(StartClient());
134 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
137 base::MessageLoop::current()->Run();
139 EXPECT_TRUE(WaitForClientShutdown());
141 // Destroy the channel proxy before shutting down the thread.
142 DestroyChannelProxy();
146 class ChannelListenerWithOnConnectedSend
: public IPC::TestChannelListener
{
148 ChannelListenerWithOnConnectedSend() {}
149 virtual ~ChannelListenerWithOnConnectedSend() {}
151 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
{
157 // Acting flakey in Windows. http://crbug.com/129595
158 #define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
160 #define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
162 // This tests the case of a listener sending back an event in its
163 // OnChannelConnected handler.
164 TEST_F(IPCChannelTest
, MAYBE_SendMessageInChannelConnected
) {
165 Init("GenericClient");
167 // Set up IPC channel and start client.
168 ChannelListenerWithOnConnectedSend listener
;
169 CreateChannel(&listener
);
170 listener
.Init(sender());
171 ASSERT_TRUE(ConnectChannel());
172 ASSERT_TRUE(StartClient());
174 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
177 base::MessageLoop::current()->Run();
179 // Close the channel so the client's OnChannelError() gets fired.
182 EXPECT_TRUE(WaitForClientShutdown());
186 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient
) {
187 base::MessageLoopForIO main_message_loop
;
188 IPC::TestChannelListener listener
;
190 // Set up IPC channel.
191 scoped_ptr
<IPC::Channel
> channel(IPC::Channel::CreateClient(
192 IPCTestBase::GetChannelName("GenericClient"),
194 CHECK(channel
->Connect());
195 listener
.Init(channel
.get());
196 IPC::TestChannelListener::SendOneMessage(channel
.get(), "hello from child");
198 base::MessageLoop::current()->Run();