10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2012, 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 <string>
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/app/webrtc/jsepicecandidate.h"
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/app/webrtc/jsepsessiondescription.h"
32cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "talk/p2p/base/candidate.h"
33cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "talk/p2p/base/constants.h"
34cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "talk/p2p/base/sessiondescription.h"
35cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "talk/session/media/mediasession.h"
362a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/gunit.h"
372a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/helpers.h"
382a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/scoped_ptr.h"
392a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/ssladapter.h"
402a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/stringencode.h"
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing webrtc::IceCandidateCollection;
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing webrtc::IceCandidateInterface;
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing webrtc::JsepIceCandidate;
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing webrtc::JsepSessionDescription;
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing webrtc::SessionDescriptionInterface;
472a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgusing rtc::scoped_ptr;
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kCandidateUfrag[] = "ufrag";
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kCandidatePwd[] = "pwd";
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kCandidateUfragVoice[] = "ufrag_voice";
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kCandidatePwdVoice[] = "pwd_voice";
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kCandidateUfragVideo[] = "ufrag_video";
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kCandidatePwdVideo[] = "pwd_video";
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// This creates a session description with both audio and video media contents.
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// In SDP this is described by two m lines, one audio and one video.
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic cricket::SessionDescription* CreateCricketSessionDescription() {
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::SessionDescription* desc(new cricket::SessionDescription());
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // AudioContentDescription
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  scoped_ptr<cricket::AudioContentDescription> audio(
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      new cricket::AudioContentDescription());
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // VideoContentDescription
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  scoped_ptr<cricket::VideoContentDescription> video(
660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      new cricket::VideoContentDescription());
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0, 0));
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  desc->AddContent(cricket::CN_AUDIO, cricket::NS_JINGLE_RTP,
700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                   audio.release());
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  video->AddCodec(cricket::VideoCodec(120, "VP8", 640, 480, 30, 0));
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  desc->AddContent(cricket::CN_VIDEO, cricket::NS_JINGLE_RTP,
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                   video.release());
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(desc->AddTransportInfo(
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      cricket::TransportInfo(
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                             cricket::CN_AUDIO,
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                             cricket::TransportDescription(
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 cricket::NS_GINGLE_P2P,
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 std::vector<std::string>(),
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 kCandidateUfragVoice, kCandidatePwdVoice,
83a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org                                 cricket::ICEMODE_FULL,
84a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org                                 cricket::CONNECTIONROLE_NONE,
85a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org                                 NULL, cricket::Candidates()))));
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(desc->AddTransportInfo(
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      cricket::TransportInfo(cricket::CN_VIDEO,
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                             cricket::TransportDescription(
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 cricket::NS_GINGLE_P2P,
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 std::vector<std::string>(),
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 kCandidateUfragVideo, kCandidatePwdVideo,
92a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org                                 cricket::ICEMODE_FULL,
93a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org                                 cricket::CONNECTIONROLE_NONE,
94a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org                                 NULL, cricket::Candidates()))));
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return desc;
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass JsepSessionDescriptionTest : public testing::Test {
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
10013a42bcb8b909faef285ec36b59740e5696a2295jiayl@webrtc.org  static void SetUpTestCase() {
1012a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    rtc::InitializeSSL();
10213a42bcb8b909faef285ec36b59740e5696a2295jiayl@webrtc.org  }
10313a42bcb8b909faef285ec36b59740e5696a2295jiayl@webrtc.org
10413a42bcb8b909faef285ec36b59740e5696a2295jiayl@webrtc.org  static void TearDownTestCase() {
1052a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    rtc::CleanupSSL();
10613a42bcb8b909faef285ec36b59740e5696a2295jiayl@webrtc.org  }
10713a42bcb8b909faef285ec36b59740e5696a2295jiayl@webrtc.org
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void SetUp() {
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    int port = 1234;
1102a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    rtc::SocketAddress address("127.0.0.1", port++);
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    cricket::Candidate candidate("rtp", cricket::ICE_CANDIDATE_COMPONENT_RTP,
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 "udp", address, 1, "",
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 "", "local", "eth0", 0, "1");
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    candidate_ = candidate;
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    const std::string session_id =
1162a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org        rtc::ToString(rtc::CreateRandomId64());
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    const std::string session_version =
1182a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org        rtc::ToString(rtc::CreateRandomId());
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    jsep_desc_.reset(new JsepSessionDescription("dummy"));
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(),
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        session_id, session_version));
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string Serialize(const SessionDescriptionInterface* desc) {
1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    std::string sdp;
1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE(desc->ToString(&sdp));
1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_FALSE(sdp.empty());
1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return sdp;
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  SessionDescriptionInterface* DeSerialize(const std::string& sdp) {
1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    JsepSessionDescription* desc(new JsepSessionDescription("dummy"));
1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE(desc->Initialize(sdp, NULL));
1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return desc;
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::Candidate candidate_;
1382a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<JsepSessionDescription> jsep_desc_;
1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Test that number_of_mediasections() returns the number of media contents in
1420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// a session description.
1430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
1440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
1450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Test that we can add a candidate to a session description.
1480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
1490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  JsepIceCandidate jsep_candidate("", 0, candidate_);
1500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
1510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
1520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(ice_candidates != NULL);
1530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1u, ice_candidates->count());
1540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
1550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(ice_candidate != NULL);
1560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  candidate_.set_username(kCandidateUfragVoice);
1570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  candidate_.set_password(kCandidatePwdVoice);
1580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(0, ice_candidate->sdp_mline_index());
1600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
1610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(JsepSessionDescriptionTest, AddCandidateWithMid) {
1640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // mid and m-line index don't match, in this case mid is preferred.
1650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  JsepIceCandidate jsep_candidate("video", 0, candidate_);
1660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
1670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
1680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
1690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(ice_candidates != NULL);
1700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1u, ice_candidates->count());
1710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
1720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(ice_candidate != NULL);
1730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  candidate_.set_username(kCandidateUfragVideo);
1740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  candidate_.set_password(kCandidatePwdVideo);
1750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
1760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // The mline index should have been updated according to mid.
1770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, ice_candidate->sdp_mline_index());
1780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  candidate_.set_username(kCandidateUfrag);
1820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  candidate_.set_password(kCandidatePwd);
1830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  JsepIceCandidate jsep_candidate("audio", 0, candidate_);
1840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
1850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
1860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(ice_candidates != NULL);
1870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1u, ice_candidates->count());
1880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
1890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT_TRUE(ice_candidate != NULL);
1900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  candidate_.set_username(kCandidateUfrag);
1910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  candidate_.set_password(kCandidatePwd);
1920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
1930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
1950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Test that we can not add a candidate if there is no corresponding media
1980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// content in the session description.
1990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
2000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  JsepIceCandidate bad_candidate1("", 55, candidate_);
2010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
2020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
2040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
2050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
207b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org// Tests that repeatedly adding the same candidate, with or without credentials,
208b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org// does not increase the number of candidates in the description.
209b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.orgTEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
210b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  JsepIceCandidate jsep_candidate("", 0, candidate_);
211b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
212b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
213b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org
214b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  // Add the same candidate again.  It should be ignored.
215b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
216b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
217b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org
218b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  // Create a new candidate, identical except that the ufrag and pwd are now
219b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  // populated.
220b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  candidate_.set_username(kCandidateUfragVoice);
221b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  candidate_.set_password(kCandidatePwdVoice);
222b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
223b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org
224b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  // This should also be identified as redundant and ignored.
225b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
226b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
227b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org}
228b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org
2290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Test that we can serialize a JsepSessionDescription and deserialize it again.
2300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
2310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string sdp = Serialize(jsep_desc_.get());
2320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(DeSerialize(sdp));
2340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
2350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
2370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(sdp, parsed_sdp);
2380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Tests that we can serialize and deserialize a JsepSesssionDescription
2410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// with candidates.
2420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
2430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string sdp = Serialize(jsep_desc_.get());
2440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Add a candidate and check that the serialized result is different.
2460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  JsepIceCandidate jsep_candidate("audio", 0, candidate_);
2470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
2480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string sdp_with_candidate = Serialize(jsep_desc_.get());
2490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_NE(sdp, sdp_with_candidate);
2500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(
2520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      DeSerialize(sdp_with_candidate));
2530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
2540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
2560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
257