Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include "p2p/base/fakeportallocator.h" |
| 12 | #include "p2p/base/teststunserver.h" |
| 13 | #include "p2p/client/basicportallocator.h" |
| 14 | #include "pc/mediasession.h" |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 15 | #include "pc/peerconnection.h" |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 16 | #include "pc/peerconnectionwrapper.h" |
| 17 | #include "pc/sdputils.h" |
| 18 | #ifdef WEBRTC_ANDROID |
| 19 | #include "pc/test/androidtestinitializer.h" |
| 20 | #endif |
Karl Wiberg | 1b0eae3 | 2017-10-17 14:48:54 +0200 | [diff] [blame] | 21 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 22 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 23 | #include "api/peerconnectionproxy.h" |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 24 | #include "pc/test/fakeaudiocapturemodule.h" |
| 25 | #include "rtc_base/fakenetwork.h" |
| 26 | #include "rtc_base/gunit.h" |
| 27 | #include "rtc_base/ptr_util.h" |
| 28 | #include "rtc_base/virtualsocketserver.h" |
| 29 | |
| 30 | namespace webrtc { |
| 31 | |
| 32 | using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; |
| 33 | using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions; |
| 34 | using rtc::SocketAddress; |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 35 | using ::testing::Combine; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 36 | using ::testing::Values; |
| 37 | |
| 38 | constexpr int kIceCandidatesTimeout = 10000; |
| 39 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 40 | class PeerConnectionWrapperForIceTest : public PeerConnectionWrapper { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 41 | public: |
| 42 | using PeerConnectionWrapper::PeerConnectionWrapper; |
| 43 | |
| 44 | // Adds a new ICE candidate to the first transport. |
| 45 | bool AddIceCandidate(cricket::Candidate* candidate) { |
| 46 | RTC_DCHECK(pc()->remote_description()); |
| 47 | const auto* desc = pc()->remote_description()->description(); |
| 48 | RTC_DCHECK(desc->contents().size() > 0); |
| 49 | const auto& first_content = desc->contents()[0]; |
| 50 | candidate->set_transport_name(first_content.name); |
| 51 | JsepIceCandidate jsep_candidate(first_content.name, 0, *candidate); |
| 52 | return pc()->AddIceCandidate(&jsep_candidate); |
| 53 | } |
| 54 | |
| 55 | // Returns ICE candidates from the remote session description. |
| 56 | std::vector<const IceCandidateInterface*> |
| 57 | GetIceCandidatesFromRemoteDescription() { |
| 58 | const SessionDescriptionInterface* sdesc = pc()->remote_description(); |
| 59 | RTC_DCHECK(sdesc); |
| 60 | std::vector<const IceCandidateInterface*> candidates; |
| 61 | for (size_t mline_index = 0; mline_index < sdesc->number_of_mediasections(); |
| 62 | mline_index++) { |
| 63 | const auto* candidate_collection = sdesc->candidates(mline_index); |
| 64 | for (size_t i = 0; i < candidate_collection->count(); i++) { |
| 65 | candidates.push_back(candidate_collection->at(i)); |
| 66 | } |
| 67 | } |
| 68 | return candidates; |
| 69 | } |
| 70 | |
| 71 | rtc::FakeNetworkManager* network() { return network_; } |
| 72 | |
| 73 | void set_network(rtc::FakeNetworkManager* network) { network_ = network; } |
| 74 | |
| 75 | private: |
| 76 | rtc::FakeNetworkManager* network_; |
| 77 | }; |
| 78 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 79 | class PeerConnectionIceBaseTest : public ::testing::Test { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 80 | protected: |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 81 | typedef std::unique_ptr<PeerConnectionWrapperForIceTest> WrapperPtr; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 82 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 83 | explicit PeerConnectionIceBaseTest(SdpSemantics sdp_semantics) |
| 84 | : vss_(new rtc::VirtualSocketServer()), |
| 85 | main_(vss_.get()), |
| 86 | sdp_semantics_(sdp_semantics) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 87 | #ifdef WEBRTC_ANDROID |
| 88 | InitializeAndroidObjects(); |
| 89 | #endif |
| 90 | pc_factory_ = CreatePeerConnectionFactory( |
| 91 | rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(), |
Karl Wiberg | 1b0eae3 | 2017-10-17 14:48:54 +0200 | [diff] [blame] | 92 | FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(), |
| 93 | CreateBuiltinAudioDecoderFactory(), nullptr, nullptr); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | WrapperPtr CreatePeerConnection() { |
| 97 | return CreatePeerConnection(RTCConfiguration()); |
| 98 | } |
| 99 | |
| 100 | WrapperPtr CreatePeerConnection(const RTCConfiguration& config) { |
| 101 | auto* fake_network = NewFakeNetwork(); |
| 102 | auto port_allocator = |
| 103 | rtc::MakeUnique<cricket::BasicPortAllocator>(fake_network); |
| 104 | port_allocator->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP | |
| 105 | cricket::PORTALLOCATOR_DISABLE_RELAY); |
| 106 | port_allocator->set_step_delay(cricket::kMinimumStepDelay); |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 107 | RTCConfiguration modified_config = config; |
| 108 | modified_config.sdp_semantics = sdp_semantics_; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 109 | auto observer = rtc::MakeUnique<MockPeerConnectionObserver>(); |
| 110 | auto pc = pc_factory_->CreatePeerConnection( |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 111 | modified_config, std::move(port_allocator), nullptr, observer.get()); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 112 | if (!pc) { |
| 113 | return nullptr; |
| 114 | } |
| 115 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 116 | auto wrapper = rtc::MakeUnique<PeerConnectionWrapperForIceTest>( |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 117 | pc_factory_, pc, std::move(observer)); |
| 118 | wrapper->set_network(fake_network); |
| 119 | return wrapper; |
| 120 | } |
| 121 | |
| 122 | // Accepts the same arguments as CreatePeerConnection and adds default audio |
| 123 | // and video tracks. |
| 124 | template <typename... Args> |
| 125 | WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) { |
| 126 | auto wrapper = CreatePeerConnection(std::forward<Args>(args)...); |
| 127 | if (!wrapper) { |
| 128 | return nullptr; |
| 129 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 130 | wrapper->AddAudioTrack("a"); |
| 131 | wrapper->AddVideoTrack("v"); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 132 | return wrapper; |
| 133 | } |
| 134 | |
| 135 | cricket::Candidate CreateLocalUdpCandidate( |
| 136 | const rtc::SocketAddress& address) { |
| 137 | cricket::Candidate candidate; |
| 138 | candidate.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 139 | candidate.set_protocol(cricket::UDP_PROTOCOL_NAME); |
| 140 | candidate.set_address(address); |
| 141 | candidate.set_type(cricket::LOCAL_PORT_TYPE); |
| 142 | return candidate; |
| 143 | } |
| 144 | |
| 145 | // Remove all ICE ufrag/pwd lines from the given session description. |
| 146 | void RemoveIceUfragPwd(SessionDescriptionInterface* sdesc) { |
| 147 | SetIceUfragPwd(sdesc, "", ""); |
| 148 | } |
| 149 | |
| 150 | // Sets all ICE ufrag/pwds on the given session description. |
| 151 | void SetIceUfragPwd(SessionDescriptionInterface* sdesc, |
| 152 | const std::string& ufrag, |
| 153 | const std::string& pwd) { |
| 154 | auto* desc = sdesc->description(); |
| 155 | for (const auto& content : desc->contents()) { |
| 156 | auto* transport_info = desc->GetTransportInfoByName(content.name); |
| 157 | transport_info->description.ice_ufrag = ufrag; |
| 158 | transport_info->description.ice_pwd = pwd; |
| 159 | } |
| 160 | } |
| 161 | |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 162 | // Set ICE mode on the given session description. |
| 163 | void SetIceMode(SessionDescriptionInterface* sdesc, |
| 164 | const cricket::IceMode ice_mode) { |
| 165 | auto* desc = sdesc->description(); |
| 166 | for (const auto& content : desc->contents()) { |
| 167 | auto* transport_info = desc->GetTransportInfoByName(content.name); |
| 168 | transport_info->description.ice_mode = ice_mode; |
| 169 | } |
| 170 | } |
| 171 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 172 | cricket::TransportDescription* GetFirstTransportDescription( |
| 173 | SessionDescriptionInterface* sdesc) { |
| 174 | auto* desc = sdesc->description(); |
| 175 | RTC_DCHECK(desc->contents().size() > 0); |
| 176 | auto* transport_info = |
| 177 | desc->GetTransportInfoByName(desc->contents()[0].name); |
| 178 | RTC_DCHECK(transport_info); |
| 179 | return &transport_info->description; |
| 180 | } |
| 181 | |
| 182 | const cricket::TransportDescription* GetFirstTransportDescription( |
| 183 | const SessionDescriptionInterface* sdesc) { |
| 184 | auto* desc = sdesc->description(); |
| 185 | RTC_DCHECK(desc->contents().size() > 0); |
| 186 | auto* transport_info = |
| 187 | desc->GetTransportInfoByName(desc->contents()[0].name); |
| 188 | RTC_DCHECK(transport_info); |
| 189 | return &transport_info->description; |
| 190 | } |
| 191 | |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 192 | // TODO(qingsi): Rewrite this method in terms of the standard IceTransport |
| 193 | // after it is implemented. |
| 194 | cricket::IceRole GetIceRole(const WrapperPtr& pc_wrapper_ptr) { |
Mirko Bonadei | e97de91 | 2017-12-13 11:29:34 +0100 | [diff] [blame] | 195 | auto* pc_proxy = |
| 196 | static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>( |
| 197 | pc_wrapper_ptr->pc()); |
| 198 | PeerConnection* pc = static_cast<PeerConnection*>(pc_proxy->internal()); |
Steve Anton | b886711 | 2018-02-13 10:07:54 -0800 | [diff] [blame] | 199 | for (auto transceiver : pc->GetTransceiversInternal()) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 200 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 201 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 202 | if (channel) { |
| 203 | return channel->rtp_dtls_transport()->ice_transport()->GetIceRole(); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | RTC_NOTREACHED(); |
| 208 | return cricket::ICEROLE_UNKNOWN; |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 211 | bool AddCandidateToFirstTransport(cricket::Candidate* candidate, |
| 212 | SessionDescriptionInterface* sdesc) { |
| 213 | auto* desc = sdesc->description(); |
| 214 | RTC_DCHECK(desc->contents().size() > 0); |
| 215 | const auto& first_content = desc->contents()[0]; |
| 216 | candidate->set_transport_name(first_content.name); |
| 217 | JsepIceCandidate jsep_candidate(first_content.name, 0, *candidate); |
| 218 | return sdesc->AddCandidate(&jsep_candidate); |
| 219 | } |
| 220 | |
| 221 | rtc::FakeNetworkManager* NewFakeNetwork() { |
| 222 | // The PeerConnection's port allocator is tied to the PeerConnection's |
| 223 | // lifetime and expects the underlying NetworkManager to outlive it. That |
| 224 | // prevents us from having the PeerConnectionWrapper own the fake network. |
| 225 | // Therefore, the test fixture will own all the fake networks even though |
| 226 | // tests should access the fake network through the PeerConnectionWrapper. |
| 227 | auto* fake_network = new rtc::FakeNetworkManager(); |
| 228 | fake_networks_.emplace_back(fake_network); |
| 229 | return fake_network; |
| 230 | } |
| 231 | |
| 232 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 233 | rtc::AutoSocketServerThread main_; |
| 234 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_; |
| 235 | std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_; |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 236 | const SdpSemantics sdp_semantics_; |
| 237 | }; |
| 238 | |
| 239 | class PeerConnectionIceTest |
| 240 | : public PeerConnectionIceBaseTest, |
| 241 | public ::testing::WithParamInterface<SdpSemantics> { |
| 242 | protected: |
| 243 | PeerConnectionIceTest() : PeerConnectionIceBaseTest(GetParam()) {} |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | ::testing::AssertionResult AssertCandidatesEqual(const char* a_expr, |
| 247 | const char* b_expr, |
| 248 | const cricket::Candidate& a, |
| 249 | const cricket::Candidate& b) { |
| 250 | std::stringstream failure_info; |
| 251 | if (a.component() != b.component()) { |
| 252 | failure_info << "\ncomponent: " << a.component() << " != " << b.component(); |
| 253 | } |
| 254 | if (a.protocol() != b.protocol()) { |
| 255 | failure_info << "\nprotocol: " << a.protocol() << " != " << b.protocol(); |
| 256 | } |
| 257 | if (a.address() != b.address()) { |
| 258 | failure_info << "\naddress: " << a.address().ToString() |
| 259 | << " != " << b.address().ToString(); |
| 260 | } |
| 261 | if (a.type() != b.type()) { |
| 262 | failure_info << "\ntype: " << a.type() << " != " << b.type(); |
| 263 | } |
| 264 | std::string failure_info_str = failure_info.str(); |
| 265 | if (failure_info_str.empty()) { |
| 266 | return ::testing::AssertionSuccess(); |
| 267 | } else { |
| 268 | return ::testing::AssertionFailure() |
| 269 | << a_expr << " and " << b_expr << " are not equal" |
| 270 | << failure_info_str; |
| 271 | } |
| 272 | } |
| 273 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 274 | TEST_P(PeerConnectionIceTest, OfferContainsGatheredCandidates) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 275 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 276 | |
| 277 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 278 | caller->network()->AddInterface(kLocalAddress); |
| 279 | |
| 280 | // Start ICE candidate gathering by setting the local offer. |
| 281 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 282 | |
| 283 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 284 | |
| 285 | auto offer = caller->CreateOffer(); |
| 286 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size()); |
| 287 | EXPECT_EQ(caller->observer()->GetCandidatesByMline(0).size(), |
| 288 | offer->candidates(0)->count()); |
| 289 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size()); |
| 290 | EXPECT_EQ(caller->observer()->GetCandidatesByMline(1).size(), |
| 291 | offer->candidates(1)->count()); |
| 292 | } |
| 293 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 294 | TEST_P(PeerConnectionIceTest, AnswerContainsGatheredCandidates) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 295 | const SocketAddress kCallerAddress("1.1.1.1", 0); |
| 296 | |
| 297 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 298 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 299 | caller->network()->AddInterface(kCallerAddress); |
| 300 | |
| 301 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 302 | ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer())); |
| 303 | |
| 304 | EXPECT_TRUE_WAIT(callee->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 305 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 306 | auto* answer = callee->pc()->local_description(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 307 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size()); |
| 308 | EXPECT_EQ(callee->observer()->GetCandidatesByMline(0).size(), |
| 309 | answer->candidates(0)->count()); |
| 310 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size()); |
| 311 | EXPECT_EQ(callee->observer()->GetCandidatesByMline(1).size(), |
| 312 | answer->candidates(1)->count()); |
| 313 | } |
| 314 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 315 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 316 | CanSetRemoteSessionDescriptionWithRemoteCandidates) { |
| 317 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 318 | |
| 319 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 320 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 321 | |
| 322 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 323 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 324 | AddCandidateToFirstTransport(&candidate, offer.get()); |
| 325 | |
| 326 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 327 | auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription(); |
| 328 | ASSERT_EQ(1u, remote_candidates.size()); |
| 329 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate, |
| 330 | remote_candidates[0]->candidate()); |
| 331 | } |
| 332 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 333 | TEST_P(PeerConnectionIceTest, SetLocalDescriptionFailsIfNoIceCredentials) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 334 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 335 | |
| 336 | auto offer = caller->CreateOffer(); |
| 337 | RemoveIceUfragPwd(offer.get()); |
| 338 | |
| 339 | EXPECT_FALSE(caller->SetLocalDescription(std::move(offer))); |
| 340 | } |
| 341 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 342 | TEST_P(PeerConnectionIceTest, SetRemoteDescriptionFailsIfNoIceCredentials) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 343 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 344 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 345 | |
| 346 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 347 | RemoveIceUfragPwd(offer.get()); |
| 348 | |
| 349 | EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer))); |
| 350 | } |
| 351 | |
| 352 | // The following group tests that ICE candidates are not generated before |
| 353 | // SetLocalDescription is called on a PeerConnection. |
| 354 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 355 | TEST_P(PeerConnectionIceTest, NoIceCandidatesBeforeSetLocalDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 356 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 357 | |
| 358 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 359 | caller->network()->AddInterface(kLocalAddress); |
| 360 | |
| 361 | // Pump for 1 second and verify that no candidates are generated. |
| 362 | rtc::Thread::Current()->ProcessMessages(1000); |
| 363 | |
| 364 | EXPECT_EQ(0u, caller->observer()->candidates_.size()); |
| 365 | } |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 366 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 367 | NoIceCandidatesBeforeAnswerSetAsLocalDescription) { |
| 368 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 369 | |
| 370 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 371 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 372 | caller->network()->AddInterface(kCallerAddress); |
| 373 | |
| 374 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 375 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 376 | AddCandidateToFirstTransport(&candidate, offer.get()); |
| 377 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 378 | |
| 379 | // Pump for 1 second and verify that no candidates are generated. |
| 380 | rtc::Thread::Current()->ProcessMessages(1000); |
| 381 | |
| 382 | EXPECT_EQ(0u, callee->observer()->candidates_.size()); |
| 383 | } |
| 384 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 385 | TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenRemoteDescriptionNotSet) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 386 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 387 | |
| 388 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 389 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
| 390 | JsepIceCandidate jsep_candidate(cricket::CN_AUDIO, 0, candidate); |
| 391 | |
| 392 | EXPECT_FALSE(caller->pc()->AddIceCandidate(&jsep_candidate)); |
| 393 | |
| 394 | caller->CreateOfferAndSetAsLocal(); |
| 395 | |
| 396 | EXPECT_FALSE(caller->pc()->AddIceCandidate(&jsep_candidate)); |
| 397 | } |
| 398 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 399 | TEST_P(PeerConnectionIceTest, DuplicateIceCandidateIgnoredWhenAdded) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 400 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 401 | |
| 402 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 403 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 404 | |
| 405 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 406 | ASSERT_TRUE( |
| 407 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 408 | |
| 409 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
| 410 | caller->AddIceCandidate(&candidate); |
| 411 | EXPECT_TRUE(caller->AddIceCandidate(&candidate)); |
| 412 | EXPECT_EQ(1u, caller->GetIceCandidatesFromRemoteDescription().size()); |
| 413 | } |
| 414 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 415 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 416 | AddRemoveCandidateWithEmptyTransportDoesNotCrash) { |
| 417 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 418 | |
| 419 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 420 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 421 | |
| 422 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 423 | ASSERT_TRUE( |
| 424 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 425 | |
| 426 | // |candidate.transport_name()| is empty. |
| 427 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 428 | auto* audio_content = cricket::GetFirstAudioContent( |
| 429 | caller->pc()->local_description()->description()); |
| 430 | JsepIceCandidate ice_candidate(audio_content->name, 0, candidate); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 431 | EXPECT_TRUE(caller->pc()->AddIceCandidate(&ice_candidate)); |
| 432 | EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate})); |
| 433 | } |
| 434 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 435 | TEST_P(PeerConnectionIceTest, RemoveCandidateRemovesFromRemoteDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 436 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 437 | |
| 438 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 439 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 440 | |
| 441 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 442 | ASSERT_TRUE( |
| 443 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 444 | |
| 445 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
| 446 | ASSERT_TRUE(caller->AddIceCandidate(&candidate)); |
| 447 | EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate})); |
| 448 | EXPECT_EQ(0u, caller->GetIceCandidatesFromRemoteDescription().size()); |
| 449 | } |
| 450 | |
| 451 | // Test that if a candidate is added via AddIceCandidate and via an updated |
| 452 | // remote description, then both candidates appear in the stored remote |
| 453 | // description. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 454 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 455 | CandidateInSubsequentOfferIsAddedToRemoteDescription) { |
| 456 | const SocketAddress kCallerAddress1("1.1.1.1", 1111); |
| 457 | const SocketAddress kCallerAddress2("2.2.2.2", 2222); |
| 458 | |
| 459 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 460 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 461 | |
| 462 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 463 | ASSERT_TRUE( |
| 464 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 465 | |
| 466 | // Add one candidate via |AddIceCandidate|. |
| 467 | cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCallerAddress1); |
| 468 | ASSERT_TRUE(callee->AddIceCandidate(&candidate1)); |
| 469 | |
| 470 | // Add the second candidate via a reoffer. |
| 471 | auto offer = caller->CreateOffer(); |
| 472 | cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCallerAddress2); |
| 473 | AddCandidateToFirstTransport(&candidate2, offer.get()); |
| 474 | |
| 475 | // Expect both candidates to appear in the callee's remote description. |
| 476 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 477 | EXPECT_EQ(2u, callee->GetIceCandidatesFromRemoteDescription().size()); |
| 478 | } |
| 479 | |
| 480 | // The follow test verifies that SetLocal/RemoteDescription fails when an offer |
| 481 | // has either ICE ufrag/pwd too short or too long and succeeds otherwise. |
| 482 | // The standard (https://tools.ietf.org/html/rfc5245#section-15.4) says that |
| 483 | // pwd must be 22-256 characters and ufrag must be 4-256 characters. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 484 | TEST_P(PeerConnectionIceTest, VerifyUfragPwdLength) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 485 | auto set_local_description_with_ufrag_pwd_length = |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 486 | [this](int ufrag_len, int pwd_len) { |
| 487 | auto pc = CreatePeerConnectionWithAudioVideo(); |
| 488 | auto offer = pc->CreateOffer(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 489 | SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'), |
| 490 | std::string(pwd_len, 'x')); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 491 | return pc->SetLocalDescription(std::move(offer)); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | auto set_remote_description_with_ufrag_pwd_length = |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 495 | [this](int ufrag_len, int pwd_len) { |
| 496 | auto pc = CreatePeerConnectionWithAudioVideo(); |
| 497 | auto offer = pc->CreateOffer(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 498 | SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'), |
| 499 | std::string(pwd_len, 'x')); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 500 | return pc->SetRemoteDescription(std::move(offer)); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 501 | }; |
| 502 | |
| 503 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(3, 22)); |
| 504 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(3, 22)); |
| 505 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(257, 22)); |
| 506 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(257, 22)); |
| 507 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 21)); |
| 508 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 21)); |
| 509 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 257)); |
| 510 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 257)); |
| 511 | EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(4, 22)); |
| 512 | EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(4, 22)); |
| 513 | EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(256, 256)); |
| 514 | EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(256, 256)); |
| 515 | } |
| 516 | |
| 517 | ::testing::AssertionResult AssertIpInCandidates( |
| 518 | const char* address_expr, |
| 519 | const char* candidates_expr, |
| 520 | const SocketAddress& address, |
| 521 | const std::vector<IceCandidateInterface*> candidates) { |
| 522 | std::stringstream candidate_hosts; |
| 523 | for (const auto* candidate : candidates) { |
| 524 | const auto& candidate_ip = candidate->candidate().address().ipaddr(); |
| 525 | if (candidate_ip == address.ipaddr()) { |
| 526 | return ::testing::AssertionSuccess(); |
| 527 | } |
| 528 | candidate_hosts << "\n" << candidate_ip; |
| 529 | } |
| 530 | return ::testing::AssertionFailure() |
| 531 | << address_expr << " (host " << address.HostAsURIString() |
| 532 | << ") not in " << candidates_expr |
| 533 | << " which have the following address hosts:" << candidate_hosts.str(); |
| 534 | } |
| 535 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 536 | TEST_P(PeerConnectionIceTest, CandidatesGeneratedForEachLocalInterface) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 537 | const SocketAddress kLocalAddress1("1.1.1.1", 0); |
| 538 | const SocketAddress kLocalAddress2("2.2.2.2", 0); |
| 539 | |
| 540 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 541 | caller->network()->AddInterface(kLocalAddress1); |
| 542 | caller->network()->AddInterface(kLocalAddress2); |
| 543 | |
| 544 | caller->CreateOfferAndSetAsLocal(); |
| 545 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 546 | |
| 547 | auto candidates = caller->observer()->GetCandidatesByMline(0); |
| 548 | EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress1, candidates); |
| 549 | EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress2, candidates); |
| 550 | } |
| 551 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 552 | TEST_P(PeerConnectionIceTest, TrickledSingleCandidateAddedToRemoteDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 553 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 554 | |
| 555 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 556 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 557 | |
| 558 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 559 | |
| 560 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 561 | callee->AddIceCandidate(&candidate); |
| 562 | auto candidates = callee->GetIceCandidatesFromRemoteDescription(); |
| 563 | ASSERT_EQ(1u, candidates.size()); |
| 564 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate, |
| 565 | candidates[0]->candidate()); |
| 566 | } |
| 567 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 568 | TEST_P(PeerConnectionIceTest, TwoTrickledCandidatesAddedToRemoteDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 569 | const SocketAddress kCalleeAddress1("1.1.1.1", 1111); |
| 570 | const SocketAddress kCalleeAddress2("2.2.2.2", 2222); |
| 571 | |
| 572 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 573 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 574 | |
| 575 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 576 | ASSERT_TRUE( |
| 577 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 578 | |
| 579 | cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCalleeAddress1); |
| 580 | caller->AddIceCandidate(&candidate1); |
| 581 | |
| 582 | cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCalleeAddress2); |
| 583 | caller->AddIceCandidate(&candidate2); |
| 584 | |
| 585 | auto candidates = caller->GetIceCandidatesFromRemoteDescription(); |
| 586 | ASSERT_EQ(2u, candidates.size()); |
| 587 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate1, |
| 588 | candidates[0]->candidate()); |
| 589 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate2, |
| 590 | candidates[1]->candidate()); |
| 591 | } |
| 592 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 593 | TEST_P(PeerConnectionIceTest, LocalDescriptionUpdatedWhenContinualGathering) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 594 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 595 | |
| 596 | RTCConfiguration config; |
| 597 | config.continual_gathering_policy = |
| 598 | PeerConnectionInterface::GATHER_CONTINUALLY; |
| 599 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 600 | caller->network()->AddInterface(kLocalAddress); |
| 601 | |
| 602 | // Start ICE candidate gathering by setting the local offer. |
| 603 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 604 | |
| 605 | // Since we're using continual gathering, we won't get "gathering done". |
| 606 | EXPECT_TRUE_WAIT( |
| 607 | caller->pc()->local_description()->candidates(0)->count() > 0, |
| 608 | kIceCandidatesTimeout); |
| 609 | } |
| 610 | |
| 611 | // Test that when continual gathering is enabled, and a network interface goes |
| 612 | // down, the candidate is signaled as removed and removed from the local |
| 613 | // description. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 614 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 615 | LocalCandidatesRemovedWhenNetworkDownIfGatheringContinually) { |
| 616 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 617 | |
| 618 | RTCConfiguration config; |
| 619 | config.continual_gathering_policy = |
| 620 | PeerConnectionInterface::GATHER_CONTINUALLY; |
| 621 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 622 | caller->network()->AddInterface(kLocalAddress); |
| 623 | |
| 624 | // Start ICE candidate gathering by setting the local offer. |
| 625 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 626 | |
| 627 | EXPECT_TRUE_WAIT( |
| 628 | caller->pc()->local_description()->candidates(0)->count() > 0, |
| 629 | kIceCandidatesTimeout); |
| 630 | |
| 631 | // Remove the only network interface, causing the PeerConnection to signal |
| 632 | // the removal of all candidates derived from this interface. |
| 633 | caller->network()->RemoveInterface(kLocalAddress); |
| 634 | |
| 635 | EXPECT_EQ_WAIT(0u, caller->pc()->local_description()->candidates(0)->count(), |
| 636 | kIceCandidatesTimeout); |
| 637 | EXPECT_LT(0, caller->observer()->num_candidates_removed_); |
| 638 | } |
| 639 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 640 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 641 | LocalCandidatesNotRemovedWhenNetworkDownIfGatheringOnce) { |
| 642 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 643 | |
| 644 | RTCConfiguration config; |
| 645 | config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE; |
| 646 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 647 | caller->network()->AddInterface(kLocalAddress); |
| 648 | |
| 649 | // Start ICE candidate gathering by setting the local offer. |
| 650 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 651 | |
| 652 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 653 | |
| 654 | caller->network()->RemoveInterface(kLocalAddress); |
| 655 | |
| 656 | // Verify that the local candidates are not removed; |
| 657 | rtc::Thread::Current()->ProcessMessages(1000); |
| 658 | EXPECT_EQ(0, caller->observer()->num_candidates_removed_); |
| 659 | } |
| 660 | |
| 661 | // The following group tests that when an offer includes a new ufrag or pwd |
| 662 | // (indicating an ICE restart) the old candidates are removed and new candidates |
| 663 | // added to the remote description. |
| 664 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 665 | TEST_P(PeerConnectionIceTest, IceRestartOfferClearsExistingCandidate) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 666 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 667 | |
| 668 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 669 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 670 | |
| 671 | auto offer = caller->CreateOffer(); |
| 672 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 673 | AddCandidateToFirstTransport(&candidate, offer.get()); |
| 674 | |
| 675 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 676 | |
| 677 | RTCOfferAnswerOptions options; |
| 678 | options.ice_restart = true; |
| 679 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer(options))); |
| 680 | |
| 681 | EXPECT_EQ(0u, callee->GetIceCandidatesFromRemoteDescription().size()); |
| 682 | } |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 683 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 684 | IceRestartOfferCandidateReplacesExistingCandidate) { |
| 685 | const SocketAddress kFirstCallerAddress("1.1.1.1", 1111); |
| 686 | const SocketAddress kRestartedCallerAddress("2.2.2.2", 2222); |
| 687 | |
| 688 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 689 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 690 | |
| 691 | auto offer = caller->CreateOffer(); |
| 692 | cricket::Candidate old_candidate = |
| 693 | CreateLocalUdpCandidate(kFirstCallerAddress); |
| 694 | AddCandidateToFirstTransport(&old_candidate, offer.get()); |
| 695 | |
| 696 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 697 | |
| 698 | RTCOfferAnswerOptions options; |
| 699 | options.ice_restart = true; |
| 700 | auto restart_offer = caller->CreateOffer(options); |
| 701 | cricket::Candidate new_candidate = |
| 702 | CreateLocalUdpCandidate(kRestartedCallerAddress); |
| 703 | AddCandidateToFirstTransport(&new_candidate, restart_offer.get()); |
| 704 | |
| 705 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(restart_offer))); |
| 706 | |
| 707 | auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription(); |
| 708 | ASSERT_EQ(1u, remote_candidates.size()); |
| 709 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, new_candidate, |
| 710 | remote_candidates[0]->candidate()); |
| 711 | } |
| 712 | |
| 713 | // Test that if there is not an ICE restart (i.e., nothing changes), then the |
| 714 | // answer to a later offer should have the same ufrag/pwd as the first answer. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 715 | TEST_P(PeerConnectionIceTest, LaterAnswerHasSameIceCredentialsIfNoIceRestart) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 716 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 717 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 718 | |
| 719 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 720 | ASSERT_TRUE( |
| 721 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 722 | |
| 723 | // Re-offer. |
| 724 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 725 | |
| 726 | auto answer = callee->CreateAnswer(); |
| 727 | auto* answer_transport_desc = GetFirstTransportDescription(answer.get()); |
| 728 | auto* local_transport_desc = |
| 729 | GetFirstTransportDescription(callee->pc()->local_description()); |
| 730 | |
| 731 | EXPECT_EQ(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag); |
| 732 | EXPECT_EQ(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd); |
| 733 | } |
| 734 | |
| 735 | // The following parameterized test verifies that if an offer is sent with a |
| 736 | // modified ICE ufrag and/or ICE pwd, then the answer should identify that the |
| 737 | // other side has initiated an ICE restart and generate a new ufrag and pwd. |
| 738 | // RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or |
| 739 | // a=ice-pwd attributes compared to the previous SDP from the peer, it |
| 740 | // indicates that ICE is restarting for this media stream." |
| 741 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 742 | class PeerConnectionIceUfragPwdAnswerTest |
| 743 | : public PeerConnectionIceBaseTest, |
| 744 | public ::testing::WithParamInterface< |
| 745 | std::tuple<SdpSemantics, std::tuple<bool, bool>>> { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 746 | protected: |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 747 | PeerConnectionIceUfragPwdAnswerTest() |
| 748 | : PeerConnectionIceBaseTest(std::get<0>(GetParam())) { |
| 749 | auto param = std::get<1>(GetParam()); |
| 750 | offer_new_ufrag_ = std::get<0>(param); |
| 751 | offer_new_pwd_ = std::get<1>(param); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | bool offer_new_ufrag_; |
| 755 | bool offer_new_pwd_; |
| 756 | }; |
| 757 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 758 | TEST_P(PeerConnectionIceUfragPwdAnswerTest, TestIncludedInAnswer) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 759 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 760 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 761 | |
| 762 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 763 | ASSERT_TRUE( |
| 764 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 765 | |
| 766 | auto offer = caller->CreateOffer(); |
| 767 | auto* offer_transport_desc = GetFirstTransportDescription(offer.get()); |
| 768 | if (offer_new_ufrag_) { |
| 769 | offer_transport_desc->ice_ufrag += "_new"; |
| 770 | } |
| 771 | if (offer_new_pwd_) { |
| 772 | offer_transport_desc->ice_pwd += "_new"; |
| 773 | } |
| 774 | |
| 775 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 776 | |
| 777 | auto answer = callee->CreateAnswer(); |
| 778 | auto* answer_transport_desc = GetFirstTransportDescription(answer.get()); |
| 779 | auto* local_transport_desc = |
| 780 | GetFirstTransportDescription(callee->pc()->local_description()); |
| 781 | |
| 782 | EXPECT_NE(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag); |
| 783 | EXPECT_NE(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd); |
| 784 | } |
| 785 | |
| 786 | INSTANTIATE_TEST_CASE_P( |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 787 | PeerConnectionIceTest, |
| 788 | PeerConnectionIceUfragPwdAnswerTest, |
| 789 | Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan), |
| 790 | Values(std::make_pair(true, true), // Both changed. |
| 791 | std::make_pair(true, false), // Only ufrag changed. |
| 792 | std::make_pair(false, true)))); // Only pwd changed. |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 793 | |
| 794 | // Test that if an ICE restart is offered on one media section, then the answer |
| 795 | // will only change ICE ufrag/pwd for that section and keep the other sections |
| 796 | // the same. |
| 797 | // Note that this only works if we have disabled BUNDLE, otherwise all media |
| 798 | // sections will share the same transport. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 799 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 800 | CreateAnswerHasNewUfragPwdForOnlyMediaSectionWhichRestarted) { |
| 801 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 802 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 803 | |
| 804 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 805 | ASSERT_TRUE( |
| 806 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 807 | |
| 808 | RTCOfferAnswerOptions disable_bundle_options; |
| 809 | disable_bundle_options.use_rtp_mux = false; |
| 810 | |
| 811 | auto offer = caller->CreateOffer(disable_bundle_options); |
| 812 | |
| 813 | // Signal ICE restart on the first media section. |
| 814 | auto* offer_transport_desc = GetFirstTransportDescription(offer.get()); |
| 815 | offer_transport_desc->ice_ufrag += "_new"; |
| 816 | offer_transport_desc->ice_pwd += "_new"; |
| 817 | |
| 818 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 819 | |
| 820 | auto answer = callee->CreateAnswer(disable_bundle_options); |
| 821 | const auto& answer_transports = answer->description()->transport_infos(); |
| 822 | const auto& local_transports = |
| 823 | callee->pc()->local_description()->description()->transport_infos(); |
| 824 | |
| 825 | EXPECT_NE(answer_transports[0].description.ice_ufrag, |
| 826 | local_transports[0].description.ice_ufrag); |
| 827 | EXPECT_NE(answer_transports[0].description.ice_pwd, |
| 828 | local_transports[0].description.ice_pwd); |
| 829 | EXPECT_EQ(answer_transports[1].description.ice_ufrag, |
| 830 | local_transports[1].description.ice_ufrag); |
| 831 | EXPECT_EQ(answer_transports[1].description.ice_pwd, |
| 832 | local_transports[1].description.ice_pwd); |
| 833 | } |
| 834 | |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 835 | // Test that when the initial offerer (caller) uses the lite implementation of |
| 836 | // ICE and the callee uses the full implementation, the caller takes the |
| 837 | // CONTROLLED role and the callee takes the CONTROLLING role. This is specified |
| 838 | // in RFC5245 Section 5.1.1. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 839 | TEST_P(PeerConnectionIceTest, |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 840 | OfferFromLiteIceControlledAndAnswerFromFullIceControlling) { |
| 841 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 842 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 843 | |
| 844 | auto offer = caller->CreateOffer(); |
| 845 | SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE); |
| 846 | ASSERT_TRUE( |
| 847 | caller->SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 848 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 849 | |
| 850 | auto answer = callee->CreateAnswer(); |
| 851 | SetIceMode(answer.get(), cricket::IceMode::ICEMODE_FULL); |
| 852 | ASSERT_TRUE( |
| 853 | callee->SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 854 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 855 | |
| 856 | EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(caller)); |
| 857 | EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(callee)); |
| 858 | } |
| 859 | |
| 860 | // Test that when the caller and the callee both use the lite implementation of |
| 861 | // ICE, the initial offerer (caller) takes the CONTROLLING role and the callee |
| 862 | // takes the CONTROLLED role. This is specified in RFC5245 Section 5.1.1. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 863 | TEST_P(PeerConnectionIceTest, |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 864 | OfferFromLiteIceControllingAndAnswerFromLiteIceControlled) { |
| 865 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 866 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 867 | |
| 868 | auto offer = caller->CreateOffer(); |
| 869 | SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE); |
| 870 | ASSERT_TRUE( |
| 871 | caller->SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 872 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 873 | |
| 874 | auto answer = callee->CreateAnswer(); |
| 875 | SetIceMode(answer.get(), cricket::IceMode::ICEMODE_LITE); |
| 876 | ASSERT_TRUE( |
| 877 | callee->SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 878 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 879 | |
| 880 | EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(caller)); |
| 881 | EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(callee)); |
| 882 | } |
| 883 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 884 | INSTANTIATE_TEST_CASE_P(PeerConnectionIceTest, |
| 885 | PeerConnectionIceTest, |
| 886 | Values(SdpSemantics::kPlanB, |
| 887 | SdpSemantics::kUnifiedPlan)); |
| 888 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 889 | } // namespace webrtc |