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