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 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 11 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 14 | #include "api/candidate.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "api/jsepicecandidate.h" |
| 16 | #include "api/jsepsessiondescription.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "p2p/base/p2pconstants.h" |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 18 | #include "p2p/base/port.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "pc/mediasession.h" |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 20 | #include "pc/sessiondescription.h" |
Mirko Bonadei | 88bc9d5 | 2017-12-18 16:00:13 +0100 | [diff] [blame] | 21 | #include "pc/webrtcsdp.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/gunit.h" |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 23 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/stringencode.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 26 | using cricket::MediaProtocolType; |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 27 | using ::testing::Values; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 28 | using webrtc::IceCandidateCollection; |
| 29 | using webrtc::IceCandidateInterface; |
| 30 | using webrtc::JsepIceCandidate; |
| 31 | using webrtc::JsepSessionDescription; |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 32 | using webrtc::SdpType; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | using webrtc::SessionDescriptionInterface; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | |
| 35 | static const char kCandidateUfrag[] = "ufrag"; |
| 36 | static const char kCandidatePwd[] = "pwd"; |
| 37 | static const char kCandidateUfragVoice[] = "ufrag_voice"; |
| 38 | static const char kCandidatePwdVoice[] = "pwd_voice"; |
| 39 | static const char kCandidateUfragVideo[] = "ufrag_video"; |
| 40 | static const char kCandidatePwdVideo[] = "pwd_video"; |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 41 | static const char kCandidateFoundation[] = "a0+B/1"; |
| 42 | static const uint32_t kCandidatePriority = 2130706432U; // pref = 1.0 |
| 43 | static const uint32_t kCandidateGeneration = 2; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | |
| 45 | // This creates a session description with both audio and video media contents. |
| 46 | // In SDP this is described by two m lines, one audio and one video. |
| 47 | static cricket::SessionDescription* CreateCricketSessionDescription() { |
| 48 | cricket::SessionDescription* desc(new cricket::SessionDescription()); |
| 49 | // AudioContentDescription |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 50 | std::unique_ptr<cricket::AudioContentDescription> audio( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | new cricket::AudioContentDescription()); |
| 52 | |
| 53 | // VideoContentDescription |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 54 | std::unique_ptr<cricket::VideoContentDescription> video( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | new cricket::VideoContentDescription()); |
| 56 | |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 57 | audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0)); |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 58 | desc->AddContent(cricket::CN_AUDIO, MediaProtocolType::kRtp, audio.release()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | |
perkj | 2675274 | 2016-10-24 01:21:16 -0700 | [diff] [blame] | 60 | video->AddCodec(cricket::VideoCodec(120, "VP8")); |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 61 | desc->AddContent(cricket::CN_VIDEO, MediaProtocolType::kRtp, video.release()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 63 | EXPECT_TRUE(desc->AddTransportInfo(cricket::TransportInfo( |
| 64 | cricket::CN_AUDIO, |
| 65 | cricket::TransportDescription( |
| 66 | std::vector<std::string>(), kCandidateUfragVoice, kCandidatePwdVoice, |
| 67 | cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)))); |
| 68 | EXPECT_TRUE(desc->AddTransportInfo(cricket::TransportInfo( |
| 69 | cricket::CN_VIDEO, |
| 70 | cricket::TransportDescription( |
| 71 | std::vector<std::string>(), kCandidateUfragVideo, kCandidatePwdVideo, |
| 72 | cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | return desc; |
| 74 | } |
| 75 | |
| 76 | class JsepSessionDescriptionTest : public testing::Test { |
| 77 | protected: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | virtual void SetUp() { |
| 79 | int port = 1234; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 80 | rtc::SocketAddress address("127.0.0.1", port++); |
guoweis@webrtc.org | 61c1247 | 2015-01-15 06:53:07 +0000 | [diff] [blame] | 81 | cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 82 | address, 1, "", "", "local", 0, "1"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | candidate_ = candidate; |
| 84 | const std::string session_id = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 85 | rtc::ToString(rtc::CreateRandomId64()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 86 | const std::string session_version = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 87 | rtc::ToString(rtc::CreateRandomId()); |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 88 | jsep_desc_ = rtc::MakeUnique<JsepSessionDescription>(SdpType::kOffer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(), |
| 90 | session_id, session_version)); |
| 91 | } |
| 92 | |
| 93 | std::string Serialize(const SessionDescriptionInterface* desc) { |
| 94 | std::string sdp; |
| 95 | EXPECT_TRUE(desc->ToString(&sdp)); |
| 96 | EXPECT_FALSE(sdp.empty()); |
| 97 | return sdp; |
| 98 | } |
| 99 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 100 | std::unique_ptr<SessionDescriptionInterface> DeSerialize( |
| 101 | const std::string& sdp) { |
| 102 | auto jsep_desc = rtc::MakeUnique<JsepSessionDescription>(SdpType::kOffer); |
| 103 | EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jsep_desc.get(), nullptr)); |
| 104 | return std::move(jsep_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | cricket::Candidate candidate_; |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 108 | std::unique_ptr<JsepSessionDescription> jsep_desc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | // Test that number_of_mediasections() returns the number of media contents in |
| 112 | // a session description. |
| 113 | TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) { |
| 114 | EXPECT_EQ(2u, jsep_desc_->number_of_mediasections()); |
| 115 | } |
| 116 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 117 | // Test that we can add a candidate to a session description without MID. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) { |
| 119 | JsepIceCandidate jsep_candidate("", 0, candidate_); |
| 120 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 121 | const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0); |
| 122 | ASSERT_TRUE(ice_candidates != NULL); |
| 123 | EXPECT_EQ(1u, ice_candidates->count()); |
| 124 | const IceCandidateInterface* ice_candidate = ice_candidates->at(0); |
| 125 | ASSERT_TRUE(ice_candidate != NULL); |
| 126 | candidate_.set_username(kCandidateUfragVoice); |
| 127 | candidate_.set_password(kCandidatePwdVoice); |
| 128 | EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_)); |
| 129 | EXPECT_EQ(0, ice_candidate->sdp_mline_index()); |
| 130 | EXPECT_EQ(0u, jsep_desc_->candidates(1)->count()); |
| 131 | } |
| 132 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 133 | // Test that we can add and remove candidates to a session description with |
| 134 | // MID. Removing candidates requires MID (transport_name). |
| 135 | TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | // mid and m-line index don't match, in this case mid is preferred. |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 137 | std::string mid = "video"; |
| 138 | JsepIceCandidate jsep_candidate(mid, 0, candidate_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 140 | EXPECT_EQ(0u, jsep_desc_->candidates(0)->count()); |
| 141 | const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1); |
| 142 | ASSERT_TRUE(ice_candidates != NULL); |
| 143 | EXPECT_EQ(1u, ice_candidates->count()); |
| 144 | const IceCandidateInterface* ice_candidate = ice_candidates->at(0); |
| 145 | ASSERT_TRUE(ice_candidate != NULL); |
| 146 | candidate_.set_username(kCandidateUfragVideo); |
| 147 | candidate_.set_password(kCandidatePwdVideo); |
| 148 | EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_)); |
| 149 | // The mline index should have been updated according to mid. |
| 150 | EXPECT_EQ(1, ice_candidate->sdp_mline_index()); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 151 | |
| 152 | std::vector<cricket::Candidate> candidates(1, candidate_); |
| 153 | candidates[0].set_transport_name(mid); |
| 154 | EXPECT_EQ(1u, jsep_desc_->RemoveCandidates(candidates)); |
| 155 | EXPECT_EQ(0u, jsep_desc_->candidates(0)->count()); |
| 156 | EXPECT_EQ(0u, jsep_desc_->candidates(1)->count()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) { |
| 160 | candidate_.set_username(kCandidateUfrag); |
| 161 | candidate_.set_password(kCandidatePwd); |
| 162 | JsepIceCandidate jsep_candidate("audio", 0, candidate_); |
| 163 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 164 | const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0); |
| 165 | ASSERT_TRUE(ice_candidates != NULL); |
| 166 | EXPECT_EQ(1u, ice_candidates->count()); |
| 167 | const IceCandidateInterface* ice_candidate = ice_candidates->at(0); |
| 168 | ASSERT_TRUE(ice_candidate != NULL); |
| 169 | candidate_.set_username(kCandidateUfrag); |
| 170 | candidate_.set_password(kCandidatePwd); |
| 171 | EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_)); |
| 172 | |
| 173 | EXPECT_EQ(0u, jsep_desc_->candidates(1)->count()); |
| 174 | } |
| 175 | |
| 176 | // Test that we can not add a candidate if there is no corresponding media |
| 177 | // content in the session description. |
| 178 | TEST_F(JsepSessionDescriptionTest, AddBadCandidate) { |
| 179 | JsepIceCandidate bad_candidate1("", 55, candidate_); |
| 180 | EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1)); |
| 181 | |
| 182 | JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_); |
| 183 | EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2)); |
| 184 | } |
| 185 | |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 186 | // Tests that repeatedly adding the same candidate, with or without credentials, |
| 187 | // does not increase the number of candidates in the description. |
| 188 | TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) { |
| 189 | JsepIceCandidate jsep_candidate("", 0, candidate_); |
| 190 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 191 | EXPECT_EQ(1u, jsep_desc_->candidates(0)->count()); |
| 192 | |
| 193 | // Add the same candidate again. It should be ignored. |
| 194 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 195 | EXPECT_EQ(1u, jsep_desc_->candidates(0)->count()); |
| 196 | |
| 197 | // Create a new candidate, identical except that the ufrag and pwd are now |
| 198 | // populated. |
| 199 | candidate_.set_username(kCandidateUfragVoice); |
| 200 | candidate_.set_password(kCandidatePwdVoice); |
| 201 | JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_); |
| 202 | |
| 203 | // This should also be identified as redundant and ignored. |
| 204 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials)); |
| 205 | EXPECT_EQ(1u, jsep_desc_->candidates(0)->count()); |
| 206 | } |
| 207 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 208 | // Test that we can serialize a JsepSessionDescription and deserialize it again. |
| 209 | TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) { |
| 210 | std::string sdp = Serialize(jsep_desc_.get()); |
| 211 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 212 | auto parsed_jsep_desc = DeSerialize(sdp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 213 | EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections()); |
| 214 | |
| 215 | std::string parsed_sdp = Serialize(parsed_jsep_desc.get()); |
| 216 | EXPECT_EQ(sdp, parsed_sdp); |
| 217 | } |
| 218 | |
| 219 | // Tests that we can serialize and deserialize a JsepSesssionDescription |
| 220 | // with candidates. |
| 221 | TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) { |
| 222 | std::string sdp = Serialize(jsep_desc_.get()); |
| 223 | |
| 224 | // Add a candidate and check that the serialized result is different. |
| 225 | JsepIceCandidate jsep_candidate("audio", 0, candidate_); |
| 226 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 227 | std::string sdp_with_candidate = Serialize(jsep_desc_.get()); |
| 228 | EXPECT_NE(sdp, sdp_with_candidate); |
| 229 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 230 | auto parsed_jsep_desc = DeSerialize(sdp_with_candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 231 | std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get()); |
| 232 | |
| 233 | EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate); |
| 234 | } |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 235 | |
| 236 | // TODO(zhihuang): Modify these tests. These are used to verify that after |
| 237 | // adding the candidates, the connection_address field is set correctly. Modify |
| 238 | // those so that the "connection address" is tested directly. |
| 239 | // Tests serialization of SDP with only IPv6 candidates and verifies that IPv6 |
| 240 | // is used as default address in c line according to preference. |
| 241 | TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithIPv6Only) { |
| 242 | // Stun has a high preference than local host. |
| 243 | cricket::Candidate candidate1( |
| 244 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 245 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 246 | cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 247 | cricket::Candidate candidate2( |
| 248 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 249 | rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "", |
| 250 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 251 | |
| 252 | JsepIceCandidate jice1("audio", 0, candidate1); |
| 253 | JsepIceCandidate jice2("audio", 0, candidate2); |
| 254 | JsepIceCandidate jice3("video", 0, candidate1); |
| 255 | JsepIceCandidate jice4("video", 0, candidate2); |
| 256 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1)); |
| 257 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2)); |
| 258 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3)); |
| 259 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4)); |
| 260 | std::string message = Serialize(jsep_desc_.get()); |
| 261 | |
| 262 | // Should have a c line like this one. |
| 263 | EXPECT_NE(message.find("c=IN IP6 ::1"), std::string::npos); |
| 264 | // Shouldn't have a IP4 c line. |
| 265 | EXPECT_EQ(message.find("c=IN IP4"), std::string::npos); |
| 266 | } |
| 267 | |
| 268 | // Tests serialization of SDP with both IPv4 and IPv6 candidates and |
| 269 | // verifies that IPv4 is used as default address in c line even if the |
| 270 | // preference of IPv4 is lower. |
| 271 | TEST_F(JsepSessionDescriptionTest, |
| 272 | SerializeSessionDescriptionWithBothIPFamilies) { |
| 273 | cricket::Candidate candidate_v4( |
| 274 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 275 | rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "", |
| 276 | cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 277 | cricket::Candidate candidate_v6( |
| 278 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 279 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 280 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 281 | |
| 282 | JsepIceCandidate jice_v4("audio", 0, candidate_v4); |
| 283 | JsepIceCandidate jice_v6("audio", 0, candidate_v6); |
| 284 | JsepIceCandidate jice_v4_video("video", 0, candidate_v4); |
| 285 | JsepIceCandidate jice_v6_video("video", 0, candidate_v6); |
| 286 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4)); |
| 287 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6)); |
| 288 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video)); |
| 289 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video)); |
| 290 | std::string message = Serialize(jsep_desc_.get()); |
| 291 | |
| 292 | // Should have a c line like this one. |
| 293 | EXPECT_NE(message.find("c=IN IP4 192.168.1.5"), std::string::npos); |
| 294 | // Shouldn't have a IP6 c line. |
| 295 | EXPECT_EQ(message.find("c=IN IP6"), std::string::npos); |
| 296 | } |
| 297 | |
| 298 | // Tests serialization of SDP with both UDP and TCP candidates and |
| 299 | // verifies that UDP is used as default address in c line even if the |
| 300 | // preference of UDP is lower. |
| 301 | TEST_F(JsepSessionDescriptionTest, |
| 302 | SerializeSessionDescriptionWithBothProtocols) { |
| 303 | // Stun has a high preference than local host. |
| 304 | cricket::Candidate candidate1( |
| 305 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp", |
| 306 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 307 | cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 308 | cricket::Candidate candidate2( |
| 309 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 310 | rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority, |
| 311 | "", "", cricket::LOCAL_PORT_TYPE, kCandidateGeneration, |
| 312 | kCandidateFoundation); |
| 313 | |
| 314 | JsepIceCandidate jice1("audio", 0, candidate1); |
| 315 | JsepIceCandidate jice2("audio", 0, candidate2); |
| 316 | JsepIceCandidate jice3("video", 0, candidate1); |
| 317 | JsepIceCandidate jice4("video", 0, candidate2); |
| 318 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1)); |
| 319 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2)); |
| 320 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3)); |
| 321 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4)); |
| 322 | std::string message = Serialize(jsep_desc_.get()); |
| 323 | |
| 324 | // Should have a c line like this one. |
| 325 | EXPECT_NE(message.find("c=IN IP6 fe80::1234:5678:abcd:ef12"), |
| 326 | std::string::npos); |
| 327 | // Shouldn't have a IP4 c line. |
| 328 | EXPECT_EQ(message.find("c=IN IP4"), std::string::npos); |
| 329 | } |
| 330 | |
| 331 | // Tests serialization of SDP with only TCP candidates and verifies that |
| 332 | // null IPv4 is used as default address in c line. |
| 333 | TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithTCPOnly) { |
| 334 | // Stun has a high preference than local host. |
| 335 | cricket::Candidate candidate1( |
| 336 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp", |
| 337 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 338 | cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 339 | cricket::Candidate candidate2( |
| 340 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp", |
| 341 | rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "", |
| 342 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 343 | |
| 344 | JsepIceCandidate jice1("audio", 0, candidate1); |
| 345 | JsepIceCandidate jice2("audio", 0, candidate2); |
| 346 | JsepIceCandidate jice3("video", 0, candidate1); |
| 347 | JsepIceCandidate jice4("video", 0, candidate2); |
| 348 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1)); |
| 349 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2)); |
| 350 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3)); |
| 351 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4)); |
| 352 | |
| 353 | std::string message = Serialize(jsep_desc_.get()); |
| 354 | EXPECT_EQ(message.find("c=IN IP6 ::3"), std::string::npos); |
| 355 | // Should have a c line like this one when no any default exists. |
| 356 | EXPECT_NE(message.find("c=IN IP4 0.0.0.0"), std::string::npos); |
| 357 | } |
| 358 | |
| 359 | // Tests that the connection address will be correctly set when the Candidate is |
| 360 | // removed. |
| 361 | TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) { |
| 362 | cricket::Candidate candidate1( |
| 363 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 364 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 365 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 366 | candidate1.set_transport_name("audio"); |
| 367 | |
| 368 | cricket::Candidate candidate2( |
| 369 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp", |
| 370 | rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "", |
| 371 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 372 | candidate2.set_transport_name("audio"); |
| 373 | |
| 374 | cricket::Candidate candidate3( |
| 375 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 376 | rtc::SocketAddress("192.168.1.1", 1236), kCandidatePriority, "", "", |
| 377 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 378 | candidate3.set_transport_name("audio"); |
| 379 | |
| 380 | JsepIceCandidate jice1("audio", 0, candidate1); |
| 381 | JsepIceCandidate jice2("audio", 0, candidate2); |
| 382 | JsepIceCandidate jice3("audio", 0, candidate3); |
| 383 | |
| 384 | size_t audio_index = 0; |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 385 | auto media_desc = |
| 386 | jsep_desc_->description()->contents()[audio_index].media_description(); |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 387 | |
| 388 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1)); |
| 389 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2)); |
| 390 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3)); |
| 391 | |
| 392 | std::vector<cricket::Candidate> candidates; |
| 393 | EXPECT_EQ("192.168.1.1:1236", media_desc->connection_address().ToString()); |
| 394 | |
| 395 | candidates.push_back(candidate3); |
| 396 | ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates)); |
| 397 | EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString()); |
| 398 | |
| 399 | candidates.clear(); |
| 400 | candidates.push_back(candidate2); |
| 401 | ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates)); |
| 402 | EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString()); |
| 403 | |
| 404 | candidates.clear(); |
| 405 | candidates.push_back(candidate1); |
| 406 | ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates)); |
| 407 | EXPECT_EQ("0.0.0.0:9", media_desc->connection_address().ToString()); |
| 408 | } |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 409 | |
| 410 | class EnumerateAllSdpTypesTest : public ::testing::Test, |
| 411 | public ::testing::WithParamInterface<SdpType> { |
| 412 | }; |
| 413 | |
| 414 | TEST_P(EnumerateAllSdpTypesTest, TestIdentity) { |
| 415 | SdpType type = GetParam(); |
| 416 | |
| 417 | const char* str = webrtc::SdpTypeToString(type); |
| 418 | EXPECT_EQ(type, webrtc::SdpTypeFromString(str)); |
| 419 | } |
| 420 | |
| 421 | INSTANTIATE_TEST_CASE_P(JsepSessionDescriptionTest, |
| 422 | EnumerateAllSdpTypesTest, |
| 423 | Values(SdpType::kOffer, |
| 424 | SdpType::kPrAnswer, |
| 425 | SdpType::kAnswer)); |