tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 5 | #include "base/sync_socket.h" |
| 6 | |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 8 | #include <stdio.h> |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 9 | #include <memory> |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 10 | #include <sstream> |
skyostil | e468e66 | 2015-05-12 20:29:21 +0900 | [diff] [blame] | 11 | #include <string> |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 12 | |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 13 | #include "base/bind.h" |
skyostil | e468e66 | 2015-05-12 20:29:21 +0900 | [diff] [blame] | 14 | #include "base/location.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 15 | #include "base/macros.h" |
fdoray | 284aae5 | 2016-06-23 04:56:16 +0900 | [diff] [blame] | 16 | #include "base/run_loop.h" |
skyostil | e468e66 | 2015-05-12 20:29:21 +0900 | [diff] [blame] | 17 | #include "base/single_thread_task_runner.h" |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 18 | #include "base/threading/thread.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 19 | #include "build/build_config.h" |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 20 | #include "ipc/ipc_test_base.h" |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 22 | |
tony@chromium.org | d4a0129 | 2011-06-03 03:37:51 +0900 | [diff] [blame] | 23 | #if defined(OS_POSIX) |
brettw@google.com | 7c5cc67 | 2011-01-01 11:17:08 +0900 | [diff] [blame] | 24 | #include "base/file_descriptor_posix.h" |
tony@chromium.org | d4a0129 | 2011-06-03 03:37:51 +0900 | [diff] [blame] | 25 | #endif |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 26 | |
viettrungluu@chromium.org | 7ca1913 | 2013-01-12 05:56:22 +0900 | [diff] [blame] | 27 | // IPC messages for testing ---------------------------------------------------- |
darin@chromium.org | b4587d5 | 2011-08-27 06:27:30 +0900 | [diff] [blame] | 28 | |
| 29 | #define IPC_MESSAGE_IMPL |
| 30 | #include "ipc/ipc_message_macros.h" |
| 31 | |
| 32 | #define IPC_MESSAGE_START TestMsgStart |
| 33 | |
| 34 | // Message class to pass a base::SyncSocket::Handle to another process. This |
| 35 | // is not as easy as it sounds, because of the differences in transferring |
| 36 | // Windows HANDLEs versus posix file descriptors. |
| 37 | #if defined(OS_WIN) |
| 38 | IPC_MESSAGE_CONTROL1(MsgClassSetHandle, base::SyncSocket::Handle) |
| 39 | #elif defined(OS_POSIX) |
| 40 | IPC_MESSAGE_CONTROL1(MsgClassSetHandle, base::FileDescriptor) |
| 41 | #endif |
| 42 | |
| 43 | // Message class to pass a response to the server. |
| 44 | IPC_MESSAGE_CONTROL1(MsgClassResponse, std::string) |
| 45 | |
| 46 | // Message class to tell the server to shut down. |
| 47 | IPC_MESSAGE_CONTROL0(MsgClassShutdown) |
| 48 | |
viettrungluu@chromium.org | 7ca1913 | 2013-01-12 05:56:22 +0900 | [diff] [blame] | 49 | // ----------------------------------------------------------------------------- |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 50 | |
| 51 | namespace { |
viettrungluu@chromium.org | 7ca1913 | 2013-01-12 05:56:22 +0900 | [diff] [blame] | 52 | |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 53 | const char kHelloString[] = "Hello, SyncSocket Client"; |
| 54 | const size_t kHelloStringLength = arraysize(kHelloString); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 55 | |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 56 | // The SyncSocket server listener class processes two sorts of |
| 57 | // messages from the client. |
brettw@chromium.org | db1259e | 2012-06-30 07:05:26 +0900 | [diff] [blame] | 58 | class SyncSocketServerListener : public IPC::Listener { |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 59 | public: |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 60 | SyncSocketServerListener() : chan_(NULL) { |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void Init(IPC::Channel* chan) { |
| 64 | chan_ = chan; |
| 65 | } |
| 66 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 67 | bool OnMessageReceived(const IPC::Message& msg) override { |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 68 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 69 | IPC_BEGIN_MESSAGE_MAP(SyncSocketServerListener, msg) |
| 70 | IPC_MESSAGE_HANDLER(MsgClassSetHandle, OnMsgClassSetHandle) |
| 71 | IPC_MESSAGE_HANDLER(MsgClassShutdown, OnMsgClassShutdown) |
| 72 | IPC_END_MESSAGE_MAP() |
| 73 | } |
jam@chromium.org | 8a2c784 | 2010-12-24 15:19:28 +0900 | [diff] [blame] | 74 | return true; |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private: |
| 78 | // This sort of message is sent first, causing the transfer of |
| 79 | // the handle for the SyncSocket. This message sends a buffer |
| 80 | // on the SyncSocket and then sends a response to the client. |
sehr@google.com | fdc9006 | 2009-11-26 09:28:02 +0900 | [diff] [blame] | 81 | #if defined(OS_WIN) |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 82 | void OnMsgClassSetHandle(const base::SyncSocket::Handle handle) { |
sehr@google.com | fdc9006 | 2009-11-26 09:28:02 +0900 | [diff] [blame] | 83 | SetHandle(handle); |
| 84 | } |
| 85 | #elif defined(OS_POSIX) |
| 86 | void OnMsgClassSetHandle(const base::FileDescriptor& fd_struct) { |
| 87 | SetHandle(fd_struct.fd); |
| 88 | } |
| 89 | #else |
| 90 | # error "What platform?" |
| 91 | #endif // defined(OS_WIN) |
| 92 | |
| 93 | void SetHandle(base::SyncSocket::Handle handle) { |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 94 | base::SyncSocket sync_socket(handle); |
xians@chromium.org | f7d9af8 | 2012-04-19 19:23:03 +0900 | [diff] [blame] | 95 | EXPECT_EQ(sync_socket.Send(kHelloString, kHelloStringLength), |
| 96 | kHelloStringLength); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 97 | IPC::Message* msg = new MsgClassResponse(kHelloString); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 98 | EXPECT_TRUE(chan_->Send(msg)); |
| 99 | } |
| 100 | |
| 101 | // When the client responds, it sends back a shutdown message, |
| 102 | // which causes the message loop to exit. |
Gabriel Charette | 274bec8 | 2017-07-26 21:36:23 +0900 | [diff] [blame] | 103 | void OnMsgClassShutdown() { base::RunLoop::QuitCurrentWhenIdleDeprecated(); } |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 104 | |
| 105 | IPC::Channel* chan_; |
| 106 | |
| 107 | DISALLOW_COPY_AND_ASSIGN(SyncSocketServerListener); |
| 108 | }; |
| 109 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 110 | // Runs the fuzzing server child mode. Returns when the preset number of |
| 111 | // messages have been received. |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 112 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(SyncSocketServerClient) { |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 113 | SyncSocketServerListener listener; |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 114 | Connect(&listener); |
| 115 | listener.Init(channel()); |
fdoray | 284aae5 | 2016-06-23 04:56:16 +0900 | [diff] [blame] | 116 | base::RunLoop().Run(); |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 117 | Close(); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // The SyncSocket client listener only processes one sort of message, |
| 121 | // a response from the server. |
brettw@chromium.org | db1259e | 2012-06-30 07:05:26 +0900 | [diff] [blame] | 122 | class SyncSocketClientListener : public IPC::Listener { |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 123 | public: |
Chris Watkins | 31bfe1a | 2017-11-30 11:11:59 +0900 | [diff] [blame] | 124 | SyncSocketClientListener() = default; |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 125 | |
| 126 | void Init(base::SyncSocket* socket, IPC::Channel* chan) { |
| 127 | socket_ = socket; |
| 128 | chan_ = chan; |
| 129 | } |
| 130 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 131 | bool OnMessageReceived(const IPC::Message& msg) override { |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 132 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 133 | IPC_BEGIN_MESSAGE_MAP(SyncSocketClientListener, msg) |
| 134 | IPC_MESSAGE_HANDLER(MsgClassResponse, OnMsgClassResponse) |
| 135 | IPC_END_MESSAGE_MAP() |
| 136 | } |
jam@chromium.org | 8a2c784 | 2010-12-24 15:19:28 +0900 | [diff] [blame] | 137 | return true; |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | private: |
| 141 | // When a response is received from the server, it sends the same |
| 142 | // string as was written on the SyncSocket. These are compared |
| 143 | // and a shutdown message is sent back to the server. |
| 144 | void OnMsgClassResponse(const std::string& str) { |
cpu@chromium.org | 750392c | 2009-12-05 07:53:22 +0900 | [diff] [blame] | 145 | // We rely on the order of sync_socket.Send() and chan_->Send() in |
| 146 | // the SyncSocketServerListener object. |
| 147 | EXPECT_EQ(kHelloStringLength, socket_->Peek()); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 148 | char buf[kHelloStringLength]; |
| 149 | socket_->Receive(static_cast<void*>(buf), kHelloStringLength); |
| 150 | EXPECT_EQ(strcmp(str.c_str(), buf), 0); |
sehr@google.com | b72918f | 2009-12-07 04:45:08 +0900 | [diff] [blame] | 151 | // After receiving from the socket there should be no bytes left. |
thomasvl@google.com | 9a24207 | 2010-07-23 23:18:59 +0900 | [diff] [blame] | 152 | EXPECT_EQ(0U, socket_->Peek()); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 153 | IPC::Message* msg = new MsgClassShutdown(); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 154 | EXPECT_TRUE(chan_->Send(msg)); |
Gabriel Charette | 274bec8 | 2017-07-26 21:36:23 +0900 | [diff] [blame] | 155 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | base::SyncSocket* socket_; |
| 159 | IPC::Channel* chan_; |
| 160 | |
| 161 | DISALLOW_COPY_AND_ASSIGN(SyncSocketClientListener); |
| 162 | }; |
| 163 | |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 164 | using SyncSocketTest = IPCChannelMojoTestBase; |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 165 | |
amistry | 87fc78f | 2016-05-05 14:12:09 +0900 | [diff] [blame] | 166 | TEST_F(SyncSocketTest, SanityTest) { |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 167 | Init("SyncSocketServerClient"); |
| 168 | |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 169 | SyncSocketClientListener listener; |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 170 | CreateChannel(&listener); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 171 | // Create a pair of SyncSockets. |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 172 | base::SyncSocket pair[2]; |
| 173 | base::SyncSocket::CreatePair(&pair[0], &pair[1]); |
sehr@google.com | b72918f | 2009-12-07 04:45:08 +0900 | [diff] [blame] | 174 | // Immediately after creation there should be no pending bytes. |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 175 | EXPECT_EQ(0U, pair[0].Peek()); |
| 176 | EXPECT_EQ(0U, pair[1].Peek()); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 177 | base::SyncSocket::Handle target_handle; |
sehr@google.com | fdc9006 | 2009-11-26 09:28:02 +0900 | [diff] [blame] | 178 | // Connect the channel and listener. |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 179 | ASSERT_TRUE(ConnectChannel()); |
| 180 | listener.Init(&pair[0], channel()); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 181 | #if defined(OS_WIN) |
| 182 | // On windows we need to duplicate the handle into the server process. |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 183 | BOOL retval = DuplicateHandle(GetCurrentProcess(), pair[1].handle(), |
rvargas | f355d88 | 2015-01-13 07:23:23 +0900 | [diff] [blame] | 184 | client_process().Handle(), &target_handle, |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 185 | 0, FALSE, DUPLICATE_SAME_ACCESS); |
| 186 | EXPECT_TRUE(retval); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 187 | // Set up a message to pass the handle to the server. |
| 188 | IPC::Message* msg = new MsgClassSetHandle(target_handle); |
sehr@google.com | fdc9006 | 2009-11-26 09:28:02 +0900 | [diff] [blame] | 189 | #else |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 190 | target_handle = pair[1].handle(); |
sehr@google.com | fdc9006 | 2009-11-26 09:28:02 +0900 | [diff] [blame] | 191 | // Set up a message to pass the handle to the server. |
| 192 | base::FileDescriptor filedesc(target_handle, false); |
| 193 | IPC::Message* msg = new MsgClassSetHandle(filedesc); |
| 194 | #endif // defined(OS_WIN) |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 195 | EXPECT_TRUE(sender()->Send(msg)); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 196 | // Use the current thread as the I/O thread. |
fdoray | 284aae5 | 2016-06-23 04:56:16 +0900 | [diff] [blame] | 197 | base::RunLoop().Run(); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 198 | // Shut down. |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 199 | pair[0].Close(); |
| 200 | pair[1].Close(); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 201 | EXPECT_TRUE(WaitForClientShutdown()); |
| 202 | DestroyChannel(); |
sehr@google.com | 4f325aa | 2009-11-25 01:14:53 +0900 | [diff] [blame] | 203 | } |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 204 | |
xians@chromium.org | f7d9af8 | 2012-04-19 19:23:03 +0900 | [diff] [blame] | 205 | // A blocking read operation that will block the thread until it receives |
| 206 | // |length| bytes of packets or Shutdown() is called on another thread. |
| 207 | static void BlockingRead(base::SyncSocket* socket, char* buf, |
| 208 | size_t length, size_t* received) { |
| 209 | DCHECK(buf != NULL); |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 210 | // Notify the parent thread that we're up and running. |
| 211 | socket->Send(kHelloString, kHelloStringLength); |
xians@chromium.org | f7d9af8 | 2012-04-19 19:23:03 +0900 | [diff] [blame] | 212 | *received = socket->Receive(buf, length); |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Tests that we can safely end a blocking Receive operation on one thread |
| 216 | // from another thread by disconnecting (but not closing) the socket. |
| 217 | TEST_F(SyncSocketTest, DisconnectTest) { |
| 218 | base::CancelableSyncSocket pair[2]; |
| 219 | ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
| 220 | |
| 221 | base::Thread worker("BlockingThread"); |
| 222 | worker.Start(); |
| 223 | |
| 224 | // Try to do a blocking read from one of the sockets on the worker thread. |
xians@chromium.org | f7d9af8 | 2012-04-19 19:23:03 +0900 | [diff] [blame] | 225 | char buf[0xff]; |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 226 | size_t received = 1U; // Initialize to an unexpected value. |
skyostil | e468e66 | 2015-05-12 20:29:21 +0900 | [diff] [blame] | 227 | worker.task_runner()->PostTask( |
| 228 | FROM_HERE, |
xians@chromium.org | f7d9af8 | 2012-04-19 19:23:03 +0900 | [diff] [blame] | 229 | base::Bind(&BlockingRead, &pair[0], &buf[0], arraysize(buf), &received)); |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 230 | |
| 231 | // Wait for the worker thread to say hello. |
| 232 | char hello[kHelloStringLength] = {0}; |
| 233 | pair[1].Receive(&hello[0], sizeof(hello)); |
xians@chromium.org | f7d9af8 | 2012-04-19 19:23:03 +0900 | [diff] [blame] | 234 | EXPECT_EQ(0, strcmp(hello, kHelloString)); |
tommi@chromium.org | d5f359a | 2012-01-25 21:04:17 +0900 | [diff] [blame] | 235 | // Give the worker a chance to start Receive(). |
| 236 | base::PlatformThread::YieldCurrentThread(); |
| 237 | |
| 238 | // Now shut down the socket that the thread is issuing a blocking read on |
| 239 | // which should cause Receive to return with an error. |
| 240 | pair[0].Shutdown(); |
| 241 | |
| 242 | worker.Stop(); |
| 243 | |
| 244 | EXPECT_EQ(0U, received); |
| 245 | } |
xians@chromium.org | f7d9af8 | 2012-04-19 19:23:03 +0900 | [diff] [blame] | 246 | |
| 247 | // Tests that read is a blocking operation. |
amistry | 87fc78f | 2016-05-05 14:12:09 +0900 | [diff] [blame] | 248 | TEST_F(SyncSocketTest, BlockingReceiveTest) { |
xians@chromium.org | f7d9af8 | 2012-04-19 19:23:03 +0900 | [diff] [blame] | 249 | base::CancelableSyncSocket pair[2]; |
| 250 | ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
| 251 | |
| 252 | base::Thread worker("BlockingThread"); |
| 253 | worker.Start(); |
| 254 | |
| 255 | // Try to do a blocking read from one of the sockets on the worker thread. |
| 256 | char buf[kHelloStringLength] = {0}; |
| 257 | size_t received = 1U; // Initialize to an unexpected value. |
skyostil | e468e66 | 2015-05-12 20:29:21 +0900 | [diff] [blame] | 258 | worker.task_runner()->PostTask(FROM_HERE, |
| 259 | base::Bind(&BlockingRead, &pair[0], &buf[0], |
| 260 | kHelloStringLength, &received)); |
xians@chromium.org | f7d9af8 | 2012-04-19 19:23:03 +0900 | [diff] [blame] | 261 | |
| 262 | // Wait for the worker thread to say hello. |
| 263 | char hello[kHelloStringLength] = {0}; |
| 264 | pair[1].Receive(&hello[0], sizeof(hello)); |
| 265 | EXPECT_EQ(0, strcmp(hello, kHelloString)); |
| 266 | // Give the worker a chance to start Receive(). |
| 267 | base::PlatformThread::YieldCurrentThread(); |
| 268 | |
| 269 | // Send a message to the socket on the blocking thead, it should free the |
| 270 | // socket from Receive(). |
| 271 | pair[1].Send(kHelloString, kHelloStringLength); |
| 272 | worker.Stop(); |
| 273 | |
| 274 | // Verify the socket has received the message. |
| 275 | EXPECT_TRUE(strcmp(buf, kHelloString) == 0); |
| 276 | EXPECT_EQ(kHelloStringLength, received); |
| 277 | } |
| 278 | |
| 279 | // Tests that the write operation is non-blocking and returns immediately |
| 280 | // when there is insufficient space in the socket's buffer. |
| 281 | TEST_F(SyncSocketTest, NonBlockingWriteTest) { |
| 282 | base::CancelableSyncSocket pair[2]; |
| 283 | ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
| 284 | |
| 285 | // Fill up the buffer for one of the socket, Send() should not block the |
| 286 | // thread even when the buffer is full. |
| 287 | while (pair[0].Send(kHelloString, kHelloStringLength) != 0) {} |
| 288 | |
| 289 | // Data should be avialble on another socket. |
| 290 | size_t bytes_in_buffer = pair[1].Peek(); |
| 291 | EXPECT_NE(bytes_in_buffer, 0U); |
| 292 | |
| 293 | // No more data can be written to the buffer since socket has been full, |
| 294 | // verify that the amount of avialble data on another socket is unchanged. |
| 295 | EXPECT_EQ(0U, pair[0].Send(kHelloString, kHelloStringLength)); |
| 296 | EXPECT_EQ(bytes_in_buffer, pair[1].Peek()); |
| 297 | |
| 298 | // Read from another socket to free some space for a new write. |
| 299 | char hello[kHelloStringLength] = {0}; |
| 300 | pair[1].Receive(&hello[0], sizeof(hello)); |
| 301 | |
| 302 | // Should be able to write more data to the buffer now. |
| 303 | EXPECT_EQ(kHelloStringLength, pair[0].Send(kHelloString, kHelloStringLength)); |
| 304 | } |
viettrungluu@chromium.org | 7ca1913 | 2013-01-12 05:56:22 +0900 | [diff] [blame] | 305 | |
| 306 | } // namespace |