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