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/base/gunit.h" 33 #include "talk/base/helpers.h" 34 #include "talk/base/scoped_ptr.h" 35 #include "talk/base/stringencode.h" 36 #include "talk/p2p/base/candidate.h" 37 #include "talk/p2p/base/constants.h" 38 #include "talk/p2p/base/sessiondescription.h" 39 #include "talk/session/media/mediasession.h" 40 41 using webrtc::IceCandidateCollection; 42 using webrtc::IceCandidateInterface; 43 using webrtc::JsepIceCandidate; 44 using webrtc::JsepSessionDescription; 45 using webrtc::SessionDescriptionInterface; 46 using talk_base::scoped_ptr; 47 48 static const char kCandidateUfrag[] = "ufrag"; 49 static const char kCandidatePwd[] = "pwd"; 50 static const char kCandidateUfragVoice[] = "ufrag_voice"; 51 static const char kCandidatePwdVoice[] = "pwd_voice"; 52 static const char kCandidateUfragVideo[] = "ufrag_video"; 53 static const char kCandidatePwdVideo[] = "pwd_video"; 54 55 // This creates a session description with both audio and video media contents. 56 // In SDP this is described by two m lines, one audio and one video. 57 static cricket::SessionDescription* CreateCricketSessionDescription() { 58 cricket::SessionDescription* desc(new cricket::SessionDescription()); 59 // AudioContentDescription 60 scoped_ptr<cricket::AudioContentDescription> audio( 61 new cricket::AudioContentDescription()); 62 63 // VideoContentDescription 64 scoped_ptr<cricket::VideoContentDescription> video( 65 new cricket::VideoContentDescription()); 66 67 audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0, 0)); 68 desc->AddContent(cricket::CN_AUDIO, cricket::NS_JINGLE_RTP, 69 audio.release()); 70 71 video->AddCodec(cricket::VideoCodec(120, "VP8", 640, 480, 30, 0)); 72 desc->AddContent(cricket::CN_VIDEO, cricket::NS_JINGLE_RTP, 73 video.release()); 74 75 EXPECT_TRUE(desc->AddTransportInfo( 76 cricket::TransportInfo( 77 cricket::CN_AUDIO, 78 cricket::TransportDescription( 79 cricket::NS_GINGLE_P2P, 80 std::vector<std::string>(), 81 kCandidateUfragVoice, kCandidatePwdVoice, 82 cricket::ICEMODE_FULL, NULL, 83 cricket::Candidates())))); 84 EXPECT_TRUE(desc->AddTransportInfo( 85 cricket::TransportInfo(cricket::CN_VIDEO, 86 cricket::TransportDescription( 87 cricket::NS_GINGLE_P2P, 88 std::vector<std::string>(), 89 kCandidateUfragVideo, kCandidatePwdVideo, 90 cricket::ICEMODE_FULL, NULL, 91 cricket::Candidates())))); 92 return desc; 93 } 94 95 class JsepSessionDescriptionTest : public testing::Test { 96 protected: 97 virtual void SetUp() { 98 int port = 1234; 99 talk_base::SocketAddress address("127.0.0.1", port++); 100 cricket::Candidate candidate("rtp", cricket::ICE_CANDIDATE_COMPONENT_RTP, 101 "udp", address, 1, "", 102 "", "local", "eth0", 0, "1"); 103 candidate_ = candidate; 104 const std::string session_id = 105 talk_base::ToString(talk_base::CreateRandomId64()); 106 const std::string session_version = 107 talk_base::ToString(talk_base::CreateRandomId()); 108 jsep_desc_.reset(new JsepSessionDescription("dummy")); 109 ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(), 110 session_id, session_version)); 111 } 112 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 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 talk_base::scoped_ptr<JsepSessionDescription> jsep_desc_; 128 }; 129 130 // Test that number_of_mediasections() returns the number of media contents in 131 // a session description. 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. 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 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 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. 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 // Test that we can serialize a JsepSessionDescription and deserialize it again. 197 TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) { 198 std::string sdp = Serialize(jsep_desc_.get()); 199 200 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(DeSerialize(sdp)); 201 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections()); 202 203 std::string parsed_sdp = Serialize(parsed_jsep_desc.get()); 204 EXPECT_EQ(sdp, parsed_sdp); 205 } 206 207 // Tests that we can serialize and deserialize a JsepSesssionDescription 208 // with candidates. 209 TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) { 210 std::string sdp = Serialize(jsep_desc_.get()); 211 212 // Add a candidate and check that the serialized result is different. 213 JsepIceCandidate jsep_candidate("audio", 0, candidate_); 214 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); 215 std::string sdp_with_candidate = Serialize(jsep_desc_.get()); 216 EXPECT_NE(sdp, sdp_with_candidate); 217 218 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc( 219 DeSerialize(sdp_with_candidate)); 220 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get()); 221 222 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate); 223 } 224