blob: 7e505a122c9cd623cff7feb3f2e0fa809608e2bf [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"
21#include "webrtc/p2p/base/constants.h"
22#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
112// Test that we can add a candidate to a session description.
113TEST_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
128TEST_F(JsepSessionDescriptionTest, AddCandidateWithMid) {
129 // mid and m-line index don't match, in this case mid is preferred.
130 JsepIceCandidate jsep_candidate("video", 0, candidate_);
131 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
132 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
133 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
134 ASSERT_TRUE(ice_candidates != NULL);
135 EXPECT_EQ(1u, ice_candidates->count());
136 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
137 ASSERT_TRUE(ice_candidate != NULL);
138 candidate_.set_username(kCandidateUfragVideo);
139 candidate_.set_password(kCandidatePwdVideo);
140 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
141 // The mline index should have been updated according to mid.
142 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
143}
144
145TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
146 candidate_.set_username(kCandidateUfrag);
147 candidate_.set_password(kCandidatePwd);
148 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
149 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
150 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
151 ASSERT_TRUE(ice_candidates != NULL);
152 EXPECT_EQ(1u, ice_candidates->count());
153 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
154 ASSERT_TRUE(ice_candidate != NULL);
155 candidate_.set_username(kCandidateUfrag);
156 candidate_.set_password(kCandidatePwd);
157 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
158
159 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
160}
161
162// Test that we can not add a candidate if there is no corresponding media
163// content in the session description.
164TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
165 JsepIceCandidate bad_candidate1("", 55, candidate_);
166 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
167
168 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
169 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
170}
171
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000172// Tests that repeatedly adding the same candidate, with or without credentials,
173// does not increase the number of candidates in the description.
174TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
175 JsepIceCandidate jsep_candidate("", 0, candidate_);
176 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
177 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
178
179 // Add the same candidate again. It should be ignored.
180 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
181 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
182
183 // Create a new candidate, identical except that the ufrag and pwd are now
184 // populated.
185 candidate_.set_username(kCandidateUfragVoice);
186 candidate_.set_password(kCandidatePwdVoice);
187 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
188
189 // This should also be identified as redundant and ignored.
190 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
191 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
192}
193
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194// Test that we can serialize a JsepSessionDescription and deserialize it again.
195TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
196 std::string sdp = Serialize(jsep_desc_.get());
197
198 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(DeSerialize(sdp));
199 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
200
201 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
202 EXPECT_EQ(sdp, parsed_sdp);
203}
204
205// Tests that we can serialize and deserialize a JsepSesssionDescription
206// with candidates.
207TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
208 std::string sdp = Serialize(jsep_desc_.get());
209
210 // Add a candidate and check that the serialized result is different.
211 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
212 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
213 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
214 EXPECT_NE(sdp, sdp_with_candidate);
215
216 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(
217 DeSerialize(sdp_with_candidate));
218 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
219
220 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
221}