henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "api/jsep_session_description.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <utility> |
| 17 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 19 | #include "api/candidate.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 20 | #include "api/jsep.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "api/jsep_ice_candidate.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 22 | #include "media/base/codec.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "p2p/base/p2p_constants.h" |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 24 | #include "p2p/base/port.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #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 Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 29 | #include "rtc_base/helpers.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 30 | #include "rtc_base/net_helper.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 31 | #include "rtc_base/socket_address.h" |
| 32 | #include "rtc_base/string_encode.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 33 | #include "test/gtest.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 35 | using cricket::MediaProtocolType; |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 36 | using ::testing::Values; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | using webrtc::IceCandidateCollection; |
| 38 | using webrtc::IceCandidateInterface; |
| 39 | using webrtc::JsepIceCandidate; |
| 40 | using webrtc::JsepSessionDescription; |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 41 | using webrtc::SdpType; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | using webrtc::SessionDescriptionInterface; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | |
| 44 | static const char kCandidateUfrag[] = "ufrag"; |
| 45 | static const char kCandidatePwd[] = "pwd"; |
| 46 | static const char kCandidateUfragVoice[] = "ufrag_voice"; |
| 47 | static const char kCandidatePwdVoice[] = "pwd_voice"; |
| 48 | static const char kCandidateUfragVideo[] = "ufrag_video"; |
| 49 | static const char kCandidatePwdVideo[] = "pwd_video"; |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 50 | static const char kCandidateFoundation[] = "a0+B/1"; |
| 51 | static const uint32_t kCandidatePriority = 2130706432U; // pref = 1.0 |
| 52 | static const uint32_t kCandidateGeneration = 2; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | |
| 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 Alvestrand | 4d7160e | 2019-04-12 07:01:29 +0200 | [diff] [blame] | 56 | static std::unique_ptr<cricket::SessionDescription> |
| 57 | CreateCricketSessionDescription() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 58 | auto desc = std::make_unique<cricket::SessionDescription>(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | |
Harald Alvestrand | 4d7160e | 2019-04-12 07:01:29 +0200 | [diff] [blame] | 60 | // AudioContentDescription |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 61 | auto audio = std::make_unique<cricket::AudioContentDescription>(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | // VideoContentDescription |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 63 | auto video = std::make_unique<cricket::VideoContentDescription>(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 65 | audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0)); |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 66 | desc->AddContent(cricket::CN_AUDIO, MediaProtocolType::kRtp, |
| 67 | std::move(audio)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | |
perkj | 2675274 | 2016-10-24 01:21:16 -0700 | [diff] [blame] | 69 | video->AddCodec(cricket::VideoCodec(120, "VP8")); |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 70 | desc->AddContent(cricket::CN_VIDEO, MediaProtocolType::kRtp, |
| 71 | std::move(video)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 73 | desc->AddTransportInfo(cricket::TransportInfo( |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 74 | cricket::CN_AUDIO, |
| 75 | cricket::TransportDescription( |
| 76 | std::vector<std::string>(), kCandidateUfragVoice, kCandidatePwdVoice, |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 77 | cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL))); |
| 78 | desc->AddTransportInfo(cricket::TransportInfo( |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 79 | cricket::CN_VIDEO, |
| 80 | cricket::TransportDescription( |
| 81 | std::vector<std::string>(), kCandidateUfragVideo, kCandidatePwdVideo, |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 82 | cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | return desc; |
| 84 | } |
| 85 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 86 | class JsepSessionDescriptionTest : public ::testing::Test { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | protected: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | virtual void SetUp() { |
| 89 | int port = 1234; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 90 | rtc::SocketAddress address("127.0.0.1", port++); |
guoweis@webrtc.org | 61c1247 | 2015-01-15 06:53:07 +0000 | [diff] [blame] | 91 | cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 92 | address, 1, "", "", "local", 0, "1"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | candidate_ = candidate; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | const std::string session_id = rtc::ToString(rtc::CreateRandomId64()); |
| 95 | const std::string session_version = rtc::ToString(rtc::CreateRandomId()); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 96 | jsep_desc_ = std::make_unique<JsepSessionDescription>(SdpType::kOffer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 98 | session_id, session_version)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | } |
| 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 Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 108 | std::unique_ptr<SessionDescriptionInterface> DeSerialize( |
| 109 | const std::string& sdp) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 110 | auto jsep_desc = std::make_unique<JsepSessionDescription>(SdpType::kOffer); |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 111 | EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jsep_desc.get(), nullptr)); |
| 112 | return std::move(jsep_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | cricket::Candidate candidate_; |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 116 | std::unique_ptr<JsepSessionDescription> jsep_desc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Harald Alvestrand | 0e7b3a9 | 2020-12-15 15:18:28 +0000 | [diff] [blame] | 119 | TEST_F(JsepSessionDescriptionTest, CloneDefault) { |
| 120 | auto new_desc = jsep_desc_->Clone(); |
| 121 | EXPECT_EQ(jsep_desc_->type(), new_desc->type()); |
| 122 | std::string old_desc_string; |
| 123 | std::string new_desc_string; |
| 124 | EXPECT_TRUE(jsep_desc_->ToString(&old_desc_string)); |
| 125 | EXPECT_TRUE(new_desc->ToString(&new_desc_string)); |
| 126 | EXPECT_EQ(old_desc_string, new_desc_string); |
| 127 | EXPECT_EQ(jsep_desc_->session_id(), new_desc->session_id()); |
| 128 | EXPECT_EQ(jsep_desc_->session_version(), new_desc->session_version()); |
| 129 | } |
| 130 | |
Byoungchan Lee | cb42e10 | 2021-07-15 14:09:40 +0900 | [diff] [blame] | 131 | TEST_F(JsepSessionDescriptionTest, CloneRollback) { |
| 132 | auto jsep_desc = std::make_unique<JsepSessionDescription>(SdpType::kRollback); |
| 133 | auto new_desc = jsep_desc->Clone(); |
| 134 | EXPECT_EQ(jsep_desc->type(), new_desc->type()); |
| 135 | } |
| 136 | |
Harald Alvestrand | fc6b871 | 2021-01-05 10:51:08 +0000 | [diff] [blame] | 137 | TEST_F(JsepSessionDescriptionTest, CloneWithCandidates) { |
| 138 | cricket::Candidate candidate_v4( |
| 139 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 140 | rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "", |
| 141 | cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 142 | cricket::Candidate candidate_v6( |
| 143 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 144 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 145 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 146 | |
| 147 | JsepIceCandidate jice_v4("audio", 0, candidate_v4); |
| 148 | JsepIceCandidate jice_v6("audio", 0, candidate_v6); |
| 149 | JsepIceCandidate jice_v4_video("video", 0, candidate_v4); |
| 150 | JsepIceCandidate jice_v6_video("video", 0, candidate_v6); |
| 151 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4)); |
| 152 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6)); |
| 153 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video)); |
| 154 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video)); |
| 155 | auto new_desc = jsep_desc_->Clone(); |
| 156 | EXPECT_EQ(jsep_desc_->type(), new_desc->type()); |
| 157 | std::string old_desc_string; |
| 158 | std::string new_desc_string; |
| 159 | EXPECT_TRUE(jsep_desc_->ToString(&old_desc_string)); |
| 160 | EXPECT_TRUE(new_desc->ToString(&new_desc_string)); |
| 161 | EXPECT_EQ(old_desc_string, new_desc_string); |
| 162 | } |
| 163 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | // Test that number_of_mediasections() returns the number of media contents in |
| 165 | // a session description. |
| 166 | TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) { |
| 167 | EXPECT_EQ(2u, jsep_desc_->number_of_mediasections()); |
| 168 | } |
| 169 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 170 | // Test that we can add a candidate to a session description without MID. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 171 | TEST_F(JsepSessionDescriptionTest, AddCandidateWithoutMid) { |
| 172 | JsepIceCandidate jsep_candidate("", 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(kCandidateUfragVoice); |
| 180 | candidate_.set_password(kCandidatePwdVoice); |
| 181 | EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_)); |
| 182 | EXPECT_EQ(0, ice_candidate->sdp_mline_index()); |
| 183 | EXPECT_EQ(0u, jsep_desc_->candidates(1)->count()); |
| 184 | } |
| 185 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 186 | // Test that we can add and remove candidates to a session description with |
| 187 | // MID. Removing candidates requires MID (transport_name). |
| 188 | TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 189 | // mid and m-line index don't match, in this case mid is preferred. |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 190 | std::string mid = "video"; |
| 191 | JsepIceCandidate jsep_candidate(mid, 0, candidate_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 192 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 193 | EXPECT_EQ(0u, jsep_desc_->candidates(0)->count()); |
| 194 | const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(1); |
| 195 | ASSERT_TRUE(ice_candidates != NULL); |
| 196 | EXPECT_EQ(1u, ice_candidates->count()); |
| 197 | const IceCandidateInterface* ice_candidate = ice_candidates->at(0); |
| 198 | ASSERT_TRUE(ice_candidate != NULL); |
| 199 | candidate_.set_username(kCandidateUfragVideo); |
| 200 | candidate_.set_password(kCandidatePwdVideo); |
| 201 | EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_)); |
| 202 | // The mline index should have been updated according to mid. |
| 203 | EXPECT_EQ(1, ice_candidate->sdp_mline_index()); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 204 | |
| 205 | std::vector<cricket::Candidate> candidates(1, candidate_); |
| 206 | candidates[0].set_transport_name(mid); |
| 207 | EXPECT_EQ(1u, jsep_desc_->RemoveCandidates(candidates)); |
| 208 | EXPECT_EQ(0u, jsep_desc_->candidates(0)->count()); |
| 209 | EXPECT_EQ(0u, jsep_desc_->candidates(1)->count()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | TEST_F(JsepSessionDescriptionTest, AddCandidateAlreadyHasUfrag) { |
| 213 | candidate_.set_username(kCandidateUfrag); |
| 214 | candidate_.set_password(kCandidatePwd); |
| 215 | JsepIceCandidate jsep_candidate("audio", 0, candidate_); |
| 216 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 217 | const IceCandidateCollection* ice_candidates = jsep_desc_->candidates(0); |
| 218 | ASSERT_TRUE(ice_candidates != NULL); |
| 219 | EXPECT_EQ(1u, ice_candidates->count()); |
| 220 | const IceCandidateInterface* ice_candidate = ice_candidates->at(0); |
| 221 | ASSERT_TRUE(ice_candidate != NULL); |
| 222 | candidate_.set_username(kCandidateUfrag); |
| 223 | candidate_.set_password(kCandidatePwd); |
| 224 | EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_)); |
| 225 | |
| 226 | EXPECT_EQ(0u, jsep_desc_->candidates(1)->count()); |
| 227 | } |
| 228 | |
| 229 | // Test that we can not add a candidate if there is no corresponding media |
| 230 | // content in the session description. |
| 231 | TEST_F(JsepSessionDescriptionTest, AddBadCandidate) { |
| 232 | JsepIceCandidate bad_candidate1("", 55, candidate_); |
| 233 | EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1)); |
| 234 | |
| 235 | JsepIceCandidate bad_candidate2("some weird mid", 0, candidate_); |
| 236 | EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2)); |
| 237 | } |
| 238 | |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 239 | // Tests that repeatedly adding the same candidate, with or without credentials, |
| 240 | // does not increase the number of candidates in the description. |
| 241 | TEST_F(JsepSessionDescriptionTest, AddCandidateDuplicates) { |
| 242 | JsepIceCandidate jsep_candidate("", 0, candidate_); |
| 243 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 244 | EXPECT_EQ(1u, jsep_desc_->candidates(0)->count()); |
| 245 | |
| 246 | // Add the same candidate again. It should be ignored. |
| 247 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 248 | EXPECT_EQ(1u, jsep_desc_->candidates(0)->count()); |
| 249 | |
| 250 | // Create a new candidate, identical except that the ufrag and pwd are now |
| 251 | // populated. |
| 252 | candidate_.set_username(kCandidateUfragVoice); |
| 253 | candidate_.set_password(kCandidatePwdVoice); |
| 254 | JsepIceCandidate jsep_candidate_with_credentials("", 0, candidate_); |
| 255 | |
| 256 | // This should also be identified as redundant and ignored. |
| 257 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate_with_credentials)); |
| 258 | EXPECT_EQ(1u, jsep_desc_->candidates(0)->count()); |
| 259 | } |
| 260 | |
Qingsi Wang | 5699142 | 2019-02-08 12:53:06 -0800 | [diff] [blame] | 261 | // Test that the connection address is set to a hostname address after adding a |
| 262 | // hostname candidate. |
| 263 | TEST_F(JsepSessionDescriptionTest, AddHostnameCandidate) { |
| 264 | cricket::Candidate c; |
| 265 | c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 266 | c.set_protocol(cricket::UDP_PROTOCOL_NAME); |
| 267 | c.set_address(rtc::SocketAddress("example.local", 1234)); |
| 268 | c.set_type(cricket::LOCAL_PORT_TYPE); |
Qingsi Wang | 3ae59d3 | 2019-07-11 21:53:45 -0700 | [diff] [blame] | 269 | const size_t audio_index = 0; |
| 270 | JsepIceCandidate hostname_candidate("audio", audio_index, c); |
Qingsi Wang | 5699142 | 2019-02-08 12:53:06 -0800 | [diff] [blame] | 271 | EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate)); |
Qingsi Wang | 3ae59d3 | 2019-07-11 21:53:45 -0700 | [diff] [blame] | 272 | |
Qingsi Wang | 5699142 | 2019-02-08 12:53:06 -0800 | [diff] [blame] | 273 | ASSERT_NE(nullptr, jsep_desc_->description()); |
Qingsi Wang | 3ae59d3 | 2019-07-11 21:53:45 -0700 | [diff] [blame] | 274 | ASSERT_EQ(2u, jsep_desc_->description()->contents().size()); |
| 275 | const auto& content = jsep_desc_->description()->contents()[audio_index]; |
| 276 | EXPECT_EQ("0.0.0.0:9", |
Qingsi Wang | 5699142 | 2019-02-08 12:53:06 -0800 | [diff] [blame] | 277 | content.media_description()->connection_address().ToString()); |
| 278 | } |
| 279 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 280 | // Test that we can serialize a JsepSessionDescription and deserialize it again. |
| 281 | TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) { |
| 282 | std::string sdp = Serialize(jsep_desc_.get()); |
| 283 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 284 | auto parsed_jsep_desc = DeSerialize(sdp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 285 | EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections()); |
| 286 | |
| 287 | std::string parsed_sdp = Serialize(parsed_jsep_desc.get()); |
| 288 | EXPECT_EQ(sdp, parsed_sdp); |
| 289 | } |
| 290 | |
Qingsi Wang | 3ae59d3 | 2019-07-11 21:53:45 -0700 | [diff] [blame] | 291 | // Test that we can serialize a JsepSessionDescription when a hostname candidate |
| 292 | // is the default destination and deserialize it again. The connection address |
| 293 | // in the deserialized description should be the dummy address 0.0.0.0:9. |
| 294 | TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithHostnameCandidate) { |
| 295 | cricket::Candidate c; |
| 296 | c.set_component(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 297 | c.set_protocol(cricket::UDP_PROTOCOL_NAME); |
| 298 | c.set_address(rtc::SocketAddress("example.local", 1234)); |
| 299 | c.set_type(cricket::LOCAL_PORT_TYPE); |
| 300 | const size_t audio_index = 0; |
| 301 | const size_t video_index = 1; |
| 302 | JsepIceCandidate hostname_candidate_audio("audio", audio_index, c); |
| 303 | JsepIceCandidate hostname_candidate_video("video", video_index, c); |
| 304 | EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_audio)); |
| 305 | EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate_video)); |
| 306 | |
| 307 | std::string sdp = Serialize(jsep_desc_.get()); |
| 308 | |
| 309 | auto parsed_jsep_desc = DeSerialize(sdp); |
| 310 | EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections()); |
| 311 | |
| 312 | ASSERT_NE(nullptr, parsed_jsep_desc->description()); |
| 313 | ASSERT_EQ(2u, parsed_jsep_desc->description()->contents().size()); |
| 314 | const auto& audio_content = |
| 315 | parsed_jsep_desc->description()->contents()[audio_index]; |
| 316 | const auto& video_content = |
| 317 | parsed_jsep_desc->description()->contents()[video_index]; |
| 318 | EXPECT_EQ("0.0.0.0:9", |
| 319 | audio_content.media_description()->connection_address().ToString()); |
| 320 | EXPECT_EQ("0.0.0.0:9", |
| 321 | video_content.media_description()->connection_address().ToString()); |
| 322 | } |
| 323 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 324 | // Tests that we can serialize and deserialize a JsepSesssionDescription |
| 325 | // with candidates. |
| 326 | TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) { |
| 327 | std::string sdp = Serialize(jsep_desc_.get()); |
| 328 | |
| 329 | // Add a candidate and check that the serialized result is different. |
| 330 | JsepIceCandidate jsep_candidate("audio", 0, candidate_); |
| 331 | EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate)); |
| 332 | std::string sdp_with_candidate = Serialize(jsep_desc_.get()); |
| 333 | EXPECT_NE(sdp, sdp_with_candidate); |
| 334 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 335 | auto parsed_jsep_desc = DeSerialize(sdp_with_candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 336 | std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get()); |
| 337 | |
| 338 | EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate); |
| 339 | } |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 340 | |
| 341 | // TODO(zhihuang): Modify these tests. These are used to verify that after |
| 342 | // adding the candidates, the connection_address field is set correctly. Modify |
| 343 | // those so that the "connection address" is tested directly. |
| 344 | // Tests serialization of SDP with only IPv6 candidates and verifies that IPv6 |
| 345 | // is used as default address in c line according to preference. |
| 346 | TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithIPv6Only) { |
| 347 | // Stun has a high preference than local host. |
| 348 | cricket::Candidate candidate1( |
| 349 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 350 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 351 | cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 352 | cricket::Candidate candidate2( |
| 353 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 354 | rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "", |
| 355 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 356 | |
| 357 | JsepIceCandidate jice1("audio", 0, candidate1); |
| 358 | JsepIceCandidate jice2("audio", 0, candidate2); |
| 359 | JsepIceCandidate jice3("video", 0, candidate1); |
| 360 | JsepIceCandidate jice4("video", 0, candidate2); |
| 361 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1)); |
| 362 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2)); |
| 363 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3)); |
| 364 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4)); |
| 365 | std::string message = Serialize(jsep_desc_.get()); |
| 366 | |
| 367 | // Should have a c line like this one. |
| 368 | EXPECT_NE(message.find("c=IN IP6 ::1"), std::string::npos); |
| 369 | // Shouldn't have a IP4 c line. |
| 370 | EXPECT_EQ(message.find("c=IN IP4"), std::string::npos); |
| 371 | } |
| 372 | |
| 373 | // Tests serialization of SDP with both IPv4 and IPv6 candidates and |
| 374 | // verifies that IPv4 is used as default address in c line even if the |
| 375 | // preference of IPv4 is lower. |
| 376 | TEST_F(JsepSessionDescriptionTest, |
| 377 | SerializeSessionDescriptionWithBothIPFamilies) { |
| 378 | cricket::Candidate candidate_v4( |
| 379 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 380 | rtc::SocketAddress("192.168.1.5", 1234), kCandidatePriority, "", "", |
| 381 | cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 382 | cricket::Candidate candidate_v6( |
| 383 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 384 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 385 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 386 | |
| 387 | JsepIceCandidate jice_v4("audio", 0, candidate_v4); |
| 388 | JsepIceCandidate jice_v6("audio", 0, candidate_v6); |
| 389 | JsepIceCandidate jice_v4_video("video", 0, candidate_v4); |
| 390 | JsepIceCandidate jice_v6_video("video", 0, candidate_v6); |
| 391 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4)); |
| 392 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6)); |
| 393 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v4_video)); |
| 394 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice_v6_video)); |
| 395 | std::string message = Serialize(jsep_desc_.get()); |
| 396 | |
| 397 | // Should have a c line like this one. |
| 398 | EXPECT_NE(message.find("c=IN IP4 192.168.1.5"), std::string::npos); |
| 399 | // Shouldn't have a IP6 c line. |
| 400 | EXPECT_EQ(message.find("c=IN IP6"), std::string::npos); |
| 401 | } |
| 402 | |
| 403 | // Tests serialization of SDP with both UDP and TCP candidates and |
| 404 | // verifies that UDP is used as default address in c line even if the |
| 405 | // preference of UDP is lower. |
| 406 | TEST_F(JsepSessionDescriptionTest, |
| 407 | SerializeSessionDescriptionWithBothProtocols) { |
| 408 | // Stun has a high preference than local host. |
| 409 | cricket::Candidate candidate1( |
| 410 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp", |
| 411 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 412 | cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 413 | cricket::Candidate candidate2( |
| 414 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 415 | rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority, |
| 416 | "", "", cricket::LOCAL_PORT_TYPE, kCandidateGeneration, |
| 417 | kCandidateFoundation); |
| 418 | |
| 419 | JsepIceCandidate jice1("audio", 0, candidate1); |
| 420 | JsepIceCandidate jice2("audio", 0, candidate2); |
| 421 | JsepIceCandidate jice3("video", 0, candidate1); |
| 422 | JsepIceCandidate jice4("video", 0, candidate2); |
| 423 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1)); |
| 424 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2)); |
| 425 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3)); |
| 426 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4)); |
| 427 | std::string message = Serialize(jsep_desc_.get()); |
| 428 | |
| 429 | // Should have a c line like this one. |
| 430 | EXPECT_NE(message.find("c=IN IP6 fe80::1234:5678:abcd:ef12"), |
| 431 | std::string::npos); |
| 432 | // Shouldn't have a IP4 c line. |
| 433 | EXPECT_EQ(message.find("c=IN IP4"), std::string::npos); |
| 434 | } |
| 435 | |
| 436 | // Tests serialization of SDP with only TCP candidates and verifies that |
| 437 | // null IPv4 is used as default address in c line. |
| 438 | TEST_F(JsepSessionDescriptionTest, SerializeSessionDescriptionWithTCPOnly) { |
| 439 | // Stun has a high preference than local host. |
| 440 | cricket::Candidate candidate1( |
| 441 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp", |
| 442 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 443 | cricket::STUN_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 444 | cricket::Candidate candidate2( |
| 445 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp", |
| 446 | rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "", |
| 447 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 448 | |
| 449 | JsepIceCandidate jice1("audio", 0, candidate1); |
| 450 | JsepIceCandidate jice2("audio", 0, candidate2); |
| 451 | JsepIceCandidate jice3("video", 0, candidate1); |
| 452 | JsepIceCandidate jice4("video", 0, candidate2); |
| 453 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1)); |
| 454 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2)); |
| 455 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3)); |
| 456 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice4)); |
| 457 | |
| 458 | std::string message = Serialize(jsep_desc_.get()); |
| 459 | EXPECT_EQ(message.find("c=IN IP6 ::3"), std::string::npos); |
| 460 | // Should have a c line like this one when no any default exists. |
| 461 | EXPECT_NE(message.find("c=IN IP4 0.0.0.0"), std::string::npos); |
| 462 | } |
| 463 | |
| 464 | // Tests that the connection address will be correctly set when the Candidate is |
| 465 | // removed. |
| 466 | TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) { |
| 467 | cricket::Candidate candidate1( |
| 468 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 469 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
| 470 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 471 | candidate1.set_transport_name("audio"); |
| 472 | |
| 473 | cricket::Candidate candidate2( |
| 474 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp", |
| 475 | rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "", |
| 476 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 477 | candidate2.set_transport_name("audio"); |
| 478 | |
| 479 | cricket::Candidate candidate3( |
| 480 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 481 | rtc::SocketAddress("192.168.1.1", 1236), kCandidatePriority, "", "", |
| 482 | cricket::LOCAL_PORT_TYPE, kCandidateGeneration, kCandidateFoundation); |
| 483 | candidate3.set_transport_name("audio"); |
| 484 | |
| 485 | JsepIceCandidate jice1("audio", 0, candidate1); |
| 486 | JsepIceCandidate jice2("audio", 0, candidate2); |
| 487 | JsepIceCandidate jice3("audio", 0, candidate3); |
| 488 | |
| 489 | size_t audio_index = 0; |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 490 | auto media_desc = |
| 491 | jsep_desc_->description()->contents()[audio_index].media_description(); |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 492 | |
| 493 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice1)); |
| 494 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice2)); |
| 495 | ASSERT_TRUE(jsep_desc_->AddCandidate(&jice3)); |
| 496 | |
| 497 | std::vector<cricket::Candidate> candidates; |
| 498 | EXPECT_EQ("192.168.1.1:1236", media_desc->connection_address().ToString()); |
| 499 | |
| 500 | candidates.push_back(candidate3); |
| 501 | ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates)); |
| 502 | EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString()); |
| 503 | |
| 504 | candidates.clear(); |
| 505 | candidates.push_back(candidate2); |
| 506 | ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates)); |
| 507 | EXPECT_EQ("[::1]:1234", media_desc->connection_address().ToString()); |
| 508 | |
| 509 | candidates.clear(); |
| 510 | candidates.push_back(candidate1); |
| 511 | ASSERT_TRUE(jsep_desc_->RemoveCandidates(candidates)); |
| 512 | EXPECT_EQ("0.0.0.0:9", media_desc->connection_address().ToString()); |
| 513 | } |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 514 | |
| 515 | class EnumerateAllSdpTypesTest : public ::testing::Test, |
| 516 | public ::testing::WithParamInterface<SdpType> { |
| 517 | }; |
| 518 | |
| 519 | TEST_P(EnumerateAllSdpTypesTest, TestIdentity) { |
| 520 | SdpType type = GetParam(); |
| 521 | |
| 522 | const char* str = webrtc::SdpTypeToString(type); |
| 523 | EXPECT_EQ(type, webrtc::SdpTypeFromString(str)); |
| 524 | } |
| 525 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 526 | INSTANTIATE_TEST_SUITE_P(JsepSessionDescriptionTest, |
| 527 | EnumerateAllSdpTypesTest, |
| 528 | Values(SdpType::kOffer, |
| 529 | SdpType::kPrAnswer, |
| 530 | SdpType::kAnswer)); |