10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2011 Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/constants.h"
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/fakesession.h"
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/p2ptransport.h"
31cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "talk/p2p/base/parsing.h"
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/rawtransport.h"
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/sessionmessages.h"
3413b2d035e2e7f2f18e3a4d3377bc1a09f43a4ff9buildbot@webrtc.org#include "webrtc/libjingle/xmllite/xmlelement.h"
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/xmpp/constants.h"
36cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/fakesslidentity.h"
37cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/gunit.h"
38cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/thread.h"
390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing cricket::Candidate;
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing cricket::Candidates;
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing cricket::Transport;
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing cricket::FakeTransport;
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing cricket::TransportChannel;
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing cricket::FakeTransportChannel;
46e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.orgusing cricket::IceRole;
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing cricket::TransportDescription;
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing cricket::WriteError;
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing cricket::ParseError;
502a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgusing rtc::SocketAddress;
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kIceUfrag1[] = "TESTICEUFRAG0001";
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kIcePwd1[] = "TESTICEPWD00000000000001";
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5539c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.orgstatic const char kIceUfrag2[] = "TESTICEUFRAG0002";
5639c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.orgstatic const char kIcePwd2[] = "TESTICEPWD00000000000002";
5739c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass TransportTest : public testing::Test,
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                      public sigslot::has_slots<> {
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportTest()
622a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      : thread_(rtc::Thread::Current()),
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        transport_(new FakeTransport(
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org            thread_, thread_, "test content name", NULL)),
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        channel_(NULL),
66f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org        connecting_signalled_(false),
67f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org        completed_(false),
68f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org        failed_(false) {
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    transport_->SignalConnecting.connect(this, &TransportTest::OnConnecting);
70f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org    transport_->SignalCompleted.connect(this, &TransportTest::OnCompleted);
71f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org    transport_->SignalFailed.connect(this, &TransportTest::OnFailed);
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ~TransportTest() {
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    transport_->DestroyAllChannels();
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SetupChannel() {
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    channel_ = CreateChannel(1);
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return (channel_ != NULL);
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  FakeTransportChannel* CreateChannel(int component) {
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return static_cast<FakeTransportChannel*>(
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        transport_->CreateChannel(component));
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void DestroyChannel() {
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    transport_->DestroyChannel(1);
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    channel_ = NULL;
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnConnecting(Transport* transport) {
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    connecting_signalled_ = true;
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
93f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  void OnCompleted(Transport* transport) {
94f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org    completed_ = true;
95f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  }
96f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  void OnFailed(Transport* transport) {
97f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org    failed_ = true;
98f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  }
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1002a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* thread_;
1012a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<FakeTransport> transport_;
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  FakeTransportChannel* channel_;
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool connecting_signalled_;
104f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  bool completed_;
105f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  bool failed_;
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass FakeCandidateTranslator : public cricket::CandidateTranslator {
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void AddMapping(int component, const std::string& channel_name) {
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    name_to_component[channel_name] = component;
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    component_to_name[component] = channel_name;
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool GetChannelNameFromComponent(
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      int component, std::string* channel_name) const {
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (component_to_name.find(component) == component_to_name.end()) {
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      return false;
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    *channel_name = component_to_name.find(component)->second;
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return true;
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool GetComponentFromChannelName(
1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      const std::string& channel_name, int* component) const {
1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (name_to_component.find(channel_name) == name_to_component.end()) {
1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      return false;
1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    *component = name_to_component.find(channel_name)->second;
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return true;
1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::map<std::string, int> name_to_component;
1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::map<int, std::string> component_to_name;
1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Test that calling ConnectChannels triggers an OnConnecting signal.
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(TransportTest, TestConnectChannelsDoesSignal) {
1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(SetupChannel());
1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  transport_->ConnectChannels();
1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(connecting_signalled_);
1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE_WAIT(connecting_signalled_, 100);
1430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Test that DestroyAllChannels kills any pending OnConnecting signals.
1460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(TransportTest, TestDestroyAllClearsPosts) {
1470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(transport_->CreateChannel(1) != NULL);
1480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  transport_->ConnectChannels();
1500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  transport_->DestroyAllChannels();
1510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  thread_->ProcessMessages(0);
1530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(connecting_signalled_);
1540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// This test verifies channels are created with proper ICE
157e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org// role, tiebreaker and remote ice mode and credentials after offer and
158e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org// answer negotiations.
1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(TransportTest, TestChannelIceParameters) {
160e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  transport_->SetIceRole(cricket::ICEROLE_CONTROLLING);
161e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  transport_->SetIceTiebreaker(99U);
1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::TransportDescription local_desc(
163a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag1, kIcePwd1);
1640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(transport_->SetLocalTransportDescription(local_desc,
165f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                       cricket::CA_OFFER,
166f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                       NULL));
167e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, transport_->ice_role());
1680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(SetupChannel());
169e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, channel_->GetIceRole());
1700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(cricket::ICEMODE_FULL, channel_->remote_ice_mode());
1710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(kIceUfrag1, channel_->ice_ufrag());
1720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(kIcePwd1, channel_->ice_pwd());
1730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::TransportDescription remote_desc(
175a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag1, kIcePwd1);
1760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(transport_->SetRemoteTransportDescription(remote_desc,
177f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                        cricket::CA_ANSWER,
178f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                        NULL));
179e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, channel_->GetIceRole());
180e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  EXPECT_EQ(99U, channel_->IceTiebreaker());
1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(cricket::ICEMODE_FULL, channel_->remote_ice_mode());
1820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Changing the transport role from CONTROLLING to CONTROLLED.
183e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  transport_->SetIceRole(cricket::ICEROLE_CONTROLLED);
184e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLED, channel_->GetIceRole());
1850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(cricket::ICEMODE_FULL, channel_->remote_ice_mode());
1860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(kIceUfrag1, channel_->remote_ice_ufrag());
1870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(kIcePwd1, channel_->remote_ice_pwd());
1880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
19039c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org// Verifies that IceCredentialsChanged returns true when either ufrag or pwd
19139c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org// changed, and false in other cases.
19239c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.orgTEST_F(TransportTest, TestIceCredentialsChanged) {
19339c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_TRUE(cricket::IceCredentialsChanged("u1", "p1", "u2", "p2"));
19439c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_TRUE(cricket::IceCredentialsChanged("u1", "p1", "u2", "p1"));
19539c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_TRUE(cricket::IceCredentialsChanged("u1", "p1", "u1", "p2"));
19639c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_FALSE(cricket::IceCredentialsChanged("u1", "p1", "u1", "p1"));
19739c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org}
19839c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
19939c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org// This test verifies that the callee's ICE role changes from controlled to
20039c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org// controlling when the callee triggers an ICE restart.
20139c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.orgTEST_F(TransportTest, TestIceControlledToControllingOnIceRestart) {
20239c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_TRUE(SetupChannel());
20339c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  transport_->SetIceRole(cricket::ICEROLE_CONTROLLED);
20439c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
20539c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  cricket::TransportDescription desc(
20639c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag1, kIcePwd1);
20739c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  ASSERT_TRUE(transport_->SetRemoteTransportDescription(desc,
20839c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                        cricket::CA_OFFER,
20939c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                        NULL));
21039c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  ASSERT_TRUE(transport_->SetLocalTransportDescription(desc,
21139c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       cricket::CA_ANSWER,
21239c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       NULL));
21339c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLED, transport_->ice_role());
21439c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
21539c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  cricket::TransportDescription new_local_desc(
21639c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag2, kIcePwd2);
21739c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  ASSERT_TRUE(transport_->SetLocalTransportDescription(new_local_desc,
21839c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       cricket::CA_OFFER,
21939c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       NULL));
22039c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, transport_->ice_role());
22139c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, channel_->GetIceRole());
22239c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org}
22339c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
22439c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org// This test verifies that the caller's ICE role changes from controlling to
22539c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org// controlled when the callee triggers an ICE restart.
22639c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.orgTEST_F(TransportTest, TestIceControllingToControlledOnIceRestart) {
22739c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_TRUE(SetupChannel());
22839c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  transport_->SetIceRole(cricket::ICEROLE_CONTROLLING);
22939c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
23039c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  cricket::TransportDescription desc(
23139c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag1, kIcePwd1);
23239c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  ASSERT_TRUE(transport_->SetLocalTransportDescription(desc,
23339c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       cricket::CA_OFFER,
23439c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       NULL));
23539c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  ASSERT_TRUE(transport_->SetRemoteTransportDescription(desc,
23639c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                        cricket::CA_ANSWER,
23739c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                        NULL));
23839c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, transport_->ice_role());
23939c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
24039c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  cricket::TransportDescription new_local_desc(
24139c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag2, kIcePwd2);
24239c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  ASSERT_TRUE(transport_->SetLocalTransportDescription(new_local_desc,
24339c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       cricket::CA_ANSWER,
24439c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       NULL));
24539c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLED, transport_->ice_role());
24639c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLED, channel_->GetIceRole());
24739c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org}
24839c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
24939c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org// This test verifies that the caller's ICE role is still controlling after the
25039c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org// callee triggers ICE restart if the callee's ICE mode is LITE.
25139c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.orgTEST_F(TransportTest, TestIceControllingOnIceRestartIfRemoteIsIceLite) {
25239c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_TRUE(SetupChannel());
25339c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  transport_->SetIceRole(cricket::ICEROLE_CONTROLLING);
25439c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
25539c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  cricket::TransportDescription desc(
25639c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag1, kIcePwd1);
25739c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  ASSERT_TRUE(transport_->SetLocalTransportDescription(desc,
25839c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       cricket::CA_OFFER,
25939c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       NULL));
26039c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
26139c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  cricket::TransportDescription remote_desc(
26239c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org      cricket::NS_JINGLE_ICE_UDP, std::vector<std::string>(),
26339c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org      kIceUfrag1, kIcePwd1, cricket::ICEMODE_LITE,
26439c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org      cricket::CONNECTIONROLE_NONE, NULL, cricket::Candidates());
26539c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  ASSERT_TRUE(transport_->SetRemoteTransportDescription(remote_desc,
26639c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                        cricket::CA_ANSWER,
26739c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                        NULL));
26839c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
26939c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, transport_->ice_role());
27039c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
27139c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  cricket::TransportDescription new_local_desc(
27239c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag2, kIcePwd2);
27339c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  ASSERT_TRUE(transport_->SetLocalTransportDescription(new_local_desc,
27439c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       cricket::CA_ANSWER,
27539c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org                                                       NULL));
27639c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, transport_->ice_role());
27739c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, channel_->GetIceRole());
27839c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org}
27939c443b6209621f0e8537e14514cc4c8b1dd5fadjiayl@webrtc.org
280f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org// This test verifies that the Completed and Failed states can be reached.
281f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.orgTEST_F(TransportTest, TestChannelCompletedAndFailed) {
282f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  transport_->SetIceRole(cricket::ICEROLE_CONTROLLING);
283f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  cricket::TransportDescription local_desc(
284f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag1, kIcePwd1);
285f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  ASSERT_TRUE(transport_->SetLocalTransportDescription(local_desc,
286f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org                                                       cricket::CA_OFFER,
287f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org                                                       NULL));
288f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  EXPECT_TRUE(SetupChannel());
289f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org
290f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  cricket::TransportDescription remote_desc(
291f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag1, kIcePwd1);
292f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  ASSERT_TRUE(transport_->SetRemoteTransportDescription(remote_desc,
293f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org                                                        cricket::CA_ANSWER,
294f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org                                                        NULL));
295f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org
296f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  channel_->SetConnectionCount(2);
297f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  channel_->SignalCandidatesAllocationDone(channel_);
298f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  channel_->SetWritable(true);
299f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  EXPECT_TRUE_WAIT(transport_->all_channels_writable(), 100);
300f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  // ICE is not yet completed because there is still more than one connection.
301f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  EXPECT_FALSE(completed_);
302f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  EXPECT_FALSE(failed_);
303f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org
304f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  // When the connection count drops to 1, SignalCompleted should be emitted,
305f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  // and completed() should be true.
306f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  channel_->SetConnectionCount(1);
307f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  EXPECT_TRUE_WAIT(completed_, 100);
308f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  completed_ = false;
309f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org
310f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  // When the connection count drops to 0, SignalFailed should be emitted, and
311f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  // completed() should be false.
312f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  channel_->SetConnectionCount(0);
313f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  EXPECT_TRUE_WAIT(failed_, 100);
314f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  EXPECT_FALSE(completed_);
315f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org}
316f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org
3170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Tests channel role is reversed after receiving ice-lite from remote.
3180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(TransportTest, TestSetRemoteIceLiteInOffer) {
319e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  transport_->SetIceRole(cricket::ICEROLE_CONTROLLED);
3200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::TransportDescription remote_desc(
3210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      cricket::NS_JINGLE_ICE_UDP, std::vector<std::string>(),
322a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org      kIceUfrag1, kIcePwd1, cricket::ICEMODE_LITE,
323a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org      cricket::CONNECTIONROLE_ACTPASS, NULL, cricket::Candidates());
3240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(transport_->SetRemoteTransportDescription(remote_desc,
325f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                        cricket::CA_OFFER,
326f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                        NULL));
3270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::TransportDescription local_desc(
328a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag1, kIcePwd1);
3290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(transport_->SetLocalTransportDescription(local_desc,
330f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                       cricket::CA_ANSWER,
331f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                       NULL));
332e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, transport_->ice_role());
3330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(SetupChannel());
334e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, channel_->GetIceRole());
3350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(cricket::ICEMODE_LITE, channel_->remote_ice_mode());
3360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
3370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Tests ice-lite in remote answer.
3390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(TransportTest, TestSetRemoteIceLiteInAnswer) {
340e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  transport_->SetIceRole(cricket::ICEROLE_CONTROLLING);
3410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::TransportDescription local_desc(
342a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org      cricket::NS_JINGLE_ICE_UDP, kIceUfrag1, kIcePwd1);
3430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(transport_->SetLocalTransportDescription(local_desc,
344f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                       cricket::CA_OFFER,
345f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                       NULL));
346e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, transport_->ice_role());
3470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(SetupChannel());
348e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, channel_->GetIceRole());
3490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Channels will be created in ICEFULL_MODE.
3500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(cricket::ICEMODE_FULL, channel_->remote_ice_mode());
3510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::TransportDescription remote_desc(
3520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      cricket::NS_JINGLE_ICE_UDP, std::vector<std::string>(),
353a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org      kIceUfrag1, kIcePwd1, cricket::ICEMODE_LITE,
354a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org      cricket::CONNECTIONROLE_NONE, NULL, cricket::Candidates());
3550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(transport_->SetRemoteTransportDescription(remote_desc,
356f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                        cricket::CA_ANSWER,
357f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                                        NULL));
358e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  EXPECT_EQ(cricket::ICEROLE_CONTROLLING, channel_->GetIceRole());
3590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // After receiving remote description with ICEMODE_LITE, channel should
3600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // have mode set to ICEMODE_LITE.
3610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(cricket::ICEMODE_LITE, channel_->remote_ice_mode());
3620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
3630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Tests that we can properly serialize/deserialize candidates.
3650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(TransportTest, TestP2PTransportWriteAndParseCandidate) {
3660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Candidate test_candidate(
3670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      "", 1, "udp",
3682a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      rtc::SocketAddress("2001:db8:fefe::1", 9999),
3690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      738197504, "abcdef", "ghijkl", "foo", "testnet", 50, "");
3700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Candidate test_candidate2(
3710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      "", 2, "tcp",
3722a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      rtc::SocketAddress("192.168.7.1", 9999),
3730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      1107296256, "mnopqr", "stuvwx", "bar", "testnet2", 100, "");
3742a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::SocketAddress host_address("www.google.com", 24601);
3752a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  host_address.SetResolvedIP(rtc::IPAddress(0x0A000001));
3760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Candidate test_candidate3(
3770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      "", 3, "spdy", host_address, 1476395008, "yzabcd",
3780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      "efghij", "baz", "testnet3", 150, "");
3790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  WriteError write_error;
3800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ParseError parse_error;
3812a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<buzz::XmlElement> elem;
3820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::Candidate parsed_candidate;
3830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::P2PTransportParser parser;
3840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  FakeCandidateTranslator translator;
3860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  translator.AddMapping(1, "test");
3870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  translator.AddMapping(2, "test2");
3880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  translator.AddMapping(3, "test3");
3890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(parser.WriteGingleCandidate(test_candidate, &translator,
3910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                          elem.accept(), &write_error));
3920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("", write_error.text);
3930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("test", elem->Attr(buzz::QN_NAME));
3940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("udp", elem->Attr(cricket::QN_PROTOCOL));
3950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("2001:db8:fefe::1", elem->Attr(cricket::QN_ADDRESS));
3960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("9999", elem->Attr(cricket::QN_PORT));
3970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("0.34", elem->Attr(cricket::QN_PREFERENCE));
3980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("abcdef", elem->Attr(cricket::QN_USERNAME));
3990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("ghijkl", elem->Attr(cricket::QN_PASSWORD));
4000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("foo", elem->Attr(cricket::QN_TYPE));
4010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("testnet", elem->Attr(cricket::QN_NETWORK));
4020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("50", elem->Attr(cricket::QN_GENERATION));
4030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(parser.ParseGingleCandidate(elem.get(), &translator,
4050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                          &parsed_candidate, &parse_error));
4060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(test_candidate.IsEquivalent(parsed_candidate));
4070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(parser.WriteGingleCandidate(test_candidate2, &translator,
4090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                          elem.accept(), &write_error));
4100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("test2", elem->Attr(buzz::QN_NAME));
4110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("tcp", elem->Attr(cricket::QN_PROTOCOL));
4120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("192.168.7.1", elem->Attr(cricket::QN_ADDRESS));
4130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("9999", elem->Attr(cricket::QN_PORT));
4140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("0.51", elem->Attr(cricket::QN_PREFERENCE));
4150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("mnopqr", elem->Attr(cricket::QN_USERNAME));
4160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("stuvwx", elem->Attr(cricket::QN_PASSWORD));
4170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("bar", elem->Attr(cricket::QN_TYPE));
4180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("testnet2", elem->Attr(cricket::QN_NETWORK));
4190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("100", elem->Attr(cricket::QN_GENERATION));
4200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(parser.ParseGingleCandidate(elem.get(), &translator,
4220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                          &parsed_candidate, &parse_error));
4230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(test_candidate2.IsEquivalent(parsed_candidate));
4240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Check that an ip is preferred over hostname.
4260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(parser.WriteGingleCandidate(test_candidate3, &translator,
4270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                          elem.accept(), &write_error));
4280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("test3", elem->Attr(cricket::QN_NAME));
4290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("spdy", elem->Attr(cricket::QN_PROTOCOL));
4300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("10.0.0.1", elem->Attr(cricket::QN_ADDRESS));
4310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("24601", elem->Attr(cricket::QN_PORT));
4320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("0.69", elem->Attr(cricket::QN_PREFERENCE));
4330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("yzabcd", elem->Attr(cricket::QN_USERNAME));
4340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("efghij", elem->Attr(cricket::QN_PASSWORD));
4350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("baz", elem->Attr(cricket::QN_TYPE));
4360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("testnet3", elem->Attr(cricket::QN_NETWORK));
4370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ("150", elem->Attr(cricket::QN_GENERATION));
4380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(parser.ParseGingleCandidate(elem.get(), &translator,
4400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                          &parsed_candidate, &parse_error));
4410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(test_candidate3.IsEquivalent(parsed_candidate));
4420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
4430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(TransportTest, TestGetStats) {
4450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(SetupChannel());
4460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::TransportStats stats;
4470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(transport_->GetStats(&stats));
4480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Note that this tests the behavior of a FakeTransportChannel.
4490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_EQ(1U, stats.channel_stats.size());
4500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, stats.channel_stats[0].component);
4510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  transport_->ConnectChannels();
4520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(transport_->GetStats(&stats));
4530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_EQ(1U, stats.channel_stats.size());
4540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, stats.channel_stats[0].component);
4550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
456