[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 1 | // Copyright 2013 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 "ppapi/tests/test_tcp_socket.h" |
| 6 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
| 9 | #include "ppapi/cpp/message_loop.h" |
[email protected] | b838eea | 2013-06-22 22:17:22 | [diff] [blame] | 10 | #include "ppapi/cpp/tcp_socket.h" |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 11 | #include "ppapi/tests/test_utils.h" |
| 12 | #include "ppapi/tests/testing_instance.h" |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | // Validates the first line of an HTTP response. |
| 17 | bool ValidateHttpResponse(const std::string& s) { |
| 18 | // Just check that it begins with "HTTP/" and ends with a "\r\n". |
| 19 | return s.size() >= 5 && |
| 20 | s.substr(0, 5) == "HTTP/" && |
| 21 | s.substr(s.size() - 2) == "\r\n"; |
| 22 | } |
| 23 | |
| 24 | } // namespace |
| 25 | |
| 26 | REGISTER_TEST_CASE(TCPSocket); |
| 27 | |
[email protected] | 7619290 | 2013-10-02 21:56:11 | [diff] [blame] | 28 | TestTCPSocket::TestTCPSocket(TestingInstance* instance) |
| 29 | : TestCase(instance), |
| 30 | socket_interface_1_0_(NULL) { |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | bool TestTCPSocket::Init() { |
[email protected] | b838eea | 2013-06-22 22:17:22 | [diff] [blame] | 34 | if (!pp::TCPSocket::IsAvailable()) |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 35 | return false; |
[email protected] | 7619290 | 2013-10-02 21:56:11 | [diff] [blame] | 36 | socket_interface_1_0_ = |
| 37 | static_cast<const PPB_TCPSocket_1_0*>( |
| 38 | pp::Module::Get()->GetBrowserInterface(PPB_TCPSOCKET_INTERFACE_1_0)); |
| 39 | if (!socket_interface_1_0_) |
| 40 | return false; |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 41 | |
| 42 | // We need something to connect to, so we connect to the HTTP server whence we |
| 43 | // came. Grab the host and port. |
| 44 | if (!EnsureRunningOverHTTP()) |
| 45 | return false; |
| 46 | |
| 47 | std::string host; |
| 48 | uint16_t port = 0; |
| 49 | if (!GetLocalHostPort(instance_->pp_instance(), &host, &port)) |
| 50 | return false; |
| 51 | |
| 52 | if (!ResolveHost(instance_->pp_instance(), host, port, &addr_)) |
| 53 | return false; |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | void TestTCPSocket::RunTests(const std::string& filter) { |
| 59 | RUN_CALLBACK_TEST(TestTCPSocket, Connect, filter); |
| 60 | RUN_CALLBACK_TEST(TestTCPSocket, ReadWrite, filter); |
| 61 | RUN_CALLBACK_TEST(TestTCPSocket, SetOption, filter); |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 62 | RUN_CALLBACK_TEST(TestTCPSocket, Listen, filter); |
| 63 | RUN_CALLBACK_TEST(TestTCPSocket, Backlog, filter); |
[email protected] | 7619290 | 2013-10-02 21:56:11 | [diff] [blame] | 64 | RUN_CALLBACK_TEST(TestTCPSocket, Interface_1_0, filter); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::string TestTCPSocket::TestConnect() { |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 68 | { |
| 69 | // The basic case. |
| 70 | pp::TCPSocket socket(instance_); |
| 71 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 72 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 73 | cb.WaitForResult(socket.Connect(addr_, cb.GetCallback())); |
| 74 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 75 | ASSERT_EQ(PP_OK, cb.result()); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 76 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 77 | pp::NetAddress local_addr, remote_addr; |
| 78 | local_addr = socket.GetLocalAddress(); |
| 79 | remote_addr = socket.GetRemoteAddress(); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 80 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 81 | ASSERT_NE(0, local_addr.pp_resource()); |
| 82 | ASSERT_NE(0, remote_addr.pp_resource()); |
| 83 | ASSERT_TRUE(EqualNetAddress(addr_, remote_addr)); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 84 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 85 | socket.Close(); |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | // Connect a bound socket. |
| 90 | pp::TCPSocket socket(instance_); |
| 91 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
| 92 | |
| 93 | pp::NetAddress any_port_address; |
| 94 | ASSERT_SUBTEST_SUCCESS(GetAddressToBind(&any_port_address)); |
| 95 | |
| 96 | cb.WaitForResult(socket.Bind(any_port_address, cb.GetCallback())); |
| 97 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 98 | ASSERT_EQ(PP_OK, cb.result()); |
| 99 | |
| 100 | cb.WaitForResult(socket.Connect(addr_, cb.GetCallback())); |
| 101 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 102 | ASSERT_EQ(PP_OK, cb.result()); |
| 103 | |
| 104 | pp::NetAddress local_addr, remote_addr; |
| 105 | local_addr = socket.GetLocalAddress(); |
| 106 | remote_addr = socket.GetRemoteAddress(); |
| 107 | |
| 108 | ASSERT_NE(0, local_addr.pp_resource()); |
| 109 | ASSERT_NE(0, remote_addr.pp_resource()); |
| 110 | ASSERT_TRUE(EqualNetAddress(addr_, remote_addr)); |
| 111 | ASSERT_NE(0u, GetPort(local_addr)); |
| 112 | |
| 113 | socket.Close(); |
| 114 | } |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 115 | |
| 116 | PASS(); |
| 117 | } |
| 118 | |
| 119 | std::string TestTCPSocket::TestReadWrite() { |
[email protected] | b838eea | 2013-06-22 22:17:22 | [diff] [blame] | 120 | pp::TCPSocket socket(instance_); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 121 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
| 122 | |
| 123 | cb.WaitForResult(socket.Connect(addr_, cb.GetCallback())); |
| 124 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 125 | ASSERT_EQ(PP_OK, cb.result()); |
| 126 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 127 | ASSERT_SUBTEST_SUCCESS(WriteToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 128 | |
| 129 | // Read up to the first \n and check that it looks like valid HTTP response. |
| 130 | std::string s; |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 131 | ASSERT_SUBTEST_SUCCESS(ReadFirstLineFromSocket(&socket, &s)); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 132 | ASSERT_TRUE(ValidateHttpResponse(s)); |
| 133 | |
| 134 | PASS(); |
| 135 | } |
| 136 | |
| 137 | std::string TestTCPSocket::TestSetOption() { |
[email protected] | b838eea | 2013-06-22 22:17:22 | [diff] [blame] | 138 | pp::TCPSocket socket(instance_); |
[email protected] | d981043 | 2013-06-20 21:24:08 | [diff] [blame] | 139 | TestCompletionCallback cb_1(instance_->pp_instance(), callback_type()); |
| 140 | TestCompletionCallback cb_2(instance_->pp_instance(), callback_type()); |
| 141 | TestCompletionCallback cb_3(instance_->pp_instance(), callback_type()); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 142 | |
hidehiko | fd305c12 | 2014-12-11 06:01:47 | [diff] [blame] | 143 | // These options can be set even before the socket is connected. |
[email protected] | d981043 | 2013-06-20 21:24:08 | [diff] [blame] | 144 | int32_t result_1 = socket.SetOption(PP_TCPSOCKET_OPTION_NO_DELAY, |
| 145 | true, cb_1.GetCallback()); |
| 146 | int32_t result_2 = socket.SetOption(PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE, |
| 147 | 256, cb_2.GetCallback()); |
| 148 | int32_t result_3 = socket.SetOption(PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE, |
| 149 | 512, cb_3.GetCallback()); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 150 | |
[email protected] | d981043 | 2013-06-20 21:24:08 | [diff] [blame] | 151 | cb_1.WaitForResult(result_1); |
| 152 | CHECK_CALLBACK_BEHAVIOR(cb_1); |
hidehiko | fd305c12 | 2014-12-11 06:01:47 | [diff] [blame] | 153 | ASSERT_EQ(PP_OK, cb_1.result()); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 154 | |
[email protected] | d981043 | 2013-06-20 21:24:08 | [diff] [blame] | 155 | cb_2.WaitForResult(result_2); |
| 156 | CHECK_CALLBACK_BEHAVIOR(cb_2); |
hidehiko | fd305c12 | 2014-12-11 06:01:47 | [diff] [blame] | 157 | ASSERT_EQ(PP_OK, cb_2.result()); |
[email protected] | d981043 | 2013-06-20 21:24:08 | [diff] [blame] | 158 | |
| 159 | cb_3.WaitForResult(result_3); |
| 160 | CHECK_CALLBACK_BEHAVIOR(cb_3); |
hidehiko | fd305c12 | 2014-12-11 06:01:47 | [diff] [blame] | 161 | ASSERT_EQ(PP_OK, cb_3.result()); |
[email protected] | d981043 | 2013-06-20 21:24:08 | [diff] [blame] | 162 | |
| 163 | cb_1.WaitForResult(socket.Connect(addr_, cb_1.GetCallback())); |
| 164 | CHECK_CALLBACK_BEHAVIOR(cb_1); |
| 165 | ASSERT_EQ(PP_OK, cb_1.result()); |
| 166 | |
| 167 | result_1 = socket.SetOption(PP_TCPSOCKET_OPTION_NO_DELAY, |
| 168 | false, cb_1.GetCallback()); |
| 169 | result_2 = socket.SetOption(PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE, |
| 170 | 512, cb_2.GetCallback()); |
| 171 | result_3 = socket.SetOption(PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE, |
| 172 | 1024, cb_3.GetCallback()); |
| 173 | |
| 174 | cb_1.WaitForResult(result_1); |
| 175 | CHECK_CALLBACK_BEHAVIOR(cb_1); |
| 176 | ASSERT_EQ(PP_OK, cb_1.result()); |
| 177 | |
| 178 | cb_2.WaitForResult(result_2); |
| 179 | CHECK_CALLBACK_BEHAVIOR(cb_2); |
| 180 | ASSERT_EQ(PP_OK, cb_2.result()); |
| 181 | |
| 182 | cb_3.WaitForResult(result_3); |
| 183 | CHECK_CALLBACK_BEHAVIOR(cb_3); |
| 184 | ASSERT_EQ(PP_OK, cb_3.result()); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 185 | |
| 186 | PASS(); |
| 187 | } |
| 188 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 189 | std::string TestTCPSocket::TestListen() { |
| 190 | static const int kBacklog = 2; |
| 191 | |
| 192 | pp::TCPSocket server_socket(instance_); |
| 193 | ASSERT_SUBTEST_SUCCESS(StartListen(&server_socket, kBacklog)); |
| 194 | |
| 195 | // We can't use a blocking callback for Accept, because it will wait forever |
| 196 | // for the client to connect, since the client connects after. |
| 197 | TestCompletionCallbackWithOutput<pp::TCPSocket> |
| 198 | accept_callback(instance_->pp_instance(), PP_REQUIRED); |
| 199 | // We need to make sure there's a message loop to run accept_callback on. |
| 200 | pp::MessageLoop current_thread_loop(pp::MessageLoop::GetCurrent()); |
| 201 | if (current_thread_loop.is_null() && testing_interface_->IsOutOfProcess()) { |
| 202 | current_thread_loop = pp::MessageLoop(instance_); |
| 203 | current_thread_loop.AttachToCurrentThread(); |
| 204 | } |
| 205 | |
| 206 | int32_t accept_rv = server_socket.Accept(accept_callback.GetCallback()); |
| 207 | |
| 208 | pp::TCPSocket client_socket; |
| 209 | TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 210 | do { |
| 211 | client_socket = pp::TCPSocket(instance_); |
| 212 | |
| 213 | callback.WaitForResult(client_socket.Connect( |
| 214 | server_socket.GetLocalAddress(), callback.GetCallback())); |
| 215 | } while (callback.result() != PP_OK); |
| 216 | |
| 217 | pp::NetAddress client_local_addr = client_socket.GetLocalAddress(); |
| 218 | pp::NetAddress client_remote_addr = client_socket.GetRemoteAddress(); |
| 219 | ASSERT_FALSE(client_local_addr.is_null()); |
| 220 | ASSERT_FALSE(client_remote_addr.is_null()); |
| 221 | |
| 222 | accept_callback.WaitForResult(accept_rv); |
| 223 | CHECK_CALLBACK_BEHAVIOR(accept_callback); |
| 224 | ASSERT_EQ(PP_OK, accept_callback.result()); |
| 225 | |
| 226 | pp::TCPSocket accepted_socket(accept_callback.output()); |
| 227 | pp::NetAddress accepted_local_addr = accepted_socket.GetLocalAddress(); |
| 228 | pp::NetAddress accepted_remote_addr = accepted_socket.GetRemoteAddress(); |
| 229 | ASSERT_FALSE(accepted_local_addr.is_null()); |
| 230 | ASSERT_FALSE(accepted_remote_addr.is_null()); |
| 231 | |
| 232 | ASSERT_TRUE(EqualNetAddress(client_local_addr, accepted_remote_addr)); |
| 233 | |
| 234 | const char kSentByte = 'a'; |
| 235 | ASSERT_SUBTEST_SUCCESS(WriteToSocket(&client_socket, |
| 236 | std::string(1, kSentByte))); |
| 237 | |
| 238 | char received_byte; |
| 239 | ASSERT_SUBTEST_SUCCESS(ReadFromSocket(&accepted_socket, |
| 240 | &received_byte, |
| 241 | sizeof(received_byte))); |
| 242 | ASSERT_EQ(kSentByte, received_byte); |
| 243 | |
| 244 | accepted_socket.Close(); |
| 245 | client_socket.Close(); |
| 246 | server_socket.Close(); |
| 247 | |
| 248 | PASS(); |
| 249 | } |
| 250 | |
| 251 | std::string TestTCPSocket::TestBacklog() { |
| 252 | static const size_t kBacklog = 5; |
| 253 | |
| 254 | pp::TCPSocket server_socket(instance_); |
| 255 | ASSERT_SUBTEST_SUCCESS(StartListen(&server_socket, 2 * kBacklog)); |
| 256 | |
| 257 | std::vector<pp::TCPSocket*> client_sockets(kBacklog); |
| 258 | std::vector<TestCompletionCallback*> connect_callbacks(kBacklog); |
| 259 | std::vector<int32_t> connect_rv(kBacklog); |
| 260 | pp::NetAddress address = server_socket.GetLocalAddress(); |
| 261 | for (size_t i = 0; i < kBacklog; ++i) { |
| 262 | client_sockets[i] = new pp::TCPSocket(instance_); |
| 263 | connect_callbacks[i] = new TestCompletionCallback(instance_->pp_instance(), |
| 264 | callback_type()); |
| 265 | connect_rv[i] = client_sockets[i]->Connect( |
| 266 | address, connect_callbacks[i]->GetCallback()); |
| 267 | } |
| 268 | |
| 269 | std::vector<pp::TCPSocket*> accepted_sockets(kBacklog); |
| 270 | for (size_t i = 0; i < kBacklog; ++i) { |
| 271 | TestCompletionCallbackWithOutput<pp::TCPSocket> callback( |
| 272 | instance_->pp_instance(), callback_type()); |
| 273 | callback.WaitForResult(server_socket.Accept(callback.GetCallback())); |
| 274 | CHECK_CALLBACK_BEHAVIOR(callback); |
| 275 | ASSERT_EQ(PP_OK, callback.result()); |
| 276 | |
| 277 | accepted_sockets[i] = new pp::TCPSocket(callback.output()); |
| 278 | ASSERT_FALSE(accepted_sockets[i]->is_null()); |
| 279 | } |
| 280 | |
| 281 | for (size_t i = 0; i < kBacklog; ++i) { |
| 282 | connect_callbacks[i]->WaitForResult(connect_rv[i]); |
| 283 | CHECK_CALLBACK_BEHAVIOR(*connect_callbacks[i]); |
| 284 | ASSERT_EQ(PP_OK, connect_callbacks[i]->result()); |
| 285 | } |
| 286 | |
| 287 | for (size_t i = 0; i < kBacklog; ++i) { |
brettw | 669d47b1 | 2015-02-13 21:17:38 | [diff] [blame] | 288 | const char byte = static_cast<char>('a' + i); |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 289 | ASSERT_SUBTEST_SUCCESS(WriteToSocket(client_sockets[i], |
| 290 | std::string(1, byte))); |
| 291 | } |
| 292 | |
| 293 | bool byte_received[kBacklog] = {}; |
| 294 | for (size_t i = 0; i < kBacklog; ++i) { |
| 295 | char byte; |
| 296 | ASSERT_SUBTEST_SUCCESS(ReadFromSocket( |
| 297 | accepted_sockets[i], &byte, sizeof(byte))); |
| 298 | const size_t index = byte - 'a'; |
| 299 | ASSERT_GE(index, 0u); |
| 300 | ASSERT_LT(index, kBacklog); |
| 301 | ASSERT_FALSE(byte_received[index]); |
| 302 | byte_received[index] = true; |
| 303 | } |
| 304 | |
| 305 | for (size_t i = 0; i < kBacklog; ++i) { |
| 306 | ASSERT_TRUE(byte_received[i]); |
| 307 | |
| 308 | delete client_sockets[i]; |
| 309 | delete connect_callbacks[i]; |
| 310 | delete accepted_sockets[i]; |
| 311 | } |
| 312 | |
| 313 | PASS(); |
| 314 | } |
| 315 | |
[email protected] | 7619290 | 2013-10-02 21:56:11 | [diff] [blame] | 316 | std::string TestTCPSocket::TestInterface_1_0() { |
| 317 | PP_Resource socket = socket_interface_1_0_->Create(instance_->pp_instance()); |
| 318 | ASSERT_NE(0, socket); |
| 319 | |
| 320 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
| 321 | cb.WaitForResult(socket_interface_1_0_->Connect( |
| 322 | socket, addr_.pp_resource(), cb.GetCallback().pp_completion_callback())); |
| 323 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 324 | ASSERT_EQ(PP_OK, cb.result()); |
| 325 | |
| 326 | ASSERT_SUBTEST_SUCCESS(WriteToSocket_1_0(socket, "GET / HTTP/1.0\r\n\r\n")); |
| 327 | |
| 328 | // Read up to the first \n and check that it looks like valid HTTP response. |
| 329 | std::string s; |
| 330 | ASSERT_SUBTEST_SUCCESS(ReadFirstLineFromSocket_1_0(socket, &s)); |
| 331 | ASSERT_TRUE(ValidateHttpResponse(s)); |
| 332 | |
| 333 | pp::Module::Get()->core()->ReleaseResource(socket); |
| 334 | PASS(); |
| 335 | } |
| 336 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 337 | std::string TestTCPSocket::ReadFirstLineFromSocket(pp::TCPSocket* socket, |
| 338 | std::string* s) { |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 339 | char buffer[1000]; |
| 340 | |
| 341 | s->clear(); |
| 342 | // Make sure we don't just hang if |Read()| spews. |
| 343 | while (s->size() < 10000) { |
| 344 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 345 | cb.WaitForResult(socket->Read(buffer, sizeof(buffer), cb.GetCallback())); |
| 346 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 347 | ASSERT_GT(cb.result(), 0); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 348 | s->reserve(s->size() + cb.result()); |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 349 | for (int32_t i = 0; i < cb.result(); ++i) { |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 350 | s->push_back(buffer[i]); |
| 351 | if (buffer[i] == '\n') |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 352 | PASS(); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 353 | } |
| 354 | } |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 355 | PASS(); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | 7619290 | 2013-10-02 21:56:11 | [diff] [blame] | 358 | std::string TestTCPSocket::ReadFirstLineFromSocket_1_0(PP_Resource socket, |
| 359 | std::string* s) { |
| 360 | char buffer[1000]; |
| 361 | |
| 362 | s->clear(); |
| 363 | // Make sure we don't just hang if |Read()| spews. |
| 364 | while (s->size() < 10000) { |
| 365 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
| 366 | cb.WaitForResult(socket_interface_1_0_->Read( |
| 367 | socket, buffer, sizeof(buffer), |
| 368 | cb.GetCallback().pp_completion_callback())); |
| 369 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 370 | ASSERT_GT(cb.result(), 0); |
| 371 | s->reserve(s->size() + cb.result()); |
| 372 | for (int32_t i = 0; i < cb.result(); ++i) { |
| 373 | s->push_back(buffer[i]); |
| 374 | if (buffer[i] == '\n') |
| 375 | PASS(); |
| 376 | } |
| 377 | } |
| 378 | PASS(); |
| 379 | } |
| 380 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 381 | std::string TestTCPSocket::ReadFromSocket(pp::TCPSocket* socket, |
| 382 | char* buffer, |
| 383 | size_t num_bytes) { |
| 384 | while (num_bytes > 0) { |
| 385 | TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 386 | callback.WaitForResult( |
brettw | 669d47b1 | 2015-02-13 21:17:38 | [diff] [blame] | 387 | socket->Read(buffer, static_cast<int32_t>(num_bytes), |
| 388 | callback.GetCallback())); |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 389 | CHECK_CALLBACK_BEHAVIOR(callback); |
| 390 | ASSERT_GT(callback.result(), 0); |
| 391 | buffer += callback.result(); |
| 392 | num_bytes -= callback.result(); |
| 393 | } |
| 394 | ASSERT_EQ(0u, num_bytes); |
| 395 | PASS(); |
| 396 | } |
| 397 | |
| 398 | std::string TestTCPSocket::WriteToSocket(pp::TCPSocket* socket, |
| 399 | const std::string& s) { |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 400 | const char* buffer = s.data(); |
| 401 | size_t written = 0; |
| 402 | while (written < s.size()) { |
| 403 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 404 | cb.WaitForResult( |
brettw | 669d47b1 | 2015-02-13 21:17:38 | [diff] [blame] | 405 | socket->Write(buffer + written, |
| 406 | static_cast<int32_t>(s.size() - written), |
| 407 | cb.GetCallback())); |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 408 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 409 | ASSERT_GT(cb.result(), 0); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 410 | written += cb.result(); |
| 411 | } |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 412 | ASSERT_EQ(written, s.size()); |
| 413 | PASS(); |
[email protected] | b1784f4 | 2013-06-13 06:48:36 | [diff] [blame] | 414 | } |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 415 | |
[email protected] | 7619290 | 2013-10-02 21:56:11 | [diff] [blame] | 416 | std::string TestTCPSocket::WriteToSocket_1_0( |
| 417 | PP_Resource socket, |
| 418 | const std::string& s) { |
| 419 | const char* buffer = s.data(); |
| 420 | size_t written = 0; |
| 421 | while (written < s.size()) { |
| 422 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
| 423 | cb.WaitForResult(socket_interface_1_0_->Write( |
brettw | 669d47b1 | 2015-02-13 21:17:38 | [diff] [blame] | 424 | socket, buffer + written, |
| 425 | static_cast<int32_t>(s.size() - written), |
[email protected] | 7619290 | 2013-10-02 21:56:11 | [diff] [blame] | 426 | cb.GetCallback().pp_completion_callback())); |
| 427 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 428 | ASSERT_GT(cb.result(), 0); |
| 429 | written += cb.result(); |
| 430 | } |
| 431 | ASSERT_EQ(written, s.size()); |
| 432 | PASS(); |
| 433 | } |
| 434 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 435 | std::string TestTCPSocket::GetAddressToBind(pp::NetAddress* address) { |
| 436 | pp::TCPSocket socket(instance_); |
| 437 | TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 438 | callback.WaitForResult(socket.Connect(addr_, callback.GetCallback())); |
| 439 | CHECK_CALLBACK_BEHAVIOR(callback); |
| 440 | ASSERT_EQ(PP_OK, callback.result()); |
| 441 | |
| 442 | ASSERT_TRUE(ReplacePort(instance_->pp_instance(), socket.GetLocalAddress(), 0, |
| 443 | address)); |
| 444 | ASSERT_FALSE(address->is_null()); |
| 445 | PASS(); |
| 446 | } |
| 447 | |
| 448 | std::string TestTCPSocket::StartListen(pp::TCPSocket* socket, int32_t backlog) { |
| 449 | pp::NetAddress any_port_address; |
| 450 | ASSERT_SUBTEST_SUCCESS(GetAddressToBind(&any_port_address)); |
| 451 | |
| 452 | TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 453 | callback.WaitForResult( |
| 454 | socket->Bind(any_port_address, callback.GetCallback())); |
| 455 | CHECK_CALLBACK_BEHAVIOR(callback); |
| 456 | ASSERT_EQ(PP_OK, callback.result()); |
| 457 | |
| 458 | callback.WaitForResult( |
| 459 | socket->Listen(backlog, callback.GetCallback())); |
| 460 | CHECK_CALLBACK_BEHAVIOR(callback); |
| 461 | ASSERT_EQ(PP_OK, callback.result()); |
| 462 | |
| 463 | PASS(); |
| 464 | } |