blob: 0c444fff3ee20722fd5699c540ab04f08e7c3e67 [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]0cb7d8c82013-01-11 15:13:3713#include "base/pickle.h"
thestigf84f17f2015-03-11 20:41:5514#include "base/strings/string16.h"
15#include "base/strings/utf_string_conversions.h"
[email protected]0cb7d8c82013-01-11 15:13:3716#include "base/threading/thread.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
tfarina8514f0d2015-07-28 14:41:4725#if defined(OS_ANDROID)
26#define MAYBE_ChannelTest DISABLED_ChannelTest
27#else
28#define MAYBE_ChannelTest ChannelTest
29#endif
30TEST_F(IPCChannelTest, MAYBE_ChannelTest) {
[email protected]3c788582013-01-25 21:51:3531 Init("GenericClient");
[email protected]0cb7d8c82013-01-11 15:13:3732
[email protected]3c788582013-01-25 21:51:3533 // Set up IPC channel and start client.
[email protected]64860882014-08-04 23:44:1734 IPC::TestChannelListener listener;
[email protected]3c788582013-01-25 21:51:3535 CreateChannel(&listener);
36 listener.Init(sender());
37 ASSERT_TRUE(ConnectChannel());
38 ASSERT_TRUE(StartClient());
[email protected]0cb7d8c82013-01-11 15:13:3739
[email protected]64860882014-08-04 23:44:1740 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:3741
42 // Run message loop.
[email protected]fd0a773a2013-04-30 20:55:0343 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:3744
[email protected]3c788582013-01-25 21:51:3545 // Close the channel so the client's OnChannelError() gets fired.
46 channel()->Close();
[email protected]0cb7d8c82013-01-11 15:13:3747
[email protected]3c788582013-01-25 21:51:3548 EXPECT_TRUE(WaitForClientShutdown());
49 DestroyChannel();
[email protected]0cb7d8c82013-01-11 15:13:3750}
51
[email protected]3c788582013-01-25 21:51:3552// TODO(viettrungluu): Move to a separate IPCChannelWinTest.
[email protected]0cb7d8c82013-01-11 15:13:3753#if defined(OS_WIN)
54TEST_F(IPCChannelTest, ChannelTestExistingPipe) {
[email protected]3c788582013-01-25 21:51:3555 Init("GenericClient");
56
57 // Create pipe manually using the standard Chromium name and set up IPC
58 // channel.
[email protected]64860882014-08-04 23:44:1759 IPC::TestChannelListener listener;
[email protected]0cb7d8c82013-01-11 15:13:3760 std::string name("\\\\.\\pipe\\chrome.");
[email protected]3c788582013-01-25 21:51:3561 name.append(GetChannelName("GenericClient"));
[email protected]0cb7d8c82013-01-11 15:13:3762 HANDLE pipe = CreateNamedPipeA(name.c_str(),
[email protected]3c788582013-01-25 21:51:3563 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
64 FILE_FLAG_FIRST_PIPE_INSTANCE,
[email protected]0cb7d8c82013-01-11 15:13:3765 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
66 1,
67 4096,
68 4096,
69 5000,
70 NULL);
[email protected]3c788582013-01-25 21:51:3571 CreateChannelFromChannelHandle(IPC::ChannelHandle(pipe), &listener);
72 CloseHandle(pipe); // The channel duplicates the handle.
73 listener.Init(sender());
[email protected]0cb7d8c82013-01-11 15:13:3774
[email protected]3c788582013-01-25 21:51:3575 // Connect to channel and start client.
76 ASSERT_TRUE(ConnectChannel());
77 ASSERT_TRUE(StartClient());
[email protected]0cb7d8c82013-01-11 15:13:3778
[email protected]64860882014-08-04 23:44:1779 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:3780
81 // Run message loop.
[email protected]fd0a773a2013-04-30 20:55:0382 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:3783
[email protected]3c788582013-01-25 21:51:3584 // Close the channel so the client's OnChannelError() gets fired.
85 channel()->Close();
[email protected]0cb7d8c82013-01-11 15:13:3786
[email protected]3c788582013-01-25 21:51:3587 EXPECT_TRUE(WaitForClientShutdown());
88 DestroyChannel();
[email protected]0cb7d8c82013-01-11 15:13:3789}
90#endif // defined (OS_WIN)
91
tfarina8514f0d2015-07-28 14:41:4792#if defined(OS_ANDROID)
93#define MAYBE_ChannelProxyTest DISABLED_ChannelProxyTest
94#else
95#define MAYBE_ChannelProxyTest ChannelProxyTest
96#endif
97TEST_F(IPCChannelTest, MAYBE_ChannelProxyTest) {
[email protected]3c788582013-01-25 21:51:3598 Init("GenericClient");
[email protected]0cb7d8c82013-01-11 15:13:3799
[email protected]0cb7d8c82013-01-11 15:13:37100 base::Thread thread("ChannelProxyTestServer");
101 base::Thread::Options options;
[email protected]fd0a773a2013-04-30 20:55:03102 options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]0cb7d8c82013-01-11 15:13:37103 thread.StartWithOptions(options);
[email protected]0cb7d8c82013-01-11 15:13:37104
[email protected]3c788582013-01-25 21:51:35105 // Set up IPC channel proxy.
[email protected]64860882014-08-04 23:44:17106 IPC::TestChannelListener listener;
skyostile687bdff2015-05-12 11:29:21107 CreateChannelProxy(&listener, thread.task_runner().get());
[email protected]3c788582013-01-25 21:51:35108 listener.Init(sender());
[email protected]0cb7d8c82013-01-11 15:13:37109
[email protected]3c788582013-01-25 21:51:35110 ASSERT_TRUE(StartClient());
[email protected]0cb7d8c82013-01-11 15:13:37111
[email protected]64860882014-08-04 23:44:17112 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:37113
[email protected]3c788582013-01-25 21:51:35114 // Run message loop.
[email protected]fd0a773a2013-04-30 20:55:03115 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:37116
[email protected]3c788582013-01-25 21:51:35117 EXPECT_TRUE(WaitForClientShutdown());
[email protected]0cb7d8c82013-01-11 15:13:37118
[email protected]3c788582013-01-25 21:51:35119 // Destroy the channel proxy before shutting down the thread.
120 DestroyChannelProxy();
[email protected]0cb7d8c82013-01-11 15:13:37121 thread.Stop();
122}
123
[email protected]64860882014-08-04 23:44:17124class ChannelListenerWithOnConnectedSend : public IPC::TestChannelListener {
[email protected]0cb7d8c82013-01-11 15:13:37125 public:
[email protected]3c788582013-01-25 21:51:35126 ChannelListenerWithOnConnectedSend() {}
dchengfe61fca2014-10-22 02:29:52127 ~ChannelListenerWithOnConnectedSend() override {}
[email protected]3c788582013-01-25 21:51:35128
tfarina10a5c062015-09-04 18:47:57129 void OnChannelConnected(int32_t peer_pid) override {
dchengf3076af2014-10-21 18:02:42130 SendNextMessage();
131 }
[email protected]0cb7d8c82013-01-11 15:13:37132};
133
tfarina8514f0d2015-07-28 14:41:47134#if defined(OS_WIN) || defined(OS_ANDROID)
[email protected]0cb7d8c82013-01-11 15:13:37135// Acting flakey in Windows. http://crbug.com/129595
136#define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
137#else
138#define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
139#endif
[email protected]3c788582013-01-25 21:51:35140// This tests the case of a listener sending back an event in its
141// OnChannelConnected handler.
[email protected]0cb7d8c82013-01-11 15:13:37142TEST_F(IPCChannelTest, MAYBE_SendMessageInChannelConnected) {
[email protected]3c788582013-01-25 21:51:35143 Init("GenericClient");
[email protected]0cb7d8c82013-01-11 15:13:37144
[email protected]3c788582013-01-25 21:51:35145 // Set up IPC channel and start client.
146 ChannelListenerWithOnConnectedSend listener;
147 CreateChannel(&listener);
148 listener.Init(sender());
149 ASSERT_TRUE(ConnectChannel());
150 ASSERT_TRUE(StartClient());
[email protected]0cb7d8c82013-01-11 15:13:37151
[email protected]64860882014-08-04 23:44:17152 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:37153
154 // Run message loop.
[email protected]fd0a773a2013-04-30 20:55:03155 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:37156
[email protected]3c788582013-01-25 21:51:35157 // Close the channel so the client's OnChannelError() gets fired.
158 channel()->Close();
[email protected]0cb7d8c82013-01-11 15:13:37159
[email protected]3c788582013-01-25 21:51:35160 EXPECT_TRUE(WaitForClientShutdown());
161 DestroyChannel();
[email protected]0cb7d8c82013-01-11 15:13:37162}
163
[email protected]3c788582013-01-25 21:51:35164MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient) {
[email protected]fd0a773a2013-04-30 20:55:03165 base::MessageLoopForIO main_message_loop;
[email protected]64860882014-08-04 23:44:17166 IPC::TestChannelListener listener;
[email protected]0cb7d8c82013-01-11 15:13:37167
[email protected]3c788582013-01-25 21:51:35168 // Set up IPC channel.
[email protected]e482111a82014-05-30 03:58:59169 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient(
erikchen30dc2812015-09-24 03:26:38170 IPCTestBase::GetChannelName("GenericClient"), &listener));
[email protected]e482111a82014-05-30 03:58:59171 CHECK(channel->Connect());
172 listener.Init(channel.get());
[email protected]64860882014-08-04 23:44:17173 IPC::TestChannelListener::SendOneMessage(channel.get(), "hello from child");
[email protected]3c788582013-01-25 21:51:35174
[email protected]fd0a773a2013-04-30 20:55:03175 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:37176 return 0;
177}
[email protected]2a3aa7b52013-01-11 20:56:22178
179} // namespace