[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "build/build_config.h" |
| 6 | |
| 7 | #if defined(OS_WIN) |
| 8 | #include <windows.h> |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 9 | #endif |
| 10 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 11 | #include <stdint.h> |
| 12 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 13 | #include <memory> |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 14 | #include <string> |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 15 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 16 | #include "base/pickle.h" |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 17 | #include "base/run_loop.h" |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 18 | #include "base/strings/string16.h" |
| 19 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 20 | #include "base/threading/thread.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 21 | #include "ipc/ipc_test_base.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 22 | #include "ipc/ipc_test_channel_listener.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 23 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 24 | namespace { |
| 25 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 26 | class IPCChannelTest : public IPCTestBase { |
| 27 | }; |
| 28 | |
brettw | a59908d2 | 2016-01-21 19:16:48 | [diff] [blame] | 29 | TEST_F(IPCChannelTest, ChannelTest) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 30 | Init("GenericClient"); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 31 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 32 | // Set up IPC channel and start client. |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 33 | IPC::TestChannelListener listener; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 34 | CreateChannel(&listener); |
| 35 | listener.Init(sender()); |
| 36 | ASSERT_TRUE(ConnectChannel()); |
| 37 | ASSERT_TRUE(StartClient()); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 38 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 39 | IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent"); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 40 | |
| 41 | // Run message loop. |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 42 | base::RunLoop().Run(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 43 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 44 | // Close the channel so the client's OnChannelError() gets fired. |
| 45 | channel()->Close(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 46 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 47 | EXPECT_TRUE(WaitForClientShutdown()); |
| 48 | DestroyChannel(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 51 | // TODO(viettrungluu): Move to a separate IPCChannelWinTest. |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 52 | #if defined(OS_WIN) |
| 53 | TEST_F(IPCChannelTest, ChannelTestExistingPipe) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 54 | Init("GenericClient"); |
| 55 | |
| 56 | // Create pipe manually using the standard Chromium name and set up IPC |
| 57 | // channel. |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 58 | IPC::TestChannelListener listener; |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 59 | std::string name("\\\\.\\pipe\\chrome."); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 60 | name.append(GetChannelName("GenericClient")); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 61 | HANDLE pipe = CreateNamedPipeA(name.c_str(), |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 62 | PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | |
| 63 | FILE_FLAG_FIRST_PIPE_INSTANCE, |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 64 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
| 65 | 1, |
| 66 | 4096, |
| 67 | 4096, |
| 68 | 5000, |
| 69 | NULL); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 70 | CreateChannelFromChannelHandle(IPC::ChannelHandle(pipe), &listener); |
| 71 | CloseHandle(pipe); // The channel duplicates the handle. |
| 72 | listener.Init(sender()); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 73 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 74 | // Connect to channel and start client. |
| 75 | ASSERT_TRUE(ConnectChannel()); |
| 76 | ASSERT_TRUE(StartClient()); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 77 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 78 | IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent"); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 79 | |
| 80 | // Run message loop. |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 81 | base::MessageLoop::current()->Run(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 82 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 83 | // Close the channel so the client's OnChannelError() gets fired. |
| 84 | channel()->Close(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 85 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 86 | EXPECT_TRUE(WaitForClientShutdown()); |
| 87 | DestroyChannel(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 88 | } |
| 89 | #endif // defined (OS_WIN) |
| 90 | |
brettw | a59908d2 | 2016-01-21 19:16:48 | [diff] [blame] | 91 | TEST_F(IPCChannelTest, ChannelProxyTest) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 92 | Init("GenericClient"); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 93 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 94 | base::Thread thread("ChannelProxyTestServer"); |
| 95 | base::Thread::Options options; |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 96 | options.message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 97 | thread.StartWithOptions(options); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 98 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 99 | // Set up IPC channel proxy. |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 100 | IPC::TestChannelListener listener; |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 101 | CreateChannelProxy(&listener, thread.task_runner().get()); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 102 | listener.Init(sender()); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 103 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 104 | ASSERT_TRUE(StartClient()); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 105 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 106 | IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent"); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 107 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 108 | // Run message loop. |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 109 | base::RunLoop().Run(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 110 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 111 | EXPECT_TRUE(WaitForClientShutdown()); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 112 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 113 | // Destroy the channel proxy before shutting down the thread. |
| 114 | DestroyChannelProxy(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 115 | thread.Stop(); |
| 116 | } |
| 117 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 118 | class ChannelListenerWithOnConnectedSend : public IPC::TestChannelListener { |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 119 | public: |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 120 | ChannelListenerWithOnConnectedSend() {} |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 121 | ~ChannelListenerWithOnConnectedSend() override {} |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 122 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 123 | void OnChannelConnected(int32_t peer_pid) override { |
dcheng | f3076af | 2014-10-21 18:02:42 | [diff] [blame] | 124 | SendNextMessage(); |
| 125 | } |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 126 | }; |
| 127 | |
brettw | a59908d2 | 2016-01-21 19:16:48 | [diff] [blame] | 128 | #if defined(OS_WIN) |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 129 | // 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] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 134 | // This tests the case of a listener sending back an event in its |
| 135 | // OnChannelConnected handler. |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 136 | TEST_F(IPCChannelTest, MAYBE_SendMessageInChannelConnected) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 137 | Init("GenericClient"); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 138 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 139 | // 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] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 145 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 146 | IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent"); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 147 | |
| 148 | // Run message loop. |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 149 | base::RunLoop().Run(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 150 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 151 | // Close the channel so the client's OnChannelError() gets fired. |
| 152 | channel()->Close(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 153 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 154 | EXPECT_TRUE(WaitForClientShutdown()); |
| 155 | DestroyChannel(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 158 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient) { |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 159 | base::MessageLoopForIO main_message_loop; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 160 | IPC::TestChannelListener listener; |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 161 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 162 | // Set up IPC channel. |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 163 | std::unique_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 164 | IPCTestBase::GetChannelName("GenericClient"), &listener)); |
[email protected] | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 165 | CHECK(channel->Connect()); |
| 166 | listener.Init(channel.get()); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 167 | IPC::TestChannelListener::SendOneMessage(channel.get(), "hello from child"); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 168 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 169 | base::RunLoop().Run(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 170 | return 0; |
| 171 | } |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 172 | |
| 173 | } // namespace |