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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <iostream>
#include <vector>
#include "mozilla/Atomics.h"
#include "runnable_utils.h"
#include "nss.h"
#include "pk11pub.h"
extern "C" {
#include "nr_api.h"
#include "nr_socket.h"
#include "transport_addr.h"
#include "ice_ctx.h"
#include "nr_socket_multi_tcp.h"
}
#include "nr_socket_prsock.h"
#include "stunserver.h"
#include "nricectxhandler.h"
#include "nricemediastream.h"
#define GTEST_HAS_RTTI 0
#include "gtest/gtest.h"
#include "gtest_utils.h"
using namespace mozilla;
namespace {
class MultiTcpSocketTest : public MtransportTest {
public:
MultiTcpSocketTest()
:MtransportTest(),
socks(3,nullptr),
readable(false),
ice_ctx_()
{}
void SetUp() {
MtransportTest::SetUp();
ice_ctx_ = NrIceCtxHandler::Create("stun", true);
test_utils_->sts_target()->Dispatch(
WrapRunnableNM(&TestStunTcpServer::GetInstance, AF_INET),
NS_DISPATCH_SYNC);
test_utils_->sts_target()->Dispatch(
WrapRunnableNM(&TestStunTcpServer::GetInstance, AF_INET6),
NS_DISPATCH_SYNC);
}
void TearDown() {
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::Shutdown_s),
NS_DISPATCH_SYNC);
MtransportTest::TearDown();
}
DISALLOW_COPY_ASSIGN(MultiTcpSocketTest);
static void SockReadable(NR_SOCKET s, int how, void *arg) {
MultiTcpSocketTest *obj=static_cast<MultiTcpSocketTest *>(arg);
obj->SetReadable(true);
}
void Shutdown_s() {
ice_ctx_ = nullptr;
for (std::vector<nr_socket *>::iterator it=socks.begin();
it!=socks.end(); ++it) {
nr_socket_destroy(&(*it));
}
}
static uint16_t GetRandomPort() {
uint16_t result;
if (PK11_GenerateRandom((unsigned char*)&result, 2) != SECSuccess) {
MOZ_ASSERT(false);
return 0;
}
return result;
}
static uint16_t EnsureEphemeral(uint16_t port) {
// IANA ephemeral port range (49152 to 65535)
return port | 49152;
}
void Create_s(nr_socket_tcp_type tcp_type, std::string stun_server_addr,
uint16_t stun_server_port, nr_socket **sock) {
nr_transport_addr local;
// Get start of port range for test
static unsigned short port_s = GetRandomPort();
int r;
if (!stun_server_addr.empty()) {
std::vector<NrIceStunServer> stun_servers;
UniquePtr<NrIceStunServer> server(NrIceStunServer::Create(
stun_server_addr, stun_server_port, kNrIceTransportTcp));
stun_servers.push_back(*server);
ASSERT_TRUE(NS_SUCCEEDED(ice_ctx_->ctx()->SetStunServers(stun_servers)));
}
r = 1;
for (int tries=10; tries && r; --tries) {
r = nr_str_port_to_transport_addr(
(char *)"127.0.0.1", EnsureEphemeral(port_s++), IPPROTO_TCP, &local);
ASSERT_EQ(0, r);
r = nr_socket_multi_tcp_create(ice_ctx_->ctx()->ctx(),
&local, tcp_type, 1, 2048, sock);
}
ASSERT_EQ(0, r);
printf("Creating socket on %s\n", local.as_string);
r = nr_socket_multi_tcp_set_readable_cb(*sock,
&MultiTcpSocketTest::SockReadable, this);
ASSERT_EQ(0, r);
}
nr_socket *Create(nr_socket_tcp_type tcp_type,
std::string stun_server_addr = "",
uint16_t stun_server_port = 0) {
nr_socket *sock=nullptr;
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::Create_s, tcp_type,
stun_server_addr, stun_server_port, &sock),
NS_DISPATCH_SYNC);
return sock;
}
void Listen_s(nr_socket *sock) {
nr_transport_addr addr;
int r=nr_socket_getaddr(sock, &addr);
ASSERT_EQ(0, r);
printf("Listening on %s\n", addr.as_string);
r = nr_socket_listen(sock, 5);
ASSERT_EQ(0, r);
}
void Listen(nr_socket *sock) {
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::Listen_s, sock),
NS_DISPATCH_SYNC);
}
void Destroy_s(nr_socket *sock) {
int r = nr_socket_destroy(&sock);
ASSERT_EQ(0, r);
}
void Destroy(nr_socket *sock) {
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::Destroy_s, sock),
NS_DISPATCH_SYNC);
}
void Connect_s(nr_socket *from, nr_socket *to) {
nr_transport_addr addr_to;
nr_transport_addr addr_from;
int r=nr_socket_getaddr(to, &addr_to);
ASSERT_EQ(0, r);
r=nr_socket_getaddr(from, &addr_from);
ASSERT_EQ(0, r);
printf("Connecting from %s to %s\n", addr_from.as_string, addr_to.as_string);
r=nr_socket_connect(from, &addr_to);
ASSERT_EQ(0, r);
}
void Connect(nr_socket *from, nr_socket *to) {
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::Connect_s, from, to),
NS_DISPATCH_SYNC);
}
void ConnectSo_s(nr_socket *so1, nr_socket *so2) {
nr_transport_addr addr_so1;
nr_transport_addr addr_so2;
int r=nr_socket_getaddr(so1, &addr_so1);
ASSERT_EQ(0, r);
r=nr_socket_getaddr(so2, &addr_so2);
ASSERT_EQ(0, r);
printf("Connecting SO %s <-> %s\n", addr_so1.as_string, addr_so2.as_string);
r=nr_socket_connect(so1, &addr_so2);
ASSERT_EQ(0, r);
r=nr_socket_connect(so2, &addr_so1);
ASSERT_EQ(0, r);
}
void ConnectSo(nr_socket *from, nr_socket *to) {
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::ConnectSo_s, from, to),
NS_DISPATCH_SYNC);
}
void SendDataToAddress_s(nr_socket *from, nr_transport_addr *to, const char *data,
size_t len) {
nr_transport_addr addr_from;
int r=nr_socket_getaddr(from, &addr_from);
ASSERT_EQ(0, r);
printf("Sending %lu bytes %s -> %s\n", (unsigned long)len,
addr_from.as_string, to->as_string);
r=nr_socket_sendto(from, data, len, 0, to);
ASSERT_EQ(0, r);
}
void SendData(nr_socket *from, nr_transport_addr *to, const char *data, size_t len) {
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::SendDataToAddress_s, from, to, data,
len),
NS_DISPATCH_SYNC);
}
void SendDataToSocket_s(nr_socket *from, nr_socket *to, const char *data,
size_t len) {
nr_transport_addr addr_to;
int r=nr_socket_getaddr(to, &addr_to);
ASSERT_EQ(0, r);
SendDataToAddress_s(from, &addr_to, data, len);
}
void SendData(nr_socket *from, nr_socket *to, const char *data, size_t len) {
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::SendDataToSocket_s, from, to, data,
len),
NS_DISPATCH_SYNC);
}
void RecvDataFromAddress_s(nr_transport_addr *expected_from,
nr_socket *sent_to,
const char *expected_data,
size_t expected_len) {
SetReadable(false);
size_t buflen = expected_len ? expected_len+1 : 100;
char received_data[buflen];
nr_transport_addr addr_to;
nr_transport_addr retaddr;
size_t retlen;
int r=nr_socket_getaddr(sent_to, &addr_to);
ASSERT_EQ(0, r);
printf("Receiving %lu bytes %s <- %s\n", (unsigned long)expected_len,
addr_to.as_string, expected_from->as_string);
r=nr_socket_recvfrom(sent_to, received_data, buflen,
&retlen, 0, &retaddr);
ASSERT_EQ(0, r);
r=nr_transport_addr_cmp(&retaddr, expected_from,
NR_TRANSPORT_ADDR_CMP_MODE_ALL);
ASSERT_EQ(0, r);
// expected_len == 0 means we just expected some data
if (expected_len == 0) {
ASSERT_GT(retlen, 0U);
} else {
ASSERT_EQ(expected_len, retlen);
r=memcmp(expected_data, received_data, retlen);
ASSERT_EQ(0, r);
}
}
void RecvData(nr_transport_addr *expected_from, nr_socket *sent_to,
const char *expected_data = nullptr, size_t expected_len = 0) {
ASSERT_TRUE_WAIT(IsReadable(), 1000);
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::RecvDataFromAddress_s,
expected_from, sent_to, expected_data,
expected_len),
NS_DISPATCH_SYNC);
}
void RecvDataFromSocket_s(nr_socket *expected_from, nr_socket *sent_to,
const char *expected_data, size_t expected_len) {
nr_transport_addr addr_from;
int r=nr_socket_getaddr(expected_from, &addr_from);
ASSERT_EQ(0, r);
RecvDataFromAddress_s(&addr_from, sent_to, expected_data, expected_len);
}
void RecvData(nr_socket *expected_from, nr_socket *sent_to,
const char *expected_data, size_t expected_len) {
ASSERT_TRUE_WAIT(IsReadable(), 1000);
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::RecvDataFromSocket_s,
expected_from, sent_to, expected_data, expected_len),
NS_DISPATCH_SYNC);
}
void RecvDataFailed_s(nr_socket *sent_to, size_t expected_len, int expected_err) {
SetReadable(false);
char received_data[expected_len+1];
nr_transport_addr addr_to;
nr_transport_addr retaddr;
size_t retlen;
int r=nr_socket_getaddr(sent_to, &addr_to);
ASSERT_EQ(0, r);
r=nr_socket_recvfrom(sent_to, received_data, expected_len+1,
&retlen, 0, &retaddr);
ASSERT_EQ(expected_err, r) << "Expecting receive failure " << expected_err
<< " on " << addr_to.as_string;
}
void RecvDataFailed(nr_socket *sent_to, size_t expected_len,
int expected_err) {
ASSERT_TRUE_WAIT(IsReadable(), 1000);
test_utils_->sts_target()->Dispatch(
WrapRunnable(
this, &MultiTcpSocketTest::RecvDataFailed_s, sent_to, expected_len,
expected_err),
NS_DISPATCH_SYNC);
}
void TransferData(nr_socket *from, nr_socket *to, const char *data,
size_t len) {
SendData(from, to, data, len);
RecvData(from, to, data, len);
}
protected:
bool IsReadable() const {
return readable;
}
void SetReadable(bool r) {
readable=r;
}
std::vector<nr_socket *> socks;
Atomic<bool> readable;
RefPtr<NrIceCtxHandler> ice_ctx_;
};
}
TEST_F(MultiTcpSocketTest, TestListen) {
socks[0] = Create(TCP_TYPE_PASSIVE);
Listen(socks[0]);
}
TEST_F(MultiTcpSocketTest, TestConnect) {
socks[0] = Create(TCP_TYPE_PASSIVE);
socks[1] = Create(TCP_TYPE_ACTIVE);
socks[2] = Create(TCP_TYPE_ACTIVE);
Listen(socks[0]);
Connect(socks[1], socks[0]);
Connect(socks[2], socks[0]);
}
TEST_F(MultiTcpSocketTest, TestTransmit) {
const char data[] = "TestTransmit";
socks[0] = Create(TCP_TYPE_ACTIVE);
socks[1] = Create(TCP_TYPE_PASSIVE);
Listen(socks[1]);
Connect(socks[0], socks[1]);
TransferData(socks[0], socks[1], data, sizeof(data));
TransferData(socks[1], socks[0], data, sizeof(data));
}
TEST_F(MultiTcpSocketTest, TestClosePassive) {
const char data[] = "TestClosePassive";
socks[0] = Create(TCP_TYPE_ACTIVE);
socks[1] = Create(TCP_TYPE_PASSIVE);
Listen(socks[1]);
Connect(socks[0], socks[1]);
TransferData(socks[0], socks[1], data, sizeof(data));
TransferData(socks[1], socks[0], data, sizeof(data));
/* We have to destroy as only that calls PR_Close() */
std::cerr << "Destructing socket" << std::endl;
Destroy(socks[1]);
RecvDataFailed(socks[0], sizeof(data), R_EOD);
socks[1] = nullptr;
}
TEST_F(MultiTcpSocketTest, TestCloseActive) {
const char data[] = "TestCloseActive";
socks[0] = Create(TCP_TYPE_ACTIVE);
socks[1] = Create(TCP_TYPE_PASSIVE);
Listen(socks[1]);
Connect(socks[0], socks[1]);
TransferData(socks[0], socks[1], data, sizeof(data));
TransferData(socks[1], socks[0], data, sizeof(data));
/* We have to destroy as only that calls PR_Close() */
std::cerr << "Destructing socket" << std::endl;
Destroy(socks[0]);
RecvDataFailed(socks[1], sizeof(data), R_EOD);
socks[0] = nullptr;
}
TEST_F(MultiTcpSocketTest, TestTwoSendsBeforeReceives) {
const char data1[] = "TestTwoSendsBeforeReceives";
const char data2[] = "2nd data";
socks[0] = Create(TCP_TYPE_ACTIVE);
socks[1] = Create(TCP_TYPE_PASSIVE);
Listen(socks[1]);
Connect(socks[0], socks[1]);
SendData(socks[0], socks[1], data1, sizeof(data1));
SendData(socks[0], socks[1], data2, sizeof(data2));
RecvData(socks[0], socks[1], data1, sizeof(data1));
/* ICE TCP framing turns TCP effectively into datagram mode */
RecvData(socks[0], socks[1], data2, sizeof(data2));
}
TEST_F(MultiTcpSocketTest, TestTwoActiveBidirectionalTransmit) {
const char data1[] = "TestTwoActiveBidirectionalTransmit";
const char data2[] = "ReplyToTheFirstSocket";
const char data3[] = "TestMessageFromTheSecondSocket";
const char data4[] = "ThisIsAReplyToTheSecondSocket";
socks[0] = Create(TCP_TYPE_PASSIVE);
socks[1] = Create(TCP_TYPE_ACTIVE);
socks[2] = Create(TCP_TYPE_ACTIVE);
Listen(socks[0]);
Connect(socks[1], socks[0]);
Connect(socks[2], socks[0]);
TransferData(socks[1], socks[0], data1, sizeof(data1));
TransferData(socks[0], socks[1], data2, sizeof(data2));
TransferData(socks[2], socks[0], data3, sizeof(data3));
TransferData(socks[0], socks[2], data4, sizeof(data4));
}
TEST_F(MultiTcpSocketTest, TestTwoPassiveBidirectionalTransmit) {
const char data1[] = "TestTwoPassiveBidirectionalTransmit";
const char data2[] = "FirstReply";
const char data3[] = "TestTwoPassiveBidirectionalTransmitToTheSecondSock";
const char data4[] = "SecondReply";
socks[0] = Create(TCP_TYPE_PASSIVE);
socks[1] = Create(TCP_TYPE_PASSIVE);
socks[2] = Create(TCP_TYPE_ACTIVE);
Listen(socks[0]);
Listen(socks[1]);
Connect(socks[2], socks[0]);
Connect(socks[2], socks[1]);
TransferData(socks[2], socks[0], data1, sizeof(data1));
TransferData(socks[0], socks[2], data2, sizeof(data2));
TransferData(socks[2], socks[1], data3, sizeof(data3));
TransferData(socks[1], socks[2], data4, sizeof(data4));
}
TEST_F(MultiTcpSocketTest, TestActivePassiveWithStunServerMockup) {
/* Fake STUN message able to pass the nr_is_stun_msg check
used in nr_socket_buffered_stun */
const char stunMessage[] = {
'\x00', '\x01', '\x00', '\x04', '\x21', '\x12', '\xa4', '\x42',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x0c', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x1c', '\xed', '\xca', '\xfe'
};
const char data[] = "TestActivePassiveWithStunServerMockup";
nr_transport_addr stun_srv_addr;
std::string stun_addr;
uint16_t stun_port;
stun_addr = TestStunTcpServer::GetInstance(AF_INET)->addr();
stun_port = TestStunTcpServer::GetInstance(AF_INET)->port();
int r = nr_str_port_to_transport_addr(stun_addr.c_str(), stun_port, IPPROTO_TCP, &stun_srv_addr);
ASSERT_EQ(0, r);
socks[0] = Create(TCP_TYPE_PASSIVE, stun_addr, stun_port);
Listen(socks[0]);
socks[1] = Create(TCP_TYPE_ACTIVE, stun_addr, stun_port);
/* Send a fake STUN request and expect a STUN error response */
SendData(socks[0], &stun_srv_addr, stunMessage, sizeof(stunMessage));
RecvData(&stun_srv_addr, socks[0]);
Connect(socks[1], socks[0]);
TransferData(socks[1], socks[0], data, sizeof(data));
TransferData(socks[0], socks[1], data, sizeof(data));
}
TEST_F(MultiTcpSocketTest, TestConnectTwoSo) {
socks[0] = Create(TCP_TYPE_SO);
socks[1] = Create(TCP_TYPE_SO);
ConnectSo(socks[0], socks[1]);
}
// test works on localhost only with delay applied:
// tc qdisc add dev lo root netem delay 5ms
TEST_F(MultiTcpSocketTest, DISABLED_TestTwoSoBidirectionalTransmit) {
const char data[] = "TestTwoSoBidirectionalTransmit";
socks[0] = Create(TCP_TYPE_SO);
socks[1] = Create(TCP_TYPE_SO);
ConnectSo(socks[0], socks[1]);
TransferData(socks[0], socks[1], data, sizeof(data));
TransferData(socks[1], socks[0], data, sizeof(data));
}
TEST_F(MultiTcpSocketTest, TestBigData) {
char buf1[2048];
char buf2[1024];
for(unsigned i=0; i<sizeof(buf1); ++i) {
buf1[i]=i&0xff;
}
for(unsigned i=0; i<sizeof(buf2); ++i) {
buf2[i]=(i+0x80)&0xff;
}
socks[0] = Create(TCP_TYPE_ACTIVE);
socks[1] = Create(TCP_TYPE_PASSIVE);
Listen(socks[1]);
Connect(socks[0], socks[1]);
TransferData(socks[0], socks[1], buf1, sizeof(buf1));
TransferData(socks[0], socks[1], buf2, sizeof(buf2));
// opposite dir
SendData(socks[1], socks[0], buf2, sizeof(buf2));
SendData(socks[1], socks[0], buf1, sizeof(buf1));
RecvData(socks[1], socks[0], buf2, sizeof(buf2));
RecvData(socks[1], socks[0], buf1, sizeof(buf1));
}
|