Roll src/third_party/WebKit 28cb7a5:eda0012 (svn 190243:190250)
[chromium-blink-merge.git] / ppapi / tests / test_tcp_socket.cc
blob877b469ea4a40b3eef25d1894cb586483a23a82a
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.
5 #include "ppapi/tests/test_tcp_socket.h"
7 #include <vector>
9 #include "ppapi/cpp/message_loop.h"
10 #include "ppapi/cpp/tcp_socket.h"
11 #include "ppapi/tests/test_utils.h"
12 #include "ppapi/tests/testing_instance.h"
14 namespace {
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";
24 } // namespace
26 REGISTER_TEST_CASE(TCPSocket);
28 TestTCPSocket::TestTCPSocket(TestingInstance* instance)
29 : TestCase(instance),
30 socket_interface_1_0_(NULL) {
33 bool TestTCPSocket::Init() {
34 if (!pp::TCPSocket::IsAvailable())
35 return false;
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;
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;
47 std::string host;
48 uint16_t port = 0;
49 if (!GetLocalHostPort(instance_->pp_instance(), &host, &port))
50 return false;
52 if (!ResolveHost(instance_->pp_instance(), host, port, &addr_))
53 return false;
55 return true;
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);
62 RUN_CALLBACK_TEST(TestTCPSocket, Listen, filter);
63 RUN_CALLBACK_TEST(TestTCPSocket, Backlog, filter);
64 RUN_CALLBACK_TEST(TestTCPSocket, Interface_1_0, filter);
67 std::string TestTCPSocket::TestConnect() {
69 // The basic case.
70 pp::TCPSocket socket(instance_);
71 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
73 cb.WaitForResult(socket.Connect(addr_, cb.GetCallback()));
74 CHECK_CALLBACK_BEHAVIOR(cb);
75 ASSERT_EQ(PP_OK, cb.result());
77 pp::NetAddress local_addr, remote_addr;
78 local_addr = socket.GetLocalAddress();
79 remote_addr = socket.GetRemoteAddress();
81 ASSERT_NE(0, local_addr.pp_resource());
82 ASSERT_NE(0, remote_addr.pp_resource());
83 ASSERT_TRUE(EqualNetAddress(addr_, remote_addr));
85 socket.Close();
89 // Connect a bound socket.
90 pp::TCPSocket socket(instance_);
91 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
93 pp::NetAddress any_port_address;
94 ASSERT_SUBTEST_SUCCESS(GetAddressToBind(&any_port_address));
96 cb.WaitForResult(socket.Bind(any_port_address, cb.GetCallback()));
97 CHECK_CALLBACK_BEHAVIOR(cb);
98 ASSERT_EQ(PP_OK, cb.result());
100 cb.WaitForResult(socket.Connect(addr_, cb.GetCallback()));
101 CHECK_CALLBACK_BEHAVIOR(cb);
102 ASSERT_EQ(PP_OK, cb.result());
104 pp::NetAddress local_addr, remote_addr;
105 local_addr = socket.GetLocalAddress();
106 remote_addr = socket.GetRemoteAddress();
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));
113 socket.Close();
116 PASS();
119 std::string TestTCPSocket::TestReadWrite() {
120 pp::TCPSocket socket(instance_);
121 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
123 cb.WaitForResult(socket.Connect(addr_, cb.GetCallback()));
124 CHECK_CALLBACK_BEHAVIOR(cb);
125 ASSERT_EQ(PP_OK, cb.result());
127 ASSERT_SUBTEST_SUCCESS(WriteToSocket(&socket, "GET / HTTP/1.0\r\n\r\n"));
129 // Read up to the first \n and check that it looks like valid HTTP response.
130 std::string s;
131 ASSERT_SUBTEST_SUCCESS(ReadFirstLineFromSocket(&socket, &s));
132 ASSERT_TRUE(ValidateHttpResponse(s));
134 PASS();
137 std::string TestTCPSocket::TestSetOption() {
138 pp::TCPSocket socket(instance_);
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());
143 // These options can be set even before the socket is connected.
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());
151 cb_1.WaitForResult(result_1);
152 CHECK_CALLBACK_BEHAVIOR(cb_1);
153 ASSERT_EQ(PP_OK, cb_1.result());
155 cb_2.WaitForResult(result_2);
156 CHECK_CALLBACK_BEHAVIOR(cb_2);
157 ASSERT_EQ(PP_OK, cb_2.result());
159 cb_3.WaitForResult(result_3);
160 CHECK_CALLBACK_BEHAVIOR(cb_3);
161 ASSERT_EQ(PP_OK, cb_3.result());
163 cb_1.WaitForResult(socket.Connect(addr_, cb_1.GetCallback()));
164 CHECK_CALLBACK_BEHAVIOR(cb_1);
165 ASSERT_EQ(PP_OK, cb_1.result());
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());
174 cb_1.WaitForResult(result_1);
175 CHECK_CALLBACK_BEHAVIOR(cb_1);
176 ASSERT_EQ(PP_OK, cb_1.result());
178 cb_2.WaitForResult(result_2);
179 CHECK_CALLBACK_BEHAVIOR(cb_2);
180 ASSERT_EQ(PP_OK, cb_2.result());
182 cb_3.WaitForResult(result_3);
183 CHECK_CALLBACK_BEHAVIOR(cb_3);
184 ASSERT_EQ(PP_OK, cb_3.result());
186 PASS();
189 std::string TestTCPSocket::TestListen() {
190 static const int kBacklog = 2;
192 pp::TCPSocket server_socket(instance_);
193 ASSERT_SUBTEST_SUCCESS(StartListen(&server_socket, kBacklog));
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();
206 int32_t accept_rv = server_socket.Accept(accept_callback.GetCallback());
208 pp::TCPSocket client_socket;
209 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
210 do {
211 client_socket = pp::TCPSocket(instance_);
213 callback.WaitForResult(client_socket.Connect(
214 server_socket.GetLocalAddress(), callback.GetCallback()));
215 } while (callback.result() != PP_OK);
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());
222 accept_callback.WaitForResult(accept_rv);
223 CHECK_CALLBACK_BEHAVIOR(accept_callback);
224 ASSERT_EQ(PP_OK, accept_callback.result());
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());
232 ASSERT_TRUE(EqualNetAddress(client_local_addr, accepted_remote_addr));
234 const char kSentByte = 'a';
235 ASSERT_SUBTEST_SUCCESS(WriteToSocket(&client_socket,
236 std::string(1, kSentByte)));
238 char received_byte;
239 ASSERT_SUBTEST_SUCCESS(ReadFromSocket(&accepted_socket,
240 &received_byte,
241 sizeof(received_byte)));
242 ASSERT_EQ(kSentByte, received_byte);
244 accepted_socket.Close();
245 client_socket.Close();
246 server_socket.Close();
248 PASS();
251 std::string TestTCPSocket::TestBacklog() {
252 static const size_t kBacklog = 5;
254 pp::TCPSocket server_socket(instance_);
255 ASSERT_SUBTEST_SUCCESS(StartListen(&server_socket, 2 * kBacklog));
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());
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());
277 accepted_sockets[i] = new pp::TCPSocket(callback.output());
278 ASSERT_FALSE(accepted_sockets[i]->is_null());
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());
287 for (size_t i = 0; i < kBacklog; ++i) {
288 const char byte = static_cast<char>('a' + i);
289 ASSERT_SUBTEST_SUCCESS(WriteToSocket(client_sockets[i],
290 std::string(1, byte)));
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;
305 for (size_t i = 0; i < kBacklog; ++i) {
306 ASSERT_TRUE(byte_received[i]);
308 delete client_sockets[i];
309 delete connect_callbacks[i];
310 delete accepted_sockets[i];
313 PASS();
316 std::string TestTCPSocket::TestInterface_1_0() {
317 PP_Resource socket = socket_interface_1_0_->Create(instance_->pp_instance());
318 ASSERT_NE(0, socket);
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());
326 ASSERT_SUBTEST_SUCCESS(WriteToSocket_1_0(socket, "GET / HTTP/1.0\r\n\r\n"));
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));
333 pp::Module::Get()->core()->ReleaseResource(socket);
334 PASS();
337 std::string TestTCPSocket::ReadFirstLineFromSocket(pp::TCPSocket* socket,
338 std::string* s) {
339 char buffer[1000];
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());
345 cb.WaitForResult(socket->Read(buffer, sizeof(buffer), cb.GetCallback()));
346 CHECK_CALLBACK_BEHAVIOR(cb);
347 ASSERT_GT(cb.result(), 0);
348 s->reserve(s->size() + cb.result());
349 for (int32_t i = 0; i < cb.result(); ++i) {
350 s->push_back(buffer[i]);
351 if (buffer[i] == '\n')
352 PASS();
355 PASS();
358 std::string TestTCPSocket::ReadFirstLineFromSocket_1_0(PP_Resource socket,
359 std::string* s) {
360 char buffer[1000];
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();
378 PASS();
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(
387 socket->Read(buffer, static_cast<int32_t>(num_bytes),
388 callback.GetCallback()));
389 CHECK_CALLBACK_BEHAVIOR(callback);
390 ASSERT_GT(callback.result(), 0);
391 buffer += callback.result();
392 num_bytes -= callback.result();
394 ASSERT_EQ(0u, num_bytes);
395 PASS();
398 std::string TestTCPSocket::WriteToSocket(pp::TCPSocket* socket,
399 const std::string& s) {
400 const char* buffer = s.data();
401 size_t written = 0;
402 while (written < s.size()) {
403 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
404 cb.WaitForResult(
405 socket->Write(buffer + written,
406 static_cast<int32_t>(s.size() - written),
407 cb.GetCallback()));
408 CHECK_CALLBACK_BEHAVIOR(cb);
409 ASSERT_GT(cb.result(), 0);
410 written += cb.result();
412 ASSERT_EQ(written, s.size());
413 PASS();
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(
424 socket, buffer + written,
425 static_cast<int32_t>(s.size() - written),
426 cb.GetCallback().pp_completion_callback()));
427 CHECK_CALLBACK_BEHAVIOR(cb);
428 ASSERT_GT(cb.result(), 0);
429 written += cb.result();
431 ASSERT_EQ(written, s.size());
432 PASS();
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());
442 ASSERT_TRUE(ReplacePort(instance_->pp_instance(), socket.GetLocalAddress(), 0,
443 address));
444 ASSERT_FALSE(address->is_null());
445 PASS();
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));
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());
458 callback.WaitForResult(
459 socket->Listen(backlog, callback.GetCallback()));
460 CHECK_CALLBACK_BEHAVIOR(callback);
461 ASSERT_EQ(PP_OK, callback.result());
463 PASS();