Automated Commit: Committing new LKGM version 7260.0.0 for chromeos.
[chromium-blink-merge.git] / ipc / ipc_channel_unittest.cc
blob2623154ba399af8459c2e02cbaa71a2c251de78d
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"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #endif
11 #include <string>
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"
21 namespace {
23 class IPCChannelTest : public IPCTestBase {
26 // TODO(viettrungluu): Move to a separate IPCMessageTest.
27 TEST_F(IPCChannelTest, BasicMessageTest) {
28 int v1 = 10;
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 base::PickleIterator iter(m);
39 int vi;
40 std::string vs;
41 base::string16 vs16;
43 EXPECT_TRUE(iter.ReadInt(&vi));
44 EXPECT_EQ(v1, vi);
46 EXPECT_TRUE(iter.ReadString(&vs));
47 EXPECT_EQ(v2, vs);
49 EXPECT_TRUE(iter.ReadString16(&vs16));
50 EXPECT_EQ(v3, vs16);
52 // should fail
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");
70 // Run message loop.
71 base::MessageLoop::current()->Run();
73 // Close the channel so the client's OnChannelError() gets fired.
74 channel()->Close();
76 EXPECT_TRUE(WaitForClientShutdown());
77 DestroyChannel();
80 // TODO(viettrungluu): Move to a separate IPCChannelWinTest.
81 #if defined(OS_WIN)
82 TEST_F(IPCChannelTest, ChannelTestExistingPipe) {
83 Init("GenericClient");
85 // Create pipe manually using the standard Chromium name and set up IPC
86 // channel.
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,
95 4096,
96 4096,
97 5000,
98 NULL);
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");
109 // Run message loop.
110 base::MessageLoop::current()->Run();
112 // Close the channel so the client's OnChannelError() gets fired.
113 channel()->Close();
115 EXPECT_TRUE(WaitForClientShutdown());
116 DestroyChannel();
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");
137 // Run message loop.
138 base::MessageLoop::current()->Run();
140 EXPECT_TRUE(WaitForClientShutdown());
142 // Destroy the channel proxy before shutting down the thread.
143 DestroyChannelProxy();
144 thread.Stop();
147 class ChannelListenerWithOnConnectedSend : public IPC::TestChannelListener {
148 public:
149 ChannelListenerWithOnConnectedSend() {}
150 ~ChannelListenerWithOnConnectedSend() override {}
152 void OnChannelConnected(int32 peer_pid) override {
153 SendNextMessage();
157 #if defined(OS_WIN)
158 // Acting flakey in Windows. http://crbug.com/129595
159 #define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
160 #else
161 #define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
162 #endif
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");
177 // Run message loop.
178 base::MessageLoop::current()->Run();
180 // Close the channel so the client's OnChannelError() gets fired.
181 channel()->Close();
183 EXPECT_TRUE(WaitForClientShutdown());
184 DestroyChannel();
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"), &listener, nullptr));
194 CHECK(channel->Connect());
195 listener.Init(channel.get());
196 IPC::TestChannelListener::SendOneMessage(channel.get(), "hello from child");
198 base::MessageLoop::current()->Run();
199 return 0;
202 } // namespace