OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/sync_socket.h" | 5 #include "base/sync_socket.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string> | 8 #include <string> |
9 #include <sstream> | 9 #include <sstream> |
10 | 10 |
| 11 #include "base/bind.h" |
11 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
12 #include "base/process_util.h" | 13 #include "base/process_util.h" |
| 14 #include "base/threading/thread.h" |
13 #include "ipc/ipc_channel_proxy.h" | 15 #include "ipc/ipc_channel_proxy.h" |
14 #include "ipc/ipc_tests.h" | 16 #include "ipc/ipc_tests.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "testing/multiprocess_func_list.h" | 18 #include "testing/multiprocess_func_list.h" |
17 | 19 |
18 #if defined(OS_POSIX) | 20 #if defined(OS_POSIX) |
19 #include "base/file_descriptor_posix.h" | 21 #include "base/file_descriptor_posix.h" |
20 #endif | 22 #endif |
21 | 23 |
22 // IPC messages for testing --------------------------------------------------- | 24 // IPC messages for testing --------------------------------------------------- |
(...skipping 22 matching lines...) Expand all Loading... |
45 | 47 |
46 namespace { | 48 namespace { |
47 const char kHelloString[] = "Hello, SyncSocket Client"; | 49 const char kHelloString[] = "Hello, SyncSocket Client"; |
48 const size_t kHelloStringLength = arraysize(kHelloString); | 50 const size_t kHelloStringLength = arraysize(kHelloString); |
49 } // namespace | 51 } // namespace |
50 | 52 |
51 // The SyncSocket server listener class processes two sorts of | 53 // The SyncSocket server listener class processes two sorts of |
52 // messages from the client. | 54 // messages from the client. |
53 class SyncSocketServerListener : public IPC::Channel::Listener { | 55 class SyncSocketServerListener : public IPC::Channel::Listener { |
54 public: | 56 public: |
55 SyncSocketServerListener() : chan_(NULL) { | 57 SyncSocketServerListener() : chan_(NULL) { |
56 } | 58 } |
57 | 59 |
58 void Init(IPC::Channel* chan) { | 60 void Init(IPC::Channel* chan) { |
59 chan_ = chan; | 61 chan_ = chan; |
60 } | 62 } |
61 | 63 |
62 virtual bool OnMessageReceived(const IPC::Message& msg) { | 64 virtual bool OnMessageReceived(const IPC::Message& msg) { |
63 if (msg.routing_id() == MSG_ROUTING_CONTROL) { | 65 if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
64 IPC_BEGIN_MESSAGE_MAP(SyncSocketServerListener, msg) | 66 IPC_BEGIN_MESSAGE_MAP(SyncSocketServerListener, msg) |
65 IPC_MESSAGE_HANDLER(MsgClassSetHandle, OnMsgClassSetHandle) | 67 IPC_MESSAGE_HANDLER(MsgClassSetHandle, OnMsgClassSetHandle) |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 class SyncSocketTest : public IPCChannelTest { | 166 class SyncSocketTest : public IPCChannelTest { |
165 }; | 167 }; |
166 | 168 |
167 TEST_F(SyncSocketTest, SanityTest) { | 169 TEST_F(SyncSocketTest, SanityTest) { |
168 SyncSocketClientListener listener; | 170 SyncSocketClientListener listener; |
169 IPC::Channel chan(kSyncSocketChannel, IPC::Channel::MODE_SERVER, | 171 IPC::Channel chan(kSyncSocketChannel, IPC::Channel::MODE_SERVER, |
170 &listener); | 172 &listener); |
171 base::ProcessHandle server_process = SpawnChild(SYNC_SOCKET_SERVER, &chan); | 173 base::ProcessHandle server_process = SpawnChild(SYNC_SOCKET_SERVER, &chan); |
172 ASSERT_TRUE(server_process); | 174 ASSERT_TRUE(server_process); |
173 // Create a pair of SyncSockets. | 175 // Create a pair of SyncSockets. |
174 base::SyncSocket* pair[2]; | 176 base::SyncSocket pair[2]; |
175 base::SyncSocket::CreatePair(pair); | 177 base::SyncSocket::CreatePair(&pair[0], &pair[1]); |
176 // Immediately after creation there should be no pending bytes. | 178 // Immediately after creation there should be no pending bytes. |
177 EXPECT_EQ(0U, pair[0]->Peek()); | 179 EXPECT_EQ(0U, pair[0].Peek()); |
178 EXPECT_EQ(0U, pair[1]->Peek()); | 180 EXPECT_EQ(0U, pair[1].Peek()); |
179 base::SyncSocket::Handle target_handle; | 181 base::SyncSocket::Handle target_handle; |
180 // Connect the channel and listener. | 182 // Connect the channel and listener. |
181 ASSERT_TRUE(chan.Connect()); | 183 ASSERT_TRUE(chan.Connect()); |
182 listener.Init(pair[0], &chan); | 184 listener.Init(&pair[0], &chan); |
183 #if defined(OS_WIN) | 185 #if defined(OS_WIN) |
184 // On windows we need to duplicate the handle into the server process. | 186 // On windows we need to duplicate the handle into the server process. |
185 BOOL retval = DuplicateHandle(GetCurrentProcess(), pair[1]->handle(), | 187 BOOL retval = DuplicateHandle(GetCurrentProcess(), pair[1].handle(), |
186 server_process, &target_handle, | 188 server_process, &target_handle, |
187 0, FALSE, DUPLICATE_SAME_ACCESS); | 189 0, FALSE, DUPLICATE_SAME_ACCESS); |
188 EXPECT_TRUE(retval); | 190 EXPECT_TRUE(retval); |
189 // Set up a message to pass the handle to the server. | 191 // Set up a message to pass the handle to the server. |
190 IPC::Message* msg = new MsgClassSetHandle(target_handle); | 192 IPC::Message* msg = new MsgClassSetHandle(target_handle); |
191 #else | 193 #else |
192 target_handle = pair[1]->handle(); | 194 target_handle = pair[1].handle(); |
193 // Set up a message to pass the handle to the server. | 195 // Set up a message to pass the handle to the server. |
194 base::FileDescriptor filedesc(target_handle, false); | 196 base::FileDescriptor filedesc(target_handle, false); |
195 IPC::Message* msg = new MsgClassSetHandle(filedesc); | 197 IPC::Message* msg = new MsgClassSetHandle(filedesc); |
196 #endif // defined(OS_WIN) | 198 #endif // defined(OS_WIN) |
197 EXPECT_TRUE(chan.Send(msg)); | 199 EXPECT_TRUE(chan.Send(msg)); |
198 // Use the current thread as the I/O thread. | 200 // Use the current thread as the I/O thread. |
199 MessageLoop::current()->Run(); | 201 MessageLoop::current()->Run(); |
200 // Shut down. | 202 // Shut down. |
201 delete pair[0]; | 203 pair[0].Close(); |
202 delete pair[1]; | 204 pair[1].Close(); |
203 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000)); | 205 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000)); |
204 base::CloseProcessHandle(server_process); | 206 base::CloseProcessHandle(server_process); |
205 } | 207 } |
| 208 |
| 209 static void BlockingRead(base::SyncSocket* socket, size_t* received) { |
| 210 // Notify the parent thread that we're up and running. |
| 211 socket->Send(kHelloString, kHelloStringLength); |
| 212 char buf[0xff]; // Won't ever be filled. |
| 213 *received = socket->Receive(buf, arraysize(buf)); |
| 214 } |
| 215 |
| 216 // Tests that we can safely end a blocking Receive operation on one thread |
| 217 // from another thread by disconnecting (but not closing) the socket. |
| 218 TEST_F(SyncSocketTest, DisconnectTest) { |
| 219 base::CancelableSyncSocket pair[2]; |
| 220 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
| 221 |
| 222 base::Thread worker("BlockingThread"); |
| 223 worker.Start(); |
| 224 |
| 225 // Try to do a blocking read from one of the sockets on the worker thread. |
| 226 size_t received = 1U; // Initialize to an unexpected value. |
| 227 worker.message_loop()->PostTask(FROM_HERE, |
| 228 base::Bind(&BlockingRead, &pair[0], &received)); |
| 229 |
| 230 // Wait for the worker thread to say hello. |
| 231 char hello[kHelloStringLength] = {0}; |
| 232 pair[1].Receive(&hello[0], sizeof(hello)); |
| 233 VLOG(1) << "Received: " << hello; |
| 234 // Give the worker a chance to start Receive(). |
| 235 base::PlatformThread::YieldCurrentThread(); |
| 236 |
| 237 // Now shut down the socket that the thread is issuing a blocking read on |
| 238 // which should cause Receive to return with an error. |
| 239 pair[0].Shutdown(); |
| 240 |
| 241 worker.Stop(); |
| 242 |
| 243 EXPECT_EQ(0U, received); |
| 244 } |
OLD | NEW |