summaryrefslogtreecommitdiffstats
path: root/ppapi/tests/test_transport.cc
blob: 52d072e5866514f059ef3f63aabf7f0921cdb76d (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
// Copyright (c) 2011 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_transport.h"

#include <stdlib.h>
#include <string.h>

#include <list>
#include <map>

#include "ppapi/c/dev/ppb_testing_dev.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_macros.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/dev/transport_dev.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"
#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"

REGISTER_TEST_CASE(Transport);

#define RUN_SUBTEST(function) { \
    std::string result = function; \
    if (!result.empty()) \
      return result; \
  }

namespace {

const char kTestChannelName[] = "test";
const int kReadBufferSize = 65536;

class StreamReader {
 public:
  StreamReader(pp::Transport_Dev* transport,
               int expected_size,
               pp::CompletionCallback done_callback)
      : expected_size_(expected_size),
        done_callback_(done_callback),
        PP_ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)),
        transport_(transport),
        received_size_(0) {
    Read();
  }

  const std::list<std::vector<char> >& received() { return received_; }
  std::list<std::string> errors() { return errors_; }

 private:
  void Read() {
    while (true) {
      buffer_.resize(kReadBufferSize);
      int result = transport_->Recv(
          &buffer_[0], buffer_.size(),
          callback_factory_.NewOptionalCallback(&StreamReader::OnReadFinished));
      if (result > 0)
        DidFinishRead(result);
      else
        break;
    }
  }

  void OnReadFinished(int result) {
    DidFinishRead(result);
    if (result > 0)
      Read();
  }

  void DidFinishRead(int result) {
    if (result > 0) {
      if (result > static_cast<int>(buffer_.size())) {
        errors_.push_back(TestCase::MakeFailureMessage(
            __FILE__, __LINE__,
            "Recv() returned value that is bigger than the buffer."));
      }
      buffer_.resize(result);
      received_.push_back(buffer_);
      received_size_ += buffer_.size();
      if (received_size_ >= expected_size_)
        done_callback_.Run(0);
    }
  }

  int expected_size_;
  pp::CompletionCallback done_callback_;
  pp::CompletionCallbackFactory<StreamReader> callback_factory_;
  pp::Transport_Dev* transport_;
  std::vector<char> buffer_;
  std::list<std::vector<char> > received_;
  int received_size_;
  std::list<std::string> errors_;
};

}  // namespace

TestTransport::~TestTransport() {
  delete transport1_;
  delete transport2_;
}

bool TestTransport::Init() {
  transport_interface_ = static_cast<const PPB_Transport_Dev*>(
      pp::Module::Get()->GetBrowserInterface(PPB_TRANSPORT_DEV_INTERFACE));
  return transport_interface_ && CheckTestingInterface();
}

void TestTransport::RunTests(const std::string& filter) {
  RUN_TEST(Create, filter);
  RUN_TEST_FORCEASYNC_AND_NOT(Connect, filter);
  RUN_TEST(SetProperty, filter);
  RUN_TEST_FORCEASYNC_AND_NOT(SendDataUdp, filter);
  RUN_TEST_FORCEASYNC_AND_NOT(SendDataTcp, filter);
  RUN_TEST_FORCEASYNC_AND_NOT(ConnectAndCloseUdp, filter);
  RUN_TEST_FORCEASYNC_AND_NOT(ConnectAndCloseTcp, filter);
}

std::string TestTransport::InitTargets(PP_TransportType type) {
  transport1_ = new pp::Transport_Dev(instance_, kTestChannelName, type);
  transport2_ = new pp::Transport_Dev(instance_, kTestChannelName, type);

  ASSERT_NE(NULL, transport1_);
  ASSERT_NE(NULL, transport2_);

  PASS();
}

std::string TestTransport::Connect() {
  TestCompletionCallback connect_cb1(instance_->pp_instance());
  TestCompletionCallback connect_cb2(instance_->pp_instance());
  ASSERT_EQ(transport1_->Connect(connect_cb1), PP_OK_COMPLETIONPENDING);
  ASSERT_EQ(transport2_->Connect(connect_cb2), PP_OK_COMPLETIONPENDING);

  pp::Var address1;
  pp::Var address2;
  TestCompletionCallback next_address_cb1(instance_->pp_instance());
  TestCompletionCallback next_address_cb2(instance_->pp_instance());
  ASSERT_EQ(transport1_->GetNextAddress(&address1, next_address_cb1),
            PP_OK_COMPLETIONPENDING);
  ASSERT_EQ(transport2_->GetNextAddress(&address2, next_address_cb2),
            PP_OK_COMPLETIONPENDING);
  ASSERT_EQ(next_address_cb1.WaitForResult(), PP_OK);
  ASSERT_EQ(next_address_cb2.WaitForResult(), PP_OK);
  ASSERT_EQ(transport1_->GetNextAddress(&address1, next_address_cb1), PP_OK);
  ASSERT_EQ(transport2_->GetNextAddress(&address2, next_address_cb2), PP_OK);

  ASSERT_EQ(transport1_->ReceiveRemoteAddress(address2), PP_OK);
  ASSERT_EQ(transport2_->ReceiveRemoteAddress(address1), PP_OK);


  ASSERT_EQ(connect_cb1.WaitForResult(), PP_OK);
  ASSERT_EQ(connect_cb2.WaitForResult(), PP_OK);

  ASSERT_TRUE(transport1_->IsWritable());
  ASSERT_TRUE(transport2_->IsWritable());

  PASS();
}

