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