• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
32 #include "webrtc/p2p/base/candidate.h"
33 #include "webrtc/p2p/base/constants.h"
34 #include "webrtc/p2p/base/sessiondescription.h"
35 #include "talk/session/media/mediasession.h"
36 #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"
41 
42 using webrtc::IceCandidateCollection;
43 using webrtc::IceCandidateInterface;
44 using webrtc::JsepIceCandidate;
45 using webrtc::JsepSessionDescription;
46 using webrtc::SessionDescriptionInterface;
47 using rtc::scoped_ptr;
48 
49 static const char kCandidateUfrag[] = "ufrag";
50 static const char kCandidatePwd[] = "pwd";
51 static const char kCandidateUfragVoice[] = "ufrag_voice";
52 static const char kCandidatePwdVoice[] = "pwd_voice";
53 static const char kCandidateUfragVideo[] = "ufrag_video";
54 static 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.
CreateCricketSessionDescription()58 static 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                                  std::vector<std::string>(),
81                                  kCandidateUfragVoice, kCandidatePwdVoice,
82                                  cricket::ICEMODE_FULL,
83                                  cricket::CONNECTIONROLE_NONE,
84                                  NULL, cricket::Candidates()))));
85   EXPECT_TRUE(desc->AddTransportInfo(
86       cricket::TransportInfo(cricket::CN_VIDEO,
87                              cricket::TransportDescription(
88                                  std::vector<std::string>(),
89                                  kCandidateUfragVideo, kCandidatePwdVideo,
90                                  cricket::ICEMODE_FULL,
91                                  cricket::CONNECTIONROLE_NONE,
92                                  NULL, cricket::Candidates()))));
93   return desc;
94 }
95 
96 class JsepSessionDescriptionTest : public testing::Test {
97  protected:
SetUp()98   virtual void SetUp() {
99     int port = 1234;
100     rtc::SocketAddress address("127.0.0.1", port++);
101     cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
102                                  address, 1, "", "", "local", 0, "1");
103     candidate_ = candidate;
104     const std::string session_id =
105         rtc::ToString(rtc::CreateRandomId64());
106     const std::string session_version =
107         rtc::ToString(rtc::CreateRandomId());
108     jsep_desc_.reset(new JsepSessionDescription("dummy"));
109     ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(),
110         session_id, session_version));
111   }
112 
Serialize(const SessionDescriptionInterface * desc)113   std::string Serialize(const SessionDescriptionInterface* desc) {
114     std::string sdp;
115     EXPECT_TRUE(desc->ToString(&sdp));
116     EXPECT_FALSE(sdp.empty());
117     return sdp;
118   }
119 
DeSerialize(const std::string & sdp)120   SessionDescriptionInterface* DeSerialize(const std::string& sdp) {
121     JsepSessionDescription* desc(new JsepSessionDescription("dummy"));
122     EXPECT_TRUE(desc->Initialize(sdp, NULL));
123     return desc;
124   }
125 
126   cricket::Candidate candidate_;
127   rtc::scoped_ptr<JsepSessionDescription> jsep_desc_;
128 };
129 
130 // Test that number_of_mediasections() returns the number of media contents in
131 // a session description.
TEST_F(JsepSessionDescriptionTest,CheckSessionDescription)132 TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
133   EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
134 }
135 
136 // Test that we can add a candidate to a session description.
TEST_F(JsepSessionDescriptionTest,AddCandidateWithoutMid)137 TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
138   JsepIceCandidate jsep_candidate("", 0, candidate_);
139   EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
140   const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
141   ASSERT_TRUE(ice_candidates != NULL);
142   EXPECT_EQ(1u, ice_candidates->count());
143   const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
144   ASSERT_TRUE(ice_candidate != NULL);
145   candidate_.set_username(kCandidateUfragVoice);
146   candidate_.set_password(kCandidatePwdVoice);
147   EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
148   EXPECT_EQ(0, ice_candidate->sdp_mline_index());
149   EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
150 }
151 
TEST_F(JsepSessionDescriptionTest,AddCandidateWithMid)152 TEST_F(JsepSessionDescriptionTest, AddCandidateWithMid) {
153   // mid and m-line index don't match, in this case mid is preferred.
154   JsepIceCandidate jsep_candidate("video", 0, candidate_);
155   EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
156   EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
157   const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
158   ASSERT_TRUE(ice_candidates != NULL);
159   EXPECT_EQ(1u, ice_candidates->count());
160   const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
161   ASSERT_TRUE(ice_candidate != NULL);
162   candidate_.set_username(kCandidateUfragVideo);
163   candidate_.set_password(kCandidatePwdVideo);
164   EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
165   // The mline index should have been updated according to mid.
166   EXPECT_EQ(1, ice_candidate->sdp_mline_index());
167 }
168 
TEST_F(JsepSessionDescriptionTest,AddCandidateAlreadyHasUfrag)169 TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
170   candidate_.set_username(kCandidateUfrag);
171   candidate_.set_password(kCandidatePwd);
172   JsepIceCandidate jsep_candidate("audio", 0, candidate_);
173   EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
174   const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
175   ASSERT_TRUE(ice_candidates != NULL);
176   EXPECT_EQ(1u, ice_candidates->count());
177   const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
178   ASSERT_TRUE(ice_candidate != NULL);
179   candidate_.set_username(kCandidateUfrag);
180   candidate_.set_password(kCandidatePwd);
181   EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
182 
183   EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
184 }
185 
186 // Test that we can not add a candidate if there is no corresponding media
187 // content in the session description.
TEST_F(JsepSessionDescriptionTest,AddBadCandidate)188 TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
189   JsepIceCandidate bad_candidate1("", 55, candidate_);
190   EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
191 
192   JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
193   EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
194 }
195 
196 // Tests that repeatedly adding the same candidate, with or without credentials,
197 // does not increase the number of candidates in the description.
TEST_F(JsepSessionDescriptionTest,AddCandidateDuplicates)198 TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
199   JsepIceCandidate jsep_candidate("", 0, candidate_);
200   EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
201   EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
202 
203   // Add the same candidate again.  It should be ignored.
204   EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
205   EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
206 
207   // Create a new candidate, identical except that the ufrag and pwd are now
208   // populated.
209   candidate_.set_username(kCandidateUfragVoice);
210   candidate_.set_password(kCandidatePwdVoice);
211   JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
212 
213   // This should also be identified as redundant and ignored.
214   EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
215   EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
216 }
217 
218 // Test that we can serialize a JsepSessionDescription and deserialize it again.
TEST_F(JsepSessionDescriptionTest,SerializeDeserialize)219 TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
220   std::string sdp = Serialize(jsep_desc_.get());
221 
222   scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(DeSerialize(sdp));
223   EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
224 
225   std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
226   EXPECT_EQ(sdp, parsed_sdp);
227 }
228 
229 // Tests that we can serialize and deserialize a JsepSesssionDescription
230 // with candidates.
TEST_F(JsepSessionDescriptionTest,SerializeDeserializeWithCandidates)231 TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
232   std::string sdp = Serialize(jsep_desc_.get());
233 
234   // Add a candidate and check that the serialized result is different.
235   JsepIceCandidate jsep_candidate("audio", 0, candidate_);
236   EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
237   std::string sdp_with_candidate = Serialize(jsep_desc_.get());
238   EXPECT_NE(sdp, sdp_with_candidate);
239 
240   scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(
241       DeSerialize(sdp_with_candidate));
242   std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
243 
244   EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
245 }
246