blob: 3d8751373116c42bcdcf8fece530800719e6d880 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <string>
12
Henrik Kjellander15583c12016-02-10 10:53:12 +010013#include "webrtc/api/jsepicecandidate.h"
14#include "webrtc/api/jsepsessiondescription.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000015#include "webrtc/base/gunit.h"
16#include "webrtc/base/helpers.h"
17#include "webrtc/base/scoped_ptr.h"
18#include "webrtc/base/ssladapter.h"
19#include "webrtc/base/stringencode.h"
kjellandera96e2d72016-02-04 23:52:28 -080020#include "webrtc/p2p/base/candidate.h"
kjellanderf4752772016-03-02 05:42:30 -080021#include "webrtc/p2p/base/p2pconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080022#include "webrtc/p2p/base/sessiondescription.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010023#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
25using webrtc::IceCandidateCollection;
26using webrtc::IceCandidateInterface;
27using webrtc::JsepIceCandidate;
28using webrtc::JsepSessionDescription;
29using webrtc::SessionDescriptionInterface;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030using rtc::scoped_ptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
32static const char kCandidateUfrag[] = "ufrag";
33static const char kCandidatePwd[] = "pwd";
34static const char kCandidateUfragVoice[] = "ufrag_voice";
35static const char kCandidatePwdVoice[] = "pwd_voice";
36static const char kCandidateUfragVideo[] = "ufrag_video";
37static const char kCandidatePwdVideo[] = "pwd_video";
38
39// This creates a session description with both audio and video media contents.
40// In SDP this is described by two m lines, one audio and one video.
41static cricket::SessionDescription* CreateCricketSessionDescription() {
42 cricket::SessionDescription* desc(new cricket::SessionDescription());
43 // AudioContentDescription
44 scoped_ptr<cricket::AudioContentDescription> audio(
45 new cricket::AudioContentDescription());
46
47 // VideoContentDescription
48 scoped_ptr<cricket::VideoContentDescription> video(
49 new cricket::VideoContentDescription());
50
51 audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0, 0));
52 desc->AddContent(cricket::CN_AUDIO, cricket::NS_JINGLE_RTP,
53 audio.release());
54
55 video->AddCodec(cricket::VideoCodec(120, "VP8", 640, 480, 30, 0));
56 desc->AddContent(cricket::CN_VIDEO, cricket::NS_JINGLE_RTP,
57 video.release());
58
deadbeef46eed762016-01-28 13:24:37 -080059 EXPECT_TRUE(desc->AddTransportInfo(cricket::TransportInfo(
60 cricket::CN_AUDIO,
61 cricket::TransportDescription(
62 std::vector<std::string>(), kCandidateUfragVoice, kCandidatePwdVoice,
63 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL))));
64 EXPECT_TRUE(desc->AddTransportInfo(cricket::TransportInfo(
65 cricket::CN_VIDEO,
66 cricket::TransportDescription(
67 std::vector<std::string>(), kCandidateUfragVideo, kCandidatePwdVideo,
68 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 return desc;
70}
71
72class JsepSessionDescriptionTest : public testing::Test {
73 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 virtual void SetUp() {
75 int port = 1234;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076 rtc::SocketAddress address("127.0.0.1", port++);
guoweis@webrtc.org61c12472015-01-15 06:53:07 +000077 cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
78 address, 1, "", "", "local", 0, "1");
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 candidate_ = candidate;
80 const std::string session_id =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000081 rtc::ToString(rtc::CreateRandomId64());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 const std::string session_version =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000083 rtc::ToString(rtc::CreateRandomId());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 jsep_desc_.reset(new JsepSessionDescription("dummy"));
85 ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(),
86 session_id, session_version));
87 }
88
89 std::string Serialize(const SessionDescriptionInterface* desc) {
90 std::string sdp;
91 EXPECT_TRUE(desc->ToString(&sdp));
92 EXPECT_FALSE(sdp.empty());
93 return sdp;
94 }
95
96 SessionDescriptionInterface* DeSerialize(const std::string& sdp) {
97 JsepSessionDescription* desc(new JsepSessionDescription("dummy"));
98 EXPECT_TRUE(desc->Initialize(sdp, NULL));
99 return desc;
100 }
101
102 cricket::Candidate candidate_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103 rtc::scoped_ptr<JsepSessionDescription> jsep_desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104};
105
106// Test that number_of_mediasections() returns the number of media contents in
107// a session description.
108TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
109 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
110}
111
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700112// Test that we can add a candidate to a session description without MID.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
114 JsepIceCandidate jsep_candidate("", 0, candidate_);
115 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
116 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
117 ASSERT_TRUE(ice_candidates != NULL);
118 EXPECT_EQ(1u, ice_candidates->count());
119 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
120 ASSERT_TRUE(ice_candidate != NULL);
121 candidate_.set_username(kCandidateUfragVoice);
122 candidate_.set_password(kCandidatePwdVoice);
123 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
124 EXPECT_EQ(0, ice_candidate->sdp_mline_index());
125 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
126}
127
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700128// Test that we can add and remove candidates to a session description with
129// MID. Removing candidates requires MID (transport_name).
130TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 // mid and m-line index don't match, in this case mid is preferred.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700132 std::string mid = "video";
133 JsepIceCandidate jsep_candidate(mid, 0, candidate_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
135 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
136 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
137 ASSERT_TRUE(ice_candidates != NULL);
138 EXPECT_EQ(1u, ice_candidates->count());
139 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
140 ASSERT_TRUE(ice_candidate != NULL);
141 candidate_.set_username(kCandidateUfragVideo);
142 candidate_.set_password(kCandidatePwdVideo);
143 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
144 // The mline index should have been updated according to mid.
145 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700146
147 std::vector<cricket::Candidate> candidates(1, candidate_);
148 candidates[0].set_transport_name(mid);
149 EXPECT_EQ(1u, jsep_desc_->RemoveCandidates(candidates));
150 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
151 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152}
153
154TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
155 candidate_.set_username(kCandidateUfrag);
156 candidate_.set_password(kCandidatePwd);
157 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
158 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
159 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
160 ASSERT_TRUE(ice_candidates != NULL);
161 EXPECT_EQ(1u, ice_candidates->count());
162 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
163 ASSERT_TRUE(ice_candidate != NULL);
164 candidate_.set_username(kCandidateUfrag);
165 candidate_.set_password(kCandidatePwd);
166 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
167
168 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
169}
170
171// Test that we can not add a candidate if there is no corresponding media
172// content in the session description.
173TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
174 JsepIceCandidate bad_candidate1("", 55, candidate_);
175 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
176
177 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
178 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
179}
180
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000181// Tests that repeatedly adding the same candidate, with or without credentials,
182// does not increase the number of candidates in the description.
183TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
184 JsepIceCandidate jsep_candidate("", 0, candidate_);
185 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
186 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
187
188 // Add the same candidate again. It should be ignored.
189 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
190 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
191
192 // Create a new candidate, identical except that the ufrag and pwd are now
193 // populated.
194 candidate_.set_username(kCandidateUfragVoice);
195 candidate_.set_password(kCandidatePwdVoice);
196 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
197
198 // This should also be identified as redundant and ignored.
199 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
200 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
201}
202
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203// Test that we can serialize a JsepSessionDescription and deserialize it again.
204TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
205 std::string sdp = Serialize(jsep_desc_.get());
206
207 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(DeSerialize(sdp));
208 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
209
210 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
211 EXPECT_EQ(sdp, parsed_sdp);
212}
213
214// Tests that we can serialize and deserialize a JsepSesssionDescription
215// with candidates.
216TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
217 std::string sdp = Serialize(jsep_desc_.get());
218
219 // Add a candidate and check that the serialized result is different.
220 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
221 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
222 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
223 EXPECT_NE(sdp, sdp_with_candidate);
224
225 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(
226 DeSerialize(sdp_with_candidate));
227 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
228
229 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
230}