henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [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 | [diff] [blame] | 9 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 11 | #include "api/jsep_session_description.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 16 | #include <utility> |
| 17 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 18 | |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 19 | #include "api/candidate.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 20 | #include "api/jsep.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "api/jsep_ice_candidate.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 22 | #include "media/base/codec.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "p2p/base/p2p_constants.h" |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 24 | #include "p2p/base/port.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [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 15:47:49 | [diff] [blame] | 29 | #include "rtc_base/helpers.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 30 | #include "rtc_base/net_helper.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 31 | #include "rtc_base/socket_address.h" |
| 32 | #include "rtc_base/string_encode.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 33 | #include "test/gtest.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 34 | |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 35 | using cricket::MediaProtocolType; |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 36 | using ::testing::Values; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 37 | using webrtc::IceCandidateCollection; |
| 38 | using webrtc::IceCandidateInterface; |
Tomas Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 39 | using webrtc::IceCandidateType; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 40 | using webrtc::JsepIceCandidate; |
| 41 | using webrtc::JsepSessionDescription; |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 42 | using webrtc::SdpType; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 43 | using webrtc::SessionDescriptionInterface; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 44 | |
| 45 | static const char kCandidateUfrag[] = "ufrag"; |
| 46 | static const char kCandidatePwd[] = "pwd"; |
| 47 | static const char kCandidateUfragVoice[] = "ufrag_voice"; |
| 48 | static const char kCandidatePwdVoice[] = "pwd_voice"; |
| 49 | static const char kCandidateUfragVideo[] = "ufrag_video"; |
| 50 | static const char kCandidatePwdVideo[] = "pwd_video"; |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 51 | static const char kCandidateFoundation[] = "a0+B/1"; |
| 52 | static const uint32_t kCandidatePriority = 2130706432U; // pref = 1.0 |
| 53 | static const uint32_t kCandidateGeneration = 2; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 54 | |
| 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 Alvestrand | 4d7160e | 2019-04-12 05:01:29 | [diff] [blame] | 57 | static std::unique_ptr<cricket::SessionDescription> |
| 58 | CreateCricketSessionDescription() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 59 | auto desc = std::make_unique<cricket::SessionDescription>(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 60 | |
Harald Alvestrand | 4d7160e | 2019-04-12 05:01:29 | [diff] [blame] | 61 | // AudioContentDescription |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 62 | auto audio = std::make_unique<cricket::AudioContentDescription>(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 63 | // VideoContentDescription |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 64 | auto video = std::make_unique<cricket::VideoContentDescription>(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 65 | |
Florent Castelli | 8c4b9ea | 2023-06-02 16:06:57 | [diff] [blame] | 66 | audio->AddCodec(cricket::CreateAudioCodec(103, "ISAC", 16000, 0)); |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 67 | desc->AddContent(cricket::CN_AUDIO, MediaProtocolType::kRtp, |
| 68 | std::move(audio)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 69 | |
Florent Castelli | 8c4b9ea | 2023-06-02 16:06:57 | [diff] [blame] | 70 | video->AddCodec(cricket::CreateVideoCodec(120, "VP8")); |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 71 | desc->AddContent(cricket::CN_VIDEO, MediaProtocolType::kRtp, |
| 72 | std::move(video)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 73 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 74 | desc->AddTransportInfo(cricket::TransportInfo( |
deadbeef | 46eed76 | 2016-01-28 21:24:37 | [diff] [blame] | 75 | cricket::CN_AUDIO, |
| 76 | cricket::TransportDescription( |
| 77 | std::vector<std::string>(), kCandidateUfragVoice, kCandidatePwdVoice, |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 78 | cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL))); |
| 79 | desc->AddTransportInfo(cricket::TransportInfo( |
deadbeef | 46eed76 | 2016-01-28 21:24:37 | [diff] [blame] | 80 | cricket::CN_VIDEO, |
| 81 | cricket::TransportDescription( |
| 82 | std::vector<std::string>(), kCandidateUfragVideo, kCandidatePwdVideo, |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 83 | cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, NULL))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 84 | return desc; |
| 85 | } |
| 86 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 13:11:12 | [diff] [blame] | 87 | class JsepSessionDescriptionTest : public ::testing::Test { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 88 | protected: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 89 | virtual void SetUp() { |
| 90 | int port = 1234; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 91 | rtc::SocketAddress address("127.0.0.1", port++); |
guoweis@webrtc.org | 61c1247 | 2015-01-15 06:53:07 | [diff] [blame] | 92 | cricket::Candidate candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
Tomas Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 93 | address, 1, "", "", IceCandidateType::kHost, 0, |
| 94 | "1"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 95 | candidate_ = candidate; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 96 | const std::string session_id = rtc::ToString(rtc::CreateRandomId64()); |
| 97 | const std::string session_version = rtc::ToString(rtc::CreateRandomId()); |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 98 | jsep_desc_ = std::make_unique<JsepSessionDescription>(SdpType::kOffer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 99 | ASSERT_TRUE(jsep_desc_->Initialize(CreateCricketSessionDescription(), |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 100 | session_id, session_version)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 101 | } |
| 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 Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 110 | std::unique_ptr<SessionDescriptionInterface> DeSerialize( |
| 111 | const std::string& sdp) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 112 | auto jsep_desc = std::make_unique<JsepSessionDescription>(SdpType::kOffer); |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 113 | EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jsep_desc.get(), nullptr)); |
| 114 | return std::move(jsep_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | cricket::Candidate candidate_; |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 118 | std::unique_ptr<JsepSessionDescription> jsep_desc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 119 | }; |
| 120 | |
Harald Alvestrand | 0e7b3a9 | 2020-12-15 15:18:28 | [diff] [blame] | 121 | TEST_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 Lee | cb42e10 | 2021-07-15 05:09:40 | [diff] [blame] | 133 | TEST_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 Alvestrand | fc6b871 | 2021-01-05 10:51:08 | [diff] [blame] | 139 | TEST_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 Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 143 | IceCandidateType::kSrflx, kCandidateGeneration, kCandidateFoundation); |
Harald Alvestrand | fc6b871 | 2021-01-05 10:51:08 | [diff] [blame] | 144 | cricket::Candidate candidate_v6( |
| 145 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 146 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
Tomas Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 147 | IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation); |
Harald Alvestrand | fc6b871 | 2021-01-05 10:51:08 | [diff] [blame] | 148 | |
| 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.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 166 | // Test that number_of_mediasections() returns the number of media contents in |
| 167 | // a session description. |
| 168 | TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) { |
| 169 | EXPECT_EQ(2u, jsep_desc_->number_of_mediasections()); |
| 170 | } |
| 171 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 172 | // Test that we can add a candidate to a session description without MID. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 173 | TEST_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 Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 188 | // Test that we can add and remove candidates to a session description with |
| 189 | // MID. Removing candidates requires MID (transport_name). |
| 190 | TEST_F(JsepSessionDescriptionTest, AddAndRemoveCandidatesWithMid) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 191 | // mid and m-line index don't match, in this case mid is preferred. |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 192 | std::string mid = "video"; |
| 193 | JsepIceCandidate jsep_candidate(mid, 0, candidate_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 194 | 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 Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 206 | |
| 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.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | TEST_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. |
| 233 | TEST_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.org | 67ee6b9 | 2014-02-03 16:57:16 | [diff] [blame] | 241 | // Tests that repeatedly adding the same candidate, with or without credentials, |
| 242 | // does not increase the number of candidates in the description. |
| 243 | TEST_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 Wang | 5699142 | 2019-02-08 20:53:06 | [diff] [blame] | 263 | // Test that the connection address is set to a hostname address after adding a |
| 264 | // hostname candidate. |
| 265 | TEST_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 Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 270 | c.set_type(IceCandidateType::kHost); |
Qingsi Wang | 3ae59d3 | 2019-07-12 04:53:45 | [diff] [blame] | 271 | const size_t audio_index = 0; |
| 272 | JsepIceCandidate hostname_candidate("audio", audio_index, c); |
Qingsi Wang | 5699142 | 2019-02-08 20:53:06 | [diff] [blame] | 273 | EXPECT_TRUE(jsep_desc_->AddCandidate(&hostname_candidate)); |
Qingsi Wang | 3ae59d3 | 2019-07-12 04:53:45 | [diff] [blame] | 274 | |
Qingsi Wang | 5699142 | 2019-02-08 20:53:06 | [diff] [blame] | 275 | ASSERT_NE(nullptr, jsep_desc_->description()); |
Qingsi Wang | 3ae59d3 | 2019-07-12 04:53:45 | [diff] [blame] | 276 | 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 Wang | 5699142 | 2019-02-08 20:53:06 | [diff] [blame] | 279 | content.media_description()->connection_address().ToString()); |
| 280 | } |
| 281 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 282 | // Test that we can serialize a JsepSessionDescription and deserialize it again. |
| 283 | TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) { |
| 284 | std::string sdp = Serialize(jsep_desc_.get()); |
| 285 | |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 286 | auto parsed_jsep_desc = DeSerialize(sdp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 287 | 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 Wang | 3ae59d3 | 2019-07-12 04:53:45 | [diff] [blame] | 293 | // 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. |
| 296 | TEST_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 Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 301 | c.set_type(IceCandidateType::kHost); |
Qingsi Wang | 3ae59d3 | 2019-07-12 04:53:45 | [diff] [blame] | 302 | 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.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 326 | // Tests that we can serialize and deserialize a JsepSesssionDescription |
| 327 | // with candidates. |
| 328 | TEST_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 Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 337 | auto parsed_jsep_desc = DeSerialize(sdp_with_candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 338 | std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get()); |
| 339 | |
| 340 | EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate); |
| 341 | } |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 342 | |
| 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. |
| 348 | TEST_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 Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 353 | IceCandidateType::kSrflx, kCandidateGeneration, kCandidateFoundation); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 354 | cricket::Candidate candidate2( |
| 355 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 356 | rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "", |
Tomas Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 357 | IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 358 | |
| 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. |
| 378 | TEST_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 Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 383 | IceCandidateType::kSrflx, kCandidateGeneration, kCandidateFoundation); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 384 | cricket::Candidate candidate_v6( |
| 385 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 386 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
Tomas Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 387 | IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 388 | |
| 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. |
| 408 | TEST_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 Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 414 | IceCandidateType::kSrflx, kCandidateGeneration, kCandidateFoundation); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 415 | cricket::Candidate candidate2( |
| 416 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 417 | rtc::SocketAddress("fe80::1234:5678:abcd:ef12", 1235), kCandidatePriority, |
Tomas Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 418 | "", "", IceCandidateType::kHost, kCandidateGeneration, |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 419 | 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. |
| 440 | TEST_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 Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 445 | IceCandidateType::kSrflx, kCandidateGeneration, kCandidateFoundation); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 446 | cricket::Candidate candidate2( |
| 447 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp", |
| 448 | rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "", |
Tomas Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 449 | IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 450 | |
| 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. |
| 468 | TEST_F(JsepSessionDescriptionTest, RemoveCandidateAndSetConnectionAddress) { |
| 469 | cricket::Candidate candidate1( |
| 470 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "udp", |
| 471 | rtc::SocketAddress("::1", 1234), kCandidatePriority, "", "", |
Tomas Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 472 | IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 473 | candidate1.set_transport_name("audio"); |
| 474 | |
| 475 | cricket::Candidate candidate2( |
| 476 | cricket::ICE_CANDIDATE_COMPONENT_RTP, "tcp", |
| 477 | rtc::SocketAddress("::2", 1235), kCandidatePriority, "", "", |
Tomas Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 478 | IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 479 | 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 Gunnarsson | 0242939 | 2024-03-07 15:24:55 | [diff] [blame] | 484 | IceCandidateType::kHost, kCandidateGeneration, kCandidateFoundation); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 485 | 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 Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 492 | auto media_desc = |
| 493 | jsep_desc_->description()->contents()[audio_index].media_description(); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 494 | |
| 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 Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 516 | |
| 517 | class EnumerateAllSdpTypesTest : public ::testing::Test, |
| 518 | public ::testing::WithParamInterface<SdpType> { |
| 519 | }; |
| 520 | |
| 521 | TEST_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 Bonadei | c84f661 | 2019-01-31 11:20:57 | [diff] [blame] | 528 | INSTANTIATE_TEST_SUITE_P(JsepSessionDescriptionTest, |
| 529 | EnumerateAllSdpTypesTest, |
| 530 | Values(SdpType::kOffer, |
| 531 | SdpType::kPrAnswer, |
| 532 | SdpType::kAnswer)); |