blob: 530c60bc064e74a8485b7afd9f4a51ca5dbce0ad [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <string>
29
30#include "talk/app/webrtc/jsepicecandidate.h"
31#include "talk/app/webrtc/jsepsessiondescription.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032#include "webrtc/p2p/base/candidate.h"
33#include "webrtc/p2p/base/constants.h"
34#include "webrtc/p2p/base/sessiondescription.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000035#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/gunit.h"
37#include "webrtc/base/helpers.h"
38#include "webrtc/base/scoped_ptr.h"
39#include "webrtc/base/ssladapter.h"
40#include "webrtc/base/stringencode.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42using webrtc::IceCandidateCollection;
43using webrtc::IceCandidateInterface;
44using webrtc::JsepIceCandidate;
45using webrtc::JsepSessionDescription;
46using webrtc::SessionDescriptionInterface;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000047using rtc::scoped_ptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49static const char kCandidateUfrag[] = "ufrag";
50static const char kCandidatePwd[] = "pwd";
51static const char kCandidateUfragVoice[] = "ufrag_voice";
52static const char kCandidatePwdVoice[] = "pwd_voice";
53static const char kCandidateUfragVideo[] = "ufrag_video";
54static const char kCandidatePwdVideo[] = "pwd_video";
55
56// This creates a session description with both audio and video media contents.
57// In SDP this is described by two m lines, one audio and one video.
58static cricket::SessionDescription* CreateCricketSessionDescription() {
59 cricket::SessionDescription* desc(new cricket::SessionDescription());
60 // AudioContentDescription
61 scoped_ptr<cricket::AudioContentDescription> audio(
62 new cricket::AudioContentDescription());
63
64 // VideoContentDescription
65 scoped_ptr<cricket::VideoContentDescription> video(
66 new cricket::VideoContentDescription());
67
68 audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0, 0));
69 desc->AddContent(cricket::CN_AUDIO, cricket::NS_JINGLE_RTP,
70 audio.release());
71
72 video->AddCodec(cricket::VideoCodec(120, "VP8", 640, 480, 30, 0));
73 desc->AddContent(cricket::CN_VIDEO, cricket::NS_JINGLE_RTP,
74 video.release());
75
76 EXPECT_TRUE(desc->AddTransportInfo(
77 cricket::TransportInfo(
78 cricket::CN_AUDIO,
79 cricket::TransportDescription(
80 cricket::NS_GINGLE_P2P,
81 std::vector<std::string>(),
82 kCandidateUfragVoice, kCandidatePwdVoice,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +000083 cricket::ICEMODE_FULL,
84 cricket::CONNECTIONROLE_NONE,
85 NULL, cricket::Candidates()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 EXPECT_TRUE(desc->AddTransportInfo(
87 cricket::TransportInfo(cricket::CN_VIDEO,
88 cricket::TransportDescription(
89 cricket::NS_GINGLE_P2P,
90 std::vector<std::string>(),
91 kCandidateUfragVideo, kCandidatePwdVideo,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +000092 cricket::ICEMODE_FULL,
93 cricket::CONNECTIONROLE_NONE,
94 NULL, cricket::Candidates()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 return desc;
96}
97
98class JsepSessionDescriptionTest : public testing::Test {
99 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 virtual void SetUp() {
101 int port = 1234;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000102 rtc::SocketAddress address("127.0.0.1", port++);
guoweis@webrtc.org61c12472015-01-15 06:53:07 +0000103 cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
104 address, 1, "", "", "local", 0, "1");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 candidate_ = candidate;
106 const std::string session_id =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000107 rtc::ToString(rtc::CreateRandomId64());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 const std::string session_version =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000109 rtc::ToString(rtc::CreateRandomId());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 jsep_desc_.reset(new JsepSessionDescription("dummy"));
111 ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(),
112 session_id, session_version));
113 }
114
115 std::string Serialize(const SessionDescriptionInterface* desc) {
116 std::string sdp;
117 EXPECT_TRUE(desc->ToString(&sdp));
118 EXPECT_FALSE(sdp.empty());
119 return sdp;
120 }
121
122 SessionDescriptionInterface* DeSerialize(const std::string& sdp) {
123 JsepSessionDescription* desc(new JsepSessionDescription("dummy"));
124 EXPECT_TRUE(desc->Initialize(sdp, NULL));
125 return desc;
126 }
127
128 cricket::Candidate candidate_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000129 rtc::scoped_ptr<JsepSessionDescription> jsep_desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130};
131
132// Test that number_of_mediasections() returns the number of media contents in
133// a session description.
134TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
135 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
136}
137
138// Test that we can add a candidate to a session description.
139TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
140 JsepIceCandidate jsep_candidate("", 0, candidate_);
141 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
142 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
143 ASSERT_TRUE(ice_candidates != NULL);
144 EXPECT_EQ(1u, ice_candidates->count());
145 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
146 ASSERT_TRUE(ice_candidate != NULL);
147 candidate_.set_username(kCandidateUfragVoice);
148 candidate_.set_password(kCandidatePwdVoice);
149 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
150 EXPECT_EQ(0, ice_candidate->sdp_mline_index());
151 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
152}
153
154TEST_F(JsepSessionDescriptionTest, AddCandidateWithMid) {
155 // mid and m-line index don't match, in this case mid is preferred.
156 JsepIceCandidate jsep_candidate("video", 0, candidate_);
157 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
158 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
159 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
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(kCandidateUfragVideo);
165 candidate_.set_password(kCandidatePwdVideo);
166 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
167 // The mline index should have been updated according to mid.
168 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
169}
170
171TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
172 candidate_.set_username(kCandidateUfrag);
173 candidate_.set_password(kCandidatePwd);
174 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
175 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
176 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
177 ASSERT_TRUE(ice_candidates != NULL);
178 EXPECT_EQ(1u, ice_candidates->count());
179 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
180 ASSERT_TRUE(ice_candidate != NULL);
181 candidate_.set_username(kCandidateUfrag);
182 candidate_.set_password(kCandidatePwd);
183 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
184
185 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
186}
187
188// Test that we can not add a candidate if there is no corresponding media
189// content in the session description.
190TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
191 JsepIceCandidate bad_candidate1("", 55, candidate_);
192 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
193
194 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
195 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
196}
197
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000198// Tests that repeatedly adding the same candidate, with or without credentials,
199// does not increase the number of candidates in the description.
200TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
201 JsepIceCandidate jsep_candidate("", 0, candidate_);
202 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
203 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
204
205 // Add the same candidate again. It should be ignored.
206 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
207 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
208
209 // Create a new candidate, identical except that the ufrag and pwd are now
210 // populated.
211 candidate_.set_username(kCandidateUfragVoice);
212 candidate_.set_password(kCandidatePwdVoice);
213 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
214
215 // This should also be identified as redundant and ignored.
216 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
217 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
218}
219
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220// Test that we can serialize a JsepSessionDescription and deserialize it again.
221TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
222 std::string sdp = Serialize(jsep_desc_.get());
223
224 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(DeSerialize(sdp));
225 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
226
227 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
228 EXPECT_EQ(sdp, parsed_sdp);
229}
230
231// Tests that we can serialize and deserialize a JsepSesssionDescription
232// with candidates.
233TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
234 std::string sdp = Serialize(jsep_desc_.get());
235
236 // Add a candidate and check that the serialized result is different.
237 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
238 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
239 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
240 EXPECT_NE(sdp, sdp_with_candidate);
241
242 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(
243 DeSerialize(sdp_with_candidate));
244 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
245
246 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
247}