3
* Copyright 2004 Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/base/basicpacketsocketfactory.h"
29
#include "talk/base/gunit.h"
30
#include "talk/base/helpers.h"
31
#include "talk/base/host.h"
32
#include "talk/base/logging.h"
33
#include "talk/base/natserver.h"
34
#include "talk/base/natsocketfactory.h"
35
#include "talk/base/physicalsocketserver.h"
36
#include "talk/base/scoped_ptr.h"
37
#include "talk/base/socketaddress.h"
38
#include "talk/base/stringutils.h"
39
#include "talk/base/thread.h"
40
#include "talk/base/virtualsocketserver.h"
41
#include "talk/p2p/base/relayport.h"
42
#include "talk/p2p/base/stunport.h"
43
#include "talk/p2p/base/tcpport.h"
44
#include "talk/p2p/base/udpport.h"
45
#include "talk/p2p/base/teststunserver.h"
46
#include "talk/p2p/base/testrelayserver.h"
48
using talk_base::AsyncPacketSocket;
49
using talk_base::NATType;
50
using talk_base::NAT_OPEN_CONE;
51
using talk_base::NAT_ADDR_RESTRICTED;
52
using talk_base::NAT_PORT_RESTRICTED;
53
using talk_base::NAT_SYMMETRIC;
54
using talk_base::PacketSocketFactory;
55
using talk_base::scoped_ptr;
56
using talk_base::Socket;
57
using talk_base::SocketAddress;
58
using namespace cricket;
60
static const int kTimeout = 1000;
61
static const SocketAddress kLocalAddr1 = SocketAddress("192.168.1.2", 0);
62
static const SocketAddress kLocalAddr2 = SocketAddress("192.168.1.3", 0);
63
static const SocketAddress kNatAddr1 = SocketAddress("77.77.77.77",
64
talk_base::NAT_SERVER_PORT);
65
static const SocketAddress kNatAddr2 = SocketAddress("88.88.88.88",
66
talk_base::NAT_SERVER_PORT);
67
static const SocketAddress kStunAddr = SocketAddress("99.99.99.1",
69
static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
70
static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
71
static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
72
static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
73
static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
74
static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
76
static Candidate GetCandidate(Port* port) {
77
assert(port->candidates().size() == 1);
78
return port->candidates()[0];
81
static SocketAddress GetAddress(Port* port) {
82
return GetCandidate(port).address();
85
class TestChannel : public sigslot::has_slots<> {
87
TestChannel(Port* p1, Port* p2)
88
: src_(p1), dst_(p2), address_count_(0), conn_(NULL),
89
remote_request_(NULL) {
90
src_->SignalAddressReady.connect(this, &TestChannel::OnAddressReady);
91
src_->SignalUnknownAddress.connect(this, &TestChannel::OnUnknownAddress);
94
int address_count() { return address_count_; }
95
Connection* conn() { return conn_; }
96
const SocketAddress& remote_address() { return remote_address_; }
97
const std::string remote_fragment() { return remote_frag_; }
100
src_->PrepareAddress();
102
void CreateConnection() {
103
conn_ = src_->CreateConnection(GetCandidate(dst_), Port::ORIGIN_MESSAGE);
105
void AcceptConnection() {
106
ASSERT_TRUE(remote_request_ != NULL);
107
Candidate c = GetCandidate(dst_);
108
c.set_address(remote_address_);
109
conn_ = src_->CreateConnection(c, Port::ORIGIN_MESSAGE);
110
src_->SendBindingResponse(remote_request_, remote_address_);
111
delete remote_request_;
117
conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed);
121
void OnAddressReady(Port* port) {
125
void OnUnknownAddress(Port* port, const SocketAddress& addr,
126
StunMessage* msg, const std::string& rf,
127
bool /*port_muxed*/) {
128
ASSERT_EQ(src_.get(), port);
129
if (!remote_address_.IsAny()) {
130
ASSERT_EQ(remote_address_, addr);
131
delete remote_request_;
133
remote_address_ = addr;
134
remote_request_ = msg;
138
void OnDestroyed(Connection* conn) {
139
ASSERT_EQ(conn_, conn);
144
talk_base::Thread* thread_;
145
talk_base::scoped_ptr<Port> src_;
150
SocketAddress remote_address_;
151
StunMessage* remote_request_;
152
std::string remote_frag_;
155
class PortTest : public testing::Test {
158
: main_(talk_base::Thread::Current()),
159
pss_(new talk_base::PhysicalSocketServer),
160
ss_(new talk_base::VirtualSocketServer(pss_.get())),
161
ss_scope_(ss_.get()),
162
network_("unittest", "unittest", talk_base::IPAddress(INADDR_ANY), 32),
163
socket_factory_(talk_base::Thread::Current()),
164
nat_factory1_(ss_.get(), kNatAddr1),
165
nat_factory2_(ss_.get(), kNatAddr2),
166
nat_socket_factory1_(&nat_factory1_),
167
nat_socket_factory2_(&nat_factory2_),
168
stun_server_(main_, kStunAddr),
169
relay_server_(main_, kRelayUdpIntAddr, kRelayUdpExtAddr,
170
kRelayTcpIntAddr, kRelayTcpExtAddr,
171
kRelaySslTcpIntAddr, kRelaySslTcpExtAddr) {
172
network_.AddIP(talk_base::IPAddress(INADDR_ANY));
176
static void SetUpTestCase() {
177
// Ensure the RNG is inited.
178
talk_base::InitRandom(NULL, 0);
181
void TestLocalToLocal() {
182
UDPPort* port1 = CreateUdpPort(kLocalAddr1);
183
UDPPort* port2 = CreateUdpPort(kLocalAddr2);
184
TestConnectivity("udp", port1, "udp", port2, true, true, true, true);
186
void TestLocalToStun(NATType type) {
187
UDPPort* port1 = CreateUdpPort(kLocalAddr1);
188
nat_server2_.reset(CreateNatServer(kNatAddr2, type));
189
StunPort* port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_);
190
TestConnectivity("udp", port1, StunName(type), port2,
191
type == NAT_OPEN_CONE, true, type != NAT_SYMMETRIC, true);
193
void TestLocalToRelay(ProtocolType proto) {
194
UDPPort* port1 = CreateUdpPort(kLocalAddr1);
195
RelayPort* port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_UDP);
196
TestConnectivity("udp", port1, RelayName(proto), port2,
197
true, true, true, true);
199
void TestStunToLocal(NATType type) {
200
nat_server1_.reset(CreateNatServer(kNatAddr1, type));
201
StunPort* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
202
UDPPort* port2 = CreateUdpPort(kLocalAddr2);
203
TestConnectivity(StunName(type), port1, "udp", port2,
204
true, type != NAT_SYMMETRIC, true, true);
206
void TestStunToStun(NATType type1, NATType type2) {
207
nat_server1_.reset(CreateNatServer(kNatAddr1, type1));
208
StunPort* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
209
nat_server2_.reset(CreateNatServer(kNatAddr2, type2));
210
StunPort* port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_);
211
TestConnectivity(StunName(type1), port1, StunName(type2), port2,
212
type2 == NAT_OPEN_CONE,
213
type1 != NAT_SYMMETRIC, type2 != NAT_SYMMETRIC,
214
type1 + type2 < (NAT_PORT_RESTRICTED + NAT_SYMMETRIC));
216
void TestStunToRelay(NATType type, ProtocolType proto) {
217
nat_server1_.reset(CreateNatServer(kNatAddr1, type));
218
StunPort* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
219
RelayPort* port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_UDP);
220
TestConnectivity(StunName(type), port1, RelayName(proto), port2,
221
true, type != NAT_SYMMETRIC, true, true);
223
void TestTcpToTcp() {
224
TCPPort* port1 = CreateTcpPort(kLocalAddr1);
225
TCPPort* port2 = CreateTcpPort(kLocalAddr2);
226
TestConnectivity("tcp", port1, "tcp", port2, true, false, true, true);
228
void TestTcpToRelay(ProtocolType proto) {
229
TCPPort* port1 = CreateTcpPort(kLocalAddr1);
230
RelayPort* port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_TCP);
231
TestConnectivity("tcp", port1, RelayName(proto), port2,
232
true, false, true, true);
234
void TestSslTcpToRelay(ProtocolType proto) {
235
TCPPort* port1 = CreateTcpPort(kLocalAddr1);
236
RelayPort* port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_SSLTCP);
237
TestConnectivity("ssltcp", port1, RelayName(proto), port2,
238
true, false, true, true);
241
// helpers for above functions
242
UDPPort* CreateUdpPort(const SocketAddress& addr) {
243
return CreateUdpPort(addr, &socket_factory_);
245
UDPPort* CreateUdpPort(const SocketAddress& addr,
246
PacketSocketFactory* socket_factory) {
247
return UDPPort::Create(main_, socket_factory, &network_,
248
addr.ipaddr(), 0, 0);
250
TCPPort* CreateTcpPort(const SocketAddress& addr) {
251
return CreateTcpPort(addr, &socket_factory_);
253
TCPPort* CreateTcpPort(const SocketAddress& addr,
254
PacketSocketFactory* socket_factory) {
255
return TCPPort::Create(main_, socket_factory, &network_,
256
addr.ipaddr(), 0, 0, true);
258
StunPort* CreateStunPort(const SocketAddress& addr,
259
talk_base::PacketSocketFactory* factory) {
260
return StunPort::Create(main_, factory, &network_,
261
addr.ipaddr(), 0, 0, kStunAddr);
263
RelayPort* CreateRelayPort(const SocketAddress& addr,
264
ProtocolType int_proto, ProtocolType ext_proto) {
265
std::string user = talk_base::CreateRandomString(16);
266
std::string pass = talk_base::CreateRandomString(16);
267
RelayPort* port = RelayPort::Create(main_, &socket_factory_, &network_,
268
addr.ipaddr(), 0, 0, user, pass, "");
269
SocketAddress addrs[] =
270
{ kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr };
271
port->AddServerAddress(ProtocolAddress(addrs[int_proto], int_proto));
272
// TODO: Add an external address for ext_proto, so that the
273
// other side can connect to this port using a non-UDP protocol.
276
talk_base::NATServer* CreateNatServer(const SocketAddress& addr,
277
talk_base::NATType type) {
278
return new talk_base::NATServer(type, ss_.get(), addr, ss_.get(), addr);
280
static const char* StunName(NATType type) {
282
case NAT_OPEN_CONE: return "stun(open cone)";
283
case NAT_ADDR_RESTRICTED: return "stun(addr restricted)";
284
case NAT_PORT_RESTRICTED: return "stun(port restricted)";
285
case NAT_SYMMETRIC: return "stun(symmetric)";
286
default: return "stun(?)";
289
static const char* RelayName(ProtocolType type) {
291
case PROTO_UDP: return "relay(udp)";
292
case PROTO_TCP: return "relay(tcp)";
293
case PROTO_SSLTCP: return "relay(ssltcp)";
294
default: return "relay(?)";
298
// this does all the work
299
void TestConnectivity(const char* name1, Port* port1,
300
const char* name2, Port* port2,
301
bool accept, bool same_addr1,
302
bool same_addr2, bool possible);
305
talk_base::Thread* main_;
306
talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_;
307
talk_base::scoped_ptr<talk_base::VirtualSocketServer> ss_;
308
talk_base::SocketServerScope ss_scope_;
309
talk_base::Network network_;
310
talk_base::BasicPacketSocketFactory socket_factory_;
311
talk_base::scoped_ptr<talk_base::NATServer> nat_server1_;
312
talk_base::scoped_ptr<talk_base::NATServer> nat_server2_;
313
talk_base::NATSocketFactory nat_factory1_;
314
talk_base::NATSocketFactory nat_factory2_;
315
talk_base::BasicPacketSocketFactory nat_socket_factory1_;
316
talk_base::BasicPacketSocketFactory nat_socket_factory2_;
317
TestStunServer stun_server_;
318
TestRelayServer relay_server_;
321
void PortTest::TestConnectivity(const char* name1, Port* port1,
322
const char* name2, Port* port2,
323
bool accept, bool same_addr1,
324
bool same_addr2, bool possible) {
325
LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": ";
326
port1->set_name("src");
327
port2->set_name("dst");
330
TestChannel ch1(port1, port2);
331
TestChannel ch2(port2, port1);
332
EXPECT_EQ(0, ch1.address_count());
333
EXPECT_EQ(0, ch2.address_count());
335
// Acquire addresses.
338
ASSERT_EQ_WAIT(1, ch1.address_count(), kTimeout);
339
ASSERT_EQ_WAIT(1, ch2.address_count(), kTimeout);
341
// Send a ping from src to dst. This may or may not make it.
342
ch1.CreateConnection();
343
ASSERT_TRUE(ch1.conn() != NULL);
344
EXPECT_TRUE_WAIT(ch1.conn()->connected(), kTimeout); // for TCP connect
346
WAIT(!ch2.remote_address().IsAny(), kTimeout);
349
// We are able to send a ping from src to dst. This is the case when
350
// sending to UDP ports and cone NATs.
351
EXPECT_TRUE(ch1.remote_address().IsAny());
352
EXPECT_EQ(ch2.remote_fragment(), port1->username_fragment());
354
// Ensure the ping came from the same address used for src.
355
// This is the case unless the source NAT was symmetric.
356
if (same_addr1) EXPECT_EQ(ch2.remote_address(), GetAddress(port1));
357
EXPECT_TRUE(same_addr2);
359
// Send a ping from dst to src.
360
ch2.AcceptConnection();
361
ASSERT_TRUE(ch2.conn() != NULL);
363
EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2.conn()->write_state(),
366
// We can't send a ping from src to dst, so flip it around. This will happen
367
// when the destination NAT is addr/port restricted or symmetric.
368
EXPECT_TRUE(ch1.remote_address().IsAny());
369
EXPECT_TRUE(ch2.remote_address().IsAny());
371
// Send a ping from dst to src. Again, this may or may not make it.
372
ch2.CreateConnection();
373
ASSERT_TRUE(ch2.conn() != NULL);
375
WAIT(ch2.conn()->write_state() == Connection::STATE_WRITABLE, kTimeout);
377
if (same_addr1 && same_addr2) {
378
// The new ping got back to the source.
379
EXPECT_EQ(Connection::STATE_READABLE, ch1.conn()->read_state());
380
EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state());
382
// First connection may not be writable if the first ping did not get
383
// through. So we will have to do another.
384
if (ch1.conn()->write_state() == Connection::STATE_WRITE_CONNECT) {
386
EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
389
} else if (!same_addr1 && possible) {
390
// The new ping went to the candidate address, but that address was bad.
391
// This will happen when the source NAT is symmetric.
392
EXPECT_TRUE(ch1.remote_address().IsAny());
393
EXPECT_TRUE(ch2.remote_address().IsAny());
395
// However, since we have now sent a ping to the source IP, we should be
396
// able to get a ping from it. This gives us the real source address.
398
EXPECT_TRUE_WAIT(!ch2.remote_address().IsAny(), kTimeout);
399
EXPECT_EQ(Connection::STATE_READ_TIMEOUT, ch2.conn()->read_state());
400
EXPECT_TRUE(ch1.remote_address().IsAny());
402
// Pick up the actual address and establish the connection.
403
ch2.AcceptConnection();
404
ASSERT_TRUE(ch2.conn() != NULL);
406
EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2.conn()->write_state(),
408
} else if (!same_addr2 && possible) {
409
// The new ping came in, but from an unexpected address. This will happen
410
// when the destination NAT is symmetric.
411
EXPECT_FALSE(ch1.remote_address().IsAny());
412
EXPECT_EQ(Connection::STATE_READ_TIMEOUT, ch1.conn()->read_state());
414
// Update our address and complete the connection.
415
ch1.AcceptConnection();
417
EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
419
} else { // (!possible)
420
// There should be s no way for the pings to reach each other. Check it.
421
EXPECT_TRUE(ch1.remote_address().IsAny());
422
EXPECT_TRUE(ch2.remote_address().IsAny());
424
WAIT(!ch2.remote_address().IsAny(), kTimeout);
425
EXPECT_TRUE(ch1.remote_address().IsAny());
426
EXPECT_TRUE(ch2.remote_address().IsAny());
430
// Everything should be good, unless we know the situation is impossible.
431
ASSERT_TRUE(ch1.conn() != NULL);
432
ASSERT_TRUE(ch2.conn() != NULL);
434
EXPECT_EQ(Connection::STATE_READABLE, ch1.conn()->read_state());
435
EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state());
436
EXPECT_EQ(Connection::STATE_READABLE, ch2.conn()->read_state());
437
EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state());
439
EXPECT_NE(Connection::STATE_READABLE, ch1.conn()->read_state());
440
EXPECT_NE(Connection::STATE_WRITABLE, ch1.conn()->write_state());
441
EXPECT_NE(Connection::STATE_READABLE, ch2.conn()->read_state());
442
EXPECT_NE(Connection::STATE_WRITABLE, ch2.conn()->write_state());
445
// Tear down and ensure that goes smoothly.
448
EXPECT_TRUE_WAIT(ch1.conn() == NULL, kTimeout);
449
EXPECT_TRUE_WAIT(ch2.conn() == NULL, kTimeout);
452
class FakePacketSocketFactory : public talk_base::PacketSocketFactory {
454
FakePacketSocketFactory()
455
: next_udp_socket_(NULL),
456
next_server_tcp_socket_(NULL),
457
next_client_tcp_socket_(NULL) {
459
virtual ~FakePacketSocketFactory() { }
461
virtual AsyncPacketSocket* CreateUdpSocket(
462
const SocketAddress& address, int min_port, int max_port) {
463
EXPECT_TRUE(next_udp_socket_ != NULL);
464
AsyncPacketSocket* result = next_udp_socket_;
465
next_udp_socket_ = NULL;
469
virtual AsyncPacketSocket* CreateServerTcpSocket(
470
const SocketAddress& local_address, int min_port, int max_port,
472
EXPECT_TRUE(next_server_tcp_socket_ != NULL);
473
AsyncPacketSocket* result = next_server_tcp_socket_;
474
next_server_tcp_socket_ = NULL;
478
// TODO: |proxy_info| and |user_agent| should be set
479
// per-factory and not when socket is created.
480
virtual AsyncPacketSocket* CreateClientTcpSocket(
481
const SocketAddress& local_address, const SocketAddress& remote_address,
482
const talk_base::ProxyInfo& proxy_info,
483
const std::string& user_agent, bool ssl) {
484
EXPECT_TRUE(next_client_tcp_socket_ != NULL);
485
AsyncPacketSocket* result = next_client_tcp_socket_;
486
next_client_tcp_socket_ = NULL;
490
void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) {
491
next_udp_socket_ = next_udp_socket;
493
void set_next_server_tcp_socket(AsyncPacketSocket* next_server_tcp_socket) {
494
next_server_tcp_socket_ = next_server_tcp_socket;
496
void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) {
497
next_client_tcp_socket_ = next_client_tcp_socket;
501
AsyncPacketSocket* next_udp_socket_;
502
AsyncPacketSocket* next_server_tcp_socket_;
503
AsyncPacketSocket* next_client_tcp_socket_;
506
class FakeAsyncPacketSocket : public AsyncPacketSocket {
508
// Returns current local address. Address may be set to NULL if the
509
// socket is not bound yet (GetState() returns STATE_BINDING).
510
virtual SocketAddress GetLocalAddress() const {
511
return SocketAddress();
514
// Returns remote address. Returns zeroes if this is not a client TCP socket.
515
virtual SocketAddress GetRemoteAddress() const {
516
return SocketAddress();
520
virtual int Send(const void *pv, size_t cb) {
523
virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) {
526
virtual int Close() {
530
virtual State GetState() const { return state_; }
531
virtual int GetOption(Socket::Option opt, int* value) { return 0; }
532
virtual int SetOption(Socket::Option opt, int value) { return 0; }
533
virtual int GetError() const { return 0; }
534
virtual void SetError(int error) { }
536
void set_state(State state) { state_ = state; }
543
TEST_F(PortTest, TestLocalToLocal) {
547
TEST_F(PortTest, TestLocalToConeNat) {
548
TestLocalToStun(NAT_OPEN_CONE);
551
TEST_F(PortTest, TestLocalToARNat) {
552
TestLocalToStun(NAT_ADDR_RESTRICTED);
555
TEST_F(PortTest, TestLocalToPRNat) {
556
TestLocalToStun(NAT_PORT_RESTRICTED);
559
TEST_F(PortTest, TestLocalToSymNat) {
560
TestLocalToStun(NAT_SYMMETRIC);
563
TEST_F(PortTest, TestLocalToRelay) {
564
TestLocalToRelay(PROTO_UDP);
567
TEST_F(PortTest, TestLocalToTcpRelay) {
568
TestLocalToRelay(PROTO_TCP);
571
TEST_F(PortTest, TestLocalToSslTcpRelay) {
572
TestLocalToRelay(PROTO_SSLTCP);
576
TEST_F(PortTest, TestConeNatToLocal) {
577
TestStunToLocal(NAT_OPEN_CONE);
580
TEST_F(PortTest, TestConeNatToConeNat) {
581
TestStunToStun(NAT_OPEN_CONE, NAT_OPEN_CONE);
584
TEST_F(PortTest, TestConeNatToARNat) {
585
TestStunToStun(NAT_OPEN_CONE, NAT_ADDR_RESTRICTED);
588
TEST_F(PortTest, TestConeNatToPRNat) {
589
TestStunToStun(NAT_OPEN_CONE, NAT_PORT_RESTRICTED);
592
TEST_F(PortTest, TestConeNatToSymNat) {
593
TestStunToStun(NAT_OPEN_CONE, NAT_SYMMETRIC);
596
TEST_F(PortTest, TestConeNatToRelay) {
597
TestStunToRelay(NAT_OPEN_CONE, PROTO_UDP);
600
TEST_F(PortTest, TestConeNatToTcpRelay) {
601
TestStunToRelay(NAT_OPEN_CONE, PROTO_TCP);
604
// Address-restricted NAT -> XXXX
605
TEST_F(PortTest, TestARNatToLocal) {
606
TestStunToLocal(NAT_ADDR_RESTRICTED);
609
TEST_F(PortTest, TestARNatToConeNat) {
610
TestStunToStun(NAT_ADDR_RESTRICTED, NAT_OPEN_CONE);
613
TEST_F(PortTest, TestARNatToARNat) {
614
TestStunToStun(NAT_ADDR_RESTRICTED, NAT_ADDR_RESTRICTED);
617
TEST_F(PortTest, TestARNatToPRNat) {
618
TestStunToStun(NAT_ADDR_RESTRICTED, NAT_PORT_RESTRICTED);
621
TEST_F(PortTest, TestARNatToSymNat) {
622
TestStunToStun(NAT_ADDR_RESTRICTED, NAT_SYMMETRIC);
625
TEST_F(PortTest, TestARNatToRelay) {
626
TestStunToRelay(NAT_ADDR_RESTRICTED, PROTO_UDP);
629
TEST_F(PortTest, TestARNATNatToTcpRelay) {
630
TestStunToRelay(NAT_ADDR_RESTRICTED, PROTO_TCP);
633
// Port-restricted NAT -> XXXX
634
TEST_F(PortTest, TestPRNatToLocal) {
635
TestStunToLocal(NAT_PORT_RESTRICTED);
638
TEST_F(PortTest, TestPRNatToConeNat) {
639
TestStunToStun(NAT_PORT_RESTRICTED, NAT_OPEN_CONE);
642
TEST_F(PortTest, TestPRNatToARNat) {
643
TestStunToStun(NAT_PORT_RESTRICTED, NAT_ADDR_RESTRICTED);
646
TEST_F(PortTest, TestPRNatToPRNat) {
647
TestStunToStun(NAT_PORT_RESTRICTED, NAT_PORT_RESTRICTED);
650
TEST_F(PortTest, TestPRNatToSymNat) {
652
TestStunToStun(NAT_PORT_RESTRICTED, NAT_SYMMETRIC);
655
TEST_F(PortTest, TestPRNatToRelay) {
656
TestStunToRelay(NAT_PORT_RESTRICTED, PROTO_UDP);
659
TEST_F(PortTest, TestPRNatToTcpRelay) {
660
TestStunToRelay(NAT_PORT_RESTRICTED, PROTO_TCP);
663
// Symmetric NAT -> XXXX
664
TEST_F(PortTest, TestSymNatToLocal) {
665
TestStunToLocal(NAT_SYMMETRIC);
668
TEST_F(PortTest, TestSymNatToConeNat) {
669
TestStunToStun(NAT_SYMMETRIC, NAT_OPEN_CONE);
672
TEST_F(PortTest, TestSymNatToARNat) {
673
TestStunToStun(NAT_SYMMETRIC, NAT_ADDR_RESTRICTED);
676
TEST_F(PortTest, TestSymNatToPRNat) {
678
TestStunToStun(NAT_SYMMETRIC, NAT_PORT_RESTRICTED);
681
TEST_F(PortTest, TestSymNatToSymNat) {
683
TestStunToStun(NAT_SYMMETRIC, NAT_SYMMETRIC);
686
TEST_F(PortTest, TestSymNatToRelay) {
687
TestStunToRelay(NAT_SYMMETRIC, PROTO_UDP);
690
TEST_F(PortTest, TestSymNatToTcpRelay) {
691
TestStunToRelay(NAT_SYMMETRIC, PROTO_TCP);
694
// Outbound TCP -> XXXX
695
TEST_F(PortTest, TestTcpToTcp) {
699
/* TODO: Enable these once testrelayserver can accept external TCP.
700
TEST_F(PortTest, TestTcpToTcpRelay) {
701
TestTcpToRelay(PROTO_TCP);
704
TEST_F(PortTest, TestTcpToSslTcpRelay) {
705
TestTcpToRelay(PROTO_SSLTCP);
709
// Outbound SSLTCP -> XXXX
710
/* TODO: Enable these once testrelayserver can accept external SSL.
711
TEST_F(PortTest, TestSslTcpToTcpRelay) {
712
TestSslTcpToRelay(PROTO_TCP);
715
TEST_F(PortTest, TestSslTcpToSslTcpRelay) {
716
TestSslTcpToRelay(PROTO_SSLTCP);
720
TEST_F(PortTest, TestTcpNoDelay) {
721
TCPPort* port1 = CreateTcpPort(kLocalAddr1);
722
int option_value = -1;
723
int success = port1->GetOption(talk_base::Socket::OPT_NODELAY,
725
ASSERT_EQ(0, success); // GetOption() should complete successfully w/ 0
726
ASSERT_EQ(1, option_value);
730
TEST_F(PortTest, TestDelayedBindingUdp) {
731
FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket();
732
FakePacketSocketFactory socket_factory;
734
socket_factory.set_next_udp_socket(socket);
735
scoped_ptr<UDPPort> port(
736
CreateUdpPort(kLocalAddr1, &socket_factory));
738
socket->set_state(AsyncPacketSocket::STATE_BINDING);
739
port->PrepareAddress();
741
EXPECT_EQ(0U, port->candidates().size());
742
socket->SignalAddressReady(socket, kLocalAddr2);
744
EXPECT_EQ(1U, port->candidates().size());
747
TEST_F(PortTest, TestDelayedBindingTcp) {
748
FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket();
749
FakePacketSocketFactory socket_factory;
751
socket_factory.set_next_server_tcp_socket(socket);
752
scoped_ptr<TCPPort> port(
753
CreateTcpPort(kLocalAddr1, &socket_factory));
755
socket->set_state(AsyncPacketSocket::STATE_BINDING);
756
port->PrepareAddress();
758
EXPECT_EQ(0U, port->candidates().size());
759
socket->SignalAddressReady(socket, kLocalAddr2);
761
EXPECT_EQ(1U, port->candidates().size());