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