summaryrefslogtreecommitdiffstats
path: root/ppapi/tests/test_tcp_socket.cc
blob: a3164deb6b6c2b2a42f9337ea0278f71eb7b1222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/tests/test_tcp_socket.h"

#include <vector>

#include "ppapi/cpp/message_loop.h"
#include "ppapi/cpp/tcp_socket.h"
#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"

namespace {

// Validates the first line of an HTTP response.
bool ValidateHttpResponse(const std::string& s) {
  // Just check that it begins with "HTTP/" and ends with a "\r\n".
  return s.size() >= 5 &&
         s.substr(0, 5) == "HTTP/" &&
         s.substr(s.size() - 2) == "\r\n";
}

}  // namespace

REGISTER_TEST_CASE(TCPSocket);

TestTCPSocket::TestTCPSocket(TestingInstance* instance)
    : TestCase(instance),
      socket_interface_1_0_(NULL) {
}

bool TestTCPSocket::Init() {
  if (!pp::TCPSocket::IsAvailable())
    return false;
  socket_interface_1_0_ =
      static_cast<const PPB_TCPSocket_1_0*>(
          pp::Module::Get()->GetBrowserInterface(PPB_TCPSOCKET_INTERFACE_1_0));
  if (!socket_interface_1_0_)
    return false;

  // We need something to connect to, so we connect to the HTTP server whence we
  // came. Grab the host and port.
  if (!EnsureRunningOverHTTP())
    return false;

  std::string host;
  uint16_t port = 0;
  if (!GetLocalHostPort(instance_->pp_instance(), &host, &port))
    return false;

  if (!ResolveHost(instance_->pp_instance(), host, port, &addr_))
    return false;

  return true;
}

void TestTCPSocket::RunTests(const std::string& filter) {
  RUN_CALLBACK_TEST(TestTCPSocket, Connect, filter);
  RUN_CALLBACK_TEST(TestTCPSocket, ReadWrite, filter);
  RUN_CALLBACK_TEST(TestTCPSocket, SetOption, filter);
  RUN_CALLBACK_TEST(TestTCPSocket, Listen, filter);
  RUN_CALLBACK_TEST(TestTCPSocket, Backlog, filter);
  RUN_CALLBACK_TEST(TestTCPSocket, Interface_1_0, filter);
}

std::string TestTCPSocket::TestConnect() {
  {
    // The basic case.
    pp::TCPSocket socket(instance_);
    TestCompletionCallback cb(instance_->pp_instance(), callback_type());

    cb.WaitForResult(socket.Connect(addr_, cb.GetCallback()));
    CHECK_CALLBACK_BEHAVIOR(cb);
    ASSERT_EQ(PP_OK, cb.result());

    pp::NetAddress local_addr, remote_addr;
    local_addr = socket.GetLocalAddress();
    remote_addr = socket.GetRemoteAddress();

    ASSERT_NE(0, local_addr.pp_resource());
    ASSERT_NE(0, remote_addr.pp_resource());
    ASSERT_TRUE(EqualNetAddress(addr_, remote_addr));

    socket.Close();
  }

  {
    // Connect a bound socket.
    pp::TCPSocket socket(instance_);
    TestCompletionCallback cb(instance_->pp_instance(), callback_type());

    pp::NetAddress any_port_address;
    ASSERT_SUBTEST_SUCCESS(GetAddressToBind(&any_port_address));

    cb.WaitForResult(socket.Bind(any_port_address, cb.GetCallback()));
    CHECK_CALLBACK_BEHAVIOR(cb);
    ASSERT_EQ(PP_OK, cb.result());

    cb.WaitForResult(socket.Connect(addr_, cb.GetCallback()));
    CHECK_CALLBACK_BEHAVIOR(cb);
    ASSERT_EQ(PP_OK, cb.result());

    pp::NetAddress local_addr, remote_addr;
    local_addr = socket.GetLocalAddress();
    remote_addr = socket.GetRemoteAddress();

    ASSERT_NE(0, local_addr.pp_resource());
    ASSERT_NE(0, remote_addr.pp_resource());
    ASSERT_TRUE(EqualNetAddress(addr_, remote_addr));
    ASSERT_NE(0u, GetPort(local_addr));

    socket.Close();
  }

  PASS();
}

std::string TestTCPSocket::TestReadWrite() {
  pp::TCPSocket socket(instance_);
  TestCompletionCallback cb(instance_->pp_instance(), callback_type());

  cb.WaitForResult(socket.Connect(addr_, cb.GetCallback()));
  CHECK_CALLBACK_BEHAVIOR(cb);
  ASSERT_EQ(PP_OK, cb.result());

  ASSERT_SUBTEST_SUCCESS(WriteToSocket(&socket, "GET / HTTP/1.0\r\n\r\n"));

  // Read up to the first \n and check that it looks like valid HTTP response.
  std::string s;
  ASSERT_SUBTEST_SUCCESS(ReadFirstLineFromSocket(&socket, &s));
  ASSERT_TRUE(ValidateHttpResponse(s));

  PASS();
}

