Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2009 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      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.
     15  *
     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.
     26  */
     27 
     28 #include "talk/p2p/base/p2ptransportchannel.h"
     29 #include "talk/p2p/base/testrelayserver.h"
     30 #include "talk/p2p/base/teststunserver.h"
     31 #include "talk/p2p/base/testturnserver.h"
     32 #include "talk/p2p/client/basicportallocator.h"
     33 #include "webrtc/base/dscp.h"
     34 #include "webrtc/base/fakenetwork.h"
     35 #include "webrtc/base/firewallsocketserver.h"
     36 #include "webrtc/base/gunit.h"
     37 #include "webrtc/base/helpers.h"
     38 #include "webrtc/base/logging.h"
     39 #include "webrtc/base/natserver.h"
     40 #include "webrtc/base/natsocketfactory.h"
     41 #include "webrtc/base/physicalsocketserver.h"
     42 #include "webrtc/base/proxyserver.h"
     43 #include "webrtc/base/socketaddress.h"
     44 #include "webrtc/base/ssladapter.h"
     45 #include "webrtc/base/thread.h"
     46 #include "webrtc/base/virtualsocketserver.h"
     47 
     48 using cricket::kDefaultPortAllocatorFlags;
     49 using cricket::kMinimumStepDelay;
     50 using cricket::kDefaultStepDelay;
     51 using cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG;
     52 using cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET;
     53 using cricket::ServerAddresses;
     54 using rtc::SocketAddress;
     55 
     56 static const int kDefaultTimeout = 1000;
     57 static const int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
     58                                    cricket::PORTALLOCATOR_DISABLE_RELAY |
     59                                    cricket::PORTALLOCATOR_DISABLE_TCP;
     60 // Addresses on the public internet.
     61 static const SocketAddress kPublicAddrs[2] =
     62     { SocketAddress("11.11.11.11", 0), SocketAddress("22.22.22.22", 0) };
     63 // IPv6 Addresses on the public internet.
     64 static const SocketAddress kIPv6PublicAddrs[2] = {
     65     SocketAddress("2400:4030:1:2c00:be30:abcd:efab:cdef", 0),
     66     SocketAddress("2620:0:1000:1b03:2e41:38ff:fea6:f2a4", 0)
     67 };
     68 // For configuring multihomed clients.
     69 static const SocketAddress kAlternateAddrs[2] =
     70     { SocketAddress("11.11.11.101", 0), SocketAddress("22.22.22.202", 0) };
     71 // Addresses for HTTP proxy servers.
     72 static const SocketAddress kHttpsProxyAddrs[2] =
     73     { SocketAddress("11.11.11.1", 443), SocketAddress("22.22.22.1", 443) };
     74 // Addresses for SOCKS proxy servers.
     75 static const SocketAddress kSocksProxyAddrs[2] =
     76     { SocketAddress("11.11.11.1", 1080), SocketAddress("22.22.22.1", 1080) };
     77 // Internal addresses for NAT boxes.
     78 static const SocketAddress kNatAddrs[2] =
     79     { SocketAddress("192.168.1.1", 0), SocketAddress("192.168.2.1", 0) };
     80 // Private addresses inside the NAT private networks.
     81 static const SocketAddress kPrivateAddrs[2] =
     82     { SocketAddress("192.168.1.11", 0), SocketAddress("192.168.2.22", 0) };
     83 // For cascaded NATs, the internal addresses of the inner NAT boxes.
     84 static const SocketAddress kCascadedNatAddrs[2] =
     85     { SocketAddress("192.168.10.1", 0), SocketAddress("192.168.20.1", 0) };
     86 // For cascaded NATs, private addresses inside the inner private networks.
     87 static const SocketAddress kCascadedPrivateAddrs[2] =
     88     { SocketAddress("192.168.10.11", 0), SocketAddress("192.168.20.22", 0) };
     89 // The address of the public STUN server.
     90 static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
     91 // The addresses for the public relay server.
     92 static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
     93 static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
     94 static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
     95 static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
     96 static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
     97 static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
     98 // The addresses for the public turn server.
     99 static const SocketAddress kTurnUdpIntAddr("99.99.99.4",
    100                                            cricket::STUN_SERVER_PORT);
    101 static const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0);
    102 static const cricket::RelayCredentials kRelayCredentials("test", "test");
    103 
    104 // Based on ICE_UFRAG_LENGTH
    105 static const char* kIceUfrag[4] = {"TESTICEUFRAG0000", "TESTICEUFRAG0001",
    106                                    "TESTICEUFRAG0002", "TESTICEUFRAG0003"};
    107 // Based on ICE_PWD_LENGTH
    108 static const char* kIcePwd[4] = {"TESTICEPWD00000000000000",
    109                                  "TESTICEPWD00000000000001",
    110                                  "TESTICEPWD00000000000002",
    111                                  "TESTICEPWD00000000000003"};
    112 
    113 static const uint64 kTiebreaker1 = 11111;
    114 static const uint64 kTiebreaker2 = 22222;
    115 
    116 // This test simulates 2 P2P endpoints that want to establish connectivity
    117 // with each other over various network topologies and conditions, which can be
    118 // specified in each individial test.
    119 // A virtual network (via VirtualSocketServer) along with virtual firewalls and
    120 // NATs (via Firewall/NATSocketServer) are used to simulate the various network
    121 // conditions. We can configure the IP addresses of the endpoints,
    122 // block various types of connectivity, or add arbitrary levels of NAT.
    123 // We also run a STUN server and a relay server on the virtual network to allow
    124 // our typical P2P mechanisms to do their thing.
    125 // For each case, we expect the P2P stack to eventually settle on a specific
    126 // form of connectivity to the other side. The test checks that the P2P
    127 // negotiation successfully establishes connectivity within a certain time,
    128 // and that the result is what we expect.
    129 // Note that this class is a base class for use by other tests, who will provide
    130 // specialized test behavior.
    131 class P2PTransportChannelTestBase : public testing::Test,
    132                                     public rtc::MessageHandler,
    133                                     public sigslot::has_slots<> {
    134  public:
    135   P2PTransportChannelTestBase()
    136       : main_(rtc::Thread::Current()),
    137         pss_(new rtc::PhysicalSocketServer),
    138         vss_(new rtc::VirtualSocketServer(pss_.get())),
    139         nss_(new rtc::NATSocketServer(vss_.get())),
    140         ss_(new rtc::FirewallSocketServer(nss_.get())),
    141         ss_scope_(ss_.get()),
    142         stun_server_(main_, kStunAddr),
    143         turn_server_(main_, kTurnUdpIntAddr, kTurnUdpExtAddr),
    144         relay_server_(main_, kRelayUdpIntAddr, kRelayUdpExtAddr,
    145                       kRelayTcpIntAddr, kRelayTcpExtAddr,
    146                       kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
    147         socks_server1_(ss_.get(), kSocksProxyAddrs[0],
    148                        ss_.get(), kSocksProxyAddrs[0]),
    149         socks_server2_(ss_.get(), kSocksProxyAddrs[1],
    150                        ss_.get(), kSocksProxyAddrs[1]),
    151         clear_remote_candidates_ufrag_pwd_(false),
    152         force_relay_(false) {
    153     ep1_.role_ = cricket::ICEROLE_CONTROLLING;
    154     ep2_.role_ = cricket::ICEROLE_CONTROLLED;
    155 
    156     ServerAddresses stun_servers;
    157     stun_servers.insert(kStunAddr);
    158     ep1_.allocator_.reset(new cricket::BasicPortAllocator(
    159         &ep1_.network_manager_,
    160         stun_servers, kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr));
    161     ep2_.allocator_.reset(new cricket::BasicPortAllocator(
    162         &ep2_.network_manager_,
    163         stun_servers, kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr));
    164   }
    165 
    166  protected:
    167   enum Config {
    168     OPEN,                           // Open to the Internet
    169     NAT_FULL_CONE,                  // NAT, no filtering
    170     NAT_ADDR_RESTRICTED,            // NAT, must send to an addr to recv
    171     NAT_PORT_RESTRICTED,            // NAT, must send to an addr+port to recv
    172     NAT_SYMMETRIC,                  // NAT, endpoint-dependent bindings
    173     NAT_DOUBLE_CONE,                // Double NAT, both cone
    174     NAT_SYMMETRIC_THEN_CONE,        // Double NAT, symmetric outer, cone inner
    175     BLOCK_UDP,                      // Firewall, UDP in/out blocked
    176     BLOCK_UDP_AND_INCOMING_TCP,     // Firewall, UDP in/out and TCP in blocked
    177     BLOCK_ALL_BUT_OUTGOING_HTTP,    // Firewall, only TCP out on 80/443
    178     PROXY_HTTPS,                    // All traffic through HTTPS proxy
    179     PROXY_SOCKS,                    // All traffic through SOCKS proxy
    180     NUM_CONFIGS
    181   };
    182 
    183   struct Result {
    184     Result(const std::string& lt, const std::string& lp,
    185            const std::string& rt, const std::string& rp,
    186            const std::string& lt2, const std::string& lp2,
    187            const std::string& rt2, const std::string& rp2, int wait)
    188         : local_type(lt), local_proto(lp), remote_type(rt), remote_proto(rp),
    189           local_type2(lt2), local_proto2(lp2), remote_type2(rt2),
    190           remote_proto2(rp2), connect_wait(wait) {
    191     }
    192     std::string local_type;
    193     std::string local_proto;
    194     std::string remote_type;
    195     std::string remote_proto;
    196     std::string local_type2;
    197     std::string local_proto2;
    198     std::string remote_type2;
    199     std::string remote_proto2;
    200     int connect_wait;
    201   };
    202 
    203   struct ChannelData {
    204     bool CheckData(const char* data, int len) {
    205       bool ret = false;
    206       if (!ch_packets_.empty()) {
    207         std::string packet =  ch_packets_.front();
    208         ret = (packet == std::string(data, len));
    209         ch_packets_.pop_front();
    210       }
    211       return ret;
    212     }
    213 
    214     std::string name_;  // TODO - Currently not used.
    215     std::list<std::string> ch_packets_;
    216     rtc::scoped_ptr<cricket::P2PTransportChannel> ch_;
    217   };
    218 
    219   struct Endpoint {
    220     Endpoint() : signaling_delay_(0), role_(cricket::ICEROLE_UNKNOWN),
    221         tiebreaker_(0), role_conflict_(false),
    222         protocol_type_(cricket::ICEPROTO_GOOGLE) {}
    223     bool HasChannel(cricket::TransportChannel* ch) {
    224       return (ch == cd1_.ch_.get() || ch == cd2_.ch_.get());
    225     }
    226     ChannelData* GetChannelData(cricket::TransportChannel* ch) {
    227       if (!HasChannel(ch)) return NULL;
    228       if (cd1_.ch_.get() == ch)
    229         return &cd1_;
    230       else
    231         return &cd2_;
    232     }
    233     void SetSignalingDelay(int delay) { signaling_delay_ = delay; }
    234 
    235     void SetIceRole(cricket::IceRole role) { role_ = role; }
    236     cricket::IceRole ice_role() { return role_; }
    237     void SetIceProtocolType(cricket::IceProtocolType type) {
    238       protocol_type_ = type;
    239     }
    240     cricket::IceProtocolType protocol_type() { return protocol_type_; }
    241     void SetIceTiebreaker(uint64 tiebreaker) { tiebreaker_ = tiebreaker; }
    242     uint64 GetIceTiebreaker() { return tiebreaker_; }
    243     void OnRoleConflict(bool role_conflict) { role_conflict_ = role_conflict; }
    244     bool role_conflict() { return role_conflict_; }
    245     void SetAllocationStepDelay(uint32 delay) {
    246       allocator_->set_step_delay(delay);
    247     }
    248     void SetAllowTcpListen(bool allow_tcp_listen) {
    249       allocator_->set_allow_tcp_listen(allow_tcp_listen);
    250     }
    251 
    252     rtc::FakeNetworkManager network_manager_;
    253     rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
    254     ChannelData cd1_;
    255     ChannelData cd2_;
    256     int signaling_delay_;
    257     cricket::IceRole role_;
    258     uint64 tiebreaker_;
    259     bool role_conflict_;
    260     cricket::IceProtocolType protocol_type_;
    261   };
    262 
    263   struct CandidateData : public rtc::MessageData {
    264     CandidateData(cricket::TransportChannel* ch, const cricket::Candidate& c)
    265         : channel(ch), candidate(c) {
    266     }
    267     cricket::TransportChannel* channel;
    268     cricket::Candidate candidate;
    269   };
    270 
    271   ChannelData* GetChannelData(cricket::TransportChannel* channel) {
    272     if (ep1_.HasChannel(channel))
    273       return ep1_.GetChannelData(channel);
    274     else
    275       return ep2_.GetChannelData(channel);
    276   }
    277 
    278   void CreateChannels(int num) {
    279     std::string ice_ufrag_ep1_cd1_ch = kIceUfrag[0];
    280     std::string ice_pwd_ep1_cd1_ch = kIcePwd[0];
    281     std::string ice_ufrag_ep2_cd1_ch = kIceUfrag[1];
    282     std::string ice_pwd_ep2_cd1_ch = kIcePwd[1];
    283     ep1_.cd1_.ch_.reset(CreateChannel(
    284         0, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT,
    285         ice_ufrag_ep1_cd1_ch, ice_pwd_ep1_cd1_ch,
    286         ice_ufrag_ep2_cd1_ch, ice_pwd_ep2_cd1_ch));
    287     ep2_.cd1_.ch_.reset(CreateChannel(
    288         1, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT,
    289         ice_ufrag_ep2_cd1_ch, ice_pwd_ep2_cd1_ch,
    290         ice_ufrag_ep1_cd1_ch, ice_pwd_ep1_cd1_ch));
    291     if (num == 2) {
    292       std::string ice_ufrag_ep1_cd2_ch = kIceUfrag[2];
    293       std::string ice_pwd_ep1_cd2_ch = kIcePwd[2];
    294       std::string ice_ufrag_ep2_cd2_ch = kIceUfrag[3];
    295       std::string ice_pwd_ep2_cd2_ch = kIcePwd[3];
    296       // In BUNDLE each endpoint must share common ICE credentials.
    297       if (ep1_.allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_BUNDLE) {
    298         ice_ufrag_ep1_cd2_ch = ice_ufrag_ep1_cd1_ch;
    299         ice_pwd_ep1_cd2_ch = ice_pwd_ep1_cd1_ch;
    300       }
    301       if (ep2_.allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_BUNDLE) {
    302         ice_ufrag_ep2_cd2_ch = ice_ufrag_ep2_cd1_ch;
    303         ice_pwd_ep2_cd2_ch = ice_pwd_ep2_cd1_ch;
    304       }
    305       ep1_.cd2_.ch_.reset(CreateChannel(
    306           0, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT,
    307           ice_ufrag_ep1_cd2_ch, ice_pwd_ep1_cd2_ch,
    308           ice_ufrag_ep2_cd2_ch, ice_pwd_ep2_cd2_ch));
    309       ep2_.cd2_.ch_.reset(CreateChannel(
    310           1, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT,
    311           ice_ufrag_ep2_cd2_ch, ice_pwd_ep2_cd2_ch,
    312           ice_ufrag_ep1_cd2_ch, ice_pwd_ep1_cd2_ch));
    313     }
    314   }
    315   cricket::P2PTransportChannel* CreateChannel(
    316       int endpoint,
    317       int component,
    318       const std::string& local_ice_ufrag,
    319       const std::string& local_ice_pwd,
    320       const std::string& remote_ice_ufrag,
    321       const std::string& remote_ice_pwd) {
    322     cricket::P2PTransportChannel* channel = new cricket::P2PTransportChannel(
    323         "test content name", component, NULL, GetAllocator(endpoint));
    324     channel->SignalRequestSignaling.connect(
    325         this, &P2PTransportChannelTestBase::OnChannelRequestSignaling);
    326     channel->SignalCandidateReady.connect(this,
    327         &P2PTransportChannelTestBase::OnCandidate);
    328     channel->SignalReadPacket.connect(
    329         this, &P2PTransportChannelTestBase::OnReadPacket);
    330     channel->SignalRoleConflict.connect(
    331         this, &P2PTransportChannelTestBase::OnRoleConflict);
    332     channel->SetIceProtocolType(GetEndpoint(endpoint)->protocol_type());
    333     channel->SetIceCredentials(local_ice_ufrag, local_ice_pwd);
    334     if (clear_remote_candidates_ufrag_pwd_) {
    335       // This only needs to be set if we're clearing them from the
    336       // candidates.  Some unit tests rely on this not being set.
    337       channel->SetRemoteIceCredentials(remote_ice_ufrag, remote_ice_pwd);
    338     }
    339     channel->SetIceRole(GetEndpoint(endpoint)->ice_role());
    340     channel->SetIceTiebreaker(GetEndpoint(endpoint)->GetIceTiebreaker());
    341     channel->Connect();
    342     return channel;
    343   }
    344   void DestroyChannels() {
    345     ep1_.cd1_.ch_.reset();
    346     ep2_.cd1_.ch_.reset();
    347     ep1_.cd2_.ch_.reset();
    348     ep2_.cd2_.ch_.reset();
    349   }
    350   cricket::P2PTransportChannel* ep1_ch1() { return ep1_.cd1_.ch_.get(); }
    351   cricket::P2PTransportChannel* ep1_ch2() { return ep1_.cd2_.ch_.get(); }
    352   cricket::P2PTransportChannel* ep2_ch1() { return ep2_.cd1_.ch_.get(); }
    353   cricket::P2PTransportChannel* ep2_ch2() { return ep2_.cd2_.ch_.get(); }
    354 
    355   // Common results.
    356   static const Result kLocalUdpToLocalUdp;
    357   static const Result kLocalUdpToStunUdp;
    358   static const Result kLocalUdpToPrflxUdp;
    359   static const Result kPrflxUdpToLocalUdp;
    360   static const Result kStunUdpToLocalUdp;
    361   static const Result kStunUdpToStunUdp;
    362   static const Result kPrflxUdpToStunUdp;
    363   static const Result kLocalUdpToRelayUdp;
    364   static const Result kPrflxUdpToRelayUdp;
    365   static const Result kLocalTcpToLocalTcp;
    366   static const Result kLocalTcpToPrflxTcp;
    367   static const Result kPrflxTcpToLocalTcp;
    368 
    369   static void SetUpTestCase() {
    370     rtc::InitializeSSL();
    371   }
    372 
    373   static void TearDownTestCase() {
    374     rtc::CleanupSSL();
    375   }
    376 
    377   rtc::NATSocketServer* nat() { return nss_.get(); }
    378   rtc::FirewallSocketServer* fw() { return ss_.get(); }
    379 
    380   Endpoint* GetEndpoint(int endpoint) {
    381     if (endpoint == 0) {
    382       return &ep1_;
    383     } else if (endpoint == 1) {
    384       return &ep2_;
    385     } else {
    386       return NULL;
    387     }
    388   }
    389   cricket::PortAllocator* GetAllocator(int endpoint) {
    390     return GetEndpoint(endpoint)->allocator_.get();
    391   }
    392   void AddAddress(int endpoint, const SocketAddress& addr) {
    393     GetEndpoint(endpoint)->network_manager_.AddInterface(addr);
    394   }
    395   void RemoveAddress(int endpoint, const SocketAddress& addr) {
    396     GetEndpoint(endpoint)->network_manager_.RemoveInterface(addr);
    397   }
    398   void SetProxy(int endpoint, rtc::ProxyType type) {
    399     rtc::ProxyInfo info;
    400     info.type = type;
    401     info.address = (type == rtc::PROXY_HTTPS) ?
    402         kHttpsProxyAddrs[endpoint] : kSocksProxyAddrs[endpoint];
    403     GetAllocator(endpoint)->set_proxy("unittest/1.0", info);
    404   }
    405   void SetAllocatorFlags(int endpoint, int flags) {
    406     GetAllocator(endpoint)->set_flags(flags);
    407   }
    408   void SetSignalingDelay(int endpoint, int delay) {
    409     GetEndpoint(endpoint)->SetSignalingDelay(delay);
    410   }
    411   void SetIceProtocol(int endpoint, cricket::IceProtocolType type) {
    412     GetEndpoint(endpoint)->SetIceProtocolType(type);
    413   }
    414   void SetIceRole(int endpoint, cricket::IceRole role) {
    415     GetEndpoint(endpoint)->SetIceRole(role);
    416   }
    417   void SetIceTiebreaker(int endpoint, uint64 tiebreaker) {
    418     GetEndpoint(endpoint)->SetIceTiebreaker(tiebreaker);
    419   }
    420   bool GetRoleConflict(int endpoint) {
    421     return GetEndpoint(endpoint)->role_conflict();
    422   }
    423   void SetAllocationStepDelay(int endpoint, uint32 delay) {
    424     return GetEndpoint(endpoint)->SetAllocationStepDelay(delay);
    425   }
    426   void SetAllowTcpListen(int endpoint, bool allow_tcp_listen) {
    427     return GetEndpoint(endpoint)->SetAllowTcpListen(allow_tcp_listen);
    428   }
    429 
    430   void Test(const Result& expected) {
    431     int32 connect_start = rtc::Time(), connect_time;
    432 
    433     // Create the channels and wait for them to connect.
    434     CreateChannels(1);
    435     EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL &&
    436                             ep2_ch1() != NULL &&
    437                             ep1_ch1()->readable() &&
    438                             ep1_ch1()->writable() &&
    439                             ep2_ch1()->readable() &&
    440                             ep2_ch1()->writable(),
    441                             expected.connect_wait,
    442                             1000);
    443     connect_time = rtc::TimeSince(connect_start);
    444     if (connect_time < expected.connect_wait) {
    445       LOG(LS_INFO) << "Connect time: " << connect_time << " ms";
    446     } else {
    447       LOG(LS_INFO) << "Connect time: " << "TIMEOUT ("
    448                    << expected.connect_wait << " ms)";
    449     }
    450 
    451     // Allow a few turns of the crank for the best connections to emerge.
    452     // This may take up to 2 seconds.
    453     if (ep1_ch1()->best_connection() &&
    454         ep2_ch1()->best_connection()) {
    455       int32 converge_start = rtc::Time(), converge_time;
    456       int converge_wait = 2000;
    457       EXPECT_TRUE_WAIT_MARGIN(
    458           LocalCandidate(ep1_ch1())->type() == expected.local_type &&
    459           LocalCandidate(ep1_ch1())->protocol() == expected.local_proto &&
    460           RemoteCandidate(ep1_ch1())->type() == expected.remote_type &&
    461           RemoteCandidate(ep1_ch1())->protocol() == expected.remote_proto,
    462           converge_wait,
    463           converge_wait);
    464 
    465       // Also do EXPECT_EQ on each part so that failures are more verbose.
    466       EXPECT_EQ(expected.local_type, LocalCandidate(ep1_ch1())->type());
    467       EXPECT_EQ(expected.local_proto, LocalCandidate(ep1_ch1())->protocol());
    468       EXPECT_EQ(expected.remote_type, RemoteCandidate(ep1_ch1())->type());
    469       EXPECT_EQ(expected.remote_proto, RemoteCandidate(ep1_ch1())->protocol());
    470 
    471       // Verifying remote channel best connection information. This is done
    472       // only for the RFC 5245 as controlled agent will use USE-CANDIDATE
    473       // from controlling (ep1) agent. We can easily predict from EP1 result
    474       // matrix.
    475       if (ep2_.protocol_type_ == cricket::ICEPROTO_RFC5245) {
    476         // Checking for best connection candidates information at remote.
    477         EXPECT_TRUE_WAIT(
    478             LocalCandidate(ep2_ch1())->type() == expected.local_type2 &&
    479             LocalCandidate(ep2_ch1())->protocol() == expected.local_proto2 &&
    480             RemoteCandidate(ep2_ch1())->protocol() == expected.remote_proto2,
    481             kDefaultTimeout);
    482 
    483         // For verbose
    484         EXPECT_EQ(expected.local_type2, LocalCandidate(ep2_ch1())->type());
    485         EXPECT_EQ(expected.local_proto2, LocalCandidate(ep2_ch1())->protocol());
    486         EXPECT_EQ(expected.remote_proto2,
    487                   RemoteCandidate(ep2_ch1())->protocol());
    488         // Removed remote_type comparision aginst best connection remote
    489         // candidate. This is done to handle remote type discrepancy from
    490         // local to stun based on the test type.
    491         // For example in case of Open -> NAT, ep2 channels will have LULU
    492         // and in other cases like NAT -> NAT it will be LUSU. To avoid these
    493         // mismatches and we are doing comparision in different way.
    494         // i.e. when don't match its remote type is either local or stun.
    495         // TODO(ronghuawu): Refine the test criteria.
    496         // https://code.google.com/p/webrtc/issues/detail?id=1953
    497         if (expected.remote_type2 != RemoteCandidate(ep2_ch1())->type()) {
    498           EXPECT_TRUE(expected.remote_type2 == cricket::LOCAL_PORT_TYPE ||
    499                       expected.remote_type2 == cricket::STUN_PORT_TYPE);
    500           EXPECT_TRUE(
    501               RemoteCandidate(ep2_ch1())->type() == cricket::LOCAL_PORT_TYPE ||
    502               RemoteCandidate(ep2_ch1())->type() == cricket::STUN_PORT_TYPE ||
    503               RemoteCandidate(ep2_ch1())->type() == cricket::PRFLX_PORT_TYPE);
    504         }
    505       }
    506 
    507       converge_time = rtc::TimeSince(converge_start);
    508       if (converge_time < converge_wait) {
    509         LOG(LS_INFO) << "Converge time: " << converge_time << " ms";
    510       } else {
    511         LOG(LS_INFO) << "Converge time: " << "TIMEOUT ("
    512                      << converge_wait << " ms)";
    513       }
    514     }
    515     // Try sending some data to other end.
    516     TestSendRecv(1);
    517 
    518     // Destroy the channels, and wait for them to be fully cleaned up.
    519     DestroyChannels();
    520   }
    521 
    522   void TestSendRecv(int channels) {
    523     for (int i = 0; i < 10; ++i) {
    524     const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    525       int len = static_cast<int>(strlen(data));
    526       // local_channel1 <==> remote_channel1
    527       EXPECT_EQ_WAIT(len, SendData(ep1_ch1(), data, len), 1000);
    528       EXPECT_TRUE_WAIT(CheckDataOnChannel(ep2_ch1(), data, len), 1000);
    529       EXPECT_EQ_WAIT(len, SendData(ep2_ch1(), data, len), 1000);
    530       EXPECT_TRUE_WAIT(CheckDataOnChannel(ep1_ch1(), data, len), 1000);
    531       if (channels == 2 && ep1_ch2() && ep2_ch2()) {
    532         // local_channel2 <==> remote_channel2
    533         EXPECT_EQ_WAIT(len, SendData(ep1_ch2(), data, len), 1000);
    534         EXPECT_TRUE_WAIT(CheckDataOnChannel(ep2_ch2(), data, len), 1000);
    535         EXPECT_EQ_WAIT(len, SendData(ep2_ch2(), data, len), 1000);
    536         EXPECT_TRUE_WAIT(CheckDataOnChannel(ep1_ch2(), data, len), 1000);
    537       }
    538     }
    539   }
    540 
    541   // This test waits for the transport to become readable and writable on both
    542   // end points. Once they are, the end points set new local ice credentials to
    543   // restart the ice gathering. Finally it waits for the transport to select a
    544   // new connection using the newly generated ice candidates.
    545   // Before calling this function the end points must be configured.
    546   void TestHandleIceUfragPasswordChanged() {
    547     ep1_ch1()->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]);
    548     ep2_ch1()->SetRemoteIceCredentials(kIceUfrag[0], kIcePwd[0]);
    549     EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->readable() && ep1_ch1()->writable() &&
    550                             ep2_ch1()->readable() && ep2_ch1()->writable(),
    551                             1000, 1000);
    552 
    553     const cricket::Candidate* old_local_candidate1 = LocalCandidate(ep1_ch1());
    554     const cricket::Candidate* old_local_candidate2 = LocalCandidate(ep2_ch1());
    555     const cricket::Candidate* old_remote_candidate1 =
    556         RemoteCandidate(ep1_ch1());
    557     const cricket::Candidate* old_remote_candidate2 =
    558         RemoteCandidate(ep2_ch1());
    559 
    560     ep1_ch1()->SetIceCredentials(kIceUfrag[2], kIcePwd[2]);
    561     ep1_ch1()->SetRemoteIceCredentials(kIceUfrag[3], kIcePwd[3]);
    562     ep2_ch1()->SetIceCredentials(kIceUfrag[3], kIcePwd[3]);
    563     ep2_ch1()->SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]);
    564 
    565     EXPECT_TRUE_WAIT_MARGIN(LocalCandidate(ep1_ch1())->generation() !=
    566                             old_local_candidate1->generation(),
    567                             1000, 1000);
    568     EXPECT_TRUE_WAIT_MARGIN(LocalCandidate(ep2_ch1())->generation() !=
    569                             old_local_candidate2->generation(),
    570                             1000, 1000);
    571     EXPECT_TRUE_WAIT_MARGIN(RemoteCandidate(ep1_ch1())->generation() !=
    572                             old_remote_candidate1->generation(),
    573                             1000, 1000);
    574     EXPECT_TRUE_WAIT_MARGIN(RemoteCandidate(ep2_ch1())->generation() !=
    575                             old_remote_candidate2->generation(),
    576                             1000, 1000);
    577     EXPECT_EQ(1u, RemoteCandidate(ep2_ch1())->generation());
    578     EXPECT_EQ(1u, RemoteCandidate(ep1_ch1())->generation());
    579   }
    580 
    581   void TestSignalRoleConflict() {
    582     SetIceProtocol(0, cricket::ICEPROTO_RFC5245);
    583     SetIceTiebreaker(0, kTiebreaker1);  // Default EP1 is in controlling state.
    584 
    585     SetIceProtocol(1, cricket::ICEPROTO_RFC5245);
    586     SetIceRole(1, cricket::ICEROLE_CONTROLLING);
    587     SetIceTiebreaker(1, kTiebreaker2);
    588 
    589     // Creating channels with both channels role set to CONTROLLING.
    590     CreateChannels(1);
    591     // Since both the channels initiated with controlling state and channel2
    592     // has higher tiebreaker value, channel1 should receive SignalRoleConflict.
    593     EXPECT_TRUE_WAIT(GetRoleConflict(0), 1000);
    594     EXPECT_FALSE(GetRoleConflict(1));
    595 
    596     EXPECT_TRUE_WAIT(ep1_ch1()->readable() &&
    597                      ep1_ch1()->writable() &&
    598                      ep2_ch1()->readable() &&
    599                      ep2_ch1()->writable(),
    600                      1000);
    601 
    602     EXPECT_TRUE(ep1_ch1()->best_connection() &&
    603                 ep2_ch1()->best_connection());
    604 
    605     TestSendRecv(1);
    606   }
    607 
    608   void TestHybridConnectivity(cricket::IceProtocolType proto) {
    609     AddAddress(0, kPublicAddrs[0]);
    610     AddAddress(1, kPublicAddrs[1]);
    611 
    612     SetAllocationStepDelay(0, kMinimumStepDelay);
    613     SetAllocationStepDelay(1, kMinimumStepDelay);
    614 
    615     SetIceRole(0, cricket::ICEROLE_CONTROLLING);
    616     SetIceProtocol(0, cricket::ICEPROTO_HYBRID);
    617     SetIceTiebreaker(0, kTiebreaker1);
    618     SetIceRole(1, cricket::ICEROLE_CONTROLLED);
    619     SetIceProtocol(1, proto);
    620     SetIceTiebreaker(1, kTiebreaker2);
    621 
    622     CreateChannels(1);
    623     // When channel is in hybrid and it's controlling agent, channel will
    624     // receive ping request from the remote. Hence connection is readable.
    625     // Since channel is in hybrid, it will not send any pings, so no writable
    626     // connection. Since channel2 is in controlled state, it will not have
    627     // any connections which are readable or writable, as it didn't received
    628     // pings (or none) with USE-CANDIDATE attribute.
    629     EXPECT_TRUE_WAIT(ep1_ch1()->readable(), 1000);
    630 
    631     // Set real protocol type.
    632     ep1_ch1()->SetIceProtocolType(proto);
    633 
    634     // Channel should able to send ping requests and connections become writable
    635     // in both directions.
    636     EXPECT_TRUE_WAIT(ep1_ch1()->readable() && ep1_ch1()->writable() &&
    637                      ep2_ch1()->readable() && ep2_ch1()->writable(),
    638                      1000);
    639     EXPECT_TRUE(
    640         ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
    641         LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
    642         RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
    643 
    644     TestSendRecv(1);
    645     DestroyChannels();
    646   }
    647 
    648   void OnChannelRequestSignaling(cricket::TransportChannelImpl* channel) {
    649     channel->OnSignalingReady();
    650   }
    651   // We pass the candidates directly to the other side.
    652   void OnCandidate(cricket::TransportChannelImpl* ch,
    653                    const cricket::Candidate& c) {
    654     if (force_relay_ && c.type() != cricket::RELAY_PORT_TYPE)
    655       return;
    656 
    657     main_->PostDelayed(GetEndpoint(ch)->signaling_delay_, this, 0,
    658                        new CandidateData(ch, c));
    659   }
    660   void OnMessage(rtc::Message* msg) {
    661     rtc::scoped_ptr<CandidateData> data(
    662         static_cast<CandidateData*>(msg->pdata));
    663     cricket::P2PTransportChannel* rch = GetRemoteChannel(data->channel);
    664     cricket::Candidate c = data->candidate;
    665     if (clear_remote_candidates_ufrag_pwd_) {
    666       c.set_username("");
    667       c.set_password("");
    668     }
    669     LOG(LS_INFO) << "Candidate(" << data->channel->component() << "->"
    670                  << rch->component() << "): " << c.type() << ", " << c.protocol()
    671                  << ", " << c.address().ToString() << ", " << c.username()
    672                  << ", " << c.generation();
    673     rch->OnCandidate(c);
    674   }
    675   void OnReadPacket(cricket::TransportChannel* channel, const char* data,
    676                     size_t len, const rtc::PacketTime& packet_time,
    677                     int flags) {
    678     std::list<std::string>& packets = GetPacketList(channel);
    679     packets.push_front(std::string(data, len));
    680   }
    681   void OnRoleConflict(cricket::TransportChannelImpl* channel) {
    682     GetEndpoint(channel)->OnRoleConflict(true);
    683     cricket::IceRole new_role =
    684         GetEndpoint(channel)->ice_role() == cricket::ICEROLE_CONTROLLING ?
    685             cricket::ICEROLE_CONTROLLED : cricket::ICEROLE_CONTROLLING;
    686     channel->SetIceRole(new_role);
    687   }
    688   int SendData(cricket::TransportChannel* channel,
    689                const char* data, size_t len) {
    690     rtc::PacketOptions options;
    691     return channel->SendPacket(data, len, options, 0);
    692   }
    693   bool CheckDataOnChannel(cricket::TransportChannel* channel,
    694                           const char* data, int len) {
    695     return GetChannelData(channel)->CheckData(data, len);
    696   }
    697   static const cricket::Candidate* LocalCandidate(
    698       cricket::P2PTransportChannel* ch) {
    699     return (ch && ch->best_connection()) ?
    700         &ch->best_connection()->local_candidate() : NULL;
    701   }
    702   static const cricket::Candidate* RemoteCandidate(
    703       cricket::P2PTransportChannel* ch) {
    704     return (ch && ch->best_connection()) ?
    705         &ch->best_connection()->remote_candidate() : NULL;
    706   }
    707   Endpoint* GetEndpoint(cricket::TransportChannel* ch) {
    708     if (ep1_.HasChannel(ch)) {
    709       return &ep1_;
    710     } else if (ep2_.HasChannel(ch)) {
    711       return &ep2_;
    712     } else {
    713       return NULL;
    714     }
    715   }
    716   cricket::P2PTransportChannel* GetRemoteChannel(
    717       cricket::TransportChannel* ch) {
    718     if (ch == ep1_ch1())
    719       return ep2_ch1();
    720     else if (ch == ep1_ch2())
    721       return ep2_ch2();
    722     else if (ch == ep2_ch1())
    723       return ep1_ch1();
    724     else if (ch == ep2_ch2())
    725       return ep1_ch2();
    726     else
    727       return NULL;
    728   }
    729   std::list<std::string>& GetPacketList(cricket::TransportChannel* ch) {
    730     return GetChannelData(ch)->ch_packets_;
    731   }
    732 
    733   void set_clear_remote_candidates_ufrag_pwd(bool clear) {
    734     clear_remote_candidates_ufrag_pwd_ = clear;
    735   }
    736 
    737   void set_force_relay(bool relay) {
    738     force_relay_ = relay;
    739   }
    740 
    741  private:
    742   rtc::Thread* main_;
    743   rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
    744   rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
    745   rtc::scoped_ptr<rtc::NATSocketServer> nss_;
    746   rtc::scoped_ptr<rtc::FirewallSocketServer> ss_;
    747   rtc::SocketServerScope ss_scope_;
    748   cricket::TestStunServer stun_server_;
    749   cricket::TestTurnServer turn_server_;
    750   cricket::TestRelayServer relay_server_;
    751   rtc::SocksProxyServer socks_server1_;
    752   rtc::SocksProxyServer socks_server2_;
    753   Endpoint ep1_;
    754   Endpoint ep2_;
    755   bool clear_remote_candidates_ufrag_pwd_;
    756   bool force_relay_;
    757 };
    758 
    759 // The tests have only a few outcomes, which we predefine.
    760 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    761     kLocalUdpToLocalUdp("local", "udp", "local", "udp",
    762                         "local", "udp", "local", "udp", 1000);
    763 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    764     kLocalUdpToStunUdp("local", "udp", "stun", "udp",
    765                        "local", "udp", "stun", "udp", 1000);
    766 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    767     kLocalUdpToPrflxUdp("local", "udp", "prflx", "udp",
    768                         "prflx", "udp", "local", "udp", 1000);
    769 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    770     kPrflxUdpToLocalUdp("prflx", "udp", "local", "udp",
    771                         "local", "udp", "prflx", "udp", 1000);
    772 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    773     kStunUdpToLocalUdp("stun", "udp", "local", "udp",
    774                        "local", "udp", "stun", "udp", 1000);
    775 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    776     kStunUdpToStunUdp("stun", "udp", "stun", "udp",
    777                       "stun", "udp", "stun", "udp", 1000);
    778 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    779     kPrflxUdpToStunUdp("prflx", "udp", "stun", "udp",
    780                        "local", "udp", "prflx", "udp", 1000);
    781 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    782     kLocalUdpToRelayUdp("local", "udp", "relay", "udp",
    783                         "relay", "udp", "local", "udp", 2000);
    784 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    785     kPrflxUdpToRelayUdp("prflx", "udp", "relay", "udp",
    786                         "relay", "udp", "prflx", "udp", 2000);
    787 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    788     kLocalTcpToLocalTcp("local", "tcp", "local", "tcp",
    789                         "local", "tcp", "local", "tcp", 3000);
    790 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    791     kLocalTcpToPrflxTcp("local", "tcp", "prflx", "tcp",
    792                         "prflx", "tcp", "local", "tcp", 3000);
    793 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
    794     kPrflxTcpToLocalTcp("prflx", "tcp", "local", "tcp",
    795                         "local", "tcp", "prflx", "tcp", 3000);
    796 
    797 // Test the matrix of all the connectivity types we expect to see in the wild.
    798 // Just test every combination of the configs in the Config enum.
    799 class P2PTransportChannelTest : public P2PTransportChannelTestBase {
    800  protected:
    801   static const Result* kMatrix[NUM_CONFIGS][NUM_CONFIGS];
    802   static const Result* kMatrixSharedUfrag[NUM_CONFIGS][NUM_CONFIGS];
    803   static const Result* kMatrixSharedSocketAsGice[NUM_CONFIGS][NUM_CONFIGS];
    804   static const Result* kMatrixSharedSocketAsIce[NUM_CONFIGS][NUM_CONFIGS];
    805   void ConfigureEndpoints(Config config1, Config config2,
    806       int allocator_flags1, int allocator_flags2,
    807       int delay1, int delay2,
    808       cricket::IceProtocolType type) {
    809     // Ideally we want to use TURN server for both GICE and ICE, but in case
    810     // of GICE, TURN server usage is not producing results reliabally.
    811     // TODO(mallinath): Remove Relay and use TURN server for all tests.
    812     ServerAddresses stun_servers;
    813     stun_servers.insert(kStunAddr);
    814     GetEndpoint(0)->allocator_.reset(
    815         new cricket::BasicPortAllocator(&(GetEndpoint(0)->network_manager_),
    816         stun_servers,
    817         rtc::SocketAddress(), rtc::SocketAddress(),
    818         rtc::SocketAddress()));
    819     GetEndpoint(1)->allocator_.reset(
    820         new cricket::BasicPortAllocator(&(GetEndpoint(1)->network_manager_),
    821         stun_servers,
    822         rtc::SocketAddress(), rtc::SocketAddress(),
    823         rtc::SocketAddress()));
    824 
    825     cricket::RelayServerConfig relay_server(cricket::RELAY_GTURN);
    826     if (type == cricket::ICEPROTO_RFC5245) {
    827       relay_server.type = cricket::RELAY_TURN;
    828       relay_server.credentials = kRelayCredentials;
    829       relay_server.ports.push_back(cricket::ProtocolAddress(
    830           kTurnUdpIntAddr, cricket::PROTO_UDP, false));
    831     } else {
    832       relay_server.ports.push_back(cricket::ProtocolAddress(
    833           kRelayUdpIntAddr, cricket::PROTO_UDP, false));
    834       relay_server.ports.push_back(cricket::ProtocolAddress(
    835           kRelayTcpIntAddr, cricket::PROTO_TCP, false));
    836       relay_server.ports.push_back(cricket::ProtocolAddress(
    837           kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP, false));
    838     }
    839     GetEndpoint(0)->allocator_->AddRelay(relay_server);
    840     GetEndpoint(1)->allocator_->AddRelay(relay_server);
    841 
    842     ConfigureEndpoint(0, config1);
    843     SetIceProtocol(0, type);
    844     SetAllocatorFlags(0, allocator_flags1);
    845     SetAllocationStepDelay(0, delay1);
    846     ConfigureEndpoint(1, config2);
    847     SetIceProtocol(1, type);
    848     SetAllocatorFlags(1, allocator_flags2);
    849     SetAllocationStepDelay(1, delay2);
    850   }
    851   void ConfigureEndpoint(int endpoint, Config config) {
    852     switch (config) {
    853       case OPEN:
    854         AddAddress(endpoint, kPublicAddrs[endpoint]);
    855         break;
    856       case NAT_FULL_CONE:
    857       case NAT_ADDR_RESTRICTED:
    858       case NAT_PORT_RESTRICTED:
    859       case NAT_SYMMETRIC:
    860         AddAddress(endpoint, kPrivateAddrs[endpoint]);
    861         // Add a single NAT of the desired type
    862         nat()->AddTranslator(kPublicAddrs[endpoint], kNatAddrs[endpoint],
    863             static_cast<rtc::NATType>(config - NAT_FULL_CONE))->
    864             AddClient(kPrivateAddrs[endpoint]);
    865         break;
    866       case NAT_DOUBLE_CONE:
    867       case NAT_SYMMETRIC_THEN_CONE:
    868         AddAddress(endpoint, kCascadedPrivateAddrs[endpoint]);
    869         // Add a two cascaded NATs of the desired types
    870         nat()->AddTranslator(kPublicAddrs[endpoint], kNatAddrs[endpoint],
    871             (config == NAT_DOUBLE_CONE) ?
    872                 rtc::NAT_OPEN_CONE : rtc::NAT_SYMMETRIC)->
    873             AddTranslator(kPrivateAddrs[endpoint], kCascadedNatAddrs[endpoint],
    874                 rtc::NAT_OPEN_CONE)->
    875                 AddClient(kCascadedPrivateAddrs[endpoint]);
    876         break;
    877       case BLOCK_UDP:
    878       case BLOCK_UDP_AND_INCOMING_TCP:
    879       case BLOCK_ALL_BUT_OUTGOING_HTTP:
    880       case PROXY_HTTPS:
    881       case PROXY_SOCKS:
    882         AddAddress(endpoint, kPublicAddrs[endpoint]);
    883         // Block all UDP
    884         fw()->AddRule(false, rtc::FP_UDP, rtc::FD_ANY,
    885                       kPublicAddrs[endpoint]);
    886         if (config == BLOCK_UDP_AND_INCOMING_TCP) {
    887           // Block TCP inbound to the endpoint
    888           fw()->AddRule(false, rtc::FP_TCP, SocketAddress(),
    889                         kPublicAddrs[endpoint]);
    890         } else if (config == BLOCK_ALL_BUT_OUTGOING_HTTP) {
    891           // Block all TCP to/from the endpoint except 80/443 out
    892           fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint],
    893                         SocketAddress(rtc::IPAddress(INADDR_ANY), 80));
    894           fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint],
    895                         SocketAddress(rtc::IPAddress(INADDR_ANY), 443));
    896           fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY,
    897                         kPublicAddrs[endpoint]);
    898         } else if (config == PROXY_HTTPS) {
    899           // Block all TCP to/from the endpoint except to the proxy server
    900           fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint],
    901                         kHttpsProxyAddrs[endpoint]);
    902           fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY,
    903                         kPublicAddrs[endpoint]);
    904           SetProxy(endpoint, rtc::PROXY_HTTPS);
    905         } else if (config == PROXY_SOCKS) {
    906           // Block all TCP to/from the endpoint except to the proxy server
    907           fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint],
    908                         kSocksProxyAddrs[endpoint]);
    909           fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY,
    910                         kPublicAddrs[endpoint]);
    911           SetProxy(endpoint, rtc::PROXY_SOCKS5);
    912         }
    913         break;
    914       default:
    915         break;
    916     }
    917   }
    918 };
    919 
    920 // Shorthands for use in the test matrix.
    921 #define LULU &kLocalUdpToLocalUdp
    922 #define LUSU &kLocalUdpToStunUdp
    923 #define LUPU &kLocalUdpToPrflxUdp
    924 #define PULU &kPrflxUdpToLocalUdp
    925 #define SULU &kStunUdpToLocalUdp
    926 #define SUSU &kStunUdpToStunUdp
    927 #define PUSU &kPrflxUdpToStunUdp
    928 #define LURU &kLocalUdpToRelayUdp
    929 #define PURU &kPrflxUdpToRelayUdp
    930 #define LTLT &kLocalTcpToLocalTcp
    931 #define LTPT &kLocalTcpToPrflxTcp
    932 #define PTLT &kPrflxTcpToLocalTcp
    933 // TODO: Enable these once TestRelayServer can accept external TCP.
    934 #define LTRT NULL
    935 #define LSRS NULL
    936 
    937 // Test matrix. Originator behavior defined by rows, receiever by columns.
    938 
    939 // Currently the p2ptransportchannel.cc (specifically the
    940 // P2PTransportChannel::OnUnknownAddress) operates in 2 modes depend on the
    941 // remote candidates - ufrag per port or shared ufrag.
    942 // For example, if the remote candidates have the shared ufrag, for the unknown
    943 // address reaches the OnUnknownAddress, we will try to find the matched
    944 // remote candidate based on the address and protocol, if not found, a new
    945 // remote candidate will be created for this address. But if the remote
    946 // candidates have different ufrags, we will try to find the matched remote
    947 // candidate by comparing the ufrag. If not found, an error will be returned.
    948 // Because currently the shared ufrag feature is under the experiment and will
    949 // be rolled out gradually. We want to test the different combinations of peers
    950 // with/without the shared ufrag enabled. And those different combinations have
    951 // different expectation of the best connection. For example in the OpenToCONE
    952 // case, an unknown address will be updated to a "host" remote candidate if the
    953 // remote peer uses different ufrag per port. But in the shared ufrag case,
    954 // a "stun" (should be peer-reflexive eventually) candidate will be created for
    955 // that. So the expected best candidate will be LUSU instead of LULU.
    956 // With all these, we have to keep 2 test matrixes for the tests:
    957 // kMatrix - for the tests that the remote peer uses different ufrag per port.
    958 // kMatrixSharedUfrag - for the tests that remote peer uses shared ufrag.
    959 // The different between the two matrixes are on:
    960 // OPToCONE, OPTo2CON,
    961 // COToCONE, COToADDR, COToPORT, COToSYMM, COTo2CON, COToSCON,
    962 // ADToCONE, ADToADDR, ADTo2CON,
    963 // POToADDR,
    964 // SYToADDR,
    965 // 2CToCONE, 2CToADDR, 2CToPORT, 2CToSYMM, 2CTo2CON, 2CToSCON,
    966 // SCToADDR,
    967 
    968 // TODO: Fix NULLs caused by lack of TCP support in NATSocket.
    969 // TODO: Fix NULLs caused by no HTTP proxy support.
    970 // TODO: Rearrange rows/columns from best to worst.
    971 // TODO(ronghuawu): Keep only one test matrix once the shared ufrag is enabled.
    972 const P2PTransportChannelTest::Result*
    973     P2PTransportChannelTest::kMatrix[NUM_CONFIGS][NUM_CONFIGS] = {
    974 //      OPEN  CONE  ADDR  PORT  SYMM  2CON  SCON  !UDP  !TCP  HTTP  PRXH  PRXS
    975 /*OP*/ {LULU, LULU, LULU, LULU, LULU, LULU, LULU, LTLT, LTLT, LSRS, NULL, LTLT},
    976 /*CO*/ {LULU, LULU, LULU, SULU, SULU, LULU, SULU, NULL, NULL, LSRS, NULL, LTRT},
    977 /*AD*/ {LULU, LULU, LULU, SUSU, SUSU, LULU, SUSU, NULL, NULL, LSRS, NULL, LTRT},
    978 /*PO*/ {LULU, LUSU, LUSU, SUSU, LURU, LUSU, LURU, NULL, NULL, LSRS, NULL, LTRT},
    979 /*SY*/ {LULU, LUSU, LUSU, LURU, LURU, LUSU, LURU, NULL, NULL, LSRS, NULL, LTRT},
    980 /*2C*/ {LULU, LULU, LULU, SULU, SULU, LULU, SULU, NULL, NULL, LSRS, NULL, LTRT},
    981 /*SC*/ {LULU, LUSU, LUSU, LURU, LURU, LUSU, LURU, NULL, NULL, LSRS, NULL, LTRT},
    982 /*!U*/ {LTLT, NULL, NULL, NULL, NULL, NULL, NULL, LTLT, LTLT, LSRS, NULL, LTRT},
    983 /*!T*/ {LTRT, NULL, NULL, NULL, NULL, NULL, NULL, LTLT, LTRT, LSRS, NULL, LTRT},
    984 /*HT*/ {LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, NULL, LSRS},
    985 /*PR*/ {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
    986 /*PR*/ {LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LSRS, NULL, LTRT},
    987 };
    988 const P2PTransportChannelTest::Result*
    989     P2PTransportChannelTest::kMatrixSharedUfrag[NUM_CONFIGS][NUM_CONFIGS] = {
    990 //      OPEN  CONE  ADDR  PORT  SYMM  2CON  SCON  !UDP  !TCP  HTTP  PRXH  PRXS
    991 /*OP*/ {LULU, LUSU, LULU, LULU, LULU, LUSU, LULU, LTLT, LTLT, LSRS, NULL, LTLT},
    992 /*CO*/ {LULU, LUSU, LUSU, SUSU, SUSU, LUSU, SUSU, NULL, NULL, LSRS, NULL, LTRT},
    993 /*AD*/ {LULU, LUSU, LUSU, SUSU, SUSU, LUSU, SUSU, NULL, NULL, LSRS, NULL, LTRT},
    994 /*PO*/ {LULU, LUSU, LUSU, SUSU, LURU, LUSU, LURU, NULL, NULL, LSRS, NULL, LTRT},
    995 /*SY*/ {LULU, LUSU, LUSU, LURU, LURU, LUSU, LURU, NULL, NULL, LSRS, NULL, LTRT},
    996 /*2C*/ {LULU, LUSU, LUSU, SUSU, SUSU, LUSU, SUSU, NULL, NULL, LSRS, NULL, LTRT},
    997 /*SC*/ {LULU, LUSU, LUSU, LURU, LURU, LUSU, LURU, NULL, NULL, LSRS, NULL, LTRT},
    998 /*!U*/ {LTLT, NULL, NULL, NULL, NULL, NULL, NULL, LTLT, LTLT, LSRS, NULL, LTRT},
    999 /*!T*/ {LTRT, NULL, NULL, NULL, NULL, NULL, NULL, LTLT, LTRT, LSRS, NULL, LTRT},
   1000 /*HT*/ {LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, NULL, LSRS},
   1001 /*PR*/ {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
   1002 /*PR*/ {LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LSRS, NULL, LTRT},
   1003 };
   1004 const P2PTransportChannelTest::Result*
   1005     P2PTransportChannelTest::kMatrixSharedSocketAsGice
   1006         [NUM_CONFIGS][NUM_CONFIGS] = {
   1007 //      OPEN  CONE  ADDR  PORT  SYMM  2CON  SCON  !UDP  !TCP  HTTP  PRXH  PRXS
   1008 /*OP*/ {LULU, LUSU, LUSU, LUSU, LUSU, LUSU, LUSU, LTLT, LTLT, LSRS, NULL, LTLT},
   1009 /*CO*/ {LULU, LUSU, LUSU, LUSU, LUSU, LUSU, LUSU, NULL, NULL, LSRS, NULL, LTRT},
   1010 /*AD*/ {LULU, LUSU, LUSU, LUSU, LUSU, LUSU, LUSU, NULL, NULL, LSRS, NULL, LTRT},
   1011 /*PO*/ {LULU, LUSU, LUSU, LUSU, LURU, LUSU, LURU, NULL, NULL, LSRS, NULL, LTRT},
   1012 /*SY*/ {LULU, LUSU, LUSU, LURU, LURU, LUSU, LURU, NULL, NULL, LSRS, NULL, LTRT},
   1013 /*2C*/ {LULU, LUSU, LUSU, LUSU, LUSU, LUSU, LUSU, NULL, NULL, LSRS, NULL, LTRT},
   1014 /*SC*/ {LULU, LUSU, LUSU, LURU, LURU, LUSU, LURU, NULL, NULL, LSRS, NULL, LTRT},
   1015 /*!U*/ {LTLT, NULL, NULL, NULL, NULL, NULL, NULL, LTLT, LTLT, LSRS, NULL, LTRT},
   1016 /*!T*/ {LTRT, NULL, NULL, NULL, NULL, NULL, NULL, LTLT, LTRT, LSRS, NULL, LTRT},
   1017 /*HT*/ {LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, NULL, LSRS},
   1018 /*PR*/ {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
   1019 /*PR*/ {LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LSRS, NULL, LTRT},
   1020 };
   1021 const P2PTransportChannelTest::Result*
   1022     P2PTransportChannelTest::kMatrixSharedSocketAsIce
   1023         [NUM_CONFIGS][NUM_CONFIGS] = {
   1024 //      OPEN  CONE  ADDR  PORT  SYMM  2CON  SCON  !UDP  !TCP  HTTP  PRXH  PRXS
   1025 /*OP*/ {LULU, LUSU, LUSU, LUSU, LUPU, LUSU, LUPU, PTLT, LTPT, LSRS, NULL, PTLT},
   1026 /*CO*/ {LULU, LUSU, LUSU, LUSU, LUPU, LUSU, LUPU, NULL, NULL, LSRS, NULL, LTRT},
   1027 /*AD*/ {LULU, LUSU, LUSU, LUSU, LUPU, LUSU, LUPU, NULL, NULL, LSRS, NULL, LTRT},
   1028 /*PO*/ {LULU, LUSU, LUSU, LUSU, LURU, LUSU, LURU, NULL, NULL, LSRS, NULL, LTRT},
   1029 /*SY*/ {PULU, PUSU, PUSU, PURU, PURU, PUSU, PURU, NULL, NULL, LSRS, NULL, LTRT},
   1030 /*2C*/ {LULU, LUSU, LUSU, LUSU, LUPU, LUSU, LUPU, NULL, NULL, LSRS, NULL, LTRT},
   1031 /*SC*/ {PULU, PUSU, PUSU, PURU, PURU, PUSU, PURU, NULL, NULL, LSRS, NULL, LTRT},
   1032 /*!U*/ {PTLT, NULL, NULL, NULL, NULL, NULL, NULL, PTLT, LTPT, LSRS, NULL, LTRT},
   1033 /*!T*/ {LTRT, NULL, NULL, NULL, NULL, NULL, NULL, PTLT, LTRT, LSRS, NULL, LTRT},
   1034 /*HT*/ {LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, NULL, LSRS},
   1035 /*PR*/ {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
   1036 /*PR*/ {LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LSRS, NULL, LTRT},
   1037 };
   1038 
   1039 // The actual tests that exercise all the various configurations.
   1040 // Test names are of the form P2PTransportChannelTest_TestOPENToNAT_FULL_CONE
   1041 // Same test case is run in both GICE and ICE mode.
   1042 // kDefaultStepDelay - is used for all Gice cases.
   1043 // kMinimumStepDelay - is used when both end points have
   1044 //                     PORTALLOCATOR_ENABLE_SHARED_UFRAG flag enabled.
   1045 // Technically we should be able to use kMinimumStepDelay irrespective of
   1046 // protocol type. But which might need modifications to current result matrices
   1047 // for tests in this file.
   1048 #define P2P_TEST_DECLARATION(x, y, z) \
   1049   TEST_F(P2PTransportChannelTest, z##Test##x##To##y##AsGiceNoneSharedUfrag) { \
   1050     ConfigureEndpoints(x, y, kDefaultPortAllocatorFlags, \
   1051                        kDefaultPortAllocatorFlags, \
   1052                        kDefaultStepDelay, kDefaultStepDelay, \
   1053                        cricket::ICEPROTO_GOOGLE); \
   1054     if (kMatrix[x][y] != NULL) \
   1055       Test(*kMatrix[x][y]); \
   1056     else \
   1057       LOG(LS_WARNING) << "Not yet implemented"; \
   1058   } \
   1059   TEST_F(P2PTransportChannelTest, z##Test##x##To##y##AsGiceP0SharedUfrag) { \
   1060     ConfigureEndpoints(x, y, PORTALLOCATOR_ENABLE_SHARED_UFRAG, \
   1061                        kDefaultPortAllocatorFlags, \
   1062                        kDefaultStepDelay, kDefaultStepDelay, \
   1063                        cricket::ICEPROTO_GOOGLE); \
   1064     if (kMatrix[x][y] != NULL) \
   1065       Test(*kMatrix[x][y]); \
   1066     else \
   1067       LOG(LS_WARNING) << "Not yet implemented"; \
   1068   } \
   1069   TEST_F(P2PTransportChannelTest, z##Test##x##To##y##AsGiceP1SharedUfrag) { \
   1070     ConfigureEndpoints(x, y, kDefaultPortAllocatorFlags, \
   1071                        PORTALLOCATOR_ENABLE_SHARED_UFRAG, \
   1072                        kDefaultStepDelay, kDefaultStepDelay, \
   1073                        cricket::ICEPROTO_GOOGLE); \
   1074     if (kMatrixSharedUfrag[x][y] != NULL) \
   1075       Test(*kMatrixSharedUfrag[x][y]); \
   1076     else \
   1077       LOG(LS_WARNING) << "Not yet implemented"; \
   1078   } \
   1079   TEST_F(P2PTransportChannelTest, z##Test##x##To##y##AsGiceBothSharedUfrag) { \
   1080     ConfigureEndpoints(x, y, PORTALLOCATOR_ENABLE_SHARED_UFRAG, \
   1081                        PORTALLOCATOR_ENABLE_SHARED_UFRAG, \
   1082                        kDefaultStepDelay, kDefaultStepDelay, \
   1083                        cricket::ICEPROTO_GOOGLE); \
   1084     if (kMatrixSharedUfrag[x][y] != NULL) \
   1085       Test(*kMatrixSharedUfrag[x][y]); \
   1086     else \
   1087       LOG(LS_WARNING) << "Not yet implemented"; \
   1088   } \
   1089   TEST_F(P2PTransportChannelTest, \
   1090          z##Test##x##To##y##AsGiceBothSharedUfragWithMinimumStepDelay) { \
   1091     ConfigureEndpoints(x, y, PORTALLOCATOR_ENABLE_SHARED_UFRAG, \
   1092                        PORTALLOCATOR_ENABLE_SHARED_UFRAG, \
   1093                        kMinimumStepDelay, kMinimumStepDelay, \
   1094                        cricket::ICEPROTO_GOOGLE); \
   1095     if (kMatrixSharedUfrag[x][y] != NULL) \
   1096       Test(*kMatrixSharedUfrag[x][y]); \
   1097     else \
   1098       LOG(LS_WARNING) << "Not yet implemented"; \
   1099   } \
   1100   TEST_F(P2PTransportChannelTest, \
   1101          z##Test##x##To##y##AsGiceBothSharedUfragSocket) { \
   1102     ConfigureEndpoints(x, y, PORTALLOCATOR_ENABLE_SHARED_UFRAG | \
   1103                        PORTALLOCATOR_ENABLE_SHARED_SOCKET, \
   1104                        PORTALLOCATOR_ENABLE_SHARED_UFRAG | \
   1105                        PORTALLOCATOR_ENABLE_SHARED_SOCKET, \
   1106                        kMinimumStepDelay, kMinimumStepDelay, \
   1107                        cricket::ICEPROTO_GOOGLE); \
   1108     if (kMatrixSharedSocketAsGice[x][y] != NULL) \
   1109       Test(*kMatrixSharedSocketAsGice[x][y]); \
   1110     else \
   1111     LOG(LS_WARNING) << "Not yet implemented"; \
   1112   } \
   1113   TEST_F(P2PTransportChannelTest, z##Test##x##To##y##AsIce) { \
   1114     ConfigureEndpoints(x, y, PORTALLOCATOR_ENABLE_SHARED_UFRAG | \
   1115                        PORTALLOCATOR_ENABLE_SHARED_SOCKET, \
   1116                        PORTALLOCATOR_ENABLE_SHARED_UFRAG | \
   1117                        PORTALLOCATOR_ENABLE_SHARED_SOCKET, \
   1118                        kMinimumStepDelay, kMinimumStepDelay, \
   1119                        cricket::ICEPROTO_RFC5245); \
   1120     if (kMatrixSharedSocketAsIce[x][y] != NULL) \
   1121       Test(*kMatrixSharedSocketAsIce[x][y]); \
   1122     else \
   1123     LOG(LS_WARNING) << "Not yet implemented"; \
   1124   }
   1125 
   1126 #define P2P_TEST(x, y) \
   1127   P2P_TEST_DECLARATION(x, y,)
   1128 
   1129 #define FLAKY_P2P_TEST(x, y) \
   1130   P2P_TEST_DECLARATION(x, y, DISABLED_)
   1131 
   1132 // TODO(holmer): Disabled due to randomly failing on webrtc buildbots.
   1133 // Issue: webrtc/2383
   1134 #define P2P_TEST_SET(x) \
   1135   P2P_TEST(x, OPEN) \
   1136   FLAKY_P2P_TEST(x, NAT_FULL_CONE) \
   1137   FLAKY_P2P_TEST(x, NAT_ADDR_RESTRICTED) \
   1138   FLAKY_P2P_TEST(x, NAT_PORT_RESTRICTED) \
   1139   P2P_TEST(x, NAT_SYMMETRIC) \
   1140   FLAKY_P2P_TEST(x, NAT_DOUBLE_CONE) \
   1141   P2P_TEST(x, NAT_SYMMETRIC_THEN_CONE) \
   1142   P2P_TEST(x, BLOCK_UDP) \
   1143   P2P_TEST(x, BLOCK_UDP_AND_INCOMING_TCP) \
   1144   P2P_TEST(x, BLOCK_ALL_BUT_OUTGOING_HTTP) \
   1145   P2P_TEST(x, PROXY_HTTPS) \
   1146   P2P_TEST(x, PROXY_SOCKS)
   1147 
   1148 #define FLAKY_P2P_TEST_SET(x) \
   1149   P2P_TEST(x, OPEN) \
   1150   P2P_TEST(x, NAT_FULL_CONE) \
   1151   P2P_TEST(x, NAT_ADDR_RESTRICTED) \
   1152   P2P_TEST(x, NAT_PORT_RESTRICTED) \
   1153   P2P_TEST(x, NAT_SYMMETRIC) \
   1154   P2P_TEST(x, NAT_DOUBLE_CONE) \
   1155   P2P_TEST(x, NAT_SYMMETRIC_THEN_CONE) \
   1156   P2P_TEST(x, BLOCK_UDP) \
   1157   P2P_TEST(x, BLOCK_UDP_AND_INCOMING_TCP) \
   1158   P2P_TEST(x, BLOCK_ALL_BUT_OUTGOING_HTTP) \
   1159   P2P_TEST(x, PROXY_HTTPS) \
   1160   P2P_TEST(x, PROXY_SOCKS)
   1161 
   1162 P2P_TEST_SET(OPEN)
   1163 P2P_TEST_SET(NAT_FULL_CONE)
   1164 P2P_TEST_SET(NAT_ADDR_RESTRICTED)
   1165 P2P_TEST_SET(NAT_PORT_RESTRICTED)
   1166 P2P_TEST_SET(NAT_SYMMETRIC)
   1167 P2P_TEST_SET(NAT_DOUBLE_CONE)
   1168 P2P_TEST_SET(NAT_SYMMETRIC_THEN_CONE)
   1169 P2P_TEST_SET(BLOCK_UDP)
   1170 P2P_TEST_SET(BLOCK_UDP_AND_INCOMING_TCP)
   1171 P2P_TEST_SET(BLOCK_ALL_BUT_OUTGOING_HTTP)
   1172 P2P_TEST_SET(PROXY_HTTPS)
   1173 P2P_TEST_SET(PROXY_SOCKS)
   1174 
   1175 // Test that we restart candidate allocation when local ufrag&pwd changed.
   1176 // Standard Ice protocol is used.
   1177 TEST_F(P2PTransportChannelTest, HandleUfragPwdChangeAsIce) {
   1178   ConfigureEndpoints(OPEN, OPEN,
   1179                      PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1180                      PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1181                      kMinimumStepDelay, kMinimumStepDelay,
   1182                      cricket::ICEPROTO_RFC5245);
   1183   CreateChannels(1);
   1184   TestHandleIceUfragPasswordChanged();
   1185   DestroyChannels();
   1186 }
   1187 
   1188 // Test that we restart candidate allocation when local ufrag&pwd changed.
   1189 // Standard Ice protocol is used.
   1190 TEST_F(P2PTransportChannelTest, HandleUfragPwdChangeBundleAsIce) {
   1191   ConfigureEndpoints(OPEN, OPEN,
   1192                      PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1193                      PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1194                      kMinimumStepDelay, kMinimumStepDelay,
   1195                      cricket::ICEPROTO_RFC5245);
   1196   SetAllocatorFlags(0, cricket::PORTALLOCATOR_ENABLE_BUNDLE);
   1197   SetAllocatorFlags(1, cricket::PORTALLOCATOR_ENABLE_BUNDLE);
   1198 
   1199   CreateChannels(2);
   1200   TestHandleIceUfragPasswordChanged();
   1201   DestroyChannels();
   1202 }
   1203 
   1204 // Test that we restart candidate allocation when local ufrag&pwd changed.
   1205 // Google Ice protocol is used.
   1206 TEST_F(P2PTransportChannelTest, HandleUfragPwdChangeAsGice) {
   1207   ConfigureEndpoints(OPEN, OPEN,
   1208                      PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1209                      PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1210                      kDefaultStepDelay, kDefaultStepDelay,
   1211                      cricket::ICEPROTO_GOOGLE);
   1212   CreateChannels(1);
   1213   TestHandleIceUfragPasswordChanged();
   1214   DestroyChannels();
   1215 }
   1216 
   1217 // Test that ICE restart works when bundle is enabled.
   1218 // Google Ice protocol is used.
   1219 TEST_F(P2PTransportChannelTest, HandleUfragPwdChangeBundleAsGice) {
   1220   ConfigureEndpoints(OPEN, OPEN,
   1221                      PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1222                      PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1223                      kDefaultStepDelay, kDefaultStepDelay,
   1224                      cricket::ICEPROTO_GOOGLE);
   1225   SetAllocatorFlags(0, cricket::PORTALLOCATOR_ENABLE_BUNDLE);
   1226   SetAllocatorFlags(1, cricket::PORTALLOCATOR_ENABLE_BUNDLE);
   1227 
   1228   CreateChannels(2);
   1229   TestHandleIceUfragPasswordChanged();
   1230   DestroyChannels();
   1231 }
   1232 
   1233 // Test the operation of GetStats.
   1234 TEST_F(P2PTransportChannelTest, GetStats) {
   1235   ConfigureEndpoints(OPEN, OPEN,
   1236                      kDefaultPortAllocatorFlags,
   1237                      kDefaultPortAllocatorFlags,
   1238                      kDefaultStepDelay, kDefaultStepDelay,
   1239                      cricket::ICEPROTO_GOOGLE);
   1240   CreateChannels(1);
   1241   EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->readable() && ep1_ch1()->writable() &&
   1242                           ep2_ch1()->readable() && ep2_ch1()->writable(),
   1243                           1000, 1000);
   1244   TestSendRecv(1);
   1245   cricket::ConnectionInfos infos;
   1246   ASSERT_TRUE(ep1_ch1()->GetStats(&infos));
   1247   ASSERT_EQ(1U, infos.size());
   1248   EXPECT_TRUE(infos[0].new_connection);
   1249   EXPECT_TRUE(infos[0].best_connection);
   1250   EXPECT_TRUE(infos[0].readable);
   1251   EXPECT_TRUE(infos[0].writable);
   1252   EXPECT_FALSE(infos[0].timeout);
   1253   EXPECT_EQ(10 * 36U, infos[0].sent_total_bytes);
   1254   EXPECT_EQ(10 * 36U, infos[0].recv_total_bytes);
   1255   EXPECT_GT(infos[0].rtt, 0U);
   1256   DestroyChannels();
   1257 }
   1258 
   1259 // Test that we properly handle getting a STUN error due to slow signaling.
   1260 TEST_F(P2PTransportChannelTest, DISABLED_SlowSignaling) {
   1261   ConfigureEndpoints(OPEN, NAT_SYMMETRIC,
   1262                      kDefaultPortAllocatorFlags,
   1263                      kDefaultPortAllocatorFlags,
   1264                      kDefaultStepDelay, kDefaultStepDelay,
   1265                      cricket::ICEPROTO_GOOGLE);
   1266   // Make signaling from the callee take 500ms, so that the initial STUN pings
   1267   // from the callee beat the signaling, and so the caller responds with a
   1268   // unknown username error. We should just eat that and carry on; mishandling
   1269   // this will instead cause all the callee's connections to be discarded.
   1270   SetSignalingDelay(1, 1000);
   1271   CreateChannels(1);
   1272   const cricket::Connection* best_connection = NULL;
   1273   // Wait until the callee's connections are created.
   1274   WAIT((best_connection = ep2_ch1()->best_connection()) != NULL, 1000);
   1275   // Wait to see if they get culled; they shouldn't.
   1276   WAIT(ep2_ch1()->best_connection() != best_connection, 1000);
   1277   EXPECT_TRUE(ep2_ch1()->best_connection() == best_connection);
   1278   DestroyChannels();
   1279 }
   1280 
   1281 // Test that if remote candidates don't have ufrag and pwd, we still work.
   1282 TEST_F(P2PTransportChannelTest, RemoteCandidatesWithoutUfragPwd) {
   1283   set_clear_remote_candidates_ufrag_pwd(true);
   1284   ConfigureEndpoints(OPEN, OPEN,
   1285                      PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1286                      PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1287                      kMinimumStepDelay, kMinimumStepDelay,
   1288                      cricket::ICEPROTO_GOOGLE);
   1289   CreateChannels(1);
   1290   const cricket::Connection* best_connection = NULL;
   1291   // Wait until the callee's connections are created.
   1292   WAIT((best_connection = ep2_ch1()->best_connection()) != NULL, 1000);
   1293   // Wait to see if they get culled; they shouldn't.
   1294   WAIT(ep2_ch1()->best_connection() != best_connection, 1000);
   1295   EXPECT_TRUE(ep2_ch1()->best_connection() == best_connection);
   1296   DestroyChannels();
   1297 }
   1298 
   1299 // Test that a host behind NAT cannot be reached when incoming_only
   1300 // is set to true.
   1301 TEST_F(P2PTransportChannelTest, IncomingOnlyBlocked) {
   1302   ConfigureEndpoints(NAT_FULL_CONE, OPEN,
   1303                      kDefaultPortAllocatorFlags,
   1304                      kDefaultPortAllocatorFlags,
   1305                      kDefaultStepDelay, kDefaultStepDelay,
   1306                      cricket::ICEPROTO_GOOGLE);
   1307 
   1308   SetAllocatorFlags(0, kOnlyLocalPorts);
   1309   CreateChannels(1);
   1310   ep1_ch1()->set_incoming_only(true);
   1311 
   1312   // Pump for 1 second and verify that the channels are not connected.
   1313   rtc::Thread::Current()->ProcessMessages(1000);
   1314 
   1315   EXPECT_FALSE(ep1_ch1()->readable());
   1316   EXPECT_FALSE(ep1_ch1()->writable());
   1317   EXPECT_FALSE(ep2_ch1()->readable());
   1318   EXPECT_FALSE(ep2_ch1()->writable());
   1319 
   1320   DestroyChannels();
   1321 }
   1322 
   1323 // Test that a peer behind NAT can connect to a peer that has
   1324 // incoming_only flag set.
   1325 TEST_F(P2PTransportChannelTest, IncomingOnlyOpen) {
   1326   ConfigureEndpoints(OPEN, NAT_FULL_CONE,
   1327                      kDefaultPortAllocatorFlags,
   1328                      kDefaultPortAllocatorFlags,
   1329                      kDefaultStepDelay, kDefaultStepDelay,
   1330                      cricket::ICEPROTO_GOOGLE);
   1331 
   1332   SetAllocatorFlags(0, kOnlyLocalPorts);
   1333   CreateChannels(1);
   1334   ep1_ch1()->set_incoming_only(true);
   1335 
   1336   EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL &&
   1337                           ep1_ch1()->readable() && ep1_ch1()->writable() &&
   1338                           ep2_ch1()->readable() && ep2_ch1()->writable(),
   1339                           1000, 1000);
   1340 
   1341   DestroyChannels();
   1342 }
   1343 
   1344 TEST_F(P2PTransportChannelTest, TestTcpConnectionsFromActiveToPassive) {
   1345   AddAddress(0, kPublicAddrs[0]);
   1346   AddAddress(1, kPublicAddrs[1]);
   1347 
   1348   SetAllocationStepDelay(0, kMinimumStepDelay);
   1349   SetAllocationStepDelay(1, kMinimumStepDelay);
   1350 
   1351   int kOnlyLocalTcpPorts = cricket::PORTALLOCATOR_DISABLE_UDP |
   1352                            cricket::PORTALLOCATOR_DISABLE_STUN |
   1353                            cricket::PORTALLOCATOR_DISABLE_RELAY |
   1354                            cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG;
   1355   // Disable all protocols except TCP.
   1356   SetAllocatorFlags(0, kOnlyLocalTcpPorts);
   1357   SetAllocatorFlags(1, kOnlyLocalTcpPorts);
   1358 
   1359   SetAllowTcpListen(0, true);   // actpass.
   1360   SetAllowTcpListen(1, false);  // active.
   1361 
   1362   CreateChannels(1);
   1363 
   1364   EXPECT_TRUE_WAIT(ep1_ch1()->readable() && ep1_ch1()->writable() &&
   1365                    ep2_ch1()->readable() && ep2_ch1()->writable(),
   1366                    1000);
   1367   EXPECT_TRUE(
   1368       ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
   1369       LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
   1370       RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
   1371 
   1372   std::string kTcpProtocol = "tcp";
   1373   EXPECT_EQ(kTcpProtocol, RemoteCandidate(ep1_ch1())->protocol());
   1374   EXPECT_EQ(kTcpProtocol, LocalCandidate(ep1_ch1())->protocol());
   1375   EXPECT_EQ(kTcpProtocol, RemoteCandidate(ep2_ch1())->protocol());
   1376   EXPECT_EQ(kTcpProtocol, LocalCandidate(ep2_ch1())->protocol());
   1377 
   1378   TestSendRecv(1);
   1379   DestroyChannels();
   1380 }
   1381 
   1382 TEST_F(P2PTransportChannelTest, TestBundleAllocatorToBundleAllocator) {
   1383   AddAddress(0, kPublicAddrs[0]);
   1384   AddAddress(1, kPublicAddrs[1]);
   1385   SetAllocatorFlags(0, cricket::PORTALLOCATOR_ENABLE_BUNDLE);
   1386   SetAllocatorFlags(1, cricket::PORTALLOCATOR_ENABLE_BUNDLE);
   1387 
   1388   CreateChannels(2);
   1389 
   1390   EXPECT_TRUE_WAIT(ep1_ch1()->readable() &&
   1391                    ep1_ch1()->writable() &&
   1392                    ep2_ch1()->readable() &&
   1393                    ep2_ch1()->writable(),
   1394                    1000);
   1395   EXPECT_TRUE(ep1_ch1()->best_connection() &&
   1396               ep2_ch1()->best_connection());
   1397 
   1398   EXPECT_FALSE(ep1_ch2()->readable());
   1399   EXPECT_FALSE(ep1_ch2()->writable());
   1400   EXPECT_FALSE(ep2_ch2()->readable());
   1401   EXPECT_FALSE(ep2_ch2()->writable());
   1402 
   1403   TestSendRecv(1);  // Only 1 channel is writable per Endpoint.
   1404   DestroyChannels();
   1405 }
   1406 
   1407 TEST_F(P2PTransportChannelTest, TestBundleAllocatorToNonBundleAllocator) {
   1408   AddAddress(0, kPublicAddrs[0]);
   1409   AddAddress(1, kPublicAddrs[1]);
   1410   // Enable BUNDLE flag at one side.
   1411   SetAllocatorFlags(0, cricket::PORTALLOCATOR_ENABLE_BUNDLE);
   1412 
   1413   CreateChannels(2);
   1414 
   1415   EXPECT_TRUE_WAIT(ep1_ch1()->readable() &&
   1416                    ep1_ch1()->writable() &&
   1417                    ep2_ch1()->readable() &&
   1418                    ep2_ch1()->writable(),
   1419                    1000);
   1420   EXPECT_TRUE_WAIT(ep1_ch2()->readable() &&
   1421                    ep1_ch2()->writable() &&
   1422                    ep2_ch2()->readable() &&
   1423                    ep2_ch2()->writable(),
   1424                    1000);
   1425 
   1426   EXPECT_TRUE(ep1_ch1()->best_connection() &&
   1427               ep2_ch1()->best_connection());
   1428   EXPECT_TRUE(ep1_ch2()->best_connection() &&
   1429               ep2_ch2()->best_connection());
   1430 
   1431   TestSendRecv(2);
   1432   DestroyChannels();
   1433 }
   1434 
   1435 TEST_F(P2PTransportChannelTest, TestIceRoleConflictWithoutBundle) {
   1436   AddAddress(0, kPublicAddrs[0]);
   1437   AddAddress(1, kPublicAddrs[1]);
   1438   TestSignalRoleConflict();
   1439 }
   1440 
   1441 TEST_F(P2PTransportChannelTest, TestIceRoleConflictWithBundle) {
   1442   AddAddress(0, kPublicAddrs[0]);
   1443   AddAddress(1, kPublicAddrs[1]);
   1444   SetAllocatorFlags(0, cricket::PORTALLOCATOR_ENABLE_BUNDLE);
   1445   SetAllocatorFlags(1, cricket::PORTALLOCATOR_ENABLE_BUNDLE);
   1446   TestSignalRoleConflict();
   1447 }
   1448 
   1449 // Tests that the ice configs (protocol, tiebreaker and role) can be passed
   1450 // down to ports.
   1451 TEST_F(P2PTransportChannelTest, TestIceConfigWillPassDownToPort) {
   1452   AddAddress(0, kPublicAddrs[0]);
   1453   AddAddress(1, kPublicAddrs[1]);
   1454 
   1455   SetIceRole(0, cricket::ICEROLE_CONTROLLING);
   1456   SetIceProtocol(0, cricket::ICEPROTO_GOOGLE);
   1457   SetIceTiebreaker(0, kTiebreaker1);
   1458   SetIceRole(1, cricket::ICEROLE_CONTROLLING);
   1459   SetIceProtocol(1, cricket::ICEPROTO_RFC5245);
   1460   SetIceTiebreaker(1, kTiebreaker2);
   1461 
   1462   CreateChannels(1);
   1463 
   1464   EXPECT_EQ_WAIT(2u, ep1_ch1()->ports().size(), 1000);
   1465 
   1466   const std::vector<cricket::PortInterface *> ports_before = ep1_ch1()->ports();
   1467   for (size_t i = 0; i < ports_before.size(); ++i) {
   1468     EXPECT_EQ(cricket::ICEROLE_CONTROLLING, ports_before[i]->GetIceRole());
   1469     EXPECT_EQ(cricket::ICEPROTO_GOOGLE, ports_before[i]->IceProtocol());
   1470     EXPECT_EQ(kTiebreaker1, ports_before[i]->IceTiebreaker());
   1471   }
   1472 
   1473   ep1_ch1()->SetIceRole(cricket::ICEROLE_CONTROLLED);
   1474   ep1_ch1()->SetIceProtocolType(cricket::ICEPROTO_RFC5245);
   1475   ep1_ch1()->SetIceTiebreaker(kTiebreaker2);
   1476 
   1477   const std::vector<cricket::PortInterface *> ports_after = ep1_ch1()->ports();
   1478   for (size_t i = 0; i < ports_after.size(); ++i) {
   1479     EXPECT_EQ(cricket::ICEROLE_CONTROLLED, ports_before[i]->GetIceRole());
   1480     EXPECT_EQ(cricket::ICEPROTO_RFC5245, ports_before[i]->IceProtocol());
   1481     // SetIceTiebreaker after Connect() has been called will fail. So expect the
   1482     // original value.
   1483     EXPECT_EQ(kTiebreaker1, ports_before[i]->IceTiebreaker());
   1484   }
   1485 
   1486   EXPECT_TRUE_WAIT(ep1_ch1()->readable() &&
   1487                    ep1_ch1()->writable() &&
   1488                    ep2_ch1()->readable() &&
   1489                    ep2_ch1()->writable(),
   1490                    1000);
   1491 
   1492   EXPECT_TRUE(ep1_ch1()->best_connection() &&
   1493               ep2_ch1()->best_connection());
   1494 
   1495   TestSendRecv(1);
   1496   DestroyChannels();
   1497 }
   1498 
   1499 // This test verifies channel can handle ice messages when channel is in
   1500 // hybrid mode.
   1501 TEST_F(P2PTransportChannelTest, TestConnectivityBetweenHybridandIce) {
   1502   TestHybridConnectivity(cricket::ICEPROTO_RFC5245);
   1503 }
   1504 
   1505 // This test verifies channel can handle Gice messages when channel is in
   1506 // hybrid mode.
   1507 TEST_F(P2PTransportChannelTest, TestConnectivityBetweenHybridandGice) {
   1508   TestHybridConnectivity(cricket::ICEPROTO_GOOGLE);
   1509 }
   1510 
   1511 // Verify that we can set DSCP value and retrieve properly from P2PTC.
   1512 TEST_F(P2PTransportChannelTest, TestDefaultDscpValue) {
   1513   AddAddress(0, kPublicAddrs[0]);
   1514   AddAddress(1, kPublicAddrs[1]);
   1515 
   1516   CreateChannels(1);
   1517   EXPECT_EQ(rtc::DSCP_NO_CHANGE,
   1518             GetEndpoint(0)->cd1_.ch_->DefaultDscpValue());
   1519   EXPECT_EQ(rtc::DSCP_NO_CHANGE,
   1520             GetEndpoint(1)->cd1_.ch_->DefaultDscpValue());
   1521   GetEndpoint(0)->cd1_.ch_->SetOption(
   1522       rtc::Socket::OPT_DSCP, rtc::DSCP_CS6);
   1523   GetEndpoint(1)->cd1_.ch_->SetOption(
   1524       rtc::Socket::OPT_DSCP, rtc::DSCP_CS6);
   1525   EXPECT_EQ(rtc::DSCP_CS6,
   1526             GetEndpoint(0)->cd1_.ch_->DefaultDscpValue());
   1527   EXPECT_EQ(rtc::DSCP_CS6,
   1528             GetEndpoint(1)->cd1_.ch_->DefaultDscpValue());
   1529   GetEndpoint(0)->cd1_.ch_->SetOption(
   1530       rtc::Socket::OPT_DSCP, rtc::DSCP_AF41);
   1531   GetEndpoint(1)->cd1_.ch_->SetOption(
   1532       rtc::Socket::OPT_DSCP, rtc::DSCP_AF41);
   1533   EXPECT_EQ(rtc::DSCP_AF41,
   1534             GetEndpoint(0)->cd1_.ch_->DefaultDscpValue());
   1535   EXPECT_EQ(rtc::DSCP_AF41,
   1536             GetEndpoint(1)->cd1_.ch_->DefaultDscpValue());
   1537 }
   1538 
   1539 // Verify IPv6 connection is preferred over IPv4.
   1540 // Flaky: https://code.google.com/p/webrtc/issues/detail?id=3317
   1541 TEST_F(P2PTransportChannelTest, DISABLED_TestIPv6Connections) {
   1542   AddAddress(0, kIPv6PublicAddrs[0]);
   1543   AddAddress(0, kPublicAddrs[0]);
   1544   AddAddress(1, kIPv6PublicAddrs[1]);
   1545   AddAddress(1, kPublicAddrs[1]);
   1546 
   1547   SetAllocationStepDelay(0, kMinimumStepDelay);
   1548   SetAllocationStepDelay(1, kMinimumStepDelay);
   1549 
   1550   // Enable IPv6
   1551   SetAllocatorFlags(0, cricket::PORTALLOCATOR_ENABLE_IPV6);
   1552   SetAllocatorFlags(1, cricket::PORTALLOCATOR_ENABLE_IPV6);
   1553 
   1554   CreateChannels(1);
   1555 
   1556   EXPECT_TRUE_WAIT(ep1_ch1()->readable() && ep1_ch1()->writable() &&
   1557                    ep2_ch1()->readable() && ep2_ch1()->writable(),
   1558                    1000);
   1559   EXPECT_TRUE(
   1560       ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
   1561       LocalCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[0]) &&
   1562       RemoteCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[1]));
   1563 
   1564   TestSendRecv(1);
   1565   DestroyChannels();
   1566 }
   1567 
   1568 // Testing forceful TURN connections.
   1569 TEST_F(P2PTransportChannelTest, TestForceTurn) {
   1570   ConfigureEndpoints(NAT_PORT_RESTRICTED, NAT_SYMMETRIC,
   1571                      kDefaultPortAllocatorFlags |
   1572                          cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
   1573                          cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1574                      kDefaultPortAllocatorFlags |
   1575                          cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
   1576                          cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG,
   1577                      kDefaultStepDelay, kDefaultStepDelay,
   1578                      cricket::ICEPROTO_RFC5245);
   1579   set_force_relay(true);
   1580 
   1581   SetAllocationStepDelay(0, kMinimumStepDelay);
   1582   SetAllocationStepDelay(1, kMinimumStepDelay);
   1583 
   1584   CreateChannels(1);
   1585 
   1586   EXPECT_TRUE_WAIT(ep1_ch1()->readable() &&
   1587                    ep1_ch1()->writable() &&
   1588                    ep2_ch1()->readable() &&
   1589                    ep2_ch1()->writable(),
   1590                    1000);
   1591 
   1592   EXPECT_TRUE(ep1_ch1()->best_connection() &&
   1593               ep2_ch1()->best_connection());
   1594 
   1595   EXPECT_EQ("relay", RemoteCandidate(ep1_ch1())->type());
   1596   EXPECT_EQ("relay", LocalCandidate(ep1_ch1())->type());
   1597   EXPECT_EQ("relay", RemoteCandidate(ep2_ch1())->type());
   1598   EXPECT_EQ("relay", LocalCandidate(ep2_ch1())->type());
   1599 
   1600   TestSendRecv(1);
   1601   DestroyChannels();
   1602 }
   1603 
   1604 // Test what happens when we have 2 users behind the same NAT. This can lead
   1605 // to interesting behavior because the STUN server will only give out the
   1606 // address of the outermost NAT.
   1607 class P2PTransportChannelSameNatTest : public P2PTransportChannelTestBase {
   1608  protected:
   1609   void ConfigureEndpoints(Config nat_type, Config config1, Config config2) {
   1610     ASSERT(nat_type >= NAT_FULL_CONE && nat_type <= NAT_SYMMETRIC);
   1611     rtc::NATSocketServer::Translator* outer_nat =
   1612         nat()->AddTranslator(kPublicAddrs[0], kNatAddrs[0],
   1613             static_cast<rtc::NATType>(nat_type - NAT_FULL_CONE));
   1614     ConfigureEndpoint(outer_nat, 0, config1);
   1615     ConfigureEndpoint(outer_nat, 1, config2);
   1616   }
   1617   void ConfigureEndpoint(rtc::NATSocketServer::Translator* nat,
   1618                          int endpoint, Config config) {
   1619     ASSERT(config <= NAT_SYMMETRIC);
   1620     if (config == OPEN) {
   1621       AddAddress(endpoint, kPrivateAddrs[endpoint]);
   1622       nat->AddClient(kPrivateAddrs[endpoint]);
   1623     } else {
   1624       AddAddress(endpoint, kCascadedPrivateAddrs[endpoint]);
   1625       nat->AddTranslator(kPrivateAddrs[endpoint], kCascadedNatAddrs[endpoint],
   1626           static_cast<rtc::NATType>(config - NAT_FULL_CONE))->AddClient(
   1627               kCascadedPrivateAddrs[endpoint]);
   1628     }
   1629   }
   1630 };
   1631 
   1632 TEST_F(P2PTransportChannelSameNatTest, TestConesBehindSameCone) {
   1633   ConfigureEndpoints(NAT_FULL_CONE, NAT_FULL_CONE, NAT_FULL_CONE);
   1634   Test(kLocalUdpToStunUdp);
   1635 }
   1636 
   1637 // Test what happens when we have multiple available pathways.
   1638 // In the future we will try different RTTs and configs for the different
   1639 // interfaces, so that we can simulate a user with Ethernet and VPN networks.
   1640 class P2PTransportChannelMultihomedTest : public P2PTransportChannelTestBase {
   1641 };
   1642 
   1643 // Test that we can establish connectivity when both peers are multihomed.
   1644 TEST_F(P2PTransportChannelMultihomedTest, DISABLED_TestBasic) {
   1645   AddAddress(0, kPublicAddrs[0]);
   1646   AddAddress(0, kAlternateAddrs[0]);
   1647   AddAddress(1, kPublicAddrs[1]);
   1648   AddAddress(1, kAlternateAddrs[1]);
   1649   Test(kLocalUdpToLocalUdp);
   1650 }
   1651 
   1652 // Test that we can quickly switch links if an interface goes down.
   1653 TEST_F(P2PTransportChannelMultihomedTest, TestFailover) {
   1654   AddAddress(0, kPublicAddrs[0]);
   1655   // Adding alternate address will make sure |kPublicAddrs| has the higher
   1656   // priority than others. This is due to FakeNetwork::AddInterface method.
   1657   AddAddress(1, kAlternateAddrs[1]);
   1658   AddAddress(1, kPublicAddrs[1]);
   1659 
   1660   // Use only local ports for simplicity.
   1661   SetAllocatorFlags(0, kOnlyLocalPorts);
   1662   SetAllocatorFlags(1, kOnlyLocalPorts);
   1663 
   1664   // Create channels and let them go writable, as usual.
   1665   CreateChannels(1);
   1666   EXPECT_TRUE_WAIT(ep1_ch1()->readable() && ep1_ch1()->writable() &&
   1667                    ep2_ch1()->readable() && ep2_ch1()->writable(),
   1668                    1000);
   1669   EXPECT_TRUE(
   1670       ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
   1671       LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
   1672       RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
   1673 
   1674   // Blackhole any traffic to or from the public addrs.
   1675   LOG(LS_INFO) << "Failing over...";
   1676   fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY,
   1677                 kPublicAddrs[1]);
   1678 
   1679   // We should detect loss of connectivity within 5 seconds or so.
   1680   EXPECT_TRUE_WAIT(!ep1_ch1()->writable(), 7000);
   1681 
   1682   // We should switch over to use the alternate addr immediately
   1683   // when we lose writability.
   1684   EXPECT_TRUE_WAIT(
   1685       ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
   1686       LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
   1687       RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[1]),
   1688       3000);
   1689 
   1690   DestroyChannels();
   1691 }
   1692 
   1693 // Test that we can switch links in a coordinated fashion.
   1694 TEST_F(P2PTransportChannelMultihomedTest, TestDrain) {
   1695   AddAddress(0, kPublicAddrs[0]);
   1696   AddAddress(1, kPublicAddrs[1]);
   1697   // Use only local ports for simplicity.
   1698   SetAllocatorFlags(0, kOnlyLocalPorts);
   1699   SetAllocatorFlags(1, kOnlyLocalPorts);
   1700 
   1701   // Create channels and let them go writable, as usual.
   1702   CreateChannels(1);
   1703   EXPECT_TRUE_WAIT(ep1_ch1()->readable() && ep1_ch1()->writable() &&
   1704                    ep2_ch1()->readable() && ep2_ch1()->writable(),
   1705                    1000);
   1706   EXPECT_TRUE(
   1707       ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
   1708       LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
   1709       RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
   1710 
   1711   // Remove the public interface, add the alternate interface, and allocate
   1712   // a new generation of candidates for the new interface (via Connect()).
   1713   LOG(LS_INFO) << "Draining...";
   1714   AddAddress(1, kAlternateAddrs[1]);
   1715   RemoveAddress(1, kPublicAddrs[1]);
   1716   ep2_ch1()->Connect();
   1717 
   1718   // We should switch over to use the alternate address after
   1719   // an exchange of pings.
   1720   EXPECT_TRUE_WAIT(
   1721       ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
   1722       LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
   1723       RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[1]),
   1724       3000);
   1725 
   1726   DestroyChannels();
   1727 }
   1728