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