std::string TestTCPSocket::TestSetOption() {
  pp::TCPSocket socket(instance_);
  TestCompletionCallback cb_1(instance_->pp_instance(), callback_type());
  TestCompletionCallback cb_2(instance_->pp_instance(), callback_type());
  TestCompletionCallback cb_3(instance_->pp_instance(), callback_type());

  // These options can be set even before the socket is connected.
  int32_t result_1 = socket.SetOption(PP_TCPSOCKET_OPTION_NO_DELAY,
                                      true, cb_1.GetCallback());
  int32_t result_2 = socket.SetOption(PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE,
                                      256, cb_2.GetCallback());
  int32_t result_3 = socket.SetOption(PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE,
                                      512, cb_3.GetCallback());

  cb_1.WaitForResult(result_1);
  CHECK_CALLBACK_BEHAVIOR(cb_1);
  ASSERT_EQ(PP_OK, cb_1.result());

  cb_2.WaitForResult(result_2);
  CHECK_CALLBACK_BEHAVIOR(cb_2);
  ASSERT_EQ(PP_OK, cb_2.result());

  cb_3.WaitForResult(result_3);
  CHECK_CALLBACK_BEHAVIOR(cb_3);
  ASSERT_EQ(PP_OK, cb_3.result());

  cb_1.WaitForResult(socket.Connect(addr_, cb_1.GetCallback()));
  CHECK_CALLBACK_BEHAVIOR(cb_1);
  ASSERT_EQ(PP_OK, cb_1.result());

  result_1 = socket.SetOption(PP_TCPSOCKET_OPTION_NO_DELAY,
                              false, cb_1.GetCallback());
  result_2 = socket.SetOption(PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE,
                              512, cb_2.GetCallback());
  result_3 = socket.SetOption(PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE,
                              1024, cb_3.GetCallback());

  cb_1.WaitForResult(result_1);
  CHECK_CALLBACK_BEHAVIOR(cb_1);
  ASSERT_EQ(PP_OK, cb_1.result());

  cb_2.WaitForResult(result_2);
  CHECK_CALLBACK_BEHAVIOR(cb_2);
  ASSERT_EQ(PP_OK, cb_2.result());

  cb_3.WaitForResult(result_3);
  CHECK_CALLBACK_BEHAVIOR(cb_3);
  ASSERT_EQ(PP_OK, cb_3.result());

  PASS();
}

std::string TestTCPSocket::TestListen() {
  static const int kBacklog = 2;

  pp::TCPSocket server_socket(instance_);
  ASSERT_SUBTEST_SUCCESS(StartListen(&server_socket, kBacklog));

  // We can't use a blocking callback for Accept, because it will wait forever
  // for the client to connect, since the client connects after.
  TestCompletionCallbackWithOutput<pp::TCPSocket>
      accept_callback(instance_->pp_instance(), PP_REQUIRED);
  // We need to make sure there's a message loop to run accept_callback on.
  pp::MessageLoop current_thread_loop(pp::MessageLoop::GetCurrent());
  if (current_thread_loop.is_null() && testing_interface_->IsOutOfProcess()) {
    current_thread_loop = pp::MessageLoop(instance_);
    current_thread_loop.AttachToCurrentThread();
  }

  int32_t accept_rv = server_socket.Accept(accept_callback.GetCallback());

  pp::TCPSocket client_socket;
  TestCompletionCallback callback(instance_->pp_instance(), callback_type());
  do {
    client_socket = pp::TCPSocket(instance_);

    callback.WaitForResult(client_socket.Connect(
        server_socket.GetLocalAddress(), callback.GetCallback()));
  } while (callback.result() != PP_OK);

  pp::NetAddress client_local_addr = client_socket.GetLocalAddress();
  pp::NetAddress client_remote_addr = client_socket.GetRemoteAddress();
  ASSERT_FALSE(client_local_addr.is_null());
  ASSERT_FALSE(client_remote_addr.is_null());

  accept_callback.WaitForResult(accept_rv);
  CHECK_CALLBACK_BEHAVIOR(accept_callback);
  ASSERT_EQ(PP_OK, accept_callback.result());

  pp::TCPSocket accepted_socket(accept_callback.output());
  pp::NetAddress accepted_local_addr = accepted_socket.GetLocalAddress();
  pp::NetAddress accepted_remote_addr = accepted_socket.GetRemoteAddress();
  ASSERT_FALSE(accepted_local_addr.is_null());
  ASSERT_FALSE(accepted_remote_addr.is_null());

  ASSERT_TRUE(EqualNetAddress(client_local_addr, accepted_remote_addr));

  const char kSentByte = 'a';
  ASSERT_SUBTEST_SUCCESS(WriteToSocket(&client_socket,
                                       std::string(1, kSentByte)));

  char received_byte;
  ASSERT_SUBTEST_SUCCESS(ReadFromSocket(&accepted_socket,
                                        &received_byte,
                                        sizeof(received_byte)));
  ASSERT_EQ(kSentByte, received_byte);

  accepted_socket.Close();
  client_socket.Close();
  server_socket.Close();

  PASS();
}

