blob: 8abb500480c9d430ea92630678f0a64f4ea6881c [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);
226 JsepIceCandidate hostname_candidate("audio", 0, c);
227 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate));
228 ASSERT_NE(nullptr, jsep_desc_->description());
229 const auto& content = jsep_desc_->description()->contents()[0];
230 EXPECT_EQ("example.local:1234",
231 content.media_description()->connection_address().ToString());
232}
233
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234// Test that we can serialize a JsepSessionDescription and deserialize it again.
235TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
236 std::string sdp = Serialize(jsep_desc_.get());
237
Steve Antona3a92c22017-12-07 10:27:41 -0800238 auto parsed_jsep_desc = DeSerialize(sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
240
241 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
242 EXPECT_EQ(sdp, parsed_sdp);
243}
244
245// Tests that we can serialize and deserialize a JsepSesssionDescription
246// with candidates.
247TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
248 std::string sdp = Serialize(jsep_desc_.get());
249
250 // Add a candidate and check that the serialized result is different.
251 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
252 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
253 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
254 EXPECT_NE(sdp, sdp_with_candidate);
255
Steve Antona3a92c22017-12-07 10:27:41 -0800256 auto parsed_jsep_desc = DeSerialize(sdp_with_candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
258
259 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
260}
zhihuang38989e52017-03-21 11:04:53 -0700261
262// TODO(zhihuang): Modify these tests. These are used to verify that after
263// adding the candidates, the connection_address field is set correctly. Modify
264// those so that the "connection address" is tested directly.
265// Tests serialization of SDP with only IPv6 candidates and verifies that IPv6
266// is used as default address in c line according to preference.
267TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithIPv6Only) {
268 // Stun has a high preference than local host.
269 cricket::Candidate candidate1(
270 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
271 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
272 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
273 cricket::Candidate candidate2(
274 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
275 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
276 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
277
278 JsepIceCandidate jice1("audio", 0, candidate1);
279 JsepIceCandidate jice2("audio", 0, candidate2);
280 JsepIceCandidate jice3("video", 0, candidate1);
281 JsepIceCandidate jice4("video", 0, candidate2);
282 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
283 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
284 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
285 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
286 std::string message = Serialize(jsep_desc_.get());
287
288 // Should have a c line like this one.
289 EXPECT_NE(message.find("c=IN IP6 ::1"), std::string::npos);
290 // Shouldn't have a IP4 c line.
291 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
292}
293
294// Tests serialization of SDP with both IPv4 and IPv6 candidates and
295// verifies that IPv4 is used as default address in c line even if the
296// preference of IPv4 is lower.
297TEST_F(JsepSessionDescriptionTest,
298 SerializeSessionDescriptionWithBothIPFamilies) {
299 cricket::Candidate candidate_v4(
300 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
301 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
302 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
303 cricket::Candidate candidate_v6(
304 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
305 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
306 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
307
308 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
309 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
310 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
311 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
312 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
313 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
314 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
315 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
316 std::string message = Serialize(jsep_desc_.get());
317
318 // Should have a c line like this one.
319 EXPECT_NE(message.find("c=IN IP4 192.168.1.5"), std::string::npos);
320 // Shouldn't have a IP6 c line.
321 EXPECT_EQ(message.find("c=IN IP6"), std::string::npos);
322}
323
324// Tests serialization of SDP with both UDP and TCP candidates and
325// verifies that UDP is used as default address in c line even if the
326// preference of UDP is lower.
327TEST_F(JsepSessionDescriptionTest,
328 SerializeSessionDescriptionWithBothProtocols) {
329 // Stun has a high preference than local host.
330 cricket::Candidate candidate1(
331 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
332 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
333 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
334 cricket::Candidate candidate2(
335 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
336 rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority,
337 "", "", cricket::LOCAL_PORT_TYPE, kCandidateGeneration,
338 kCandidateFoundation);
339
340 JsepIceCandidate jice1("audio", 0, candidate1);
341 JsepIceCandidate jice2("audio", 0, candidate2);
342 JsepIceCandidate jice3("video", 0, candidate1);
343 JsepIceCandidate jice4("video", 0, candidate2);
344 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
345 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
346 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
347 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
348 std::string message = Serialize(jsep_desc_.get());
349
350 // Should have a c line like this one.
351 EXPECT_NE(message.find("c=IN IP6 fe80::1234:5678:abcd:ef12"),
352 std::string::npos);
353 // Shouldn't have a IP4 c line.
354 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
355}
356
357// Tests serialization of SDP with only TCP candidates and verifies that
358// null IPv4 is used as default address in c line.
359TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithTCPOnly) {
360 // Stun has a high preference than local host.
361 cricket::Candidate candidate1(
362 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
363 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
364 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
365 cricket::Candidate candidate2(
366 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
367 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
368 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
369
370 JsepIceCandidate jice1("audio", 0, candidate1);
371 JsepIceCandidate jice2("audio", 0, candidate2);
372 JsepIceCandidate jice3("video", 0, candidate1);
373 JsepIceCandidate jice4("video", 0, candidate2);
374 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
375 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
376 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
377 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
378
379 std::string message = Serialize(jsep_desc_.get());
380 EXPECT_EQ(message.find("c=IN IP6 ::3"), std::string::npos);
381 // Should have a c line like this one when no any default exists.
382 EXPECT_NE(message.find("c=IN IP4 0.0.0.0"), std::string::npos);
383}
384
385// Tests that the connection address will be correctly set when the Candidate is
386// removed.
387TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) {
388 cricket::Candidate candidate1(
389 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
390 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
391 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
392 candidate1.set_transport_name("audio");
393
394 cricket::Candidate candidate2(
395 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
396 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
397 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
398 candidate2.set_transport_name("audio");
399
400 cricket::Candidate candidate3(
401 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
402 rtc::SocketAddress("192.168.1.1", 1236), kCandidatePriority, "", "",
403 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
404 candidate3.set_transport_name("audio");
405
406 JsepIceCandidate jice1("audio", 0, candidate1);
407 JsepIceCandidate jice2("audio", 0, candidate2);
408 JsepIceCandidate jice3("audio", 0, candidate3);
409
410 size_t audio_index = 0;
Steve Antonb1c1de12017-12-21 15:14:30 -0800411 auto media_desc =
412 jsep_desc_->description()->contents()[audio_index].media_description();
zhihuang38989e52017-03-21 11:04:53 -0700413
414 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
415 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
416 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
417
418 std::vector<cricket::Candidate> candidates;
419 EXPECT_EQ("192.168.1.1:1236", media_desc->connection_address().ToString());
420
421 candidates.push_back(candidate3);
422 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
423 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
424
425 candidates.clear();
426 candidates.push_back(candidate2);
427 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
428 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
429
430 candidates.clear();
431 candidates.push_back(candidate1);
432 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
433 EXPECT_EQ("0.0.0.0:9", media_desc->connection_address().ToString());
434}
Steve Anton88f2cb92017-12-05 12:47:32 -0800435
436class EnumerateAllSdpTypesTest : public ::testing::Test,
437 public ::testing::WithParamInterface<SdpType> {
438};
439
440TEST_P(EnumerateAllSdpTypesTest, TestIdentity) {
441 SdpType type = GetParam();
442
443 const char* str = webrtc::SdpTypeToString(type);
444 EXPECT_EQ(type, webrtc::SdpTypeFromString(str));
445}
446
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100447INSTANTIATE_TEST_SUITE_P(JsepSessionDescriptionTest,
448 EnumerateAllSdpTypesTest,
449 Values(SdpType::kOffer,
450 SdpType::kPrAnswer,
451 SdpType::kAnswer));