Update .zip name in remoting_client.gypi to fix archive bots after crrev.com/321815.
[chromium-blink-merge.git] / ipc / ipc_channel_unittest.cc
blob19cfad862ddae3e31be0d091e0c19d2abb903684
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/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"
22 namespace {
24 class IPCChannelTest : public IPCTestBase {
27 // TODO(viettrungluu): Move to a separate IPCMessageTest.
28 TEST_F(IPCChannelTest, BasicMessageTest) {
29 int v1 = 10;
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);
40 int vi;
41 std::string vs;
42 base::string16 vs16;
44 EXPECT_TRUE(iter.ReadInt(&vi));
45 EXPECT_EQ(v1, vi);
47 EXPECT_TRUE(iter.ReadString(&vs));
48 EXPECT_EQ(v2, vs);
50 EXPECT_TRUE(iter.ReadString16(&vs16));
51 EXPECT_EQ(v3, vs16);
53 // should fail
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");
71 // Run message loop.
72 base::MessageLoop::current()->Run();
74 // Close the channel so the client's OnChannelError() gets fired.
75 channel()->Close();
77 EXPECT_TRUE(WaitForClientShutdown());
78 DestroyChannel();
81 // TODO(viettrungluu): Move to a separate IPCChannelWinTest.
82 #if defined(OS_WIN)
83 TEST_F(IPCChannelTest, ChannelTestExistingPipe) {
84 Init("GenericClient");
86 // Create pipe manually using the standard Chromium name and set up IPC
87 // channel.
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,
96 4096,
97 4096,
98 5000,
99 NULL);
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");
110 // Run message loop.
111 base::MessageLoop::current()->Run();
113 // Close the channel so the client's OnChannelError() gets fired.
114 channel()->Close();
116 EXPECT_TRUE(WaitForClientShutdown());
117 DestroyChannel();
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");
138 // Run message loop.
139 base::MessageLoop::current()->Run();
141 EXPECT_TRUE(WaitForClientShutdown());
143 // Destroy the channel proxy before shutting down the thread.
144 DestroyChannelProxy();
145 thread.Stop();
148 class ChannelListenerWithOnConnectedSend : public IPC::TestChannelListener {
149 public:
150 ChannelListenerWithOnConnectedSend() {}
151 ~ChannelListenerWithOnConnectedSend() override {}
153 void OnChannelConnected(int32 peer_pid) override {
154 SendNextMessage();
158 #if defined(OS_WIN)
159 // Acting flakey in Windows. http://crbug.com/129595
160 #define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
161 #else
162 #define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
163 #endif
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");
178 // Run message loop.
179 base::MessageLoop::current()->Run();
181 // Close the channel so the client's OnChannelError() gets fired.
182 channel()->Close();
184 EXPECT_TRUE(WaitForClientShutdown());
185 DestroyChannel();
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"),
195 &listener));
196 CHECK(channel->Connect());
197 listener.Init(channel.get());
198 IPC::TestChannelListener::SendOneMessage(channel.get(), "hello from child");
200 base::MessageLoop::current()->Run();
201 return 0;
204 } // namespace