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) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 203 | auto dtls_transport = static_cast<cricket::DtlsTransportInternal*>( |
| 204 | channel->rtp_packet_transport()); |
| 205 | return dtls_transport->ice_transport()->GetIceRole(); |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
| 209 | RTC_NOTREACHED(); |
| 210 | return cricket::ICEROLE_UNKNOWN; |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 213 | bool AddCandidateToFirstTransport(cricket::Candidate* candidate, |
| 214 | SessionDescriptionInterface* sdesc) { |
| 215 | auto* desc = sdesc->description(); |
| 216 | RTC_DCHECK(desc->contents().size() > 0); |
| 217 | const auto& first_content = desc->contents()[0]; |
| 218 | candidate->set_transport_name(first_content.name); |
| 219 | JsepIceCandidate jsep_candidate(first_content.name, 0, *candidate); |
| 220 | return sdesc->AddCandidate(&jsep_candidate); |
| 221 | } |
| 222 | |
| 223 | rtc::FakeNetworkManager* NewFakeNetwork() { |
| 224 | // The PeerConnection's port allocator is tied to the PeerConnection's |
| 225 | // lifetime and expects the underlying NetworkManager to outlive it. That |
| 226 | // prevents us from having the PeerConnectionWrapper own the fake network. |
| 227 | // Therefore, the test fixture will own all the fake networks even though |
| 228 | // tests should access the fake network through the PeerConnectionWrapper. |
| 229 | auto* fake_network = new rtc::FakeNetworkManager(); |
| 230 | fake_networks_.emplace_back(fake_network); |
| 231 | return fake_network; |
| 232 | } |
| 233 | |
| 234 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 235 | rtc::AutoSocketServerThread main_; |
| 236 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_; |
| 237 | std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_; |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 238 | const SdpSemantics sdp_semantics_; |
| 239 | }; |
| 240 | |
| 241 | class PeerConnectionIceTest |
| 242 | : public PeerConnectionIceBaseTest, |
| 243 | public ::testing::WithParamInterface<SdpSemantics> { |
| 244 | protected: |
| 245 | PeerConnectionIceTest() : PeerConnectionIceBaseTest(GetParam()) {} |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | ::testing::AssertionResult AssertCandidatesEqual(const char* a_expr, |
| 249 | const char* b_expr, |
| 250 | const cricket::Candidate& a, |
| 251 | const cricket::Candidate& b) { |
| 252 | std::stringstream failure_info; |
| 253 | if (a.component() != b.component()) { |
| 254 | failure_info << "\ncomponent: " << a.component() << " != " << b.component(); |
| 255 | } |
| 256 | if (a.protocol() != b.protocol()) { |
| 257 | failure_info << "\nprotocol: " << a.protocol() << " != " << b.protocol(); |
| 258 | } |
| 259 | if (a.address() != b.address()) { |
| 260 | failure_info << "\naddress: " << a.address().ToString() |
| 261 | << " != " << b.address().ToString(); |
| 262 | } |
| 263 | if (a.type() != b.type()) { |
| 264 | failure_info << "\ntype: " << a.type() << " != " << b.type(); |
| 265 | } |
| 266 | std::string failure_info_str = failure_info.str(); |
| 267 | if (failure_info_str.empty()) { |
| 268 | return ::testing::AssertionSuccess(); |
| 269 | } else { |
| 270 | return ::testing::AssertionFailure() |
| 271 | << a_expr << " and " << b_expr << " are not equal" |
| 272 | << failure_info_str; |
| 273 | } |
| 274 | } |
| 275 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 276 | TEST_P(PeerConnectionIceTest, OfferContainsGatheredCandidates) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 277 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 278 | |
| 279 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 280 | caller->network()->AddInterface(kLocalAddress); |
| 281 | |
| 282 | // Start ICE candidate gathering by setting the local offer. |
| 283 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 284 | |
| 285 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 286 | |
| 287 | auto offer = caller->CreateOffer(); |
| 288 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size()); |
| 289 | EXPECT_EQ(caller->observer()->GetCandidatesByMline(0).size(), |
| 290 | offer->candidates(0)->count()); |
| 291 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size()); |
| 292 | EXPECT_EQ(caller->observer()->GetCandidatesByMline(1).size(), |
| 293 | offer->candidates(1)->count()); |
| 294 | } |
| 295 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 296 | TEST_P(PeerConnectionIceTest, AnswerContainsGatheredCandidates) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 297 | const SocketAddress kCallerAddress("1.1.1.1", 0); |
| 298 | |
| 299 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 300 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 301 | caller->network()->AddInterface(kCallerAddress); |
| 302 | |
| 303 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 304 | ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer())); |
| 305 | |
| 306 | EXPECT_TRUE_WAIT(callee->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 307 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 308 | auto* answer = callee->pc()->local_description(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 309 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size()); |
| 310 | EXPECT_EQ(callee->observer()->GetCandidatesByMline(0).size(), |
| 311 | answer->candidates(0)->count()); |
| 312 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size()); |
| 313 | EXPECT_EQ(callee->observer()->GetCandidatesByMline(1).size(), |
| 314 | answer->candidates(1)->count()); |
| 315 | } |
| 316 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 317 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 318 | CanSetRemoteSessionDescriptionWithRemoteCandidates) { |
| 319 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 320 | |
| 321 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 322 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 323 | |
| 324 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 325 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 326 | AddCandidateToFirstTransport(&candidate, offer.get()); |
| 327 | |
| 328 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 329 | auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription(); |
| 330 | ASSERT_EQ(1u, remote_candidates.size()); |
| 331 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate, |
| 332 | remote_candidates[0]->candidate()); |
| 333 | } |
| 334 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 335 | TEST_P(PeerConnectionIceTest, SetLocalDescriptionFailsIfNoIceCredentials) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 336 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 337 | |
| 338 | auto offer = caller->CreateOffer(); |
| 339 | RemoveIceUfragPwd(offer.get()); |
| 340 | |
| 341 | EXPECT_FALSE(caller->SetLocalDescription(std::move(offer))); |
| 342 | } |
| 343 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 344 | TEST_P(PeerConnectionIceTest, SetRemoteDescriptionFailsIfNoIceCredentials) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 345 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 346 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 347 | |
| 348 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 349 | RemoveIceUfragPwd(offer.get()); |
| 350 | |
| 351 | EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer))); |
| 352 | } |
| 353 | |
| 354 | // The following group tests that ICE candidates are not generated before |
| 355 | // SetLocalDescription is called on a PeerConnection. |
| 356 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 357 | TEST_P(PeerConnectionIceTest, NoIceCandidatesBeforeSetLocalDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 358 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 359 | |
| 360 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 361 | caller->network()->AddInterface(kLocalAddress); |
| 362 | |
| 363 | // Pump for 1 second and verify that no candidates are generated. |
| 364 | rtc::Thread::Current()->ProcessMessages(1000); |
| 365 | |
| 366 | EXPECT_EQ(0u, caller->observer()->candidates_.size()); |
| 367 | } |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 368 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 369 | NoIceCandidatesBeforeAnswerSetAsLocalDescription) { |
| 370 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 371 | |
| 372 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 373 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 374 | caller->network()->AddInterface(kCallerAddress); |
| 375 | |
| 376 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 377 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 378 | AddCandidateToFirstTransport(&candidate, offer.get()); |
| 379 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 380 | |
| 381 | // Pump for 1 second and verify that no candidates are generated. |
| 382 | rtc::Thread::Current()->ProcessMessages(1000); |
| 383 | |
| 384 | EXPECT_EQ(0u, callee->observer()->candidates_.size()); |
| 385 | } |
| 386 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 387 | TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenRemoteDescriptionNotSet) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 388 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 389 | |
| 390 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 391 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
| 392 | JsepIceCandidate jsep_candidate(cricket::CN_AUDIO, 0, candidate); |
| 393 | |
| 394 | EXPECT_FALSE(caller->pc()->AddIceCandidate(&jsep_candidate)); |
| 395 | |
| 396 | caller->CreateOfferAndSetAsLocal(); |
| 397 | |
| 398 | EXPECT_FALSE(caller->pc()->AddIceCandidate(&jsep_candidate)); |
| 399 | } |
| 400 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 401 | TEST_P(PeerConnectionIceTest, DuplicateIceCandidateIgnoredWhenAdded) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 402 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 403 | |
| 404 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 405 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 406 | |
| 407 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 408 | ASSERT_TRUE( |
| 409 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 410 | |
| 411 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
| 412 | caller->AddIceCandidate(&candidate); |
| 413 | EXPECT_TRUE(caller->AddIceCandidate(&candidate)); |
| 414 | EXPECT_EQ(1u, caller->GetIceCandidatesFromRemoteDescription().size()); |
| 415 | } |
| 416 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 417 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 418 | AddRemoveCandidateWithEmptyTransportDoesNotCrash) { |
| 419 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 420 | |
| 421 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 422 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 423 | |
| 424 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 425 | ASSERT_TRUE( |
| 426 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 427 | |
| 428 | // |candidate.transport_name()| is empty. |
| 429 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 430 | auto* audio_content = cricket::GetFirstAudioContent( |
| 431 | caller->pc()->local_description()->description()); |
| 432 | JsepIceCandidate ice_candidate(audio_content->name, 0, candidate); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 433 | EXPECT_TRUE(caller->pc()->AddIceCandidate(&ice_candidate)); |
| 434 | EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate})); |
| 435 | } |
| 436 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 437 | TEST_P(PeerConnectionIceTest, RemoveCandidateRemovesFromRemoteDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 438 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 439 | |
| 440 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 441 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 442 | |
| 443 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 444 | ASSERT_TRUE( |
| 445 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 446 | |
| 447 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
| 448 | ASSERT_TRUE(caller->AddIceCandidate(&candidate)); |
| 449 | EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate})); |
| 450 | EXPECT_EQ(0u, caller->GetIceCandidatesFromRemoteDescription().size()); |
| 451 | } |
| 452 | |
| 453 | // Test that if a candidate is added via AddIceCandidate and via an updated |
| 454 | // remote description, then both candidates appear in the stored remote |
| 455 | // description. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 456 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 457 | CandidateInSubsequentOfferIsAddedToRemoteDescription) { |
| 458 | const SocketAddress kCallerAddress1("1.1.1.1", 1111); |
| 459 | const SocketAddress kCallerAddress2("2.2.2.2", 2222); |
| 460 | |
| 461 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 462 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 463 | |
| 464 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 465 | ASSERT_TRUE( |
| 466 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 467 | |
| 468 | // Add one candidate via |AddIceCandidate|. |
| 469 | cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCallerAddress1); |
| 470 | ASSERT_TRUE(callee->AddIceCandidate(&candidate1)); |
| 471 | |
| 472 | // Add the second candidate via a reoffer. |
| 473 | auto offer = caller->CreateOffer(); |
| 474 | cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCallerAddress2); |
| 475 | AddCandidateToFirstTransport(&candidate2, offer.get()); |
| 476 | |
| 477 | // Expect both candidates to appear in the callee's remote description. |
| 478 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 479 | EXPECT_EQ(2u, callee->GetIceCandidatesFromRemoteDescription().size()); |
| 480 | } |
| 481 | |
| 482 | // The follow test verifies that SetLocal/RemoteDescription fails when an offer |
| 483 | // has either ICE ufrag/pwd too short or too long and succeeds otherwise. |
| 484 | // The standard (https://tools.ietf.org/html/rfc5245#section-15.4) says that |
| 485 | // 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] | 486 | TEST_P(PeerConnectionIceTest, VerifyUfragPwdLength) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 487 | auto set_local_description_with_ufrag_pwd_length = |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 488 | [this](int ufrag_len, int pwd_len) { |
| 489 | auto pc = CreatePeerConnectionWithAudioVideo(); |
| 490 | auto offer = pc->CreateOffer(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 491 | SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'), |
| 492 | std::string(pwd_len, 'x')); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 493 | return pc->SetLocalDescription(std::move(offer)); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | auto set_remote_description_with_ufrag_pwd_length = |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 497 | [this](int ufrag_len, int pwd_len) { |
| 498 | auto pc = CreatePeerConnectionWithAudioVideo(); |
| 499 | auto offer = pc->CreateOffer(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 500 | SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'), |
| 501 | std::string(pwd_len, 'x')); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 502 | return pc->SetRemoteDescription(std::move(offer)); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 503 | }; |
| 504 | |
| 505 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(3, 22)); |
| 506 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(3, 22)); |
| 507 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(257, 22)); |
| 508 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(257, 22)); |
| 509 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 21)); |
| 510 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 21)); |
| 511 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 257)); |
| 512 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 257)); |
| 513 | EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(4, 22)); |
| 514 | EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(4, 22)); |
| 515 | EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(256, 256)); |
| 516 | EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(256, 256)); |
| 517 | } |
| 518 | |
| 519 | ::testing::AssertionResult AssertIpInCandidates( |
| 520 | const char* address_expr, |
| 521 | const char* candidates_expr, |
| 522 | const SocketAddress& address, |
| 523 | const std::vector<IceCandidateInterface*> candidates) { |
| 524 | std::stringstream candidate_hosts; |
| 525 | for (const auto* candidate : candidates) { |
| 526 | const auto& candidate_ip = candidate->candidate().address().ipaddr(); |
| 527 | if (candidate_ip == address.ipaddr()) { |
| 528 | return ::testing::AssertionSuccess(); |
| 529 | } |
| 530 | candidate_hosts << "\n" << candidate_ip; |
| 531 | } |
| 532 | return ::testing::AssertionFailure() |
| 533 | << address_expr << " (host " << address.HostAsURIString() |
| 534 | << ") not in " << candidates_expr |
| 535 | << " which have the following address hosts:" << candidate_hosts.str(); |
| 536 | } |
| 537 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 538 | TEST_P(PeerConnectionIceTest, CandidatesGeneratedForEachLocalInterface) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 539 | const SocketAddress kLocalAddress1("1.1.1.1", 0); |
| 540 | const SocketAddress kLocalAddress2("2.2.2.2", 0); |
| 541 | |
| 542 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 543 | caller->network()->AddInterface(kLocalAddress1); |
| 544 | caller->network()->AddInterface(kLocalAddress2); |
| 545 | |
| 546 | caller->CreateOfferAndSetAsLocal(); |
| 547 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 548 | |
| 549 | auto candidates = caller->observer()->GetCandidatesByMline(0); |
| 550 | EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress1, candidates); |
| 551 | EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress2, candidates); |
| 552 | } |
| 553 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 554 | TEST_P(PeerConnectionIceTest, TrickledSingleCandidateAddedToRemoteDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 555 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 556 | |
| 557 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 558 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 559 | |
| 560 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 561 | |
| 562 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 563 | callee->AddIceCandidate(&candidate); |
| 564 | auto candidates = callee->GetIceCandidatesFromRemoteDescription(); |
| 565 | ASSERT_EQ(1u, candidates.size()); |
| 566 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate, |
| 567 | candidates[0]->candidate()); |
| 568 | } |
| 569 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 570 | TEST_P(PeerConnectionIceTest, TwoTrickledCandidatesAddedToRemoteDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 571 | const SocketAddress kCalleeAddress1("1.1.1.1", 1111); |
| 572 | const SocketAddress kCalleeAddress2("2.2.2.2", 2222); |
| 573 | |
| 574 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 575 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 576 | |
| 577 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 578 | ASSERT_TRUE( |
| 579 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 580 | |
| 581 | cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCalleeAddress1); |
| 582 | caller->AddIceCandidate(&candidate1); |
| 583 | |
| 584 | cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCalleeAddress2); |
| 585 | caller->AddIceCandidate(&candidate2); |
| 586 | |
| 587 | auto candidates = caller->GetIceCandidatesFromRemoteDescription(); |
| 588 | ASSERT_EQ(2u, candidates.size()); |
| 589 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate1, |
| 590 | candidates[0]->candidate()); |
| 591 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate2, |
| 592 | candidates[1]->candidate()); |
| 593 | } |
| 594 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 595 | TEST_P(PeerConnectionIceTest, LocalDescriptionUpdatedWhenContinualGathering) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 596 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 597 | |
| 598 | RTCConfiguration config; |
| 599 | config.continual_gathering_policy = |
| 600 | PeerConnectionInterface::GATHER_CONTINUALLY; |
| 601 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 602 | caller->network()->AddInterface(kLocalAddress); |
| 603 | |
| 604 | // Start ICE candidate gathering by setting the local offer. |
| 605 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 606 | |
| 607 | // Since we're using continual gathering, we won't get "gathering done". |
| 608 | EXPECT_TRUE_WAIT( |
| 609 | caller->pc()->local_description()->candidates(0)->count() > 0, |
| 610 | kIceCandidatesTimeout); |
| 611 | } |
| 612 | |
| 613 | // Test that when continual gathering is enabled, and a network interface goes |
| 614 | // down, the candidate is signaled as removed and removed from the local |
| 615 | // description. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 616 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 617 | LocalCandidatesRemovedWhenNetworkDownIfGatheringContinually) { |
| 618 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 619 | |
| 620 | RTCConfiguration config; |
| 621 | config.continual_gathering_policy = |
| 622 | PeerConnectionInterface::GATHER_CONTINUALLY; |
| 623 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 624 | caller->network()->AddInterface(kLocalAddress); |
| 625 | |
| 626 | // Start ICE candidate gathering by setting the local offer. |
| 627 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 628 | |
| 629 | EXPECT_TRUE_WAIT( |
| 630 | caller->pc()->local_description()->candidates(0)->count() > 0, |
| 631 | kIceCandidatesTimeout); |
| 632 | |
| 633 | // Remove the only network interface, causing the PeerConnection to signal |
| 634 | // the removal of all candidates derived from this interface. |
| 635 | caller->network()->RemoveInterface(kLocalAddress); |
| 636 | |
| 637 | EXPECT_EQ_WAIT(0u, caller->pc()->local_description()->candidates(0)->count(), |
| 638 | kIceCandidatesTimeout); |
| 639 | EXPECT_LT(0, caller->observer()->num_candidates_removed_); |
| 640 | } |
| 641 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 642 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 643 | LocalCandidatesNotRemovedWhenNetworkDownIfGatheringOnce) { |
| 644 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 645 | |
| 646 | RTCConfiguration config; |
| 647 | config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE; |
| 648 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 649 | caller->network()->AddInterface(kLocalAddress); |
| 650 | |
| 651 | // Start ICE candidate gathering by setting the local offer. |
| 652 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 653 | |
| 654 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 655 | |
| 656 | caller->network()->RemoveInterface(kLocalAddress); |
| 657 | |
| 658 | // Verify that the local candidates are not removed; |
| 659 | rtc::Thread::Current()->ProcessMessages(1000); |
| 660 | EXPECT_EQ(0, caller->observer()->num_candidates_removed_); |
| 661 | } |
| 662 | |
| 663 | // The following group tests that when an offer includes a new ufrag or pwd |
| 664 | // (indicating an ICE restart) the old candidates are removed and new candidates |
| 665 | // added to the remote description. |
| 666 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 667 | TEST_P(PeerConnectionIceTest, IceRestartOfferClearsExistingCandidate) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 668 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 669 | |
| 670 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 671 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 672 | |
| 673 | auto offer = caller->CreateOffer(); |
| 674 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 675 | AddCandidateToFirstTransport(&candidate, offer.get()); |
| 676 | |
| 677 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 678 | |
| 679 | RTCOfferAnswerOptions options; |
| 680 | options.ice_restart = true; |
| 681 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer(options))); |
| 682 | |
| 683 | EXPECT_EQ(0u, callee->GetIceCandidatesFromRemoteDescription().size()); |
| 684 | } |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 685 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 686 | IceRestartOfferCandidateReplacesExistingCandidate) { |
| 687 | const SocketAddress kFirstCallerAddress("1.1.1.1", 1111); |
| 688 | const SocketAddress kRestartedCallerAddress("2.2.2.2", 2222); |
| 689 | |
| 690 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 691 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 692 | |
| 693 | auto offer = caller->CreateOffer(); |
| 694 | cricket::Candidate old_candidate = |
| 695 | CreateLocalUdpCandidate(kFirstCallerAddress); |
| 696 | AddCandidateToFirstTransport(&old_candidate, offer.get()); |
| 697 | |
| 698 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 699 | |
| 700 | RTCOfferAnswerOptions options; |
| 701 | options.ice_restart = true; |
| 702 | auto restart_offer = caller->CreateOffer(options); |
| 703 | cricket::Candidate new_candidate = |
| 704 | CreateLocalUdpCandidate(kRestartedCallerAddress); |
| 705 | AddCandidateToFirstTransport(&new_candidate, restart_offer.get()); |
| 706 | |
| 707 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(restart_offer))); |
| 708 | |
| 709 | auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription(); |
| 710 | ASSERT_EQ(1u, remote_candidates.size()); |
| 711 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, new_candidate, |
| 712 | remote_candidates[0]->candidate()); |
| 713 | } |
| 714 | |
| 715 | // Test that if there is not an ICE restart (i.e., nothing changes), then the |
| 716 | // 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] | 717 | TEST_P(PeerConnectionIceTest, LaterAnswerHasSameIceCredentialsIfNoIceRestart) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 718 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 719 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 720 | |
| 721 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 722 | ASSERT_TRUE( |
| 723 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 724 | |
| 725 | // Re-offer. |
| 726 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 727 | |
| 728 | auto answer = callee->CreateAnswer(); |
| 729 | auto* answer_transport_desc = GetFirstTransportDescription(answer.get()); |
| 730 | auto* local_transport_desc = |
| 731 | GetFirstTransportDescription(callee->pc()->local_description()); |
| 732 | |
| 733 | EXPECT_EQ(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag); |
| 734 | EXPECT_EQ(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd); |
| 735 | } |
| 736 | |
| 737 | // The following parameterized test verifies that if an offer is sent with a |
| 738 | // modified ICE ufrag and/or ICE pwd, then the answer should identify that the |
| 739 | // other side has initiated an ICE restart and generate a new ufrag and pwd. |
| 740 | // RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or |
| 741 | // a=ice-pwd attributes compared to the previous SDP from the peer, it |
| 742 | // indicates that ICE is restarting for this media stream." |
| 743 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 744 | class PeerConnectionIceUfragPwdAnswerTest |
| 745 | : public PeerConnectionIceBaseTest, |
| 746 | public ::testing::WithParamInterface< |
| 747 | std::tuple<SdpSemantics, std::tuple<bool, bool>>> { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 748 | protected: |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 749 | PeerConnectionIceUfragPwdAnswerTest() |
| 750 | : PeerConnectionIceBaseTest(std::get<0>(GetParam())) { |
| 751 | auto param = std::get<1>(GetParam()); |
| 752 | offer_new_ufrag_ = std::get<0>(param); |
| 753 | offer_new_pwd_ = std::get<1>(param); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | bool offer_new_ufrag_; |
| 757 | bool offer_new_pwd_; |
| 758 | }; |
| 759 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 760 | TEST_P(PeerConnectionIceUfragPwdAnswerTest, TestIncludedInAnswer) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 761 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 762 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 763 | |
| 764 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 765 | ASSERT_TRUE( |
| 766 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 767 | |
| 768 | auto offer = caller->CreateOffer(); |
| 769 | auto* offer_transport_desc = GetFirstTransportDescription(offer.get()); |
| 770 | if (offer_new_ufrag_) { |
| 771 | offer_transport_desc->ice_ufrag += "_new"; |
| 772 | } |
| 773 | if (offer_new_pwd_) { |
| 774 | offer_transport_desc->ice_pwd += "_new"; |
| 775 | } |
| 776 | |
| 777 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 778 | |
| 779 | auto answer = callee->CreateAnswer(); |
| 780 | auto* answer_transport_desc = GetFirstTransportDescription(answer.get()); |
| 781 | auto* local_transport_desc = |
| 782 | GetFirstTransportDescription(callee->pc()->local_description()); |
| 783 | |
| 784 | EXPECT_NE(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag); |
| 785 | EXPECT_NE(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd); |
| 786 | } |
| 787 | |
| 788 | INSTANTIATE_TEST_CASE_P( |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 789 | PeerConnectionIceTest, |
| 790 | PeerConnectionIceUfragPwdAnswerTest, |
| 791 | Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan), |
| 792 | Values(std::make_pair(true, true), // Both changed. |
| 793 | std::make_pair(true, false), // Only ufrag changed. |
| 794 | std::make_pair(false, true)))); // Only pwd changed. |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 795 | |
| 796 | // Test that if an ICE restart is offered on one media section, then the answer |
| 797 | // will only change ICE ufrag/pwd for that section and keep the other sections |
| 798 | // the same. |
| 799 | // Note that this only works if we have disabled BUNDLE, otherwise all media |
| 800 | // sections will share the same transport. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 801 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 802 | CreateAnswerHasNewUfragPwdForOnlyMediaSectionWhichRestarted) { |
| 803 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 804 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 805 | |
| 806 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 807 | ASSERT_TRUE( |
| 808 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 809 | |
| 810 | RTCOfferAnswerOptions disable_bundle_options; |
| 811 | disable_bundle_options.use_rtp_mux = false; |
| 812 | |
| 813 | auto offer = caller->CreateOffer(disable_bundle_options); |
| 814 | |
| 815 | // Signal ICE restart on the first media section. |
| 816 | auto* offer_transport_desc = GetFirstTransportDescription(offer.get()); |
| 817 | offer_transport_desc->ice_ufrag += "_new"; |
| 818 | offer_transport_desc->ice_pwd += "_new"; |
| 819 | |
| 820 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 821 | |
| 822 | auto answer = callee->CreateAnswer(disable_bundle_options); |
| 823 | const auto& answer_transports = answer->description()->transport_infos(); |
| 824 | const auto& local_transports = |
| 825 | callee->pc()->local_description()->description()->transport_infos(); |
| 826 | |
| 827 | EXPECT_NE(answer_transports[0].description.ice_ufrag, |
| 828 | local_transports[0].description.ice_ufrag); |
| 829 | EXPECT_NE(answer_transports[0].description.ice_pwd, |
| 830 | local_transports[0].description.ice_pwd); |
| 831 | EXPECT_EQ(answer_transports[1].description.ice_ufrag, |
| 832 | local_transports[1].description.ice_ufrag); |
| 833 | EXPECT_EQ(answer_transports[1].description.ice_pwd, |
| 834 | local_transports[1].description.ice_pwd); |
| 835 | } |
| 836 | |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 837 | // Test that when the initial offerer (caller) uses the lite implementation of |
| 838 | // ICE and the callee uses the full implementation, the caller takes the |
| 839 | // CONTROLLED role and the callee takes the CONTROLLING role. This is specified |
| 840 | // in RFC5245 Section 5.1.1. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 841 | TEST_P(PeerConnectionIceTest, |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 842 | OfferFromLiteIceControlledAndAnswerFromFullIceControlling) { |
| 843 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 844 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 845 | |
| 846 | auto offer = caller->CreateOffer(); |
| 847 | SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE); |
| 848 | ASSERT_TRUE( |
| 849 | caller->SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 850 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 851 | |
| 852 | auto answer = callee->CreateAnswer(); |
| 853 | SetIceMode(answer.get(), cricket::IceMode::ICEMODE_FULL); |
| 854 | ASSERT_TRUE( |
| 855 | callee->SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 856 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 857 | |
| 858 | EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(caller)); |
| 859 | EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(callee)); |
| 860 | } |
| 861 | |
| 862 | // Test that when the caller and the callee both use the lite implementation of |
| 863 | // ICE, the initial offerer (caller) takes the CONTROLLING role and the callee |
| 864 | // 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] | 865 | TEST_P(PeerConnectionIceTest, |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 866 | OfferFromLiteIceControllingAndAnswerFromLiteIceControlled) { |
| 867 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 868 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 869 | |
| 870 | auto offer = caller->CreateOffer(); |
| 871 | SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE); |
| 872 | ASSERT_TRUE( |
| 873 | caller->SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 874 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 875 | |
| 876 | auto answer = callee->CreateAnswer(); |
| 877 | SetIceMode(answer.get(), cricket::IceMode::ICEMODE_LITE); |
| 878 | ASSERT_TRUE( |
| 879 | callee->SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 880 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 881 | |
| 882 | EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(caller)); |
| 883 | EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(callee)); |
| 884 | } |
| 885 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 886 | INSTANTIATE_TEST_CASE_P(PeerConnectionIceTest, |
| 887 | PeerConnectionIceTest, |
| 888 | Values(SdpSemantics::kPlanB, |
| 889 | SdpSemantics::kUnifiedPlan)); |
| 890 | |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 891 | class PeerConnectionIceConfigTest : public testing::Test { |
| 892 | protected: |
| 893 | void SetUp() override { |
| 894 | pc_factory_ = CreatePeerConnectionFactory( |
| 895 | rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(), |
| 896 | FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(), |
| 897 | CreateBuiltinAudioDecoderFactory(), nullptr, nullptr); |
| 898 | } |
| 899 | void CreatePeerConnection(const RTCConfiguration& config) { |
| 900 | std::unique_ptr<cricket::FakePortAllocator> port_allocator( |
| 901 | new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)); |
| 902 | port_allocator_ = port_allocator.get(); |
| 903 | rtc::scoped_refptr<PeerConnectionInterface> pc( |
| 904 | pc_factory_->CreatePeerConnection( |
| 905 | config, nullptr /* constraint */, std::move(port_allocator), |
| 906 | nullptr /* cert_generator */, &observer_)); |
| 907 | EXPECT_TRUE(pc.get()); |
| 908 | pc_ = std::move(pc.get()); |
| 909 | } |
| 910 | |
| 911 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_ = nullptr; |
| 912 | rtc::scoped_refptr<PeerConnectionInterface> pc_ = nullptr; |
| 913 | cricket::FakePortAllocator* port_allocator_ = nullptr; |
| 914 | |
| 915 | MockPeerConnectionObserver observer_; |
| 916 | }; |
| 917 | |
| 918 | TEST_F(PeerConnectionIceConfigTest, SetStunCandidateKeepaliveInterval) { |
| 919 | RTCConfiguration config; |
| 920 | config.stun_candidate_keepalive_interval = 123; |
| 921 | config.ice_candidate_pool_size = 1; |
| 922 | CreatePeerConnection(config); |
| 923 | ASSERT_NE(port_allocator_, nullptr); |
| 924 | rtc::Optional<int> actual_stun_keepalive_interval = |
| 925 | port_allocator_->stun_candidate_keepalive_interval(); |
| 926 | EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 123); |
| 927 | config.stun_candidate_keepalive_interval = 321; |
| 928 | RTCError error; |
| 929 | pc_->SetConfiguration(config, &error); |
| 930 | actual_stun_keepalive_interval = |
| 931 | port_allocator_->stun_candidate_keepalive_interval(); |
| 932 | EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 321); |
| 933 | } |
| 934 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 935 | } // namespace webrtc |