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/crc32.h"
29 #include "talk/base/gunit.h"
30 #include "talk/base/helpers.h"
31 #include "talk/base/logging.h"
32 #include "talk/base/natserver.h"
33 #include "talk/base/natsocketfactory.h"
34 #include "talk/base/physicalsocketserver.h"
35 #include "talk/base/scoped_ptr.h"
36 #include "talk/base/socketaddress.h"
37 #include "talk/base/ssladapter.h"
38 #include "talk/base/stringutils.h"
39 #include "talk/base/thread.h"
40 #include "talk/base/virtualsocketserver.h"
41 #include "talk/p2p/base/basicpacketsocketfactory.h"
42 #include "talk/p2p/base/portproxy.h"
43 #include "talk/p2p/base/relayport.h"
44 #include "talk/p2p/base/stunport.h"
45 #include "talk/p2p/base/tcpport.h"
46 #include "talk/p2p/base/testrelayserver.h"
47 #include "talk/p2p/base/teststunserver.h"
48 #include "talk/p2p/base/testturnserver.h"
49 #include "talk/p2p/base/transport.h"
50 #include "talk/p2p/base/turnport.h"
52 using talk_base::AsyncPacketSocket;
53 using talk_base::ByteBuffer;
54 using talk_base::NATType;
55 using talk_base::NAT_OPEN_CONE;
56 using talk_base::NAT_ADDR_RESTRICTED;
57 using talk_base::NAT_PORT_RESTRICTED;
58 using talk_base::NAT_SYMMETRIC;
59 using talk_base::PacketSocketFactory;
60 using talk_base::scoped_ptr;
61 using talk_base::Socket;
62 using talk_base::SocketAddress;
63 using namespace cricket;
65 static const int kTimeout = 1000;
66 static const SocketAddress kLocalAddr1("192.168.1.2", 0);
67 static const SocketAddress kLocalAddr2("192.168.1.3", 0);
68 static const SocketAddress kNatAddr1("77.77.77.77", talk_base::NAT_SERVER_PORT);
69 static const SocketAddress kNatAddr2("88.88.88.88", talk_base::NAT_SERVER_PORT);
70 static const SocketAddress kStunAddr("99.99.99.1", STUN_SERVER_PORT);
71 static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
72 static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
73 static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
74 static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
75 static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
76 static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
77 static const SocketAddress kTurnUdpIntAddr("99.99.99.4", STUN_SERVER_PORT);
78 static const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0);
79 static const RelayCredentials kRelayCredentials("test", "test");
81 // TODO: Update these when RFC5245 is completely supported.
82 // Magic value of 30 is from RFC3484, for IPv4 addresses.
83 static const uint32 kDefaultPrflxPriority = ICE_TYPE_PREFERENCE_PRFLX << 24 |
84 30 << 8 | (256 - ICE_CANDIDATE_COMPONENT_DEFAULT);
85 static const int STUN_ERROR_BAD_REQUEST_AS_GICE =
86 STUN_ERROR_BAD_REQUEST / 256 * 100 + STUN_ERROR_BAD_REQUEST % 256;
87 static const int STUN_ERROR_UNAUTHORIZED_AS_GICE =
88 STUN_ERROR_UNAUTHORIZED / 256 * 100 + STUN_ERROR_UNAUTHORIZED % 256;
89 static const int STUN_ERROR_SERVER_ERROR_AS_GICE =
90 STUN_ERROR_SERVER_ERROR / 256 * 100 + STUN_ERROR_SERVER_ERROR % 256;
92 static const int kTiebreaker1 = 11111;
93 static const int kTiebreaker2 = 22222;
95 static Candidate GetCandidate(Port* port) {
96 assert(port->Candidates().size() == 1);
97 return port->Candidates()[0];
100 static SocketAddress GetAddress(Port* port) {
101 return GetCandidate(port).address();
104 static IceMessage* CopyStunMessage(const IceMessage* src) {
105 IceMessage* dst = new IceMessage();
112 static bool WriteStunMessage(const StunMessage* msg, ByteBuffer* buf) {
113 buf->Resize(0); // clear out any existing buffer contents
114 return msg->Write(buf);
117 // Stub port class for testing STUN generation and processing.
118 class TestPort : public Port {
120 TestPort(talk_base::Thread* thread, const std::string& type,
121 talk_base::PacketSocketFactory* factory, talk_base::Network* network,
122 const talk_base::IPAddress& ip, int min_port, int max_port,
123 const std::string& username_fragment, const std::string& password)
124 : Port(thread, type, factory, network, ip,
125 min_port, max_port, username_fragment, password) {
129 // Expose GetStunMessage so that we can test it.
130 using cricket::Port::GetStunMessage;
132 // The last StunMessage that was sent on this Port.
133 // TODO: Make these const; requires changes to SendXXXXResponse.
134 ByteBuffer* last_stun_buf() { return last_stun_buf_.get(); }
135 IceMessage* last_stun_msg() { return last_stun_msg_.get(); }
136 int last_stun_error_code() {
138 if (last_stun_msg_) {
139 const StunErrorCodeAttribute* error_attr = last_stun_msg_->GetErrorCode();
141 code = error_attr->code();
147 virtual void PrepareAddress() {
148 talk_base::SocketAddress addr(ip(), min_port());
149 AddAddress(addr, addr, "udp", Type(), ICE_TYPE_PREFERENCE_HOST, true);
152 // Exposed for testing candidate building.
153 void AddCandidateAddress(const talk_base::SocketAddress& addr) {
154 AddAddress(addr, addr, "udp", Type(), type_preference_, false);
156 void AddCandidateAddress(const talk_base::SocketAddress& addr,
157 const talk_base::SocketAddress& base_address,
158 const std::string& type,
161 AddAddress(addr, base_address, "udp", type,
162 type_preference, final);
165 virtual Connection* CreateConnection(const Candidate& remote_candidate,
166 CandidateOrigin origin) {
167 Connection* conn = new ProxyConnection(this, 0, remote_candidate);
169 // Set use-candidate attribute flag as this will add USE-CANDIDATE attribute
170 // in STUN binding requests.
171 conn->set_use_candidate_attr(true);
175 const void* data, size_t size, const talk_base::SocketAddress& addr,
176 const talk_base::PacketOptions& options, bool payload) {
178 IceMessage* msg = new IceMessage;
179 ByteBuffer* buf = new ByteBuffer(static_cast<const char*>(data), size);
180 ByteBuffer::ReadPosition pos(buf->GetReadPosition());
181 if (!msg->Read(buf)) {
186 buf->SetReadPosition(pos);
187 last_stun_buf_.reset(buf);
188 last_stun_msg_.reset(msg);
190 return static_cast<int>(size);
192 virtual int SetOption(talk_base::Socket::Option opt, int value) {
195 virtual int GetOption(talk_base::Socket::Option opt, int* value) {
198 virtual int GetError() {
202 last_stun_buf_.reset();
203 last_stun_msg_.reset();
205 void set_type_preference(int type_preference) {
206 type_preference_ = type_preference;
210 talk_base::scoped_ptr<ByteBuffer> last_stun_buf_;
211 talk_base::scoped_ptr<IceMessage> last_stun_msg_;
212 int type_preference_;
215 class TestChannel : public sigslot::has_slots<> {
217 // Takes ownership of |p1| (but not |p2|).
218 TestChannel(Port* p1, Port* p2)
219 : ice_mode_(ICEMODE_FULL), src_(p1), dst_(p2), complete_count_(0),
220 conn_(NULL), remote_request_(), nominated_(false) {
221 src_->SignalPortComplete.connect(
222 this, &TestChannel::OnPortComplete);
223 src_->SignalUnknownAddress.connect(this, &TestChannel::OnUnknownAddress);
224 src_->SignalDestroyed.connect(this, &TestChannel::OnSrcPortDestroyed);
227 int complete_count() { return complete_count_; }
228 Connection* conn() { return conn_; }
229 const SocketAddress& remote_address() { return remote_address_; }
230 const std::string remote_fragment() { return remote_frag_; }
233 src_->PrepareAddress();
235 void CreateConnection() {
236 conn_ = src_->CreateConnection(GetCandidate(dst_), Port::ORIGIN_MESSAGE);
237 IceMode remote_ice_mode =
238 (ice_mode_ == ICEMODE_FULL) ? ICEMODE_LITE : ICEMODE_FULL;
239 conn_->set_remote_ice_mode(remote_ice_mode);
240 conn_->set_use_candidate_attr(remote_ice_mode == ICEMODE_FULL);
241 conn_->SignalStateChange.connect(
242 this, &TestChannel::OnConnectionStateChange);
244 void OnConnectionStateChange(Connection* conn) {
245 if (conn->write_state() == Connection::STATE_WRITABLE) {
246 conn->set_use_candidate_attr(true);
250 void AcceptConnection() {
251 ASSERT_TRUE(remote_request_.get() != NULL);
252 Candidate c = GetCandidate(dst_);
253 c.set_address(remote_address_);
254 conn_ = src_->CreateConnection(c, Port::ORIGIN_MESSAGE);
255 src_->SendBindingResponse(remote_request_.get(), remote_address_);
256 remote_request_.reset();
261 void Ping(uint32 now) {
265 conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed);
269 void OnPortComplete(Port* port) {
272 void SetIceMode(IceMode ice_mode) {
273 ice_mode_ = ice_mode;
276 void OnUnknownAddress(PortInterface* port, const SocketAddress& addr,
278 IceMessage* msg, const std::string& rf,
279 bool /*port_muxed*/) {
280 ASSERT_EQ(src_.get(), port);
281 if (!remote_address_.IsNil()) {
282 ASSERT_EQ(remote_address_, addr);
284 // MI and PRIORITY attribute should be present in ping requests when port
285 // is in ICEPROTO_RFC5245 mode.
286 const cricket::StunUInt32Attribute* priority_attr =
287 msg->GetUInt32(STUN_ATTR_PRIORITY);
288 const cricket::StunByteStringAttribute* mi_attr =
289 msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY);
290 const cricket::StunUInt32Attribute* fingerprint_attr =
291 msg->GetUInt32(STUN_ATTR_FINGERPRINT);
292 if (src_->IceProtocol() == cricket::ICEPROTO_RFC5245) {
293 EXPECT_TRUE(priority_attr != NULL);
294 EXPECT_TRUE(mi_attr != NULL);
295 EXPECT_TRUE(fingerprint_attr != NULL);
297 EXPECT_TRUE(priority_attr == NULL);
298 EXPECT_TRUE(mi_attr == NULL);
299 EXPECT_TRUE(fingerprint_attr == NULL);
301 remote_address_ = addr;
302 remote_request_.reset(CopyStunMessage(msg));
306 void OnDestroyed(Connection* conn) {
307 ASSERT_EQ(conn_, conn);
311 void OnSrcPortDestroyed(PortInterface* port) {
312 Port* destroyed_src = src_.release();
313 ASSERT_EQ(destroyed_src, port);
316 bool nominated() const { return nominated_; }
320 talk_base::scoped_ptr<Port> src_;
325 SocketAddress remote_address_;
326 talk_base::scoped_ptr<StunMessage> remote_request_;
327 std::string remote_frag_;
331 class PortTest : public testing::Test, public sigslot::has_slots<> {
334 : main_(talk_base::Thread::Current()),
335 pss_(new talk_base::PhysicalSocketServer),
336 ss_(new talk_base::VirtualSocketServer(pss_.get())),
337 ss_scope_(ss_.get()),
338 network_("unittest", "unittest", talk_base::IPAddress(INADDR_ANY), 32),
339 socket_factory_(talk_base::Thread::Current()),
340 nat_factory1_(ss_.get(), kNatAddr1),
341 nat_factory2_(ss_.get(), kNatAddr2),
342 nat_socket_factory1_(&nat_factory1_),
343 nat_socket_factory2_(&nat_factory2_),
344 stun_server_(main_, kStunAddr),
345 turn_server_(main_, kTurnUdpIntAddr, kTurnUdpExtAddr),
346 relay_server_(main_, kRelayUdpIntAddr, kRelayUdpExtAddr,
347 kRelayTcpIntAddr, kRelayTcpExtAddr,
348 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
349 username_(talk_base::CreateRandomString(ICE_UFRAG_LENGTH)),
350 password_(talk_base::CreateRandomString(ICE_PWD_LENGTH)),
351 ice_protocol_(cricket::ICEPROTO_GOOGLE),
352 role_conflict_(false),
354 network_.AddIP(talk_base::IPAddress(INADDR_ANY));
358 static void SetUpTestCase() {
359 talk_base::InitializeSSL();
362 static void TearDownTestCase() {
363 talk_base::CleanupSSL();
367 void TestLocalToLocal() {
368 Port* port1 = CreateUdpPort(kLocalAddr1);
369 Port* port2 = CreateUdpPort(kLocalAddr2);
370 TestConnectivity("udp", port1, "udp", port2, true, true, true, true);
372 void TestLocalToStun(NATType ntype) {
373 Port* port1 = CreateUdpPort(kLocalAddr1);
374 nat_server2_.reset(CreateNatServer(kNatAddr2, ntype));
375 Port* port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_);
376 TestConnectivity("udp", port1, StunName(ntype), port2,
377 ntype == NAT_OPEN_CONE, true,
378 ntype != NAT_SYMMETRIC, true);
380 void TestLocalToRelay(RelayType rtype, ProtocolType proto) {
381 Port* port1 = CreateUdpPort(kLocalAddr1);
382 Port* port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_UDP);
383 TestConnectivity("udp", port1, RelayName(rtype, proto), port2,
384 rtype == RELAY_GTURN, true, true, true);
386 void TestStunToLocal(NATType ntype) {
387 nat_server1_.reset(CreateNatServer(kNatAddr1, ntype));
388 Port* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
389 Port* port2 = CreateUdpPort(kLocalAddr2);
390 TestConnectivity(StunName(ntype), port1, "udp", port2,
391 true, ntype != NAT_SYMMETRIC, true, true);
393 void TestStunToStun(NATType ntype1, NATType ntype2) {
394 nat_server1_.reset(CreateNatServer(kNatAddr1, ntype1));
395 Port* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
396 nat_server2_.reset(CreateNatServer(kNatAddr2, ntype2));
397 Port* port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_);
398 TestConnectivity(StunName(ntype1), port1, StunName(ntype2), port2,
399 ntype2 == NAT_OPEN_CONE,
400 ntype1 != NAT_SYMMETRIC, ntype2 != NAT_SYMMETRIC,
401 ntype1 + ntype2 < (NAT_PORT_RESTRICTED + NAT_SYMMETRIC));
403 void TestStunToRelay(NATType ntype, RelayType rtype, ProtocolType proto) {
404 nat_server1_.reset(CreateNatServer(kNatAddr1, ntype));
405 Port* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
406 Port* port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_UDP);
407 TestConnectivity(StunName(ntype), port1, RelayName(rtype, proto), port2,
408 rtype == RELAY_GTURN, ntype != NAT_SYMMETRIC, true, true);
410 void TestTcpToTcp() {
411 Port* port1 = CreateTcpPort(kLocalAddr1);
412 Port* port2 = CreateTcpPort(kLocalAddr2);
413 TestConnectivity("tcp", port1, "tcp", port2, true, false, true, true);
415 void TestTcpToRelay(RelayType rtype, ProtocolType proto) {
416 Port* port1 = CreateTcpPort(kLocalAddr1);
417 Port* port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_TCP);
418 TestConnectivity("tcp", port1, RelayName(rtype, proto), port2,
419 rtype == RELAY_GTURN, false, true, true);
421 void TestSslTcpToRelay(RelayType rtype, ProtocolType proto) {
422 Port* port1 = CreateTcpPort(kLocalAddr1);
423 Port* port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_SSLTCP);
424 TestConnectivity("ssltcp", port1, RelayName(rtype, proto), port2,
425 rtype == RELAY_GTURN, false, true, true);
428 // helpers for above functions
429 UDPPort* CreateUdpPort(const SocketAddress& addr) {
430 return CreateUdpPort(addr, &socket_factory_);
432 UDPPort* CreateUdpPort(const SocketAddress& addr,
433 PacketSocketFactory* socket_factory) {
434 UDPPort* port = UDPPort::Create(main_, socket_factory, &network_,
435 addr.ipaddr(), 0, 0, username_, password_);
436 port->SetIceProtocolType(ice_protocol_);
439 TCPPort* CreateTcpPort(const SocketAddress& addr) {
440 TCPPort* port = CreateTcpPort(addr, &socket_factory_);
441 port->SetIceProtocolType(ice_protocol_);
444 TCPPort* CreateTcpPort(const SocketAddress& addr,
445 PacketSocketFactory* socket_factory) {
446 TCPPort* port = TCPPort::Create(main_, socket_factory, &network_,
447 addr.ipaddr(), 0, 0, username_, password_,
449 port->SetIceProtocolType(ice_protocol_);
452 StunPort* CreateStunPort(const SocketAddress& addr,
453 talk_base::PacketSocketFactory* factory) {
454 StunPort* port = StunPort::Create(main_, factory, &network_,
456 username_, password_, kStunAddr);
457 port->SetIceProtocolType(ice_protocol_);
460 Port* CreateRelayPort(const SocketAddress& addr, RelayType rtype,
461 ProtocolType int_proto, ProtocolType ext_proto) {
462 if (rtype == RELAY_TURN) {
463 return CreateTurnPort(addr, &socket_factory_, int_proto, ext_proto);
465 return CreateGturnPort(addr, int_proto, ext_proto);
468 TurnPort* CreateTurnPort(const SocketAddress& addr,
469 PacketSocketFactory* socket_factory,
470 ProtocolType int_proto, ProtocolType ext_proto) {
471 TurnPort* port = TurnPort::Create(main_, socket_factory, &network_,
473 username_, password_, ProtocolAddress(
474 kTurnUdpIntAddr, PROTO_UDP),
476 port->SetIceProtocolType(ice_protocol_);
479 RelayPort* CreateGturnPort(const SocketAddress& addr,
480 ProtocolType int_proto, ProtocolType ext_proto) {
481 RelayPort* port = CreateGturnPort(addr);
482 SocketAddress addrs[] =
483 { kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr };
484 port->AddServerAddress(ProtocolAddress(addrs[int_proto], int_proto));
487 RelayPort* CreateGturnPort(const SocketAddress& addr) {
488 RelayPort* port = RelayPort::Create(main_, &socket_factory_, &network_,
490 username_, password_);
491 // TODO: Add an external address for ext_proto, so that the
492 // other side can connect to this port using a non-UDP protocol.
493 port->SetIceProtocolType(ice_protocol_);
496 talk_base::NATServer* CreateNatServer(const SocketAddress& addr,
497 talk_base::NATType type) {
498 return new talk_base::NATServer(type, ss_.get(), addr, ss_.get(), addr);
500 static const char* StunName(NATType type) {
502 case NAT_OPEN_CONE: return "stun(open cone)";
503 case NAT_ADDR_RESTRICTED: return "stun(addr restricted)";
504 case NAT_PORT_RESTRICTED: return "stun(port restricted)";
505 case NAT_SYMMETRIC: return "stun(symmetric)";
506 default: return "stun(?)";
509 static const char* RelayName(RelayType type, ProtocolType proto) {
510 if (type == RELAY_TURN) {
512 case PROTO_UDP: return "turn(udp)";
513 case PROTO_TCP: return "turn(tcp)";
514 case PROTO_SSLTCP: return "turn(ssltcp)";
515 default: return "turn(?)";
519 case PROTO_UDP: return "gturn(udp)";
520 case PROTO_TCP: return "gturn(tcp)";
521 case PROTO_SSLTCP: return "gturn(ssltcp)";
522 default: return "gturn(?)";
527 void TestCrossFamilyPorts(int type);
529 // This does all the work and then deletes |port1| and |port2|.
530 void TestConnectivity(const char* name1, Port* port1,
531 const char* name2, Port* port2,
532 bool accept, bool same_addr1,
533 bool same_addr2, bool possible);
535 // This connects and disconnects the provided channels in the same sequence as
536 // TestConnectivity with all options set to |true|. It does not delete either
538 void ConnectAndDisconnectChannels(TestChannel* ch1, TestChannel* ch2);
540 void SetIceProtocolType(cricket::IceProtocolType protocol) {
541 ice_protocol_ = protocol;
544 IceMessage* CreateStunMessage(int type) {
545 IceMessage* msg = new IceMessage();
547 msg->SetTransactionID("TESTTESTTEST");
550 IceMessage* CreateStunMessageWithUsername(int type,
551 const std::string& username) {
552 IceMessage* msg = CreateStunMessage(type);
554 new StunByteStringAttribute(STUN_ATTR_USERNAME, username));
557 TestPort* CreateTestPort(const talk_base::SocketAddress& addr,
558 const std::string& username,
559 const std::string& password) {
560 TestPort* port = new TestPort(main_, "test", &socket_factory_, &network_,
561 addr.ipaddr(), 0, 0, username, password);
562 port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict);
565 TestPort* CreateTestPort(const talk_base::SocketAddress& addr,
566 const std::string& username,
567 const std::string& password,
568 cricket::IceProtocolType type,
569 cricket::IceRole role,
571 TestPort* port = CreateTestPort(addr, username, password);
572 port->SetIceProtocolType(type);
573 port->SetIceRole(role);
574 port->SetIceTiebreaker(tiebreaker);
578 void OnRoleConflict(PortInterface* port) {
579 role_conflict_ = true;
581 bool role_conflict() const { return role_conflict_; }
583 void ConnectToSignalDestroyed(PortInterface* port) {
584 port->SignalDestroyed.connect(this, &PortTest::OnDestroyed);
587 void OnDestroyed(PortInterface* port) {
590 bool destroyed() const { return destroyed_; }
592 talk_base::BasicPacketSocketFactory* nat_socket_factory1() {
593 return &nat_socket_factory1_;
597 talk_base::Thread* main_;
598 talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_;
599 talk_base::scoped_ptr<talk_base::VirtualSocketServer> ss_;
600 talk_base::SocketServerScope ss_scope_;
601 talk_base::Network network_;
602 talk_base::BasicPacketSocketFactory socket_factory_;
603 talk_base::scoped_ptr<talk_base::NATServer> nat_server1_;
604 talk_base::scoped_ptr<talk_base::NATServer> nat_server2_;
605 talk_base::NATSocketFactory nat_factory1_;
606 talk_base::NATSocketFactory nat_factory2_;
607 talk_base::BasicPacketSocketFactory nat_socket_factory1_;
608 talk_base::BasicPacketSocketFactory nat_socket_factory2_;
609 TestStunServer stun_server_;
610 TestTurnServer turn_server_;
611 TestRelayServer relay_server_;
612 std::string username_;
613 std::string password_;
614 cricket::IceProtocolType ice_protocol_;
619 void PortTest::TestConnectivity(const char* name1, Port* port1,
620 const char* name2, Port* port2,
621 bool accept, bool same_addr1,
622 bool same_addr2, bool possible) {
623 LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": ";
624 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
625 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
627 // Set up channels and ensure both ports will be deleted.
628 TestChannel ch1(port1, port2);
629 TestChannel ch2(port2, port1);
630 EXPECT_EQ(0, ch1.complete_count());
631 EXPECT_EQ(0, ch2.complete_count());
633 // Acquire addresses.
636 ASSERT_EQ_WAIT(1, ch1.complete_count(), kTimeout);
637 ASSERT_EQ_WAIT(1, ch2.complete_count(), kTimeout);
639 // Send a ping from src to dst. This may or may not make it.
640 ch1.CreateConnection();
641 ASSERT_TRUE(ch1.conn() != NULL);
642 EXPECT_TRUE_WAIT(ch1.conn()->connected(), kTimeout); // for TCP connect
644 WAIT(!ch2.remote_address().IsNil(), kTimeout);
647 // We are able to send a ping from src to dst. This is the case when
648 // sending to UDP ports and cone NATs.
649 EXPECT_TRUE(ch1.remote_address().IsNil());
650 EXPECT_EQ(ch2.remote_fragment(), port1->username_fragment());
652 // Ensure the ping came from the same address used for src.
653 // This is the case unless the source NAT was symmetric.
654 if (same_addr1) EXPECT_EQ(ch2.remote_address(), GetAddress(port1));
655 EXPECT_TRUE(same_addr2);
657 // Send a ping from dst to src.
658 ch2.AcceptConnection();
659 ASSERT_TRUE(ch2.conn() != NULL);
661 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2.conn()->write_state(),
664 // We can't send a ping from src to dst, so flip it around. This will happen
665 // when the destination NAT is addr/port restricted or symmetric.
666 EXPECT_TRUE(ch1.remote_address().IsNil());
667 EXPECT_TRUE(ch2.remote_address().IsNil());
669 // Send a ping from dst to src. Again, this may or may not make it.
670 ch2.CreateConnection();
671 ASSERT_TRUE(ch2.conn() != NULL);
673 WAIT(ch2.conn()->write_state() == Connection::STATE_WRITABLE, kTimeout);
675 if (same_addr1 && same_addr2) {
676 // The new ping got back to the source.
677 EXPECT_EQ(Connection::STATE_READABLE, ch1.conn()->read_state());
678 EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state());
680 // First connection may not be writable if the first ping did not get
681 // through. So we will have to do another.
682 if (ch1.conn()->write_state() == Connection::STATE_WRITE_INIT) {
684 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
687 } else if (!same_addr1 && possible) {
688 // The new ping went to the candidate address, but that address was bad.
689 // This will happen when the source NAT is symmetric.
690 EXPECT_TRUE(ch1.remote_address().IsNil());
691 EXPECT_TRUE(ch2.remote_address().IsNil());
693 // However, since we have now sent a ping to the source IP, we should be
694 // able to get a ping from it. This gives us the real source address.
696 EXPECT_TRUE_WAIT(!ch2.remote_address().IsNil(), kTimeout);
697 EXPECT_EQ(Connection::STATE_READ_INIT, ch2.conn()->read_state());
698 EXPECT_TRUE(ch1.remote_address().IsNil());
700 // Pick up the actual address and establish the connection.
701 ch2.AcceptConnection();
702 ASSERT_TRUE(ch2.conn() != NULL);
704 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2.conn()->write_state(),
706 } else if (!same_addr2 && possible) {
707 // The new ping came in, but from an unexpected address. This will happen
708 // when the destination NAT is symmetric.
709 EXPECT_FALSE(ch1.remote_address().IsNil());
710 EXPECT_EQ(Connection::STATE_READ_INIT, ch1.conn()->read_state());
712 // Update our address and complete the connection.
713 ch1.AcceptConnection();
715 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
717 } else { // (!possible)
718 // There should be s no way for the pings to reach each other. Check it.
719 EXPECT_TRUE(ch1.remote_address().IsNil());
720 EXPECT_TRUE(ch2.remote_address().IsNil());
722 WAIT(!ch2.remote_address().IsNil(), kTimeout);
723 EXPECT_TRUE(ch1.remote_address().IsNil());
724 EXPECT_TRUE(ch2.remote_address().IsNil());
728 // Everything should be good, unless we know the situation is impossible.
729 ASSERT_TRUE(ch1.conn() != NULL);
730 ASSERT_TRUE(ch2.conn() != NULL);
732 EXPECT_EQ(Connection::STATE_READABLE, ch1.conn()->read_state());
733 EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state());
734 EXPECT_EQ(Connection::STATE_READABLE, ch2.conn()->read_state());
735 EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state());
737 EXPECT_NE(Connection::STATE_READABLE, ch1.conn()->read_state());
738 EXPECT_NE(Connection::STATE_WRITABLE, ch1.conn()->write_state());
739 EXPECT_NE(Connection::STATE_READABLE, ch2.conn()->read_state());
740 EXPECT_NE(Connection::STATE_WRITABLE, ch2.conn()->write_state());
743 // Tear down and ensure that goes smoothly.
746 EXPECT_TRUE_WAIT(ch1.conn() == NULL, kTimeout);
747 EXPECT_TRUE_WAIT(ch2.conn() == NULL, kTimeout);
750 void PortTest::ConnectAndDisconnectChannels(TestChannel* ch1,
752 // Acquire addresses.
756 // Send a ping from src to dst.
757 ch1->CreateConnection();
758 EXPECT_TRUE_WAIT(ch1->conn()->connected(), kTimeout); // for TCP connect
760 WAIT(!ch2->remote_address().IsNil(), kTimeout);
762 // Send a ping from dst to src.
763 ch2->AcceptConnection();
765 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2->conn()->write_state(),
768 // Destroy the connections.
773 class FakePacketSocketFactory : public talk_base::PacketSocketFactory {
775 FakePacketSocketFactory()
776 : next_udp_socket_(NULL),
777 next_server_tcp_socket_(NULL),
778 next_client_tcp_socket_(NULL) {
780 virtual ~FakePacketSocketFactory() { }
782 virtual AsyncPacketSocket* CreateUdpSocket(
783 const SocketAddress& address, int min_port, int max_port) {
784 EXPECT_TRUE(next_udp_socket_ != NULL);
785 AsyncPacketSocket* result = next_udp_socket_;
786 next_udp_socket_ = NULL;
790 virtual AsyncPacketSocket* CreateServerTcpSocket(
791 const SocketAddress& local_address, int min_port, int max_port,
793 EXPECT_TRUE(next_server_tcp_socket_ != NULL);
794 AsyncPacketSocket* result = next_server_tcp_socket_;
795 next_server_tcp_socket_ = NULL;
799 // TODO: |proxy_info| and |user_agent| should be set
800 // per-factory and not when socket is created.
801 virtual AsyncPacketSocket* CreateClientTcpSocket(
802 const SocketAddress& local_address, const SocketAddress& remote_address,
803 const talk_base::ProxyInfo& proxy_info,
804 const std::string& user_agent, int opts) {
805 EXPECT_TRUE(next_client_tcp_socket_ != NULL);
806 AsyncPacketSocket* result = next_client_tcp_socket_;
807 next_client_tcp_socket_ = NULL;
811 void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) {
812 next_udp_socket_ = next_udp_socket;
814 void set_next_server_tcp_socket(AsyncPacketSocket* next_server_tcp_socket) {
815 next_server_tcp_socket_ = next_server_tcp_socket;
817 void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) {
818 next_client_tcp_socket_ = next_client_tcp_socket;
820 talk_base::AsyncResolverInterface* CreateAsyncResolver() {
825 AsyncPacketSocket* next_udp_socket_;
826 AsyncPacketSocket* next_server_tcp_socket_;
827 AsyncPacketSocket* next_client_tcp_socket_;
830 class FakeAsyncPacketSocket : public AsyncPacketSocket {
832 // Returns current local address. Address may be set to NULL if the
833 // socket is not bound yet (GetState() returns STATE_BINDING).
834 virtual SocketAddress GetLocalAddress() const {
835 return SocketAddress();
838 // Returns remote address. Returns zeroes if this is not a client TCP socket.
839 virtual SocketAddress GetRemoteAddress() const {
840 return SocketAddress();
844 virtual int Send(const void *pv, size_t cb,
845 const talk_base::PacketOptions& options) {
846 return static_cast<int>(cb);
848 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
849 const talk_base::PacketOptions& options) {
850 return static_cast<int>(cb);
852 virtual int Close() {
856 virtual State GetState() const { return state_; }
857 virtual int GetOption(Socket::Option opt, int* value) { return 0; }
858 virtual int SetOption(Socket::Option opt, int value) { return 0; }
859 virtual int GetError() const { return 0; }
860 virtual void SetError(int error) { }
862 void set_state(State state) { state_ = state; }
869 TEST_F(PortTest, TestLocalToLocal) {
873 TEST_F(PortTest, TestLocalToConeNat) {
874 TestLocalToStun(NAT_OPEN_CONE);
877 TEST_F(PortTest, TestLocalToARNat) {
878 TestLocalToStun(NAT_ADDR_RESTRICTED);
881 TEST_F(PortTest, TestLocalToPRNat) {
882 TestLocalToStun(NAT_PORT_RESTRICTED);
885 TEST_F(PortTest, TestLocalToSymNat) {
886 TestLocalToStun(NAT_SYMMETRIC);
889 TEST_F(PortTest, TestLocalToTurn) {
890 TestLocalToRelay(RELAY_TURN, PROTO_UDP);
893 TEST_F(PortTest, TestLocalToGturn) {
894 TestLocalToRelay(RELAY_GTURN, PROTO_UDP);
897 TEST_F(PortTest, TestLocalToTcpGturn) {
898 TestLocalToRelay(RELAY_GTURN, PROTO_TCP);
901 TEST_F(PortTest, TestLocalToSslTcpGturn) {
902 TestLocalToRelay(RELAY_GTURN, PROTO_SSLTCP);
906 TEST_F(PortTest, TestConeNatToLocal) {
907 TestStunToLocal(NAT_OPEN_CONE);
910 TEST_F(PortTest, TestConeNatToConeNat) {
911 TestStunToStun(NAT_OPEN_CONE, NAT_OPEN_CONE);
914 TEST_F(PortTest, TestConeNatToARNat) {
915 TestStunToStun(NAT_OPEN_CONE, NAT_ADDR_RESTRICTED);
918 TEST_F(PortTest, TestConeNatToPRNat) {
919 TestStunToStun(NAT_OPEN_CONE, NAT_PORT_RESTRICTED);
922 TEST_F(PortTest, TestConeNatToSymNat) {
923 TestStunToStun(NAT_OPEN_CONE, NAT_SYMMETRIC);
926 TEST_F(PortTest, TestConeNatToTurn) {
927 TestStunToRelay(NAT_OPEN_CONE, RELAY_TURN, PROTO_UDP);
930 TEST_F(PortTest, TestConeNatToGturn) {
931 TestStunToRelay(NAT_OPEN_CONE, RELAY_GTURN, PROTO_UDP);
934 TEST_F(PortTest, TestConeNatToTcpGturn) {
935 TestStunToRelay(NAT_OPEN_CONE, RELAY_GTURN, PROTO_TCP);
938 // Address-restricted NAT -> XXXX
939 TEST_F(PortTest, TestARNatToLocal) {
940 TestStunToLocal(NAT_ADDR_RESTRICTED);
943 TEST_F(PortTest, TestARNatToConeNat) {
944 TestStunToStun(NAT_ADDR_RESTRICTED, NAT_OPEN_CONE);
947 TEST_F(PortTest, TestARNatToARNat) {
948 TestStunToStun(NAT_ADDR_RESTRICTED, NAT_ADDR_RESTRICTED);
951 TEST_F(PortTest, TestARNatToPRNat) {
952 TestStunToStun(NAT_ADDR_RESTRICTED, NAT_PORT_RESTRICTED);
955 TEST_F(PortTest, TestARNatToSymNat) {
956 TestStunToStun(NAT_ADDR_RESTRICTED, NAT_SYMMETRIC);
959 TEST_F(PortTest, TestARNatToTurn) {
960 TestStunToRelay(NAT_ADDR_RESTRICTED, RELAY_TURN, PROTO_UDP);
963 TEST_F(PortTest, TestARNatToGturn) {
964 TestStunToRelay(NAT_ADDR_RESTRICTED, RELAY_GTURN, PROTO_UDP);
967 TEST_F(PortTest, TestARNATNatToTcpGturn) {
968 TestStunToRelay(NAT_ADDR_RESTRICTED, RELAY_GTURN, PROTO_TCP);
971 // Port-restricted NAT -> XXXX
972 TEST_F(PortTest, TestPRNatToLocal) {
973 TestStunToLocal(NAT_PORT_RESTRICTED);
976 TEST_F(PortTest, TestPRNatToConeNat) {
977 TestStunToStun(NAT_PORT_RESTRICTED, NAT_OPEN_CONE);
980 TEST_F(PortTest, TestPRNatToARNat) {
981 TestStunToStun(NAT_PORT_RESTRICTED, NAT_ADDR_RESTRICTED);
984 TEST_F(PortTest, TestPRNatToPRNat) {
985 TestStunToStun(NAT_PORT_RESTRICTED, NAT_PORT_RESTRICTED);
988 TEST_F(PortTest, TestPRNatToSymNat) {
990 TestStunToStun(NAT_PORT_RESTRICTED, NAT_SYMMETRIC);
993 TEST_F(PortTest, TestPRNatToTurn) {
994 TestStunToRelay(NAT_PORT_RESTRICTED, RELAY_TURN, PROTO_UDP);
997 TEST_F(PortTest, TestPRNatToGturn) {
998 TestStunToRelay(NAT_PORT_RESTRICTED, RELAY_GTURN, PROTO_UDP);
1001 TEST_F(PortTest, TestPRNatToTcpGturn) {
1002 TestStunToRelay(NAT_PORT_RESTRICTED, RELAY_GTURN, PROTO_TCP);
1005 // Symmetric NAT -> XXXX
1006 TEST_F(PortTest, TestSymNatToLocal) {
1007 TestStunToLocal(NAT_SYMMETRIC);
1010 TEST_F(PortTest, TestSymNatToConeNat) {
1011 TestStunToStun(NAT_SYMMETRIC, NAT_OPEN_CONE);
1014 TEST_F(PortTest, TestSymNatToARNat) {
1015 TestStunToStun(NAT_SYMMETRIC, NAT_ADDR_RESTRICTED);
1018 TEST_F(PortTest, TestSymNatToPRNat) {
1020 TestStunToStun(NAT_SYMMETRIC, NAT_PORT_RESTRICTED);
1023 TEST_F(PortTest, TestSymNatToSymNat) {
1025 TestStunToStun(NAT_SYMMETRIC, NAT_SYMMETRIC);
1028 TEST_F(PortTest, TestSymNatToTurn) {
1029 TestStunToRelay(NAT_SYMMETRIC, RELAY_TURN, PROTO_UDP);
1032 TEST_F(PortTest, TestSymNatToGturn) {
1033 TestStunToRelay(NAT_SYMMETRIC, RELAY_GTURN, PROTO_UDP);
1036 TEST_F(PortTest, TestSymNatToTcpGturn) {
1037 TestStunToRelay(NAT_SYMMETRIC, RELAY_GTURN, PROTO_TCP);
1040 // Outbound TCP -> XXXX
1041 TEST_F(PortTest, TestTcpToTcp) {
1045 /* TODO: Enable these once testrelayserver can accept external TCP.
1046 TEST_F(PortTest, TestTcpToTcpRelay) {
1047 TestTcpToRelay(PROTO_TCP);
1050 TEST_F(PortTest, TestTcpToSslTcpRelay) {
1051 TestTcpToRelay(PROTO_SSLTCP);
1055 // Outbound SSLTCP -> XXXX
1056 /* TODO: Enable these once testrelayserver can accept external SSL.
1057 TEST_F(PortTest, TestSslTcpToTcpRelay) {
1058 TestSslTcpToRelay(PROTO_TCP);
1061 TEST_F(PortTest, TestSslTcpToSslTcpRelay) {
1062 TestSslTcpToRelay(PROTO_SSLTCP);
1066 // This test case verifies standard ICE features in STUN messages. Currently it
1067 // verifies Message Integrity attribute in STUN messages and username in STUN
1068 // binding request will have colon (":") between remote and local username.
1069 TEST_F(PortTest, TestLocalToLocalAsIce) {
1070 SetIceProtocolType(cricket::ICEPROTO_RFC5245);
1071 UDPPort* port1 = CreateUdpPort(kLocalAddr1);
1072 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
1073 port1->SetIceTiebreaker(kTiebreaker1);
1074 ASSERT_EQ(cricket::ICEPROTO_RFC5245, port1->IceProtocol());
1075 UDPPort* port2 = CreateUdpPort(kLocalAddr2);
1076 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
1077 port2->SetIceTiebreaker(kTiebreaker2);
1078 ASSERT_EQ(cricket::ICEPROTO_RFC5245, port2->IceProtocol());
1079 // Same parameters as TestLocalToLocal above.
1080 TestConnectivity("udp", port1, "udp", port2, true, true, true, true);
1083 // This test is trying to validate a successful and failure scenario in a
1084 // loopback test when protocol is RFC5245. For success IceTiebreaker, username
1085 // should remain equal to the request generated by the port and role of port
1086 // must be in controlling.
1087 TEST_F(PortTest, TestLoopbackCallAsIce) {
1088 talk_base::scoped_ptr<TestPort> lport(
1089 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
1090 lport->SetIceProtocolType(ICEPROTO_RFC5245);
1091 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1092 lport->SetIceTiebreaker(kTiebreaker1);
1093 lport->PrepareAddress();
1094 ASSERT_FALSE(lport->Candidates().empty());
1095 Connection* conn = lport->CreateConnection(lport->Candidates()[0],
1096 Port::ORIGIN_MESSAGE);
1099 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000);
1100 IceMessage* msg = lport->last_stun_msg();
1101 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1102 conn->OnReadPacket(lport->last_stun_buf()->Data(),
1103 lport->last_stun_buf()->Length(),
1104 talk_base::PacketTime());
1105 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000);
1106 msg = lport->last_stun_msg();
1107 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
1109 // If the tiebreaker value is different from port, we expect a error
1112 lport->AddCandidateAddress(kLocalAddr2);
1113 // Creating a different connection as |conn| is in STATE_READABLE.
1114 Connection* conn1 = lport->CreateConnection(lport->Candidates()[1],
1115 Port::ORIGIN_MESSAGE);
1118 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000);
1119 msg = lport->last_stun_msg();
1120 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1121 talk_base::scoped_ptr<IceMessage> modified_req(
1122 CreateStunMessage(STUN_BINDING_REQUEST));
1123 const StunByteStringAttribute* username_attr = msg->GetByteString(
1124 STUN_ATTR_USERNAME);
1125 modified_req->AddAttribute(new StunByteStringAttribute(
1126 STUN_ATTR_USERNAME, username_attr->GetString()));
1127 // To make sure we receive error response, adding tiebreaker less than
1128 // what's present in request.
1129 modified_req->AddAttribute(new StunUInt64Attribute(
1130 STUN_ATTR_ICE_CONTROLLING, kTiebreaker1 - 1));
1131 modified_req->AddMessageIntegrity("lpass");
1132 modified_req->AddFingerprint();
1135 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
1136 WriteStunMessage(modified_req.get(), buf.get());
1137 conn1->OnReadPacket(buf->Data(), buf->Length(), talk_base::PacketTime());
1138 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000);
1139 msg = lport->last_stun_msg();
1140 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type());
1143 // This test verifies role conflict signal is received when there is
1144 // conflict in the role. In this case both ports are in controlling and
1145 // |rport| has higher tiebreaker value than |lport|. Since |lport| has lower
1146 // value of tiebreaker, when it receives ping request from |rport| it will
1147 // send role conflict signal.
1148 TEST_F(PortTest, TestIceRoleConflict) {
1149 talk_base::scoped_ptr<TestPort> lport(
1150 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
1151 lport->SetIceProtocolType(ICEPROTO_RFC5245);
1152 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1153 lport->SetIceTiebreaker(kTiebreaker1);
1154 talk_base::scoped_ptr<TestPort> rport(
1155 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1156 rport->SetIceProtocolType(ICEPROTO_RFC5245);
1157 rport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1158 rport->SetIceTiebreaker(kTiebreaker2);
1160 lport->PrepareAddress();
1161 rport->PrepareAddress();
1162 ASSERT_FALSE(lport->Candidates().empty());
1163 ASSERT_FALSE(rport->Candidates().empty());
1164 Connection* lconn = lport->CreateConnection(rport->Candidates()[0],
1165 Port::ORIGIN_MESSAGE);
1166 Connection* rconn = rport->CreateConnection(lport->Candidates()[0],
1167 Port::ORIGIN_MESSAGE);
1170 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000);
1171 IceMessage* msg = rport->last_stun_msg();
1172 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1173 // Send rport binding request to lport.
1174 lconn->OnReadPacket(rport->last_stun_buf()->Data(),
1175 rport->last_stun_buf()->Length(),
1176 talk_base::PacketTime());
1178 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000);
1179 EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type());
1180 EXPECT_TRUE(role_conflict());
1183 TEST_F(PortTest, TestTcpNoDelay) {
1184 TCPPort* port1 = CreateTcpPort(kLocalAddr1);
1185 int option_value = -1;
1186 int success = port1->GetOption(talk_base::Socket::OPT_NODELAY,
1188 ASSERT_EQ(0, success); // GetOption() should complete successfully w/ 0
1189 ASSERT_EQ(1, option_value);
1193 TEST_F(PortTest, TestDelayedBindingUdp) {
1194 FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket();
1195 FakePacketSocketFactory socket_factory;
1197 socket_factory.set_next_udp_socket(socket);
1198 scoped_ptr<UDPPort> port(
1199 CreateUdpPort(kLocalAddr1, &socket_factory));
1201 socket->set_state(AsyncPacketSocket::STATE_BINDING);
1202 port->PrepareAddress();
1204 EXPECT_EQ(0U, port->Candidates().size());
1205 socket->SignalAddressReady(socket, kLocalAddr2);
1207 EXPECT_EQ(1U, port->Candidates().size());
1210 TEST_F(PortTest, TestDelayedBindingTcp) {
1211 FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket();
1212 FakePacketSocketFactory socket_factory;
1214 socket_factory.set_next_server_tcp_socket(socket);
1215 scoped_ptr<TCPPort> port(
1216 CreateTcpPort(kLocalAddr1, &socket_factory));
1218 socket->set_state(AsyncPacketSocket::STATE_BINDING);
1219 port->PrepareAddress();
1221 EXPECT_EQ(0U, port->Candidates().size());
1222 socket->SignalAddressReady(socket, kLocalAddr2);
1224 EXPECT_EQ(1U, port->Candidates().size());
1227 void PortTest::TestCrossFamilyPorts(int type) {
1228 FakePacketSocketFactory factory;
1229 scoped_ptr<Port> ports[4];
1230 SocketAddress addresses[4] = {SocketAddress("192.168.1.3", 0),
1231 SocketAddress("192.168.1.4", 0),
1232 SocketAddress("2001:db8::1", 0),
1233 SocketAddress("2001:db8::2", 0)};
1234 for (int i = 0; i < 4; i++) {
1235 FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket();
1236 if (type == SOCK_DGRAM) {
1237 factory.set_next_udp_socket(socket);
1238 ports[i].reset(CreateUdpPort(addresses[i], &factory));
1239 } else if (type == SOCK_STREAM) {
1240 factory.set_next_server_tcp_socket(socket);
1241 ports[i].reset(CreateTcpPort(addresses[i], &factory));
1243 socket->set_state(AsyncPacketSocket::STATE_BINDING);
1244 socket->SignalAddressReady(socket, addresses[i]);
1245 ports[i]->PrepareAddress();
1248 // IPv4 Port, connects to IPv6 candidate and then to IPv4 candidate.
1249 if (type == SOCK_STREAM) {
1250 FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket();
1251 factory.set_next_client_tcp_socket(clientsocket);
1253 Connection* c = ports[0]->CreateConnection(GetCandidate(ports[2].get()),
1254 Port::ORIGIN_MESSAGE);
1255 EXPECT_TRUE(NULL == c);
1256 EXPECT_EQ(0U, ports[0]->connections().size());
1257 c = ports[0]->CreateConnection(GetCandidate(ports[1].get()),
1258 Port::ORIGIN_MESSAGE);
1259 EXPECT_FALSE(NULL == c);
1260 EXPECT_EQ(1U, ports[0]->connections().size());
1262 // IPv6 Port, connects to IPv4 candidate and to IPv6 candidate.
1263 if (type == SOCK_STREAM) {
1264 FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket();
1265 factory.set_next_client_tcp_socket(clientsocket);
1267 c = ports[2]->CreateConnection(GetCandidate(ports[0].get()),
1268 Port::ORIGIN_MESSAGE);
1269 EXPECT_TRUE(NULL == c);
1270 EXPECT_EQ(0U, ports[2]->connections().size());
1271 c = ports[2]->CreateConnection(GetCandidate(ports[3].get()),
1272 Port::ORIGIN_MESSAGE);
1273 EXPECT_FALSE(NULL == c);
1274 EXPECT_EQ(1U, ports[2]->connections().size());
1277 TEST_F(PortTest, TestSkipCrossFamilyTcp) {
1278 TestCrossFamilyPorts(SOCK_STREAM);
1281 TEST_F(PortTest, TestSkipCrossFamilyUdp) {
1282 TestCrossFamilyPorts(SOCK_DGRAM);
1285 // This test verifies DSCP value set through SetOption interface can be
1286 // get through DefaultDscpValue.
1287 TEST_F(PortTest, TestDefaultDscpValue) {
1289 talk_base::scoped_ptr<UDPPort> udpport(CreateUdpPort(kLocalAddr1));
1290 EXPECT_EQ(0, udpport->SetOption(talk_base::Socket::OPT_DSCP,
1291 talk_base::DSCP_CS6));
1292 EXPECT_EQ(0, udpport->GetOption(talk_base::Socket::OPT_DSCP, &dscp));
1293 talk_base::scoped_ptr<TCPPort> tcpport(CreateTcpPort(kLocalAddr1));
1294 EXPECT_EQ(0, tcpport->SetOption(talk_base::Socket::OPT_DSCP,
1295 talk_base::DSCP_AF31));
1296 EXPECT_EQ(0, tcpport->GetOption(talk_base::Socket::OPT_DSCP, &dscp));
1297 EXPECT_EQ(talk_base::DSCP_AF31, dscp);
1298 talk_base::scoped_ptr<StunPort> stunport(
1299 CreateStunPort(kLocalAddr1, nat_socket_factory1()));
1300 EXPECT_EQ(0, stunport->SetOption(talk_base::Socket::OPT_DSCP,
1301 talk_base::DSCP_AF41));
1302 EXPECT_EQ(0, stunport->GetOption(talk_base::Socket::OPT_DSCP, &dscp));
1303 EXPECT_EQ(talk_base::DSCP_AF41, dscp);
1304 talk_base::scoped_ptr<TurnPort> turnport1(CreateTurnPort(
1305 kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP));
1306 // Socket is created in PrepareAddress.
1307 turnport1->PrepareAddress();
1308 EXPECT_EQ(0, turnport1->SetOption(talk_base::Socket::OPT_DSCP,
1309 talk_base::DSCP_CS7));
1310 EXPECT_EQ(0, turnport1->GetOption(talk_base::Socket::OPT_DSCP, &dscp));
1311 EXPECT_EQ(talk_base::DSCP_CS7, dscp);
1312 // This will verify correct value returned without the socket.
1313 talk_base::scoped_ptr<TurnPort> turnport2(CreateTurnPort(
1314 kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP));
1315 EXPECT_EQ(0, turnport2->SetOption(talk_base::Socket::OPT_DSCP,
1316 talk_base::DSCP_CS6));
1317 EXPECT_EQ(0, turnport2->GetOption(talk_base::Socket::OPT_DSCP, &dscp));
1318 EXPECT_EQ(talk_base::DSCP_CS6, dscp);
1321 // Test sending STUN messages in GICE format.
1322 TEST_F(PortTest, TestSendStunMessageAsGice) {
1323 talk_base::scoped_ptr<TestPort> lport(
1324 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
1325 talk_base::scoped_ptr<TestPort> rport(
1326 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1327 lport->SetIceProtocolType(ICEPROTO_GOOGLE);
1328 rport->SetIceProtocolType(ICEPROTO_GOOGLE);
1330 // Send a fake ping from lport to rport.
1331 lport->PrepareAddress();
1332 rport->PrepareAddress();
1333 ASSERT_FALSE(rport->Candidates().empty());
1334 Connection* conn = lport->CreateConnection(rport->Candidates()[0],
1335 Port::ORIGIN_MESSAGE);
1336 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
1339 // Check that it's a proper BINDING-REQUEST.
1340 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000);
1341 IceMessage* msg = lport->last_stun_msg();
1342 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1343 EXPECT_FALSE(msg->IsLegacy());
1344 const StunByteStringAttribute* username_attr = msg->GetByteString(
1345 STUN_ATTR_USERNAME);
1346 ASSERT_TRUE(username_attr != NULL);
1347 EXPECT_EQ("rfraglfrag", username_attr->GetString());
1348 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) == NULL);
1349 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL);
1350 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_FINGERPRINT) == NULL);
1352 // Save a copy of the BINDING-REQUEST for use below.
1353 talk_base::scoped_ptr<IceMessage> request(CopyStunMessage(msg));
1355 // Respond with a BINDING-RESPONSE.
1356 rport->SendBindingResponse(request.get(), lport->Candidates()[0].address());
1357 msg = rport->last_stun_msg();
1358 ASSERT_TRUE(msg != NULL);
1359 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
1360 EXPECT_FALSE(msg->IsLegacy());
1361 username_attr = msg->GetByteString(STUN_ATTR_USERNAME);
1362 ASSERT_TRUE(username_attr != NULL); // GICE has a username in the response.
1363 EXPECT_EQ("rfraglfrag", username_attr->GetString());
1364 const StunAddressAttribute* addr_attr = msg->GetAddress(
1365 STUN_ATTR_MAPPED_ADDRESS);
1366 ASSERT_TRUE(addr_attr != NULL);
1367 EXPECT_EQ(lport->Candidates()[0].address(), addr_attr->GetAddress());
1368 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_XOR_MAPPED_ADDRESS) == NULL);
1369 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) == NULL);
1370 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL);
1371 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_FINGERPRINT) == NULL);
1373 // Respond with a BINDING-ERROR-RESPONSE. This wouldn't happen in real life,
1374 // but we can do it here.
1375 rport->SendBindingErrorResponse(request.get(),
1376 rport->Candidates()[0].address(),
1377 STUN_ERROR_SERVER_ERROR,
1378 STUN_ERROR_REASON_SERVER_ERROR);
1379 msg = rport->last_stun_msg();
1380 ASSERT_TRUE(msg != NULL);
1381 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type());
1382 EXPECT_FALSE(msg->IsLegacy());
1383 username_attr = msg->GetByteString(STUN_ATTR_USERNAME);
1384 ASSERT_TRUE(username_attr != NULL); // GICE has a username in the response.
1385 EXPECT_EQ("rfraglfrag", username_attr->GetString());
1386 const StunErrorCodeAttribute* error_attr = msg->GetErrorCode();
1387 ASSERT_TRUE(error_attr != NULL);
1388 // The GICE wire format for error codes is incorrect.
1389 EXPECT_EQ(STUN_ERROR_SERVER_ERROR_AS_GICE, error_attr->code());
1390 EXPECT_EQ(STUN_ERROR_SERVER_ERROR / 256, error_attr->eclass());
1391 EXPECT_EQ(STUN_ERROR_SERVER_ERROR % 256, error_attr->number());
1392 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), error_attr->reason());
1393 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL);
1394 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) == NULL);
1395 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_FINGERPRINT) == NULL);
1398 // Test sending STUN messages in ICE format.
1399 TEST_F(PortTest, TestSendStunMessageAsIce) {
1400 talk_base::scoped_ptr<TestPort> lport(
1401 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
1402 talk_base::scoped_ptr<TestPort> rport(
1403 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1404 lport->SetIceProtocolType(ICEPROTO_RFC5245);
1405 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1406 lport->SetIceTiebreaker(kTiebreaker1);
1407 rport->SetIceProtocolType(ICEPROTO_RFC5245);
1408 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
1409 rport->SetIceTiebreaker(kTiebreaker2);
1411 // Send a fake ping from lport to rport.
1412 lport->PrepareAddress();
1413 rport->PrepareAddress();
1414 ASSERT_FALSE(rport->Candidates().empty());
1415 Connection* lconn = lport->CreateConnection(
1416 rport->Candidates()[0], Port::ORIGIN_MESSAGE);
1417 Connection* rconn = rport->CreateConnection(
1418 lport->Candidates()[0], Port::ORIGIN_MESSAGE);
1421 // Check that it's a proper BINDING-REQUEST.
1422 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000);
1423 IceMessage* msg = lport->last_stun_msg();
1424 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1425 EXPECT_FALSE(msg->IsLegacy());
1426 const StunByteStringAttribute* username_attr =
1427 msg->GetByteString(STUN_ATTR_USERNAME);
1428 ASSERT_TRUE(username_attr != NULL);
1429 const StunUInt32Attribute* priority_attr = msg->GetUInt32(STUN_ATTR_PRIORITY);
1430 ASSERT_TRUE(priority_attr != NULL);
1431 EXPECT_EQ(kDefaultPrflxPriority, priority_attr->value());
1432 EXPECT_EQ("rfrag:lfrag", username_attr->GetString());
1433 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL);
1434 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity(
1435 lport->last_stun_buf()->Data(), lport->last_stun_buf()->Length(),
1437 const StunUInt64Attribute* ice_controlling_attr =
1438 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING);
1439 ASSERT_TRUE(ice_controlling_attr != NULL);
1440 EXPECT_EQ(lport->IceTiebreaker(), ice_controlling_attr->value());
1441 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL);
1442 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL);
1443 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL);
1444 EXPECT_TRUE(StunMessage::ValidateFingerprint(
1445 lport->last_stun_buf()->Data(), lport->last_stun_buf()->Length()));
1447 // Request should not include ping count.
1448 ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL);
1450 // Save a copy of the BINDING-REQUEST for use below.
1451 talk_base::scoped_ptr<IceMessage> request(CopyStunMessage(msg));
1453 // Respond with a BINDING-RESPONSE.
1454 rport->SendBindingResponse(request.get(), lport->Candidates()[0].address());
1455 msg = rport->last_stun_msg();
1456 ASSERT_TRUE(msg != NULL);
1457 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
1460 EXPECT_FALSE(msg->IsLegacy());
1461 const StunAddressAttribute* addr_attr = msg->GetAddress(
1462 STUN_ATTR_XOR_MAPPED_ADDRESS);
1463 ASSERT_TRUE(addr_attr != NULL);
1464 EXPECT_EQ(lport->Candidates()[0].address(), addr_attr->GetAddress());
1465 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL);
1466 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity(
1467 rport->last_stun_buf()->Data(), rport->last_stun_buf()->Length(),
1469 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL);
1470 EXPECT_TRUE(StunMessage::ValidateFingerprint(
1471 lport->last_stun_buf()->Data(), lport->last_stun_buf()->Length()));
1472 // No USERNAME or PRIORITY in ICE responses.
1473 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL);
1474 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL);
1475 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MAPPED_ADDRESS) == NULL);
1476 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLING) == NULL);
1477 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL);
1478 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL);
1480 // Response should not include ping count.
1481 ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL);
1483 // Respond with a BINDING-ERROR-RESPONSE. This wouldn't happen in real life,
1484 // but we can do it here.
1485 rport->SendBindingErrorResponse(request.get(),
1486 lport->Candidates()[0].address(),
1487 STUN_ERROR_SERVER_ERROR,
1488 STUN_ERROR_REASON_SERVER_ERROR);
1489 msg = rport->last_stun_msg();
1490 ASSERT_TRUE(msg != NULL);
1491 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type());
1492 EXPECT_FALSE(msg->IsLegacy());
1493 const StunErrorCodeAttribute* error_attr = msg->GetErrorCode();
1494 ASSERT_TRUE(error_attr != NULL);
1495 EXPECT_EQ(STUN_ERROR_SERVER_ERROR, error_attr->code());
1496 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), error_attr->reason());
1497 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL);
1498 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity(
1499 rport->last_stun_buf()->Data(), rport->last_stun_buf()->Length(),
1501 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL);
1502 EXPECT_TRUE(StunMessage::ValidateFingerprint(
1503 lport->last_stun_buf()->Data(), lport->last_stun_buf()->Length()));
1504 // No USERNAME with ICE.
1505 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL);
1506 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL);
1508 // Testing STUN binding requests from rport --> lport, having ICE_CONTROLLED
1509 // and (incremented) RETRANSMIT_COUNT attributes.
1511 rport->set_send_retransmit_count_attribute(true);
1515 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000);
1516 msg = rport->last_stun_msg();
1517 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1518 const StunUInt64Attribute* ice_controlled_attr =
1519 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED);
1520 ASSERT_TRUE(ice_controlled_attr != NULL);
1521 EXPECT_EQ(rport->IceTiebreaker(), ice_controlled_attr->value());
1522 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL);
1524 // Request should include ping count.
1525 const StunUInt32Attribute* retransmit_attr =
1526 msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT);
1527 ASSERT_TRUE(retransmit_attr != NULL);
1528 EXPECT_EQ(2U, retransmit_attr->value());
1530 // Respond with a BINDING-RESPONSE.
1531 request.reset(CopyStunMessage(msg));
1532 lport->SendBindingResponse(request.get(), rport->Candidates()[0].address());
1533 msg = lport->last_stun_msg();
1535 // Response should include same ping count.
1536 retransmit_attr = msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT);
1537 ASSERT_TRUE(retransmit_attr != NULL);
1538 EXPECT_EQ(2U, retransmit_attr->value());
1541 TEST_F(PortTest, TestUseCandidateAttribute) {
1542 talk_base::scoped_ptr<TestPort> lport(
1543 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
1544 talk_base::scoped_ptr<TestPort> rport(
1545 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1546 lport->SetIceProtocolType(ICEPROTO_RFC5245);
1547 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1548 lport->SetIceTiebreaker(kTiebreaker1);
1549 rport->SetIceProtocolType(ICEPROTO_RFC5245);
1550 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
1551 rport->SetIceTiebreaker(kTiebreaker2);
1553 // Send a fake ping from lport to rport.
1554 lport->PrepareAddress();
1555 rport->PrepareAddress();
1556 ASSERT_FALSE(rport->Candidates().empty());
1557 Connection* lconn = lport->CreateConnection(
1558 rport->Candidates()[0], Port::ORIGIN_MESSAGE);
1560 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000);
1561 IceMessage* msg = lport->last_stun_msg();
1562 const StunUInt64Attribute* ice_controlling_attr =
1563 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING);
1564 ASSERT_TRUE(ice_controlling_attr != NULL);
1565 const StunByteStringAttribute* use_candidate_attr = msg->GetByteString(
1566 STUN_ATTR_USE_CANDIDATE);
1567 ASSERT_TRUE(use_candidate_attr != NULL);
1570 // Test handling STUN messages in GICE format.
1571 TEST_F(PortTest, TestHandleStunMessageAsGice) {
1572 // Our port will act as the "remote" port.
1573 talk_base::scoped_ptr<TestPort> port(
1574 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1575 port->SetIceProtocolType(ICEPROTO_GOOGLE);
1577 talk_base::scoped_ptr<IceMessage> in_msg, out_msg;
1578 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
1579 talk_base::SocketAddress addr(kLocalAddr1);
1580 std::string username;
1582 // BINDING-REQUEST from local to remote with valid GICE username and no M-I.
1583 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1585 WriteStunMessage(in_msg.get(), buf.get());
1586 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1587 out_msg.accept(), &username));
1588 EXPECT_TRUE(out_msg.get() != NULL); // Succeeds, since this is GICE.
1589 EXPECT_EQ("lfrag", username);
1591 // Add M-I; should be ignored and rest of message parsed normally.
1592 in_msg->AddMessageIntegrity("password");
1593 WriteStunMessage(in_msg.get(), buf.get());
1594 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1595 out_msg.accept(), &username));
1596 EXPECT_TRUE(out_msg.get() != NULL);
1597 EXPECT_EQ("lfrag", username);
1599 // BINDING-RESPONSE with username, as done in GICE. Should succeed.
1600 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_RESPONSE,
1602 in_msg->AddAttribute(
1603 new StunAddressAttribute(STUN_ATTR_MAPPED_ADDRESS, kLocalAddr2));
1604 WriteStunMessage(in_msg.get(), buf.get());
1605 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1606 out_msg.accept(), &username));
1607 EXPECT_TRUE(out_msg.get() != NULL);
1608 EXPECT_EQ("", username);
1610 // BINDING-RESPONSE without username. Should be tolerated as well.
1611 in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE));
1612 in_msg->AddAttribute(
1613 new StunAddressAttribute(STUN_ATTR_MAPPED_ADDRESS, kLocalAddr2));
1614 WriteStunMessage(in_msg.get(), buf.get());
1615 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1616 out_msg.accept(), &username));
1617 EXPECT_TRUE(out_msg.get() != NULL);
1618 EXPECT_EQ("", username);
1620 // BINDING-ERROR-RESPONSE with username and error code.
1621 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_ERROR_RESPONSE,
1623 in_msg->AddAttribute(new StunErrorCodeAttribute(STUN_ATTR_ERROR_CODE,
1624 STUN_ERROR_SERVER_ERROR_AS_GICE, STUN_ERROR_REASON_SERVER_ERROR));
1625 WriteStunMessage(in_msg.get(), buf.get());
1626 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1627 out_msg.accept(), &username));
1628 ASSERT_TRUE(out_msg.get() != NULL);
1629 EXPECT_EQ("", username);
1630 ASSERT_TRUE(out_msg->GetErrorCode() != NULL);
1631 // GetStunMessage doesn't unmunge the GICE error code (happens downstream).
1632 EXPECT_EQ(STUN_ERROR_SERVER_ERROR_AS_GICE, out_msg->GetErrorCode()->code());
1633 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR),
1634 out_msg->GetErrorCode()->reason());
1637 // Test handling STUN messages in ICE format.
1638 TEST_F(PortTest, TestHandleStunMessageAsIce) {
1639 // Our port will act as the "remote" port.
1640 talk_base::scoped_ptr<TestPort> port(
1641 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1642 port->SetIceProtocolType(ICEPROTO_RFC5245);
1644 talk_base::scoped_ptr<IceMessage> in_msg, out_msg;
1645 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
1646 talk_base::SocketAddress addr(kLocalAddr1);
1647 std::string username;
1649 // BINDING-REQUEST from local to remote with valid ICE username,
1650 // MESSAGE-INTEGRITY, and FINGERPRINT.
1651 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1653 in_msg->AddMessageIntegrity("rpass");
1654 in_msg->AddFingerprint();
1655 WriteStunMessage(in_msg.get(), buf.get());
1656 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1657 out_msg.accept(), &username));
1658 EXPECT_TRUE(out_msg.get() != NULL);
1659 EXPECT_EQ("lfrag", username);
1661 // BINDING-RESPONSE without username, with MESSAGE-INTEGRITY and FINGERPRINT.
1662 in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE));
1663 in_msg->AddAttribute(
1664 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2));
1665 in_msg->AddMessageIntegrity("rpass");
1666 in_msg->AddFingerprint();
1667 WriteStunMessage(in_msg.get(), buf.get());
1668 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1669 out_msg.accept(), &username));
1670 EXPECT_TRUE(out_msg.get() != NULL);
1671 EXPECT_EQ("", username);
1673 // BINDING-ERROR-RESPONSE without username, with error, M-I, and FINGERPRINT.
1674 in_msg.reset(CreateStunMessage(STUN_BINDING_ERROR_RESPONSE));
1675 in_msg->AddAttribute(new StunErrorCodeAttribute(STUN_ATTR_ERROR_CODE,
1676 STUN_ERROR_SERVER_ERROR, STUN_ERROR_REASON_SERVER_ERROR));
1677 in_msg->AddFingerprint();
1678 WriteStunMessage(in_msg.get(), buf.get());
1679 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1680 out_msg.accept(), &username));
1681 EXPECT_TRUE(out_msg.get() != NULL);
1682 EXPECT_EQ("", username);
1683 ASSERT_TRUE(out_msg->GetErrorCode() != NULL);
1684 EXPECT_EQ(STUN_ERROR_SERVER_ERROR, out_msg->GetErrorCode()->code());
1685 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR),
1686 out_msg->GetErrorCode()->reason());
1689 // This test verifies port can handle ICE messages in Hybrid mode and switches
1690 // ICEPROTO_RFC5245 mode after successfully handling the message.
1691 TEST_F(PortTest, TestHandleStunMessageAsIceInHybridMode) {
1692 // Our port will act as the "remote" port.
1693 talk_base::scoped_ptr<TestPort> port(
1694 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1695 port->SetIceProtocolType(ICEPROTO_HYBRID);
1697 talk_base::scoped_ptr<IceMessage> in_msg, out_msg;
1698 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
1699 talk_base::SocketAddress addr(kLocalAddr1);
1700 std::string username;
1702 // BINDING-REQUEST from local to remote with valid ICE username,
1703 // MESSAGE-INTEGRITY, and FINGERPRINT.
1704 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1706 in_msg->AddMessageIntegrity("rpass");
1707 in_msg->AddFingerprint();
1708 WriteStunMessage(in_msg.get(), buf.get());
1709 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1710 out_msg.accept(), &username));
1711 EXPECT_TRUE(out_msg.get() != NULL);
1712 EXPECT_EQ("lfrag", username);
1713 EXPECT_EQ(ICEPROTO_RFC5245, port->IceProtocol());
1716 // This test verifies port can handle GICE messages in Hybrid mode and switches
1717 // ICEPROTO_GOOGLE mode after successfully handling the message.
1718 TEST_F(PortTest, TestHandleStunMessageAsGiceInHybridMode) {
1719 // Our port will act as the "remote" port.
1720 talk_base::scoped_ptr<TestPort> port(
1721 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1722 port->SetIceProtocolType(ICEPROTO_HYBRID);
1724 talk_base::scoped_ptr<IceMessage> in_msg, out_msg;
1725 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
1726 talk_base::SocketAddress addr(kLocalAddr1);
1727 std::string username;
1729 // BINDING-REQUEST from local to remote with valid GICE username and no M-I.
1730 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1732 WriteStunMessage(in_msg.get(), buf.get());
1733 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1734 out_msg.accept(), &username));
1735 EXPECT_TRUE(out_msg.get() != NULL); // Succeeds, since this is GICE.
1736 EXPECT_EQ("lfrag", username);
1737 EXPECT_EQ(ICEPROTO_GOOGLE, port->IceProtocol());
1740 // Verify port is not switched out of RFC5245 mode if GICE message is received
1742 TEST_F(PortTest, TestHandleStunMessageAsGiceInIceMode) {
1743 // Our port will act as the "remote" port.
1744 talk_base::scoped_ptr<TestPort> port(
1745 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1746 port->SetIceProtocolType(ICEPROTO_RFC5245);
1748 talk_base::scoped_ptr<IceMessage> in_msg, out_msg;
1749 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
1750 talk_base::SocketAddress addr(kLocalAddr1);
1751 std::string username;
1753 // BINDING-REQUEST from local to remote with valid GICE username and no M-I.
1754 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1756 WriteStunMessage(in_msg.get(), buf.get());
1757 // Should fail as there is no MI and fingerprint.
1758 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1759 out_msg.accept(), &username));
1760 EXPECT_EQ(ICEPROTO_RFC5245, port->IceProtocol());
1764 // Tests handling of GICE binding requests with missing or incorrect usernames.
1765 TEST_F(PortTest, TestHandleStunMessageAsGiceBadUsername) {
1766 talk_base::scoped_ptr<TestPort> port(
1767 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1768 port->SetIceProtocolType(ICEPROTO_GOOGLE);
1770 talk_base::scoped_ptr<IceMessage> in_msg, out_msg;
1771 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
1772 talk_base::SocketAddress addr(kLocalAddr1);
1773 std::string username;
1775 // BINDING-REQUEST with no username.
1776 in_msg.reset(CreateStunMessage(STUN_BINDING_REQUEST));
1777 WriteStunMessage(in_msg.get(), buf.get());
1778 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1779 out_msg.accept(), &username));
1780 EXPECT_TRUE(out_msg.get() == NULL);
1781 EXPECT_EQ("", username);
1782 EXPECT_EQ(STUN_ERROR_BAD_REQUEST_AS_GICE, port->last_stun_error_code());
1784 // BINDING-REQUEST with empty username.
1785 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, ""));
1786 WriteStunMessage(in_msg.get(), buf.get());
1787 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1788 out_msg.accept(), &username));
1789 EXPECT_TRUE(out_msg.get() == NULL);
1790 EXPECT_EQ("", username);
1791 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED_AS_GICE, port->last_stun_error_code());
1793 // BINDING-REQUEST with too-short username.
1794 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "lfra"));
1795 WriteStunMessage(in_msg.get(), buf.get());
1796 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1797 out_msg.accept(), &username));
1798 EXPECT_TRUE(out_msg.get() == NULL);
1799 EXPECT_EQ("", username);
1800 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED_AS_GICE, port->last_stun_error_code());
1802 // BINDING-REQUEST with reversed username.
1803 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1805 WriteStunMessage(in_msg.get(), buf.get());
1806 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1807 out_msg.accept(), &username));
1808 EXPECT_TRUE(out_msg.get() == NULL);
1809 EXPECT_EQ("", username);
1810 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED_AS_GICE, port->last_stun_error_code());
1812 // BINDING-REQUEST with garbage username.
1813 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1815 WriteStunMessage(in_msg.get(), buf.get());
1816 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1817 out_msg.accept(), &username));
1818 EXPECT_TRUE(out_msg.get() == NULL);
1819 EXPECT_EQ("", username);
1820 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED_AS_GICE, port->last_stun_error_code());
1823 // Tests handling of ICE binding requests with missing or incorrect usernames.
1824 TEST_F(PortTest, TestHandleStunMessageAsIceBadUsername) {
1825 talk_base::scoped_ptr<TestPort> port(
1826 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1827 port->SetIceProtocolType(ICEPROTO_RFC5245);
1829 talk_base::scoped_ptr<IceMessage> in_msg, out_msg;
1830 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
1831 talk_base::SocketAddress addr(kLocalAddr1);
1832 std::string username;
1834 // BINDING-REQUEST with no username.
1835 in_msg.reset(CreateStunMessage(STUN_BINDING_REQUEST));
1836 in_msg->AddMessageIntegrity("rpass");
1837 in_msg->AddFingerprint();
1838 WriteStunMessage(in_msg.get(), buf.get());
1839 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1840 out_msg.accept(), &username));
1841 EXPECT_TRUE(out_msg.get() == NULL);
1842 EXPECT_EQ("", username);
1843 EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code());
1845 // BINDING-REQUEST with empty username.
1846 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, ""));
1847 in_msg->AddMessageIntegrity("rpass");
1848 in_msg->AddFingerprint();
1849 WriteStunMessage(in_msg.get(), buf.get());
1850 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1851 out_msg.accept(), &username));
1852 EXPECT_TRUE(out_msg.get() == NULL);
1853 EXPECT_EQ("", username);
1854 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
1856 // BINDING-REQUEST with too-short username.
1857 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfra"));
1858 in_msg->AddMessageIntegrity("rpass");
1859 in_msg->AddFingerprint();
1860 WriteStunMessage(in_msg.get(), buf.get());
1861 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1862 out_msg.accept(), &username));
1863 EXPECT_TRUE(out_msg.get() == NULL);
1864 EXPECT_EQ("", username);
1865 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
1867 // BINDING-REQUEST with reversed username.
1868 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1870 in_msg->AddMessageIntegrity("rpass");
1871 in_msg->AddFingerprint();
1872 WriteStunMessage(in_msg.get(), buf.get());
1873 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1874 out_msg.accept(), &username));
1875 EXPECT_TRUE(out_msg.get() == NULL);
1876 EXPECT_EQ("", username);
1877 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
1879 // BINDING-REQUEST with garbage username.
1880 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1882 in_msg->AddMessageIntegrity("rpass");
1883 in_msg->AddFingerprint();
1884 WriteStunMessage(in_msg.get(), buf.get());
1885 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1886 out_msg.accept(), &username));
1887 EXPECT_TRUE(out_msg.get() == NULL);
1888 EXPECT_EQ("", username);
1889 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
1892 // Test handling STUN messages (as ICE) with missing or malformed M-I.
1893 TEST_F(PortTest, TestHandleStunMessageAsIceBadMessageIntegrity) {
1894 // Our port will act as the "remote" port.
1895 talk_base::scoped_ptr<TestPort> port(
1896 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1897 port->SetIceProtocolType(ICEPROTO_RFC5245);
1899 talk_base::scoped_ptr<IceMessage> in_msg, out_msg;
1900 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
1901 talk_base::SocketAddress addr(kLocalAddr1);
1902 std::string username;
1904 // BINDING-REQUEST from local to remote with valid ICE username and
1905 // FINGERPRINT, but no MESSAGE-INTEGRITY.
1906 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1908 in_msg->AddFingerprint();
1909 WriteStunMessage(in_msg.get(), buf.get());
1910 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1911 out_msg.accept(), &username));
1912 EXPECT_TRUE(out_msg.get() == NULL);
1913 EXPECT_EQ("", username);
1914 EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code());
1916 // BINDING-REQUEST from local to remote with valid ICE username and
1917 // FINGERPRINT, but invalid MESSAGE-INTEGRITY.
1918 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1920 in_msg->AddMessageIntegrity("invalid");
1921 in_msg->AddFingerprint();
1922 WriteStunMessage(in_msg.get(), buf.get());
1923 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1924 out_msg.accept(), &username));
1925 EXPECT_TRUE(out_msg.get() == NULL);
1926 EXPECT_EQ("", username);
1927 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
1929 // TODO: BINDING-RESPONSES and BINDING-ERROR-RESPONSES are checked
1930 // by the Connection, not the Port, since they require the remote username.
1931 // Change this test to pass in data via Connection::OnReadPacket instead.
1934 // Test handling STUN messages (as ICE) with missing or malformed FINGERPRINT.
1935 TEST_F(PortTest, TestHandleStunMessageAsIceBadFingerprint) {
1936 // Our port will act as the "remote" port.
1937 talk_base::scoped_ptr<TestPort> port(
1938 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1939 port->SetIceProtocolType(ICEPROTO_RFC5245);
1941 talk_base::scoped_ptr<IceMessage> in_msg, out_msg;
1942 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
1943 talk_base::SocketAddress addr(kLocalAddr1);
1944 std::string username;
1946 // BINDING-REQUEST from local to remote with valid ICE username and
1947 // MESSAGE-INTEGRITY, but no FINGERPRINT; GetStunMessage should fail.
1948 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1950 in_msg->AddMessageIntegrity("rpass");
1951 WriteStunMessage(in_msg.get(), buf.get());
1952 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1953 out_msg.accept(), &username));
1954 EXPECT_EQ(0, port->last_stun_error_code());
1956 // Now, add a fingerprint, but munge the message so it's not valid.
1957 in_msg->AddFingerprint();
1958 in_msg->SetTransactionID("TESTTESTBADD");
1959 WriteStunMessage(in_msg.get(), buf.get());
1960 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1961 out_msg.accept(), &username));
1962 EXPECT_EQ(0, port->last_stun_error_code());
1964 // Valid BINDING-RESPONSE, except no FINGERPRINT.
1965 in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE));
1966 in_msg->AddAttribute(
1967 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2));
1968 in_msg->AddMessageIntegrity("rpass");
1969 WriteStunMessage(in_msg.get(), buf.get());
1970 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1971 out_msg.accept(), &username));
1972 EXPECT_EQ(0, port->last_stun_error_code());
1974 // Now, add a fingerprint, but munge the message so it's not valid.
1975 in_msg->AddFingerprint();
1976 in_msg->SetTransactionID("TESTTESTBADD");
1977 WriteStunMessage(in_msg.get(), buf.get());
1978 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1979 out_msg.accept(), &username));
1980 EXPECT_EQ(0, port->last_stun_error_code());
1982 // Valid BINDING-ERROR-RESPONSE, except no FINGERPRINT.
1983 in_msg.reset(CreateStunMessage(STUN_BINDING_ERROR_RESPONSE));
1984 in_msg->AddAttribute(new StunErrorCodeAttribute(STUN_ATTR_ERROR_CODE,
1985 STUN_ERROR_SERVER_ERROR, STUN_ERROR_REASON_SERVER_ERROR));
1986 in_msg->AddMessageIntegrity("rpass");
1987 WriteStunMessage(in_msg.get(), buf.get());
1988 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1989 out_msg.accept(), &username));
1990 EXPECT_EQ(0, port->last_stun_error_code());
1992 // Now, add a fingerprint, but munge the message so it's not valid.
1993 in_msg->AddFingerprint();
1994 in_msg->SetTransactionID("TESTTESTBADD");
1995 WriteStunMessage(in_msg.get(), buf.get());
1996 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr,
1997 out_msg.accept(), &username));
1998 EXPECT_EQ(0, port->last_stun_error_code());
2001 // Test handling of STUN binding indication messages (as ICE). STUN binding
2002 // indications are allowed only to the connection which is in read mode.
2003 TEST_F(PortTest, TestHandleStunBindingIndication) {
2004 talk_base::scoped_ptr<TestPort> lport(
2005 CreateTestPort(kLocalAddr2, "lfrag", "lpass"));
2006 lport->SetIceProtocolType(ICEPROTO_RFC5245);
2007 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
2008 lport->SetIceTiebreaker(kTiebreaker1);
2010 // Verifying encoding and decoding STUN indication message.
2011 talk_base::scoped_ptr<IceMessage> in_msg, out_msg;
2012 talk_base::scoped_ptr<ByteBuffer> buf(new ByteBuffer());
2013 talk_base::SocketAddress addr(kLocalAddr1);
2014 std::string username;
2016 in_msg.reset(CreateStunMessage(STUN_BINDING_INDICATION));
2017 in_msg->AddFingerprint();
2018 WriteStunMessage(in_msg.get(), buf.get());
2019 EXPECT_TRUE(lport->GetStunMessage(buf->Data(), buf->Length(), addr,
2020 out_msg.accept(), &username));
2021 EXPECT_TRUE(out_msg.get() != NULL);
2022 EXPECT_EQ(out_msg->type(), STUN_BINDING_INDICATION);
2023 EXPECT_EQ("", username);
2025 // Verify connection can handle STUN indication and updates
2026 // last_ping_received.
2027 talk_base::scoped_ptr<TestPort> rport(
2028 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
2029 rport->SetIceProtocolType(ICEPROTO_RFC5245);
2030 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
2031 rport->SetIceTiebreaker(kTiebreaker2);
2033 lport->PrepareAddress();
2034 rport->PrepareAddress();
2035 ASSERT_FALSE(lport->Candidates().empty());
2036 ASSERT_FALSE(rport->Candidates().empty());
2038 Connection* lconn = lport->CreateConnection(rport->Candidates()[0],
2039 Port::ORIGIN_MESSAGE);
2040 Connection* rconn = rport->CreateConnection(lport->Candidates()[0],
2041 Port::ORIGIN_MESSAGE);
2044 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000);
2045 IceMessage* msg = rport->last_stun_msg();
2046 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
2047 // Send rport binding request to lport.
2048 lconn->OnReadPacket(rport->last_stun_buf()->Data(),
2049 rport->last_stun_buf()->Length(),
2050 talk_base::PacketTime());
2051 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000);
2052 EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type());
2053 uint32 last_ping_received1 = lconn->last_ping_received();
2055 // Adding a delay of 100ms.
2056 talk_base::Thread::Current()->ProcessMessages(100);
2057 // Pinging lconn using stun indication message.
2058 lconn->OnReadPacket(buf->Data(), buf->Length(), talk_base::PacketTime());
2059 uint32 last_ping_received2 = lconn->last_ping_received();
2060 EXPECT_GT(last_ping_received2, last_ping_received1);
2063 TEST_F(PortTest, TestComputeCandidatePriority) {
2064 talk_base::scoped_ptr<TestPort> port(
2065 CreateTestPort(kLocalAddr1, "name", "pass"));
2066 port->set_type_preference(90);
2067 port->set_component(177);
2068 port->AddCandidateAddress(SocketAddress("192.168.1.4", 1234));
2069 port->AddCandidateAddress(SocketAddress("2001:db8::1234", 1234));
2070 port->AddCandidateAddress(SocketAddress("fc12:3456::1234", 1234));
2071 port->AddCandidateAddress(SocketAddress("::ffff:192.168.1.4", 1234));
2072 port->AddCandidateAddress(SocketAddress("::192.168.1.4", 1234));
2073 port->AddCandidateAddress(SocketAddress("2002::1234:5678", 1234));
2074 port->AddCandidateAddress(SocketAddress("2001::1234:5678", 1234));
2075 port->AddCandidateAddress(SocketAddress("fecf::1234:5678", 1234));
2076 port->AddCandidateAddress(SocketAddress("3ffe::1234:5678", 1234));
2077 // These should all be:
2078 // (90 << 24) | ([rfc3484 pref value] << 8) | (256 - 177)
2079 uint32 expected_priority_v4 = 1509957199U;
2080 uint32 expected_priority_v6 = 1509959759U;
2081 uint32 expected_priority_ula = 1509962319U;
2082 uint32 expected_priority_v4mapped = expected_priority_v4;
2083 uint32 expected_priority_v4compat = 1509949775U;
2084 uint32 expected_priority_6to4 = 1509954639U;
2085 uint32 expected_priority_teredo = 1509952079U;
2086 uint32 expected_priority_sitelocal = 1509949775U;
2087 uint32 expected_priority_6bone = 1509949775U;
2088 ASSERT_EQ(expected_priority_v4, port->Candidates()[0].priority());
2089 ASSERT_EQ(expected_priority_v6, port->Candidates()[1].priority());
2090 ASSERT_EQ(expected_priority_ula, port->Candidates()[2].priority());
2091 ASSERT_EQ(expected_priority_v4mapped, port->Candidates()[3].priority());
2092 ASSERT_EQ(expected_priority_v4compat, port->Candidates()[4].priority());
2093 ASSERT_EQ(expected_priority_6to4, port->Candidates()[5].priority());
2094 ASSERT_EQ(expected_priority_teredo, port->Candidates()[6].priority());
2095 ASSERT_EQ(expected_priority_sitelocal, port->Candidates()[7].priority());
2096 ASSERT_EQ(expected_priority_6bone, port->Candidates()[8].priority());
2099 TEST_F(PortTest, TestPortProxyProperties) {
2100 talk_base::scoped_ptr<TestPort> port(
2101 CreateTestPort(kLocalAddr1, "name", "pass"));
2102 port->SetIceRole(cricket::ICEROLE_CONTROLLING);
2103 port->SetIceTiebreaker(kTiebreaker1);
2105 // Create a proxy port.
2106 talk_base::scoped_ptr<PortProxy> proxy(new PortProxy());
2107 proxy->set_impl(port.get());
2108 EXPECT_EQ(port->Type(), proxy->Type());
2109 EXPECT_EQ(port->Network(), proxy->Network());
2110 EXPECT_EQ(port->GetIceRole(), proxy->GetIceRole());
2111 EXPECT_EQ(port->IceTiebreaker(), proxy->IceTiebreaker());
2114 // In the case of shared socket, one port may be shared by local and stun.
2115 // Test that candidates with different types will have different foundation.
2116 TEST_F(PortTest, TestFoundation) {
2117 talk_base::scoped_ptr<TestPort> testport(
2118 CreateTestPort(kLocalAddr1, "name", "pass"));
2119 testport->AddCandidateAddress(kLocalAddr1, kLocalAddr1,
2121 cricket::ICE_TYPE_PREFERENCE_HOST, false);
2122 testport->AddCandidateAddress(kLocalAddr2, kLocalAddr1,
2124 cricket::ICE_TYPE_PREFERENCE_SRFLX, true);
2125 EXPECT_NE(testport->Candidates()[0].foundation(),
2126 testport->Candidates()[1].foundation());
2129 // This test verifies the foundation of different types of ICE candidates.
2130 TEST_F(PortTest, TestCandidateFoundation) {
2131 talk_base::scoped_ptr<talk_base::NATServer> nat_server(
2132 CreateNatServer(kNatAddr1, NAT_OPEN_CONE));
2133 talk_base::scoped_ptr<UDPPort> udpport1(CreateUdpPort(kLocalAddr1));
2134 udpport1->PrepareAddress();
2135 talk_base::scoped_ptr<UDPPort> udpport2(CreateUdpPort(kLocalAddr1));
2136 udpport2->PrepareAddress();
2137 EXPECT_EQ(udpport1->Candidates()[0].foundation(),
2138 udpport2->Candidates()[0].foundation());
2139 talk_base::scoped_ptr<TCPPort> tcpport1(CreateTcpPort(kLocalAddr1));
2140 tcpport1->PrepareAddress();
2141 talk_base::scoped_ptr<TCPPort> tcpport2(CreateTcpPort(kLocalAddr1));
2142 tcpport2->PrepareAddress();
2143 EXPECT_EQ(tcpport1->Candidates()[0].foundation(),
2144 tcpport2->Candidates()[0].foundation());
2145 talk_base::scoped_ptr<Port> stunport(
2146 CreateStunPort(kLocalAddr1, nat_socket_factory1()));
2147 stunport->PrepareAddress();
2148 ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kTimeout);
2149 EXPECT_NE(tcpport1->Candidates()[0].foundation(),
2150 stunport->Candidates()[0].foundation());
2151 EXPECT_NE(tcpport2->Candidates()[0].foundation(),
2152 stunport->Candidates()[0].foundation());
2153 EXPECT_NE(udpport1->Candidates()[0].foundation(),
2154 stunport->Candidates()[0].foundation());
2155 EXPECT_NE(udpport2->Candidates()[0].foundation(),
2156 stunport->Candidates()[0].foundation());
2157 // Verify GTURN candidate foundation.
2158 talk_base::scoped_ptr<RelayPort> relayport(
2159 CreateGturnPort(kLocalAddr1));
2160 relayport->AddServerAddress(
2161 cricket::ProtocolAddress(kRelayUdpIntAddr, cricket::PROTO_UDP));
2162 relayport->PrepareAddress();
2163 ASSERT_EQ_WAIT(1U, relayport->Candidates().size(), kTimeout);
2164 EXPECT_NE(udpport1->Candidates()[0].foundation(),
2165 relayport->Candidates()[0].foundation());
2166 EXPECT_NE(udpport2->Candidates()[0].foundation(),
2167 relayport->Candidates()[0].foundation());
2168 // Verifying TURN candidate foundation.
2169 talk_base::scoped_ptr<Port> turnport(CreateTurnPort(
2170 kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP));
2171 turnport->PrepareAddress();
2172 ASSERT_EQ_WAIT(1U, turnport->Candidates().size(), kTimeout);
2173 EXPECT_NE(udpport1->Candidates()[0].foundation(),
2174 turnport->Candidates()[0].foundation());
2175 EXPECT_NE(udpport2->Candidates()[0].foundation(),
2176 turnport->Candidates()[0].foundation());
2177 EXPECT_NE(stunport->Candidates()[0].foundation(),
2178 turnport->Candidates()[0].foundation());
2181 // This test verifies the related addresses of different types of
2183 TEST_F(PortTest, TestCandidateRelatedAddress) {
2184 talk_base::scoped_ptr<talk_base::NATServer> nat_server(
2185 CreateNatServer(kNatAddr1, NAT_OPEN_CONE));
2186 talk_base::scoped_ptr<UDPPort> udpport(CreateUdpPort(kLocalAddr1));
2187 udpport->PrepareAddress();
2188 // For UDPPort, related address will be empty.
2189 EXPECT_TRUE(udpport->Candidates()[0].related_address().IsNil());
2190 // Testing related address for stun candidates.
2191 // For stun candidate related address must be equal to the base
2193 talk_base::scoped_ptr<StunPort> stunport(
2194 CreateStunPort(kLocalAddr1, nat_socket_factory1()));
2195 stunport->PrepareAddress();
2196 ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kTimeout);
2197 // Check STUN candidate address.
2198 EXPECT_EQ(stunport->Candidates()[0].address().ipaddr(),
2199 kNatAddr1.ipaddr());
2200 // Check STUN candidate related address.
2201 EXPECT_EQ(stunport->Candidates()[0].related_address(),
2202 stunport->GetLocalAddress());
2203 // Verifying the related address for the GTURN candidates.
2204 // NOTE: In case of GTURN related address will be equal to the mapped
2205 // address, but address(mapped) will not be XOR.
2206 talk_base::scoped_ptr<RelayPort> relayport(
2207 CreateGturnPort(kLocalAddr1));
2208 relayport->AddServerAddress(
2209 cricket::ProtocolAddress(kRelayUdpIntAddr, cricket::PROTO_UDP));
2210 relayport->PrepareAddress();
2211 ASSERT_EQ_WAIT(1U, relayport->Candidates().size(), kTimeout);
2212 // For Gturn related address is set to "0.0.0.0:0"
2213 EXPECT_EQ(talk_base::SocketAddress(),
2214 relayport->Candidates()[0].related_address());
2215 // Verifying the related address for TURN candidate.
2216 // For TURN related address must be equal to the mapped address.
2217 talk_base::scoped_ptr<Port> turnport(CreateTurnPort(
2218 kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP));
2219 turnport->PrepareAddress();
2220 ASSERT_EQ_WAIT(1U, turnport->Candidates().size(), kTimeout);
2221 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(),
2222 turnport->Candidates()[0].address().ipaddr());
2223 EXPECT_EQ(kNatAddr1.ipaddr(),
2224 turnport->Candidates()[0].related_address().ipaddr());
2227 // Test priority value overflow handling when preference is set to 3.
2228 TEST_F(PortTest, TestCandidatePreference) {
2229 cricket::Candidate cand1;
2230 cand1.set_preference(3);
2231 cricket::Candidate cand2;
2232 cand2.set_preference(1);
2233 EXPECT_TRUE(cand1.preference() > cand2.preference());
2236 // Test the Connection priority is calculated correctly.
2237 TEST_F(PortTest, TestConnectionPriority) {
2238 talk_base::scoped_ptr<TestPort> lport(
2239 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
2240 lport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_HOST);
2241 talk_base::scoped_ptr<TestPort> rport(
2242 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
2243 rport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_RELAY);
2244 lport->set_component(123);
2245 lport->AddCandidateAddress(SocketAddress("192.168.1.4", 1234));
2246 rport->set_component(23);
2247 rport->AddCandidateAddress(SocketAddress("10.1.1.100", 1234));
2249 EXPECT_EQ(0x7E001E85U, lport->Candidates()[0].priority());
2250 EXPECT_EQ(0x2001EE9U, rport->Candidates()[0].priority());
2253 // pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0)
2254 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
2255 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
2256 Connection* lconn = lport->CreateConnection(
2257 rport->Candidates()[0], Port::ORIGIN_MESSAGE);
2259 EXPECT_EQ(0x2001EE9FC003D0BU, lconn->priority());
2261 EXPECT_EQ(0x2001EE9FC003D0BLLU, lconn->priority());
2264 lport->SetIceRole(cricket::ICEROLE_CONTROLLED);
2265 rport->SetIceRole(cricket::ICEROLE_CONTROLLING);
2266 Connection* rconn = rport->CreateConnection(
2267 lport->Candidates()[0], Port::ORIGIN_MESSAGE);
2269 EXPECT_EQ(0x2001EE9FC003D0AU, rconn->priority());
2271 EXPECT_EQ(0x2001EE9FC003D0ALLU, rconn->priority());
2275 TEST_F(PortTest, TestWritableState) {
2276 UDPPort* port1 = CreateUdpPort(kLocalAddr1);
2277 UDPPort* port2 = CreateUdpPort(kLocalAddr2);
2280 TestChannel ch1(port1, port2);
2281 TestChannel ch2(port2, port1);
2283 // Acquire addresses.
2286 ASSERT_EQ_WAIT(1, ch1.complete_count(), kTimeout);
2287 ASSERT_EQ_WAIT(1, ch2.complete_count(), kTimeout);
2289 // Send a ping from src to dst.
2290 ch1.CreateConnection();
2291 ASSERT_TRUE(ch1.conn() != NULL);
2292 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state());
2293 EXPECT_TRUE_WAIT(ch1.conn()->connected(), kTimeout); // for TCP connect
2295 WAIT(!ch2.remote_address().IsNil(), kTimeout);
2297 // Data should be unsendable until the connection is accepted.
2298 char data[] = "abcd";
2299 int data_size = ARRAY_SIZE(data);
2300 talk_base::PacketOptions options;
2301 EXPECT_EQ(SOCKET_ERROR, ch1.conn()->Send(data, data_size, options));
2303 // Accept the connection to return the binding response, transition to
2304 // writable, and allow data to be sent.
2305 ch2.AcceptConnection();
2306 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
2308 EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options));
2310 // Ask the connection to update state as if enough time has passed to lose
2311 // full writability and 5 pings went unresponded to. We'll accomplish the
2312 // latter by sending pings but not pumping messages.
2313 for (uint32 i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) {
2316 uint32 unreliable_timeout_delay = CONNECTION_WRITE_CONNECT_TIMEOUT + 500u;
2317 ch1.conn()->UpdateState(unreliable_timeout_delay);
2318 EXPECT_EQ(Connection::STATE_WRITE_UNRELIABLE, ch1.conn()->write_state());
2320 // Data should be able to be sent in this state.
2321 EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options));
2323 // And now allow the other side to process the pings and send binding
2325 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
2328 // Wait long enough for a full timeout (past however long we've already
2330 for (uint32 i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) {
2331 ch1.Ping(unreliable_timeout_delay + i);
2333 ch1.conn()->UpdateState(unreliable_timeout_delay + CONNECTION_WRITE_TIMEOUT +
2335 EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state());
2337 // Now that the connection has completely timed out, data send should fail.
2338 EXPECT_EQ(SOCKET_ERROR, ch1.conn()->Send(data, data_size, options));
2344 TEST_F(PortTest, TestTimeoutForNeverWritable) {
2345 UDPPort* port1 = CreateUdpPort(kLocalAddr1);
2346 UDPPort* port2 = CreateUdpPort(kLocalAddr2);
2349 TestChannel ch1(port1, port2);
2350 TestChannel ch2(port2, port1);
2352 // Acquire addresses.
2356 ch1.CreateConnection();
2357 ASSERT_TRUE(ch1.conn() != NULL);
2358 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state());
2360 // Attempt to go directly to write timeout.
2361 for (uint32 i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) {
2364 ch1.conn()->UpdateState(CONNECTION_WRITE_TIMEOUT + 500u);
2365 EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state());
2368 // This test verifies the connection setup between ICEMODE_FULL
2369 // and ICEMODE_LITE.
2370 // In this test |ch1| behaves like FULL mode client and we have created
2371 // port which responds to the ping message just like LITE client.
2372 TEST_F(PortTest, TestIceLiteConnectivity) {
2373 TestPort* ice_full_port = CreateTestPort(
2374 kLocalAddr1, "lfrag", "lpass", cricket::ICEPROTO_RFC5245,
2375 cricket::ICEROLE_CONTROLLING, kTiebreaker1);
2377 talk_base::scoped_ptr<TestPort> ice_lite_port(CreateTestPort(
2378 kLocalAddr2, "rfrag", "rpass", cricket::ICEPROTO_RFC5245,
2379 cricket::ICEROLE_CONTROLLED, kTiebreaker2));
2380 // Setup TestChannel. This behaves like FULL mode client.
2381 TestChannel ch1(ice_full_port, ice_lite_port.get());
2382 ch1.SetIceMode(ICEMODE_FULL);
2384 // Start gathering candidates.
2386 ice_lite_port->PrepareAddress();
2388 ASSERT_EQ_WAIT(1, ch1.complete_count(), kTimeout);
2389 ASSERT_FALSE(ice_lite_port->Candidates().empty());
2391 ch1.CreateConnection();
2392 ASSERT_TRUE(ch1.conn() != NULL);
2393 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state());
2395 // Send ping from full mode client.
2396 // This ping must not have USE_CANDIDATE_ATTR.
2399 // Verify stun ping is without USE_CANDIDATE_ATTR. Getting message directly
2401 ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != NULL, 1000);
2402 IceMessage* msg = ice_full_port->last_stun_msg();
2403 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL);
2405 // Respond with a BINDING-RESPONSE from litemode client.
2406 // NOTE: Ideally we should't create connection at this stage from lite
2407 // port, as it should be done only after receiving ping with USE_CANDIDATE.
2408 // But we need a connection to send a response message.
2409 ice_lite_port->CreateConnection(
2410 ice_full_port->Candidates()[0], cricket::Port::ORIGIN_MESSAGE);
2411 talk_base::scoped_ptr<IceMessage> request(CopyStunMessage(msg));
2412 ice_lite_port->SendBindingResponse(
2413 request.get(), ice_full_port->Candidates()[0].address());
2415 // Feeding the respone message from litemode to the full mode connection.
2416 ch1.conn()->OnReadPacket(ice_lite_port->last_stun_buf()->Data(),
2417 ice_lite_port->last_stun_buf()->Length(),
2418 talk_base::PacketTime());
2419 // Verifying full mode connection becomes writable from the response.
2420 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
2422 EXPECT_TRUE_WAIT(ch1.nominated(), kTimeout);
2424 // Clear existing stun messsages. Otherwise we will process old stun
2425 // message right after we send ping.
2426 ice_full_port->Reset();
2427 // Send ping. This must have USE_CANDIDATE_ATTR.
2429 ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != NULL, 1000);
2430 msg = ice_full_port->last_stun_msg();
2431 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL);
2435 // This test case verifies that the CONTROLLING port does not time out.
2436 TEST_F(PortTest, TestControllingNoTimeout) {
2437 SetIceProtocolType(cricket::ICEPROTO_RFC5245);
2438 UDPPort* port1 = CreateUdpPort(kLocalAddr1);
2439 ConnectToSignalDestroyed(port1);
2440 port1->set_timeout_delay(10); // milliseconds
2441 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
2442 port1->SetIceTiebreaker(kTiebreaker1);
2444 UDPPort* port2 = CreateUdpPort(kLocalAddr2);
2445 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
2446 port2->SetIceTiebreaker(kTiebreaker2);
2448 // Set up channels and ensure both ports will be deleted.
2449 TestChannel ch1(port1, port2);
2450 TestChannel ch2(port2, port1);
2452 // Simulate a connection that succeeds, and then is destroyed.
2453 ConnectAndDisconnectChannels(&ch1, &ch2);
2455 // After the connection is destroyed, the port should not be destroyed.
2456 talk_base::Thread::Current()->ProcessMessages(kTimeout);
2457 EXPECT_FALSE(destroyed());
2460 // This test case verifies that the CONTROLLED port does time out, but only
2461 // after connectivity is lost.
2462 TEST_F(PortTest, TestControlledTimeout) {
2463 SetIceProtocolType(cricket::ICEPROTO_RFC5245);
2464 UDPPort* port1 = CreateUdpPort(kLocalAddr1);
2465 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
2466 port1->SetIceTiebreaker(kTiebreaker1);
2468 UDPPort* port2 = CreateUdpPort(kLocalAddr2);
2469 ConnectToSignalDestroyed(port2);
2470 port2->set_timeout_delay(10); // milliseconds
2471 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
2472 port2->SetIceTiebreaker(kTiebreaker2);
2474 // The connection must not be destroyed before a connection is attempted.
2475 EXPECT_FALSE(destroyed());
2477 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
2478 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
2480 // Set up channels and ensure both ports will be deleted.
2481 TestChannel ch1(port1, port2);
2482 TestChannel ch2(port2, port1);
2484 // Simulate a connection that succeeds, and then is destroyed.
2485 ConnectAndDisconnectChannels(&ch1, &ch2);
2487 // The controlled port should be destroyed after 10 milliseconds.
2488 EXPECT_TRUE_WAIT(destroyed(), kTimeout);