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