std::string TestTransport::Clean() {
  delete transport1_;
  transport1_ = NULL;
  delete transport2_;
  transport2_ = NULL;

  PASS();
}

std::string TestTransport::TestCreate() {
  RUN_SUBTEST(InitTargets(PP_TRANSPORTTYPE_DATAGRAM));

  Clean();

  PASS();
}

std::string TestTransport::TestSetProperty() {
  RUN_SUBTEST(InitTargets(PP_TRANSPORTTYPE_STREAM));

  // Try settings STUN and Relay properties.
  ASSERT_EQ(transport1_->SetProperty(
      PP_TRANSPORTPROPERTY_STUN_SERVER,
      pp::Var("stun.example.com:19302")), PP_OK);

  ASSERT_EQ(transport1_->SetProperty(
      PP_TRANSPORTPROPERTY_RELAY_SERVER,
      pp::Var("ralay.example.com:80")), PP_OK);

  ASSERT_EQ(transport1_->SetProperty(
      PP_TRANSPORTPROPERTY_RELAY_USERNAME,
      pp::Var("USERNAME")), PP_OK);
  ASSERT_EQ(transport1_->SetProperty(
      PP_TRANSPORTPROPERTY_RELAY_PASSWORD,
      pp::Var("PASSWORD")), PP_OK);

  // Try changing TCP window size.
  ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_RECEIVE_WINDOW,
                                     pp::Var(65536)), PP_OK);
  ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_RECEIVE_WINDOW,
                                     pp::Var(10000000)), PP_ERROR_BADARGUMENT);

  ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_SEND_WINDOW,
                                     pp::Var(65536)), PP_OK);
  ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_SEND_WINDOW,
                                     pp::Var(10000000)), PP_ERROR_BADARGUMENT);

  ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_NO_DELAY,
                                     pp::Var(true)), PP_OK);

  ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_ACK_DELAY,
                                     pp::Var(10)), PP_OK);
  ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_ACK_DELAY,
                                     pp::Var(10000)), PP_ERROR_BADARGUMENT);

  TestCompletionCallback connect_cb(instance_->pp_instance());
  ASSERT_EQ(transport1_->Connect(connect_cb), PP_OK_COMPLETIONPENDING);

  // SetProperty() should fail after we've connected.
  ASSERT_EQ(transport1_->SetProperty(
      PP_TRANSPORTPROPERTY_STUN_SERVER,
      pp::Var("stun.example.com:31323")), PP_ERROR_FAILED);

  Clean();
  ASSERT_EQ(connect_cb.WaitForResult(), PP_ERROR_ABORTED);

  PASS();
}

std::string TestTransport::TestConnect() {
  RUN_SUBTEST(InitTargets(PP_TRANSPORTTYPE_DATAGRAM));
  RUN_SUBTEST(Connect());

  Clean();

  PASS();
}

