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, 83 cricket::CONNECTIONROLE_NONE, 84 NULL, cricket::Candidates())))); 85 EXPECT_TRUE(desc->AddTransportInfo( 86 cricket::TransportInfo(cricket::CN_VIDEO, 87 cricket::TransportDescription( 88 cricket::NS_GINGLE_P2P, 89 std::vector<std::string>(), 90 kCandidateUfragVideo, kCandidatePwdVideo, 91 cricket::ICEMODE_FULL, 92 cricket::CONNECTIONROLE_NONE, 93 NULL, cricket::Candidates())))); 94 return desc; 95 } 96 97 class JsepSessionDescriptionTest : public testing::Test { 98 protected: 99 virtual void SetUp() { 100 int port = 1234; 101 talk_base::SocketAddress address("127.0.0.1", port++); 102 cricket::Candidate candidate("rtp", cricket::ICE_CANDIDATE_COMPONENT_RTP, 103 "udp", address, 1, "", 104 "", "local", "eth0", 0, "1"); 105 candidate_ = candidate; 106 const std::string session_id = 107 talk_base::ToString(talk_base::CreateRandomId64()); 108 const std::string session_version = 109 talk_base::ToString(talk_base::CreateRandomId()); 110 jsep_desc_.reset(new JsepSessionDescription("dummy")); 111 ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(), 112 session_id, session_version)); 113 } 114 115 std::string Serialize(const SessionDescriptionInterface* desc) { 116 std::string sdp; 117 EXPECT_TRUE(desc->ToString(&sdp)); 118 EXPECT_FALSE(sdp.empty()); 119 return sdp; 120 } 121 122 SessionDescriptionInterface* DeSerialize(const std::string& sdp) { 123 JsepSessionDescription* desc(new JsepSessionDescription("dummy")); 124 EXPECT_TRUE(desc->Initialize(sdp, NULL)); 125 return desc; 126 } 127 128 cricket::Candidate candidate_; 129 talk_base::scoped_ptr<JsepSessionDescription> jsep_desc_; 130 }; 131 132 // Test that number_of_mediasections() returns the number of media contents in 133 // a session description. 134 TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) { 135 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections()); 136 } 137 138 // Test that we can add a candidate to a session description. 139 TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) { 140 JsepIceCandidate jsep_candidate("", 0, candidate_); 141 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); 142 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0); 143 ASSERT_TRUE(ice_candidates != NULL); 144 EXPECT_EQ(1u, ice_candidates->count()); 145 const IceCandidateInterface* ice_candidate = ice_candidates->at(0); 146 ASSERT_TRUE(ice_candidate != NULL); 147 candidate_.set_username(kCandidateUfragVoice); 148 candidate_.set_password(kCandidatePwdVoice); 149 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_)); 150 EXPECT_EQ(0, ice_candidate->sdp_mline_index()); 151 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count()); 152 } 153 154 TEST_F(JsepSessionDescriptionTest, AddCandidateWithMid) { 155 // mid and m-line index don't match, in this case mid is preferred. 156 JsepIceCandidate jsep_candidate("video", 0, candidate_); 157 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); 158 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count()); 159 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1); 160 ASSERT_TRUE(ice_candidates != NULL); 161 EXPECT_EQ(1u, ice_candidates->count()); 162 const IceCandidateInterface* ice_candidate = ice_candidates->at(0); 163 ASSERT_TRUE(ice_candidate != NULL); 164 candidate_.set_username(kCandidateUfragVideo); 165 candidate_.set_password(kCandidatePwdVideo); 166 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_)); 167 // The mline index should have been updated according to mid. 168 EXPECT_EQ(1, ice_candidate->sdp_mline_index()); 169 } 170 171 TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) { 172 candidate_.set_username(kCandidateUfrag); 173 candidate_.set_password(kCandidatePwd); 174 JsepIceCandidate jsep_candidate("audio", 0, candidate_); 175 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); 176 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0); 177 ASSERT_TRUE(ice_candidates != NULL); 178 EXPECT_EQ(1u, ice_candidates->count()); 179 const IceCandidateInterface* ice_candidate = ice_candidates->at(0); 180 ASSERT_TRUE(ice_candidate != NULL); 181 candidate_.set_username(kCandidateUfrag); 182 candidate_.set_password(kCandidatePwd); 183 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_)); 184 185 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count()); 186 } 187 188 // Test that we can not add a candidate if there is no corresponding media 189 // content in the session description. 190 TEST_F(JsepSessionDescriptionTest, AddBadCandidate) { 191 JsepIceCandidate bad_candidate1("", 55, candidate_); 192 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1)); 193 194 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_); 195 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2)); 196 } 197 198 // Test that we can serialize a JsepSessionDescription and deserialize it again. 199 TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) { 200 std::string sdp = Serialize(jsep_desc_.get()); 201 202 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(DeSerialize(sdp)); 203 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections()); 204 205 std::string parsed_sdp = Serialize(parsed_jsep_desc.get()); 206 EXPECT_EQ(sdp, parsed_sdp); 207 } 208 209 // Tests that we can serialize and deserialize a JsepSesssionDescription 210 // with candidates. 211 TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) { 212 std::string sdp = Serialize(jsep_desc_.get()); 213 214 // Add a candidate and check that the serialized result is different. 215 JsepIceCandidate jsep_candidate("audio", 0, candidate_); 216 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); 217 std::string sdp_with_candidate = Serialize(jsep_desc_.get()); 218 EXPECT_NE(sdp, sdp_with_candidate); 219 220 scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc( 221 DeSerialize(sdp_with_candidate)); 222 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get()); 223 224 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate); 225 } 226