henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <string> |
| 12 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 13 | #include "webrtc/api/jsepicecandidate.h" |
| 14 | #include "webrtc/api/jsepsessiondescription.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 15 | #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" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 20 | #include "webrtc/p2p/base/candidate.h" |
| 21 | #include "webrtc/p2p/base/constants.h" |
| 22 | #include "webrtc/p2p/base/sessiondescription.h" |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 06:47:59 +0100 | [diff] [blame^] | 23 | #include "webrtc/pc/mediasession.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 24 | |
| 25 | using webrtc::IceCandidateCollection; |
| 26 | using webrtc::IceCandidateInterface; |
| 27 | using webrtc::JsepIceCandidate; |
| 28 | using webrtc::JsepSessionDescription; |
| 29 | using webrtc::SessionDescriptionInterface; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 30 | using rtc::scoped_ptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 31 | |
| 32 | static const char kCandidateUfrag[] = "ufrag"; |
| 33 | static const char kCandidatePwd[] = "pwd"; |
| 34 | static const char kCandidateUfragVoice[] = "ufrag_voice"; |
| 35 | static const char kCandidatePwdVoice[] = "pwd_voice"; |
| 36 | static const char kCandidateUfragVideo[] = "ufrag_video"; |
| 37 | static 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. |
| 41 | static 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 | |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 59 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | return desc; |
| 70 | } |
| 71 | |
| 72 | class JsepSessionDescriptionTest : public testing::Test { |
| 73 | protected: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | virtual void SetUp() { |
| 75 | int port = 1234; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 76 | rtc::SocketAddress address("127.0.0.1", port++); |
guoweis@webrtc.org | 61c1247 | 2015-01-15 06:53:07 +0000 | [diff] [blame] | 77 | cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 78 | address, 1, "", "", "local", 0, "1"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | candidate_ = candidate; |
| 80 | const std::string session_id = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 81 | rtc::ToString(rtc::CreateRandomId64()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | const std::string session_version = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 83 | rtc::ToString(rtc::CreateRandomId()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 103 | rtc::scoped_ptr<JsepSessionDescription> jsep_desc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | // Test that number_of_mediasections() returns the number of media contents in |
| 107 | // a session description. |
| 108 | TEST_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. |
| 113 | TEST_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 | |
| 128 | TEST_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 | |
| 145 | TEST_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. |
| 164 | TEST_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.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 172 | // Tests that repeatedly adding the same candidate, with or without credentials, |
| 173 | // does not increase the number of candidates in the description. |
| 174 | TEST_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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 194 | // Test that we can serialize a JsepSessionDescription and deserialize it again. |
| 195 | TEST_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. |
| 207 | TEST_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 | } |