// Creating datagram connection and try sending data over it. Verify
// that at least some packets are received (some packets may be lost).
std::string TestTransport::TestSendDataUdp() {
  RUN_SUBTEST(InitTargets(PP_TRANSPORTTYPE_DATAGRAM));
  RUN_SUBTEST(Connect());

  const int kNumPackets = 100;
  const int kSendBufferSize = 1200;
  const int kUdpWaitTimeMs = 1000;  // 1 second.

  TestCompletionCallback done_cb(instance_->pp_instance());
  StreamReader reader(transport1_, kSendBufferSize * kNumPackets, done_cb);

  std::map<int, std::vector<char> > sent_packets;
  for (int i = 0; i < kNumPackets; ++i) {
    std::vector<char> send_buffer(kSendBufferSize);
    for (size_t j = 0; j < send_buffer.size(); ++j) {
      send_buffer[j] = rand() % 256;
    }
    // Put packet index in the beginning.
    memcpy(&send_buffer[0], &i, sizeof(i));

    TestCompletionCallback send_cb(instance_->pp_instance(), force_async_);
    int32_t result = transport2_->Send(&send_buffer[0], send_buffer.size(),
                                       send_cb);
    if (force_async_) {
      ASSERT_EQ(result, PP_OK_COMPLETIONPENDING);
      ASSERT_EQ(send_cb.WaitForResult(), static_cast<int>(send_buffer.size()));
    } else {
      ASSERT_EQ(result, static_cast<int>(send_buffer.size()));
    }
    sent_packets[i] = send_buffer;
  }

  // Limit waiting time.
  TestCompletionCallback timeout_cb(instance_->pp_instance());
  pp::Module::Get()->core()->CallOnMainThread(kUdpWaitTimeMs, timeout_cb);
  ASSERT_EQ(timeout_cb.WaitForResult(), PP_OK);

  ASSERT_TRUE(reader.errors().size() == 0);
  ASSERT_TRUE(reader.received().size() > 0);
  for (std::list<std::vector<char> >::const_iterator it =
           reader.received().begin(); it != reader.received().end(); ++it) {
    int index;
    memcpy(&index, &(*it)[0], sizeof(index));
    ASSERT_TRUE(sent_packets[index] == *it);
  }

  Clean();

  PASS();
}

// Creating reliable (TCP-like) connection and try sending data over
// it. Verify that all data is received correctly.
std::string TestTransport::TestSendDataTcp() {
  RUN_SUBTEST(InitTargets(PP_TRANSPORTTYPE_STREAM));
  RUN_SUBTEST(Connect());

  const int kTcpSendSize = 100000;

  TestCompletionCallback done_cb(instance_->pp_instance());
  StreamReader reader(transport1_, kTcpSendSize, done_cb);

  std::vector<char> send_buffer(kTcpSendSize);
  for (size_t j = 0; j < send_buffer.size(); ++j) {
    send_buffer[j] = rand() % 256;
  }

  int pos = 0;
  while (pos < static_cast<int>(send_buffer.size())) {
    TestCompletionCallback send_cb(instance_->pp_instance(), force_async_);
    int result = transport2_->Send(
        &send_buffer[0] + pos, send_buffer.size() - pos, send_cb);
    if (force_async_)
      ASSERT_EQ(result, PP_OK_COMPLETIONPENDING);
    if (result == PP_OK_COMPLETIONPENDING)
      result = send_cb.WaitForResult();
    ASSERT_TRUE(result > 0);
    pos += result;
  }

  ASSERT_EQ(done_cb.WaitForResult(), PP_OK);

  ASSERT_TRUE(reader.errors().size() == 0);

  std::vector<char> received_data;
  for (std::list<std::vector<char> >::const_iterator it =
           reader.received().begin(); it != reader.received().end(); ++it) {
    received_data.insert(received_data.end(), it->begin(), it->end());
  }
  ASSERT_EQ(send_buffer, received_data);

  Clean();

  PASS();
}

std::string TestTransport::TestConnectAndCloseUdp() {
  RUN_SUBTEST(InitTargets(PP_TRANSPORTTYPE_DATAGRAM));
  RUN_SUBTEST(Connect());

  std::vector<char> recv_buffer(kReadBufferSize);
  TestCompletionCallback recv_cb(instance_->pp_instance());
  ASSERT_EQ(
      transport1_->Recv(&recv_buffer[0], recv_buffer.size(), recv_cb),
      PP_OK_COMPLETIONPENDING);

  // Close the transport and verify that callback is aborted.
  ASSERT_EQ(transport1_->Close(), PP_OK);

  ASSERT_EQ(recv_cb.run_count(), 1);
  ASSERT_EQ(recv_cb.result(), PP_ERROR_ABORTED);

  Clean();

  PASS();
}

std::string TestTransport::TestConnectAndCloseTcp() {
  RUN_SUBTEST(InitTargets(PP_TRANSPORTTYPE_STREAM));
  RUN_SUBTEST(Connect());

  std::vector<char> recv_buffer(kReadBufferSize);
  TestCompletionCallback recv_cb(instance_->pp_instance());
  ASSERT_EQ(
      transport1_->Recv(&recv_buffer[0], recv_buffer.size(), recv_cb),
      PP_OK_COMPLETIONPENDING);

  // Close the transport and verify that callback is aborted.
  ASSERT_EQ(transport1_->Close(), PP_OK);

  ASSERT_EQ(recv_cb.run_count(), 1);
  ASSERT_EQ(recv_cb.result(), PP_ERROR_ABORTED);

  Clean();

  PASS();
}