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