blob: 8caac9461378bf0b1ed6c908035dcc0f17c08e0b [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
120// Test that number_of_mediasections() returns the number of media contents in
121// a session description.
122TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
123 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
124}
125
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700126// Test that we can add a candidate to a session description without MID.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
128 JsepIceCandidate jsep_candidate("", 0, candidate_);
129 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
130 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
131 ASSERT_TRUE(ice_candidates != NULL);
132 EXPECT_EQ(1u, ice_candidates->count());
133 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
134 ASSERT_TRUE(ice_candidate != NULL);
135 candidate_.set_username(kCandidateUfragVoice);
136 candidate_.set_password(kCandidatePwdVoice);
137 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
138 EXPECT_EQ(0, ice_candidate->sdp_mline_index());
139 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
140}
141
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700142// Test that we can add and remove candidates to a session description with
143// MID. Removing candidates requires MID (transport_name).
144TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 // mid and m-line index don't match, in this case mid is preferred.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700146 std::string mid = "video";
147 JsepIceCandidate jsep_candidate(mid, 0, candidate_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
149 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
150 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
151 ASSERT_TRUE(ice_candidates != NULL);
152 EXPECT_EQ(1u, ice_candidates->count());
153 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
154 ASSERT_TRUE(ice_candidate != NULL);
155 candidate_.set_username(kCandidateUfragVideo);
156 candidate_.set_password(kCandidatePwdVideo);
157 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
158 // The mline index should have been updated according to mid.
159 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700160
161 std::vector<cricket::Candidate> candidates(1, candidate_);
162 candidates[0].set_transport_name(mid);
163 EXPECT_EQ(1u, jsep_desc_->RemoveCandidates(candidates));
164 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
165 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166}
167
168TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
169 candidate_.set_username(kCandidateUfrag);
170 candidate_.set_password(kCandidatePwd);
171 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
172 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
173 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
174 ASSERT_TRUE(ice_candidates != NULL);
175 EXPECT_EQ(1u, ice_candidates->count());
176 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
177 ASSERT_TRUE(ice_candidate != NULL);
178 candidate_.set_username(kCandidateUfrag);
179 candidate_.set_password(kCandidatePwd);
180 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
181
182 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
183}
184
185// Test that we can not add a candidate if there is no corresponding media
186// content in the session description.
187TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
188 JsepIceCandidate bad_candidate1("", 55, candidate_);
189 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
190
191 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
192 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
193}
194
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000195// Tests that repeatedly adding the same candidate, with or without credentials,
196// does not increase the number of candidates in the description.
197TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
198 JsepIceCandidate jsep_candidate("", 0, candidate_);
199 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
200 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
201
202 // Add the same candidate again. It should be ignored.
203 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
204 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
205
206 // Create a new candidate, identical except that the ufrag and pwd are now
207 // populated.
208 candidate_.set_username(kCandidateUfragVoice);
209 candidate_.set_password(kCandidatePwdVoice);
210 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
211
212 // This should also be identified as redundant and ignored.
213 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
214 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
215}
216
Qingsi Wang56991422019-02-08 12:53:06 -0800217// Test that the connection address is set to a hostname address after adding a
218// hostname candidate.
219TEST_F(JsepSessionDescriptionTest, AddHostnameCandidate) {
220 cricket::Candidate c;
221 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
222 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
223 c.set_address(rtc::SocketAddress("example.local", 1234));
224 c.set_type(cricket::LOCAL_PORT_TYPE);
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700225 const size_t audio_index = 0;
226 JsepIceCandidate hostname_candidate("audio", audio_index, c);
Qingsi Wang56991422019-02-08 12:53:06 -0800227 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate));
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700228
Qingsi Wang56991422019-02-08 12:53:06 -0800229 ASSERT_NE(nullptr, jsep_desc_->description());
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700230 ASSERT_EQ(2u, jsep_desc_->description()->contents().size());
231 const auto& content = jsep_desc_->description()->contents()[audio_index];
232 EXPECT_EQ("0.0.0.0:9",
Qingsi Wang56991422019-02-08 12:53:06 -0800233 content.media_description()->connection_address().ToString());
234}
235
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236// Test that we can serialize a JsepSessionDescription and deserialize it again.
237TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
238 std::string sdp = Serialize(jsep_desc_.get());
239
Steve Antona3a92c22017-12-07 10:27:41 -0800240 auto parsed_jsep_desc = DeSerialize(sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
242
243 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
244 EXPECT_EQ(sdp, parsed_sdp);
245}
246
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700247// Test that we can serialize a JsepSessionDescription when a hostname candidate
248// is the default destination and deserialize it again. The connection address
249// in the deserialized description should be the dummy address 0.0.0.0:9.
250TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithHostnameCandidate) {
251 cricket::Candidate c;
252 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
253 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
254 c.set_address(rtc::SocketAddress("example.local", 1234));
255 c.set_type(cricket::LOCAL_PORT_TYPE);
256 const size_t audio_index = 0;
257 const size_t video_index = 1;
258 JsepIceCandidate hostname_candidate_audio("audio", audio_index, c);
259 JsepIceCandidate hostname_candidate_video("video", video_index, c);
260 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_audio));
261 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_video));
262
263 std::string sdp = Serialize(jsep_desc_.get());
264
265 auto parsed_jsep_desc = DeSerialize(sdp);
266 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
267
268 ASSERT_NE(nullptr, parsed_jsep_desc->description());
269 ASSERT_EQ(2u, parsed_jsep_desc->description()->contents().size());
270 const auto& audio_content =
271 parsed_jsep_desc->description()->contents()[audio_index];
272 const auto& video_content =
273 parsed_jsep_desc->description()->contents()[video_index];
274 EXPECT_EQ("0.0.0.0:9",
275 audio_content.media_description()->connection_address().ToString());
276 EXPECT_EQ("0.0.0.0:9",
277 video_content.media_description()->connection_address().ToString());
278}
279
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280// Tests that we can serialize and deserialize a JsepSesssionDescription
281// with candidates.
282TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
283 std::string sdp = Serialize(jsep_desc_.get());
284
285 // Add a candidate and check that the serialized result is different.
286 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
287 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
288 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
289 EXPECT_NE(sdp, sdp_with_candidate);
290
Steve Antona3a92c22017-12-07 10:27:41 -0800291 auto parsed_jsep_desc = DeSerialize(sdp_with_candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
293
294 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
295}
zhihuang38989e52017-03-21 11:04:53 -0700296
297// TODO(zhihuang): Modify these tests. These are used to verify that after
298// adding the candidates, the connection_address field is set correctly. Modify
299// those so that the "connection address" is tested directly.
300// Tests serialization of SDP with only IPv6 candidates and verifies that IPv6
301// is used as default address in c line according to preference.
302TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithIPv6Only) {
303 // Stun has a high preference than local host.
304 cricket::Candidate candidate1(
305 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
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("::2", 1235), kCandidatePriority, "", "",
311 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
312
313 JsepIceCandidate jice1("audio", 0, candidate1);
314 JsepIceCandidate jice2("audio", 0, candidate2);
315 JsepIceCandidate jice3("video", 0, candidate1);
316 JsepIceCandidate jice4("video", 0, candidate2);
317 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
318 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
319 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
320 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
321 std::string message = Serialize(jsep_desc_.get());
322
323 // Should have a c line like this one.
324 EXPECT_NE(message.find("c=IN IP6 ::1"), std::string::npos);
325 // Shouldn't have a IP4 c line.
326 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
327}
328
329// Tests serialization of SDP with both IPv4 and IPv6 candidates and
330// verifies that IPv4 is used as default address in c line even if the
331// preference of IPv4 is lower.
332TEST_F(JsepSessionDescriptionTest,
333 SerializeSessionDescriptionWithBothIPFamilies) {
334 cricket::Candidate candidate_v4(
335 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
336 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
337 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
338 cricket::Candidate candidate_v6(
339 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
340 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
341 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
342
343 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
344 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
345 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
346 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
347 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
348 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
349 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
350 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
351 std::string message = Serialize(jsep_desc_.get());
352
353 // Should have a c line like this one.
354 EXPECT_NE(message.find("c=IN IP4 192.168.1.5"), std::string::npos);
355 // Shouldn't have a IP6 c line.
356 EXPECT_EQ(message.find("c=IN IP6"), std::string::npos);
357}
358
359// Tests serialization of SDP with both UDP and TCP candidates and
360// verifies that UDP is used as default address in c line even if the
361// preference of UDP is lower.
362TEST_F(JsepSessionDescriptionTest,
363 SerializeSessionDescriptionWithBothProtocols) {
364 // Stun has a high preference than local host.
365 cricket::Candidate candidate1(
366 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
367 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
368 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
369 cricket::Candidate candidate2(
370 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
371 rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority,
372 "", "", cricket::LOCAL_PORT_TYPE, kCandidateGeneration,
373 kCandidateFoundation);
374
375 JsepIceCandidate jice1("audio", 0, candidate1);
376 JsepIceCandidate jice2("audio", 0, candidate2);
377 JsepIceCandidate jice3("video", 0, candidate1);
378 JsepIceCandidate jice4("video", 0, candidate2);
379 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
380 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
381 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
382 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
383 std::string message = Serialize(jsep_desc_.get());
384
385 // Should have a c line like this one.
386 EXPECT_NE(message.find("c=IN IP6 fe80::1234:5678:abcd:ef12"),
387 std::string::npos);
388 // Shouldn't have a IP4 c line.
389 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
390}
391
392// Tests serialization of SDP with only TCP candidates and verifies that
393// null IPv4 is used as default address in c line.
394TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithTCPOnly) {
395 // Stun has a high preference than local host.
396 cricket::Candidate candidate1(
397 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
398 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
399 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
400 cricket::Candidate candidate2(
401 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
402 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
403 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
404
405 JsepIceCandidate jice1("audio", 0, candidate1);
406 JsepIceCandidate jice2("audio", 0, candidate2);
407 JsepIceCandidate jice3("video", 0, candidate1);
408 JsepIceCandidate jice4("video", 0, candidate2);
409 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
410 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
411 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
412 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
413
414 std::string message = Serialize(jsep_desc_.get());
415 EXPECT_EQ(message.find("c=IN IP6 ::3"), std::string::npos);
416 // Should have a c line like this one when no any default exists.
417 EXPECT_NE(message.find("c=IN IP4 0.0.0.0"), std::string::npos);
418}
419
420// Tests that the connection address will be correctly set when the Candidate is
421// removed.
422TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) {
423 cricket::Candidate candidate1(
424 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
425 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
426 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
427 candidate1.set_transport_name("audio");
428
429 cricket::Candidate candidate2(
430 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
431 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
432 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
433 candidate2.set_transport_name("audio");
434
435 cricket::Candidate candidate3(
436 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
437 rtc::SocketAddress("192.168.1.1", 1236), kCandidatePriority, "", "",
438 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
439 candidate3.set_transport_name("audio");
440
441 JsepIceCandidate jice1("audio", 0, candidate1);
442 JsepIceCandidate jice2("audio", 0, candidate2);
443 JsepIceCandidate jice3("audio", 0, candidate3);
444
445 size_t audio_index = 0;
Steve Antonb1c1de12017-12-21 15:14:30 -0800446 auto media_desc =
447 jsep_desc_->description()->contents()[audio_index].media_description();
zhihuang38989e52017-03-21 11:04:53 -0700448
449 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
450 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
451 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
452
453 std::vector<cricket::Candidate> candidates;
454 EXPECT_EQ("192.168.1.1:1236", media_desc->connection_address().ToString());
455
456 candidates.push_back(candidate3);
457 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
458 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
459
460 candidates.clear();
461 candidates.push_back(candidate2);
462 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
463 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
464
465 candidates.clear();
466 candidates.push_back(candidate1);
467 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
468 EXPECT_EQ("0.0.0.0:9", media_desc->connection_address().ToString());
469}
Steve Anton88f2cb92017-12-05 12:47:32 -0800470
471class EnumerateAllSdpTypesTest : public ::testing::Test,
472 public ::testing::WithParamInterface<SdpType> {
473};
474
475TEST_P(EnumerateAllSdpTypesTest, TestIdentity) {
476 SdpType type = GetParam();
477
478 const char* str = webrtc::SdpTypeToString(type);
479 EXPECT_EQ(type, webrtc::SdpTypeFromString(str));
480}
481
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100482INSTANTIATE_TEST_SUITE_P(JsepSessionDescriptionTest,
483 EnumerateAllSdpTypesTest,
484 Values(SdpType::kOffer,
485 SdpType::kPrAnswer,
486 SdpType::kAnswer));