blob: 2202aa81d0b4442b2dff49b872e0ae84d8d97944 [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
Byoungchan Leecb42e102021-07-15 14:09:40 +0900132TEST_F(JsepSessionDescriptionTest, CloneRollback) {
133 auto jsep_desc = std::make_unique<JsepSessionDescription>(SdpType::kRollback);
134 auto new_desc = jsep_desc->Clone();
135 EXPECT_EQ(jsep_desc->type(), new_desc->type());
136}
137
Harald Alvestrandfc6b8712021-01-05 10:51:08 +0000138TEST_F(JsepSessionDescriptionTest, CloneWithCandidates) {
139 cricket::Candidate candidate_v4(
140 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
141 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
142 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
143 cricket::Candidate candidate_v6(
144 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
145 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
146 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
147
148 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
149 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
150 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
151 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
152 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
153 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
154 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
155 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
156 auto new_desc = jsep_desc_->Clone();
157 EXPECT_EQ(jsep_desc_->type(), new_desc->type());
158 std::string old_desc_string;
159 std::string new_desc_string;
160 EXPECT_TRUE(jsep_desc_->ToString(&old_desc_string));
161 EXPECT_TRUE(new_desc->ToString(&new_desc_string));
162 EXPECT_EQ(old_desc_string, new_desc_string);
163}
164
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165// Test that number_of_mediasections() returns the number of media contents in
166// a session description.
167TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
168 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
169}
170
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700171// Test that we can add a candidate to a session description without MID.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
173 JsepIceCandidate jsep_candidate("", 0, candidate_);
174 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
175 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
176 ASSERT_TRUE(ice_candidates != NULL);
177 EXPECT_EQ(1u, ice_candidates->count());
178 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
179 ASSERT_TRUE(ice_candidate != NULL);
180 candidate_.set_username(kCandidateUfragVoice);
181 candidate_.set_password(kCandidatePwdVoice);
182 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
183 EXPECT_EQ(0, ice_candidate->sdp_mline_index());
184 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
185}
186
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700187// Test that we can add and remove candidates to a session description with
188// MID. Removing candidates requires MID (transport_name).
189TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 // mid and m-line index don't match, in this case mid is preferred.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700191 std::string mid = "video";
192 JsepIceCandidate jsep_candidate(mid, 0, candidate_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
194 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
195 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
196 ASSERT_TRUE(ice_candidates != NULL);
197 EXPECT_EQ(1u, ice_candidates->count());
198 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
199 ASSERT_TRUE(ice_candidate != NULL);
200 candidate_.set_username(kCandidateUfragVideo);
201 candidate_.set_password(kCandidatePwdVideo);
202 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
203 // The mline index should have been updated according to mid.
204 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700205
206 std::vector<cricket::Candidate> candidates(1, candidate_);
207 candidates[0].set_transport_name(mid);
208 EXPECT_EQ(1u, jsep_desc_->RemoveCandidates(candidates));
209 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
210 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211}
212
213TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
214 candidate_.set_username(kCandidateUfrag);
215 candidate_.set_password(kCandidatePwd);
216 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
217 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
218 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
219 ASSERT_TRUE(ice_candidates != NULL);
220 EXPECT_EQ(1u, ice_candidates->count());
221 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
222 ASSERT_TRUE(ice_candidate != NULL);
223 candidate_.set_username(kCandidateUfrag);
224 candidate_.set_password(kCandidatePwd);
225 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
226
227 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
228}
229
230// Test that we can not add a candidate if there is no corresponding media
231// content in the session description.
232TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
233 JsepIceCandidate bad_candidate1("", 55, candidate_);
234 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
235
236 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
237 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
238}
239
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000240// Tests that repeatedly adding the same candidate, with or without credentials,
241// does not increase the number of candidates in the description.
242TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
243 JsepIceCandidate jsep_candidate("", 0, candidate_);
244 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
245 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
246
247 // Add the same candidate again. It should be ignored.
248 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
249 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
250
251 // Create a new candidate, identical except that the ufrag and pwd are now
252 // populated.
253 candidate_.set_username(kCandidateUfragVoice);
254 candidate_.set_password(kCandidatePwdVoice);
255 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
256
257 // This should also be identified as redundant and ignored.
258 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
259 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
260}
261
Qingsi Wang56991422019-02-08 12:53:06 -0800262// Test that the connection address is set to a hostname address after adding a
263// hostname candidate.
264TEST_F(JsepSessionDescriptionTest, AddHostnameCandidate) {
265 cricket::Candidate c;
266 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
267 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
268 c.set_address(rtc::SocketAddress("example.local", 1234));
269 c.set_type(cricket::LOCAL_PORT_TYPE);
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700270 const size_t audio_index = 0;
271 JsepIceCandidate hostname_candidate("audio", audio_index, c);
Qingsi Wang56991422019-02-08 12:53:06 -0800272 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate));
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700273
Qingsi Wang56991422019-02-08 12:53:06 -0800274 ASSERT_NE(nullptr, jsep_desc_->description());
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700275 ASSERT_EQ(2u, jsep_desc_->description()->contents().size());
276 const auto& content = jsep_desc_->description()->contents()[audio_index];
277 EXPECT_EQ("0.0.0.0:9",
Qingsi Wang56991422019-02-08 12:53:06 -0800278 content.media_description()->connection_address().ToString());
279}
280
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281// Test that we can serialize a JsepSessionDescription and deserialize it again.
282TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
283 std::string sdp = Serialize(jsep_desc_.get());
284
Steve Antona3a92c22017-12-07 10:27:41 -0800285 auto parsed_jsep_desc = DeSerialize(sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
287
288 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
289 EXPECT_EQ(sdp, parsed_sdp);
290}
291
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700292// Test that we can serialize a JsepSessionDescription when a hostname candidate
293// is the default destination and deserialize it again. The connection address
294// in the deserialized description should be the dummy address 0.0.0.0:9.
295TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithHostnameCandidate) {
296 cricket::Candidate c;
297 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
298 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
299 c.set_address(rtc::SocketAddress("example.local", 1234));
300 c.set_type(cricket::LOCAL_PORT_TYPE);
301 const size_t audio_index = 0;
302 const size_t video_index = 1;
303 JsepIceCandidate hostname_candidate_audio("audio", audio_index, c);
304 JsepIceCandidate hostname_candidate_video("video", video_index, c);
305 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_audio));
306 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_video));
307
308 std::string sdp = Serialize(jsep_desc_.get());
309
310 auto parsed_jsep_desc = DeSerialize(sdp);
311 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
312
313 ASSERT_NE(nullptr, parsed_jsep_desc->description());
314 ASSERT_EQ(2u, parsed_jsep_desc->description()->contents().size());
315 const auto& audio_content =
316 parsed_jsep_desc->description()->contents()[audio_index];
317 const auto& video_content =
318 parsed_jsep_desc->description()->contents()[video_index];
319 EXPECT_EQ("0.0.0.0:9",
320 audio_content.media_description()->connection_address().ToString());
321 EXPECT_EQ("0.0.0.0:9",
322 video_content.media_description()->connection_address().ToString());
323}
324
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325// Tests that we can serialize and deserialize a JsepSesssionDescription
326// with candidates.
327TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
328 std::string sdp = Serialize(jsep_desc_.get());
329
330 // Add a candidate and check that the serialized result is different.
331 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
332 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
333 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
334 EXPECT_NE(sdp, sdp_with_candidate);
335
Steve Antona3a92c22017-12-07 10:27:41 -0800336 auto parsed_jsep_desc = DeSerialize(sdp_with_candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
338
339 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
340}
zhihuang38989e52017-03-21 11:04:53 -0700341
342// TODO(zhihuang): Modify these tests. These are used to verify that after
343// adding the candidates, the connection_address field is set correctly. Modify
344// those so that the "connection address" is tested directly.
345// Tests serialization of SDP with only IPv6 candidates and verifies that IPv6
346// is used as default address in c line according to preference.
347TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithIPv6Only) {
348 // Stun has a high preference than local host.
349 cricket::Candidate candidate1(
350 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
351 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
352 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
353 cricket::Candidate candidate2(
354 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
355 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
356 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
357
358 JsepIceCandidate jice1("audio", 0, candidate1);
359 JsepIceCandidate jice2("audio", 0, candidate2);
360 JsepIceCandidate jice3("video", 0, candidate1);
361 JsepIceCandidate jice4("video", 0, candidate2);
362 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
363 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
364 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
365 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
366 std::string message = Serialize(jsep_desc_.get());
367
368 // Should have a c line like this one.
369 EXPECT_NE(message.find("c=IN IP6 ::1"), std::string::npos);
370 // Shouldn't have a IP4 c line.
371 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
372}
373
374// Tests serialization of SDP with both IPv4 and IPv6 candidates and
375// verifies that IPv4 is used as default address in c line even if the
376// preference of IPv4 is lower.
377TEST_F(JsepSessionDescriptionTest,
378 SerializeSessionDescriptionWithBothIPFamilies) {
379 cricket::Candidate candidate_v4(
380 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
381 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
382 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
383 cricket::Candidate candidate_v6(
384 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
385 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
386 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
387
388 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
389 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
390 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
391 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
392 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
393 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
394 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
395 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
396 std::string message = Serialize(jsep_desc_.get());
397
398 // Should have a c line like this one.
399 EXPECT_NE(message.find("c=IN IP4 192.168.1.5"), std::string::npos);
400 // Shouldn't have a IP6 c line.
401 EXPECT_EQ(message.find("c=IN IP6"), std::string::npos);
402}
403
404// Tests serialization of SDP with both UDP and TCP candidates and
405// verifies that UDP is used as default address in c line even if the
406// preference of UDP is lower.
407TEST_F(JsepSessionDescriptionTest,
408 SerializeSessionDescriptionWithBothProtocols) {
409 // Stun has a high preference than local host.
410 cricket::Candidate candidate1(
411 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
412 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
413 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
414 cricket::Candidate candidate2(
415 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
416 rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority,
417 "", "", cricket::LOCAL_PORT_TYPE, kCandidateGeneration,
418 kCandidateFoundation);
419
420 JsepIceCandidate jice1("audio", 0, candidate1);
421 JsepIceCandidate jice2("audio", 0, candidate2);
422 JsepIceCandidate jice3("video", 0, candidate1);
423 JsepIceCandidate jice4("video", 0, candidate2);
424 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
425 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
426 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
427 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
428 std::string message = Serialize(jsep_desc_.get());
429
430 // Should have a c line like this one.
431 EXPECT_NE(message.find("c=IN IP6 fe80::1234:5678:abcd:ef12"),
432 std::string::npos);
433 // Shouldn't have a IP4 c line.
434 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
435}
436
437// Tests serialization of SDP with only TCP candidates and verifies that
438// null IPv4 is used as default address in c line.
439TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithTCPOnly) {
440 // Stun has a high preference than local host.
441 cricket::Candidate candidate1(
442 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
443 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
444 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
445 cricket::Candidate candidate2(
446 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
447 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
448 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
449
450 JsepIceCandidate jice1("audio", 0, candidate1);
451 JsepIceCandidate jice2("audio", 0, candidate2);
452 JsepIceCandidate jice3("video", 0, candidate1);
453 JsepIceCandidate jice4("video", 0, candidate2);
454 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
455 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
456 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
457 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
458
459 std::string message = Serialize(jsep_desc_.get());
460 EXPECT_EQ(message.find("c=IN IP6 ::3"), std::string::npos);
461 // Should have a c line like this one when no any default exists.
462 EXPECT_NE(message.find("c=IN IP4 0.0.0.0"), std::string::npos);
463}
464
465// Tests that the connection address will be correctly set when the Candidate is
466// removed.
467TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) {
468 cricket::Candidate candidate1(
469 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
470 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
471 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
472 candidate1.set_transport_name("audio");
473
474 cricket::Candidate candidate2(
475 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
476 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
477 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
478 candidate2.set_transport_name("audio");
479
480 cricket::Candidate candidate3(
481 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
482 rtc::SocketAddress("192.168.1.1", 1236), kCandidatePriority, "", "",
483 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
484 candidate3.set_transport_name("audio");
485
486 JsepIceCandidate jice1("audio", 0, candidate1);
487 JsepIceCandidate jice2("audio", 0, candidate2);
488 JsepIceCandidate jice3("audio", 0, candidate3);
489
490 size_t audio_index = 0;
Steve Antonb1c1de12017-12-21 15:14:30 -0800491 auto media_desc =
492 jsep_desc_->description()->contents()[audio_index].media_description();
zhihuang38989e52017-03-21 11:04:53 -0700493
494 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
495 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
496 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
497
498 std::vector<cricket::Candidate> candidates;
499 EXPECT_EQ("192.168.1.1:1236", media_desc->connection_address().ToString());
500
501 candidates.push_back(candidate3);
502 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
503 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
504
505 candidates.clear();
506 candidates.push_back(candidate2);
507 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
508 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
509
510 candidates.clear();
511 candidates.push_back(candidate1);
512 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
513 EXPECT_EQ("0.0.0.0:9", media_desc->connection_address().ToString());
514}
Steve Anton88f2cb92017-12-05 12:47:32 -0800515
516class EnumerateAllSdpTypesTest : public ::testing::Test,
517 public ::testing::WithParamInterface<SdpType> {
518};
519
520TEST_P(EnumerateAllSdpTypesTest, TestIdentity) {
521 SdpType type = GetParam();
522
523 const char* str = webrtc::SdpTypeToString(type);
524 EXPECT_EQ(type, webrtc::SdpTypeFromString(str));
525}
526
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100527INSTANTIATE_TEST_SUITE_P(JsepSessionDescriptionTest,
528 EnumerateAllSdpTypesTest,
529 Values(SdpType::kOffer,
530 SdpType::kPrAnswer,
531 SdpType::kAnswer));