blob: ef86ef41fb9a07aae2e76b4052345f076af14872 [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
Karl Wiberg918f50c2018-07-05 11:40:33 +020021#include "absl/memory/memory.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020022#include "api/candidate.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "api/jsep_ice_candidate.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "p2p/base/p2p_constants.h"
Steve Antona3a92c22017-12-07 10:27:41 -080027#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "p2p/base/transport_description.h"
29#include "p2p/base/transport_info.h"
30#include "pc/session_description.h"
31#include "pc/webrtc_sdp.h"
Yves Gerey3e707812018-11-28 16:47:49 +010032#include "rtc_base/helpers.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/socket_address.h"
34#include "rtc_base/string_encode.h"
Yves Gerey3e707812018-11-28 16:47:49 +010035#include "test/gtest.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
Steve Anton5adfafd2017-12-20 16:34:00 -080037using cricket::MediaProtocolType;
Steve Anton88f2cb92017-12-05 12:47:32 -080038using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039using webrtc::IceCandidateCollection;
40using webrtc::IceCandidateInterface;
41using webrtc::JsepIceCandidate;
42using webrtc::JsepSessionDescription;
Steve Anton88f2cb92017-12-05 12:47:32 -080043using webrtc::SdpType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044using webrtc::SessionDescriptionInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46static const char kCandidateUfrag[] = "ufrag";
47static const char kCandidatePwd[] = "pwd";
48static const char kCandidateUfragVoice[] = "ufrag_voice";
49static const char kCandidatePwdVoice[] = "pwd_voice";
50static const char kCandidateUfragVideo[] = "ufrag_video";
51static const char kCandidatePwdVideo[] = "pwd_video";
zhihuang38989e52017-03-21 11:04:53 -070052static const char kCandidateFoundation[] = "a0+B/1";
53static const uint32_t kCandidatePriority = 2130706432U; // pref = 1.0
54static const uint32_t kCandidateGeneration = 2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055
56// This creates a session description with both audio and video media contents.
57// In SDP this is described by two m lines, one audio and one video.
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020058static std::unique_ptr<cricket::SessionDescription>
59CreateCricketSessionDescription() {
60 auto desc = absl::make_unique<cricket::SessionDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020062 // AudioContentDescription
63 auto audio = absl::make_unique<cricket::AudioContentDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 // VideoContentDescription
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020065 auto video = absl::make_unique<cricket::VideoContentDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
deadbeef67cf2c12016-04-13 10:07:16 -070067 audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0));
Harald Alvestrand1716d392019-06-03 20:35:45 +020068 desc->AddContent(cricket::CN_AUDIO, MediaProtocolType::kRtp,
69 std::move(audio));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
perkj26752742016-10-24 01:21:16 -070071 video->AddCodec(cricket::VideoCodec(120, "VP8"));
Harald Alvestrand1716d392019-06-03 20:35:45 +020072 desc->AddContent(cricket::CN_VIDEO, MediaProtocolType::kRtp,
73 std::move(video));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
Steve Anton06817cd2018-12-18 15:55:30 -080075 desc->AddTransportInfo(cricket::TransportInfo(
deadbeef46eed762016-01-28 13:24:37 -080076 cricket::CN_AUDIO,
77 cricket::TransportDescription(
78 std::vector<std::string>(), kCandidateUfragVoice, kCandidatePwdVoice,
Steve Anton06817cd2018-12-18 15:55:30 -080079 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)));
80 desc->AddTransportInfo(cricket::TransportInfo(
deadbeef46eed762016-01-28 13:24:37 -080081 cricket::CN_VIDEO,
82 cricket::TransportDescription(
83 std::vector<std::string>(), kCandidateUfragVideo, kCandidatePwdVideo,
Steve Anton06817cd2018-12-18 15:55:30 -080084 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 return desc;
86}
87
Mirko Bonadei6a489f22019-04-09 15:11:12 +020088class JsepSessionDescriptionTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 virtual void SetUp() {
91 int port = 1234;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092 rtc::SocketAddress address("127.0.0.1", port++);
guoweis@webrtc.org61c12472015-01-15 06:53:07 +000093 cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
94 address, 1, "", "", "local", 0, "1");
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 candidate_ = candidate;
Yves Gerey665174f2018-06-19 15:03:05 +020096 const std::string session_id = rtc::ToString(rtc::CreateRandomId64());
97 const std::string session_version = rtc::ToString(rtc::CreateRandomId());
Karl Wiberg918f50c2018-07-05 11:40:33 +020098 jsep_desc_ = absl::make_unique<JsepSessionDescription>(SdpType::kOffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(),
Yves Gerey665174f2018-06-19 15:03:05 +0200100 session_id, session_version));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 }
102
103 std::string Serialize(const SessionDescriptionInterface* desc) {
104 std::string sdp;
105 EXPECT_TRUE(desc->ToString(&sdp));
106 EXPECT_FALSE(sdp.empty());
107 return sdp;
108 }
109
Steve Antona3a92c22017-12-07 10:27:41 -0800110 std::unique_ptr<SessionDescriptionInterface> DeSerialize(
111 const std::string& sdp) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200112 auto jsep_desc = absl::make_unique<JsepSessionDescription>(SdpType::kOffer);
Steve Antona3a92c22017-12-07 10:27:41 -0800113 EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jsep_desc.get(), nullptr));
114 return std::move(jsep_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 }
116
117 cricket::Candidate candidate_;
kwibergd1fe2812016-04-27 06:47:29 -0700118 std::unique_ptr<JsepSessionDescription> jsep_desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119};
120
121// Test that number_of_mediasections() returns the number of media contents in
122// a session description.
123TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
124 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
125}
126
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700127// Test that we can add a candidate to a session description without MID.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
129 JsepIceCandidate jsep_candidate("", 0, candidate_);
130 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
131 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
132 ASSERT_TRUE(ice_candidates != NULL);
133 EXPECT_EQ(1u, ice_candidates->count());
134 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
135 ASSERT_TRUE(ice_candidate != NULL);
136 candidate_.set_username(kCandidateUfragVoice);
137 candidate_.set_password(kCandidatePwdVoice);
138 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
139 EXPECT_EQ(0, ice_candidate->sdp_mline_index());
140 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
141}
142
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700143// Test that we can add and remove candidates to a session description with
144// MID. Removing candidates requires MID (transport_name).
145TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 // mid and m-line index don't match, in this case mid is preferred.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700147 std::string mid = "video";
148 JsepIceCandidate jsep_candidate(mid, 0, candidate_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
150 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
151 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
152 ASSERT_TRUE(ice_candidates != NULL);
153 EXPECT_EQ(1u, ice_candidates->count());
154 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
155 ASSERT_TRUE(ice_candidate != NULL);
156 candidate_.set_username(kCandidateUfragVideo);
157 candidate_.set_password(kCandidatePwdVideo);
158 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
159 // The mline index should have been updated according to mid.
160 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700161
162 std::vector<cricket::Candidate> candidates(1, candidate_);
163 candidates[0].set_transport_name(mid);
164 EXPECT_EQ(1u, jsep_desc_->RemoveCandidates(candidates));
165 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
166 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167}
168
169TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
170 candidate_.set_username(kCandidateUfrag);
171 candidate_.set_password(kCandidatePwd);
172 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
173 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
174 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
175 ASSERT_TRUE(ice_candidates != NULL);
176 EXPECT_EQ(1u, ice_candidates->count());
177 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
178 ASSERT_TRUE(ice_candidate != NULL);
179 candidate_.set_username(kCandidateUfrag);
180 candidate_.set_password(kCandidatePwd);
181 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
182
183 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
184}
185
186// Test that we can not add a candidate if there is no corresponding media
187// content in the session description.
188TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
189 JsepIceCandidate bad_candidate1("", 55, candidate_);
190 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
191
192 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
193 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
194}
195
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000196// Tests that repeatedly adding the same candidate, with or without credentials,
197// does not increase the number of candidates in the description.
198TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
199 JsepIceCandidate jsep_candidate("", 0, candidate_);
200 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
201 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
202
203 // Add the same candidate again. It should be ignored.
204 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
205 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
206
207 // Create a new candidate, identical except that the ufrag and pwd are now
208 // populated.
209 candidate_.set_username(kCandidateUfragVoice);
210 candidate_.set_password(kCandidatePwdVoice);
211 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
212
213 // This should also be identified as redundant and ignored.
214 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
215 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
216}
217
Qingsi Wang56991422019-02-08 12:53:06 -0800218// Test that the connection address is set to a hostname address after adding a
219// hostname candidate.
220TEST_F(JsepSessionDescriptionTest, AddHostnameCandidate) {
221 cricket::Candidate c;
222 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
223 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
224 c.set_address(rtc::SocketAddress("example.local", 1234));
225 c.set_type(cricket::LOCAL_PORT_TYPE);
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700226 const size_t audio_index = 0;
227 JsepIceCandidate hostname_candidate("audio", audio_index, c);
Qingsi Wang56991422019-02-08 12:53:06 -0800228 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate));
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700229
Qingsi Wang56991422019-02-08 12:53:06 -0800230 ASSERT_NE(nullptr, jsep_desc_->description());
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700231 ASSERT_EQ(2u, jsep_desc_->description()->contents().size());
232 const auto& content = jsep_desc_->description()->contents()[audio_index];
233 EXPECT_EQ("0.0.0.0:9",
Qingsi Wang56991422019-02-08 12:53:06 -0800234 content.media_description()->connection_address().ToString());
235}
236
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237// Test that we can serialize a JsepSessionDescription and deserialize it again.
238TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
239 std::string sdp = Serialize(jsep_desc_.get());
240
Steve Antona3a92c22017-12-07 10:27:41 -0800241 auto parsed_jsep_desc = DeSerialize(sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
243
244 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
245 EXPECT_EQ(sdp, parsed_sdp);
246}
247
Qingsi Wang3ae59d32019-07-11 21:53:45 -0700248// Test that we can serialize a JsepSessionDescription when a hostname candidate
249// is the default destination and deserialize it again. The connection address
250// in the deserialized description should be the dummy address 0.0.0.0:9.
251TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithHostnameCandidate) {
252 cricket::Candidate c;
253 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
254 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
255 c.set_address(rtc::SocketAddress("example.local", 1234));
256 c.set_type(cricket::LOCAL_PORT_TYPE);
257 const size_t audio_index = 0;
258 const size_t video_index = 1;
259 JsepIceCandidate hostname_candidate_audio("audio", audio_index, c);
260 JsepIceCandidate hostname_candidate_video("video", video_index, c);
261 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_audio));
262 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_video));
263
264 std::string sdp = Serialize(jsep_desc_.get());
265
266 auto parsed_jsep_desc = DeSerialize(sdp);
267 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
268
269 ASSERT_NE(nullptr, parsed_jsep_desc->description());
270 ASSERT_EQ(2u, parsed_jsep_desc->description()->contents().size());
271 const auto& audio_content =
272 parsed_jsep_desc->description()->contents()[audio_index];
273 const auto& video_content =
274 parsed_jsep_desc->description()->contents()[video_index];
275 EXPECT_EQ("0.0.0.0:9",
276 audio_content.media_description()->connection_address().ToString());
277 EXPECT_EQ("0.0.0.0:9",
278 video_content.media_description()->connection_address().ToString());
279}
280
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281// Tests that we can serialize and deserialize a JsepSesssionDescription
282// with candidates.
283TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
284 std::string sdp = Serialize(jsep_desc_.get());
285
286 // Add a candidate and check that the serialized result is different.
287 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
288 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
289 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
290 EXPECT_NE(sdp, sdp_with_candidate);
291
Steve Antona3a92c22017-12-07 10:27:41 -0800292 auto parsed_jsep_desc = DeSerialize(sdp_with_candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
294
295 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
296}
zhihuang38989e52017-03-21 11:04:53 -0700297
298// TODO(zhihuang): Modify these tests. These are used to verify that after
299// adding the candidates, the connection_address field is set correctly. Modify
300// those so that the "connection address" is tested directly.
301// Tests serialization of SDP with only IPv6 candidates and verifies that IPv6
302// is used as default address in c line according to preference.
303TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithIPv6Only) {
304 // Stun has a high preference than local host.
305 cricket::Candidate candidate1(
306 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
307 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
308 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
309 cricket::Candidate candidate2(
310 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
311 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
312 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, 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 ::1"), std::string::npos);
326 // Shouldn't have a IP4 c line.
327 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
328}
329
330// Tests serialization of SDP with both IPv4 and IPv6 candidates and
331// verifies that IPv4 is used as default address in c line even if the
332// preference of IPv4 is lower.
333TEST_F(JsepSessionDescriptionTest,
334 SerializeSessionDescriptionWithBothIPFamilies) {
335 cricket::Candidate candidate_v4(
336 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
337 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
338 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
339 cricket::Candidate candidate_v6(
340 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
341 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
342 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
343
344 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
345 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
346 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
347 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
348 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
349 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
350 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
351 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
352 std::string message = Serialize(jsep_desc_.get());
353
354 // Should have a c line like this one.
355 EXPECT_NE(message.find("c=IN IP4 192.168.1.5"), std::string::npos);
356 // Shouldn't have a IP6 c line.
357 EXPECT_EQ(message.find("c=IN IP6"), std::string::npos);
358}
359
360// Tests serialization of SDP with both UDP and TCP candidates and
361// verifies that UDP is used as default address in c line even if the
362// preference of UDP is lower.
363TEST_F(JsepSessionDescriptionTest,
364 SerializeSessionDescriptionWithBothProtocols) {
365 // Stun has a high preference than local host.
366 cricket::Candidate candidate1(
367 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
368 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
369 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
370 cricket::Candidate candidate2(
371 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
372 rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority,
373 "", "", cricket::LOCAL_PORT_TYPE, kCandidateGeneration,
374 kCandidateFoundation);
375
376 JsepIceCandidate jice1("audio", 0, candidate1);
377 JsepIceCandidate jice2("audio", 0, candidate2);
378 JsepIceCandidate jice3("video", 0, candidate1);
379 JsepIceCandidate jice4("video", 0, candidate2);
380 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
381 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
382 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
383 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
384 std::string message = Serialize(jsep_desc_.get());
385
386 // Should have a c line like this one.
387 EXPECT_NE(message.find("c=IN IP6 fe80::1234:5678:abcd:ef12"),
388 std::string::npos);
389 // Shouldn't have a IP4 c line.
390 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
391}
392
393// Tests serialization of SDP with only TCP candidates and verifies that
394// null IPv4 is used as default address in c line.
395TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithTCPOnly) {
396 // Stun has a high preference than local host.
397 cricket::Candidate candidate1(
398 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
399 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
400 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
401 cricket::Candidate candidate2(
402 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
403 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
404 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
405
406 JsepIceCandidate jice1("audio", 0, candidate1);
407 JsepIceCandidate jice2("audio", 0, candidate2);
408 JsepIceCandidate jice3("video", 0, candidate1);
409 JsepIceCandidate jice4("video", 0, candidate2);
410 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
411 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
412 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
413 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
414
415 std::string message = Serialize(jsep_desc_.get());
416 EXPECT_EQ(message.find("c=IN IP6 ::3"), std::string::npos);
417 // Should have a c line like this one when no any default exists.
418 EXPECT_NE(message.find("c=IN IP4 0.0.0.0"), std::string::npos);
419}
420
421// Tests that the connection address will be correctly set when the Candidate is
422// removed.
423TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) {
424 cricket::Candidate candidate1(
425 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
426 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
427 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
428 candidate1.set_transport_name("audio");
429
430 cricket::Candidate candidate2(
431 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
432 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
433 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
434 candidate2.set_transport_name("audio");
435
436 cricket::Candidate candidate3(
437 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
438 rtc::SocketAddress("192.168.1.1", 1236), kCandidatePriority, "", "",
439 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
440 candidate3.set_transport_name("audio");
441
442 JsepIceCandidate jice1("audio", 0, candidate1);
443 JsepIceCandidate jice2("audio", 0, candidate2);
444 JsepIceCandidate jice3("audio", 0, candidate3);
445
446 size_t audio_index = 0;
Steve Antonb1c1de12017-12-21 15:14:30 -0800447 auto media_desc =
448 jsep_desc_->description()->contents()[audio_index].media_description();
zhihuang38989e52017-03-21 11:04:53 -0700449
450 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
451 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
452 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
453
454 std::vector<cricket::Candidate> candidates;
455 EXPECT_EQ("192.168.1.1:1236", media_desc->connection_address().ToString());
456
457 candidates.push_back(candidate3);
458 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
459 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
460
461 candidates.clear();
462 candidates.push_back(candidate2);
463 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
464 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
465
466 candidates.clear();
467 candidates.push_back(candidate1);
468 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
469 EXPECT_EQ("0.0.0.0:9", media_desc->connection_address().ToString());
470}
Steve Anton88f2cb92017-12-05 12:47:32 -0800471
472class EnumerateAllSdpTypesTest : public ::testing::Test,
473 public ::testing::WithParamInterface<SdpType> {
474};
475
476TEST_P(EnumerateAllSdpTypesTest, TestIdentity) {
477 SdpType type = GetParam();
478
479 const char* str = webrtc::SdpTypeToString(type);
480 EXPECT_EQ(type, webrtc::SdpTypeFromString(str));
481}
482
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100483INSTANTIATE_TEST_SUITE_P(JsepSessionDescriptionTest,
484 EnumerateAllSdpTypesTest,
485 Values(SdpType::kOffer,
486 SdpType::kPrAnswer,
487 SdpType::kAnswer));