std::string TestTCPSocket::TestBacklog() {
  static const size_t kBacklog = 5;

  pp::TCPSocket server_socket(instance_);
  ASSERT_SUBTEST_SUCCESS(StartListen(&server_socket, 2 * kBacklog));

  std::vector<pp::TCPSocket*> client_sockets(kBacklog);
  std::vector<TestCompletionCallback*> connect_callbacks(kBacklog);
  std::vector<int32_t> connect_rv(kBacklog);
  pp::NetAddress address = server_socket.GetLocalAddress();
  for (size_t i = 0; i < kBacklog; ++i) {
    client_sockets[i] = new pp::TCPSocket(instance_);
    connect_callbacks[i] = new TestCompletionCallback(instance_->pp_instance(),
                                                      callback_type());
    connect_rv[i] = client_sockets[i]->Connect(
        address, connect_callbacks[i]->GetCallback());
  }

  std::vector<pp::TCPSocket*> accepted_sockets(kBacklog);
  for (size_t i = 0; i < kBacklog; ++i) {
    TestCompletionCallbackWithOutput<pp::TCPSocket> callback(
        instance_->pp_instance(), callback_type());
    callback.WaitForResult(server_socket.Accept(callback.GetCallback()));
    CHECK_CALLBACK_BEHAVIOR(callback);
    ASSERT_EQ(PP_OK, callback.result());

    accepted_sockets[i] = new pp::TCPSocket(callback.output());
    ASSERT_FALSE(accepted_sockets[i]->is_null());
  }

  for (size_t i = 0; i < kBacklog; ++i) {
    connect_callbacks[i]->WaitForResult(connect_rv[i]);
    CHECK_CALLBACK_BEHAVIOR(*connect_callbacks[i]);
    ASSERT_EQ(PP_OK, connect_callbacks[i]->result());
  }

  for (size_t i = 0; i < kBacklog; ++i) {
    const char byte = 'a' + i;
    ASSERT_SUBTEST_SUCCESS(WriteToSocket(client_sockets[i],
                                         std::string(1, byte)));
  }

  bool byte_received[kBacklog] = {};
  for (size_t i = 0; i < kBacklog; ++i) {
    char byte;
    ASSERT_SUBTEST_SUCCESS(ReadFromSocket(
        accepted_sockets[i], &byte, sizeof(byte)));
    const size_t index = byte - 'a';
    ASSERT_GE(index, 0u);
    ASSERT_LT(index, kBacklog);
    ASSERT_FALSE(byte_received[index]);
    byte_received[index] = true;
  }

  for (size_t i = 0; i < kBacklog; ++i) {
    ASSERT_TRUE(byte_received[i]);

    delete client_sockets[i];
    delete connect_callbacks[i];
    delete accepted_sockets[i];
  }

  PASS();
}

std::string TestTCPSocket::TestInterface_1_0() {
  PP_Resource socket = socket_interface_1_0_->Create(instance_->pp_instance());
  ASSERT_NE(0, socket);

  TestCompletionCallback cb(instance_->pp_instance(), callback_type());
  cb.WaitForResult(socket_interface_1_0_->Connect(
      socket, addr_.pp_resource(), cb.GetCallback().pp_completion_callback()));
  CHECK_CALLBACK_BEHAVIOR(cb);
  ASSERT_EQ(PP_OK, cb.result());

  ASSERT_SUBTEST_SUCCESS(WriteToSocket_1_0(socket, "GET / HTTP/1.0\r\n\r\n"));

  // Read up to the first \n and check that it looks like valid HTTP response.
  std::string s;
  ASSERT_SUBTEST_SUCCESS(ReadFirstLineFromSocket_1_0(socket, &s));
  ASSERT_TRUE(ValidateHttpResponse(s));

  pp::Module::Get()->core()->ReleaseResource(socket);
  PASS();
}

