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 |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 relay_server_(main_, | 377 relay_server_(main_, |
378 kRelayUdpIntAddr, | 378 kRelayUdpIntAddr, |
379 kRelayUdpExtAddr, | 379 kRelayUdpExtAddr, |
380 kRelayTcpIntAddr, | 380 kRelayTcpIntAddr, |
381 kRelayTcpExtAddr, | 381 kRelayTcpExtAddr, |
382 kRelaySslTcpIntAddr, | 382 kRelaySslTcpIntAddr, |
383 kRelaySslTcpExtAddr), | 383 kRelaySslTcpExtAddr), |
384 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), | 384 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), |
385 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)), | 385 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)), |
386 role_conflict_(false), | 386 role_conflict_(false), |
387 destroyed_(false) { | 387 ports_destroyed_(0) { |
388 network_.AddIP(rtc::IPAddress(INADDR_ANY)); | 388 network_.AddIP(rtc::IPAddress(INADDR_ANY)); |
389 } | 389 } |
390 | 390 |
391 protected: | 391 protected: |
392 void TestLocalToLocal() { | 392 void TestLocalToLocal() { |
393 Port* port1 = CreateUdpPort(kLocalAddr1); | 393 Port* port1 = CreateUdpPort(kLocalAddr1); |
394 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); | 394 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
395 Port* port2 = CreateUdpPort(kLocalAddr2); | 395 Port* port2 = CreateUdpPort(kLocalAddr2); |
396 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); | 396 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
397 TestConnectivity("udp", port1, "udp", port2, true, true, true, true); | 397 TestConnectivity("udp", port1, "udp", port2, true, true, true, true); |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 | 748 |
749 void OnRoleConflict(PortInterface* port) { | 749 void OnRoleConflict(PortInterface* port) { |
750 role_conflict_ = true; | 750 role_conflict_ = true; |
751 } | 751 } |
752 bool role_conflict() const { return role_conflict_; } | 752 bool role_conflict() const { return role_conflict_; } |
753 | 753 |
754 void ConnectToSignalDestroyed(PortInterface* port) { | 754 void ConnectToSignalDestroyed(PortInterface* port) { |
755 port->SignalDestroyed.connect(this, &PortTest::OnDestroyed); | 755 port->SignalDestroyed.connect(this, &PortTest::OnDestroyed); |
756 } | 756 } |
757 | 757 |
758 void OnDestroyed(PortInterface* port) { | 758 void OnDestroyed(PortInterface* port) { ++ports_destroyed_; } |
759 destroyed_ = true; | 759 int ports_destroyed() const { return ports_destroyed_; } |
760 } | |
761 bool destroyed() const { return destroyed_; } | |
762 | 760 |
763 rtc::BasicPacketSocketFactory* nat_socket_factory1() { | 761 rtc::BasicPacketSocketFactory* nat_socket_factory1() { |
764 return &nat_socket_factory1_; | 762 return &nat_socket_factory1_; |
765 } | 763 } |
766 | 764 |
767 rtc::VirtualSocketServer* vss() { return ss_.get(); } | 765 rtc::VirtualSocketServer* vss() { return ss_.get(); } |
768 | 766 |
769 private: | 767 private: |
770 rtc::Thread* main_; | 768 rtc::Thread* main_; |
771 std::unique_ptr<rtc::PhysicalSocketServer> pss_; | 769 std::unique_ptr<rtc::PhysicalSocketServer> pss_; |
772 std::unique_ptr<rtc::VirtualSocketServer> ss_; | 770 std::unique_ptr<rtc::VirtualSocketServer> ss_; |
773 rtc::SocketServerScope ss_scope_; | 771 rtc::SocketServerScope ss_scope_; |
774 rtc::Network network_; | 772 rtc::Network network_; |
775 rtc::BasicPacketSocketFactory socket_factory_; | 773 rtc::BasicPacketSocketFactory socket_factory_; |
776 std::unique_ptr<rtc::NATServer> nat_server1_; | 774 std::unique_ptr<rtc::NATServer> nat_server1_; |
777 std::unique_ptr<rtc::NATServer> nat_server2_; | 775 std::unique_ptr<rtc::NATServer> nat_server2_; |
778 rtc::NATSocketFactory nat_factory1_; | 776 rtc::NATSocketFactory nat_factory1_; |
779 rtc::NATSocketFactory nat_factory2_; | 777 rtc::NATSocketFactory nat_factory2_; |
780 rtc::BasicPacketSocketFactory nat_socket_factory1_; | 778 rtc::BasicPacketSocketFactory nat_socket_factory1_; |
781 rtc::BasicPacketSocketFactory nat_socket_factory2_; | 779 rtc::BasicPacketSocketFactory nat_socket_factory2_; |
782 std::unique_ptr<TestStunServer> stun_server_; | 780 std::unique_ptr<TestStunServer> stun_server_; |
783 TestTurnServer turn_server_; | 781 TestTurnServer turn_server_; |
784 TestRelayServer relay_server_; | 782 TestRelayServer relay_server_; |
785 std::string username_; | 783 std::string username_; |
786 std::string password_; | 784 std::string password_; |
787 bool role_conflict_; | 785 bool role_conflict_; |
788 bool destroyed_; | 786 int ports_destroyed_; |
789 }; | 787 }; |
790 | 788 |
791 void PortTest::TestConnectivity(const char* name1, Port* port1, | 789 void PortTest::TestConnectivity(const char* name1, Port* port1, |
792 const char* name2, Port* port2, | 790 const char* name2, Port* port2, |
793 bool accept, bool same_addr1, | 791 bool accept, bool same_addr1, |
794 bool same_addr2, bool possible) { | 792 bool same_addr2, bool possible) { |
795 LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": "; | 793 LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": "; |
796 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); | 794 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
797 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); | 795 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
798 | 796 |
(...skipping 1761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2560 // message right after we send ping. | 2558 // message right after we send ping. |
2561 ice_full_port->Reset(); | 2559 ice_full_port->Reset(); |
2562 // Send ping. This must have USE_CANDIDATE_ATTR. | 2560 // Send ping. This must have USE_CANDIDATE_ATTR. |
2563 ch1.Ping(); | 2561 ch1.Ping(); |
2564 ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != NULL, 1000); | 2562 ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != NULL, 1000); |
2565 msg = ice_full_port->last_stun_msg(); | 2563 msg = ice_full_port->last_stun_msg(); |
2566 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); | 2564 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); |
2567 ch1.Stop(); | 2565 ch1.Stop(); |
2568 } | 2566 } |
2569 | 2567 |
2570 // This test case verifies that the CONTROLLING port does not time out. | 2568 // This test case verifies that both the controlling port and the controlled |
2571 TEST_F(PortTest, TestControllingNoTimeout) { | 2569 // port will time out after connectivity is lost, if they are not used. |
| 2570 TEST_F(PortTest, TestPortTimeoutIfNotUsed) { |
| 2571 rtc::ScopedFakeClock clock; |
| 2572 int timeout_delay = 100; |
2572 UDPPort* port1 = CreateUdpPort(kLocalAddr1); | 2573 UDPPort* port1 = CreateUdpPort(kLocalAddr1); |
2573 ConnectToSignalDestroyed(port1); | 2574 ConnectToSignalDestroyed(port1); |
2574 port1->set_timeout_delay(10); // milliseconds | 2575 port1->set_timeout_delay(timeout_delay); // milliseconds |
2575 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); | 2576 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
2576 port1->SetIceTiebreaker(kTiebreaker1); | 2577 port1->SetIceTiebreaker(kTiebreaker1); |
2577 | 2578 |
2578 UDPPort* port2 = CreateUdpPort(kLocalAddr2); | 2579 UDPPort* port2 = CreateUdpPort(kLocalAddr2); |
| 2580 ConnectToSignalDestroyed(port2); |
| 2581 port2->set_timeout_delay(timeout_delay); // milliseconds |
| 2582 |
2579 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); | 2583 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
2580 port2->SetIceTiebreaker(kTiebreaker2); | 2584 port2->SetIceTiebreaker(kTiebreaker2); |
2581 | 2585 |
2582 // Set up channels and ensure both ports will be deleted. | 2586 // Set up channels and ensure both ports will be deleted. |
2583 TestChannel ch1(port1); | 2587 TestChannel ch1(port1); |
2584 TestChannel ch2(port2); | 2588 TestChannel ch2(port2); |
2585 | 2589 |
2586 // Simulate a connection that succeeds, and then is destroyed. | 2590 // Simulate a connection that succeeds, and then is destroyed. |
2587 StartConnectAndStopChannels(&ch1, &ch2); | 2591 StartConnectAndStopChannels(&ch1, &ch2); |
2588 | 2592 // After the connection is destroyed, the port will be destroyed because |
2589 // After the connection is destroyed, the port should not be destroyed. | 2593 // none of them is marked as being used. |
2590 rtc::Thread::Current()->ProcessMessages(kTimeout); | 2594 EXPECT_EQ_SIMULATED_WAIT(2, ports_destroyed(), kTimeout, clock); |
2591 EXPECT_FALSE(destroyed()); | |
2592 } | 2595 } |
2593 | 2596 |
2594 // This test case verifies that the CONTROLLED port does time out, but only | 2597 // This test case verifies that neither the controlling port nor the controlled |
2595 // after connectivity is lost. | 2598 // port will time out after connectivity is lost, if it is used. If it is marked |
2596 TEST_F(PortTest, TestControlledTimeout) { | 2599 // as "not used" later, it will be destroyed. |
| 2600 TEST_F(PortTest, TestPortNotTimeoutUntilNotUsed) { |
| 2601 rtc::ScopedFakeClock clock; |
| 2602 int timeout_delay = 100; |
2597 UDPPort* port1 = CreateUdpPort(kLocalAddr1); | 2603 UDPPort* port1 = CreateUdpPort(kLocalAddr1); |
| 2604 ConnectToSignalDestroyed(port1); |
| 2605 port1->set_timeout_delay(timeout_delay); // milliseconds |
2598 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); | 2606 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
2599 port1->SetIceTiebreaker(kTiebreaker1); | 2607 port1->SetIceTiebreaker(kTiebreaker1); |
2600 | 2608 |
2601 UDPPort* port2 = CreateUdpPort(kLocalAddr2); | 2609 UDPPort* port2 = CreateUdpPort(kLocalAddr2); |
2602 ConnectToSignalDestroyed(port2); | 2610 ConnectToSignalDestroyed(port2); |
2603 port2->set_timeout_delay(10); // milliseconds | 2611 port2->set_timeout_delay(timeout_delay); // milliseconds |
2604 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); | 2612 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
2605 port2->SetIceTiebreaker(kTiebreaker2); | 2613 port2->SetIceTiebreaker(kTiebreaker2); |
2606 | 2614 |
2607 // The connection must not be destroyed before a connection is attempted. | 2615 // Set up channels and start using the ports. |
2608 EXPECT_FALSE(destroyed()); | |
2609 | |
2610 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); | |
2611 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); | |
2612 | |
2613 // Set up channels and ensure both ports will be deleted. | |
2614 TestChannel ch1(port1); | 2616 TestChannel ch1(port1); |
2615 TestChannel ch2(port2); | 2617 TestChannel ch2(port2); |
| 2618 port1->StartBeingUsed(); |
| 2619 port2->StartBeingUsed(); |
2616 | 2620 |
2617 // Simulate a connection that succeeds, and then is destroyed. | 2621 // Simulate a connection that succeeds, and then is destroyed. |
2618 StartConnectAndStopChannels(&ch1, &ch2); | 2622 StartConnectAndStopChannels(&ch1, &ch2); |
| 2623 // After the connection is destroyed, the ports will not destroyed because |
| 2624 // they are marked as being used. |
| 2625 SIMULATED_WAIT(ports_destroyed() > 0, kTimeout, clock); |
| 2626 EXPECT_EQ(0, ports_destroyed()); |
2619 | 2627 |
2620 // The controlled port should be destroyed after 10 milliseconds. | 2628 // They will be destroyed right away if it is marked as not being used. |
2621 EXPECT_TRUE_WAIT(destroyed(), kTimeout); | 2629 port1->StopBeingUsed(); |
2622 } | 2630 port2->StopBeingUsed(); |
2623 | 2631 EXPECT_EQ_SIMULATED_WAIT(2, ports_destroyed(), 1, clock); |
2624 // This test case verifies that if the role of a port changes from controlled | |
2625 // to controlling after all connections fail, the port will not be destroyed. | |
2626 TEST_F(PortTest, TestControlledToControllingNotDestroyed) { | |
2627 UDPPort* port1 = CreateUdpPort(kLocalAddr1); | |
2628 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); | |
2629 port1->SetIceTiebreaker(kTiebreaker1); | |
2630 | |
2631 UDPPort* port2 = CreateUdpPort(kLocalAddr2); | |
2632 ConnectToSignalDestroyed(port2); | |
2633 port2->set_timeout_delay(10); // milliseconds | |
2634 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); | |
2635 port2->SetIceTiebreaker(kTiebreaker2); | |
2636 | |
2637 // The connection must not be destroyed before a connection is attempted. | |
2638 EXPECT_FALSE(destroyed()); | |
2639 | |
2640 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); | |
2641 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); | |
2642 | |
2643 // Set up channels and ensure both ports will be deleted. | |
2644 TestChannel ch1(port1); | |
2645 TestChannel ch2(port2); | |
2646 | |
2647 // Simulate a connection that succeeds, and then is destroyed. | |
2648 StartConnectAndStopChannels(&ch1, &ch2); | |
2649 // Switch the role after all connections are destroyed. | |
2650 EXPECT_TRUE_WAIT(ch2.conn() == nullptr, kTimeout); | |
2651 port1->SetIceRole(cricket::ICEROLE_CONTROLLED); | |
2652 port2->SetIceRole(cricket::ICEROLE_CONTROLLING); | |
2653 | |
2654 // After the connection is destroyed, the port should not be destroyed. | |
2655 rtc::Thread::Current()->ProcessMessages(kTimeout); | |
2656 EXPECT_FALSE(destroyed()); | |
2657 } | 2632 } |
2658 | 2633 |
2659 TEST_F(PortTest, TestSupportsProtocol) { | 2634 TEST_F(PortTest, TestSupportsProtocol) { |
2660 std::unique_ptr<Port> udp_port(CreateUdpPort(kLocalAddr1)); | 2635 std::unique_ptr<Port> udp_port(CreateUdpPort(kLocalAddr1)); |
2661 EXPECT_TRUE(udp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); | 2636 EXPECT_TRUE(udp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
2662 EXPECT_FALSE(udp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); | 2637 EXPECT_FALSE(udp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
2663 | 2638 |
2664 std::unique_ptr<Port> stun_port( | 2639 std::unique_ptr<Port> stun_port( |
2665 CreateStunPort(kLocalAddr1, nat_socket_factory1())); | 2640 CreateStunPort(kLocalAddr1, nat_socket_factory1())); |
2666 EXPECT_TRUE(stun_port->SupportsProtocol(UDP_PROTOCOL_NAME)); | 2641 EXPECT_TRUE(stun_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2714 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); | 2689 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
2715 EXPECT_NE(conn1, conn2); | 2690 EXPECT_NE(conn1, conn2); |
2716 conn_in_use = port->GetConnection(address); | 2691 conn_in_use = port->GetConnection(address); |
2717 EXPECT_EQ(conn2, conn_in_use); | 2692 EXPECT_EQ(conn2, conn_in_use); |
2718 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); | 2693 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); |
2719 | 2694 |
2720 // Make sure the new connection was not deleted. | 2695 // Make sure the new connection was not deleted. |
2721 rtc::Thread::Current()->ProcessMessages(300); | 2696 rtc::Thread::Current()->ProcessMessages(300); |
2722 EXPECT_TRUE(port->GetConnection(address) != nullptr); | 2697 EXPECT_TRUE(port->GetConnection(address) != nullptr); |
2723 } | 2698 } |
OLD | NEW |