blob: cf992c07fe8ca4865ba709a0a806baedd06bfdcc [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++);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 cricket::Candidate candidate("rtp", cricket::ICE_CANDIDATE_COMPONENT_RTP,
104 "udp", address, 1, "",
105 "", "local", "eth0", 0, "1");
106 candidate_ = candidate;
107 const std::string session_id =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000108 rtc::ToString(rtc::CreateRandomId64());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 const std::string session_version =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000110 rtc::ToString(rtc::CreateRandomId());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 jsep_desc_.reset(new JsepSessionDescription("dummy"));
112 ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(),
113 session_id, session_version));
114 }
115
116 std::string Serialize(const SessionDescriptionInterface* desc) {
117 std::string sdp;
118 EXPECT_TRUE(desc->ToString(&sdp));
119 EXPECT_FALSE(sdp.empty());
120 return sdp;
121 }
122
123 SessionDescriptionInterface* DeSerialize(const std::string& sdp) {
124 JsepSessionDescription* desc(new JsepSessionDescription("dummy"));
125 EXPECT_TRUE(desc->Initialize(sdp, NULL));
126 return desc;
127 }
128
129 cricket::Candidate candidate_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000130 rtc::scoped_ptr<JsepSessionDescription> jsep_desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131};
132
133// Test that number_of_mediasections() returns the number of media contents in
134// a session description.
135TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
136 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
137}
138
139// Test that we can add a candidate to a session description.
140TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
141 JsepIceCandidate jsep_candidate("", 0, candidate_);
142 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
143 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
144 ASSERT_TRUE(ice_candidates != NULL);
145 EXPECT_EQ(1u, ice_candidates->count());
146 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
147 ASSERT_TRUE(ice_candidate != NULL);
148 candidate_.set_username(kCandidateUfragVoice);
149 candidate_.set_password(kCandidatePwdVoice);
150 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
151 EXPECT_EQ(0, ice_candidate->sdp_mline_index());
152 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
153}
154
155TEST_F(JsepSessionDescriptionTest, AddCandidateWithMid) {
156 // mid and m-line index don't match, in this case mid is preferred.
157 JsepIceCandidate jsep_candidate("video", 0, candidate_);
158 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
159 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
160 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
161 ASSERT_TRUE(ice_candidates != NULL);
162 EXPECT_EQ(1u, ice_candidates->count());
163 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
164 ASSERT_TRUE(ice_candidate != NULL);
165 candidate_.set_username(kCandidateUfragVideo);
166 candidate_.set_password(kCandidatePwdVideo);
167 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
168 // The mline index should have been updated according to mid.
169 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
170}
171
172TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
173 candidate_.set_username(kCandidateUfrag);
174 candidate_.set_password(kCandidatePwd);
175 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
176 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
177 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
178 ASSERT_TRUE(ice_candidates != NULL);
179 EXPECT_EQ(1u, ice_candidates->count());
180 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
181 ASSERT_TRUE(ice_candidate != NULL);
182 candidate_.set_username(kCandidateUfrag);
183 candidate_.set_password(kCandidatePwd);
184 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
185
186 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
187}
188
189// Test that we can not add a candidate if there is no corresponding media
190// content in the session description.
191TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
192 JsepIceCandidate bad_candidate1("", 55, candidate_);
193 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
194
195 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
196 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
197}
198
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000199// Tests that repeatedly adding the same candidate, with or without credentials,
200// does not increase the number of candidates in the description.
201TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
202 JsepIceCandidate jsep_candidate("", 0, candidate_);
203 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
204 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
205
206 // Add the same candidate again. It should be ignored.
207 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
208 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
209
210 // Create a new candidate, identical except that the ufrag and pwd are now
211 // populated.
212 candidate_.set_username(kCandidateUfragVoice);
213 candidate_.set_password(kCandidatePwdVoice);
214 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
215
216 // This should also be identified as redundant and ignored.
217 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
218 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
219}
220
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221// Test that we can serialize a JsepSessionDescription and deserialize it again.
222TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
223 std::string sdp = Serialize(jsep_desc_.get());
224
225 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(DeSerialize(sdp));
226 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
227
228 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
229 EXPECT_EQ(sdp, parsed_sdp);
230}
231
232// Tests that we can serialize and deserialize a JsepSesssionDescription
233// with candidates.
234TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
235 std::string sdp = Serialize(jsep_desc_.get());
236
237 // Add a candidate and check that the serialized result is different.
238 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
239 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
240 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
241 EXPECT_NE(sdp, sdp_with_candidate);
242
243 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(
244 DeSerialize(sdp_with_candidate));
245 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
246
247 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
248}