OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
| 11 #include <list> |
11 #include <memory> | 12 #include <memory> |
12 | 13 |
13 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | 14 #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
14 #include "webrtc/p2p/base/jseptransport.h" | 15 #include "webrtc/p2p/base/jseptransport.h" |
15 #include "webrtc/p2p/base/relayport.h" | 16 #include "webrtc/p2p/base/relayport.h" |
16 #include "webrtc/p2p/base/stunport.h" | 17 #include "webrtc/p2p/base/stunport.h" |
17 #include "webrtc/p2p/base/tcpport.h" | 18 #include "webrtc/p2p/base/tcpport.h" |
18 #include "webrtc/p2p/base/testrelayserver.h" | 19 #include "webrtc/p2p/base/testrelayserver.h" |
19 #include "webrtc/p2p/base/teststunserver.h" | 20 #include "webrtc/p2p/base/teststunserver.h" |
20 #include "webrtc/p2p/base/testturnserver.h" | 21 #include "webrtc/p2p/base/testturnserver.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 return msg->Write(buf); | 103 return msg->Write(buf); |
103 } | 104 } |
104 | 105 |
105 // Stub port class for testing STUN generation and processing. | 106 // Stub port class for testing STUN generation and processing. |
106 class TestPort : public Port { | 107 class TestPort : public Port { |
107 public: | 108 public: |
108 TestPort(rtc::Thread* thread, | 109 TestPort(rtc::Thread* thread, |
109 const std::string& type, | 110 const std::string& type, |
110 rtc::PacketSocketFactory* factory, | 111 rtc::PacketSocketFactory* factory, |
111 rtc::Network* network, | 112 rtc::Network* network, |
112 const rtc::IPAddress& ip, | |
113 uint16_t min_port, | 113 uint16_t min_port, |
114 uint16_t max_port, | 114 uint16_t max_port, |
115 const std::string& username_fragment, | 115 const std::string& username_fragment, |
116 const std::string& password) | 116 const std::string& password) |
117 : Port(thread, | 117 : Port(thread, |
118 type, | 118 type, |
119 factory, | 119 factory, |
120 network, | 120 network, |
121 ip, | |
122 min_port, | 121 min_port, |
123 max_port, | 122 max_port, |
124 username_fragment, | 123 username_fragment, |
125 password) {} | 124 password) {} |
126 ~TestPort() {} | 125 ~TestPort() {} |
127 | 126 |
128 // Expose GetStunMessage so that we can test it. | 127 // Expose GetStunMessage so that we can test it. |
129 using cricket::Port::GetStunMessage; | 128 using cricket::Port::GetStunMessage; |
130 | 129 |
131 // The last StunMessage that was sent on this Port. | 130 // The last StunMessage that was sent on this Port. |
132 // TODO: Make these const; requires changes to SendXXXXResponse. | 131 // TODO: Make these const; requires changes to SendXXXXResponse. |
133 Buffer* last_stun_buf() { return last_stun_buf_.get(); } | 132 Buffer* last_stun_buf() { return last_stun_buf_.get(); } |
134 IceMessage* last_stun_msg() { return last_stun_msg_.get(); } | 133 IceMessage* last_stun_msg() { return last_stun_msg_.get(); } |
135 int last_stun_error_code() { | 134 int last_stun_error_code() { |
136 int code = 0; | 135 int code = 0; |
137 if (last_stun_msg_) { | 136 if (last_stun_msg_) { |
138 const StunErrorCodeAttribute* error_attr = last_stun_msg_->GetErrorCode(); | 137 const StunErrorCodeAttribute* error_attr = last_stun_msg_->GetErrorCode(); |
139 if (error_attr) { | 138 if (error_attr) { |
140 code = error_attr->code(); | 139 code = error_attr->code(); |
141 } | 140 } |
142 } | 141 } |
143 return code; | 142 return code; |
144 } | 143 } |
145 | 144 |
146 virtual void PrepareAddress() { | 145 virtual void PrepareAddress() { |
147 rtc::SocketAddress addr(ip(), min_port()); | 146 // Act as if the socket was bound to the best IP on the network, to the |
| 147 // first port in the allowed range. |
| 148 rtc::SocketAddress addr(Network()->GetBestIP(), min_port()); |
148 AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(), | 149 AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(), |
149 ICE_TYPE_PREFERENCE_HOST, 0, "", true); | 150 ICE_TYPE_PREFERENCE_HOST, 0, "", true); |
150 } | 151 } |
151 | 152 |
152 virtual bool SupportsProtocol(const std::string& protocol) const { | 153 virtual bool SupportsProtocol(const std::string& protocol) const { |
153 return true; | 154 return true; |
154 } | 155 } |
155 | 156 |
156 virtual ProtocolType GetProtocol() const { return PROTO_UDP; } | 157 virtual ProtocolType GetProtocol() const { return PROTO_UDP; } |
157 | 158 |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 std::string remote_frag_; | 376 std::string remote_frag_; |
376 bool nominated_; | 377 bool nominated_; |
377 bool connection_ready_to_send_ = false; | 378 bool connection_ready_to_send_ = false; |
378 }; | 379 }; |
379 | 380 |
380 class PortTest : public testing::Test, public sigslot::has_slots<> { | 381 class PortTest : public testing::Test, public sigslot::has_slots<> { |
381 public: | 382 public: |
382 PortTest() | 383 PortTest() |
383 : ss_(new rtc::VirtualSocketServer()), | 384 : ss_(new rtc::VirtualSocketServer()), |
384 main_(ss_.get()), | 385 main_(ss_.get()), |
385 network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), | |
386 socket_factory_(rtc::Thread::Current()), | 386 socket_factory_(rtc::Thread::Current()), |
387 nat_factory1_(ss_.get(), kNatAddr1, SocketAddress()), | 387 nat_factory1_(ss_.get(), kNatAddr1, SocketAddress()), |
388 nat_factory2_(ss_.get(), kNatAddr2, SocketAddress()), | 388 nat_factory2_(ss_.get(), kNatAddr2, SocketAddress()), |
389 nat_socket_factory1_(&nat_factory1_), | 389 nat_socket_factory1_(&nat_factory1_), |
390 nat_socket_factory2_(&nat_factory2_), | 390 nat_socket_factory2_(&nat_factory2_), |
391 stun_server_(TestStunServer::Create(&main_, kStunAddr)), | 391 stun_server_(TestStunServer::Create(&main_, kStunAddr)), |
392 turn_server_(&main_, kTurnUdpIntAddr, kTurnUdpExtAddr), | 392 turn_server_(&main_, kTurnUdpIntAddr, kTurnUdpExtAddr), |
393 relay_server_(&main_, | 393 relay_server_(&main_, |
394 kRelayUdpIntAddr, | 394 kRelayUdpIntAddr, |
395 kRelayUdpExtAddr, | 395 kRelayUdpExtAddr, |
396 kRelayTcpIntAddr, | 396 kRelayTcpIntAddr, |
397 kRelayTcpExtAddr, | 397 kRelayTcpExtAddr, |
398 kRelaySslTcpIntAddr, | 398 kRelaySslTcpIntAddr, |
399 kRelaySslTcpExtAddr), | 399 kRelaySslTcpExtAddr), |
400 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), | 400 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), |
401 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)), | 401 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)), |
402 role_conflict_(false), | 402 role_conflict_(false), |
403 ports_destroyed_(0) { | 403 ports_destroyed_(0) { |
404 network_.AddIP(rtc::IPAddress(INADDR_ANY)); | |
405 } | 404 } |
406 | 405 |
407 protected: | 406 protected: |
408 void TestLocalToLocal() { | 407 void TestLocalToLocal() { |
409 Port* port1 = CreateUdpPort(kLocalAddr1); | 408 Port* port1 = CreateUdpPort(kLocalAddr1); |
410 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); | 409 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
411 Port* port2 = CreateUdpPort(kLocalAddr2); | 410 Port* port2 = CreateUdpPort(kLocalAddr2); |
412 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); | 411 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
413 TestConnectivity("udp", port1, "udp", port2, true, true, true, true); | 412 TestConnectivity("udp", port1, "udp", port2, true, true, true, true); |
414 } | 413 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 rtype == RELAY_GTURN, false, true, true); | 475 rtype == RELAY_GTURN, false, true, true); |
477 } | 476 } |
478 void TestSslTcpToRelay(RelayType rtype, ProtocolType proto) { | 477 void TestSslTcpToRelay(RelayType rtype, ProtocolType proto) { |
479 Port* port1 = CreateTcpPort(kLocalAddr1); | 478 Port* port1 = CreateTcpPort(kLocalAddr1); |
480 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); | 479 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
481 Port* port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_SSLTCP); | 480 Port* port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_SSLTCP); |
482 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); | 481 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
483 TestConnectivity("ssltcp", port1, RelayName(rtype, proto), port2, | 482 TestConnectivity("ssltcp", port1, RelayName(rtype, proto), port2, |
484 rtype == RELAY_GTURN, false, true, true); | 483 rtype == RELAY_GTURN, false, true, true); |
485 } | 484 } |
| 485 |
| 486 rtc::Network* MakeNetwork(const SocketAddress& addr) { |
| 487 networks_.emplace_back("unittest", "unittest", addr.ipaddr(), 32); |
| 488 networks_.back().AddIP(addr.ipaddr()); |
| 489 return &networks_.back(); |
| 490 } |
| 491 |
486 // helpers for above functions | 492 // helpers for above functions |
487 UDPPort* CreateUdpPort(const SocketAddress& addr) { | 493 UDPPort* CreateUdpPort(const SocketAddress& addr) { |
488 return CreateUdpPort(addr, &socket_factory_); | 494 return CreateUdpPort(addr, &socket_factory_); |
489 } | 495 } |
490 UDPPort* CreateUdpPort(const SocketAddress& addr, | 496 UDPPort* CreateUdpPort(const SocketAddress& addr, |
491 PacketSocketFactory* socket_factory) { | 497 PacketSocketFactory* socket_factory) { |
492 return UDPPort::Create(&main_, socket_factory, &network_, addr.ipaddr(), 0, | 498 return UDPPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
493 0, username_, password_, std::string(), true); | 499 username_, password_, std::string(), true); |
494 } | 500 } |
495 TCPPort* CreateTcpPort(const SocketAddress& addr) { | 501 TCPPort* CreateTcpPort(const SocketAddress& addr) { |
496 return CreateTcpPort(addr, &socket_factory_); | 502 return CreateTcpPort(addr, &socket_factory_); |
497 } | 503 } |
498 TCPPort* CreateTcpPort(const SocketAddress& addr, | 504 TCPPort* CreateTcpPort(const SocketAddress& addr, |
499 PacketSocketFactory* socket_factory) { | 505 PacketSocketFactory* socket_factory) { |
500 return TCPPort::Create(&main_, socket_factory, &network_, | 506 return TCPPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
501 addr.ipaddr(), 0, 0, username_, password_, | 507 username_, password_, true); |
502 true); | |
503 } | 508 } |
504 StunPort* CreateStunPort(const SocketAddress& addr, | 509 StunPort* CreateStunPort(const SocketAddress& addr, |
505 rtc::PacketSocketFactory* factory) { | 510 rtc::PacketSocketFactory* factory) { |
506 ServerAddresses stun_servers; | 511 ServerAddresses stun_servers; |
507 stun_servers.insert(kStunAddr); | 512 stun_servers.insert(kStunAddr); |
508 return StunPort::Create(&main_, factory, &network_, | 513 return StunPort::Create(&main_, factory, MakeNetwork(addr), 0, 0, username_, |
509 addr.ipaddr(), 0, 0, | 514 password_, stun_servers, std::string()); |
510 username_, password_, stun_servers, | |
511 std::string()); | |
512 } | 515 } |
513 Port* CreateRelayPort(const SocketAddress& addr, RelayType rtype, | 516 Port* CreateRelayPort(const SocketAddress& addr, RelayType rtype, |
514 ProtocolType int_proto, ProtocolType ext_proto) { | 517 ProtocolType int_proto, ProtocolType ext_proto) { |
515 if (rtype == RELAY_TURN) { | 518 if (rtype == RELAY_TURN) { |
516 return CreateTurnPort(addr, &socket_factory_, int_proto, ext_proto); | 519 return CreateTurnPort(addr, &socket_factory_, int_proto, ext_proto); |
517 } else { | 520 } else { |
518 return CreateGturnPort(addr, int_proto, ext_proto); | 521 return CreateGturnPort(addr, int_proto, ext_proto); |
519 } | 522 } |
520 } | 523 } |
521 TurnPort* CreateTurnPort(const SocketAddress& addr, | 524 TurnPort* CreateTurnPort(const SocketAddress& addr, |
522 PacketSocketFactory* socket_factory, | 525 PacketSocketFactory* socket_factory, |
523 ProtocolType int_proto, ProtocolType ext_proto) { | 526 ProtocolType int_proto, ProtocolType ext_proto) { |
524 SocketAddress server_addr = | 527 SocketAddress server_addr = |
525 int_proto == PROTO_TCP ? kTurnTcpIntAddr : kTurnUdpIntAddr; | 528 int_proto == PROTO_TCP ? kTurnTcpIntAddr : kTurnUdpIntAddr; |
526 return CreateTurnPort(addr, socket_factory, int_proto, ext_proto, | 529 return CreateTurnPort(addr, socket_factory, int_proto, ext_proto, |
527 server_addr); | 530 server_addr); |
528 } | 531 } |
529 TurnPort* CreateTurnPort(const SocketAddress& addr, | 532 TurnPort* CreateTurnPort(const SocketAddress& addr, |
530 PacketSocketFactory* socket_factory, | 533 PacketSocketFactory* socket_factory, |
531 ProtocolType int_proto, ProtocolType ext_proto, | 534 ProtocolType int_proto, ProtocolType ext_proto, |
532 const rtc::SocketAddress& server_addr) { | 535 const rtc::SocketAddress& server_addr) { |
533 return TurnPort::Create(&main_, socket_factory, &network_, addr.ipaddr(), 0, | 536 return TurnPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
534 0, username_, password_, | 537 username_, password_, |
535 ProtocolAddress(server_addr, int_proto), | 538 ProtocolAddress(server_addr, int_proto), |
536 kRelayCredentials, 0, std::string()); | 539 kRelayCredentials, 0, std::string()); |
537 } | 540 } |
538 RelayPort* CreateGturnPort(const SocketAddress& addr, | 541 RelayPort* CreateGturnPort(const SocketAddress& addr, |
539 ProtocolType int_proto, ProtocolType ext_proto) { | 542 ProtocolType int_proto, ProtocolType ext_proto) { |
540 RelayPort* port = CreateGturnPort(addr); | 543 RelayPort* port = CreateGturnPort(addr); |
541 SocketAddress addrs[] = | 544 SocketAddress addrs[] = |
542 { kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr }; | 545 { kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr }; |
543 port->AddServerAddress(ProtocolAddress(addrs[int_proto], int_proto)); | 546 port->AddServerAddress(ProtocolAddress(addrs[int_proto], int_proto)); |
544 return port; | 547 return port; |
545 } | 548 } |
546 RelayPort* CreateGturnPort(const SocketAddress& addr) { | 549 RelayPort* CreateGturnPort(const SocketAddress& addr) { |
547 // TODO(pthatcher): Remove GTURN. | 550 // TODO(pthatcher): Remove GTURN. |
548 // Generate a username with length of 16 for Gturn only. | 551 // Generate a username with length of 16 for Gturn only. |
549 std::string username = rtc::CreateRandomString(kGturnUserNameLength); | 552 std::string username = rtc::CreateRandomString(kGturnUserNameLength); |
550 return RelayPort::Create(&main_, &socket_factory_, &network_, addr.ipaddr(), | 553 return RelayPort::Create(&main_, &socket_factory_, MakeNetwork(addr), 0, 0, |
551 0, 0, username, password_); | 554 username, password_); |
552 // TODO: Add an external address for ext_proto, so that the | 555 // TODO: Add an external address for ext_proto, so that the |
553 // other side can connect to this port using a non-UDP protocol. | 556 // other side can connect to this port using a non-UDP protocol. |
554 } | 557 } |
555 rtc::NATServer* CreateNatServer(const SocketAddress& addr, | 558 rtc::NATServer* CreateNatServer(const SocketAddress& addr, |
556 rtc::NATType type) { | 559 rtc::NATType type) { |
557 return new rtc::NATServer(type, ss_.get(), addr, addr, ss_.get(), addr); | 560 return new rtc::NATServer(type, ss_.get(), addr, addr, ss_.get(), addr); |
558 } | 561 } |
559 static const char* StunName(NATType type) { | 562 static const char* StunName(NATType type) { |
560 switch (type) { | 563 switch (type) { |
561 case NAT_OPEN_CONE: | 564 case NAT_OPEN_CONE: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 case PROTO_SSLTCP: | 596 case PROTO_SSLTCP: |
594 return "gturn(ssltcp)"; | 597 return "gturn(ssltcp)"; |
595 case PROTO_TLS: | 598 case PROTO_TLS: |
596 return "gturn(tls)"; | 599 return "gturn(tls)"; |
597 default: | 600 default: |
598 return "gturn(?)"; | 601 return "gturn(?)"; |
599 } | 602 } |
600 } | 603 } |
601 } | 604 } |
602 | 605 |
603 void SetNetworkType(rtc::AdapterType adapter_type) { | |
604 network_.set_type(adapter_type); | |
605 } | |
606 | |
607 void TestCrossFamilyPorts(int type); | 606 void TestCrossFamilyPorts(int type); |
608 | 607 |
609 void ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2); | 608 void ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2); |
610 | 609 |
611 // This does all the work and then deletes |port1| and |port2|. | 610 // This does all the work and then deletes |port1| and |port2|. |
612 void TestConnectivity(const char* name1, Port* port1, | 611 void TestConnectivity(const char* name1, Port* port1, |
613 const char* name2, Port* port2, | 612 const char* name2, Port* port2, |
614 bool accept, bool same_addr1, | 613 bool accept, bool same_addr1, |
615 bool same_addr2, bool possible); | 614 bool same_addr2, bool possible); |
616 | 615 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
757 IceMessage* CreateStunMessageWithUsername(int type, | 756 IceMessage* CreateStunMessageWithUsername(int type, |
758 const std::string& username) { | 757 const std::string& username) { |
759 IceMessage* msg = CreateStunMessage(type); | 758 IceMessage* msg = CreateStunMessage(type); |
760 msg->AddAttribute( | 759 msg->AddAttribute( |
761 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USERNAME, username)); | 760 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USERNAME, username)); |
762 return msg; | 761 return msg; |
763 } | 762 } |
764 TestPort* CreateTestPort(const rtc::SocketAddress& addr, | 763 TestPort* CreateTestPort(const rtc::SocketAddress& addr, |
765 const std::string& username, | 764 const std::string& username, |
766 const std::string& password) { | 765 const std::string& password) { |
767 TestPort* port = new TestPort(&main_, "test", &socket_factory_, &network_, | 766 TestPort* port = new TestPort(&main_, "test", &socket_factory_, |
768 addr.ipaddr(), 0, 0, username, password); | 767 MakeNetwork(addr), 0, 0, username, password); |
769 port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); | 768 port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); |
770 return port; | 769 return port; |
771 } | 770 } |
772 TestPort* CreateTestPort(const rtc::SocketAddress& addr, | 771 TestPort* CreateTestPort(const rtc::SocketAddress& addr, |
773 const std::string& username, | 772 const std::string& username, |
774 const std::string& password, | 773 const std::string& password, |
775 cricket::IceRole role, | 774 cricket::IceRole role, |
776 int tiebreaker) { | 775 int tiebreaker) { |
777 TestPort* port = CreateTestPort(addr, username, password); | 776 TestPort* port = CreateTestPort(addr, username, password); |
778 port->SetIceRole(role); | 777 port->SetIceRole(role); |
779 port->SetIceTiebreaker(tiebreaker); | 778 port->SetIceTiebreaker(tiebreaker); |
780 return port; | 779 return port; |
781 } | 780 } |
| 781 // Overload to create a test port given an rtc::Network directly. |
| 782 TestPort* CreateTestPort(rtc::Network* network, |
| 783 const std::string& username, |
| 784 const std::string& password) { |
| 785 TestPort* port = new TestPort(&main_, "test", &socket_factory_, network, 0, |
| 786 0, username, password); |
| 787 port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); |
| 788 return port; |
| 789 } |
782 | 790 |
783 void OnRoleConflict(PortInterface* port) { | 791 void OnRoleConflict(PortInterface* port) { |
784 role_conflict_ = true; | 792 role_conflict_ = true; |
785 } | 793 } |
786 bool role_conflict() const { return role_conflict_; } | 794 bool role_conflict() const { return role_conflict_; } |
787 | 795 |
788 void ConnectToSignalDestroyed(PortInterface* port) { | 796 void ConnectToSignalDestroyed(PortInterface* port) { |
789 port->SignalDestroyed.connect(this, &PortTest::OnDestroyed); | 797 port->SignalDestroyed.connect(this, &PortTest::OnDestroyed); |
790 } | 798 } |
791 | 799 |
792 void OnDestroyed(PortInterface* port) { ++ports_destroyed_; } | 800 void OnDestroyed(PortInterface* port) { ++ports_destroyed_; } |
793 int ports_destroyed() const { return ports_destroyed_; } | 801 int ports_destroyed() const { return ports_destroyed_; } |
794 | 802 |
795 rtc::BasicPacketSocketFactory* nat_socket_factory1() { | 803 rtc::BasicPacketSocketFactory* nat_socket_factory1() { |
796 return &nat_socket_factory1_; | 804 return &nat_socket_factory1_; |
797 } | 805 } |
798 | 806 |
799 rtc::VirtualSocketServer* vss() { return ss_.get(); } | 807 rtc::VirtualSocketServer* vss() { return ss_.get(); } |
800 | 808 |
801 private: | 809 private: |
| 810 // When a "create port" helper method is called with an IP, we create a |
| 811 // Network with that IP and add it to this list. Using a list instead of a |
| 812 // vector so that when it grows, pointers aren't invalidated. |
| 813 std::list<rtc::Network> networks_; |
802 std::unique_ptr<rtc::VirtualSocketServer> ss_; | 814 std::unique_ptr<rtc::VirtualSocketServer> ss_; |
803 rtc::AutoSocketServerThread main_; | 815 rtc::AutoSocketServerThread main_; |
804 rtc::Network network_; | |
805 rtc::BasicPacketSocketFactory socket_factory_; | 816 rtc::BasicPacketSocketFactory socket_factory_; |
806 std::unique_ptr<rtc::NATServer> nat_server1_; | 817 std::unique_ptr<rtc::NATServer> nat_server1_; |
807 std::unique_ptr<rtc::NATServer> nat_server2_; | 818 std::unique_ptr<rtc::NATServer> nat_server2_; |
808 rtc::NATSocketFactory nat_factory1_; | 819 rtc::NATSocketFactory nat_factory1_; |
809 rtc::NATSocketFactory nat_factory2_; | 820 rtc::NATSocketFactory nat_factory2_; |
810 rtc::BasicPacketSocketFactory nat_socket_factory1_; | 821 rtc::BasicPacketSocketFactory nat_socket_factory1_; |
811 rtc::BasicPacketSocketFactory nat_socket_factory2_; | 822 rtc::BasicPacketSocketFactory nat_socket_factory2_; |
812 std::unique_ptr<TestStunServer> stun_server_; | 823 std::unique_ptr<TestStunServer> stun_server_; |
813 TestTurnServer turn_server_; | 824 TestTurnServer turn_server_; |
814 TestRelayServer relay_server_; | 825 TestRelayServer relay_server_; |
(...skipping 1109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1924 ASSERT_TRUE(ice_controlling_attr != NULL); | 1935 ASSERT_TRUE(ice_controlling_attr != NULL); |
1925 const StunByteStringAttribute* use_candidate_attr = msg->GetByteString( | 1936 const StunByteStringAttribute* use_candidate_attr = msg->GetByteString( |
1926 STUN_ATTR_USE_CANDIDATE); | 1937 STUN_ATTR_USE_CANDIDATE); |
1927 ASSERT_TRUE(use_candidate_attr != NULL); | 1938 ASSERT_TRUE(use_candidate_attr != NULL); |
1928 } | 1939 } |
1929 | 1940 |
1930 // Tests that when the network type changes, the network cost of the port will | 1941 // Tests that when the network type changes, the network cost of the port will |
1931 // change, the network cost of the local candidates will change. Also tests that | 1942 // change, the network cost of the local candidates will change. Also tests that |
1932 // the remote network costs are updated with the stun binding requests. | 1943 // the remote network costs are updated with the stun binding requests. |
1933 TEST_F(PortTest, TestNetworkCostChange) { | 1944 TEST_F(PortTest, TestNetworkCostChange) { |
| 1945 rtc::Network* test_network = MakeNetwork(kLocalAddr1); |
1934 std::unique_ptr<TestPort> lport( | 1946 std::unique_ptr<TestPort> lport( |
1935 CreateTestPort(kLocalAddr1, "lfrag", "lpass")); | 1947 CreateTestPort(test_network, "lfrag", "lpass")); |
1936 std::unique_ptr<TestPort> rport( | 1948 std::unique_ptr<TestPort> rport( |
1937 CreateTestPort(kLocalAddr2, "rfrag", "rpass")); | 1949 CreateTestPort(test_network, "rfrag", "rpass")); |
1938 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); | 1950 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
1939 lport->SetIceTiebreaker(kTiebreaker1); | 1951 lport->SetIceTiebreaker(kTiebreaker1); |
1940 rport->SetIceRole(cricket::ICEROLE_CONTROLLED); | 1952 rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
1941 rport->SetIceTiebreaker(kTiebreaker2); | 1953 rport->SetIceTiebreaker(kTiebreaker2); |
1942 lport->PrepareAddress(); | 1954 lport->PrepareAddress(); |
1943 rport->PrepareAddress(); | 1955 rport->PrepareAddress(); |
1944 | 1956 |
1945 // Default local port cost is rtc::kNetworkCostUnknown. | 1957 // Default local port cost is rtc::kNetworkCostUnknown. |
1946 EXPECT_EQ(rtc::kNetworkCostUnknown, lport->network_cost()); | 1958 EXPECT_EQ(rtc::kNetworkCostUnknown, lport->network_cost()); |
1947 ASSERT_TRUE(!lport->Candidates().empty()); | 1959 ASSERT_TRUE(!lport->Candidates().empty()); |
1948 for (const cricket::Candidate& candidate : lport->Candidates()) { | 1960 for (const cricket::Candidate& candidate : lport->Candidates()) { |
1949 EXPECT_EQ(rtc::kNetworkCostUnknown, candidate.network_cost()); | 1961 EXPECT_EQ(rtc::kNetworkCostUnknown, candidate.network_cost()); |
1950 } | 1962 } |
1951 | 1963 |
1952 // Change the network type to wifi. | 1964 // Change the network type to wifi. |
1953 SetNetworkType(rtc::ADAPTER_TYPE_WIFI); | 1965 test_network->set_type(rtc::ADAPTER_TYPE_WIFI); |
1954 EXPECT_EQ(rtc::kNetworkCostLow, lport->network_cost()); | 1966 EXPECT_EQ(rtc::kNetworkCostLow, lport->network_cost()); |
1955 for (const cricket::Candidate& candidate : lport->Candidates()) { | 1967 for (const cricket::Candidate& candidate : lport->Candidates()) { |
1956 EXPECT_EQ(rtc::kNetworkCostLow, candidate.network_cost()); | 1968 EXPECT_EQ(rtc::kNetworkCostLow, candidate.network_cost()); |
1957 } | 1969 } |
1958 | 1970 |
1959 // Add a connection and then change the network type. | 1971 // Add a connection and then change the network type. |
1960 Connection* lconn = | 1972 Connection* lconn = |
1961 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); | 1973 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
1962 // Change the network type to cellular. | 1974 // Change the network type to cellular. |
1963 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); | 1975 test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
1964 EXPECT_EQ(rtc::kNetworkCostHigh, lport->network_cost()); | 1976 EXPECT_EQ(rtc::kNetworkCostHigh, lport->network_cost()); |
1965 for (const cricket::Candidate& candidate : lport->Candidates()) { | 1977 for (const cricket::Candidate& candidate : lport->Candidates()) { |
1966 EXPECT_EQ(rtc::kNetworkCostHigh, candidate.network_cost()); | 1978 EXPECT_EQ(rtc::kNetworkCostHigh, candidate.network_cost()); |
1967 } | 1979 } |
1968 | 1980 |
1969 SetNetworkType(rtc::ADAPTER_TYPE_WIFI); | 1981 test_network->set_type(rtc::ADAPTER_TYPE_WIFI); |
1970 Connection* rconn = | 1982 Connection* rconn = |
1971 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); | 1983 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
1972 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); | 1984 test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
1973 lconn->Ping(0); | 1985 lconn->Ping(0); |
1974 // The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping | 1986 // The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping |
1975 // contains an attribute of network cost of rtc::kNetworkCostHigh. Once the | 1987 // contains an attribute of network cost of rtc::kNetworkCostHigh. Once the |
1976 // message is handled in rconn, The rconn's remote candidate will have cost | 1988 // message is handled in rconn, The rconn's remote candidate will have cost |
1977 // rtc::kNetworkCostHigh; | 1989 // rtc::kNetworkCostHigh; |
1978 EXPECT_EQ(rtc::kNetworkCostLow, rconn->remote_candidate().network_cost()); | 1990 EXPECT_EQ(rtc::kNetworkCostLow, rconn->remote_candidate().network_cost()); |
1979 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); | 1991 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
1980 IceMessage* msg = lport->last_stun_msg(); | 1992 IceMessage* msg = lport->last_stun_msg(); |
1981 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); | 1993 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
1982 // Pass the binding request to rport. | 1994 // Pass the binding request to rport. |
1983 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), | 1995 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
1984 lport->last_stun_buf()->size(), rtc::PacketTime()); | 1996 lport->last_stun_buf()->size(), rtc::PacketTime()); |
1985 // Wait until rport sends the response and then check the remote network cost. | 1997 // Wait until rport sends the response and then check the remote network cost. |
1986 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); | 1998 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
1987 EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost()); | 1999 EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost()); |
1988 } | 2000 } |
1989 | 2001 |
1990 TEST_F(PortTest, TestNetworkInfoAttribute) { | 2002 TEST_F(PortTest, TestNetworkInfoAttribute) { |
| 2003 rtc::Network* test_network = MakeNetwork(kLocalAddr1); |
1991 std::unique_ptr<TestPort> lport( | 2004 std::unique_ptr<TestPort> lport( |
1992 CreateTestPort(kLocalAddr1, "lfrag", "lpass")); | 2005 CreateTestPort(test_network, "lfrag", "lpass")); |
1993 std::unique_ptr<TestPort> rport( | 2006 std::unique_ptr<TestPort> rport( |
1994 CreateTestPort(kLocalAddr2, "rfrag", "rpass")); | 2007 CreateTestPort(test_network, "rfrag", "rpass")); |
1995 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); | 2008 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
1996 lport->SetIceTiebreaker(kTiebreaker1); | 2009 lport->SetIceTiebreaker(kTiebreaker1); |
1997 rport->SetIceRole(cricket::ICEROLE_CONTROLLED); | 2010 rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
1998 rport->SetIceTiebreaker(kTiebreaker2); | 2011 rport->SetIceTiebreaker(kTiebreaker2); |
1999 | 2012 |
2000 uint16_t lnetwork_id = 9; | 2013 uint16_t lnetwork_id = 9; |
2001 lport->Network()->set_id(lnetwork_id); | 2014 lport->Network()->set_id(lnetwork_id); |
2002 // Send a fake ping from lport to rport. | 2015 // Send a fake ping from lport to rport. |
2003 lport->PrepareAddress(); | 2016 lport->PrepareAddress(); |
2004 rport->PrepareAddress(); | 2017 rport->PrepareAddress(); |
2005 Connection* lconn = | 2018 Connection* lconn = |
2006 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); | 2019 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
2007 lconn->Ping(0); | 2020 lconn->Ping(0); |
2008 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); | 2021 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
2009 IceMessage* msg = lport->last_stun_msg(); | 2022 IceMessage* msg = lport->last_stun_msg(); |
2010 const StunUInt32Attribute* network_info_attr = | 2023 const StunUInt32Attribute* network_info_attr = |
2011 msg->GetUInt32(STUN_ATTR_NETWORK_INFO); | 2024 msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
2012 ASSERT_TRUE(network_info_attr != NULL); | 2025 ASSERT_TRUE(network_info_attr != NULL); |
2013 uint32_t network_info = network_info_attr->value(); | 2026 uint32_t network_info = network_info_attr->value(); |
2014 EXPECT_EQ(lnetwork_id, network_info >> 16); | 2027 EXPECT_EQ(lnetwork_id, network_info >> 16); |
2015 // Default network has unknown type and cost kNetworkCostUnknown. | 2028 // Default network has unknown type and cost kNetworkCostUnknown. |
2016 EXPECT_EQ(rtc::kNetworkCostUnknown, network_info & 0xFFFF); | 2029 EXPECT_EQ(rtc::kNetworkCostUnknown, network_info & 0xFFFF); |
2017 | 2030 |
2018 // Set the network type to be cellular so its cost will be kNetworkCostHigh. | 2031 // Set the network type to be cellular so its cost will be kNetworkCostHigh. |
2019 // Send a fake ping from rport to lport. | 2032 // Send a fake ping from rport to lport. |
2020 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); | 2033 test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
2021 uint16_t rnetwork_id = 8; | 2034 uint16_t rnetwork_id = 8; |
2022 rport->Network()->set_id(rnetwork_id); | 2035 rport->Network()->set_id(rnetwork_id); |
2023 Connection* rconn = | 2036 Connection* rconn = |
2024 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); | 2037 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
2025 rconn->Ping(0); | 2038 rconn->Ping(0); |
2026 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); | 2039 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
2027 msg = rport->last_stun_msg(); | 2040 msg = rport->last_stun_msg(); |
2028 network_info_attr = msg->GetUInt32(STUN_ATTR_NETWORK_INFO); | 2041 network_info_attr = msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
2029 ASSERT_TRUE(network_info_attr != NULL); | 2042 ASSERT_TRUE(network_info_attr != NULL); |
2030 network_info = network_info_attr->value(); | 2043 network_info = network_info_attr->value(); |
(...skipping 845 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2876 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); | 2889 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
2877 EXPECT_NE(conn1, conn2); | 2890 EXPECT_NE(conn1, conn2); |
2878 conn_in_use = port->GetConnection(address); | 2891 conn_in_use = port->GetConnection(address); |
2879 EXPECT_EQ(conn2, conn_in_use); | 2892 EXPECT_EQ(conn2, conn_in_use); |
2880 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); | 2893 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); |
2881 | 2894 |
2882 // Make sure the new connection was not deleted. | 2895 // Make sure the new connection was not deleted. |
2883 rtc::Thread::Current()->ProcessMessages(300); | 2896 rtc::Thread::Current()->ProcessMessages(300); |
2884 EXPECT_TRUE(port->GetConnection(address) != nullptr); | 2897 EXPECT_TRUE(port->GetConnection(address) != nullptr); |
2885 } | 2898 } |
OLD | NEW |