blob: dd31e9e9c474e20457dff9c2375eae0440ec9d35 [file] [log] [blame]
[email protected]0cb7d8c82013-01-11 15:13:371// 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.
4
5#include "build/build_config.h"
6
7#if defined(OS_WIN)
8#include <windows.h>
[email protected]0cb7d8c82013-01-11 15:13:379#endif
10
[email protected]0cb7d8c82013-01-11 15:13:3711#include <string>
[email protected]0cb7d8c82013-01-11 15:13:3712
[email protected]2a9ec0e2013-07-17 23:00:3013#include "base/message_loop/message_loop.h"
[email protected]0cb7d8c82013-01-11 15:13:3714#include "base/pickle.h"
[email protected]0cb7d8c82013-01-11 15:13:3715#include "base/threading/thread.h"
[email protected]3c788582013-01-25 21:51:3516#include "ipc/ipc_message.h"
[email protected]0cb7d8c82013-01-11 15:13:3717#include "ipc/ipc_test_base.h"
[email protected]64860882014-08-04 23:44:1718#include "ipc/ipc_test_channel_listener.h"
[email protected]0cb7d8c82013-01-11 15:13:3719
[email protected]2a3aa7b52013-01-11 20:56:2220namespace {
21
[email protected]0cb7d8c82013-01-11 15:13:3722class IPCChannelTest : public IPCTestBase {
23};
24
[email protected]3c788582013-01-25 21:51:3525// TODO(viettrungluu): Move to a separate IPCMessageTest.
[email protected]0cb7d8c82013-01-11 15:13:3726TEST_F(IPCChannelTest, BasicMessageTest) {
27 int v1 = 10;
28 std::string v2("foobar");
29 std::wstring v3(L"hello world");
30
[email protected]753bb252013-11-04 22:28:1231 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
[email protected]0cb7d8c82013-01-11 15:13:3732 EXPECT_TRUE(m.WriteInt(v1));
33 EXPECT_TRUE(m.WriteString(v2));
34 EXPECT_TRUE(m.WriteWString(v3));
35
36 PickleIterator iter(m);
37
38 int vi;
39 std::string vs;
40 std::wstring vw;
41
42 EXPECT_TRUE(m.ReadInt(&iter, &vi));
43 EXPECT_EQ(v1, vi);
44
45 EXPECT_TRUE(m.ReadString(&iter, &vs));
46 EXPECT_EQ(v2, vs);
47
48 EXPECT_TRUE(m.ReadWString(&iter, &vw));
49 EXPECT_EQ(v3, vw);
50
51 // should fail
52 EXPECT_FALSE(m.ReadInt(&iter, &vi));
53 EXPECT_FALSE(m.ReadString(&iter, &vs));
54 EXPECT_FALSE(m.ReadWString(&iter, &vw));
55}
56
[email protected]0cb7d8c82013-01-11 15:13:3757TEST_F(IPCChannelTest, ChannelTest) {
[email protected]3c788582013-01-25 21:51:3558 Init("GenericClient");
[email protected]0cb7d8c82013-01-11 15:13:3759
[email protected]3c788582013-01-25 21:51:3560 // Set up IPC channel and start client.
[email protected]64860882014-08-04 23:44:1761 IPC::TestChannelListener listener;
[email protected]3c788582013-01-25 21:51:3562 CreateChannel(&listener);
63 listener.Init(sender());
64 ASSERT_TRUE(ConnectChannel());
65 ASSERT_TRUE(StartClient());
[email protected]0cb7d8c82013-01-11 15:13:3766
[email protected]64860882014-08-04 23:44:1767 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:3768
69 // Run message loop.
[email protected]fd0a773a2013-04-30 20:55:0370 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:3771
[email protected]3c788582013-01-25 21:51:3572 // Close the channel so the client's OnChannelError() gets fired.
73 channel()->Close();
[email protected]0cb7d8c82013-01-11 15:13:3774
[email protected]3c788582013-01-25 21:51:3575 EXPECT_TRUE(WaitForClientShutdown());
76 DestroyChannel();
[email protected]0cb7d8c82013-01-11 15:13:3777}
78
[email protected]3c788582013-01-25 21:51:3579// TODO(viettrungluu): Move to a separate IPCChannelWinTest.
[email protected]0cb7d8c82013-01-11 15:13:3780#if defined(OS_WIN)
81TEST_F(IPCChannelTest, ChannelTestExistingPipe) {
[email protected]3c788582013-01-25 21:51:3582 Init("GenericClient");
83
84 // Create pipe manually using the standard Chromium name and set up IPC
85 // channel.
[email protected]64860882014-08-04 23:44:1786 IPC::TestChannelListener listener;
[email protected]0cb7d8c82013-01-11 15:13:3787 std::string name("\\\\.\\pipe\\chrome.");
[email protected]3c788582013-01-25 21:51:3588 name.append(GetChannelName("GenericClient"));
[email protected]0cb7d8c82013-01-11 15:13:3789 HANDLE pipe = CreateNamedPipeA(name.c_str(),
[email protected]3c788582013-01-25 21:51:3590 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
91 FILE_FLAG_FIRST_PIPE_INSTANCE,
[email protected]0cb7d8c82013-01-11 15:13:3792 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
93 1,
94 4096,
95 4096,
96 5000,
97 NULL);
[email protected]3c788582013-01-25 21:51:3598 CreateChannelFromChannelHandle(IPC::ChannelHandle(pipe), &listener);
99 CloseHandle(pipe); // The channel duplicates the handle.
100 listener.Init(sender());
[email protected]0cb7d8c82013-01-11 15:13:37101
[email protected]3c788582013-01-25 21:51:35102 // Connect to channel and start client.
103 ASSERT_TRUE(ConnectChannel());
104 ASSERT_TRUE(StartClient());
[email protected]0cb7d8c82013-01-11 15:13:37105
[email protected]64860882014-08-04 23:44:17106 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:37107
108 // Run message loop.
[email protected]fd0a773a2013-04-30 20:55:03109 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:37110
[email protected]3c788582013-01-25 21:51:35111 // Close the channel so the client's OnChannelError() gets fired.
112 channel()->Close();
[email protected]0cb7d8c82013-01-11 15:13:37113
[email protected]3c788582013-01-25 21:51:35114 EXPECT_TRUE(WaitForClientShutdown());
115 DestroyChannel();
[email protected]0cb7d8c82013-01-11 15:13:37116}
117#endif // defined (OS_WIN)
118
119TEST_F(IPCChannelTest, ChannelProxyTest) {
[email protected]3c788582013-01-25 21:51:35120 Init("GenericClient");
[email protected]0cb7d8c82013-01-11 15:13:37121
[email protected]0cb7d8c82013-01-11 15:13:37122 base::Thread thread("ChannelProxyTestServer");
123 base::Thread::Options options;
[email protected]fd0a773a2013-04-30 20:55:03124 options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]0cb7d8c82013-01-11 15:13:37125 thread.StartWithOptions(options);
[email protected]0cb7d8c82013-01-11 15:13:37126
[email protected]3c788582013-01-25 21:51:35127 // Set up IPC channel proxy.
[email protected]64860882014-08-04 23:44:17128 IPC::TestChannelListener listener;
[email protected]cadac622013-06-11 16:46:36129 CreateChannelProxy(&listener, thread.message_loop_proxy().get());
[email protected]3c788582013-01-25 21:51:35130 listener.Init(sender());
[email protected]0cb7d8c82013-01-11 15:13:37131
[email protected]3c788582013-01-25 21:51:35132 ASSERT_TRUE(StartClient());
[email protected]0cb7d8c82013-01-11 15:13:37133
[email protected]64860882014-08-04 23:44:17134 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:37135
[email protected]3c788582013-01-25 21:51:35136 // Run message loop.
[email protected]fd0a773a2013-04-30 20:55:03137 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:37138
[email protected]3c788582013-01-25 21:51:35139 EXPECT_TRUE(WaitForClientShutdown());
[email protected]0cb7d8c82013-01-11 15:13:37140
[email protected]3c788582013-01-25 21:51:35141 // Destroy the channel proxy before shutting down the thread.
142 DestroyChannelProxy();
[email protected]0cb7d8c82013-01-11 15:13:37143 thread.Stop();
144}
145
[email protected]64860882014-08-04 23:44:17146class ChannelListenerWithOnConnectedSend : public IPC::TestChannelListener {
[email protected]0cb7d8c82013-01-11 15:13:37147 public:
[email protected]3c788582013-01-25 21:51:35148 ChannelListenerWithOnConnectedSend() {}
dchengfe61fca2014-10-22 02:29:52149 ~ChannelListenerWithOnConnectedSend() override {}
[email protected]3c788582013-01-25 21:51:35150
dchengfe61fca2014-10-22 02:29:52151 void OnChannelConnected(int32 peer_pid) override {
dchengf3076af2014-10-21 18:02:42152 SendNextMessage();
153 }
[email protected]0cb7d8c82013-01-11 15:13:37154};
155
156#if defined(OS_WIN)
157// Acting flakey in Windows. http://crbug.com/129595
158#define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
159#else
160#define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
161#endif
[email protected]3c788582013-01-25 21:51:35162// This tests the case of a listener sending back an event in its
163// OnChannelConnected handler.
[email protected]0cb7d8c82013-01-11 15:13:37164TEST_F(IPCChannelTest, MAYBE_SendMessageInChannelConnected) {
[email protected]3c788582013-01-25 21:51:35165 Init("GenericClient");
[email protected]0cb7d8c82013-01-11 15:13:37166
[email protected]3c788582013-01-25 21:51:35167 // 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());
[email protected]0cb7d8c82013-01-11 15:13:37173
[email protected]64860882014-08-04 23:44:17174 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:37175
176 // Run message loop.
[email protected]fd0a773a2013-04-30 20:55:03177 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:37178
[email protected]3c788582013-01-25 21:51:35179 // Close the channel so the client's OnChannelError() gets fired.
180 channel()->Close();
[email protected]0cb7d8c82013-01-11 15:13:37181
[email protected]3c788582013-01-25 21:51:35182 EXPECT_TRUE(WaitForClientShutdown());
183 DestroyChannel();
[email protected]0cb7d8c82013-01-11 15:13:37184}
185
[email protected]3c788582013-01-25 21:51:35186MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient) {
[email protected]fd0a773a2013-04-30 20:55:03187 base::MessageLoopForIO main_message_loop;
[email protected]64860882014-08-04 23:44:17188 IPC::TestChannelListener listener;
[email protected]0cb7d8c82013-01-11 15:13:37189
[email protected]3c788582013-01-25 21:51:35190 // Set up IPC channel.
[email protected]e482111a82014-05-30 03:58:59191 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient(
192 IPCTestBase::GetChannelName("GenericClient"),
193 &listener));
194 CHECK(channel->Connect());
195 listener.Init(channel.get());
[email protected]64860882014-08-04 23:44:17196 IPC::TestChannelListener::SendOneMessage(channel.get(), "hello from child");
[email protected]3c788582013-01-25 21:51:35197
[email protected]fd0a773a2013-04-30 20:55:03198 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:37199 return 0;
200}
[email protected]2a3aa7b52013-01-11 20:56:22201
202} // namespace