blob: c3e1f330e4b9bff368f602b2629c8ecd234dfa4f [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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:369 */
10
Jonas Olssona4d87372019-07-05 17:08:3311#include "api/jsep_session_description.h"
12
Yves Gerey3e707812018-11-28 15:47:4913#include <stddef.h>
14#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Yves Gerey3e707812018-11-28 15:47:4916#include <utility>
17#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:3618
Patrik Höglunde2d6a062017-10-05 12:53:3319#include "api/candidate.h"
Yves Gerey3e707812018-11-28 15:47:4920#include "api/jsep.h"
Steve Anton10542f22019-01-11 17:11:0021#include "api/jsep_ice_candidate.h"
Yves Gerey3e707812018-11-28 15:47:4922#include "media/base/codec.h"
Steve Anton10542f22019-01-11 17:11:0023#include "p2p/base/p2p_constants.h"
Steve Antona3a92c22017-12-07 18:27:4124#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 17:11:0025#include "p2p/base/transport_description.h"
26#include "p2p/base/transport_info.h"
27#include "pc/session_description.h"
28#include "pc/webrtc_sdp.h"
Yves Gerey3e707812018-11-28 15:47:4929#include "rtc_base/helpers.h"
Harald Alvestrandc24a2182022-02-23 13:44:5930#include "rtc_base/net_helper.h"
Steve Anton10542f22019-01-11 17:11:0031#include "rtc_base/socket_address.h"
32#include "rtc_base/string_encode.h"
Yves Gerey3e707812018-11-28 15:47:4933#include "test/gtest.h"
henrike@webrtc.org28e20752013-07-10 00:45:3634
Steve Anton5adfafd2017-12-21 00:34:0035using cricket::MediaProtocolType;
Steve Anton88f2cb92017-12-05 20:47:3236using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:3637using webrtc::IceCandidateCollection;
38using webrtc::IceCandidateInterface;
Tomas Gunnarsson02429392024-03-07 15:24:5539using webrtc::IceCandidateType;
henrike@webrtc.org28e20752013-07-10 00:45:3640using webrtc::JsepIceCandidate;
41using webrtc::JsepSessionDescription;
Steve Anton88f2cb92017-12-05 20:47:3242using webrtc::SdpType;
henrike@webrtc.org28e20752013-07-10 00:45:3643using webrtc::SessionDescriptionInterface;
henrike@webrtc.org28e20752013-07-10 00:45:3644
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 18:04:5351static 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:3654
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 05:01:2957static std::unique_ptr<cricket::SessionDescription>
58CreateCricketSessionDescription() {
Mirko Bonadei317a1f02019-09-17 15:06:1859 auto desc = std::make_unique<cricket::SessionDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:3660
Harald Alvestrand4d7160e2019-04-12 05:01:2961 // AudioContentDescription
Mirko Bonadei317a1f02019-09-17 15:06:1862 auto audio = std::make_unique<cricket::AudioContentDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:3663 // VideoContentDescription
Mirko Bonadei317a1f02019-09-17 15:06:1864 auto video = std::make_unique<cricket::VideoContentDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:3665
Florent Castelli8c4b9ea2023-06-02 16:06:5766 audio->AddCodec(cricket::CreateAudioCodec(103, "ISAC", 16000, 0));
Harald Alvestrand1716d392019-06-03 18:35:4567 desc->AddContent(cricket::CN_AUDIO, MediaProtocolType::kRtp,
68 std::move(audio));
henrike@webrtc.org28e20752013-07-10 00:45:3669
Florent Castelli8c4b9ea2023-06-02 16:06:5770 video->AddCodec(cricket::CreateVideoCodec(120, "VP8"));
Harald Alvestrand1716d392019-06-03 18:35:4571 desc->AddContent(cricket::CN_VIDEO, MediaProtocolType::kRtp,
72 std::move(video));
henrike@webrtc.org28e20752013-07-10 00:45:3673
Steve Anton06817cd2018-12-18 23:55:3074 desc->AddTransportInfo(cricket::TransportInfo(
deadbeef46eed762016-01-28 21:24:3775 cricket::CN_AUDIO,
76 cricket::TransportDescription(
77 std::vector<std::string>(), kCandidateUfragVoice, kCandidatePwdVoice,
Steve Anton06817cd2018-12-18 23:55:3078 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)));
79 desc->AddTransportInfo(cricket::TransportInfo(
deadbeef46eed762016-01-28 21:24:3780 cricket::CN_VIDEO,
81 cricket::TransportDescription(
82 std::vector<std::string>(), kCandidateUfragVideo, kCandidatePwdVideo,
Steve Anton06817cd2018-12-18 23:55:3083 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:3684 return desc;
85}
86
Mirko Bonadei6a489f22019-04-09 13:11:1287class JsepSessionDescriptionTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:3688 protected:
henrike@webrtc.org28e20752013-07-10 00:45:3689 virtual void SetUp() {
90 int port = 1234;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5291 rtc::SocketAddress address("127.0.0.1", port++);
guoweis@webrtc.org61c12472015-01-15 06:53:0792 cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
Tomas Gunnarsson02429392024-03-07 15:24:5593 address, 1, "", "", IceCandidateType::kHost, 0,
94 "1");
henrike@webrtc.org28e20752013-07-10 00:45:3695 candidate_ = candidate;
Yves Gerey665174f2018-06-19 13:03:0596 const std::string session_id = rtc::ToString(rtc::CreateRandomId64());
97 const std::string session_version = rtc::ToString(rtc::CreateRandomId());
Mirko Bonadei317a1f02019-09-17 15:06:1898 jsep_desc_ = std::make_unique<JsepSessionDescription>(SdpType::kOffer);
henrike@webrtc.org28e20752013-07-10 00:45:3699 ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(),
Yves Gerey665174f2018-06-19 13:03:05100 session_id, session_version));
henrike@webrtc.org28e20752013-07-10 00:45:36101 }
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 18:27:41110 std::unique_ptr<SessionDescriptionInterface> DeSerialize(
111 const std::string& sdp) {
Mirko Bonadei317a1f02019-09-17 15:06:18112 auto jsep_desc = std::make_unique<JsepSessionDescription>(SdpType::kOffer);
Steve Antona3a92c22017-12-07 18:27:41113 EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jsep_desc.get(), nullptr));
114 return std::move(jsep_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36115 }
116
117 cricket::Candidate candidate_;
kwibergd1fe2812016-04-27 13:47:29118 std::unique_ptr<JsepSessionDescription> jsep_desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36119};
120
Harald Alvestrand0e7b3a92020-12-15 15:18:28121TEST_F(JsepSessionDescriptionTest, CloneDefault) {
122 auto new_desc = jsep_desc_->Clone();
123 EXPECT_EQ(jsep_desc_->type(), new_desc->type());
124 std::string old_desc_string;
125 std::string new_desc_string;
126 EXPECT_TRUE(jsep_desc_->ToString(&old_desc_string));
127 EXPECT_TRUE(new_desc->ToString(&new_desc_string));
128 EXPECT_EQ(old_desc_string, new_desc_string);
129 EXPECT_EQ(jsep_desc_->session_id(), new_desc->session_id());
130 EXPECT_EQ(jsep_desc_->session_version(), new_desc->session_version());
131}
132
Byoungchan Leecb42e102021-07-15 05:09:40133TEST_F(JsepSessionDescriptionTest, CloneRollback) {
134 auto jsep_desc = std::make_unique<JsepSessionDescription>(SdpType::kRollback);
135 auto new_desc = jsep_desc->Clone();
136 EXPECT_EQ(jsep_desc->type(), new_desc->type());
137}
138
Harald Alvestrandfc6b8712021-01-05 10:51:08139TEST_F(JsepSessionDescriptionTest, CloneWithCandidates) {
140 cricket::Candidate candidate_v4(
141 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
142 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55143 IceCandidateType::kSrflx, kCandidateGeneration, kCandidateFoundation);
Harald Alvestrandfc6b8712021-01-05 10:51:08144 cricket::Candidate candidate_v6(
145 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
146 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55147 IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation);
Harald Alvestrandfc6b8712021-01-05 10:51:08148
149 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
150 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
151 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
152 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
153 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
154 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
155 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
156 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
157 auto new_desc = jsep_desc_->Clone();
158 EXPECT_EQ(jsep_desc_->type(), new_desc->type());
159 std::string old_desc_string;
160 std::string new_desc_string;
161 EXPECT_TRUE(jsep_desc_->ToString(&old_desc_string));
162 EXPECT_TRUE(new_desc->ToString(&new_desc_string));
163 EXPECT_EQ(old_desc_string, new_desc_string);
164}
165
henrike@webrtc.org28e20752013-07-10 00:45:36166// Test that number_of_mediasections() returns the number of media contents in
167// a session description.
168TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
169 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
170}
171
Honghai Zhang7fb69db2016-03-14 18:59:18172// Test that we can add a candidate to a session description without MID.
henrike@webrtc.org28e20752013-07-10 00:45:36173TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
174 JsepIceCandidate jsep_candidate("", 0, candidate_);
175 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
176 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
177 ASSERT_TRUE(ice_candidates != NULL);
178 EXPECT_EQ(1u, ice_candidates->count());
179 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
180 ASSERT_TRUE(ice_candidate != NULL);
181 candidate_.set_username(kCandidateUfragVoice);
182 candidate_.set_password(kCandidatePwdVoice);
183 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
184 EXPECT_EQ(0, ice_candidate->sdp_mline_index());
185 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
186}
187
Honghai Zhang7fb69db2016-03-14 18:59:18188// Test that we can add and remove candidates to a session description with
189// MID. Removing candidates requires MID (transport_name).
190TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) {
henrike@webrtc.org28e20752013-07-10 00:45:36191 // mid and m-line index don't match, in this case mid is preferred.
Honghai Zhang7fb69db2016-03-14 18:59:18192 std::string mid = "video";
193 JsepIceCandidate jsep_candidate(mid, 0, candidate_);
henrike@webrtc.org28e20752013-07-10 00:45:36194 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
195 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
196 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
197 ASSERT_TRUE(ice_candidates != NULL);
198 EXPECT_EQ(1u, ice_candidates->count());
199 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
200 ASSERT_TRUE(ice_candidate != NULL);
201 candidate_.set_username(kCandidateUfragVideo);
202 candidate_.set_password(kCandidatePwdVideo);
203 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
204 // The mline index should have been updated according to mid.
205 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
Honghai Zhang7fb69db2016-03-14 18:59:18206
207 std::vector<cricket::Candidate> candidates(1, candidate_);
208 candidates[0].set_transport_name(mid);
209 EXPECT_EQ(1u, jsep_desc_->RemoveCandidates(candidates));
210 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
211 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36212}
213
214TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
215 candidate_.set_username(kCandidateUfrag);
216 candidate_.set_password(kCandidatePwd);
217 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
218 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
219 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
220 ASSERT_TRUE(ice_candidates != NULL);
221 EXPECT_EQ(1u, ice_candidates->count());
222 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
223 ASSERT_TRUE(ice_candidate != NULL);
224 candidate_.set_username(kCandidateUfrag);
225 candidate_.set_password(kCandidatePwd);
226 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
227
228 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
229}
230
231// Test that we can not add a candidate if there is no corresponding media
232// content in the session description.
233TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
234 JsepIceCandidate bad_candidate1("", 55, candidate_);
235 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
236
237 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
238 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
239}
240
mallinath@webrtc.org67ee6b92014-02-03 16:57:16241// Tests that repeatedly adding the same candidate, with or without credentials,
242// does not increase the number of candidates in the description.
243TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
244 JsepIceCandidate jsep_candidate("", 0, candidate_);
245 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
246 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
247
248 // Add the same candidate again. It should be ignored.
249 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
250 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
251
252 // Create a new candidate, identical except that the ufrag and pwd are now
253 // populated.
254 candidate_.set_username(kCandidateUfragVoice);
255 candidate_.set_password(kCandidatePwdVoice);
256 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
257
258 // This should also be identified as redundant and ignored.
259 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
260 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
261}
262
Qingsi Wang56991422019-02-08 20:53:06263// Test that the connection address is set to a hostname address after adding a
264// hostname candidate.
265TEST_F(JsepSessionDescriptionTest, AddHostnameCandidate) {
266 cricket::Candidate c;
267 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
268 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
269 c.set_address(rtc::SocketAddress("example.local", 1234));
Tomas Gunnarsson02429392024-03-07 15:24:55270 c.set_type(IceCandidateType::kHost);
Qingsi Wang3ae59d32019-07-12 04:53:45271 const size_t audio_index = 0;
272 JsepIceCandidate hostname_candidate("audio", audio_index, c);
Qingsi Wang56991422019-02-08 20:53:06273 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate));
Qingsi Wang3ae59d32019-07-12 04:53:45274
Qingsi Wang56991422019-02-08 20:53:06275 ASSERT_NE(nullptr, jsep_desc_->description());
Qingsi Wang3ae59d32019-07-12 04:53:45276 ASSERT_EQ(2u, jsep_desc_->description()->contents().size());
277 const auto& content = jsep_desc_->description()->contents()[audio_index];
278 EXPECT_EQ("0.0.0.0:9",
Qingsi Wang56991422019-02-08 20:53:06279 content.media_description()->connection_address().ToString());
280}
281
henrike@webrtc.org28e20752013-07-10 00:45:36282// Test that we can serialize a JsepSessionDescription and deserialize it again.
283TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
284 std::string sdp = Serialize(jsep_desc_.get());
285
Steve Antona3a92c22017-12-07 18:27:41286 auto parsed_jsep_desc = DeSerialize(sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36287 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
288
289 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
290 EXPECT_EQ(sdp, parsed_sdp);
291}
292
Qingsi Wang3ae59d32019-07-12 04:53:45293// Test that we can serialize a JsepSessionDescription when a hostname candidate
294// is the default destination and deserialize it again. The connection address
295// in the deserialized description should be the dummy address 0.0.0.0:9.
296TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithHostnameCandidate) {
297 cricket::Candidate c;
298 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
299 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
300 c.set_address(rtc::SocketAddress("example.local", 1234));
Tomas Gunnarsson02429392024-03-07 15:24:55301 c.set_type(IceCandidateType::kHost);
Qingsi Wang3ae59d32019-07-12 04:53:45302 const size_t audio_index = 0;
303 const size_t video_index = 1;
304 JsepIceCandidate hostname_candidate_audio("audio", audio_index, c);
305 JsepIceCandidate hostname_candidate_video("video", video_index, c);
306 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_audio));
307 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_video));
308
309 std::string sdp = Serialize(jsep_desc_.get());
310
311 auto parsed_jsep_desc = DeSerialize(sdp);
312 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
313
314 ASSERT_NE(nullptr, parsed_jsep_desc->description());
315 ASSERT_EQ(2u, parsed_jsep_desc->description()->contents().size());
316 const auto& audio_content =
317 parsed_jsep_desc->description()->contents()[audio_index];
318 const auto& video_content =
319 parsed_jsep_desc->description()->contents()[video_index];
320 EXPECT_EQ("0.0.0.0:9",
321 audio_content.media_description()->connection_address().ToString());
322 EXPECT_EQ("0.0.0.0:9",
323 video_content.media_description()->connection_address().ToString());
324}
325
henrike@webrtc.org28e20752013-07-10 00:45:36326// Tests that we can serialize and deserialize a JsepSesssionDescription
327// with candidates.
328TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
329 std::string sdp = Serialize(jsep_desc_.get());
330
331 // Add a candidate and check that the serialized result is different.
332 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
333 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
334 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
335 EXPECT_NE(sdp, sdp_with_candidate);
336
Steve Antona3a92c22017-12-07 18:27:41337 auto parsed_jsep_desc = DeSerialize(sdp_with_candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36338 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
339
340 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
341}
zhihuang38989e52017-03-21 18:04:53342
343// TODO(zhihuang): Modify these tests. These are used to verify that after
344// adding the candidates, the connection_address field is set correctly. Modify
345// those so that the "connection address" is tested directly.
346// Tests serialization of SDP with only IPv6 candidates and verifies that IPv6
347// is used as default address in c line according to preference.
348TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithIPv6Only) {
349 // Stun has a high preference than local host.
350 cricket::Candidate candidate1(
351 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
352 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55353 IceCandidateType::kSrflx, kCandidateGeneration, kCandidateFoundation);
zhihuang38989e52017-03-21 18:04:53354 cricket::Candidate candidate2(
355 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
356 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55357 IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation);
zhihuang38989e52017-03-21 18:04:53358
359 JsepIceCandidate jice1("audio", 0, candidate1);
360 JsepIceCandidate jice2("audio", 0, candidate2);
361 JsepIceCandidate jice3("video", 0, candidate1);
362 JsepIceCandidate jice4("video", 0, candidate2);
363 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
364 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
365 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
366 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
367 std::string message = Serialize(jsep_desc_.get());
368
369 // Should have a c line like this one.
370 EXPECT_NE(message.find("c=IN IP6 ::1"), std::string::npos);
371 // Shouldn't have a IP4 c line.
372 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
373}
374
375// Tests serialization of SDP with both IPv4 and IPv6 candidates and
376// verifies that IPv4 is used as default address in c line even if the
377// preference of IPv4 is lower.
378TEST_F(JsepSessionDescriptionTest,
379 SerializeSessionDescriptionWithBothIPFamilies) {
380 cricket::Candidate candidate_v4(
381 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
382 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55383 IceCandidateType::kSrflx, kCandidateGeneration, kCandidateFoundation);
zhihuang38989e52017-03-21 18:04:53384 cricket::Candidate candidate_v6(
385 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
386 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55387 IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation);
zhihuang38989e52017-03-21 18:04:53388
389 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
390 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
391 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
392 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
393 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
394 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
395 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
396 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
397 std::string message = Serialize(jsep_desc_.get());
398
399 // Should have a c line like this one.
400 EXPECT_NE(message.find("c=IN IP4 192.168.1.5"), std::string::npos);
401 // Shouldn't have a IP6 c line.
402 EXPECT_EQ(message.find("c=IN IP6"), std::string::npos);
403}
404
405// Tests serialization of SDP with both UDP and TCP candidates and
406// verifies that UDP is used as default address in c line even if the
407// preference of UDP is lower.
408TEST_F(JsepSessionDescriptionTest,
409 SerializeSessionDescriptionWithBothProtocols) {
410 // Stun has a high preference than local host.
411 cricket::Candidate candidate1(
412 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
413 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55414 IceCandidateType::kSrflx, kCandidateGeneration, kCandidateFoundation);
zhihuang38989e52017-03-21 18:04:53415 cricket::Candidate candidate2(
416 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
417 rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority,
Tomas Gunnarsson02429392024-03-07 15:24:55418 "", "", IceCandidateType::kHost, kCandidateGeneration,
zhihuang38989e52017-03-21 18:04:53419 kCandidateFoundation);
420
421 JsepIceCandidate jice1("audio", 0, candidate1);
422 JsepIceCandidate jice2("audio", 0, candidate2);
423 JsepIceCandidate jice3("video", 0, candidate1);
424 JsepIceCandidate jice4("video", 0, candidate2);
425 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
426 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
427 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
428 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
429 std::string message = Serialize(jsep_desc_.get());
430
431 // Should have a c line like this one.
432 EXPECT_NE(message.find("c=IN IP6 fe80::1234:5678:abcd:ef12"),
433 std::string::npos);
434 // Shouldn't have a IP4 c line.
435 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
436}
437
438// Tests serialization of SDP with only TCP candidates and verifies that
439// null IPv4 is used as default address in c line.
440TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithTCPOnly) {
441 // Stun has a high preference than local host.
442 cricket::Candidate candidate1(
443 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
444 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55445 IceCandidateType::kSrflx, kCandidateGeneration, kCandidateFoundation);
zhihuang38989e52017-03-21 18:04:53446 cricket::Candidate candidate2(
447 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
448 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55449 IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation);
zhihuang38989e52017-03-21 18:04:53450
451 JsepIceCandidate jice1("audio", 0, candidate1);
452 JsepIceCandidate jice2("audio", 0, candidate2);
453 JsepIceCandidate jice3("video", 0, candidate1);
454 JsepIceCandidate jice4("video", 0, candidate2);
455 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
456 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
457 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
458 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
459
460 std::string message = Serialize(jsep_desc_.get());
461 EXPECT_EQ(message.find("c=IN IP6 ::3"), std::string::npos);
462 // Should have a c line like this one when no any default exists.
463 EXPECT_NE(message.find("c=IN IP4 0.0.0.0"), std::string::npos);
464}
465
466// Tests that the connection address will be correctly set when the Candidate is
467// removed.
468TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) {
469 cricket::Candidate candidate1(
470 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
471 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55472 IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation);
zhihuang38989e52017-03-21 18:04:53473 candidate1.set_transport_name("audio");
474
475 cricket::Candidate candidate2(
476 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
477 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55478 IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation);
zhihuang38989e52017-03-21 18:04:53479 candidate2.set_transport_name("audio");
480
481 cricket::Candidate candidate3(
482 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
483 rtc::SocketAddress("192.168.1.1", 1236), kCandidatePriority, "", "",
Tomas Gunnarsson02429392024-03-07 15:24:55484 IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation);
zhihuang38989e52017-03-21 18:04:53485 candidate3.set_transport_name("audio");
486
487 JsepIceCandidate jice1("audio", 0, candidate1);
488 JsepIceCandidate jice2("audio", 0, candidate2);
489 JsepIceCandidate jice3("audio", 0, candidate3);
490
491 size_t audio_index = 0;
Steve Antonb1c1de12017-12-21 23:14:30492 auto media_desc =
493 jsep_desc_->description()->contents()[audio_index].media_description();
zhihuang38989e52017-03-21 18:04:53494
495 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
496 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
497 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
498
499 std::vector<cricket::Candidate> candidates;
500 EXPECT_EQ("192.168.1.1:1236", media_desc->connection_address().ToString());
501
502 candidates.push_back(candidate3);
503 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
504 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
505
506 candidates.clear();
507 candidates.push_back(candidate2);
508 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
509 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
510
511 candidates.clear();
512 candidates.push_back(candidate1);
513 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
514 EXPECT_EQ("0.0.0.0:9", media_desc->connection_address().ToString());
515}
Steve Anton88f2cb92017-12-05 20:47:32516
517class EnumerateAllSdpTypesTest : public ::testing::Test,
518 public ::testing::WithParamInterface<SdpType> {
519};
520
521TEST_P(EnumerateAllSdpTypesTest, TestIdentity) {
522 SdpType type = GetParam();
523
524 const char* str = webrtc::SdpTypeToString(type);
525 EXPECT_EQ(type, webrtc::SdpTypeFromString(str));
526}
527
Mirko Bonadeic84f6612019-01-31 11:20:57528INSTANTIATE_TEST_SUITE_P(JsepSessionDescriptionTest,
529 EnumerateAllSdpTypesTest,
530 Values(SdpType::kOffer,
531 SdpType::kPrAnswer,
532 SdpType::kAnswer));