blob: d922a586c524f41df8e6c43212fef3269ea991a1 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "api/jsep_session_description.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
14#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
kwibergd1fe2812016-04-27 06:47:29 -070016#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <string>
Yves Gerey3e707812018-11-28 16:47:49 +010018#include <utility>
19#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
Patrik Höglunde2d6a062017-10-05 14:53:33 +020021#include "api/candidate.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/jsep_ice_candidate.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "p2p/base/p2p_constants.h"
Steve Antona3a92c22017-12-07 10:27:41 -080026#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "p2p/base/transport_description.h"
28#include "p2p/base/transport_info.h"
29#include "pc/session_description.h"
30#include "pc/webrtc_sdp.h"
Yves Gerey3e707812018-11-28 16:47:49 +010031#include "rtc_base/helpers.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/socket_address.h"
33#include "rtc_base/string_encode.h"
Yves Gerey3e707812018-11-28 16:47:49 +010034#include "test/gtest.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
Steve Anton5adfafd2017-12-20 16:34:00 -080036using cricket::MediaProtocolType;
Steve Anton88f2cb92017-12-05 12:47:32 -080037using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038using webrtc::IceCandidateCollection;
39using webrtc::IceCandidateInterface;
40using webrtc::JsepIceCandidate;
41using webrtc::JsepSessionDescription;
Steve Anton88f2cb92017-12-05 12:47:32 -080042using webrtc::SdpType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043using webrtc::SessionDescriptionInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45static const char kCandidateUfrag[] = "ufrag";
46static const char kCandidatePwd[] = "pwd";
47static const char kCandidateUfragVoice[] = "ufrag_voice";
48static const char kCandidatePwdVoice[] = "pwd_voice";
49static const char kCandidateUfragVideo[] = "ufrag_video";
50static const char kCandidatePwdVideo[] = "pwd_video";
zhihuang38989e52017-03-21 11:04:53 -070051static const char kCandidateFoundation[] = "a0+B/1";
52static const uint32_t kCandidatePriority = 2130706432U; // pref = 1.0
53static const uint32_t kCandidateGeneration = 2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
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.
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020057static std::unique_ptr<cricket::SessionDescription>
58CreateCricketSessionDescription() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020059 auto desc = std::make_unique<cricket::SessionDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020061 // AudioContentDescription
Mirko Bonadei317a1f02019-09-17 17:06:18 +020062 auto audio = std::make_unique<cricket::AudioContentDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 // VideoContentDescription
Mirko Bonadei317a1f02019-09-17 17:06:18 +020064 auto video = std::make_unique<cricket::VideoContentDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
deadbeef67cf2c12016-04-13 10:07:16 -070066 audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0));
Harald Alvestrand1716d392019-06-03 20:35:45 +020067 desc->AddContent(cricket::CN_AUDIO, MediaProtocolType::kRtp,
68 std::move(audio));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069
perkj26752742016-10-24 01:21:16 -070070 video->AddCodec(cricket::VideoCodec(120, "VP8"));
Harald Alvestrand1716d392019-06-03 20:35:45 +020071 desc->AddContent(cricket::CN_VIDEO, MediaProtocolType::kRtp,
72 std::move(video));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
Steve Anton06817cd2018-12-18 15:55:30 -080074 desc->AddTransportInfo(cricket::TransportInfo(
deadbeef46eed762016-01-28 13:24:37 -080075 cricket::CN_AUDIO,
76 cricket::TransportDescription(
77 std::vector<std::string>(), kCandidateUfragVoice, kCandidatePwdVoice,
Steve Anton06817cd2018-12-18 15:55:30 -080078 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)));
79 desc->AddTransportInfo(cricket::TransportInfo(
deadbeef46eed762016-01-28 13:24:37 -080080 cricket::CN_VIDEO,
81 cricket::TransportDescription(
82 std::vector<std::string>(), kCandidateUfragVideo, kCandidatePwdVideo,
Steve Anton06817cd2018-12-18 15:55:30 -080083 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 return desc;
85}
86
Mirko Bonadei6a489f22019-04-09 15:11:12 +020087class JsepSessionDescriptionTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 virtual void SetUp() {
90 int port = 1234;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091 rtc::SocketAddress address("127.0.0.1", port++);
guoweis@webrtc.org61c12472015-01-15 06:53:07 +000092 cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
93 address, 1, "", "", "local", 0, "1");
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 candidate_ = candidate;
Yves Gerey665174f2018-06-19 15:03:05 +020095 const std::string session_id = rtc::ToString(rtc::CreateRandomId64());
96 const std::string session_version = rtc::ToString(rtc::CreateRandomId());
Mirko Bonadei317a1f02019-09-17 17:06:18 +020097 jsep_desc_ = std::make_unique<JsepSessionDescription>(SdpType::kOffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(),
Yves Gerey665174f2018-06-19 15:03:05 +020099 session_id, session_version));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 }
101
102 std::string Serialize(const SessionDescriptionInterface* desc) {
103 std::string sdp;
104 EXPECT_TRUE(desc->ToString(&sdp));
105 EXPECT_FALSE(sdp.empty());
106 return sdp;
107 }
108
Steve Antona3a92c22017-12-07 10:27:41 -0800109 std::unique_ptr<SessionDescriptionInterface> DeSerialize(
110 const std::string& sdp) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200111 auto jsep_desc = std::make_unique<JsepSessionDescription>(SdpType::kOffer);
Steve Antona3a92c22017-12-07 10:27:41 -0800112 EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jsep_desc.get(), nullptr));
113 return std::move(jsep_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 }
115
116 cricket::Candidate candidate_;
kwibergd1fe2812016-04-27 06:47:29 -0700117 std::unique_ptr<JsepSessionDescription> jsep_desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118};
119
Harald Alvestrand0e7b3a92020-12-15 15:18:28 +0000120TEST_F(JsepSessionDescriptionTest, CloneDefault) {
121 auto new_desc = jsep_desc_->Clone();
122 EXPECT_EQ(jsep_desc_->type(), new_desc->type());
123 std::string old_desc_string;
124 std::string new_desc_string;
125 EXPECT_TRUE(jsep_desc_->ToString(&old_desc_string));
126 EXPECT_TRUE(new_desc->ToString(&new_desc_string));
127 EXPECT_EQ(old_desc_string, new_desc_string);
128 EXPECT_EQ(jsep_desc_->session_id(), new_desc->session_id());
129 EXPECT_EQ(jsep_desc_->session_version(), new_desc->session_version());
130}
131
Harald Alvestrandfc6b8712021-01-05 10:51:08 +0000132TEST_F(JsepSessionDescriptionTest, CloneWithCandidates) {
133 cricket::Candidate candidate_v4(
134 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
135 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
136 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
137 cricket::Candidate candidate_v6(
138 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
139 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
140 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
141
142 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
143 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
144 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
145 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
146 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
147 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
148 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
149 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
150 auto new_desc = jsep_desc_->Clone();
151 EXPECT_EQ(jsep_desc_->type(), new_desc->type());
152 std::string old_desc_string;
153 std::string new_desc_string;
154 EXPECT_TRUE(jsep_desc_->ToString(&old_desc_string));
155 EXPECT_TRUE(new_desc->ToString(&new_desc_string));
156 EXPECT_EQ(old_desc_string, new_desc_string);
157}
158
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159// Test that number_of_mediasections() returns the number of media contents in
160// a session description.
161TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
162 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
163}
164
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700165// Test that we can add a candidate to a session description without MID.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
167 JsepIceCandidate jsep_candidate("", 0, candidate_);
168 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
169 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
170 ASSERT_TRUE(ice_candidates != NULL);
171 EXPECT_EQ(1u, ice_candidates->count());
172 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
173 ASSERT_TRUE(ice_candidate != NULL);
174 candidate_.set_username(kCandidateUfragVoice);
175 candidate_.set_password(kCandidatePwdVoice);
176 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
177 EXPECT_EQ(0, ice_candidate->sdp_mline_index());
178 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
179}
180
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700181// Test that we can add and remove candidates to a session description with
182// MID. Removing candidates requires MID (transport_name).
183TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 // mid and m-line index don't match, in this case mid is preferred.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700185 std::string mid = "video";
186 JsepIceCandidate jsep_candidate(mid, 0, candidate_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
188 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
189 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
190 ASSERT_TRUE(ice_candidates != NULL);
191 EXPECT_EQ(1u, ice_candidates->count());
192 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
193 ASSERT_TRUE(ice_candidate != NULL);
194 candidate_.set_username(kCandidateUfragVideo);
195 candidate_.set_password(kCandidatePwdVideo);
196 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
197 // The mline index should have been updated according to mid.
198 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700199
200 std::vector<cricket::Candidate> candidates(1, candidate_);
201 candidates[0].set_transport_name(mid);
202 EXPECT_EQ(1u, jsep_desc_->RemoveCandidates(candidates));
203 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
204 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205}
206
207TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
208 candidate_.set_username(kCandidateUfrag);
209 candidate_.set_password(kCandidatePwd);
210 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
211 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
212 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
213 ASSERT_TRUE(ice_candidates != NULL);
214 EXPECT_EQ(1u, ice_candidates->count());
215 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
216 ASSERT_TRUE(ice_candidate != NULL);
217 candidate_.set_username(kCandidateUfrag);
218 candidate_.set_password(kCandidatePwd);
219 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
220
221 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
222}
223
224// Test that we can not add a candidate if there is no corresponding media
225// content in the session description.
226TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
227 JsepIceCandidate bad_candidate1("", 55, candidate_);
228 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
229
230 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
231 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
232}
233
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000234// Tests that repeatedly adding the same candidate, with or without credentials,
235// does not increase the number of candidates in the description.
236TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
237 JsepIceCandidate jsep_candidate("", 0, candidate_);
238 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
239 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
240
241 // Add the same candidate again. It should be ignored.
242 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
243 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
244
245 // Create a new candidate, identical except that the ufrag and pwd are now
246 // populated.
247 candidate_.set_username(kCandidateUfragVoice);
248 candidate_.set_password(kCandidatePwdVoice);
249 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
250
251 // This should also be identified as redundant and ignored.
252 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
253 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
254}
255
Qingsi Wang56991422019-02-08 12:53:06 -0800256// Test that the connection address is set to a hostname address after adding a
257// hostname candidate.
258TEST_F(JsepSessionDescriptionTest, AddHostnameCandidate) {
259 cricket::Candidate c;
260 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
261 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
262 c.set_address(rtc::SocketAddress("example.local", 1234));
263 c.set_type(cricket::LOCAL_PORT_TYPE);
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700264 const size_t audio_index = 0;
265 JsepIceCandidate hostname_candidate("audio", audio_index, c);
Qingsi Wang56991422019-02-08 12:53:06 -0800266 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate));
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700267
Qingsi Wang56991422019-02-08 12:53:06 -0800268 ASSERT_NE(nullptr, jsep_desc_->description());
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700269 ASSERT_EQ(2u, jsep_desc_->description()->contents().size());
270 const auto& content = jsep_desc_->description()->contents()[audio_index];
271 EXPECT_EQ("0.0.0.0:9",
Qingsi Wang56991422019-02-08 12:53:06 -0800272 content.media_description()->connection_address().ToString());
273}
274
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275// Test that we can serialize a JsepSessionDescription and deserialize it again.
276TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
277 std::string sdp = Serialize(jsep_desc_.get());
278
Steve Antona3a92c22017-12-07 10:27:41 -0800279 auto parsed_jsep_desc = DeSerialize(sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
281
282 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
283 EXPECT_EQ(sdp, parsed_sdp);
284}
285
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700286// Test that we can serialize a JsepSessionDescription when a hostname candidate
287// is the default destination and deserialize it again. The connection address
288// in the deserialized description should be the dummy address 0.0.0.0:9.
289TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithHostnameCandidate) {
290 cricket::Candidate c;
291 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
292 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
293 c.set_address(rtc::SocketAddress("example.local", 1234));
294 c.set_type(cricket::LOCAL_PORT_TYPE);
295 const size_t audio_index = 0;
296 const size_t video_index = 1;
297 JsepIceCandidate hostname_candidate_audio("audio", audio_index, c);
298 JsepIceCandidate hostname_candidate_video("video", video_index, c);
299 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_audio));
300 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_video));
301
302 std::string sdp = Serialize(jsep_desc_.get());
303
304 auto parsed_jsep_desc = DeSerialize(sdp);
305 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
306
307 ASSERT_NE(nullptr, parsed_jsep_desc->description());
308 ASSERT_EQ(2u, parsed_jsep_desc->description()->contents().size());
309 const auto& audio_content =
310 parsed_jsep_desc->description()->contents()[audio_index];
311 const auto& video_content =
312 parsed_jsep_desc->description()->contents()[video_index];
313 EXPECT_EQ("0.0.0.0:9",
314 audio_content.media_description()->connection_address().ToString());
315 EXPECT_EQ("0.0.0.0:9",
316 video_content.media_description()->connection_address().ToString());
317}
318
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319// Tests that we can serialize and deserialize a JsepSesssionDescription
320// with candidates.
321TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
322 std::string sdp = Serialize(jsep_desc_.get());
323
324 // Add a candidate and check that the serialized result is different.
325 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
326 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
327 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
328 EXPECT_NE(sdp, sdp_with_candidate);
329
Steve Antona3a92c22017-12-07 10:27:41 -0800330 auto parsed_jsep_desc = DeSerialize(sdp_with_candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
332
333 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
334}
zhihuang38989e52017-03-21 11:04:53 -0700335
336// TODO(zhihuang): Modify these tests. These are used to verify that after
337// adding the candidates, the connection_address field is set correctly. Modify
338// those so that the "connection address" is tested directly.
339// Tests serialization of SDP with only IPv6 candidates and verifies that IPv6
340// is used as default address in c line according to preference.
341TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithIPv6Only) {
342 // Stun has a high preference than local host.
343 cricket::Candidate candidate1(
344 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
345 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
346 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
347 cricket::Candidate candidate2(
348 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
349 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
350 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
351
352 JsepIceCandidate jice1("audio", 0, candidate1);
353 JsepIceCandidate jice2("audio", 0, candidate2);
354 JsepIceCandidate jice3("video", 0, candidate1);
355 JsepIceCandidate jice4("video", 0, candidate2);
356 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
357 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
358 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
359 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
360 std::string message = Serialize(jsep_desc_.get());
361
362 // Should have a c line like this one.
363 EXPECT_NE(message.find("c=IN IP6 ::1"), std::string::npos);
364 // Shouldn't have a IP4 c line.
365 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
366}
367
368// Tests serialization of SDP with both IPv4 and IPv6 candidates and
369// verifies that IPv4 is used as default address in c line even if the
370// preference of IPv4 is lower.
371TEST_F(JsepSessionDescriptionTest,
372 SerializeSessionDescriptionWithBothIPFamilies) {
373 cricket::Candidate candidate_v4(
374 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
375 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
376 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
377 cricket::Candidate candidate_v6(
378 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
379 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
380 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
381
382 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
383 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
384 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
385 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
386 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
387 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
388 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
389 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
390 std::string message = Serialize(jsep_desc_.get());
391
392 // Should have a c line like this one.
393 EXPECT_NE(message.find("c=IN IP4 192.168.1.5"), std::string::npos);
394 // Shouldn't have a IP6 c line.
395 EXPECT_EQ(message.find("c=IN IP6"), std::string::npos);
396}
397
398// Tests serialization of SDP with both UDP and TCP candidates and
399// verifies that UDP is used as default address in c line even if the
400// preference of UDP is lower.
401TEST_F(JsepSessionDescriptionTest,
402 SerializeSessionDescriptionWithBothProtocols) {
403 // Stun has a high preference than local host.
404 cricket::Candidate candidate1(
405 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
406 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
407 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
408 cricket::Candidate candidate2(
409 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
410 rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority,
411 "", "", cricket::LOCAL_PORT_TYPE, kCandidateGeneration,
412 kCandidateFoundation);
413
414 JsepIceCandidate jice1("audio", 0, candidate1);
415 JsepIceCandidate jice2("audio", 0, candidate2);
416 JsepIceCandidate jice3("video", 0, candidate1);
417 JsepIceCandidate jice4("video", 0, candidate2);
418 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
419 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
420 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
421 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
422 std::string message = Serialize(jsep_desc_.get());
423
424 // Should have a c line like this one.
425 EXPECT_NE(message.find("c=IN IP6 fe80::1234:5678:abcd:ef12"),
426 std::string::npos);
427 // Shouldn't have a IP4 c line.
428 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
429}
430
431// Tests serialization of SDP with only TCP candidates and verifies that
432// null IPv4 is used as default address in c line.
433TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithTCPOnly) {
434 // Stun has a high preference than local host.
435 cricket::Candidate candidate1(
436 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
437 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
438 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
439 cricket::Candidate candidate2(
440 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
441 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
442 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
443
444 JsepIceCandidate jice1("audio", 0, candidate1);
445 JsepIceCandidate jice2("audio", 0, candidate2);
446 JsepIceCandidate jice3("video", 0, candidate1);
447 JsepIceCandidate jice4("video", 0, candidate2);
448 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
449 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
450 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
451 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
452
453 std::string message = Serialize(jsep_desc_.get());
454 EXPECT_EQ(message.find("c=IN IP6 ::3"), std::string::npos);
455 // Should have a c line like this one when no any default exists.
456 EXPECT_NE(message.find("c=IN IP4 0.0.0.0"), std::string::npos);
457}
458
459// Tests that the connection address will be correctly set when the Candidate is
460// removed.
461TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) {
462 cricket::Candidate candidate1(
463 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
464 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
465 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
466 candidate1.set_transport_name("audio");
467
468 cricket::Candidate candidate2(
469 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
470 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
471 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
472 candidate2.set_transport_name("audio");
473
474 cricket::Candidate candidate3(
475 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
476 rtc::SocketAddress("192.168.1.1", 1236), kCandidatePriority, "", "",
477 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
478 candidate3.set_transport_name("audio");
479
480 JsepIceCandidate jice1("audio", 0, candidate1);
481 JsepIceCandidate jice2("audio", 0, candidate2);
482 JsepIceCandidate jice3("audio", 0, candidate3);
483
484 size_t audio_index = 0;
Steve Antonb1c1de12017-12-21 15:14:30 -0800485 auto media_desc =
486 jsep_desc_->description()->contents()[audio_index].media_description();
zhihuang38989e52017-03-21 11:04:53 -0700487
488 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
489 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
490 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
491
492 std::vector<cricket::Candidate> candidates;
493 EXPECT_EQ("192.168.1.1:1236", media_desc->connection_address().ToString());
494
495 candidates.push_back(candidate3);
496 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
497 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
498
499 candidates.clear();
500 candidates.push_back(candidate2);
501 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
502 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
503
504 candidates.clear();
505 candidates.push_back(candidate1);
506 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
507 EXPECT_EQ("0.0.0.0:9", media_desc->connection_address().ToString());
508}
Steve Anton88f2cb92017-12-05 12:47:32 -0800509
510class EnumerateAllSdpTypesTest : public ::testing::Test,
511 public ::testing::WithParamInterface<SdpType> {
512};
513
514TEST_P(EnumerateAllSdpTypesTest, TestIdentity) {
515 SdpType type = GetParam();
516
517 const char* str = webrtc::SdpTypeToString(type);
518 EXPECT_EQ(type, webrtc::SdpTypeFromString(str));
519}
520
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100521INSTANTIATE_TEST_SUITE_P(JsepSessionDescriptionTest,
522 EnumerateAllSdpTypesTest,
523 Values(SdpType::kOffer,
524 SdpType::kPrAnswer,
525 SdpType::kAnswer));