blob: f5ee2061006427f30cd8cc0283e2277f0a30aa2a [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
avi246998d82015-12-22 02:39:0411#include <stdint.h>
12
danakj03de39b22016-04-23 04:21:0913#include <memory>
[email protected]0cb7d8c82013-01-11 15:13:3714#include <string>
[email protected]0cb7d8c82013-01-11 15:13:3715
[email protected]0cb7d8c82013-01-11 15:13:3716#include "base/pickle.h"
fdoray8e32586852016-06-22 19:56:1617#include "base/run_loop.h"
thestigf84f17f2015-03-11 20:41:5518#include "base/strings/string16.h"
19#include "base/strings/utf_string_conversions.h"
[email protected]0cb7d8c82013-01-11 15:13:3720#include "base/threading/thread.h"
[email protected]0cb7d8c82013-01-11 15:13:3721#include "ipc/ipc_test_base.h"
[email protected]64860882014-08-04 23:44:1722#include "ipc/ipc_test_channel_listener.h"
[email protected]0cb7d8c82013-01-11 15:13:3723
[email protected]2a3aa7b52013-01-11 20:56:2224namespace {
25
[email protected]0cb7d8c82013-01-11 15:13:3726class IPCChannelTest : public IPCTestBase {
27};
28
brettwa59908d22016-01-21 19:16:4829TEST_F(IPCChannelTest, ChannelTest) {
[email protected]3c788582013-01-25 21:51:3530 Init("GenericClient");
[email protected]0cb7d8c82013-01-11 15:13:3731
[email protected]3c788582013-01-25 21:51:3532 // Set up IPC channel and start client.
[email protected]64860882014-08-04 23:44:1733 IPC::TestChannelListener listener;
[email protected]3c788582013-01-25 21:51:3534 CreateChannel(&listener);
35 listener.Init(sender());
36 ASSERT_TRUE(ConnectChannel());
37 ASSERT_TRUE(StartClient());
[email protected]0cb7d8c82013-01-11 15:13:3738
[email protected]64860882014-08-04 23:44:1739 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:3740
41 // Run message loop.
fdoray8e32586852016-06-22 19:56:1642 base::RunLoop().Run();
[email protected]0cb7d8c82013-01-11 15:13:3743
[email protected]3c788582013-01-25 21:51:3544 // Close the channel so the client's OnChannelError() gets fired.
45 channel()->Close();
[email protected]0cb7d8c82013-01-11 15:13:3746
[email protected]3c788582013-01-25 21:51:3547 EXPECT_TRUE(WaitForClientShutdown());
48 DestroyChannel();
[email protected]0cb7d8c82013-01-11 15:13:3749}
50
[email protected]3c788582013-01-25 21:51:3551// TODO(viettrungluu): Move to a separate IPCChannelWinTest.
[email protected]0cb7d8c82013-01-11 15:13:3752#if defined(OS_WIN)
53TEST_F(IPCChannelTest, ChannelTestExistingPipe) {
[email protected]3c788582013-01-25 21:51:3554 Init("GenericClient");
55
56 // Create pipe manually using the standard Chromium name and set up IPC
57 // channel.
[email protected]64860882014-08-04 23:44:1758 IPC::TestChannelListener listener;
[email protected]0cb7d8c82013-01-11 15:13:3759 std::string name("\\\\.\\pipe\\chrome.");
[email protected]3c788582013-01-25 21:51:3560 name.append(GetChannelName("GenericClient"));
[email protected]0cb7d8c82013-01-11 15:13:3761 HANDLE pipe = CreateNamedPipeA(name.c_str(),
[email protected]3c788582013-01-25 21:51:3562 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
63 FILE_FLAG_FIRST_PIPE_INSTANCE,
[email protected]0cb7d8c82013-01-11 15:13:3764 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
65 1,
66 4096,
67 4096,
68 5000,
69 NULL);
[email protected]3c788582013-01-25 21:51:3570 CreateChannelFromChannelHandle(IPC::ChannelHandle(pipe), &listener);
71 CloseHandle(pipe); // The channel duplicates the handle.
72 listener.Init(sender());
[email protected]0cb7d8c82013-01-11 15:13:3773
[email protected]3c788582013-01-25 21:51:3574 // Connect to channel and start client.
75 ASSERT_TRUE(ConnectChannel());
76 ASSERT_TRUE(StartClient());
[email protected]0cb7d8c82013-01-11 15:13:3777
[email protected]64860882014-08-04 23:44:1778 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:3779
80 // Run message loop.
[email protected]fd0a773a2013-04-30 20:55:0381 base::MessageLoop::current()->Run();
[email protected]0cb7d8c82013-01-11 15:13:3782
[email protected]3c788582013-01-25 21:51:3583 // Close the channel so the client's OnChannelError() gets fired.
84 channel()->Close();
[email protected]0cb7d8c82013-01-11 15:13:3785
[email protected]3c788582013-01-25 21:51:3586 EXPECT_TRUE(WaitForClientShutdown());
87 DestroyChannel();
[email protected]0cb7d8c82013-01-11 15:13:3788}
89#endif // defined (OS_WIN)
90
brettwa59908d22016-01-21 19:16:4891TEST_F(IPCChannelTest, ChannelProxyTest) {
[email protected]3c788582013-01-25 21:51:3592 Init("GenericClient");
[email protected]0cb7d8c82013-01-11 15:13:3793
[email protected]0cb7d8c82013-01-11 15:13:3794 base::Thread thread("ChannelProxyTestServer");
95 base::Thread::Options options;
[email protected]fd0a773a2013-04-30 20:55:0396 options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]0cb7d8c82013-01-11 15:13:3797 thread.StartWithOptions(options);
[email protected]0cb7d8c82013-01-11 15:13:3798
[email protected]3c788582013-01-25 21:51:3599 // Set up IPC channel proxy.
[email protected]64860882014-08-04 23:44:17100 IPC::TestChannelListener listener;
skyostile687bdff2015-05-12 11:29:21101 CreateChannelProxy(&listener, thread.task_runner().get());
[email protected]3c788582013-01-25 21:51:35102 listener.Init(sender());
[email protected]0cb7d8c82013-01-11 15:13:37103
[email protected]3c788582013-01-25 21:51:35104 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
[email protected]3c788582013-01-25 21:51:35108 // Run message loop.
fdoray8e32586852016-06-22 19:56:16109 base::RunLoop().Run();
[email protected]0cb7d8c82013-01-11 15:13:37110
[email protected]3c788582013-01-25 21:51:35111 EXPECT_TRUE(WaitForClientShutdown());
[email protected]0cb7d8c82013-01-11 15:13:37112
[email protected]3c788582013-01-25 21:51:35113 // Destroy the channel proxy before shutting down the thread.
114 DestroyChannelProxy();
[email protected]0cb7d8c82013-01-11 15:13:37115 thread.Stop();
116}
117
[email protected]64860882014-08-04 23:44:17118class ChannelListenerWithOnConnectedSend : public IPC::TestChannelListener {
[email protected]0cb7d8c82013-01-11 15:13:37119 public:
[email protected]3c788582013-01-25 21:51:35120 ChannelListenerWithOnConnectedSend() {}
dchengfe61fca2014-10-22 02:29:52121 ~ChannelListenerWithOnConnectedSend() override {}
[email protected]3c788582013-01-25 21:51:35122
tfarina10a5c062015-09-04 18:47:57123 void OnChannelConnected(int32_t peer_pid) override {
dchengf3076af2014-10-21 18:02:42124 SendNextMessage();
125 }
[email protected]0cb7d8c82013-01-11 15:13:37126};
127
brettwa59908d22016-01-21 19:16:48128#if defined(OS_WIN)
[email protected]0cb7d8c82013-01-11 15:13:37129// Acting flakey in Windows. http://crbug.com/129595
130#define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
131#else
132#define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
133#endif
[email protected]3c788582013-01-25 21:51:35134// This tests the case of a listener sending back an event in its
135// OnChannelConnected handler.
[email protected]0cb7d8c82013-01-11 15:13:37136TEST_F(IPCChannelTest, MAYBE_SendMessageInChannelConnected) {
[email protected]3c788582013-01-25 21:51:35137 Init("GenericClient");
[email protected]0cb7d8c82013-01-11 15:13:37138
[email protected]3c788582013-01-25 21:51:35139 // Set up IPC channel and start client.
140 ChannelListenerWithOnConnectedSend listener;
141 CreateChannel(&listener);
142 listener.Init(sender());
143 ASSERT_TRUE(ConnectChannel());
144 ASSERT_TRUE(StartClient());
[email protected]0cb7d8c82013-01-11 15:13:37145
[email protected]64860882014-08-04 23:44:17146 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]0cb7d8c82013-01-11 15:13:37147
148 // Run message loop.
fdoray8e32586852016-06-22 19:56:16149 base::RunLoop().Run();
[email protected]0cb7d8c82013-01-11 15:13:37150
[email protected]3c788582013-01-25 21:51:35151 // Close the channel so the client's OnChannelError() gets fired.
152 channel()->Close();
[email protected]0cb7d8c82013-01-11 15:13:37153
[email protected]3c788582013-01-25 21:51:35154 EXPECT_TRUE(WaitForClientShutdown());
155 DestroyChannel();
[email protected]0cb7d8c82013-01-11 15:13:37156}
157
[email protected]3c788582013-01-25 21:51:35158MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient) {
[email protected]fd0a773a2013-04-30 20:55:03159 base::MessageLoopForIO main_message_loop;
[email protected]64860882014-08-04 23:44:17160 IPC::TestChannelListener listener;
[email protected]0cb7d8c82013-01-11 15:13:37161
[email protected]3c788582013-01-25 21:51:35162 // Set up IPC channel.
danakj03de39b22016-04-23 04:21:09163 std::unique_ptr<IPC::Channel> channel(IPC::Channel::CreateClient(
erikchen30dc2812015-09-24 03:26:38164 IPCTestBase::GetChannelName("GenericClient"), &listener));
[email protected]e482111a82014-05-30 03:58:59165 CHECK(channel->Connect());
166 listener.Init(channel.get());
[email protected]64860882014-08-04 23:44:17167 IPC::TestChannelListener::SendOneMessage(channel.get(), "hello from child");
[email protected]3c788582013-01-25 21:51:35168
fdoray8e32586852016-06-22 19:56:16169 base::RunLoop().Run();
[email protected]0cb7d8c82013-01-11 15:13:37170 return 0;
171}
[email protected]2a3aa7b52013-01-11 20:56:22172
173} // namespace