blob: 674b6e72f3fef9d5c80d318dc11bbfffcf0c1729 [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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <stddef.h>
12#include <stdint.h>
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <utility>
16#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
Karl Wiberg918f50c2018-07-05 11:40:33 +020018#include "absl/memory/memory.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020019#include "api/candidate.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "api/jsep_ice_candidate.h"
22#include "api/jsep_session_description.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "p2p/base/p2p_constants.h"
Steve Antona3a92c22017-12-07 10:27:41 -080025#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "p2p/base/transport_description.h"
27#include "p2p/base/transport_info.h"
28#include "pc/session_description.h"
29#include "pc/webrtc_sdp.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "rtc_base/helpers.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/socket_address.h"
32#include "rtc_base/string_encode.h"
Yves Gerey3e707812018-11-28 16:47:49 +010033#include "test/gtest.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
Steve Anton5adfafd2017-12-20 16:34:00 -080035using cricket::MediaProtocolType;
Steve Anton88f2cb92017-12-05 12:47:32 -080036using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037using webrtc::IceCandidateCollection;
38using webrtc::IceCandidateInterface;
39using webrtc::JsepIceCandidate;
40using webrtc::JsepSessionDescription;
Steve Anton88f2cb92017-12-05 12:47:32 -080041using webrtc::SdpType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042using webrtc::SessionDescriptionInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44static const char kCandidateUfrag[] = "ufrag";
45static const char kCandidatePwd[] = "pwd";
46static const char kCandidateUfragVoice[] = "ufrag_voice";
47static const char kCandidatePwdVoice[] = "pwd_voice";
48static const char kCandidateUfragVideo[] = "ufrag_video";
49static const char kCandidatePwdVideo[] = "pwd_video";
zhihuang38989e52017-03-21 11:04:53 -070050static const char kCandidateFoundation[] = "a0+B/1";
51static const uint32_t kCandidatePriority = 2130706432U; // pref = 1.0
52static const uint32_t kCandidateGeneration = 2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54// This creates a session description with both audio and video media contents.
55// In SDP this is described by two m lines, one audio and one video.
56static cricket::SessionDescription* CreateCricketSessionDescription() {
57 cricket::SessionDescription* desc(new cricket::SessionDescription());
58 // AudioContentDescription
kwibergd1fe2812016-04-27 06:47:29 -070059 std::unique_ptr<cricket::AudioContentDescription> audio(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 new cricket::AudioContentDescription());
61
62 // VideoContentDescription
kwibergd1fe2812016-04-27 06:47:29 -070063 std::unique_ptr<cricket::VideoContentDescription> video(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 new cricket::VideoContentDescription());
65
deadbeef67cf2c12016-04-13 10:07:16 -070066 audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0));
Steve Anton5adfafd2017-12-20 16:34:00 -080067 desc->AddContent(cricket::CN_AUDIO, MediaProtocolType::kRtp, audio.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
perkj26752742016-10-24 01:21:16 -070069 video->AddCodec(cricket::VideoCodec(120, "VP8"));
Steve Anton5adfafd2017-12-20 16:34:00 -080070 desc->AddContent(cricket::CN_VIDEO, MediaProtocolType::kRtp, video.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071
Steve Anton06817cd2018-12-18 15:55:30 -080072 desc->AddTransportInfo(cricket::TransportInfo(
deadbeef46eed762016-01-28 13:24:37 -080073 cricket::CN_AUDIO,
74 cricket::TransportDescription(
75 std::vector<std::string>(), kCandidateUfragVoice, kCandidatePwdVoice,
Steve Anton06817cd2018-12-18 15:55:30 -080076 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)));
77 desc->AddTransportInfo(cricket::TransportInfo(
deadbeef46eed762016-01-28 13:24:37 -080078 cricket::CN_VIDEO,
79 cricket::TransportDescription(
80 std::vector<std::string>(), kCandidateUfragVideo, kCandidatePwdVideo,
Steve Anton06817cd2018-12-18 15:55:30 -080081 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 return desc;
83}
84
Mirko Bonadei6a489f22019-04-09 15:11:12 +020085class JsepSessionDescriptionTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 virtual void SetUp() {
88 int port = 1234;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089 rtc::SocketAddress address("127.0.0.1", port++);
guoweis@webrtc.org61c12472015-01-15 06:53:07 +000090 cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
91 address, 1, "", "", "local", 0, "1");
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 candidate_ = candidate;
Yves Gerey665174f2018-06-19 15:03:05 +020093 const std::string session_id = rtc::ToString(rtc::CreateRandomId64());
94 const std::string session_version = rtc::ToString(rtc::CreateRandomId());
Karl Wiberg918f50c2018-07-05 11:40:33 +020095 jsep_desc_ = absl::make_unique<JsepSessionDescription>(SdpType::kOffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(),
Yves Gerey665174f2018-06-19 15:03:05 +020097 session_id, session_version));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 }
99
100 std::string Serialize(const SessionDescriptionInterface* desc) {
101 std::string sdp;
102 EXPECT_TRUE(desc->ToString(&sdp));
103 EXPECT_FALSE(sdp.empty());
104 return sdp;
105 }
106
Steve Antona3a92c22017-12-07 10:27:41 -0800107 std::unique_ptr<SessionDescriptionInterface> DeSerialize(
108 const std::string& sdp) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200109 auto jsep_desc = absl::make_unique<JsepSessionDescription>(SdpType::kOffer);
Steve Antona3a92c22017-12-07 10:27:41 -0800110 EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jsep_desc.get(), nullptr));
111 return std::move(jsep_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 }
113
114 cricket::Candidate candidate_;
kwibergd1fe2812016-04-27 06:47:29 -0700115 std::unique_ptr<JsepSessionDescription> jsep_desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116};
117
118// Test that number_of_mediasections() returns the number of media contents in
119// a session description.
120TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
121 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
122}
123
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700124// Test that we can add a candidate to a session description without MID.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
126 JsepIceCandidate jsep_candidate("", 0, candidate_);
127 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
128 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
129 ASSERT_TRUE(ice_candidates != NULL);
130 EXPECT_EQ(1u, ice_candidates->count());
131 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
132 ASSERT_TRUE(ice_candidate != NULL);
133 candidate_.set_username(kCandidateUfragVoice);
134 candidate_.set_password(kCandidatePwdVoice);
135 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
136 EXPECT_EQ(0, ice_candidate->sdp_mline_index());
137 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
138}
139
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700140// Test that we can add and remove candidates to a session description with
141// MID. Removing candidates requires MID (transport_name).
142TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 // mid and m-line index don't match, in this case mid is preferred.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700144 std::string mid = "video";
145 JsepIceCandidate jsep_candidate(mid, 0, candidate_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
147 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
148 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
149 ASSERT_TRUE(ice_candidates != NULL);
150 EXPECT_EQ(1u, ice_candidates->count());
151 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
152 ASSERT_TRUE(ice_candidate != NULL);
153 candidate_.set_username(kCandidateUfragVideo);
154 candidate_.set_password(kCandidatePwdVideo);
155 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
156 // The mline index should have been updated according to mid.
157 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700158
159 std::vector<cricket::Candidate> candidates(1, candidate_);
160 candidates[0].set_transport_name(mid);
161 EXPECT_EQ(1u, jsep_desc_->RemoveCandidates(candidates));
162 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
163 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164}
165
166TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
167 candidate_.set_username(kCandidateUfrag);
168 candidate_.set_password(kCandidatePwd);
169 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
170 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
171 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
172 ASSERT_TRUE(ice_candidates != NULL);
173 EXPECT_EQ(1u, ice_candidates->count());
174 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
175 ASSERT_TRUE(ice_candidate != NULL);
176 candidate_.set_username(kCandidateUfrag);
177 candidate_.set_password(kCandidatePwd);
178 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
179
180 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
181}
182
183// Test that we can not add a candidate if there is no corresponding media
184// content in the session description.
185TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
186 JsepIceCandidate bad_candidate1("", 55, candidate_);
187 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
188
189 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
190 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
191}
192
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000193// Tests that repeatedly adding the same candidate, with or without credentials,
194// does not increase the number of candidates in the description.
195TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
196 JsepIceCandidate jsep_candidate("", 0, candidate_);
197 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
198 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
199
200 // Add the same candidate again. It should be ignored.
201 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
202 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
203
204 // Create a new candidate, identical except that the ufrag and pwd are now
205 // populated.
206 candidate_.set_username(kCandidateUfragVoice);
207 candidate_.set_password(kCandidatePwdVoice);
208 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
209
210 // This should also be identified as redundant and ignored.
211 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
212 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
213}
214
Qingsi Wang56991422019-02-08 12:53:06 -0800215// Test that the connection address is set to a hostname address after adding a
216// hostname candidate.
217TEST_F(JsepSessionDescriptionTest, AddHostnameCandidate) {
218 cricket::Candidate c;
219 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
220 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
221 c.set_address(rtc::SocketAddress("example.local", 1234));
222 c.set_type(cricket::LOCAL_PORT_TYPE);
223 JsepIceCandidate hostname_candidate("audio", 0, c);
224 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate));
225 ASSERT_NE(nullptr, jsep_desc_->description());
226 const auto& content = jsep_desc_->description()->contents()[0];
227 EXPECT_EQ("example.local:1234",
228 content.media_description()->connection_address().ToString());
229}
230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231// Test that we can serialize a JsepSessionDescription and deserialize it again.
232TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
233 std::string sdp = Serialize(jsep_desc_.get());
234
Steve Antona3a92c22017-12-07 10:27:41 -0800235 auto parsed_jsep_desc = DeSerialize(sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
237
238 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
239 EXPECT_EQ(sdp, parsed_sdp);
240}
241
242// Tests that we can serialize and deserialize a JsepSesssionDescription
243// with candidates.
244TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
245 std::string sdp = Serialize(jsep_desc_.get());
246
247 // Add a candidate and check that the serialized result is different.
248 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
249 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
250 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
251 EXPECT_NE(sdp, sdp_with_candidate);
252
Steve Antona3a92c22017-12-07 10:27:41 -0800253 auto parsed_jsep_desc = DeSerialize(sdp_with_candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
255
256 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
257}
zhihuang38989e52017-03-21 11:04:53 -0700258
259// TODO(zhihuang): Modify these tests. These are used to verify that after
260// adding the candidates, the connection_address field is set correctly. Modify
261// those so that the "connection address" is tested directly.
262// Tests serialization of SDP with only IPv6 candidates and verifies that IPv6
263// is used as default address in c line according to preference.
264TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithIPv6Only) {
265 // Stun has a high preference than local host.
266 cricket::Candidate candidate1(
267 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
268 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
269 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
270 cricket::Candidate candidate2(
271 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
272 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
273 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
274
275 JsepIceCandidate jice1("audio", 0, candidate1);
276 JsepIceCandidate jice2("audio", 0, candidate2);
277 JsepIceCandidate jice3("video", 0, candidate1);
278 JsepIceCandidate jice4("video", 0, candidate2);
279 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
280 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
281 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
282 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
283 std::string message = Serialize(jsep_desc_.get());
284
285 // Should have a c line like this one.
286 EXPECT_NE(message.find("c=IN IP6 ::1"), std::string::npos);
287 // Shouldn't have a IP4 c line.
288 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
289}
290
291// Tests serialization of SDP with both IPv4 and IPv6 candidates and
292// verifies that IPv4 is used as default address in c line even if the
293// preference of IPv4 is lower.
294TEST_F(JsepSessionDescriptionTest,
295 SerializeSessionDescriptionWithBothIPFamilies) {
296 cricket::Candidate candidate_v4(
297 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
298 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
299 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
300 cricket::Candidate candidate_v6(
301 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
302 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
303 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
304
305 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
306 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
307 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
308 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
309 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
310 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
311 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
312 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
313 std::string message = Serialize(jsep_desc_.get());
314
315 // Should have a c line like this one.
316 EXPECT_NE(message.find("c=IN IP4 192.168.1.5"), std::string::npos);
317 // Shouldn't have a IP6 c line.
318 EXPECT_EQ(message.find("c=IN IP6"), std::string::npos);
319}
320
321// Tests serialization of SDP with both UDP and TCP candidates and
322// verifies that UDP is used as default address in c line even if the
323// preference of UDP is lower.
324TEST_F(JsepSessionDescriptionTest,
325 SerializeSessionDescriptionWithBothProtocols) {
326 // Stun has a high preference than local host.
327 cricket::Candidate candidate1(
328 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
329 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
330 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
331 cricket::Candidate candidate2(
332 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
333 rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority,
334 "", "", cricket::LOCAL_PORT_TYPE, kCandidateGeneration,
335 kCandidateFoundation);
336
337 JsepIceCandidate jice1("audio", 0, candidate1);
338 JsepIceCandidate jice2("audio", 0, candidate2);
339 JsepIceCandidate jice3("video", 0, candidate1);
340 JsepIceCandidate jice4("video", 0, candidate2);
341 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
342 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
343 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
344 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
345 std::string message = Serialize(jsep_desc_.get());
346
347 // Should have a c line like this one.
348 EXPECT_NE(message.find("c=IN IP6 fe80::1234:5678:abcd:ef12"),
349 std::string::npos);
350 // Shouldn't have a IP4 c line.
351 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
352}
353
354// Tests serialization of SDP with only TCP candidates and verifies that
355// null IPv4 is used as default address in c line.
356TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithTCPOnly) {
357 // Stun has a high preference than local host.
358 cricket::Candidate candidate1(
359 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
360 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
361 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
362 cricket::Candidate candidate2(
363 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
364 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
365 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
366
367 JsepIceCandidate jice1("audio", 0, candidate1);
368 JsepIceCandidate jice2("audio", 0, candidate2);
369 JsepIceCandidate jice3("video", 0, candidate1);
370 JsepIceCandidate jice4("video", 0, candidate2);
371 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
372 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
373 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
374 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
375
376 std::string message = Serialize(jsep_desc_.get());
377 EXPECT_EQ(message.find("c=IN IP6 ::3"), std::string::npos);
378 // Should have a c line like this one when no any default exists.
379 EXPECT_NE(message.find("c=IN IP4 0.0.0.0"), std::string::npos);
380}
381
382// Tests that the connection address will be correctly set when the Candidate is
383// removed.
384TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) {
385 cricket::Candidate candidate1(
386 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
387 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
388 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
389 candidate1.set_transport_name("audio");
390
391 cricket::Candidate candidate2(
392 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
393 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
394 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
395 candidate2.set_transport_name("audio");
396
397 cricket::Candidate candidate3(
398 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
399 rtc::SocketAddress("192.168.1.1", 1236), kCandidatePriority, "", "",
400 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
401 candidate3.set_transport_name("audio");
402
403 JsepIceCandidate jice1("audio", 0, candidate1);
404 JsepIceCandidate jice2("audio", 0, candidate2);
405 JsepIceCandidate jice3("audio", 0, candidate3);
406
407 size_t audio_index = 0;
Steve Antonb1c1de12017-12-21 15:14:30 -0800408 auto media_desc =
409 jsep_desc_->description()->contents()[audio_index].media_description();
zhihuang38989e52017-03-21 11:04:53 -0700410
411 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
412 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
413 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
414
415 std::vector<cricket::Candidate> candidates;
416 EXPECT_EQ("192.168.1.1:1236", media_desc->connection_address().ToString());
417
418 candidates.push_back(candidate3);
419 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
420 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
421
422 candidates.clear();
423 candidates.push_back(candidate2);
424 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
425 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
426
427 candidates.clear();
428 candidates.push_back(candidate1);
429 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
430 EXPECT_EQ("0.0.0.0:9", media_desc->connection_address().ToString());
431}
Steve Anton88f2cb92017-12-05 12:47:32 -0800432
433class EnumerateAllSdpTypesTest : public ::testing::Test,
434 public ::testing::WithParamInterface<SdpType> {
435};
436
437TEST_P(EnumerateAllSdpTypesTest, TestIdentity) {
438 SdpType type = GetParam();
439
440 const char* str = webrtc::SdpTypeToString(type);
441 EXPECT_EQ(type, webrtc::SdpTypeFromString(str));
442}
443
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100444INSTANTIATE_TEST_SUITE_P(JsepSessionDescriptionTest,
445 EnumerateAllSdpTypesTest,
446 Values(SdpType::kOffer,
447 SdpType::kPrAnswer,
448 SdpType::kAnswer));