blob: d2fc6e595767c85d1fcd3544ac9821a6878cdd8c [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.
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020056static std::unique_ptr<cricket::SessionDescription>
57CreateCricketSessionDescription() {
58 auto desc = absl::make_unique<cricket::SessionDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020060 // AudioContentDescription
61 auto audio = absl::make_unique<cricket::AudioContentDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 // VideoContentDescription
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020063 auto video = absl::make_unique<cricket::VideoContentDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
deadbeef67cf2c12016-04-13 10:07:16 -070065 audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0));
Harald Alvestrand1716d392019-06-03 20:35:45 +020066 desc->AddContent(cricket::CN_AUDIO, MediaProtocolType::kRtp,
67 std::move(audio));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
perkj26752742016-10-24 01:21:16 -070069 video->AddCodec(cricket::VideoCodec(120, "VP8"));
Harald Alvestrand1716d392019-06-03 20:35:45 +020070 desc->AddContent(cricket::CN_VIDEO, MediaProtocolType::kRtp,
71 std::move(video));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
Steve Anton06817cd2018-12-18 15:55:30 -080073 desc->AddTransportInfo(cricket::TransportInfo(
deadbeef46eed762016-01-28 13:24:37 -080074 cricket::CN_AUDIO,
75 cricket::TransportDescription(
76 std::vector<std::string>(), kCandidateUfragVoice, kCandidatePwdVoice,
Steve Anton06817cd2018-12-18 15:55:30 -080077 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)));
78 desc->AddTransportInfo(cricket::TransportInfo(
deadbeef46eed762016-01-28 13:24:37 -080079 cricket::CN_VIDEO,
80 cricket::TransportDescription(
81 std::vector<std::string>(), kCandidateUfragVideo, kCandidatePwdVideo,
Steve Anton06817cd2018-12-18 15:55:30 -080082 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 return desc;
84}
85
Mirko Bonadei6a489f22019-04-09 15:11:12 +020086class JsepSessionDescriptionTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 virtual void SetUp() {
89 int port = 1234;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000090 rtc::SocketAddress address("127.0.0.1", port++);
guoweis@webrtc.org61c12472015-01-15 06:53:07 +000091 cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
92 address, 1, "", "", "local", 0, "1");
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 candidate_ = candidate;
Yves Gerey665174f2018-06-19 15:03:05 +020094 const std::string session_id = rtc::ToString(rtc::CreateRandomId64());
95 const std::string session_version = rtc::ToString(rtc::CreateRandomId());
Karl Wiberg918f50c2018-07-05 11:40:33 +020096 jsep_desc_ = absl::make_unique<JsepSessionDescription>(SdpType::kOffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(),
Yves Gerey665174f2018-06-19 15:03:05 +020098 session_id, session_version));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 }
100
101 std::string Serialize(const SessionDescriptionInterface* desc) {
102 std::string sdp;
103 EXPECT_TRUE(desc->ToString(&sdp));
104 EXPECT_FALSE(sdp.empty());
105 return sdp;
106 }
107
Steve Antona3a92c22017-12-07 10:27:41 -0800108 std::unique_ptr<SessionDescriptionInterface> DeSerialize(
109 const std::string& sdp) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200110 auto jsep_desc = absl::make_unique<JsepSessionDescription>(SdpType::kOffer);
Steve Antona3a92c22017-12-07 10:27:41 -0800111 EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jsep_desc.get(), nullptr));
112 return std::move(jsep_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 }
114
115 cricket::Candidate candidate_;
kwibergd1fe2812016-04-27 06:47:29 -0700116 std::unique_ptr<JsepSessionDescription> jsep_desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117};
118
119// Test that number_of_mediasections() returns the number of media contents in
120// a session description.
121TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
122 EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
123}
124
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700125// Test that we can add a candidate to a session description without MID.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) {
127 JsepIceCandidate jsep_candidate("", 0, candidate_);
128 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
129 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
130 ASSERT_TRUE(ice_candidates != NULL);
131 EXPECT_EQ(1u, ice_candidates->count());
132 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
133 ASSERT_TRUE(ice_candidate != NULL);
134 candidate_.set_username(kCandidateUfragVoice);
135 candidate_.set_password(kCandidatePwdVoice);
136 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
137 EXPECT_EQ(0, ice_candidate->sdp_mline_index());
138 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
139}
140
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700141// Test that we can add and remove candidates to a session description with
142// MID. Removing candidates requires MID (transport_name).
143TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 // mid and m-line index don't match, in this case mid is preferred.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700145 std::string mid = "video";
146 JsepIceCandidate jsep_candidate(mid, 0, candidate_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
148 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
149 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1);
150 ASSERT_TRUE(ice_candidates != NULL);
151 EXPECT_EQ(1u, ice_candidates->count());
152 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
153 ASSERT_TRUE(ice_candidate != NULL);
154 candidate_.set_username(kCandidateUfragVideo);
155 candidate_.set_password(kCandidatePwdVideo);
156 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
157 // The mline index should have been updated according to mid.
158 EXPECT_EQ(1, ice_candidate->sdp_mline_index());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700159
160 std::vector<cricket::Candidate> candidates(1, candidate_);
161 candidates[0].set_transport_name(mid);
162 EXPECT_EQ(1u, jsep_desc_->RemoveCandidates(candidates));
163 EXPECT_EQ(0u, jsep_desc_->candidates(0)->count());
164 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165}
166
167TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) {
168 candidate_.set_username(kCandidateUfrag);
169 candidate_.set_password(kCandidatePwd);
170 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
171 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
172 const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0);
173 ASSERT_TRUE(ice_candidates != NULL);
174 EXPECT_EQ(1u, ice_candidates->count());
175 const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
176 ASSERT_TRUE(ice_candidate != NULL);
177 candidate_.set_username(kCandidateUfrag);
178 candidate_.set_password(kCandidatePwd);
179 EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
180
181 EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
182}
183
184// Test that we can not add a candidate if there is no corresponding media
185// content in the session description.
186TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
187 JsepIceCandidate bad_candidate1("", 55, candidate_);
188 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
189
190 JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_);
191 EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
192}
193
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000194// Tests that repeatedly adding the same candidate, with or without credentials,
195// does not increase the number of candidates in the description.
196TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) {
197 JsepIceCandidate jsep_candidate("", 0, candidate_);
198 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
199 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
200
201 // Add the same candidate again. It should be ignored.
202 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
203 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
204
205 // Create a new candidate, identical except that the ufrag and pwd are now
206 // populated.
207 candidate_.set_username(kCandidateUfragVoice);
208 candidate_.set_password(kCandidatePwdVoice);
209 JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_);
210
211 // This should also be identified as redundant and ignored.
212 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials));
213 EXPECT_EQ(1u, jsep_desc_->candidates(0)->count());
214}
215
Qingsi Wang56991422019-02-08 12:53:06 -0800216// Test that the connection address is set to a hostname address after adding a
217// hostname candidate.
218TEST_F(JsepSessionDescriptionTest, AddHostnameCandidate) {
219 cricket::Candidate c;
220 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP);
221 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
222 c.set_address(rtc::SocketAddress("example.local", 1234));
223 c.set_type(cricket::LOCAL_PORT_TYPE);
224 JsepIceCandidate hostname_candidate("audio", 0, c);
225 EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate));
226 ASSERT_NE(nullptr, jsep_desc_->description());
227 const auto& content = jsep_desc_->description()->contents()[0];
228 EXPECT_EQ("example.local:1234",
229 content.media_description()->connection_address().ToString());
230}
231
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232// Test that we can serialize a JsepSessionDescription and deserialize it again.
233TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
234 std::string sdp = Serialize(jsep_desc_.get());
235
Steve Antona3a92c22017-12-07 10:27:41 -0800236 auto parsed_jsep_desc = DeSerialize(sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
238
239 std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
240 EXPECT_EQ(sdp, parsed_sdp);
241}
242
243// Tests that we can serialize and deserialize a JsepSesssionDescription
244// with candidates.
245TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
246 std::string sdp = Serialize(jsep_desc_.get());
247
248 // Add a candidate and check that the serialized result is different.
249 JsepIceCandidate jsep_candidate("audio", 0, candidate_);
250 EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
251 std::string sdp_with_candidate = Serialize(jsep_desc_.get());
252 EXPECT_NE(sdp, sdp_with_candidate);
253
Steve Antona3a92c22017-12-07 10:27:41 -0800254 auto parsed_jsep_desc = DeSerialize(sdp_with_candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
256
257 EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);
258}
zhihuang38989e52017-03-21 11:04:53 -0700259
260// TODO(zhihuang): Modify these tests. These are used to verify that after
261// adding the candidates, the connection_address field is set correctly. Modify
262// those so that the "connection address" is tested directly.
263// Tests serialization of SDP with only IPv6 candidates and verifies that IPv6
264// is used as default address in c line according to preference.
265TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithIPv6Only) {
266 // Stun has a high preference than local host.
267 cricket::Candidate candidate1(
268 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
269 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
270 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
271 cricket::Candidate candidate2(
272 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
273 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
274 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
275
276 JsepIceCandidate jice1("audio", 0, candidate1);
277 JsepIceCandidate jice2("audio", 0, candidate2);
278 JsepIceCandidate jice3("video", 0, candidate1);
279 JsepIceCandidate jice4("video", 0, candidate2);
280 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
281 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
282 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
283 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
284 std::string message = Serialize(jsep_desc_.get());
285
286 // Should have a c line like this one.
287 EXPECT_NE(message.find("c=IN IP6 ::1"), std::string::npos);
288 // Shouldn't have a IP4 c line.
289 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
290}
291
292// Tests serialization of SDP with both IPv4 and IPv6 candidates and
293// verifies that IPv4 is used as default address in c line even if the
294// preference of IPv4 is lower.
295TEST_F(JsepSessionDescriptionTest,
296 SerializeSessionDescriptionWithBothIPFamilies) {
297 cricket::Candidate candidate_v4(
298 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
299 rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "",
300 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
301 cricket::Candidate candidate_v6(
302 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
303 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
304 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
305
306 JsepIceCandidate jice_v4("audio", 0, candidate_v4);
307 JsepIceCandidate jice_v6("audio", 0, candidate_v6);
308 JsepIceCandidate jice_v4_video("video", 0, candidate_v4);
309 JsepIceCandidate jice_v6_video("video", 0, candidate_v6);
310 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4));
311 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6));
312 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video));
313 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video));
314 std::string message = Serialize(jsep_desc_.get());
315
316 // Should have a c line like this one.
317 EXPECT_NE(message.find("c=IN IP4 192.168.1.5"), std::string::npos);
318 // Shouldn't have a IP6 c line.
319 EXPECT_EQ(message.find("c=IN IP6"), std::string::npos);
320}
321
322// Tests serialization of SDP with both UDP and TCP candidates and
323// verifies that UDP is used as default address in c line even if the
324// preference of UDP is lower.
325TEST_F(JsepSessionDescriptionTest,
326 SerializeSessionDescriptionWithBothProtocols) {
327 // Stun has a high preference than local host.
328 cricket::Candidate candidate1(
329 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
330 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
331 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
332 cricket::Candidate candidate2(
333 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
334 rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority,
335 "", "", cricket::LOCAL_PORT_TYPE, kCandidateGeneration,
336 kCandidateFoundation);
337
338 JsepIceCandidate jice1("audio", 0, candidate1);
339 JsepIceCandidate jice2("audio", 0, candidate2);
340 JsepIceCandidate jice3("video", 0, candidate1);
341 JsepIceCandidate jice4("video", 0, candidate2);
342 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
343 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
344 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
345 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
346 std::string message = Serialize(jsep_desc_.get());
347
348 // Should have a c line like this one.
349 EXPECT_NE(message.find("c=IN IP6 fe80::1234:5678:abcd:ef12"),
350 std::string::npos);
351 // Shouldn't have a IP4 c line.
352 EXPECT_EQ(message.find("c=IN IP4"), std::string::npos);
353}
354
355// Tests serialization of SDP with only TCP candidates and verifies that
356// null IPv4 is used as default address in c line.
357TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithTCPOnly) {
358 // Stun has a high preference than local host.
359 cricket::Candidate candidate1(
360 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
361 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
362 cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
363 cricket::Candidate candidate2(
364 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
365 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
366 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
367
368 JsepIceCandidate jice1("audio", 0, candidate1);
369 JsepIceCandidate jice2("audio", 0, candidate2);
370 JsepIceCandidate jice3("video", 0, candidate1);
371 JsepIceCandidate jice4("video", 0, candidate2);
372 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
373 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
374 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
375 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4));
376
377 std::string message = Serialize(jsep_desc_.get());
378 EXPECT_EQ(message.find("c=IN IP6 ::3"), std::string::npos);
379 // Should have a c line like this one when no any default exists.
380 EXPECT_NE(message.find("c=IN IP4 0.0.0.0"), std::string::npos);
381}
382
383// Tests that the connection address will be correctly set when the Candidate is
384// removed.
385TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) {
386 cricket::Candidate candidate1(
387 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
388 rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "",
389 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
390 candidate1.set_transport_name("audio");
391
392 cricket::Candidate candidate2(
393 cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp",
394 rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "",
395 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
396 candidate2.set_transport_name("audio");
397
398 cricket::Candidate candidate3(
399 cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp",
400 rtc::SocketAddress("192.168.1.1", 1236), kCandidatePriority, "", "",
401 cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation);
402 candidate3.set_transport_name("audio");
403
404 JsepIceCandidate jice1("audio", 0, candidate1);
405 JsepIceCandidate jice2("audio", 0, candidate2);
406 JsepIceCandidate jice3("audio", 0, candidate3);
407
408 size_t audio_index = 0;
Steve Antonb1c1de12017-12-21 15:14:30 -0800409 auto media_desc =
410 jsep_desc_->description()->contents()[audio_index].media_description();
zhihuang38989e52017-03-21 11:04:53 -0700411
412 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1));
413 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2));
414 ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3));
415
416 std::vector<cricket::Candidate> candidates;
417 EXPECT_EQ("192.168.1.1:1236", media_desc->connection_address().ToString());
418
419 candidates.push_back(candidate3);
420 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
421 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
422
423 candidates.clear();
424 candidates.push_back(candidate2);
425 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
426 EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString());
427
428 candidates.clear();
429 candidates.push_back(candidate1);
430 ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates));
431 EXPECT_EQ("0.0.0.0:9", media_desc->connection_address().ToString());
432}
Steve Anton88f2cb92017-12-05 12:47:32 -0800433
434class EnumerateAllSdpTypesTest : public ::testing::Test,
435 public ::testing::WithParamInterface<SdpType> {
436};
437
438TEST_P(EnumerateAllSdpTypesTest, TestIdentity) {
439 SdpType type = GetParam();
440
441 const char* str = webrtc::SdpTypeToString(type);
442 EXPECT_EQ(type, webrtc::SdpTypeFromString(str));
443}
444
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100445INSTANTIATE_TEST_SUITE_P(JsepSessionDescriptionTest,
446 EnumerateAllSdpTypesTest,
447 Values(SdpType::kOffer,
448 SdpType::kPrAnswer,
449 SdpType::kAnswer));