std::string TestTCPSocket::ReadFirstLineFromSocket(pp::TCPSocket* socket,
                                                   std::string* s) {
  char buffer[1000];

  s->clear();
  // Make sure we don't just hang if |Read()| spews.
  while (s->size() < 10000) {
    TestCompletionCallback cb(instance_->pp_instance(), callback_type());
    cb.WaitForResult(socket->Read(buffer, sizeof(buffer), cb.GetCallback()));
    CHECK_CALLBACK_BEHAVIOR(cb);
    ASSERT_GT(cb.result(), 0);
    s->reserve(s->size() + cb.result());
    for (int32_t i = 0; i < cb.result(); ++i) {
      s->push_back(buffer[i]);
      if (buffer[i] == '\n')
        PASS();
    }
  }
  PASS();
}

std::string TestTCPSocket::ReadFirstLineFromSocket_1_0(PP_Resource socket,
                                                       std::string* s) {
  char buffer[1000];

  s->clear();
  // Make sure we don't just hang if |Read()| spews.
  while (s->size() < 10000) {
    TestCompletionCallback cb(instance_->pp_instance(), callback_type());
    cb.WaitForResult(socket_interface_1_0_->Read(
        socket, buffer, sizeof(buffer),
        cb.GetCallback().pp_completion_callback()));
    CHECK_CALLBACK_BEHAVIOR(cb);
    ASSERT_GT(cb.result(), 0);
    s->reserve(s->size() + cb.result());
    for (int32_t i = 0; i < cb.result(); ++i) {
      s->push_back(buffer[i]);
      if (buffer[i] == '\n')
        PASS();
    }
  }
  PASS();
}

std::string TestTCPSocket::ReadFromSocket(pp::TCPSocket* socket,
                                          char* buffer,
                                          size_t num_bytes) {
  while (num_bytes > 0) {
    TestCompletionCallback callback(instance_->pp_instance(), callback_type());
    callback.WaitForResult(
        socket->Read(buffer, num_bytes, callback.GetCallback()));
    CHECK_CALLBACK_BEHAVIOR(callback);
    ASSERT_GT(callback.result(), 0);
    buffer += callback.result();
    num_bytes -= callback.result();
  }
  ASSERT_EQ(0u, num_bytes);
  PASS();
}

std::string TestTCPSocket::WriteToSocket(pp::TCPSocket* socket,
                                         const std::string& s) {
  const char* buffer = s.data();
  size_t written = 0;
  while (written < s.size()) {
    TestCompletionCallback cb(instance_->pp_instance(), callback_type());
    cb.WaitForResult(
        socket->Write(buffer + written, s.size() - written, cb.GetCallback()));
    CHECK_CALLBACK_BEHAVIOR(cb);
    ASSERT_GT(cb.result(), 0);
    written += cb.result();
  }
  ASSERT_EQ(written, s.size());
  PASS();
}

std::string TestTCPSocket::WriteToSocket_1_0(
    PP_Resource socket,
    const std::string& s) {
  const char* buffer = s.data();
  size_t written = 0;
  while (written < s.size()) {
    TestCompletionCallback cb(instance_->pp_instance(), callback_type());
    cb.WaitForResult(socket_interface_1_0_->Write(
        socket, buffer + written, s.size() - written,
        cb.GetCallback().pp_completion_callback()));
    CHECK_CALLBACK_BEHAVIOR(cb);
    ASSERT_GT(cb.result(), 0);
    written += cb.result();
  }
  ASSERT_EQ(written, s.size());
  PASS();
}

std::string TestTCPSocket::GetAddressToBind(pp::NetAddress* address) {
  pp::TCPSocket socket(instance_);
  TestCompletionCallback callback(instance_->pp_instance(), callback_type());
  callback.WaitForResult(socket.Connect(addr_, callback.GetCallback()));
  CHECK_CALLBACK_BEHAVIOR(callback);
  ASSERT_EQ(PP_OK, callback.result());

  ASSERT_TRUE(ReplacePort(instance_->pp_instance(), socket.GetLocalAddress(), 0,
                          address));
  ASSERT_FALSE(address->is_null());
  PASS();
}

std::string TestTCPSocket::StartListen(pp::TCPSocket* socket, int32_t backlog) {
  pp::NetAddress any_port_address;
  ASSERT_SUBTEST_SUCCESS(GetAddressToBind(&any_port_address));

  TestCompletionCallback callback(instance_->pp_instance(), callback_type());
  callback.WaitForResult(
      socket->Bind(any_port_address, callback.GetCallback()));
  CHECK_CALLBACK_BEHAVIOR(callback);
  ASSERT_EQ(PP_OK, callback.result());

  callback.WaitForResult(
      socket->Listen(backlog, callback.GetCallback()));
  CHECK_CALLBACK_BEHAVIOR(callback);
  ASSERT_EQ(PP_OK, callback.result());

  PASS();
}