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 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 13 | #include "p2p/base/fake_port_allocator.h" |
| 14 | #include "p2p/base/test_stun_server.h" |
| 15 | #include "p2p/client/basic_port_allocator.h" |
| 16 | #include "pc/media_session.h" |
| 17 | #include "pc/peer_connection.h" |
| 18 | #include "pc/peer_connection_wrapper.h" |
| 19 | #include "pc/sdp_utils.h" |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 20 | #ifdef WEBRTC_ANDROID |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "pc/test/android_test_initializer.h" |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 22 | #endif |
Karl Wiberg | 1b0eae3 | 2017-10-17 14:48:54 +0200 | [diff] [blame] | 23 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 24 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
Mirko Bonadei | 2ff3f49 | 2018-11-22 09:00:13 +0100 | [diff] [blame] | 25 | #include "api/create_peerconnection_factory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "api/peer_connection_proxy.h" |
| 27 | #include "api/uma_metrics.h" |
Anders Carlsson | 6753795 | 2018-05-03 11:28:29 +0200 | [diff] [blame] | 28 | #include "api/video_codecs/builtin_video_decoder_factory.h" |
| 29 | #include "api/video_codecs/builtin_video_encoder_factory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 30 | #include "pc/test/fake_audio_capture_module.h" |
Henrik Boström | ee6f4f6 | 2019-11-06 12:36:12 +0100 | [diff] [blame] | 31 | #include "pc/test/mock_peer_connection_observers.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 32 | #include "rtc_base/fake_network.h" |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 33 | #include "rtc_base/gunit.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 34 | #include "rtc_base/strings/string_builder.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 35 | #include "rtc_base/virtual_socket_server.h" |
Mirko Bonadei | 17f4878 | 2018-09-28 08:51:10 +0200 | [diff] [blame] | 36 | #include "system_wrappers/include/metrics.h" |
Steve Anton | b443dfe | 2019-03-05 14:09:49 -0800 | [diff] [blame] | 37 | #include "test/gmock.h" |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 38 | |
| 39 | namespace webrtc { |
| 40 | |
| 41 | using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; |
| 42 | using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions; |
| 43 | using rtc::SocketAddress; |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 44 | using ::testing::Combine; |
Steve Anton | b443dfe | 2019-03-05 14:09:49 -0800 | [diff] [blame] | 45 | using ::testing::ElementsAre; |
| 46 | using ::testing::Pair; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 47 | using ::testing::Values; |
| 48 | |
| 49 | constexpr int kIceCandidatesTimeout = 10000; |
Henrik Boström | ee6f4f6 | 2019-11-06 12:36:12 +0100 | [diff] [blame] | 50 | constexpr int64_t kWaitTimeout = 10000; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 51 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 52 | class PeerConnectionWrapperForIceTest : public PeerConnectionWrapper { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 53 | public: |
| 54 | using PeerConnectionWrapper::PeerConnectionWrapper; |
| 55 | |
Henrik Boström | ee6f4f6 | 2019-11-06 12:36:12 +0100 | [diff] [blame] | 56 | std::unique_ptr<IceCandidateInterface> CreateJsepCandidateForFirstTransport( |
| 57 | cricket::Candidate* candidate) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 58 | RTC_DCHECK(pc()->remote_description()); |
| 59 | const auto* desc = pc()->remote_description()->description(); |
| 60 | RTC_DCHECK(desc->contents().size() > 0); |
| 61 | const auto& first_content = desc->contents()[0]; |
| 62 | candidate->set_transport_name(first_content.name); |
Henrik Boström | ee6f4f6 | 2019-11-06 12:36:12 +0100 | [diff] [blame] | 63 | return CreateIceCandidate(first_content.name, -1, *candidate); |
| 64 | } |
| 65 | |
| 66 | // Adds a new ICE candidate to the first transport. |
| 67 | bool AddIceCandidate(cricket::Candidate* candidate) { |
| 68 | return pc()->AddIceCandidate( |
| 69 | CreateJsepCandidateForFirstTransport(candidate).get()); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // Returns ICE candidates from the remote session description. |
| 73 | std::vector<const IceCandidateInterface*> |
| 74 | GetIceCandidatesFromRemoteDescription() { |
| 75 | const SessionDescriptionInterface* sdesc = pc()->remote_description(); |
| 76 | RTC_DCHECK(sdesc); |
| 77 | std::vector<const IceCandidateInterface*> candidates; |
| 78 | for (size_t mline_index = 0; mline_index < sdesc->number_of_mediasections(); |
| 79 | mline_index++) { |
| 80 | const auto* candidate_collection = sdesc->candidates(mline_index); |
| 81 | for (size_t i = 0; i < candidate_collection->count(); i++) { |
| 82 | candidates.push_back(candidate_collection->at(i)); |
| 83 | } |
| 84 | } |
| 85 | return candidates; |
| 86 | } |
| 87 | |
| 88 | rtc::FakeNetworkManager* network() { return network_; } |
| 89 | |
| 90 | void set_network(rtc::FakeNetworkManager* network) { network_ = network; } |
| 91 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 92 | // The port allocator used by this PC. |
| 93 | cricket::PortAllocator* port_allocator_; |
| 94 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 95 | private: |
| 96 | rtc::FakeNetworkManager* network_; |
| 97 | }; |
| 98 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 99 | class PeerConnectionIceBaseTest : public ::testing::Test { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 100 | protected: |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 101 | typedef std::unique_ptr<PeerConnectionWrapperForIceTest> WrapperPtr; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 102 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 103 | explicit PeerConnectionIceBaseTest(SdpSemantics sdp_semantics) |
| 104 | : vss_(new rtc::VirtualSocketServer()), |
| 105 | main_(vss_.get()), |
| 106 | sdp_semantics_(sdp_semantics) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 107 | #ifdef WEBRTC_ANDROID |
| 108 | InitializeAndroidObjects(); |
| 109 | #endif |
| 110 | pc_factory_ = CreatePeerConnectionFactory( |
| 111 | rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(), |
Anders Carlsson | 6753795 | 2018-05-03 11:28:29 +0200 | [diff] [blame] | 112 | rtc::scoped_refptr<AudioDeviceModule>(FakeAudioCaptureModule::Create()), |
| 113 | CreateBuiltinAudioEncoderFactory(), CreateBuiltinAudioDecoderFactory(), |
| 114 | CreateBuiltinVideoEncoderFactory(), CreateBuiltinVideoDecoderFactory(), |
| 115 | nullptr /* audio_mixer */, nullptr /* audio_processing */); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | WrapperPtr CreatePeerConnection() { |
| 119 | return CreatePeerConnection(RTCConfiguration()); |
| 120 | } |
| 121 | |
| 122 | WrapperPtr CreatePeerConnection(const RTCConfiguration& config) { |
| 123 | auto* fake_network = NewFakeNetwork(); |
| 124 | auto port_allocator = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 125 | std::make_unique<cricket::BasicPortAllocator>(fake_network); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 126 | port_allocator->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP | |
| 127 | cricket::PORTALLOCATOR_DISABLE_RELAY); |
| 128 | port_allocator->set_step_delay(cricket::kMinimumStepDelay); |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 129 | RTCConfiguration modified_config = config; |
| 130 | modified_config.sdp_semantics = sdp_semantics_; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 131 | auto observer = std::make_unique<MockPeerConnectionObserver>(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 132 | auto port_allocator_copy = port_allocator.get(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 133 | auto pc = pc_factory_->CreatePeerConnection( |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 134 | modified_config, std::move(port_allocator), nullptr, observer.get()); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 135 | if (!pc) { |
| 136 | return nullptr; |
| 137 | } |
| 138 | |
Yves Gerey | 4e93329 | 2018-10-31 15:36:05 +0100 | [diff] [blame] | 139 | observer->SetPeerConnectionInterface(pc.get()); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 140 | auto wrapper = std::make_unique<PeerConnectionWrapperForIceTest>( |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 141 | pc_factory_, pc, std::move(observer)); |
| 142 | wrapper->set_network(fake_network); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 143 | wrapper->port_allocator_ = port_allocator_copy; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 144 | return wrapper; |
| 145 | } |
| 146 | |
| 147 | // Accepts the same arguments as CreatePeerConnection and adds default audio |
| 148 | // and video tracks. |
| 149 | template <typename... Args> |
| 150 | WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) { |
| 151 | auto wrapper = CreatePeerConnection(std::forward<Args>(args)...); |
| 152 | if (!wrapper) { |
| 153 | return nullptr; |
| 154 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 155 | wrapper->AddAudioTrack("a"); |
| 156 | wrapper->AddVideoTrack("v"); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 157 | return wrapper; |
| 158 | } |
| 159 | |
| 160 | cricket::Candidate CreateLocalUdpCandidate( |
| 161 | const rtc::SocketAddress& address) { |
| 162 | cricket::Candidate candidate; |
| 163 | candidate.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 164 | candidate.set_protocol(cricket::UDP_PROTOCOL_NAME); |
| 165 | candidate.set_address(address); |
| 166 | candidate.set_type(cricket::LOCAL_PORT_TYPE); |
| 167 | return candidate; |
| 168 | } |
| 169 | |
| 170 | // Remove all ICE ufrag/pwd lines from the given session description. |
| 171 | void RemoveIceUfragPwd(SessionDescriptionInterface* sdesc) { |
| 172 | SetIceUfragPwd(sdesc, "", ""); |
| 173 | } |
| 174 | |
| 175 | // Sets all ICE ufrag/pwds on the given session description. |
| 176 | void SetIceUfragPwd(SessionDescriptionInterface* sdesc, |
| 177 | const std::string& ufrag, |
| 178 | const std::string& pwd) { |
| 179 | auto* desc = sdesc->description(); |
| 180 | for (const auto& content : desc->contents()) { |
| 181 | auto* transport_info = desc->GetTransportInfoByName(content.name); |
| 182 | transport_info->description.ice_ufrag = ufrag; |
| 183 | transport_info->description.ice_pwd = pwd; |
| 184 | } |
| 185 | } |
| 186 | |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 187 | // Set ICE mode on the given session description. |
| 188 | void SetIceMode(SessionDescriptionInterface* sdesc, |
| 189 | const cricket::IceMode ice_mode) { |
| 190 | auto* desc = sdesc->description(); |
| 191 | for (const auto& content : desc->contents()) { |
| 192 | auto* transport_info = desc->GetTransportInfoByName(content.name); |
| 193 | transport_info->description.ice_mode = ice_mode; |
| 194 | } |
| 195 | } |
| 196 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 197 | cricket::TransportDescription* GetFirstTransportDescription( |
| 198 | SessionDescriptionInterface* sdesc) { |
| 199 | auto* desc = sdesc->description(); |
| 200 | RTC_DCHECK(desc->contents().size() > 0); |
| 201 | auto* transport_info = |
| 202 | desc->GetTransportInfoByName(desc->contents()[0].name); |
| 203 | RTC_DCHECK(transport_info); |
| 204 | return &transport_info->description; |
| 205 | } |
| 206 | |
| 207 | const cricket::TransportDescription* GetFirstTransportDescription( |
| 208 | const SessionDescriptionInterface* sdesc) { |
| 209 | auto* desc = sdesc->description(); |
| 210 | RTC_DCHECK(desc->contents().size() > 0); |
| 211 | auto* transport_info = |
| 212 | desc->GetTransportInfoByName(desc->contents()[0].name); |
| 213 | RTC_DCHECK(transport_info); |
| 214 | return &transport_info->description; |
| 215 | } |
| 216 | |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 217 | // TODO(qingsi): Rewrite this method in terms of the standard IceTransport |
| 218 | // after it is implemented. |
| 219 | cricket::IceRole GetIceRole(const WrapperPtr& pc_wrapper_ptr) { |
Mirko Bonadei | e97de91 | 2017-12-13 11:29:34 +0100 | [diff] [blame] | 220 | auto* pc_proxy = |
| 221 | static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>( |
| 222 | pc_wrapper_ptr->pc()); |
| 223 | PeerConnection* pc = static_cast<PeerConnection*>(pc_proxy->internal()); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 224 | for (const auto& transceiver : pc->GetTransceiversInternal()) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 225 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Bjorn A Mellem | 3a1b927 | 2019-05-24 16:13:08 -0700 | [diff] [blame] | 226 | auto dtls_transport = pc->LookupDtlsTransportByMidInternal( |
| 227 | transceiver->internal()->channel()->content_name()); |
| 228 | return dtls_transport->ice_transport()->internal()->GetIceRole(); |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | RTC_NOTREACHED(); |
| 232 | return cricket::ICEROLE_UNKNOWN; |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 235 | // Returns a list of (ufrag, pwd) pairs in the order that they appear in |
| 236 | // |description|, or the empty list if |description| is null. |
| 237 | std::vector<std::pair<std::string, std::string>> GetIceCredentials( |
| 238 | const SessionDescriptionInterface* description) { |
| 239 | std::vector<std::pair<std::string, std::string>> ice_credentials; |
| 240 | if (!description) |
| 241 | return ice_credentials; |
| 242 | const auto* desc = description->description(); |
| 243 | for (const auto& content_info : desc->contents()) { |
| 244 | const auto* transport_info = |
| 245 | desc->GetTransportInfoByName(content_info.name); |
| 246 | if (transport_info) { |
| 247 | ice_credentials.push_back( |
| 248 | std::make_pair(transport_info->description.ice_ufrag, |
| 249 | transport_info->description.ice_pwd)); |
| 250 | } |
| 251 | } |
| 252 | return ice_credentials; |
| 253 | } |
| 254 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 255 | bool AddCandidateToFirstTransport(cricket::Candidate* candidate, |
| 256 | SessionDescriptionInterface* sdesc) { |
| 257 | auto* desc = sdesc->description(); |
| 258 | RTC_DCHECK(desc->contents().size() > 0); |
| 259 | const auto& first_content = desc->contents()[0]; |
| 260 | candidate->set_transport_name(first_content.name); |
Steve Anton | 27ab0e5 | 2018-07-23 15:11:53 -0700 | [diff] [blame] | 261 | std::unique_ptr<IceCandidateInterface> jsep_candidate = |
| 262 | CreateIceCandidate(first_content.name, 0, *candidate); |
| 263 | return sdesc->AddCandidate(jsep_candidate.get()); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | rtc::FakeNetworkManager* NewFakeNetwork() { |
| 267 | // The PeerConnection's port allocator is tied to the PeerConnection's |
| 268 | // lifetime and expects the underlying NetworkManager to outlive it. That |
| 269 | // prevents us from having the PeerConnectionWrapper own the fake network. |
| 270 | // Therefore, the test fixture will own all the fake networks even though |
| 271 | // tests should access the fake network through the PeerConnectionWrapper. |
| 272 | auto* fake_network = new rtc::FakeNetworkManager(); |
| 273 | fake_networks_.emplace_back(fake_network); |
| 274 | return fake_network; |
| 275 | } |
| 276 | |
| 277 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 278 | rtc::AutoSocketServerThread main_; |
| 279 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_; |
| 280 | std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_; |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 281 | const SdpSemantics sdp_semantics_; |
| 282 | }; |
| 283 | |
| 284 | class PeerConnectionIceTest |
| 285 | : public PeerConnectionIceBaseTest, |
| 286 | public ::testing::WithParamInterface<SdpSemantics> { |
| 287 | protected: |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 288 | PeerConnectionIceTest() : PeerConnectionIceBaseTest(GetParam()) { |
| 289 | webrtc::metrics::Reset(); |
| 290 | } |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | ::testing::AssertionResult AssertCandidatesEqual(const char* a_expr, |
| 294 | const char* b_expr, |
| 295 | const cricket::Candidate& a, |
| 296 | const cricket::Candidate& b) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 297 | rtc::StringBuilder failure_info; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 298 | if (a.component() != b.component()) { |
| 299 | failure_info << "\ncomponent: " << a.component() << " != " << b.component(); |
| 300 | } |
| 301 | if (a.protocol() != b.protocol()) { |
| 302 | failure_info << "\nprotocol: " << a.protocol() << " != " << b.protocol(); |
| 303 | } |
| 304 | if (a.address() != b.address()) { |
| 305 | failure_info << "\naddress: " << a.address().ToString() |
| 306 | << " != " << b.address().ToString(); |
| 307 | } |
| 308 | if (a.type() != b.type()) { |
| 309 | failure_info << "\ntype: " << a.type() << " != " << b.type(); |
| 310 | } |
| 311 | std::string failure_info_str = failure_info.str(); |
| 312 | if (failure_info_str.empty()) { |
| 313 | return ::testing::AssertionSuccess(); |
| 314 | } else { |
| 315 | return ::testing::AssertionFailure() |
| 316 | << a_expr << " and " << b_expr << " are not equal" |
| 317 | << failure_info_str; |
| 318 | } |
| 319 | } |
| 320 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 321 | TEST_P(PeerConnectionIceTest, OfferContainsGatheredCandidates) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 322 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 323 | |
| 324 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 325 | caller->network()->AddInterface(kLocalAddress); |
| 326 | |
| 327 | // Start ICE candidate gathering by setting the local offer. |
| 328 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 329 | |
| 330 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 331 | |
| 332 | auto offer = caller->CreateOffer(); |
| 333 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size()); |
| 334 | EXPECT_EQ(caller->observer()->GetCandidatesByMline(0).size(), |
| 335 | offer->candidates(0)->count()); |
| 336 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size()); |
| 337 | EXPECT_EQ(caller->observer()->GetCandidatesByMline(1).size(), |
| 338 | offer->candidates(1)->count()); |
| 339 | } |
| 340 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 341 | TEST_P(PeerConnectionIceTest, AnswerContainsGatheredCandidates) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 342 | const SocketAddress kCallerAddress("1.1.1.1", 0); |
| 343 | |
| 344 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 345 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 346 | caller->network()->AddInterface(kCallerAddress); |
| 347 | |
| 348 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 349 | ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer())); |
| 350 | |
| 351 | EXPECT_TRUE_WAIT(callee->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 352 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 353 | auto* answer = callee->pc()->local_description(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 354 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size()); |
| 355 | EXPECT_EQ(callee->observer()->GetCandidatesByMline(0).size(), |
| 356 | answer->candidates(0)->count()); |
| 357 | EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size()); |
| 358 | EXPECT_EQ(callee->observer()->GetCandidatesByMline(1).size(), |
| 359 | answer->candidates(1)->count()); |
| 360 | } |
| 361 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 362 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 363 | CanSetRemoteSessionDescriptionWithRemoteCandidates) { |
| 364 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 365 | |
| 366 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 367 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 368 | |
| 369 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 370 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 371 | AddCandidateToFirstTransport(&candidate, offer.get()); |
| 372 | |
| 373 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 374 | auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription(); |
| 375 | ASSERT_EQ(1u, remote_candidates.size()); |
| 376 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate, |
| 377 | remote_candidates[0]->candidate()); |
| 378 | } |
| 379 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 380 | TEST_P(PeerConnectionIceTest, SetLocalDescriptionFailsIfNoIceCredentials) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 381 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 382 | |
| 383 | auto offer = caller->CreateOffer(); |
| 384 | RemoveIceUfragPwd(offer.get()); |
| 385 | |
| 386 | EXPECT_FALSE(caller->SetLocalDescription(std::move(offer))); |
| 387 | } |
| 388 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 389 | TEST_P(PeerConnectionIceTest, SetRemoteDescriptionFailsIfNoIceCredentials) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 390 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 391 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 392 | |
| 393 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 394 | RemoveIceUfragPwd(offer.get()); |
| 395 | |
| 396 | EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer))); |
| 397 | } |
| 398 | |
Steve Anton | f764cf4 | 2018-05-01 14:32:17 -0700 | [diff] [blame] | 399 | // Test that doing an offer/answer exchange with no transport (i.e., no data |
| 400 | // channel or media) results in the ICE connection state staying at New. |
| 401 | TEST_P(PeerConnectionIceTest, |
| 402 | OfferAnswerWithNoTransportsDoesNotChangeIceConnectionState) { |
| 403 | auto caller = CreatePeerConnection(); |
| 404 | auto callee = CreatePeerConnection(); |
| 405 | |
| 406 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 407 | |
| 408 | EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew, |
| 409 | caller->pc()->ice_connection_state()); |
| 410 | EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew, |
| 411 | callee->pc()->ice_connection_state()); |
| 412 | } |
| 413 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 414 | // The following group tests that ICE candidates are not generated before |
| 415 | // SetLocalDescription is called on a PeerConnection. |
| 416 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 417 | TEST_P(PeerConnectionIceTest, NoIceCandidatesBeforeSetLocalDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 418 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 419 | |
| 420 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 421 | caller->network()->AddInterface(kLocalAddress); |
| 422 | |
| 423 | // Pump for 1 second and verify that no candidates are generated. |
| 424 | rtc::Thread::Current()->ProcessMessages(1000); |
| 425 | |
| 426 | EXPECT_EQ(0u, caller->observer()->candidates_.size()); |
| 427 | } |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 428 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 429 | NoIceCandidatesBeforeAnswerSetAsLocalDescription) { |
| 430 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 431 | |
| 432 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 433 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 434 | caller->network()->AddInterface(kCallerAddress); |
| 435 | |
| 436 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 437 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 438 | AddCandidateToFirstTransport(&candidate, offer.get()); |
| 439 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 440 | |
| 441 | // Pump for 1 second and verify that no candidates are generated. |
| 442 | rtc::Thread::Current()->ProcessMessages(1000); |
| 443 | |
| 444 | EXPECT_EQ(0u, callee->observer()->candidates_.size()); |
| 445 | } |
| 446 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 447 | TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenRemoteDescriptionNotSet) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 448 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 449 | |
| 450 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 451 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
Steve Anton | 27ab0e5 | 2018-07-23 15:11:53 -0700 | [diff] [blame] | 452 | std::unique_ptr<IceCandidateInterface> jsep_candidate = |
| 453 | CreateIceCandidate(cricket::CN_AUDIO, 0, candidate); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 454 | |
Steve Anton | 27ab0e5 | 2018-07-23 15:11:53 -0700 | [diff] [blame] | 455 | EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get())); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 456 | |
| 457 | caller->CreateOfferAndSetAsLocal(); |
| 458 | |
Steve Anton | 27ab0e5 | 2018-07-23 15:11:53 -0700 | [diff] [blame] | 459 | EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get())); |
Ying Wang | ef3998f | 2019-12-09 13:06:53 +0100 | [diff] [blame] | 460 | EXPECT_METRIC_THAT( |
| 461 | webrtc::metrics::Samples("WebRTC.PeerConnection.AddIceCandidate"), |
| 462 | ElementsAre(Pair(kAddIceCandidateFailNoRemoteDescription, 2))); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 465 | TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenPeerConnectionClosed) { |
| 466 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 467 | |
| 468 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 469 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 470 | |
| 471 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 472 | |
| 473 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
| 474 | auto* audio_content = cricket::GetFirstAudioContent( |
| 475 | caller->pc()->local_description()->description()); |
Steve Anton | 27ab0e5 | 2018-07-23 15:11:53 -0700 | [diff] [blame] | 476 | std::unique_ptr<IceCandidateInterface> jsep_candidate = |
| 477 | CreateIceCandidate(audio_content->name, 0, candidate); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 478 | |
| 479 | caller->pc()->Close(); |
| 480 | |
Steve Anton | 27ab0e5 | 2018-07-23 15:11:53 -0700 | [diff] [blame] | 481 | EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get())); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 484 | TEST_P(PeerConnectionIceTest, DuplicateIceCandidateIgnoredWhenAdded) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 485 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 486 | |
| 487 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 488 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 489 | |
| 490 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 491 | ASSERT_TRUE( |
| 492 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 493 | |
| 494 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
| 495 | caller->AddIceCandidate(&candidate); |
| 496 | EXPECT_TRUE(caller->AddIceCandidate(&candidate)); |
| 497 | EXPECT_EQ(1u, caller->GetIceCandidatesFromRemoteDescription().size()); |
| 498 | } |
| 499 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 500 | TEST_P(PeerConnectionIceTest, |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 501 | CannotRemoveIceCandidatesWhenPeerConnectionClosed) { |
| 502 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 503 | |
| 504 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 505 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 506 | |
| 507 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 508 | |
| 509 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
| 510 | auto* audio_content = cricket::GetFirstAudioContent( |
| 511 | caller->pc()->local_description()->description()); |
Steve Anton | 27ab0e5 | 2018-07-23 15:11:53 -0700 | [diff] [blame] | 512 | std::unique_ptr<IceCandidateInterface> ice_candidate = |
| 513 | CreateIceCandidate(audio_content->name, 0, candidate); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 514 | |
Steve Anton | 27ab0e5 | 2018-07-23 15:11:53 -0700 | [diff] [blame] | 515 | ASSERT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get())); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 516 | |
| 517 | caller->pc()->Close(); |
| 518 | |
| 519 | EXPECT_FALSE(caller->pc()->RemoveIceCandidates({candidate})); |
| 520 | } |
| 521 | |
| 522 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 523 | AddRemoveCandidateWithEmptyTransportDoesNotCrash) { |
| 524 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 525 | |
| 526 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 527 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 528 | |
| 529 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 530 | ASSERT_TRUE( |
| 531 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 532 | |
| 533 | // |candidate.transport_name()| is empty. |
| 534 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 535 | auto* audio_content = cricket::GetFirstAudioContent( |
| 536 | caller->pc()->local_description()->description()); |
Steve Anton | 27ab0e5 | 2018-07-23 15:11:53 -0700 | [diff] [blame] | 537 | std::unique_ptr<IceCandidateInterface> ice_candidate = |
| 538 | CreateIceCandidate(audio_content->name, 0, candidate); |
| 539 | EXPECT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get())); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 540 | EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate})); |
| 541 | } |
| 542 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 543 | TEST_P(PeerConnectionIceTest, RemoveCandidateRemovesFromRemoteDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 544 | const SocketAddress kCalleeAddress("1.1.1.1", 1111); |
| 545 | |
| 546 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 547 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 548 | |
| 549 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 550 | ASSERT_TRUE( |
| 551 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 552 | |
| 553 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress); |
| 554 | ASSERT_TRUE(caller->AddIceCandidate(&candidate)); |
| 555 | EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate})); |
| 556 | EXPECT_EQ(0u, caller->GetIceCandidatesFromRemoteDescription().size()); |
| 557 | } |
| 558 | |
| 559 | // Test that if a candidate is added via AddIceCandidate and via an updated |
| 560 | // remote description, then both candidates appear in the stored remote |
| 561 | // description. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 562 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 563 | CandidateInSubsequentOfferIsAddedToRemoteDescription) { |
| 564 | const SocketAddress kCallerAddress1("1.1.1.1", 1111); |
| 565 | const SocketAddress kCallerAddress2("2.2.2.2", 2222); |
| 566 | |
| 567 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 568 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 569 | |
| 570 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 571 | ASSERT_TRUE( |
| 572 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 573 | |
| 574 | // Add one candidate via |AddIceCandidate|. |
| 575 | cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCallerAddress1); |
| 576 | ASSERT_TRUE(callee->AddIceCandidate(&candidate1)); |
| 577 | |
| 578 | // Add the second candidate via a reoffer. |
| 579 | auto offer = caller->CreateOffer(); |
| 580 | cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCallerAddress2); |
| 581 | AddCandidateToFirstTransport(&candidate2, offer.get()); |
| 582 | |
| 583 | // Expect both candidates to appear in the callee's remote description. |
| 584 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 585 | EXPECT_EQ(2u, callee->GetIceCandidatesFromRemoteDescription().size()); |
| 586 | } |
| 587 | |
| 588 | // The follow test verifies that SetLocal/RemoteDescription fails when an offer |
| 589 | // has either ICE ufrag/pwd too short or too long and succeeds otherwise. |
| 590 | // The standard (https://tools.ietf.org/html/rfc5245#section-15.4) says that |
| 591 | // 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] | 592 | TEST_P(PeerConnectionIceTest, VerifyUfragPwdLength) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 593 | auto set_local_description_with_ufrag_pwd_length = [this](int ufrag_len, |
| 594 | int pwd_len) { |
| 595 | auto pc = CreatePeerConnectionWithAudioVideo(); |
| 596 | auto offer = pc->CreateOffer(); |
| 597 | SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'), |
| 598 | std::string(pwd_len, 'x')); |
| 599 | return pc->SetLocalDescription(std::move(offer)); |
| 600 | }; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 601 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 602 | auto set_remote_description_with_ufrag_pwd_length = [this](int ufrag_len, |
| 603 | int pwd_len) { |
| 604 | auto pc = CreatePeerConnectionWithAudioVideo(); |
| 605 | auto offer = pc->CreateOffer(); |
| 606 | SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'), |
| 607 | std::string(pwd_len, 'x')); |
| 608 | return pc->SetRemoteDescription(std::move(offer)); |
| 609 | }; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 610 | |
| 611 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(3, 22)); |
| 612 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(3, 22)); |
| 613 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(257, 22)); |
| 614 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(257, 22)); |
| 615 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 21)); |
| 616 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 21)); |
| 617 | EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 257)); |
| 618 | EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 257)); |
| 619 | EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(4, 22)); |
| 620 | EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(4, 22)); |
| 621 | EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(256, 256)); |
| 622 | EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(256, 256)); |
| 623 | } |
| 624 | |
| 625 | ::testing::AssertionResult AssertIpInCandidates( |
| 626 | const char* address_expr, |
| 627 | const char* candidates_expr, |
| 628 | const SocketAddress& address, |
| 629 | const std::vector<IceCandidateInterface*> candidates) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 630 | rtc::StringBuilder candidate_hosts; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 631 | for (const auto* candidate : candidates) { |
| 632 | const auto& candidate_ip = candidate->candidate().address().ipaddr(); |
| 633 | if (candidate_ip == address.ipaddr()) { |
| 634 | return ::testing::AssertionSuccess(); |
| 635 | } |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 636 | candidate_hosts << "\n" << candidate_ip.ToString(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 637 | } |
| 638 | return ::testing::AssertionFailure() |
| 639 | << address_expr << " (host " << address.HostAsURIString() |
| 640 | << ") not in " << candidates_expr |
| 641 | << " which have the following address hosts:" << candidate_hosts.str(); |
| 642 | } |
| 643 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 644 | TEST_P(PeerConnectionIceTest, CandidatesGeneratedForEachLocalInterface) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 645 | const SocketAddress kLocalAddress1("1.1.1.1", 0); |
| 646 | const SocketAddress kLocalAddress2("2.2.2.2", 0); |
| 647 | |
| 648 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 649 | caller->network()->AddInterface(kLocalAddress1); |
| 650 | caller->network()->AddInterface(kLocalAddress2); |
| 651 | |
| 652 | caller->CreateOfferAndSetAsLocal(); |
| 653 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 654 | |
| 655 | auto candidates = caller->observer()->GetCandidatesByMline(0); |
| 656 | EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress1, candidates); |
| 657 | EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress2, candidates); |
| 658 | } |
| 659 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 660 | TEST_P(PeerConnectionIceTest, TrickledSingleCandidateAddedToRemoteDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 661 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 662 | |
| 663 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 664 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 665 | |
| 666 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 667 | |
| 668 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 669 | callee->AddIceCandidate(&candidate); |
| 670 | auto candidates = callee->GetIceCandidatesFromRemoteDescription(); |
| 671 | ASSERT_EQ(1u, candidates.size()); |
| 672 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate, |
| 673 | candidates[0]->candidate()); |
| 674 | } |
| 675 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 676 | TEST_P(PeerConnectionIceTest, TwoTrickledCandidatesAddedToRemoteDescription) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 677 | const SocketAddress kCalleeAddress1("1.1.1.1", 1111); |
| 678 | const SocketAddress kCalleeAddress2("2.2.2.2", 2222); |
| 679 | |
| 680 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 681 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 682 | |
| 683 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 684 | ASSERT_TRUE( |
| 685 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 686 | |
| 687 | cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCalleeAddress1); |
| 688 | caller->AddIceCandidate(&candidate1); |
| 689 | |
| 690 | cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCalleeAddress2); |
| 691 | caller->AddIceCandidate(&candidate2); |
| 692 | |
| 693 | auto candidates = caller->GetIceCandidatesFromRemoteDescription(); |
| 694 | ASSERT_EQ(2u, candidates.size()); |
| 695 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate1, |
| 696 | candidates[0]->candidate()); |
| 697 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate2, |
| 698 | candidates[1]->candidate()); |
| 699 | } |
| 700 | |
Henrik Boström | ee6f4f6 | 2019-11-06 12:36:12 +0100 | [diff] [blame] | 701 | TEST_P(PeerConnectionIceTest, AsyncAddIceCandidateIsAddedToRemoteDescription) { |
| 702 | auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111)); |
| 703 | |
| 704 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 705 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 706 | |
| 707 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 708 | |
| 709 | auto jsep_candidate = |
| 710 | callee->CreateJsepCandidateForFirstTransport(&candidate); |
| 711 | bool operation_completed = false; |
| 712 | callee->pc()->AddIceCandidate(std::move(jsep_candidate), |
| 713 | [&operation_completed](RTCError result) { |
| 714 | EXPECT_TRUE(result.ok()); |
| 715 | operation_completed = true; |
| 716 | }); |
| 717 | EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout); |
| 718 | |
| 719 | auto candidates = callee->GetIceCandidatesFromRemoteDescription(); |
| 720 | ASSERT_EQ(1u, candidates.size()); |
| 721 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate, |
| 722 | candidates[0]->candidate()); |
| 723 | } |
| 724 | |
| 725 | TEST_P(PeerConnectionIceTest, |
| 726 | AsyncAddIceCandidateCompletesImmediatelyIfNoPendingOperation) { |
| 727 | auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111)); |
| 728 | |
| 729 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 730 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 731 | |
| 732 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 733 | |
| 734 | auto jsep_candidate = |
| 735 | callee->CreateJsepCandidateForFirstTransport(&candidate); |
| 736 | bool operation_completed = false; |
| 737 | callee->pc()->AddIceCandidate( |
| 738 | std::move(jsep_candidate), |
| 739 | [&operation_completed](RTCError result) { operation_completed = true; }); |
| 740 | EXPECT_TRUE(operation_completed); |
| 741 | } |
| 742 | |
| 743 | TEST_P(PeerConnectionIceTest, |
| 744 | AsyncAddIceCandidateCompletesWhenPendingOperationCompletes) { |
| 745 | auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111)); |
| 746 | |
| 747 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 748 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 749 | |
| 750 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 751 | |
| 752 | // Chain an operation that will block AddIceCandidate() from executing. |
| 753 | rtc::scoped_refptr<MockCreateSessionDescriptionObserver> answer_observer( |
| 754 | new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>()); |
| 755 | callee->pc()->CreateAnswer(answer_observer, RTCOfferAnswerOptions()); |
| 756 | |
| 757 | auto jsep_candidate = |
| 758 | callee->CreateJsepCandidateForFirstTransport(&candidate); |
| 759 | bool operation_completed = false; |
| 760 | callee->pc()->AddIceCandidate( |
| 761 | std::move(jsep_candidate), |
| 762 | [&operation_completed](RTCError result) { operation_completed = true; }); |
| 763 | // The operation will not be able to complete until we EXPECT_TRUE_WAIT() |
| 764 | // allowing CreateAnswer() to complete. |
| 765 | EXPECT_FALSE(operation_completed); |
| 766 | EXPECT_TRUE_WAIT(answer_observer->called(), kWaitTimeout); |
| 767 | // As soon as it does, AddIceCandidate() will execute without delay, so it |
| 768 | // must also have completed. |
| 769 | EXPECT_TRUE(operation_completed); |
| 770 | } |
| 771 | |
| 772 | TEST_P(PeerConnectionIceTest, |
| 773 | AsyncAddIceCandidateFailsBeforeSetRemoteDescription) { |
| 774 | auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111)); |
| 775 | |
| 776 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 777 | std::unique_ptr<IceCandidateInterface> jsep_candidate = |
| 778 | CreateIceCandidate(cricket::CN_AUDIO, 0, candidate); |
| 779 | |
| 780 | bool operation_completed = false; |
| 781 | caller->pc()->AddIceCandidate( |
| 782 | std::move(jsep_candidate), [&operation_completed](RTCError result) { |
| 783 | EXPECT_FALSE(result.ok()); |
| 784 | EXPECT_EQ(result.message(), |
| 785 | std::string("Error processing ICE candidate")); |
| 786 | operation_completed = true; |
| 787 | }); |
| 788 | EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout); |
| 789 | } |
| 790 | |
| 791 | TEST_P(PeerConnectionIceTest, |
| 792 | AsyncAddIceCandidateFailsIfPeerConnectionDestroyed) { |
| 793 | auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111)); |
| 794 | |
| 795 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 796 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 797 | |
| 798 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 799 | |
| 800 | // Chain an operation that will block AddIceCandidate() from executing. |
| 801 | rtc::scoped_refptr<MockCreateSessionDescriptionObserver> answer_observer( |
| 802 | new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>()); |
| 803 | callee->pc()->CreateAnswer(answer_observer, RTCOfferAnswerOptions()); |
| 804 | |
| 805 | auto jsep_candidate = |
| 806 | callee->CreateJsepCandidateForFirstTransport(&candidate); |
| 807 | bool operation_completed = false; |
| 808 | callee->pc()->AddIceCandidate( |
| 809 | std::move(jsep_candidate), [&operation_completed](RTCError result) { |
| 810 | EXPECT_FALSE(result.ok()); |
| 811 | EXPECT_EQ( |
| 812 | result.message(), |
| 813 | std::string( |
| 814 | "AddIceCandidate failed because the session was shut down")); |
| 815 | operation_completed = true; |
| 816 | }); |
| 817 | // The operation will not be able to run until EXPECT_TRUE_WAIT(), giving us |
| 818 | // time to remove all references to the PeerConnection. |
| 819 | EXPECT_FALSE(operation_completed); |
| 820 | // This should delete the callee PC. |
| 821 | callee = nullptr; |
| 822 | EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout); |
| 823 | } |
| 824 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 825 | TEST_P(PeerConnectionIceTest, LocalDescriptionUpdatedWhenContinualGathering) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 826 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 827 | |
| 828 | RTCConfiguration config; |
| 829 | config.continual_gathering_policy = |
| 830 | PeerConnectionInterface::GATHER_CONTINUALLY; |
| 831 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 832 | caller->network()->AddInterface(kLocalAddress); |
| 833 | |
| 834 | // Start ICE candidate gathering by setting the local offer. |
| 835 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 836 | |
| 837 | // Since we're using continual gathering, we won't get "gathering done". |
| 838 | EXPECT_TRUE_WAIT( |
| 839 | caller->pc()->local_description()->candidates(0)->count() > 0, |
| 840 | kIceCandidatesTimeout); |
| 841 | } |
| 842 | |
| 843 | // Test that when continual gathering is enabled, and a network interface goes |
| 844 | // down, the candidate is signaled as removed and removed from the local |
| 845 | // description. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 846 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 847 | LocalCandidatesRemovedWhenNetworkDownIfGatheringContinually) { |
| 848 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 849 | |
| 850 | RTCConfiguration config; |
| 851 | config.continual_gathering_policy = |
| 852 | PeerConnectionInterface::GATHER_CONTINUALLY; |
| 853 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 854 | caller->network()->AddInterface(kLocalAddress); |
| 855 | |
| 856 | // Start ICE candidate gathering by setting the local offer. |
| 857 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 858 | |
| 859 | EXPECT_TRUE_WAIT( |
| 860 | caller->pc()->local_description()->candidates(0)->count() > 0, |
| 861 | kIceCandidatesTimeout); |
| 862 | |
| 863 | // Remove the only network interface, causing the PeerConnection to signal |
| 864 | // the removal of all candidates derived from this interface. |
| 865 | caller->network()->RemoveInterface(kLocalAddress); |
| 866 | |
| 867 | EXPECT_EQ_WAIT(0u, caller->pc()->local_description()->candidates(0)->count(), |
| 868 | kIceCandidatesTimeout); |
| 869 | EXPECT_LT(0, caller->observer()->num_candidates_removed_); |
| 870 | } |
| 871 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 872 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 873 | LocalCandidatesNotRemovedWhenNetworkDownIfGatheringOnce) { |
| 874 | const SocketAddress kLocalAddress("1.1.1.1", 0); |
| 875 | |
| 876 | RTCConfiguration config; |
| 877 | config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE; |
| 878 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 879 | caller->network()->AddInterface(kLocalAddress); |
| 880 | |
| 881 | // Start ICE candidate gathering by setting the local offer. |
| 882 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 883 | |
| 884 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout); |
| 885 | |
| 886 | caller->network()->RemoveInterface(kLocalAddress); |
| 887 | |
| 888 | // Verify that the local candidates are not removed; |
| 889 | rtc::Thread::Current()->ProcessMessages(1000); |
| 890 | EXPECT_EQ(0, caller->observer()->num_candidates_removed_); |
| 891 | } |
| 892 | |
| 893 | // The following group tests that when an offer includes a new ufrag or pwd |
| 894 | // (indicating an ICE restart) the old candidates are removed and new candidates |
| 895 | // added to the remote description. |
| 896 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 897 | TEST_P(PeerConnectionIceTest, IceRestartOfferClearsExistingCandidate) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 898 | const SocketAddress kCallerAddress("1.1.1.1", 1111); |
| 899 | |
| 900 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 901 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 902 | |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 903 | auto offer = caller->CreateOfferAndSetAsLocal(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 904 | cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress); |
| 905 | AddCandidateToFirstTransport(&candidate, offer.get()); |
| 906 | |
| 907 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 908 | |
| 909 | RTCOfferAnswerOptions options; |
| 910 | options.ice_restart = true; |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 911 | ASSERT_TRUE( |
| 912 | callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options))); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 913 | |
| 914 | EXPECT_EQ(0u, callee->GetIceCandidatesFromRemoteDescription().size()); |
| 915 | } |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 916 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 917 | IceRestartOfferCandidateReplacesExistingCandidate) { |
| 918 | const SocketAddress kFirstCallerAddress("1.1.1.1", 1111); |
| 919 | const SocketAddress kRestartedCallerAddress("2.2.2.2", 2222); |
| 920 | |
| 921 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 922 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 923 | |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 924 | auto offer = caller->CreateOfferAndSetAsLocal(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 925 | cricket::Candidate old_candidate = |
| 926 | CreateLocalUdpCandidate(kFirstCallerAddress); |
| 927 | AddCandidateToFirstTransport(&old_candidate, offer.get()); |
| 928 | |
| 929 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 930 | |
| 931 | RTCOfferAnswerOptions options; |
| 932 | options.ice_restart = true; |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 933 | auto restart_offer = caller->CreateOfferAndSetAsLocal(options); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 934 | cricket::Candidate new_candidate = |
| 935 | CreateLocalUdpCandidate(kRestartedCallerAddress); |
| 936 | AddCandidateToFirstTransport(&new_candidate, restart_offer.get()); |
| 937 | |
| 938 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(restart_offer))); |
| 939 | |
| 940 | auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription(); |
| 941 | ASSERT_EQ(1u, remote_candidates.size()); |
| 942 | EXPECT_PRED_FORMAT2(AssertCandidatesEqual, new_candidate, |
| 943 | remote_candidates[0]->candidate()); |
| 944 | } |
| 945 | |
| 946 | // Test that if there is not an ICE restart (i.e., nothing changes), then the |
| 947 | // 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] | 948 | TEST_P(PeerConnectionIceTest, LaterAnswerHasSameIceCredentialsIfNoIceRestart) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 949 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 950 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 951 | |
| 952 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 953 | ASSERT_TRUE( |
| 954 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 955 | |
| 956 | // Re-offer. |
| 957 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 958 | |
| 959 | auto answer = callee->CreateAnswer(); |
| 960 | auto* answer_transport_desc = GetFirstTransportDescription(answer.get()); |
| 961 | auto* local_transport_desc = |
| 962 | GetFirstTransportDescription(callee->pc()->local_description()); |
| 963 | |
| 964 | EXPECT_EQ(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag); |
| 965 | EXPECT_EQ(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd); |
| 966 | } |
| 967 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 968 | TEST_P(PeerConnectionIceTest, RestartIceGeneratesNewCredentials) { |
| 969 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 970 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 971 | |
| 972 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 973 | auto initial_ice_credentials = |
| 974 | GetIceCredentials(caller->pc()->local_description()); |
| 975 | caller->pc()->RestartIce(); |
| 976 | ASSERT_TRUE(caller->CreateOfferAndSetAsLocal()); |
| 977 | auto restarted_ice_credentials = |
| 978 | GetIceCredentials(caller->pc()->local_description()); |
| 979 | EXPECT_NE(initial_ice_credentials, restarted_ice_credentials); |
| 980 | } |
| 981 | |
| 982 | TEST_P(PeerConnectionIceTest, |
| 983 | RestartIceWhileLocalOfferIsPendingGeneratesNewCredentialsInNextOffer) { |
| 984 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 985 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 986 | |
| 987 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 988 | auto initial_ice_credentials = |
| 989 | GetIceCredentials(caller->pc()->local_description()); |
| 990 | // ICE restart becomes needed while an O/A is pending and |caller| is the |
| 991 | // offerer. |
| 992 | caller->pc()->RestartIce(); |
| 993 | ASSERT_TRUE( |
| 994 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 995 | ASSERT_TRUE(caller->CreateOfferAndSetAsLocal()); |
| 996 | auto restarted_ice_credentials = |
| 997 | GetIceCredentials(caller->pc()->local_description()); |
| 998 | EXPECT_NE(initial_ice_credentials, restarted_ice_credentials); |
| 999 | } |
| 1000 | |
| 1001 | TEST_P(PeerConnectionIceTest, |
| 1002 | RestartIceWhileRemoteOfferIsPendingGeneratesNewCredentialsInNextOffer) { |
| 1003 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1004 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1005 | |
| 1006 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 1007 | auto initial_ice_credentials = |
| 1008 | GetIceCredentials(caller->pc()->local_description()); |
| 1009 | ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal())); |
| 1010 | // ICE restart becomes needed while an O/A is pending and |caller| is the |
| 1011 | // answerer. |
| 1012 | caller->pc()->RestartIce(); |
| 1013 | ASSERT_TRUE( |
| 1014 | callee->SetRemoteDescription(caller->CreateAnswerAndSetAsLocal())); |
| 1015 | ASSERT_TRUE(caller->CreateOfferAndSetAsLocal()); |
| 1016 | auto restarted_ice_credentials = |
| 1017 | GetIceCredentials(caller->pc()->local_description()); |
| 1018 | EXPECT_NE(initial_ice_credentials, restarted_ice_credentials); |
| 1019 | } |
| 1020 | |
| 1021 | TEST_P(PeerConnectionIceTest, RestartIceTriggeredByRemoteSide) { |
| 1022 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1023 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1024 | |
| 1025 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 1026 | auto initial_ice_credentials = |
| 1027 | GetIceCredentials(caller->pc()->local_description()); |
| 1028 | |
| 1029 | // Remote restart and O/A exchange with |caller| as the answerer should |
| 1030 | // restart ICE locally as well. |
| 1031 | callee->pc()->RestartIce(); |
| 1032 | ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get())); |
| 1033 | |
| 1034 | auto restarted_ice_credentials = |
| 1035 | GetIceCredentials(caller->pc()->local_description()); |
| 1036 | EXPECT_NE(initial_ice_credentials, restarted_ice_credentials); |
| 1037 | } |
| 1038 | |
| 1039 | TEST_P(PeerConnectionIceTest, RestartIceCausesNegotiationNeeded) { |
| 1040 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1041 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1042 | |
| 1043 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1044 | caller->observer()->clear_legacy_renegotiation_needed(); |
| 1045 | caller->observer()->clear_latest_negotiation_needed_event(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1046 | caller->pc()->RestartIce(); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1047 | EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed()); |
| 1048 | EXPECT_TRUE(caller->observer()->has_negotiation_needed_event()); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | // In Unified Plan, "onnegotiationneeded" is spec-compliant, including not |
| 1052 | // firing multipe times in a row, or firing when returning to the stable |
| 1053 | // signaling state if negotiation is still needed. In Plan B it fires any time |
| 1054 | // something changes. As such, some tests are SdpSemantics-specific. |
| 1055 | class PeerConnectionIceTestUnifiedPlan : public PeerConnectionIceBaseTest { |
| 1056 | protected: |
| 1057 | PeerConnectionIceTestUnifiedPlan() |
| 1058 | : PeerConnectionIceBaseTest(SdpSemantics::kUnifiedPlan) {} |
| 1059 | }; |
| 1060 | |
| 1061 | TEST_F(PeerConnectionIceTestUnifiedPlan, |
| 1062 | RestartIceWhileLocalOfferIsPendingCausesNegotiationNeededWhenStable) { |
| 1063 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1064 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1065 | |
| 1066 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 1067 | // ICE restart becomes needed while an O/A is pending and |caller| is the |
| 1068 | // offerer. |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1069 | caller->observer()->clear_legacy_renegotiation_needed(); |
| 1070 | caller->observer()->clear_latest_negotiation_needed_event(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1071 | caller->pc()->RestartIce(); |
| 1072 | // In Unified Plan, the event should not fire until we are back in the stable |
| 1073 | // signaling state. |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1074 | EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed()); |
| 1075 | EXPECT_FALSE(caller->observer()->has_negotiation_needed_event()); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1076 | ASSERT_TRUE( |
| 1077 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1078 | EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed()); |
| 1079 | EXPECT_TRUE(caller->observer()->has_negotiation_needed_event()); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | TEST_F(PeerConnectionIceTestUnifiedPlan, |
| 1083 | RestartIceWhileRemoteOfferIsPendingCausesNegotiationNeededWhenStable) { |
| 1084 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1085 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1086 | |
| 1087 | // Establish initial credentials as the caller. |
| 1088 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 1089 | ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal())); |
| 1090 | // ICE restart becomes needed while an O/A is pending and |caller| is the |
| 1091 | // answerer. |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1092 | caller->observer()->clear_legacy_renegotiation_needed(); |
| 1093 | caller->observer()->clear_latest_negotiation_needed_event(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1094 | caller->pc()->RestartIce(); |
| 1095 | // In Unified Plan, the event should not fire until we are back in the stable |
| 1096 | // signaling state. |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1097 | EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed()); |
| 1098 | EXPECT_FALSE(caller->observer()->has_negotiation_needed_event()); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1099 | ASSERT_TRUE( |
| 1100 | callee->SetRemoteDescription(caller->CreateAnswerAndSetAsLocal())); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1101 | EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed()); |
| 1102 | EXPECT_TRUE(caller->observer()->has_negotiation_needed_event()); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | TEST_F(PeerConnectionIceTestUnifiedPlan, |
| 1106 | RestartIceTriggeredByRemoteSideCauseNegotiationNotNeeded) { |
| 1107 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1108 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1109 | |
| 1110 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 1111 | // Local restart. |
| 1112 | caller->pc()->RestartIce(); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1113 | caller->observer()->clear_legacy_renegotiation_needed(); |
| 1114 | caller->observer()->clear_latest_negotiation_needed_event(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1115 | // Remote restart and O/A exchange with |caller| as the answerer should |
| 1116 | // restart ICE locally as well. |
| 1117 | callee->pc()->RestartIce(); |
| 1118 | ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get())); |
| 1119 | // Having restarted ICE by the remote offer, we do not need to renegotiate ICE |
| 1120 | // credentials when back in the stable signaling state. |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1121 | EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed()); |
| 1122 | EXPECT_FALSE(caller->observer()->has_negotiation_needed_event()); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | TEST_F(PeerConnectionIceTestUnifiedPlan, |
| 1126 | RestartIceTwiceDoesNotFireNegotiationNeededTwice) { |
| 1127 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1128 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1129 | |
| 1130 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 1131 | caller->pc()->RestartIce(); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1132 | EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed()); |
| 1133 | EXPECT_TRUE(caller->observer()->has_negotiation_needed_event()); |
| 1134 | caller->observer()->clear_legacy_renegotiation_needed(); |
| 1135 | caller->observer()->clear_latest_negotiation_needed_event(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1136 | caller->pc()->RestartIce(); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1137 | EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed()); |
| 1138 | EXPECT_FALSE(caller->observer()->has_negotiation_needed_event()); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | // In Plan B, "onnegotiationneeded" is not spec-compliant, firing based on if |
| 1142 | // something changed rather than if negotiation is needed. In Unified Plan it |
| 1143 | // fires according to spec. As such, some tests are SdpSemantics-specific. |
| 1144 | class PeerConnectionIceTestPlanB : public PeerConnectionIceBaseTest { |
| 1145 | protected: |
| 1146 | PeerConnectionIceTestPlanB() |
| 1147 | : PeerConnectionIceBaseTest(SdpSemantics::kPlanB) {} |
| 1148 | }; |
| 1149 | |
| 1150 | TEST_F(PeerConnectionIceTestPlanB, |
| 1151 | RestartIceWhileOfferIsPendingCausesNegotiationNeededImmediately) { |
| 1152 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1153 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1154 | |
| 1155 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1156 | caller->observer()->clear_legacy_renegotiation_needed(); |
| 1157 | caller->observer()->clear_latest_negotiation_needed_event(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1158 | caller->pc()->RestartIce(); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1159 | EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed()); |
| 1160 | EXPECT_TRUE(caller->observer()->has_negotiation_needed_event()); |
| 1161 | caller->observer()->clear_legacy_renegotiation_needed(); |
| 1162 | caller->observer()->clear_latest_negotiation_needed_event(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1163 | ASSERT_TRUE( |
| 1164 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 1165 | // In Plan B, the event fired early so we don't expect it to fire now. This is |
| 1166 | // not spec-compliant but follows the pattern of existing Plan B behavior. |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1167 | EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed()); |
| 1168 | EXPECT_FALSE(caller->observer()->has_negotiation_needed_event()); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | TEST_F(PeerConnectionIceTestPlanB, |
| 1172 | RestartIceTwiceDoesFireNegotiationNeededTwice) { |
| 1173 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1174 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1175 | |
| 1176 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1177 | caller->observer()->clear_legacy_renegotiation_needed(); |
| 1178 | caller->observer()->clear_latest_negotiation_needed_event(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1179 | caller->pc()->RestartIce(); |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1180 | EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed()); |
| 1181 | EXPECT_TRUE(caller->observer()->has_negotiation_needed_event()); |
| 1182 | caller->observer()->clear_legacy_renegotiation_needed(); |
| 1183 | caller->observer()->clear_latest_negotiation_needed_event(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1184 | caller->pc()->RestartIce(); |
| 1185 | // In Plan B, the event fires every time something changed, even if we have |
| 1186 | // already fired the event. This is not spec-compliant but follows the same |
| 1187 | // pattern of existing Plan B behavior. |
Henrik Boström | e574a31 | 2020-08-25 10:20:11 +0200 | [diff] [blame] | 1188 | EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed()); |
| 1189 | EXPECT_TRUE(caller->observer()->has_negotiation_needed_event()); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1192 | // The following parameterized test verifies that if an offer is sent with a |
| 1193 | // modified ICE ufrag and/or ICE pwd, then the answer should identify that the |
| 1194 | // other side has initiated an ICE restart and generate a new ufrag and pwd. |
| 1195 | // RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or |
| 1196 | // a=ice-pwd attributes compared to the previous SDP from the peer, it |
| 1197 | // indicates that ICE is restarting for this media stream." |
| 1198 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 1199 | class PeerConnectionIceUfragPwdAnswerTest |
| 1200 | : public PeerConnectionIceBaseTest, |
| 1201 | public ::testing::WithParamInterface< |
| 1202 | std::tuple<SdpSemantics, std::tuple<bool, bool>>> { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1203 | protected: |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 1204 | PeerConnectionIceUfragPwdAnswerTest() |
| 1205 | : PeerConnectionIceBaseTest(std::get<0>(GetParam())) { |
| 1206 | auto param = std::get<1>(GetParam()); |
| 1207 | offer_new_ufrag_ = std::get<0>(param); |
| 1208 | offer_new_pwd_ = std::get<1>(param); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | bool offer_new_ufrag_; |
| 1212 | bool offer_new_pwd_; |
| 1213 | }; |
| 1214 | |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 1215 | TEST_P(PeerConnectionIceUfragPwdAnswerTest, TestIncludedInAnswer) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1216 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1217 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1218 | |
| 1219 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 1220 | ASSERT_TRUE( |
| 1221 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 1222 | |
| 1223 | auto offer = caller->CreateOffer(); |
| 1224 | auto* offer_transport_desc = GetFirstTransportDescription(offer.get()); |
| 1225 | if (offer_new_ufrag_) { |
Steve Anton | 71ff073 | 2020-01-24 16:28:15 -0800 | [diff] [blame] | 1226 | offer_transport_desc->ice_ufrag += "+new"; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1227 | } |
| 1228 | if (offer_new_pwd_) { |
Steve Anton | 71ff073 | 2020-01-24 16:28:15 -0800 | [diff] [blame] | 1229 | offer_transport_desc->ice_pwd += "+new"; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 1233 | |
| 1234 | auto answer = callee->CreateAnswer(); |
| 1235 | auto* answer_transport_desc = GetFirstTransportDescription(answer.get()); |
| 1236 | auto* local_transport_desc = |
| 1237 | GetFirstTransportDescription(callee->pc()->local_description()); |
| 1238 | |
| 1239 | EXPECT_NE(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag); |
| 1240 | EXPECT_NE(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd); |
| 1241 | } |
| 1242 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 1243 | INSTANTIATE_TEST_SUITE_P( |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 1244 | PeerConnectionIceTest, |
| 1245 | PeerConnectionIceUfragPwdAnswerTest, |
| 1246 | Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan), |
| 1247 | Values(std::make_pair(true, true), // Both changed. |
| 1248 | std::make_pair(true, false), // Only ufrag changed. |
| 1249 | std::make_pair(false, true)))); // Only pwd changed. |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1250 | |
| 1251 | // Test that if an ICE restart is offered on one media section, then the answer |
| 1252 | // will only change ICE ufrag/pwd for that section and keep the other sections |
| 1253 | // the same. |
| 1254 | // Note that this only works if we have disabled BUNDLE, otherwise all media |
| 1255 | // sections will share the same transport. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 1256 | TEST_P(PeerConnectionIceTest, |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1257 | CreateAnswerHasNewUfragPwdForOnlyMediaSectionWhichRestarted) { |
| 1258 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1259 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1260 | |
| 1261 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 1262 | ASSERT_TRUE( |
| 1263 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 1264 | |
| 1265 | RTCOfferAnswerOptions disable_bundle_options; |
| 1266 | disable_bundle_options.use_rtp_mux = false; |
| 1267 | |
| 1268 | auto offer = caller->CreateOffer(disable_bundle_options); |
| 1269 | |
| 1270 | // Signal ICE restart on the first media section. |
| 1271 | auto* offer_transport_desc = GetFirstTransportDescription(offer.get()); |
Steve Anton | 71ff073 | 2020-01-24 16:28:15 -0800 | [diff] [blame] | 1272 | offer_transport_desc->ice_ufrag += "+new"; |
| 1273 | offer_transport_desc->ice_pwd += "+new"; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1274 | |
| 1275 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 1276 | |
| 1277 | auto answer = callee->CreateAnswer(disable_bundle_options); |
| 1278 | const auto& answer_transports = answer->description()->transport_infos(); |
| 1279 | const auto& local_transports = |
| 1280 | callee->pc()->local_description()->description()->transport_infos(); |
| 1281 | |
| 1282 | EXPECT_NE(answer_transports[0].description.ice_ufrag, |
| 1283 | local_transports[0].description.ice_ufrag); |
| 1284 | EXPECT_NE(answer_transports[0].description.ice_pwd, |
| 1285 | local_transports[0].description.ice_pwd); |
| 1286 | EXPECT_EQ(answer_transports[1].description.ice_ufrag, |
| 1287 | local_transports[1].description.ice_ufrag); |
| 1288 | EXPECT_EQ(answer_transports[1].description.ice_pwd, |
| 1289 | local_transports[1].description.ice_pwd); |
| 1290 | } |
| 1291 | |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 1292 | // Test that when the initial offerer (caller) uses the lite implementation of |
| 1293 | // ICE and the callee uses the full implementation, the caller takes the |
| 1294 | // CONTROLLED role and the callee takes the CONTROLLING role. This is specified |
| 1295 | // in RFC5245 Section 5.1.1. |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 1296 | TEST_P(PeerConnectionIceTest, |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 1297 | OfferFromLiteIceControlledAndAnswerFromFullIceControlling) { |
| 1298 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1299 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1300 | |
| 1301 | auto offer = caller->CreateOffer(); |
| 1302 | SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE); |
| 1303 | ASSERT_TRUE( |
| 1304 | caller->SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 1305 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 1306 | |
| 1307 | auto answer = callee->CreateAnswer(); |
| 1308 | SetIceMode(answer.get(), cricket::IceMode::ICEMODE_FULL); |
| 1309 | ASSERT_TRUE( |
| 1310 | callee->SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 1311 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 1312 | |
| 1313 | EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(caller)); |
| 1314 | EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(callee)); |
| 1315 | } |
| 1316 | |
| 1317 | // Test that when the caller and the callee both use the lite implementation of |
| 1318 | // ICE, the initial offerer (caller) takes the CONTROLLING role and the callee |
| 1319 | // 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] | 1320 | TEST_P(PeerConnectionIceTest, |
Qingsi Wang | e169272 | 2017-11-29 13:27:20 -0800 | [diff] [blame] | 1321 | OfferFromLiteIceControllingAndAnswerFromLiteIceControlled) { |
| 1322 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 1323 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 1324 | |
| 1325 | auto offer = caller->CreateOffer(); |
| 1326 | SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE); |
| 1327 | ASSERT_TRUE( |
| 1328 | caller->SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 1329 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 1330 | |
| 1331 | auto answer = callee->CreateAnswer(); |
| 1332 | SetIceMode(answer.get(), cricket::IceMode::ICEMODE_LITE); |
| 1333 | ASSERT_TRUE( |
| 1334 | callee->SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 1335 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 1336 | |
| 1337 | EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(caller)); |
| 1338 | EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(callee)); |
| 1339 | } |
| 1340 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 1341 | INSTANTIATE_TEST_SUITE_P(PeerConnectionIceTest, |
| 1342 | PeerConnectionIceTest, |
| 1343 | Values(SdpSemantics::kPlanB, |
| 1344 | SdpSemantics::kUnifiedPlan)); |
Steve Anton | 46d926a | 2018-01-23 10:23:06 -0800 | [diff] [blame] | 1345 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 1346 | class PeerConnectionIceConfigTest : public ::testing::Test { |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 1347 | protected: |
| 1348 | void SetUp() override { |
| 1349 | pc_factory_ = CreatePeerConnectionFactory( |
| 1350 | rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(), |
| 1351 | FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(), |
Anders Carlsson | 6753795 | 2018-05-03 11:28:29 +0200 | [diff] [blame] | 1352 | CreateBuiltinAudioDecoderFactory(), CreateBuiltinVideoEncoderFactory(), |
| 1353 | CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */, |
| 1354 | nullptr /* audio_processing */); |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 1355 | } |
| 1356 | void CreatePeerConnection(const RTCConfiguration& config) { |
| 1357 | std::unique_ptr<cricket::FakePortAllocator> port_allocator( |
| 1358 | new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)); |
| 1359 | port_allocator_ = port_allocator.get(); |
| 1360 | rtc::scoped_refptr<PeerConnectionInterface> pc( |
Niels Möller | f06f923 | 2018-08-07 12:32:18 +0200 | [diff] [blame] | 1361 | pc_factory_->CreatePeerConnection(config, std::move(port_allocator), |
| 1362 | nullptr /* cert_generator */, |
| 1363 | &observer_)); |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 1364 | EXPECT_TRUE(pc.get()); |
Mirko Bonadei | 1c54605 | 2019-02-04 14:50:38 +0100 | [diff] [blame] | 1365 | pc_ = std::move(pc); |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_ = nullptr; |
| 1369 | rtc::scoped_refptr<PeerConnectionInterface> pc_ = nullptr; |
| 1370 | cricket::FakePortAllocator* port_allocator_ = nullptr; |
| 1371 | |
| 1372 | MockPeerConnectionObserver observer_; |
| 1373 | }; |
| 1374 | |
| 1375 | TEST_F(PeerConnectionIceConfigTest, SetStunCandidateKeepaliveInterval) { |
| 1376 | RTCConfiguration config; |
| 1377 | config.stun_candidate_keepalive_interval = 123; |
| 1378 | config.ice_candidate_pool_size = 1; |
| 1379 | CreatePeerConnection(config); |
| 1380 | ASSERT_NE(port_allocator_, nullptr); |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 1381 | absl::optional<int> actual_stun_keepalive_interval = |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 1382 | port_allocator_->stun_candidate_keepalive_interval(); |
| 1383 | EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 123); |
| 1384 | config.stun_candidate_keepalive_interval = 321; |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 1385 | ASSERT_TRUE(pc_->SetConfiguration(config).ok()); |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 1386 | actual_stun_keepalive_interval = |
| 1387 | port_allocator_->stun_candidate_keepalive_interval(); |
| 1388 | EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 321); |
| 1389 | } |
| 1390 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1391 | TEST_P(PeerConnectionIceTest, IceCredentialsCreateOffer) { |
| 1392 | RTCConfiguration config; |
| 1393 | config.ice_candidate_pool_size = 1; |
| 1394 | auto pc = CreatePeerConnectionWithAudioVideo(config); |
| 1395 | ASSERT_NE(pc->port_allocator_, nullptr); |
| 1396 | auto offer = pc->CreateOffer(); |
| 1397 | auto credentials = pc->port_allocator_->GetPooledIceCredentials(); |
| 1398 | ASSERT_EQ(1u, credentials.size()); |
| 1399 | |
| 1400 | auto* desc = offer->description(); |
| 1401 | for (const auto& content : desc->contents()) { |
| 1402 | auto* transport_info = desc->GetTransportInfoByName(content.name); |
| 1403 | EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag); |
| 1404 | EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd); |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | TEST_P(PeerConnectionIceTest, IceCredentialsCreateAnswer) { |
| 1409 | RTCConfiguration config; |
| 1410 | config.ice_candidate_pool_size = 1; |
| 1411 | auto pc = CreatePeerConnectionWithAudioVideo(config); |
| 1412 | ASSERT_NE(pc->port_allocator_, nullptr); |
| 1413 | auto offer = pc->CreateOffer(); |
| 1414 | ASSERT_TRUE(pc->SetRemoteDescription(std::move(offer))); |
| 1415 | auto answer = pc->CreateAnswer(); |
| 1416 | |
| 1417 | auto credentials = pc->port_allocator_->GetPooledIceCredentials(); |
| 1418 | ASSERT_EQ(1u, credentials.size()); |
| 1419 | |
| 1420 | auto* desc = answer->description(); |
| 1421 | for (const auto& content : desc->contents()) { |
| 1422 | auto* transport_info = desc->GetTransportInfoByName(content.name); |
| 1423 | EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag); |
| 1424 | EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd); |
| 1425 | } |
| 1426 | } |
| 1427 | |
Steve Anton | ec47b57 | 2020-01-24 14:53:37 -0800 | [diff] [blame] | 1428 | // Regression test for https://bugs.chromium.org/p/webrtc/issues/detail?id=4728 |
| 1429 | TEST_P(PeerConnectionIceTest, CloseDoesNotTransitionGatheringStateToComplete) { |
| 1430 | auto pc = CreatePeerConnectionWithAudioVideo(); |
| 1431 | pc->pc()->Close(); |
| 1432 | EXPECT_FALSE(pc->IsIceGatheringDone()); |
| 1433 | EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew, |
| 1434 | pc->pc()->ice_gathering_state()); |
| 1435 | } |
| 1436 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 1437 | } // namespace webrtc |