blob: f2ac38c4380b0dc7e226f7c80bd87cdfef789223 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
kwiberg0eb15ed2015-12-17 03:04:15 -080012#include <utility>
deadbeefcbecd352015-09-23 11:50:27 -070013#include <vector>
14
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/audiotrack.h"
16#include "webrtc/api/fakemediacontroller.h"
17#include "webrtc/api/fakemetricsobserver.h"
18#include "webrtc/api/jsepicecandidate.h"
19#include "webrtc/api/jsepsessiondescription.h"
20#include "webrtc/api/peerconnection.h"
21#include "webrtc/api/sctputils.h"
Henrik Boströmd79599d2016-06-01 13:58:50 +020022#include "webrtc/api/test/fakertccertificategenerator.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/videotrack.h"
24#include "webrtc/api/webrtcsession.h"
25#include "webrtc/api/webrtcsessiondescriptionfactory.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026#include "webrtc/base/fakenetwork.h"
27#include "webrtc/base/firewallsocketserver.h"
28#include "webrtc/base/gunit.h"
29#include "webrtc/base/logging.h"
30#include "webrtc/base/network.h"
31#include "webrtc/base/physicalsocketserver.h"
32#include "webrtc/base/ssladapter.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020033#include "webrtc/base/sslidentity.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/sslstreamadapter.h"
35#include "webrtc/base/stringutils.h"
36#include "webrtc/base/thread.h"
37#include "webrtc/base/virtualsocketserver.h"
kjellandera96e2d72016-02-04 23:52:28 -080038#include "webrtc/media/base/fakemediaengine.h"
39#include "webrtc/media/base/fakevideorenderer.h"
40#include "webrtc/media/base/mediachannel.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010041#include "webrtc/media/engine/fakewebrtccall.h"
kjellandera96e2d72016-02-04 23:52:28 -080042#include "webrtc/p2p/base/stunserver.h"
43#include "webrtc/p2p/base/teststunserver.h"
44#include "webrtc/p2p/base/testturnserver.h"
45#include "webrtc/p2p/base/transportchannel.h"
46#include "webrtc/p2p/client/basicportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010047#include "webrtc/pc/channelmanager.h"
48#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50#define MAYBE_SKIP_TEST(feature) \
51 if (!(feature())) { \
52 LOG(LS_INFO) << "Feature disabled... skipping"; \
53 return; \
54 }
55
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056using cricket::FakeVoiceMediaChannel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057using cricket::TransportInfo;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000058using rtc::SocketAddress;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059using rtc::Thread;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060using webrtc::CreateSessionDescription;
wu@webrtc.org91053e72013-08-10 07:18:04 +000061using webrtc::CreateSessionDescriptionObserver;
62using webrtc::CreateSessionDescriptionRequest;
deadbeefab9b2d12015-10-14 11:33:11 -070063using webrtc::DataChannel;
Henrik Boström5e56c592015-08-11 10:33:13 +020064using webrtc::DtlsIdentityStoreInterface;
jbauchac8869e2015-07-03 01:36:14 -070065using webrtc::FakeMetricsObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066using webrtc::IceCandidateCollection;
deadbeefab9b2d12015-10-14 11:33:11 -070067using webrtc::InternalDataChannelInit;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068using webrtc::JsepIceCandidate;
69using webrtc::JsepSessionDescription;
wu@webrtc.org97077a32013-10-25 21:18:33 +000070using webrtc::PeerConnectionFactoryInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071using webrtc::PeerConnectionInterface;
72using webrtc::SessionDescriptionInterface;
deadbeefd59daf82015-10-14 15:02:44 -070073using webrtc::SessionStats;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074using webrtc::StreamCollection;
wu@webrtc.org91053e72013-08-10 07:18:04 +000075using webrtc::WebRtcSession;
wu@webrtc.org364f2042013-11-20 21:49:41 +000076using webrtc::kBundleWithoutRtcpMux;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000077using webrtc::kCreateChannelFailed;
78using webrtc::kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079using webrtc::kMlineMismatch;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000080using webrtc::kPushDownTDFailed;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000081using webrtc::kSdpWithoutIceUfragPwd;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000082using webrtc::kSdpWithoutDtlsFingerprint;
83using webrtc::kSdpWithoutSdesCrypto;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084using webrtc::kSessionError;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000085using webrtc::kSessionErrorDesc;
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000086using webrtc::kMaxUnsignalledRecvStreams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +000088typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
89
wu@webrtc.org364f2042013-11-20 21:49:41 +000090static const int kClientAddrPort = 0;
91static const char kClientAddrHost1[] = "11.11.11.11";
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +000092static const char kClientIPv6AddrHost1[] =
93 "2620:0:aaaa:bbbb:cccc:dddd:eeee:ffff";
wu@webrtc.org364f2042013-11-20 21:49:41 +000094static const char kClientAddrHost2[] = "22.22.22.22";
95static const char kStunAddrHost[] = "99.99.99.1";
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000096static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
97static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +000098static const char kTurnUsername[] = "test";
99static const char kTurnPassword[] = "test";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
101static const char kSessionVersion[] = "1";
102
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103// Media index of candidates belonging to the first media content.
104static const int kMediaContentIndex0 = 0;
105static const char kMediaContentName0[] = "audio";
106
107// Media index of candidates belonging to the second media content.
108static const int kMediaContentIndex1 = 1;
109static const char kMediaContentName1[] = "video";
110
111static const int kIceCandidatesTimeout = 10000;
112
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000113static const char kFakeDtlsFingerprint[] =
114 "BB:CD:72:F7:2F:D0:BA:43:F3:68:B1:0C:23:72:B6:4A:"
115 "0F:DE:34:06:BC:E0:FE:01:BC:73:C8:6D:F4:65:D5:24";
116
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +0000117static const char kTooLongIceUfragPwd[] =
118 "IceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfrag"
119 "IceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfrag"
120 "IceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfrag"
121 "IceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfrag";
122
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000123static const char kSdpWithRtx[] =
124 "v=0\r\n"
125 "o=- 4104004319237231850 2 IN IP4 127.0.0.1\r\n"
126 "s=-\r\n"
127 "t=0 0\r\n"
128 "a=msid-semantic: WMS stream1\r\n"
129 "m=video 9 RTP/SAVPF 0 96\r\n"
130 "c=IN IP4 0.0.0.0\r\n"
131 "a=rtcp:9 IN IP4 0.0.0.0\r\n"
132 "a=ice-ufrag:CerjGp19G7wpXwl7\r\n"
133 "a=ice-pwd:cMvOlFvQ6ochez1ZOoC2uBEC\r\n"
134 "a=mid:video\r\n"
135 "a=sendrecv\r\n"
136 "a=rtcp-mux\r\n"
137 "a=crypto:1 AES_CM_128_HMAC_SHA1_80 "
138 "inline:5/4N5CDvMiyDArHtBByUM71VIkguH17ZNoX60GrA\r\n"
139 "a=rtpmap:0 fake_video_codec/90000\r\n"
140 "a=rtpmap:96 rtx/90000\r\n"
141 "a=fmtp:96 apt=0\r\n";
142
deadbeefab9b2d12015-10-14 11:33:11 -0700143static const char kStream1[] = "stream1";
144static const char kVideoTrack1[] = "video1";
145static const char kAudioTrack1[] = "audio1";
146
147static const char kStream2[] = "stream2";
148static const char kVideoTrack2[] = "video2";
149static const char kAudioTrack2[] = "audio2";
150
Henrik Boström87713d02015-08-25 09:53:21 +0200151enum RTCCertificateGenerationMethod { ALREADY_GENERATED, DTLS_IDENTITY_STORE };
152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153class MockIceObserver : public webrtc::IceObserver {
154 public:
155 MockIceObserver()
156 : oncandidatesready_(false),
157 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
158 ice_gathering_state_(PeerConnectionInterface::kIceGatheringNew) {
159 }
160
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200161 virtual ~MockIceObserver() = default;
162
perkjdfb769d2016-02-09 03:09:43 -0800163 void OnIceConnectionChange(
164 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 ice_connection_state_ = new_state;
skvlad6c87a672016-05-17 17:49:52 -0700166 ice_connection_state_history_.push_back(new_state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 }
perkjdfb769d2016-02-09 03:09:43 -0800168 void OnIceGatheringChange(
169 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 // We can never transition back to "new".
171 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, new_state);
172 ice_gathering_state_ = new_state;
perkjdfb769d2016-02-09 03:09:43 -0800173 oncandidatesready_ =
174 new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 }
176
177 // Found a new candidate.
perkjdfb769d2016-02-09 03:09:43 -0800178 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000179 switch (candidate->sdp_mline_index()) {
180 case kMediaContentIndex0:
181 mline_0_candidates_.push_back(candidate->candidate());
182 break;
183 case kMediaContentIndex1:
184 mline_1_candidates_.push_back(candidate->candidate());
185 break;
186 default:
187 ASSERT(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 }
wu@webrtc.org364f2042013-11-20 21:49:41 +0000189
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 // The ICE gathering state should always be Gathering when a candidate is
191 // received (or possibly Completed in the case of the final candidate).
192 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, ice_gathering_state_);
193 }
194
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700195 // Some local candidates are removed.
196 void OnIceCandidatesRemoved(
nisseef8b61e2016-04-29 06:09:15 -0700197 const std::vector<cricket::Candidate>& candidates) override {
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700198 num_candidates_removed_ += candidates.size();
199 }
200
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 bool oncandidatesready_;
202 std::vector<cricket::Candidate> mline_0_candidates_;
203 std::vector<cricket::Candidate> mline_1_candidates_;
204 PeerConnectionInterface::IceConnectionState ice_connection_state_;
205 PeerConnectionInterface::IceGatheringState ice_gathering_state_;
skvlad6c87a672016-05-17 17:49:52 -0700206 std::vector<PeerConnectionInterface::IceConnectionState>
207 ice_connection_state_history_;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700208 size_t num_candidates_removed_ = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209};
210
211class WebRtcSessionForTest : public webrtc::WebRtcSession {
212 public:
stefanc1aeaf02015-10-15 07:26:07 -0700213 WebRtcSessionForTest(webrtc::MediaControllerInterface* media_controller,
danilchape9021a32016-05-17 01:52:02 -0700214 rtc::Thread* network_thread,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000215 rtc::Thread* worker_thread,
danilchape9021a32016-05-17 01:52:02 -0700216 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 cricket::PortAllocator* port_allocator,
deadbeefab9b2d12015-10-14 11:33:11 -0700218 webrtc::IceObserver* ice_observer)
stefanc1aeaf02015-10-15 07:26:07 -0700219 : WebRtcSession(media_controller,
danilchape9021a32016-05-17 01:52:02 -0700220 network_thread,
stefanc1aeaf02015-10-15 07:26:07 -0700221 worker_thread,
danilchape9021a32016-05-17 01:52:02 -0700222 signaling_thread,
stefanc1aeaf02015-10-15 07:26:07 -0700223 port_allocator) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 RegisterIceObserver(ice_observer);
225 }
226 virtual ~WebRtcSessionForTest() {}
227
deadbeefcbecd352015-09-23 11:50:27 -0700228 // Note that these methods are only safe to use if the signaling thread
229 // is the same as the worker thread
230 cricket::TransportChannel* voice_rtp_transport_channel() {
231 return rtp_transport_channel(voice_channel());
232 }
233
234 cricket::TransportChannel* voice_rtcp_transport_channel() {
235 return rtcp_transport_channel(voice_channel());
236 }
237
238 cricket::TransportChannel* video_rtp_transport_channel() {
239 return rtp_transport_channel(video_channel());
240 }
241
242 cricket::TransportChannel* video_rtcp_transport_channel() {
243 return rtcp_transport_channel(video_channel());
244 }
245
246 cricket::TransportChannel* data_rtp_transport_channel() {
247 return rtp_transport_channel(data_channel());
248 }
249
250 cricket::TransportChannel* data_rtcp_transport_channel() {
251 return rtcp_transport_channel(data_channel());
252 }
253
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 using webrtc::WebRtcSession::SetAudioPlayout;
255 using webrtc::WebRtcSession::SetAudioSend;
nisse2ded9b12016-04-08 02:23:55 -0700256 using webrtc::WebRtcSession::SetSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 using webrtc::WebRtcSession::SetVideoPlayout;
258 using webrtc::WebRtcSession::SetVideoSend;
deadbeefcbecd352015-09-23 11:50:27 -0700259
260 private:
261 cricket::TransportChannel* rtp_transport_channel(cricket::BaseChannel* ch) {
262 if (!ch) {
263 return nullptr;
264 }
265 return ch->transport_channel();
266 }
267
268 cricket::TransportChannel* rtcp_transport_channel(cricket::BaseChannel* ch) {
269 if (!ch) {
270 return nullptr;
271 }
272 return ch->rtcp_transport_channel();
273 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274};
275
wu@webrtc.org91053e72013-08-10 07:18:04 +0000276class WebRtcSessionCreateSDPObserverForTest
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000277 : public rtc::RefCountedObject<CreateSessionDescriptionObserver> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 public:
wu@webrtc.org91053e72013-08-10 07:18:04 +0000279 enum State {
280 kInit,
281 kFailed,
282 kSucceeded,
283 };
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000284 WebRtcSessionCreateSDPObserverForTest() : state_(kInit) {}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000285
286 // CreateSessionDescriptionObserver implementation.
287 virtual void OnSuccess(SessionDescriptionInterface* desc) {
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000288 description_.reset(desc);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000289 state_ = kSucceeded;
290 }
291 virtual void OnFailure(const std::string& error) {
292 state_ = kFailed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 }
294
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000295 SessionDescriptionInterface* description() { return description_.get(); }
296
297 SessionDescriptionInterface* ReleaseDescription() {
298 return description_.release();
299 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300
wu@webrtc.org91053e72013-08-10 07:18:04 +0000301 State state() const { return state_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
wu@webrtc.org91053e72013-08-10 07:18:04 +0000303 protected:
304 ~WebRtcSessionCreateSDPObserverForTest() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305
306 private:
kwibergd1fe2812016-04-27 06:47:29 -0700307 std::unique_ptr<SessionDescriptionInterface> description_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000308 State state_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309};
310
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800311class FakeAudioSource : public cricket::AudioSource {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000312 public:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800313 FakeAudioSource() : sink_(NULL) {}
314 virtual ~FakeAudioSource() {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000315 if (sink_)
316 sink_->OnClose();
317 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000318
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000319 void SetSink(Sink* sink) override { sink_ = sink; }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000320
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800321 const cricket::AudioSource::Sink* sink() const { return sink_; }
322
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000323 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800324 cricket::AudioSource::Sink* sink_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000325};
326
Henrik Boström87713d02015-08-25 09:53:21 +0200327class WebRtcSessionTest
deadbeefab9b2d12015-10-14 11:33:11 -0700328 : public testing::TestWithParam<RTCCertificateGenerationMethod>,
329 public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 protected:
331 // TODO Investigate why ChannelManager crashes, if it's created
332 // after stun_server.
333 WebRtcSessionTest()
stefanc1aeaf02015-10-15 07:26:07 -0700334 : media_engine_(new cricket::FakeMediaEngine()),
335 data_engine_(new cricket::FakeDataEngine()),
336 channel_manager_(
337 new cricket::ChannelManager(media_engine_,
338 data_engine_,
stefanc1aeaf02015-10-15 07:26:07 -0700339 rtc::Thread::Current())),
340 fake_call_(webrtc::Call::Config()),
341 media_controller_(
nisse51542be2016-02-12 02:27:06 -0800342 webrtc::MediaControllerInterface::Create(cricket::MediaConfig(),
343 rtc::Thread::Current(),
stefanc1aeaf02015-10-15 07:26:07 -0700344 channel_manager_.get())),
345 tdesc_factory_(new cricket::TransportDescriptionFactory()),
346 desc_factory_(
347 new cricket::MediaSessionDescriptionFactory(channel_manager_.get(),
348 tdesc_factory_.get())),
349 pss_(new rtc::PhysicalSocketServer),
350 vss_(new rtc::VirtualSocketServer(pss_.get())),
351 fss_(new rtc::FirewallSocketServer(vss_.get())),
352 ss_scope_(fss_.get()),
353 stun_socket_addr_(
354 rtc::SocketAddress(kStunAddrHost, cricket::STUN_SERVER_PORT)),
355 stun_server_(cricket::TestStunServer::Create(Thread::Current(),
356 stun_socket_addr_)),
357 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
358 metrics_observer_(new rtc::RefCountedObject<FakeMetricsObserver>()) {
buildbot@webrtc.org51c55082014-07-28 22:26:15 +0000359 cricket::ServerAddresses stun_servers;
360 stun_servers.insert(stun_socket_addr_);
361 allocator_.reset(new cricket::BasicPortAllocator(
362 &network_manager_,
363 stun_servers,
364 SocketAddress(), SocketAddress(), SocketAddress()));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000365 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700366 cricket::PORTALLOCATOR_DISABLE_RELAY);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 EXPECT_TRUE(channel_manager_->Init());
368 desc_factory_->set_add_legacy_streams(false);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000369 allocator_->set_step_delay(cricket::kMinimumStepDelay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 }
371
372 void AddInterface(const SocketAddress& addr) {
373 network_manager_.AddInterface(addr);
374 }
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700375 void RemoveInterface(const SocketAddress& addr) {
376 network_manager_.RemoveInterface(addr);
377 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378
Henrik Boströmd79599d2016-06-01 13:58:50 +0200379 // If |cert_generator| != null or |rtc_configuration| contains |certificates|
380 // then DTLS will be enabled unless explicitly disabled by |rtc_configuration|
381 // options. When DTLS is enabled a certificate will be used if provided,
382 // otherwise one will be generated using the |cert_generator|.
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000383 void Init(
Henrik Boströmd79599d2016-06-01 13:58:50 +0200384 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 ASSERT_TRUE(session_.get() == NULL);
386 session_.reset(new WebRtcSessionForTest(
stefanc1aeaf02015-10-15 07:26:07 -0700387 media_controller_.get(), rtc::Thread::Current(), rtc::Thread::Current(),
danilchape9021a32016-05-17 01:52:02 -0700388 rtc::Thread::Current(), allocator_.get(), &observer_));
deadbeefab9b2d12015-10-14 11:33:11 -0700389 session_->SignalDataChannelOpenMessage.connect(
390 this, &WebRtcSessionTest::OnDataChannelOpenMessage);
deadbeef057ecf02016-01-20 14:30:43 -0800391 session_->GetOnDestroyedSignal()->connect(
392 this, &WebRtcSessionTest::OnSessionDestroyed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393
394 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
395 observer_.ice_connection_state_);
396 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
397 observer_.ice_gathering_state_);
398
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200399 EXPECT_TRUE(session_->Initialize(options_, std::move(cert_generator),
htaa2a49d92016-03-04 02:51:39 -0800400 configuration_));
jbauchac8869e2015-07-03 01:36:14 -0700401 session_->set_metrics_observer(metrics_observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 }
403
deadbeefab9b2d12015-10-14 11:33:11 -0700404 void OnDataChannelOpenMessage(const std::string& label,
405 const InternalDataChannelInit& config) {
406 last_data_channel_label_ = label;
407 last_data_channel_config_ = config;
408 }
409
deadbeef057ecf02016-01-20 14:30:43 -0800410 void OnSessionDestroyed() { session_destroyed_ = true; }
411
htaa2a49d92016-03-04 02:51:39 -0800412 void Init() { Init(nullptr); }
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000413
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000414 void InitWithBundlePolicy(
415 PeerConnectionInterface::BundlePolicy bundle_policy) {
htaa2a49d92016-03-04 02:51:39 -0800416 configuration_.bundle_policy = bundle_policy;
417 Init();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700418 }
419
420 void InitWithRtcpMuxPolicy(
421 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy) {
422 PeerConnectionInterface::RTCConfiguration configuration;
htaa2a49d92016-03-04 02:51:39 -0800423 configuration_.rtcp_mux_policy = rtcp_mux_policy;
424 Init();
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000425 }
426
Henrik Boström87713d02015-08-25 09:53:21 +0200427 // Successfully init with DTLS; with a certificate generated and supplied or
428 // with a store that generates it for us.
429 void InitWithDtls(RTCCertificateGenerationMethod cert_gen_method) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200430 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator;
Henrik Boström87713d02015-08-25 09:53:21 +0200431 if (cert_gen_method == ALREADY_GENERATED) {
htaa2a49d92016-03-04 02:51:39 -0800432 configuration_.certificates.push_back(
Henrik Boströmd79599d2016-06-01 13:58:50 +0200433 FakeRTCCertificateGenerator::GenerateCertificate());
Henrik Boström87713d02015-08-25 09:53:21 +0200434 } else if (cert_gen_method == DTLS_IDENTITY_STORE) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200435 cert_generator.reset(new FakeRTCCertificateGenerator());
436 cert_generator->set_should_fail(false);
Henrik Boström87713d02015-08-25 09:53:21 +0200437 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700438 RTC_CHECK(false);
Henrik Boström87713d02015-08-25 09:53:21 +0200439 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200440 Init(std::move(cert_generator));
Henrik Boström87713d02015-08-25 09:53:21 +0200441 }
442
443 // Init with DTLS with a store that will fail to generate a certificate.
444 void InitWithDtlsIdentityGenFail() {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200445 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
446 new FakeRTCCertificateGenerator());
447 cert_generator->set_should_fail(true);
448 Init(std::move(cert_generator));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000449 }
450
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 void InitWithDtmfCodec() {
452 // Add kTelephoneEventCodec for dtmf test.
deadbeef67cf2c12016-04-13 10:07:16 -0700453 const cricket::AudioCodec kTelephoneEventCodec(106, "telephone-event", 8000,
454 0, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455 std::vector<cricket::AudioCodec> codecs;
456 codecs.push_back(kTelephoneEventCodec);
457 media_engine_->SetAudioCodecs(codecs);
458 desc_factory_->set_audio_codecs(codecs);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000459 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 }
461
deadbeefab9b2d12015-10-14 11:33:11 -0700462 void SendAudioVideoStream1() {
463 send_stream_1_ = true;
464 send_stream_2_ = false;
465 send_audio_ = true;
466 send_video_ = true;
467 }
468
469 void SendAudioVideoStream2() {
470 send_stream_1_ = false;
471 send_stream_2_ = true;
472 send_audio_ = true;
473 send_video_ = true;
474 }
475
476 void SendAudioVideoStream1And2() {
477 send_stream_1_ = true;
478 send_stream_2_ = true;
479 send_audio_ = true;
480 send_video_ = true;
481 }
482
483 void SendNothing() {
484 send_stream_1_ = false;
485 send_stream_2_ = false;
486 send_audio_ = false;
487 send_video_ = false;
488 }
489
490 void SendAudioOnlyStream2() {
491 send_stream_1_ = false;
492 send_stream_2_ = true;
493 send_audio_ = true;
494 send_video_ = false;
495 }
496
497 void SendVideoOnlyStream2() {
498 send_stream_1_ = false;
499 send_stream_2_ = true;
500 send_audio_ = false;
501 send_video_ = true;
502 }
503
504 void AddStreamsToOptions(cricket::MediaSessionOptions* session_options) {
505 if (send_stream_1_ && send_audio_) {
506 session_options->AddSendStream(cricket::MEDIA_TYPE_AUDIO, kAudioTrack1,
507 kStream1);
508 }
509 if (send_stream_1_ && send_video_) {
510 session_options->AddSendStream(cricket::MEDIA_TYPE_VIDEO, kVideoTrack1,
511 kStream1);
512 }
513 if (send_stream_2_ && send_audio_) {
514 session_options->AddSendStream(cricket::MEDIA_TYPE_AUDIO, kAudioTrack2,
515 kStream2);
516 }
517 if (send_stream_2_ && send_video_) {
518 session_options->AddSendStream(cricket::MEDIA_TYPE_VIDEO, kVideoTrack2,
519 kStream2);
520 }
521 if (data_channel_ && session_->data_channel_type() == cricket::DCT_RTP) {
522 session_options->AddSendStream(cricket::MEDIA_TYPE_DATA,
523 data_channel_->label(),
524 data_channel_->label());
525 }
526 }
527
528 void GetOptionsForOffer(
529 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
530 cricket::MediaSessionOptions* session_options) {
htaaac2dea2016-03-10 13:35:55 -0800531 ASSERT_TRUE(ExtractMediaSessionOptions(rtc_options, true, session_options));
deadbeefab9b2d12015-10-14 11:33:11 -0700532
deadbeefc80741f2015-10-22 13:14:45 -0700533 AddStreamsToOptions(session_options);
534 if (rtc_options.offer_to_receive_audio ==
535 RTCOfferAnswerOptions::kUndefined) {
536 session_options->recv_audio =
537 session_options->HasSendMediaStream(cricket::MEDIA_TYPE_AUDIO);
538 }
539 if (rtc_options.offer_to_receive_video ==
540 RTCOfferAnswerOptions::kUndefined) {
541 session_options->recv_video =
542 session_options->HasSendMediaStream(cricket::MEDIA_TYPE_VIDEO);
543 }
544 session_options->bundle_enabled =
545 session_options->bundle_enabled &&
546 (session_options->has_audio() || session_options->has_video() ||
547 session_options->has_data());
548
deadbeefab9b2d12015-10-14 11:33:11 -0700549 if (session_->data_channel_type() == cricket::DCT_SCTP && data_channel_) {
550 session_options->data_channel_type = cricket::DCT_SCTP;
551 }
552 }
553
htaa2a49d92016-03-04 02:51:39 -0800554 void GetOptionsForAnswer(cricket::MediaSessionOptions* session_options) {
555 // ParseConstraintsForAnswer is used to set some defaults.
556 ASSERT_TRUE(webrtc::ParseConstraintsForAnswer(nullptr, session_options));
deadbeefab9b2d12015-10-14 11:33:11 -0700557
deadbeefc80741f2015-10-22 13:14:45 -0700558 AddStreamsToOptions(session_options);
559 session_options->bundle_enabled =
560 session_options->bundle_enabled &&
561 (session_options->has_audio() || session_options->has_video() ||
562 session_options->has_data());
563
deadbeefab9b2d12015-10-14 11:33:11 -0700564 if (session_->data_channel_type() == cricket::DCT_SCTP) {
565 session_options->data_channel_type = cricket::DCT_SCTP;
566 }
567 }
568
569 // Creates a local offer and applies it. Starts ICE.
570 // Call SendAudioVideoStreamX() before this function
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 // to decide which streams to create.
572 void InitiateCall() {
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000573 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 SetLocalDescriptionWithoutError(offer);
575 EXPECT_TRUE_WAIT(PeerConnectionInterface::kIceGatheringNew !=
576 observer_.ice_gathering_state_,
577 kIceCandidatesTimeout);
578 }
579
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000580 SessionDescriptionInterface* CreateOffer() {
581 PeerConnectionInterface::RTCOfferAnswerOptions options;
582 options.offer_to_receive_audio =
583 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
584
585 return CreateOffer(options);
586 }
587
wu@webrtc.org91053e72013-08-10 07:18:04 +0000588 SessionDescriptionInterface* CreateOffer(
htaa2a49d92016-03-04 02:51:39 -0800589 const PeerConnectionInterface::RTCOfferAnswerOptions options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000590 rtc::scoped_refptr<WebRtcSessionCreateSDPObserverForTest>
wu@webrtc.org91053e72013-08-10 07:18:04 +0000591 observer = new WebRtcSessionCreateSDPObserverForTest();
deadbeefab9b2d12015-10-14 11:33:11 -0700592 cricket::MediaSessionOptions session_options;
593 GetOptionsForOffer(options, &session_options);
594 session_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000595 EXPECT_TRUE_WAIT(
596 observer->state() != WebRtcSessionCreateSDPObserverForTest::kInit,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000597 2000);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000598 return observer->ReleaseDescription();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000599 }
600
601 SessionDescriptionInterface* CreateAnswer(
htaa2a49d92016-03-04 02:51:39 -0800602 const cricket::MediaSessionOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000603 rtc::scoped_refptr<WebRtcSessionCreateSDPObserverForTest> observer
wu@webrtc.org91053e72013-08-10 07:18:04 +0000604 = new WebRtcSessionCreateSDPObserverForTest();
htaa2a49d92016-03-04 02:51:39 -0800605 cricket::MediaSessionOptions session_options = options;
606 GetOptionsForAnswer(&session_options);
607 // Overwrite recv_audio and recv_video with passed-in values.
608 session_options.recv_video = options.recv_video;
609 session_options.recv_audio = options.recv_audio;
610 session_->CreateAnswer(observer, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000611 EXPECT_TRUE_WAIT(
612 observer->state() != WebRtcSessionCreateSDPObserverForTest::kInit,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000613 2000);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000614 return observer->ReleaseDescription();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000615 }
616
htaa2a49d92016-03-04 02:51:39 -0800617 SessionDescriptionInterface* CreateAnswer() {
618 cricket::MediaSessionOptions options;
619 options.recv_video = true;
620 options.recv_audio = true;
621 return CreateAnswer(options);
622 }
623
wu@webrtc.org364f2042013-11-20 21:49:41 +0000624 bool ChannelsExist() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 return (session_->voice_channel() != NULL &&
626 session_->video_channel() != NULL);
627 }
628
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 void VerifyCryptoParams(const cricket::SessionDescription* sdp) {
630 ASSERT_TRUE(session_.get() != NULL);
631 const cricket::ContentInfo* content = cricket::GetFirstAudioContent(sdp);
632 ASSERT_TRUE(content != NULL);
633 const cricket::AudioContentDescription* audio_content =
634 static_cast<const cricket::AudioContentDescription*>(
635 content->description);
636 ASSERT_TRUE(audio_content != NULL);
637 ASSERT_EQ(1U, audio_content->cryptos().size());
638 ASSERT_EQ(47U, audio_content->cryptos()[0].key_params.size());
639 ASSERT_EQ("AES_CM_128_HMAC_SHA1_80",
640 audio_content->cryptos()[0].cipher_suite);
641 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
642 audio_content->protocol());
643
644 content = cricket::GetFirstVideoContent(sdp);
645 ASSERT_TRUE(content != NULL);
646 const cricket::VideoContentDescription* video_content =
647 static_cast<const cricket::VideoContentDescription*>(
648 content->description);
649 ASSERT_TRUE(video_content != NULL);
650 ASSERT_EQ(1U, video_content->cryptos().size());
651 ASSERT_EQ("AES_CM_128_HMAC_SHA1_80",
652 video_content->cryptos()[0].cipher_suite);
653 ASSERT_EQ(47U, video_content->cryptos()[0].key_params.size());
654 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
655 video_content->protocol());
656 }
657
658 void VerifyNoCryptoParams(const cricket::SessionDescription* sdp, bool dtls) {
659 const cricket::ContentInfo* content = cricket::GetFirstAudioContent(sdp);
660 ASSERT_TRUE(content != NULL);
661 const cricket::AudioContentDescription* audio_content =
662 static_cast<const cricket::AudioContentDescription*>(
663 content->description);
664 ASSERT_TRUE(audio_content != NULL);
665 ASSERT_EQ(0U, audio_content->cryptos().size());
666
667 content = cricket::GetFirstVideoContent(sdp);
668 ASSERT_TRUE(content != NULL);
669 const cricket::VideoContentDescription* video_content =
670 static_cast<const cricket::VideoContentDescription*>(
671 content->description);
672 ASSERT_TRUE(video_content != NULL);
673 ASSERT_EQ(0U, video_content->cryptos().size());
674
675 if (dtls) {
deadbeeff3938292015-07-15 12:20:53 -0700676 EXPECT_EQ(std::string(cricket::kMediaProtocolDtlsSavpf),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 audio_content->protocol());
deadbeeff3938292015-07-15 12:20:53 -0700678 EXPECT_EQ(std::string(cricket::kMediaProtocolDtlsSavpf),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 video_content->protocol());
680 } else {
681 EXPECT_EQ(std::string(cricket::kMediaProtocolAvpf),
682 audio_content->protocol());
683 EXPECT_EQ(std::string(cricket::kMediaProtocolAvpf),
684 video_content->protocol());
685 }
686 }
687
688 // Set the internal fake description factories to do DTLS-SRTP.
689 void SetFactoryDtlsSrtp() {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000690 desc_factory_->set_secure(cricket::SEC_DISABLED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 std::string identity_name = "WebRTC" +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000692 rtc::ToString(rtc::CreateRandomId());
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200693 // Confirmed to work with KT_RSA and KT_ECDSA.
kwiberg0eb15ed2015-12-17 03:04:15 -0800694 tdesc_factory_->set_certificate(
jbauch555604a2016-04-26 03:13:22 -0700695 rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>(
kwiberg0eb15ed2015-12-17 03:04:15 -0800696 rtc::SSLIdentity::Generate(identity_name, rtc::KT_DEFAULT))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 tdesc_factory_->set_secure(cricket::SEC_REQUIRED);
698 }
699
700 void VerifyFingerprintStatus(const cricket::SessionDescription* sdp,
701 bool expected) {
702 const TransportInfo* audio = sdp->GetTransportInfoByName("audio");
703 ASSERT_TRUE(audio != NULL);
704 ASSERT_EQ(expected, audio->description.identity_fingerprint.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 const TransportInfo* video = sdp->GetTransportInfoByName("video");
706 ASSERT_TRUE(video != NULL);
707 ASSERT_EQ(expected, video->description.identity_fingerprint.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 }
709
710 void VerifyAnswerFromNonCryptoOffer() {
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +0000711 // Create an SDP without Crypto.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +0000713 options.recv_video = true;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000714 JsepSessionDescription* offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 CreateRemoteOffer(options, cricket::SEC_DISABLED));
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000716 ASSERT_TRUE(offer != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 VerifyNoCryptoParams(offer->description(), false);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000718 SetRemoteDescriptionOfferExpectError(kSdpWithoutSdesCrypto,
719 offer);
htaa2a49d92016-03-04 02:51:39 -0800720 const webrtc::SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 // Answer should be NULL as no crypto params in offer.
722 ASSERT_TRUE(answer == NULL);
723 }
724
725 void VerifyAnswerFromCryptoOffer() {
726 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +0000727 options.recv_video = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 options.bundle_enabled = true;
kwibergd1fe2812016-04-27 06:47:29 -0700729 std::unique_ptr<JsepSessionDescription> offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 CreateRemoteOffer(options, cricket::SEC_REQUIRED));
731 ASSERT_TRUE(offer.get() != NULL);
732 VerifyCryptoParams(offer->description());
733 SetRemoteDescriptionWithoutError(offer.release());
kwibergd1fe2812016-04-27 06:47:29 -0700734 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 ASSERT_TRUE(answer.get() != NULL);
736 VerifyCryptoParams(answer->description());
737 }
738
deadbeef0ed85b22016-02-23 17:24:52 -0800739 bool IceUfragPwdEqual(const cricket::SessionDescription* desc1,
740 const cricket::SessionDescription* desc2) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 if (desc1->contents().size() != desc2->contents().size()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800742 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 }
744
745 const cricket::ContentInfos& contents = desc1->contents();
746 cricket::ContentInfos::const_iterator it = contents.begin();
747
748 for (; it != contents.end(); ++it) {
749 const cricket::TransportDescription* transport_desc1 =
750 desc1->GetTransportDescriptionByName(it->name);
751 const cricket::TransportDescription* transport_desc2 =
752 desc2->GetTransportDescriptionByName(it->name);
753 if (!transport_desc1 || !transport_desc2) {
deadbeef0ed85b22016-02-23 17:24:52 -0800754 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 }
756 if (transport_desc1->ice_pwd != transport_desc2->ice_pwd ||
757 transport_desc1->ice_ufrag != transport_desc2->ice_ufrag) {
deadbeef0ed85b22016-02-23 17:24:52 -0800758 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 }
760 }
deadbeef0ed85b22016-02-23 17:24:52 -0800761 return true;
762 }
763
764 // Compares ufrag/password only for the specified |media_type|.
765 bool IceUfragPwdEqual(const cricket::SessionDescription* desc1,
766 const cricket::SessionDescription* desc2,
767 cricket::MediaType media_type) {
768 if (desc1->contents().size() != desc2->contents().size()) {
769 return false;
770 }
771
772 const cricket::ContentInfo* cinfo =
773 cricket::GetFirstMediaContent(desc1->contents(), media_type);
774 const cricket::TransportDescription* transport_desc1 =
775 desc1->GetTransportDescriptionByName(cinfo->name);
776 const cricket::TransportDescription* transport_desc2 =
777 desc2->GetTransportDescriptionByName(cinfo->name);
778 if (!transport_desc1 || !transport_desc2) {
779 return false;
780 }
781 if (transport_desc1->ice_pwd != transport_desc2->ice_pwd ||
782 transport_desc1->ice_ufrag != transport_desc2->ice_ufrag) {
783 return false;
784 }
785 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000787
788 void RemoveIceUfragPwdLines(const SessionDescriptionInterface* current_desc,
789 std::string *sdp) {
790 const cricket::SessionDescription* desc = current_desc->description();
791 EXPECT_TRUE(current_desc->ToString(sdp));
792
793 const cricket::ContentInfos& contents = desc->contents();
794 cricket::ContentInfos::const_iterator it = contents.begin();
795 // Replace ufrag and pwd lines with empty strings.
796 for (; it != contents.end(); ++it) {
797 const cricket::TransportDescription* transport_desc =
798 desc->GetTransportDescriptionByName(it->name);
799 std::string ufrag_line = "a=ice-ufrag:" + transport_desc->ice_ufrag
800 + "\r\n";
801 std::string pwd_line = "a=ice-pwd:" + transport_desc->ice_pwd
802 + "\r\n";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000803 rtc::replace_substrs(ufrag_line.c_str(), ufrag_line.length(),
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000804 "", 0,
805 sdp);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000806 rtc::replace_substrs(pwd_line.c_str(), pwd_line.length(),
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000807 "", 0,
808 sdp);
809 }
810 }
811
deadbeef0ed85b22016-02-23 17:24:52 -0800812 void SetIceUfragPwd(SessionDescriptionInterface* current_desc,
813 const std::string& ufrag,
814 const std::string& pwd) {
815 cricket::SessionDescription* desc = current_desc->description();
816 for (TransportInfo& transport_info : desc->transport_infos()) {
817 cricket::TransportDescription& transport_desc =
818 transport_info.description;
819 transport_desc.ice_ufrag = ufrag;
820 transport_desc.ice_pwd = pwd;
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +0000821 }
822 }
823
deadbeef0ed85b22016-02-23 17:24:52 -0800824 // Sets ufrag/pwd for specified |media_type|.
825 void SetIceUfragPwd(SessionDescriptionInterface* current_desc,
826 cricket::MediaType media_type,
827 const std::string& ufrag,
828 const std::string& pwd) {
829 cricket::SessionDescription* desc = current_desc->description();
830 const cricket::ContentInfo* cinfo =
831 cricket::GetFirstMediaContent(desc->contents(), media_type);
832 TransportInfo* transport_info = desc->GetTransportInfoByName(cinfo->name);
833 cricket::TransportDescription* transport_desc =
834 &transport_info->description;
835 transport_desc->ice_ufrag = ufrag;
836 transport_desc->ice_pwd = pwd;
837 }
838
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 // Creates a remote offer and and applies it as a remote description,
840 // creates a local answer and applies is as a local description.
deadbeefab9b2d12015-10-14 11:33:11 -0700841 // Call SendAudioVideoStreamX() before this function
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 // to decide which local and remote streams to create.
843 void CreateAndSetRemoteOfferAndLocalAnswer() {
844 SessionDescriptionInterface* offer = CreateRemoteOffer();
845 SetRemoteDescriptionWithoutError(offer);
htaa2a49d92016-03-04 02:51:39 -0800846 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 SetLocalDescriptionWithoutError(answer);
848 }
849 void SetLocalDescriptionWithoutError(SessionDescriptionInterface* desc) {
850 EXPECT_TRUE(session_->SetLocalDescription(desc, NULL));
deadbeefcbecd352015-09-23 11:50:27 -0700851 session_->MaybeStartGathering();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 }
853 void SetLocalDescriptionExpectState(SessionDescriptionInterface* desc,
deadbeefd59daf82015-10-14 15:02:44 -0700854 WebRtcSession::State expected_state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 SetLocalDescriptionWithoutError(desc);
856 EXPECT_EQ(expected_state, session_->state());
857 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000858 void SetLocalDescriptionExpectError(const std::string& action,
859 const std::string& expected_error,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 SessionDescriptionInterface* desc) {
861 std::string error;
862 EXPECT_FALSE(session_->SetLocalDescription(desc, &error));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000863 std::string sdp_type = "local ";
864 sdp_type.append(action);
865 EXPECT_NE(std::string::npos, error.find(sdp_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 EXPECT_NE(std::string::npos, error.find(expected_error));
867 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000868 void SetLocalDescriptionOfferExpectError(const std::string& expected_error,
869 SessionDescriptionInterface* desc) {
870 SetLocalDescriptionExpectError(SessionDescriptionInterface::kOffer,
871 expected_error, desc);
872 }
873 void SetLocalDescriptionAnswerExpectError(const std::string& expected_error,
874 SessionDescriptionInterface* desc) {
875 SetLocalDescriptionExpectError(SessionDescriptionInterface::kAnswer,
876 expected_error, desc);
877 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 void SetRemoteDescriptionWithoutError(SessionDescriptionInterface* desc) {
879 EXPECT_TRUE(session_->SetRemoteDescription(desc, NULL));
880 }
881 void SetRemoteDescriptionExpectState(SessionDescriptionInterface* desc,
deadbeefd59daf82015-10-14 15:02:44 -0700882 WebRtcSession::State expected_state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 SetRemoteDescriptionWithoutError(desc);
884 EXPECT_EQ(expected_state, session_->state());
885 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000886 void SetRemoteDescriptionExpectError(const std::string& action,
887 const std::string& expected_error,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 SessionDescriptionInterface* desc) {
889 std::string error;
890 EXPECT_FALSE(session_->SetRemoteDescription(desc, &error));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000891 std::string sdp_type = "remote ";
892 sdp_type.append(action);
893 EXPECT_NE(std::string::npos, error.find(sdp_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 EXPECT_NE(std::string::npos, error.find(expected_error));
895 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000896 void SetRemoteDescriptionOfferExpectError(
897 const std::string& expected_error, SessionDescriptionInterface* desc) {
898 SetRemoteDescriptionExpectError(SessionDescriptionInterface::kOffer,
899 expected_error, desc);
900 }
901 void SetRemoteDescriptionPranswerExpectError(
902 const std::string& expected_error, SessionDescriptionInterface* desc) {
903 SetRemoteDescriptionExpectError(SessionDescriptionInterface::kPrAnswer,
904 expected_error, desc);
905 }
906 void SetRemoteDescriptionAnswerExpectError(
907 const std::string& expected_error, SessionDescriptionInterface* desc) {
908 SetRemoteDescriptionExpectError(SessionDescriptionInterface::kAnswer,
909 expected_error, desc);
910 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911
912 void CreateCryptoOfferAndNonCryptoAnswer(SessionDescriptionInterface** offer,
913 SessionDescriptionInterface** nocrypto_answer) {
914 // Create a SDP without Crypto.
915 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +0000916 options.recv_video = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 options.bundle_enabled = true;
918 *offer = CreateRemoteOffer(options, cricket::SEC_ENABLED);
919 ASSERT_TRUE(*offer != NULL);
920 VerifyCryptoParams((*offer)->description());
921
922 *nocrypto_answer = CreateRemoteAnswer(*offer, options,
923 cricket::SEC_DISABLED);
924 EXPECT_TRUE(*nocrypto_answer != NULL);
925 }
926
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000927 void CreateDtlsOfferAndNonDtlsAnswer(SessionDescriptionInterface** offer,
928 SessionDescriptionInterface** nodtls_answer) {
929 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +0000930 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000931 options.bundle_enabled = true;
932
kwibergd1fe2812016-04-27 06:47:29 -0700933 std::unique_ptr<SessionDescriptionInterface> temp_offer(
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000934 CreateRemoteOffer(options, cricket::SEC_ENABLED));
935
936 *nodtls_answer =
937 CreateRemoteAnswer(temp_offer.get(), options, cricket::SEC_ENABLED);
938 EXPECT_TRUE(*nodtls_answer != NULL);
939 VerifyFingerprintStatus((*nodtls_answer)->description(), false);
940 VerifyCryptoParams((*nodtls_answer)->description());
941
942 SetFactoryDtlsSrtp();
943 *offer = CreateRemoteOffer(options, cricket::SEC_ENABLED);
944 ASSERT_TRUE(*offer != NULL);
945 VerifyFingerprintStatus((*offer)->description(), true);
946 VerifyCryptoParams((*offer)->description());
947 }
948
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 JsepSessionDescription* CreateRemoteOfferWithVersion(
950 cricket::MediaSessionOptions options,
951 cricket::SecurePolicy secure_policy,
952 const std::string& session_version,
953 const SessionDescriptionInterface* current_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000954 std::string session_id = rtc::ToString(rtc::CreateRandomId64());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 const cricket::SessionDescription* cricket_desc = NULL;
956 if (current_desc) {
957 cricket_desc = current_desc->description();
958 session_id = current_desc->session_id();
959 }
960
961 desc_factory_->set_secure(secure_policy);
962 JsepSessionDescription* offer(
963 new JsepSessionDescription(JsepSessionDescription::kOffer));
964 if (!offer->Initialize(desc_factory_->CreateOffer(options, cricket_desc),
965 session_id, session_version)) {
966 delete offer;
967 offer = NULL;
968 }
969 return offer;
970 }
971 JsepSessionDescription* CreateRemoteOffer(
972 cricket::MediaSessionOptions options) {
973 return CreateRemoteOfferWithVersion(options, cricket::SEC_ENABLED,
974 kSessionVersion, NULL);
975 }
976 JsepSessionDescription* CreateRemoteOffer(
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000977 cricket::MediaSessionOptions options, cricket::SecurePolicy sdes_policy) {
978 return CreateRemoteOfferWithVersion(
979 options, sdes_policy, kSessionVersion, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 }
981 JsepSessionDescription* CreateRemoteOffer(
982 cricket::MediaSessionOptions options,
983 const SessionDescriptionInterface* current_desc) {
984 return CreateRemoteOfferWithVersion(options, cricket::SEC_ENABLED,
985 kSessionVersion, current_desc);
986 }
987
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000988 JsepSessionDescription* CreateRemoteOfferWithSctpPort(
989 const char* sctp_stream_name, int new_port,
990 cricket::MediaSessionOptions options) {
991 options.data_channel_type = cricket::DCT_SCTP;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +0000992 options.AddSendStream(cricket::MEDIA_TYPE_DATA, "datachannel",
993 sctp_stream_name);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000994 return ChangeSDPSctpPort(new_port, CreateRemoteOffer(options));
995 }
996
997 // Takes ownership of offer_basis (and deletes it).
998 JsepSessionDescription* ChangeSDPSctpPort(
999 int new_port, webrtc::SessionDescriptionInterface *offer_basis) {
1000 // Stringify the input SDP, swap the 5000 for 'new_port' and create a new
1001 // SessionDescription from the mutated string.
1002 const char* default_port_str = "5000";
1003 char new_port_str[16];
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001004 rtc::sprintfn(new_port_str, sizeof(new_port_str), "%d", new_port);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001005 std::string offer_str;
1006 offer_basis->ToString(&offer_str);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001007 rtc::replace_substrs(default_port_str, strlen(default_port_str),
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001008 new_port_str, strlen(new_port_str),
1009 &offer_str);
1010 JsepSessionDescription* offer = new JsepSessionDescription(
1011 offer_basis->type());
1012 delete offer_basis;
1013 offer->Initialize(offer_str, NULL);
1014 return offer;
1015 }
1016
deadbeefab9b2d12015-10-14 11:33:11 -07001017 // Create a remote offer. Call SendAudioVideoStreamX()
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 // before this function to decide which streams to create.
1019 JsepSessionDescription* CreateRemoteOffer() {
1020 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -08001021 GetOptionsForAnswer(&options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022 return CreateRemoteOffer(options, session_->remote_description());
1023 }
1024
1025 JsepSessionDescription* CreateRemoteAnswer(
1026 const SessionDescriptionInterface* offer,
1027 cricket::MediaSessionOptions options,
1028 cricket::SecurePolicy policy) {
1029 desc_factory_->set_secure(policy);
1030 const std::string session_id =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001031 rtc::ToString(rtc::CreateRandomId64());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032 JsepSessionDescription* answer(
1033 new JsepSessionDescription(JsepSessionDescription::kAnswer));
1034 if (!answer->Initialize(desc_factory_->CreateAnswer(offer->description(),
1035 options, NULL),
1036 session_id, kSessionVersion)) {
1037 delete answer;
1038 answer = NULL;
1039 }
1040 return answer;
1041 }
1042
1043 JsepSessionDescription* CreateRemoteAnswer(
1044 const SessionDescriptionInterface* offer,
1045 cricket::MediaSessionOptions options) {
1046 return CreateRemoteAnswer(offer, options, cricket::SEC_REQUIRED);
1047 }
1048
deadbeefab9b2d12015-10-14 11:33:11 -07001049 // Creates an answer session description.
1050 // Call SendAudioVideoStreamX() before this function
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 // to decide which streams to create.
1052 JsepSessionDescription* CreateRemoteAnswer(
1053 const SessionDescriptionInterface* offer) {
1054 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -08001055 GetOptionsForAnswer(&options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 return CreateRemoteAnswer(offer, options, cricket::SEC_REQUIRED);
1057 }
1058
1059 void TestSessionCandidatesWithBundleRtcpMux(bool bundle, bool rtcp_mux) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001060 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001061 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001062 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001063
1064 PeerConnectionInterface::RTCOfferAnswerOptions options;
1065 options.use_rtp_mux = bundle;
1066
1067 SessionDescriptionInterface* offer = CreateOffer(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
1069 // and answer.
1070 SetLocalDescriptionWithoutError(offer);
1071
kwibergd1fe2812016-04-27 06:47:29 -07001072 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org723d6832013-07-12 16:04:50 +00001073 CreateRemoteAnswer(session_->local_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 std::string sdp;
1075 EXPECT_TRUE(answer->ToString(&sdp));
1076
1077 size_t expected_candidate_num = 2;
1078 if (!rtcp_mux) {
1079 // If rtcp_mux is enabled we should expect 4 candidates - host and srflex
1080 // for rtp and rtcp.
1081 expected_candidate_num = 4;
1082 // Disable rtcp-mux from the answer
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 const std::string kRtcpMux = "a=rtcp-mux";
1084 const std::string kXRtcpMux = "a=xrtcp-mux";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001085 rtc::replace_substrs(kRtcpMux.c_str(), kRtcpMux.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 kXRtcpMux.c_str(), kXRtcpMux.length(),
1087 &sdp);
1088 }
1089
1090 SessionDescriptionInterface* new_answer = CreateSessionDescription(
1091 JsepSessionDescription::kAnswer, sdp, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092
1093 // SetRemoteDescription to enable rtcp mux.
henrike@webrtc.org723d6832013-07-12 16:04:50 +00001094 SetRemoteDescriptionWithoutError(new_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1096 EXPECT_EQ(expected_candidate_num, observer_.mline_0_candidates_.size());
deadbeefcbecd352015-09-23 11:50:27 -07001097 if (bundle) {
1098 EXPECT_EQ(0, observer_.mline_1_candidates_.size());
1099 } else {
1100 EXPECT_EQ(expected_candidate_num, observer_.mline_1_candidates_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 }
1102 }
1103 // Tests that we can only send DTMF when the dtmf codec is supported.
1104 void TestCanInsertDtmf(bool can) {
1105 if (can) {
1106 InitWithDtmfCodec();
1107 } else {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001108 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 }
deadbeefab9b2d12015-10-14 11:33:11 -07001110 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 CreateAndSetRemoteOfferAndLocalAnswer();
1112 EXPECT_FALSE(session_->CanInsertDtmf(""));
1113 EXPECT_EQ(can, session_->CanInsertDtmf(kAudioTrack1));
1114 }
1115
zhihuang3a334652016-05-05 18:37:49 -07001116 bool ContainsVideoCodecWithName(const SessionDescriptionInterface* desc,
1117 const std::string& codec_name) {
1118 for (const auto& content : desc->description()->contents()) {
1119 if (static_cast<cricket::MediaContentDescription*>(content.description)
1120 ->type() == cricket::MEDIA_TYPE_VIDEO) {
1121 const auto* mdesc =
1122 static_cast<cricket::VideoContentDescription*>(content.description);
1123 for (const auto& codec : mdesc->codecs()) {
1124 if (codec.name == codec_name) {
1125 return true;
1126 }
1127 }
1128 }
1129 }
1130 return false;
1131 }
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001132 // Helper class to configure loopback network and verify Best
1133 // Connection using right IP protocol for TestLoopbackCall
1134 // method. LoopbackNetworkManager applies firewall rules to block
1135 // all ping traffic once ICE completed, and remove them to observe
1136 // ICE reconnected again. This LoopbackNetworkConfiguration struct
1137 // verifies the best connection is using the right IP protocol after
1138 // initial ICE convergences.
1139
1140 class LoopbackNetworkConfiguration {
deadbeefcbecd352015-09-23 11:50:27 -07001141 public:
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001142 LoopbackNetworkConfiguration()
1143 : test_ipv6_network_(false),
1144 test_extra_ipv4_network_(false),
1145 best_connection_after_initial_ice_converged_(1, 0) {}
1146
1147 // Used to track the expected best connection count in each IP protocol.
1148 struct ExpectedBestConnection {
1149 ExpectedBestConnection(int ipv4_count, int ipv6_count)
1150 : ipv4_count_(ipv4_count),
1151 ipv6_count_(ipv6_count) {}
1152
1153 int ipv4_count_;
1154 int ipv6_count_;
1155 };
1156
1157 bool test_ipv6_network_;
1158 bool test_extra_ipv4_network_;
1159 ExpectedBestConnection best_connection_after_initial_ice_converged_;
1160
1161 void VerifyBestConnectionAfterIceConverge(
jbauchac8869e2015-07-03 01:36:14 -07001162 const rtc::scoped_refptr<FakeMetricsObserver> metrics_observer) const {
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001163 Verify(metrics_observer, best_connection_after_initial_ice_converged_);
1164 }
1165
1166 private:
jbauchac8869e2015-07-03 01:36:14 -07001167 void Verify(const rtc::scoped_refptr<FakeMetricsObserver> metrics_observer,
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001168 const ExpectedBestConnection& expected) const {
1169 EXPECT_EQ(
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001170 metrics_observer->GetEnumCounter(webrtc::kEnumCounterAddressFamily,
1171 webrtc::kBestConnections_IPv4),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001172 expected.ipv4_count_);
1173 EXPECT_EQ(
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001174 metrics_observer->GetEnumCounter(webrtc::kEnumCounterAddressFamily,
1175 webrtc::kBestConnections_IPv6),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001176 expected.ipv6_count_);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001177 // This is used in the loopback call so there is only single host to host
1178 // candidate pair.
1179 EXPECT_EQ(metrics_observer->GetEnumCounter(
1180 webrtc::kEnumCounterIceCandidatePairTypeUdp,
1181 webrtc::kIceCandidatePairHostHost),
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -07001182 0);
1183 EXPECT_EQ(metrics_observer->GetEnumCounter(
1184 webrtc::kEnumCounterIceCandidatePairTypeUdp,
1185 webrtc::kIceCandidatePairHostPublicHostPublic),
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001186 1);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001187 }
1188 };
1189
1190 class LoopbackNetworkManager {
1191 public:
1192 LoopbackNetworkManager(WebRtcSessionTest* session,
1193 const LoopbackNetworkConfiguration& config)
1194 : config_(config) {
1195 session->AddInterface(
1196 rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
1197 if (config_.test_extra_ipv4_network_) {
1198 session->AddInterface(
1199 rtc::SocketAddress(kClientAddrHost2, kClientAddrPort));
1200 }
1201 if (config_.test_ipv6_network_) {
1202 session->AddInterface(
1203 rtc::SocketAddress(kClientIPv6AddrHost1, kClientAddrPort));
1204 }
1205 }
1206
1207 void ApplyFirewallRules(rtc::FirewallSocketServer* fss) {
1208 fss->AddRule(false, rtc::FP_ANY, rtc::FD_ANY,
1209 rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
1210 if (config_.test_extra_ipv4_network_) {
1211 fss->AddRule(false, rtc::FP_ANY, rtc::FD_ANY,
1212 rtc::SocketAddress(kClientAddrHost2, kClientAddrPort));
1213 }
1214 if (config_.test_ipv6_network_) {
1215 fss->AddRule(false, rtc::FP_ANY, rtc::FD_ANY,
1216 rtc::SocketAddress(kClientIPv6AddrHost1, kClientAddrPort));
1217 }
1218 }
1219
1220 void ClearRules(rtc::FirewallSocketServer* fss) { fss->ClearRules(); }
1221
1222 private:
1223 LoopbackNetworkConfiguration config_;
1224 };
1225
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 // The method sets up a call from the session to itself, in a loopback
1227 // arrangement. It also uses a firewall rule to create a temporary
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001228 // disconnection, and then a permanent disconnection.
1229 // This code is placed in a method so that it can be invoked
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 // by multiple tests with different allocators (e.g. with and without BUNDLE).
1231 // While running the call, this method also checks if the session goes through
1232 // the correct sequence of ICE states when a connection is established,
1233 // broken, and re-established.
1234 // The Connection state should go:
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001235 // New -> Checking -> (Connected) -> Completed -> Disconnected -> Completed
1236 // -> Failed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 // The Gathering state should go: New -> Gathering -> Completed.
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001238
stefanc1aeaf02015-10-15 07:26:07 -07001239 void SetupLoopbackCall() {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001240 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001241 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001242 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243
1244 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
1245 observer_.ice_gathering_state_);
1246 SetLocalDescriptionWithoutError(offer);
1247 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
1248 observer_.ice_connection_state_);
1249 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringGathering,
stefanc1aeaf02015-10-15 07:26:07 -07001250 observer_.ice_gathering_state_, kIceCandidatesTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1252 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
stefanc1aeaf02015-10-15 07:26:07 -07001253 observer_.ice_gathering_state_, kIceCandidatesTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254
1255 std::string sdp;
1256 offer->ToString(&sdp);
stefanc1aeaf02015-10-15 07:26:07 -07001257 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
1258 JsepSessionDescription::kAnswer, sdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 ASSERT_TRUE(desc != NULL);
1260 SetRemoteDescriptionWithoutError(desc);
1261
1262 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionChecking,
stefanc1aeaf02015-10-15 07:26:07 -07001263 observer_.ice_connection_state_, kIceCandidatesTimeout);
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001264
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001265 // The ice connection state is "Connected" too briefly to catch in a test.
1266 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
stefanc1aeaf02015-10-15 07:26:07 -07001267 observer_.ice_connection_state_, kIceCandidatesTimeout);
1268 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269
stefanc1aeaf02015-10-15 07:26:07 -07001270 void TestLoopbackCall(const LoopbackNetworkConfiguration& config) {
1271 LoopbackNetworkManager loopback_network_manager(this, config);
1272 SetupLoopbackCall();
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001273 config.VerifyBestConnectionAfterIceConverge(metrics_observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 // Adding firewall rule to block ping requests, which should cause
1275 // transport channel failure.
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001276
1277 loopback_network_manager.ApplyFirewallRules(fss_.get());
1278
1279 LOG(LS_INFO) << "Firewall Rules applied";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
1281 observer_.ice_connection_state_,
1282 kIceCandidatesTimeout);
1283
jbauchac8869e2015-07-03 01:36:14 -07001284 metrics_observer_->Reset();
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001285
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 // Clearing the rules, session should move back to completed state.
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001287 loopback_network_manager.ClearRules(fss_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001289 LOG(LS_INFO) << "Firewall Rules cleared";
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001290 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 observer_.ice_connection_state_,
1292 kIceCandidatesTimeout);
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001293
1294 // Now we block ping requests and wait until the ICE connection transitions
1295 // to the Failed state. This will take at least 30 seconds because it must
1296 // wait for the Port to timeout.
1297 int port_timeout = 30000;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001298
1299 loopback_network_manager.ApplyFirewallRules(fss_.get());
1300 LOG(LS_INFO) << "Firewall Rules applied again";
jlmiller@webrtc.org804eb462015-02-20 02:20:03 +00001301 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001302 observer_.ice_connection_state_,
1303 kIceCandidatesTimeout + port_timeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 }
1305
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001306 void TestLoopbackCall() {
1307 LoopbackNetworkConfiguration config;
1308 TestLoopbackCall(config);
1309 }
1310
stefanc1aeaf02015-10-15 07:26:07 -07001311 void TestPacketOptions() {
1312 media_controller_.reset(
1313 new cricket::FakeMediaController(channel_manager_.get(), &fake_call_));
1314 LoopbackNetworkConfiguration config;
1315 LoopbackNetworkManager loopback_network_manager(this, config);
1316
1317 SetupLoopbackCall();
1318
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001319 // Wait for channel to be ready for sending.
1320 EXPECT_TRUE_WAIT(media_engine_->GetVideoChannel(0)->sending(), 100);
stefanc1aeaf02015-10-15 07:26:07 -07001321 uint8_t test_packet[15] = {0};
1322 rtc::PacketOptions options;
1323 options.packet_id = 10;
1324 media_engine_->GetVideoChannel(0)
1325 ->SendRtp(test_packet, sizeof(test_packet), options);
1326
1327 const int kPacketTimeout = 2000;
1328 EXPECT_EQ_WAIT(fake_call_.last_sent_packet().packet_id, 10, kPacketTimeout);
1329 EXPECT_GT(fake_call_.last_sent_packet().send_time_ms, -1);
1330 }
1331
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 // Adds CN codecs to FakeMediaEngine and MediaDescriptionFactory.
1333 void AddCNCodecs() {
deadbeef67cf2c12016-04-13 10:07:16 -07001334 const cricket::AudioCodec kCNCodec1(102, "CN", 8000, 0, 1);
1335 const cricket::AudioCodec kCNCodec2(103, "CN", 16000, 0, 1);
wu@webrtc.org364f2042013-11-20 21:49:41 +00001336
1337 // Add kCNCodec for dtmf test.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338 std::vector<cricket::AudioCodec> codecs = media_engine_->audio_codecs();;
1339 codecs.push_back(kCNCodec1);
1340 codecs.push_back(kCNCodec2);
1341 media_engine_->SetAudioCodecs(codecs);
1342 desc_factory_->set_audio_codecs(codecs);
1343 }
1344
1345 bool VerifyNoCNCodecs(const cricket::ContentInfo* content) {
1346 const cricket::ContentDescription* description = content->description;
1347 ASSERT(description != NULL);
1348 const cricket::AudioContentDescription* audio_content_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001349 static_cast<const cricket::AudioContentDescription*>(description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 ASSERT(audio_content_desc != NULL);
1351 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1352 if (audio_content_desc->codecs()[i].name == "CN")
1353 return false;
1354 }
1355 return true;
1356 }
1357
deadbeefab9b2d12015-10-14 11:33:11 -07001358 void CreateDataChannel() {
deadbeeffc648b62015-10-13 16:42:33 -07001359 webrtc::InternalDataChannelInit dci;
htaa2a49d92016-03-04 02:51:39 -08001360 ASSERT(session_.get());
deadbeefab9b2d12015-10-14 11:33:11 -07001361 dci.reliable = session_->data_channel_type() == cricket::DCT_SCTP;
1362 data_channel_ = DataChannel::Create(
1363 session_.get(), session_->data_channel_type(), "datachannel", dci);
1364 }
1365
1366 void SetLocalDescriptionWithDataChannel() {
1367 CreateDataChannel();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001368 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 SetLocalDescriptionWithoutError(offer);
1370 }
1371
wu@webrtc.org91053e72013-08-10 07:18:04 +00001372 void VerifyMultipleAsyncCreateDescription(
Henrik Boström87713d02015-08-25 09:53:21 +02001373 RTCCertificateGenerationMethod cert_gen_method,
1374 CreateSessionDescriptionRequest::Type type) {
1375 InitWithDtls(cert_gen_method);
1376 VerifyMultipleAsyncCreateDescriptionAfterInit(true, type);
1377 }
1378
1379 void VerifyMultipleAsyncCreateDescriptionIdentityGenFailure(
1380 CreateSessionDescriptionRequest::Type type) {
1381 InitWithDtlsIdentityGenFail();
1382 VerifyMultipleAsyncCreateDescriptionAfterInit(false, type);
1383 }
1384
1385 void VerifyMultipleAsyncCreateDescriptionAfterInit(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001386 bool success, CreateSessionDescriptionRequest::Type type) {
henrikg91d6ede2015-09-17 00:24:34 -07001387 RTC_CHECK(session_);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001388 SetFactoryDtlsSrtp();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001389 if (type == CreateSessionDescriptionRequest::kAnswer) {
1390 cricket::MediaSessionOptions options;
kwibergd1fe2812016-04-27 06:47:29 -07001391 std::unique_ptr<JsepSessionDescription> offer(
1392 CreateRemoteOffer(options, cricket::SEC_DISABLED));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001393 ASSERT_TRUE(offer.get() != NULL);
1394 SetRemoteDescriptionWithoutError(offer.release());
1395 }
1396
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001397 PeerConnectionInterface::RTCOfferAnswerOptions options;
deadbeefab9b2d12015-10-14 11:33:11 -07001398 cricket::MediaSessionOptions session_options;
wu@webrtc.org91053e72013-08-10 07:18:04 +00001399 const int kNumber = 3;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001400 rtc::scoped_refptr<WebRtcSessionCreateSDPObserverForTest>
wu@webrtc.org91053e72013-08-10 07:18:04 +00001401 observers[kNumber];
1402 for (int i = 0; i < kNumber; ++i) {
1403 observers[i] = new WebRtcSessionCreateSDPObserverForTest();
1404 if (type == CreateSessionDescriptionRequest::kOffer) {
deadbeefab9b2d12015-10-14 11:33:11 -07001405 session_->CreateOffer(observers[i], options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001406 } else {
htaa2a49d92016-03-04 02:51:39 -08001407 session_->CreateAnswer(observers[i], session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001408 }
1409 }
1410
1411 WebRtcSessionCreateSDPObserverForTest::State expected_state =
1412 success ? WebRtcSessionCreateSDPObserverForTest::kSucceeded :
1413 WebRtcSessionCreateSDPObserverForTest::kFailed;
1414
1415 for (int i = 0; i < kNumber; ++i) {
1416 EXPECT_EQ_WAIT(expected_state, observers[i]->state(), 1000);
1417 if (success) {
1418 EXPECT_TRUE(observers[i]->description() != NULL);
1419 } else {
1420 EXPECT_TRUE(observers[i]->description() == NULL);
1421 }
1422 }
1423 }
1424
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001425 void ConfigureAllocatorWithTurn() {
deadbeef653b8e02015-11-11 12:55:10 -08001426 cricket::RelayServerConfig turn_server(cricket::RELAY_TURN);
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001427 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001428 turn_server.credentials = credentials;
1429 turn_server.ports.push_back(
1430 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
1431 allocator_->AddTurnServer(turn_server);
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001432 allocator_->set_step_delay(cricket::kMinimumStepDelay);
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001433 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP);
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001434 }
1435
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436 cricket::FakeMediaEngine* media_engine_;
1437 cricket::FakeDataEngine* data_engine_;
kwibergd1fe2812016-04-27 06:47:29 -07001438 std::unique_ptr<cricket::ChannelManager> channel_manager_;
stefanc1aeaf02015-10-15 07:26:07 -07001439 cricket::FakeCall fake_call_;
kwibergd1fe2812016-04-27 06:47:29 -07001440 std::unique_ptr<webrtc::MediaControllerInterface> media_controller_;
1441 std::unique_ptr<cricket::TransportDescriptionFactory> tdesc_factory_;
1442 std::unique_ptr<cricket::MediaSessionDescriptionFactory> desc_factory_;
1443 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
1444 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1445 std::unique_ptr<rtc::FirewallSocketServer> fss_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001446 rtc::SocketServerScope ss_scope_;
1447 rtc::SocketAddress stun_socket_addr_;
kwibergd1fe2812016-04-27 06:47:29 -07001448 std::unique_ptr<cricket::TestStunServer> stun_server_;
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001449 cricket::TestTurnServer turn_server_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001450 rtc::FakeNetworkManager network_manager_;
kwibergd1fe2812016-04-27 06:47:29 -07001451 std::unique_ptr<cricket::BasicPortAllocator> allocator_;
wu@webrtc.org97077a32013-10-25 21:18:33 +00001452 PeerConnectionFactoryInterface::Options options_;
htaa2a49d92016-03-04 02:51:39 -08001453 PeerConnectionInterface::RTCConfiguration configuration_;
kwibergd1fe2812016-04-27 06:47:29 -07001454 std::unique_ptr<WebRtcSessionForTest> session_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 MockIceObserver observer_;
1456 cricket::FakeVideoMediaChannel* video_channel_;
1457 cricket::FakeVoiceMediaChannel* voice_channel_;
jbauchac8869e2015-07-03 01:36:14 -07001458 rtc::scoped_refptr<FakeMetricsObserver> metrics_observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001459 // The following flags affect options created for CreateOffer/CreateAnswer.
1460 bool send_stream_1_ = false;
1461 bool send_stream_2_ = false;
1462 bool send_audio_ = false;
1463 bool send_video_ = false;
1464 rtc::scoped_refptr<DataChannel> data_channel_;
1465 // Last values received from data channel creation signal.
1466 std::string last_data_channel_label_;
1467 InternalDataChannelInit last_data_channel_config_;
deadbeef8947a012016-01-21 10:26:38 -08001468 bool session_destroyed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469};
1470
Henrik Boström87713d02015-08-25 09:53:21 +02001471TEST_P(WebRtcSessionTest, TestInitializeWithDtls) {
1472 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001473 // SDES is disabled when DTLS is on.
1474 EXPECT_EQ(cricket::SEC_DISABLED, session_->SdesPolicy());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475}
1476
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001477TEST_F(WebRtcSessionTest, TestInitializeWithoutDtls) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001478 Init();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001479 // SDES is required if DTLS is off.
1480 EXPECT_EQ(cricket::SEC_REQUIRED, session_->SdesPolicy());
wu@webrtc.org91053e72013-08-10 07:18:04 +00001481}
1482
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483TEST_F(WebRtcSessionTest, TestSessionCandidates) {
1484 TestSessionCandidatesWithBundleRtcpMux(false, false);
1485}
1486
1487// Below test cases (TestSessionCandidatesWith*) verify the candidates gathered
1488// with rtcp-mux and/or bundle.
1489TEST_F(WebRtcSessionTest, TestSessionCandidatesWithRtcpMux) {
1490 TestSessionCandidatesWithBundleRtcpMux(false, true);
1491}
1492
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493TEST_F(WebRtcSessionTest, TestSessionCandidatesWithBundleRtcpMux) {
1494 TestSessionCandidatesWithBundleRtcpMux(true, true);
1495}
1496
1497TEST_F(WebRtcSessionTest, TestMultihomeCandidates) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001498 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
1499 AddInterface(rtc::SocketAddress(kClientAddrHost2, kClientAddrPort));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001500 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001501 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 InitiateCall();
1503 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1504 EXPECT_EQ(8u, observer_.mline_0_candidates_.size());
1505 EXPECT_EQ(8u, observer_.mline_1_candidates_.size());
1506}
1507
minyuec8930ba2016-01-22 06:17:46 -08001508// Crashes on Win only. See webrtc:5411.
1509#if defined(WEBRTC_WIN)
1510#define MAYBE_TestStunError DISABLED_TestStunError
1511#else
1512#define MAYBE_TestStunError TestStunError
1513#endif
1514TEST_F(WebRtcSessionTest, MAYBE_TestStunError) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001515 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
1516 AddInterface(rtc::SocketAddress(kClientAddrHost2, kClientAddrPort));
wu@webrtc.org364f2042013-11-20 21:49:41 +00001517 fss_->AddRule(false,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001518 rtc::FP_UDP,
1519 rtc::FD_ANY,
1520 rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001521 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001522 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 InitiateCall();
wu@webrtc.org364f2042013-11-20 21:49:41 +00001524 // Since kClientAddrHost1 is blocked, not expecting stun candidates for it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1526 EXPECT_EQ(6u, observer_.mline_0_candidates_.size());
1527 EXPECT_EQ(6u, observer_.mline_1_candidates_.size());
1528}
1529
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001530TEST_F(WebRtcSessionTest, SetSdpFailedOnInvalidSdp) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001531 Init();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001532 SessionDescriptionInterface* offer = NULL;
1533 // Since |offer| is NULL, there's no way to tell if it's an offer or answer.
1534 std::string unknown_action;
1535 SetLocalDescriptionExpectError(unknown_action, kInvalidSdp, offer);
1536 SetRemoteDescriptionExpectError(unknown_action, kInvalidSdp, offer);
1537}
1538
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539// Test creating offers and receive answers and make sure the
1540// media engine creates the expected send and receive streams.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001541TEST_F(WebRtcSessionTest, TestCreateSdesOfferReceiveSdesAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001542 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001543 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001544 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 const std::string session_id_orig = offer->session_id();
1546 const std::string session_version_orig = offer->session_version();
1547 SetLocalDescriptionWithoutError(offer);
1548
deadbeefab9b2d12015-10-14 11:33:11 -07001549 SendAudioVideoStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 SessionDescriptionInterface* answer =
1551 CreateRemoteAnswer(session_->local_description());
1552 SetRemoteDescriptionWithoutError(answer);
1553
1554 video_channel_ = media_engine_->GetVideoChannel(0);
1555 voice_channel_ = media_engine_->GetVoiceChannel(0);
1556
1557 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1558 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1559
1560 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1561 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1562
1563 ASSERT_EQ(1u, video_channel_->send_streams().size());
1564 EXPECT_TRUE(kVideoTrack1 == video_channel_->send_streams()[0].id);
1565 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1566 EXPECT_TRUE(kAudioTrack1 == voice_channel_->send_streams()[0].id);
1567
1568 // Create new offer without send streams.
deadbeefab9b2d12015-10-14 11:33:11 -07001569 SendNothing();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001570 offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571
1572 // Verify the session id is the same and the session version is
1573 // increased.
1574 EXPECT_EQ(session_id_orig, offer->session_id());
Peter Boström0c4e06b2015-10-07 12:23:21 +02001575 EXPECT_LT(rtc::FromString<uint64_t>(session_version_orig),
1576 rtc::FromString<uint64_t>(offer->session_version()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577
1578 SetLocalDescriptionWithoutError(offer);
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001579 EXPECT_EQ(0u, video_channel_->send_streams().size());
1580 EXPECT_EQ(0u, voice_channel_->send_streams().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581
deadbeefab9b2d12015-10-14 11:33:11 -07001582 SendAudioVideoStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 answer = CreateRemoteAnswer(session_->local_description());
1584 SetRemoteDescriptionWithoutError(answer);
1585
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 // Make sure the receive streams have not changed.
1587 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1588 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1589 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1590 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1591}
1592
1593// Test receiving offers and creating answers and make sure the
1594// media engine creates the expected send and receive streams.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001595TEST_F(WebRtcSessionTest, TestReceiveSdesOfferCreateSdesAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001596 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001597 SendAudioVideoStream2();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001598 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001599 VerifyCryptoParams(offer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 SetRemoteDescriptionWithoutError(offer);
1601
deadbeefab9b2d12015-10-14 11:33:11 -07001602 SendAudioVideoStream1();
htaa2a49d92016-03-04 02:51:39 -08001603 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001604 VerifyCryptoParams(answer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 SetLocalDescriptionWithoutError(answer);
1606
1607 const std::string session_id_orig = answer->session_id();
1608 const std::string session_version_orig = answer->session_version();
1609
1610 video_channel_ = media_engine_->GetVideoChannel(0);
1611 voice_channel_ = media_engine_->GetVoiceChannel(0);
1612
htaa2a49d92016-03-04 02:51:39 -08001613 ASSERT_TRUE(video_channel_);
1614 ASSERT_TRUE(voice_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1616 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1617
1618 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1619 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1620
1621 ASSERT_EQ(1u, video_channel_->send_streams().size());
1622 EXPECT_TRUE(kVideoTrack1 == video_channel_->send_streams()[0].id);
1623 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1624 EXPECT_TRUE(kAudioTrack1 == voice_channel_->send_streams()[0].id);
1625
deadbeefab9b2d12015-10-14 11:33:11 -07001626 SendAudioVideoStream1And2();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001627 offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 SetRemoteDescriptionWithoutError(offer);
1629
1630 // Answer by turning off all send streams.
deadbeefab9b2d12015-10-14 11:33:11 -07001631 SendNothing();
htaa2a49d92016-03-04 02:51:39 -08001632 answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633
1634 // Verify the session id is the same and the session version is
1635 // increased.
1636 EXPECT_EQ(session_id_orig, answer->session_id());
Peter Boström0c4e06b2015-10-07 12:23:21 +02001637 EXPECT_LT(rtc::FromString<uint64_t>(session_version_orig),
1638 rtc::FromString<uint64_t>(answer->session_version()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 SetLocalDescriptionWithoutError(answer);
1640
1641 ASSERT_EQ(2u, video_channel_->recv_streams().size());
1642 EXPECT_TRUE(kVideoTrack1 == video_channel_->recv_streams()[0].id);
1643 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[1].id);
1644 ASSERT_EQ(2u, voice_channel_->recv_streams().size());
1645 EXPECT_TRUE(kAudioTrack1 == voice_channel_->recv_streams()[0].id);
1646 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[1].id);
1647
1648 // Make sure we have no send streams.
1649 EXPECT_EQ(0u, video_channel_->send_streams().size());
1650 EXPECT_EQ(0u, voice_channel_->send_streams().size());
1651}
1652
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001653TEST_F(WebRtcSessionTest, SetLocalSdpFailedOnCreateChannel) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001654 Init();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001655 media_engine_->set_fail_create_channel(true);
1656
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001657 SessionDescriptionInterface* offer = CreateOffer();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001658 ASSERT_TRUE(offer != NULL);
1659 // SetRemoteDescription and SetLocalDescription will take the ownership of
1660 // the offer.
1661 SetRemoteDescriptionOfferExpectError(kCreateChannelFailed, offer);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001662 offer = CreateOffer();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001663 ASSERT_TRUE(offer != NULL);
1664 SetLocalDescriptionOfferExpectError(kCreateChannelFailed, offer);
1665}
1666
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001667//
1668// Tests for creating/setting SDP under different SDES/DTLS polices:
1669//
1670// --DTLS off and SDES on
1671// TestCreateSdesOfferReceiveSdesAnswer/TestReceiveSdesOfferCreateSdesAnswer:
1672// set local/remote offer/answer with crypto --> success
1673// TestSetNonSdesOfferWhenSdesOn: set local/remote offer without crypto --->
1674// failure
1675// TestSetLocalNonSdesAnswerWhenSdesOn: set local answer without crypto -->
1676// failure
1677// TestSetRemoteNonSdesAnswerWhenSdesOn: set remote answer without crypto -->
1678// failure
1679//
1680// --DTLS on and SDES off
1681// TestCreateDtlsOfferReceiveDtlsAnswer/TestReceiveDtlsOfferCreateDtlsAnswer:
1682// set local/remote offer/answer with DTLS fingerprint --> success
1683// TestReceiveNonDtlsOfferWhenDtlsOn: set local/remote offer without DTLS
1684// fingerprint --> failure
1685// TestSetLocalNonDtlsAnswerWhenDtlsOn: set local answer without fingerprint
1686// --> failure
1687// TestSetRemoteNonDtlsAnswerWhenDtlsOn: set remote answer without fingerprint
1688// --> failure
1689//
1690// --Encryption disabled: DTLS off and SDES off
1691// TestCreateOfferReceiveAnswerWithoutEncryption: set local offer and remote
1692// answer without SDES or DTLS --> success
1693// TestCreateAnswerReceiveOfferWithoutEncryption: set remote offer and local
1694// answer without SDES or DTLS --> success
1695//
1696
1697// Test that we return a failure when applying a remote/local offer that doesn't
1698// have cryptos enabled when DTLS is off.
1699TEST_F(WebRtcSessionTest, TestSetNonSdesOfferWhenSdesOn) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001700 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001702 options.recv_video = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 JsepSessionDescription* offer = CreateRemoteOffer(
1704 options, cricket::SEC_DISABLED);
1705 ASSERT_TRUE(offer != NULL);
1706 VerifyNoCryptoParams(offer->description(), false);
1707 // SetRemoteDescription and SetLocalDescription will take the ownership of
1708 // the offer.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001709 SetRemoteDescriptionOfferExpectError(kSdpWithoutSdesCrypto, offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 offer = CreateRemoteOffer(options, cricket::SEC_DISABLED);
1711 ASSERT_TRUE(offer != NULL);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001712 SetLocalDescriptionOfferExpectError(kSdpWithoutSdesCrypto, offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713}
1714
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001715// Test that we return a failure when applying a local answer that doesn't have
1716// cryptos enabled when DTLS is off.
1717TEST_F(WebRtcSessionTest, TestSetLocalNonSdesAnswerWhenSdesOn) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001718 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 SessionDescriptionInterface* offer = NULL;
1720 SessionDescriptionInterface* answer = NULL;
1721 CreateCryptoOfferAndNonCryptoAnswer(&offer, &answer);
1722 // SetRemoteDescription and SetLocalDescription will take the ownership of
1723 // the offer.
1724 SetRemoteDescriptionWithoutError(offer);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001725 SetLocalDescriptionAnswerExpectError(kSdpWithoutSdesCrypto, answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726}
1727
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001728// Test we will return fail when apply an remote answer that doesn't have
1729// crypto enabled when DTLS is off.
1730TEST_F(WebRtcSessionTest, TestSetRemoteNonSdesAnswerWhenSdesOn) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001731 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 SessionDescriptionInterface* offer = NULL;
1733 SessionDescriptionInterface* answer = NULL;
1734 CreateCryptoOfferAndNonCryptoAnswer(&offer, &answer);
1735 // SetRemoteDescription and SetLocalDescription will take the ownership of
1736 // the offer.
1737 SetLocalDescriptionWithoutError(offer);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001738 SetRemoteDescriptionAnswerExpectError(kSdpWithoutSdesCrypto, answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739}
1740
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001741// Test that we accept an offer with a DTLS fingerprint when DTLS is on
1742// and that we return an answer with a DTLS fingerprint.
Henrik Boström87713d02015-08-25 09:53:21 +02001743TEST_P(WebRtcSessionTest, TestReceiveDtlsOfferCreateDtlsAnswer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001744 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
deadbeefab9b2d12015-10-14 11:33:11 -07001745 SendAudioVideoStream1();
Henrik Boström87713d02015-08-25 09:53:21 +02001746 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 SetFactoryDtlsSrtp();
1748 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001749 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001750 JsepSessionDescription* offer =
1751 CreateRemoteOffer(options, cricket::SEC_DISABLED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 ASSERT_TRUE(offer != NULL);
1753 VerifyFingerprintStatus(offer->description(), true);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001754 VerifyNoCryptoParams(offer->description(), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755
1756 // SetRemoteDescription will take the ownership of the offer.
1757 SetRemoteDescriptionWithoutError(offer);
1758
1759 // Verify that we get a crypto fingerprint in the answer.
htaa2a49d92016-03-04 02:51:39 -08001760 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 ASSERT_TRUE(answer != NULL);
1762 VerifyFingerprintStatus(answer->description(), true);
1763 // Check that we don't have an a=crypto line in the answer.
1764 VerifyNoCryptoParams(answer->description(), true);
1765
1766 // Now set the local description, which should work, even without a=crypto.
1767 SetLocalDescriptionWithoutError(answer);
1768}
1769
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001770// Test that we set a local offer with a DTLS fingerprint when DTLS is on
1771// and then we accept a remote answer with a DTLS fingerprint successfully.
Henrik Boström87713d02015-08-25 09:53:21 +02001772TEST_P(WebRtcSessionTest, TestCreateDtlsOfferReceiveDtlsAnswer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001773 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
deadbeefab9b2d12015-10-14 11:33:11 -07001774 SendAudioVideoStream1();
Henrik Boström87713d02015-08-25 09:53:21 +02001775 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001776 SetFactoryDtlsSrtp();
1777
1778 // Verify that we get a crypto fingerprint in the answer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001779 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001780 ASSERT_TRUE(offer != NULL);
1781 VerifyFingerprintStatus(offer->description(), true);
1782 // Check that we don't have an a=crypto line in the offer.
1783 VerifyNoCryptoParams(offer->description(), true);
1784
1785 // Now set the local description, which should work, even without a=crypto.
1786 SetLocalDescriptionWithoutError(offer);
1787
1788 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001789 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001790 JsepSessionDescription* answer =
1791 CreateRemoteAnswer(offer, options, cricket::SEC_DISABLED);
1792 ASSERT_TRUE(answer != NULL);
1793 VerifyFingerprintStatus(answer->description(), true);
1794 VerifyNoCryptoParams(answer->description(), true);
1795
1796 // SetRemoteDescription will take the ownership of the answer.
1797 SetRemoteDescriptionWithoutError(answer);
1798}
1799
1800// Test that if we support DTLS and the other side didn't offer a fingerprint,
1801// we will fail to set the remote description.
Henrik Boström87713d02015-08-25 09:53:21 +02001802TEST_P(WebRtcSessionTest, TestReceiveNonDtlsOfferWhenDtlsOn) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001803 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02001804 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001806 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001807 options.bundle_enabled = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 JsepSessionDescription* offer = CreateRemoteOffer(
1809 options, cricket::SEC_REQUIRED);
1810 ASSERT_TRUE(offer != NULL);
1811 VerifyFingerprintStatus(offer->description(), false);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001812 VerifyCryptoParams(offer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001814 // SetRemoteDescription will take the ownership of the offer.
1815 SetRemoteDescriptionOfferExpectError(
1816 kSdpWithoutDtlsFingerprint, offer);
1817
1818 offer = CreateRemoteOffer(options, cricket::SEC_REQUIRED);
1819 // SetLocalDescription will take the ownership of the offer.
1820 SetLocalDescriptionOfferExpectError(
1821 kSdpWithoutDtlsFingerprint, offer);
1822}
1823
1824// Test that we return a failure when applying a local answer that doesn't have
1825// a DTLS fingerprint when DTLS is required.
Henrik Boström87713d02015-08-25 09:53:21 +02001826TEST_P(WebRtcSessionTest, TestSetLocalNonDtlsAnswerWhenDtlsOn) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001827 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02001828 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001829 SessionDescriptionInterface* offer = NULL;
1830 SessionDescriptionInterface* answer = NULL;
1831 CreateDtlsOfferAndNonDtlsAnswer(&offer, &answer);
1832
1833 // SetRemoteDescription and SetLocalDescription will take the ownership of
1834 // the offer and answer.
1835 SetRemoteDescriptionWithoutError(offer);
1836 SetLocalDescriptionAnswerExpectError(
1837 kSdpWithoutDtlsFingerprint, answer);
1838}
1839
1840// Test that we return a failure when applying a remote answer that doesn't have
1841// a DTLS fingerprint when DTLS is required.
Henrik Boström87713d02015-08-25 09:53:21 +02001842TEST_P(WebRtcSessionTest, TestSetRemoteNonDtlsAnswerWhenDtlsOn) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001843 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02001844 InitWithDtls(GetParam());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001845 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001846 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001847 options.recv_video = true;
kwibergd1fe2812016-04-27 06:47:29 -07001848 std::unique_ptr<SessionDescriptionInterface> temp_offer(
deadbeefcbecd352015-09-23 11:50:27 -07001849 CreateRemoteOffer(options, cricket::SEC_ENABLED));
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001850 JsepSessionDescription* answer =
deadbeefcbecd352015-09-23 11:50:27 -07001851 CreateRemoteAnswer(temp_offer.get(), options, cricket::SEC_ENABLED);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001852
1853 // SetRemoteDescription and SetLocalDescription will take the ownership of
1854 // the offer and answer.
1855 SetLocalDescriptionWithoutError(offer);
1856 SetRemoteDescriptionAnswerExpectError(
1857 kSdpWithoutDtlsFingerprint, answer);
1858}
1859
1860// Test that we create a local offer without SDES or DTLS and accept a remote
1861// answer without SDES or DTLS when encryption is disabled.
Henrik Boström87713d02015-08-25 09:53:21 +02001862TEST_P(WebRtcSessionTest, TestCreateOfferReceiveAnswerWithoutEncryption) {
deadbeefab9b2d12015-10-14 11:33:11 -07001863 SendAudioVideoStream1();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001864 options_.disable_encryption = true;
Henrik Boström87713d02015-08-25 09:53:21 +02001865 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001866
1867 // Verify that we get a crypto fingerprint in the answer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001868 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001869 ASSERT_TRUE(offer != NULL);
1870 VerifyFingerprintStatus(offer->description(), false);
1871 // Check that we don't have an a=crypto line in the offer.
1872 VerifyNoCryptoParams(offer->description(), false);
1873
1874 // Now set the local description, which should work, even without a=crypto.
1875 SetLocalDescriptionWithoutError(offer);
1876
1877 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001878 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001879 JsepSessionDescription* answer =
1880 CreateRemoteAnswer(offer, options, cricket::SEC_DISABLED);
1881 ASSERT_TRUE(answer != NULL);
1882 VerifyFingerprintStatus(answer->description(), false);
1883 VerifyNoCryptoParams(answer->description(), false);
1884
1885 // SetRemoteDescription will take the ownership of the answer.
1886 SetRemoteDescriptionWithoutError(answer);
1887}
1888
1889// Test that we create a local answer without SDES or DTLS and accept a remote
1890// offer without SDES or DTLS when encryption is disabled.
Henrik Boström87713d02015-08-25 09:53:21 +02001891TEST_P(WebRtcSessionTest, TestCreateAnswerReceiveOfferWithoutEncryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001892 options_.disable_encryption = true;
Henrik Boström87713d02015-08-25 09:53:21 +02001893 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001894
1895 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001896 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001897 JsepSessionDescription* offer =
1898 CreateRemoteOffer(options, cricket::SEC_DISABLED);
1899 ASSERT_TRUE(offer != NULL);
1900 VerifyFingerprintStatus(offer->description(), false);
1901 VerifyNoCryptoParams(offer->description(), false);
1902
1903 // SetRemoteDescription will take the ownership of the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 SetRemoteDescriptionWithoutError(offer);
1905
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001906 // Verify that we get a crypto fingerprint in the answer.
htaa2a49d92016-03-04 02:51:39 -08001907 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908 ASSERT_TRUE(answer != NULL);
1909 VerifyFingerprintStatus(answer->description(), false);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001910 // Check that we don't have an a=crypto line in the answer.
1911 VerifyNoCryptoParams(answer->description(), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001912
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001913 // Now set the local description, which should work, even without a=crypto.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914 SetLocalDescriptionWithoutError(answer);
1915}
1916
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001917// Test that we can create and set an answer correctly when different
1918// SSL roles have been negotiated for different transports.
1919// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4525
1920TEST_P(WebRtcSessionTest, TestCreateAnswerWithDifferentSslRoles) {
1921 SendAudioVideoStream1();
1922 InitWithDtls(GetParam());
1923 SetFactoryDtlsSrtp();
1924
1925 SessionDescriptionInterface* offer = CreateOffer();
1926 SetLocalDescriptionWithoutError(offer);
1927
1928 cricket::MediaSessionOptions options;
1929 options.recv_video = true;
1930
1931 // First, negotiate different SSL roles.
1932 SessionDescriptionInterface* answer =
1933 CreateRemoteAnswer(offer, options, cricket::SEC_DISABLED);
1934 TransportInfo* audio_transport_info =
1935 answer->description()->GetTransportInfoByName("audio");
1936 audio_transport_info->description.connection_role =
1937 cricket::CONNECTIONROLE_ACTIVE;
1938 TransportInfo* video_transport_info =
1939 answer->description()->GetTransportInfoByName("video");
1940 video_transport_info->description.connection_role =
1941 cricket::CONNECTIONROLE_PASSIVE;
1942 SetRemoteDescriptionWithoutError(answer);
1943
1944 // Now create an offer in the reverse direction, and ensure the initial
1945 // offerer responds with an answer with correct SSL roles.
1946 offer = CreateRemoteOfferWithVersion(options, cricket::SEC_DISABLED,
1947 kSessionVersion,
1948 session_->remote_description());
1949 SetRemoteDescriptionWithoutError(offer);
1950
htaa2a49d92016-03-04 02:51:39 -08001951 answer = CreateAnswer();
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001952 audio_transport_info = answer->description()->GetTransportInfoByName("audio");
1953 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
1954 audio_transport_info->description.connection_role);
1955 video_transport_info = answer->description()->GetTransportInfoByName("video");
1956 EXPECT_EQ(cricket::CONNECTIONROLE_ACTIVE,
1957 video_transport_info->description.connection_role);
1958 SetLocalDescriptionWithoutError(answer);
1959
1960 // Lastly, start BUNDLE-ing on "audio", expecting that the "passive" role of
1961 // audio is transferred over to video in the answer that completes the BUNDLE
1962 // negotiation.
1963 options.bundle_enabled = true;
1964 offer = CreateRemoteOfferWithVersion(options, cricket::SEC_DISABLED,
1965 kSessionVersion,
1966 session_->remote_description());
1967 SetRemoteDescriptionWithoutError(offer);
htaa2a49d92016-03-04 02:51:39 -08001968 answer = CreateAnswer();
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001969 audio_transport_info = answer->description()->GetTransportInfoByName("audio");
1970 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
1971 audio_transport_info->description.connection_role);
1972 video_transport_info = answer->description()->GetTransportInfoByName("video");
1973 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
1974 video_transport_info->description.connection_role);
1975 SetLocalDescriptionWithoutError(answer);
1976}
1977
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978TEST_F(WebRtcSessionTest, TestSetLocalOfferTwice) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001979 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001980 SendNothing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 // SetLocalDescription take ownership of offer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001982 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 SetLocalDescriptionWithoutError(offer);
1984
1985 // SetLocalDescription take ownership of offer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001986 SessionDescriptionInterface* offer2 = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987 SetLocalDescriptionWithoutError(offer2);
1988}
1989
1990TEST_F(WebRtcSessionTest, TestSetRemoteOfferTwice) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001991 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001992 SendNothing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 // SetLocalDescription take ownership of offer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001994 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995 SetRemoteDescriptionWithoutError(offer);
1996
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001997 SessionDescriptionInterface* offer2 = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 SetRemoteDescriptionWithoutError(offer2);
1999}
2000
2001TEST_F(WebRtcSessionTest, TestSetLocalAndRemoteOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002002 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002003 SendNothing();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002004 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 SetLocalDescriptionWithoutError(offer);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002006 offer = CreateOffer();
deadbeefd59daf82015-10-14 15:02:44 -07002007 SetRemoteDescriptionOfferExpectError("Called in wrong state: STATE_SENTOFFER",
2008 offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009}
2010
2011TEST_F(WebRtcSessionTest, TestSetRemoteAndLocalOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002012 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002013 SendNothing();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002014 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015 SetRemoteDescriptionWithoutError(offer);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002016 offer = CreateOffer();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002017 SetLocalDescriptionOfferExpectError(
deadbeefd59daf82015-10-14 15:02:44 -07002018 "Called in wrong state: STATE_RECEIVEDOFFER", offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019}
2020
2021TEST_F(WebRtcSessionTest, TestSetLocalPrAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002022 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002023 SendNothing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024 SessionDescriptionInterface* offer = CreateRemoteOffer();
deadbeefd59daf82015-10-14 15:02:44 -07002025 SetRemoteDescriptionExpectState(offer, WebRtcSession::STATE_RECEIVEDOFFER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026
htaa2a49d92016-03-04 02:51:39 -08002027 JsepSessionDescription* pranswer =
2028 static_cast<JsepSessionDescription*>(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 pranswer->set_type(SessionDescriptionInterface::kPrAnswer);
deadbeefd59daf82015-10-14 15:02:44 -07002030 SetLocalDescriptionExpectState(pranswer, WebRtcSession::STATE_SENTPRANSWER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031
deadbeefab9b2d12015-10-14 11:33:11 -07002032 SendAudioVideoStream1();
htaa2a49d92016-03-04 02:51:39 -08002033 JsepSessionDescription* pranswer2 =
2034 static_cast<JsepSessionDescription*>(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 pranswer2->set_type(SessionDescriptionInterface::kPrAnswer);
2036
deadbeefd59daf82015-10-14 15:02:44 -07002037 SetLocalDescriptionExpectState(pranswer2, WebRtcSession::STATE_SENTPRANSWER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038
deadbeefab9b2d12015-10-14 11:33:11 -07002039 SendAudioVideoStream2();
htaa2a49d92016-03-04 02:51:39 -08002040 SessionDescriptionInterface* answer = CreateAnswer();
deadbeefd59daf82015-10-14 15:02:44 -07002041 SetLocalDescriptionExpectState(answer, WebRtcSession::STATE_INPROGRESS);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042}
2043
2044TEST_F(WebRtcSessionTest, TestSetRemotePrAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002045 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002046 SendNothing();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002047 SessionDescriptionInterface* offer = CreateOffer();
deadbeefd59daf82015-10-14 15:02:44 -07002048 SetLocalDescriptionExpectState(offer, WebRtcSession::STATE_SENTOFFER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049
2050 JsepSessionDescription* pranswer =
2051 CreateRemoteAnswer(session_->local_description());
2052 pranswer->set_type(SessionDescriptionInterface::kPrAnswer);
2053
2054 SetRemoteDescriptionExpectState(pranswer,
deadbeefd59daf82015-10-14 15:02:44 -07002055 WebRtcSession::STATE_RECEIVEDPRANSWER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056
deadbeefab9b2d12015-10-14 11:33:11 -07002057 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 JsepSessionDescription* pranswer2 =
2059 CreateRemoteAnswer(session_->local_description());
2060 pranswer2->set_type(SessionDescriptionInterface::kPrAnswer);
2061
2062 SetRemoteDescriptionExpectState(pranswer2,
deadbeefd59daf82015-10-14 15:02:44 -07002063 WebRtcSession::STATE_RECEIVEDPRANSWER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064
deadbeefab9b2d12015-10-14 11:33:11 -07002065 SendAudioVideoStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 SessionDescriptionInterface* answer =
2067 CreateRemoteAnswer(session_->local_description());
deadbeefd59daf82015-10-14 15:02:44 -07002068 SetRemoteDescriptionExpectState(answer, WebRtcSession::STATE_INPROGRESS);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069}
2070
2071TEST_F(WebRtcSessionTest, TestSetLocalAnswerWithoutOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002072 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002073 SendNothing();
kwibergd1fe2812016-04-27 06:47:29 -07002074 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002075
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 SessionDescriptionInterface* answer =
2077 CreateRemoteAnswer(offer.get());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002078 SetLocalDescriptionAnswerExpectError("Called in wrong state: STATE_INIT",
2079 answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080}
2081
2082TEST_F(WebRtcSessionTest, TestSetRemoteAnswerWithoutOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002083 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002084 SendNothing();
kwibergd1fe2812016-04-27 06:47:29 -07002085 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002086
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 SessionDescriptionInterface* answer =
2088 CreateRemoteAnswer(offer.get());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002089 SetRemoteDescriptionAnswerExpectError(
2090 "Called in wrong state: STATE_INIT", answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091}
2092
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002093// Tests that the remote candidates are added and removed successfully.
2094TEST_F(WebRtcSessionTest, TestAddAndRemoveRemoteCandidates) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002095 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002096 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002098 cricket::Candidate candidate(1, "udp", rtc::SocketAddress("1.1.1.1", 5000), 0,
2099 "", "", "host", 0, "");
2100 candidate.set_transport_name("audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002101 JsepIceCandidate ice_candidate1(kMediaContentName0, 0, candidate);
2102
deadbeefd59daf82015-10-14 15:02:44 -07002103 // Fail since we have not set a remote description.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104 EXPECT_FALSE(session_->ProcessIceMessage(&ice_candidate1));
2105
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002106 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 SetLocalDescriptionWithoutError(offer);
deadbeefd59daf82015-10-14 15:02:44 -07002108
2109 // Fail since we have not set a remote description.
2110 EXPECT_FALSE(session_->ProcessIceMessage(&ice_candidate1));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111
2112 SessionDescriptionInterface* answer = CreateRemoteAnswer(
2113 session_->local_description());
2114 SetRemoteDescriptionWithoutError(answer);
2115
deadbeefd59daf82015-10-14 15:02:44 -07002116 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate1));
2117 candidate.set_component(2);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002118 candidate.set_address(rtc::SocketAddress("2.2.2.2", 6000));
deadbeefd59daf82015-10-14 15:02:44 -07002119 JsepIceCandidate ice_candidate2(kMediaContentName0, 0, candidate);
2120 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate2));
2121
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122 // Verifying the candidates are copied properly from internal vector.
2123 const SessionDescriptionInterface* remote_desc =
2124 session_->remote_description();
2125 ASSERT_TRUE(remote_desc != NULL);
2126 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
2127 const IceCandidateCollection* candidates =
2128 remote_desc->candidates(kMediaContentIndex0);
2129 ASSERT_EQ(2u, candidates->count());
2130 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
2131 EXPECT_EQ(kMediaContentName0, candidates->at(0)->sdp_mid());
2132 EXPECT_EQ(1, candidates->at(0)->candidate().component());
2133 EXPECT_EQ(2, candidates->at(1)->candidate().component());
2134
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002135 // |ice_candidate3| is identical to |ice_candidate2|. It can be added
2136 // successfully, but the total count of candidates will not increase.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 candidate.set_component(2);
2138 JsepIceCandidate ice_candidate3(kMediaContentName0, 0, candidate);
2139 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate3));
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002140 ASSERT_EQ(2u, candidates->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141
2142 JsepIceCandidate bad_ice_candidate("bad content name", 99, candidate);
2143 EXPECT_FALSE(session_->ProcessIceMessage(&bad_ice_candidate));
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002144
2145 // Remove candidate1 and candidate2
2146 std::vector<cricket::Candidate> remote_candidates;
2147 remote_candidates.push_back(ice_candidate1.candidate());
2148 remote_candidates.push_back(ice_candidate2.candidate());
2149 EXPECT_TRUE(session_->RemoveRemoteIceCandidates(remote_candidates));
2150 EXPECT_EQ(0u, candidates->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151}
2152
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002153// Tests that a remote candidate is added to the remote session description and
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154// that it is retained if the remote session description is changed.
2155TEST_F(WebRtcSessionTest, TestRemoteCandidatesAddedToSessionDescription) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002156 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 cricket::Candidate candidate1;
2158 candidate1.set_component(1);
2159 JsepIceCandidate ice_candidate1(kMediaContentName0, kMediaContentIndex0,
2160 candidate1);
deadbeefab9b2d12015-10-14 11:33:11 -07002161 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162 CreateAndSetRemoteOfferAndLocalAnswer();
2163
2164 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate1));
2165 const SessionDescriptionInterface* remote_desc =
2166 session_->remote_description();
2167 ASSERT_TRUE(remote_desc != NULL);
2168 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
2169 const IceCandidateCollection* candidates =
2170 remote_desc->candidates(kMediaContentIndex0);
2171 ASSERT_EQ(1u, candidates->count());
2172 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
2173
2174 // Update the RemoteSessionDescription with a new session description and
2175 // a candidate and check that the new remote session description contains both
2176 // candidates.
2177 SessionDescriptionInterface* offer = CreateRemoteOffer();
2178 cricket::Candidate candidate2;
2179 JsepIceCandidate ice_candidate2(kMediaContentName0, kMediaContentIndex0,
2180 candidate2);
2181 EXPECT_TRUE(offer->AddCandidate(&ice_candidate2));
2182 SetRemoteDescriptionWithoutError(offer);
2183
2184 remote_desc = session_->remote_description();
2185 ASSERT_TRUE(remote_desc != NULL);
2186 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
2187 candidates = remote_desc->candidates(kMediaContentIndex0);
2188 ASSERT_EQ(2u, candidates->count());
2189 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
2190 // Username and password have be updated with the TransportInfo of the
2191 // SessionDescription, won't be equal to the original one.
2192 candidate2.set_username(candidates->at(0)->candidate().username());
2193 candidate2.set_password(candidates->at(0)->candidate().password());
2194 EXPECT_TRUE(candidate2.IsEquivalent(candidates->at(0)->candidate()));
2195 EXPECT_EQ(kMediaContentIndex0, candidates->at(1)->sdp_mline_index());
2196 // No need to verify the username and password.
2197 candidate1.set_username(candidates->at(1)->candidate().username());
2198 candidate1.set_password(candidates->at(1)->candidate().password());
2199 EXPECT_TRUE(candidate1.IsEquivalent(candidates->at(1)->candidate()));
2200
2201 // Test that the candidate is ignored if we can add the same candidate again.
2202 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate2));
2203}
2204
2205// Test that local candidates are added to the local session description and
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002206// that they are retained if the local session description is changed. And if
2207// continual gathering is enabled, they are removed from the local session
2208// description when the network is down.
2209TEST_F(WebRtcSessionTest,
2210 TestLocalCandidatesAddedAndRemovedIfGatherContinually) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002211 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002212 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002213 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 CreateAndSetRemoteOfferAndLocalAnswer();
2215
2216 const SessionDescriptionInterface* local_desc = session_->local_description();
2217 const IceCandidateCollection* candidates =
2218 local_desc->candidates(kMediaContentIndex0);
2219 ASSERT_TRUE(candidates != NULL);
2220 EXPECT_EQ(0u, candidates->count());
2221
2222 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
2223
2224 local_desc = session_->local_description();
2225 candidates = local_desc->candidates(kMediaContentIndex0);
2226 ASSERT_TRUE(candidates != NULL);
2227 EXPECT_LT(0u, candidates->count());
2228 candidates = local_desc->candidates(1);
2229 ASSERT_TRUE(candidates != NULL);
deadbeefcbecd352015-09-23 11:50:27 -07002230 EXPECT_EQ(0u, candidates->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231
2232 // Update the session descriptions.
deadbeefab9b2d12015-10-14 11:33:11 -07002233 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 CreateAndSetRemoteOfferAndLocalAnswer();
2235
2236 local_desc = session_->local_description();
2237 candidates = local_desc->candidates(kMediaContentIndex0);
2238 ASSERT_TRUE(candidates != NULL);
2239 EXPECT_LT(0u, candidates->count());
2240 candidates = local_desc->candidates(1);
2241 ASSERT_TRUE(candidates != NULL);
deadbeefcbecd352015-09-23 11:50:27 -07002242 EXPECT_EQ(0u, candidates->count());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002243
2244 candidates = local_desc->candidates(kMediaContentIndex0);
2245 size_t num_local_candidates = candidates->count();
2246 // Enable Continual Gathering
2247 session_->SetIceConfig(cricket::IceConfig(-1, -1, true, false, -1));
2248 // Bring down the network interface to trigger candidate removals.
2249 RemoveInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
2250 // Verify that all local candidates are removed.
2251 EXPECT_EQ(0, observer_.num_candidates_removed_);
2252 EXPECT_EQ_WAIT(num_local_candidates, observer_.num_candidates_removed_,
2253 kIceCandidatesTimeout);
2254 EXPECT_EQ_WAIT(0u, candidates->count(), kIceCandidatesTimeout);
2255}
2256
2257// Tests that if continual gathering is disabled, local candidates won't be
2258// removed when the interface is turned down.
2259TEST_F(WebRtcSessionTest, TestLocalCandidatesNotRemovedIfNotGatherContinually) {
2260 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
2261 Init();
2262 SendAudioVideoStream1();
2263 CreateAndSetRemoteOfferAndLocalAnswer();
2264
2265 const SessionDescriptionInterface* local_desc = session_->local_description();
2266 const IceCandidateCollection* candidates =
2267 local_desc->candidates(kMediaContentIndex0);
2268 ASSERT_TRUE(candidates != NULL);
2269 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
2270
2271 size_t num_local_candidates = candidates->count();
2272 EXPECT_LT(0u, num_local_candidates);
2273 // By default, Continual Gathering is disabled.
2274 // Bring down the network interface.
2275 RemoveInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
2276 // Verify that the local candidates are not removed.
2277 rtc::Thread::Current()->ProcessMessages(1000);
2278 EXPECT_EQ(0, observer_.num_candidates_removed_);
2279 EXPECT_EQ(num_local_candidates, candidates->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280}
2281
2282// Test that we can set a remote session description with remote candidates.
2283TEST_F(WebRtcSessionTest, TestSetRemoteSessionDescriptionWithCandidates) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002284 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285
2286 cricket::Candidate candidate1;
2287 candidate1.set_component(1);
2288 JsepIceCandidate ice_candidate(kMediaContentName0, kMediaContentIndex0,
2289 candidate1);
deadbeefab9b2d12015-10-14 11:33:11 -07002290 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002291 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292
2293 EXPECT_TRUE(offer->AddCandidate(&ice_candidate));
2294 SetRemoteDescriptionWithoutError(offer);
2295
2296 const SessionDescriptionInterface* remote_desc =
2297 session_->remote_description();
2298 ASSERT_TRUE(remote_desc != NULL);
2299 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
2300 const IceCandidateCollection* candidates =
2301 remote_desc->candidates(kMediaContentIndex0);
2302 ASSERT_EQ(1u, candidates->count());
2303 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
2304
htaa2a49d92016-03-04 02:51:39 -08002305 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306 SetLocalDescriptionWithoutError(answer);
2307}
2308
2309// Test that offers and answers contains ice candidates when Ice candidates have
2310// been gathered.
2311TEST_F(WebRtcSessionTest, TestSetLocalAndRemoteDescriptionWithCandidates) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002312 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002313 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002314 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315 // Ice is started but candidates are not provided until SetLocalDescription
2316 // is called.
2317 EXPECT_EQ(0u, observer_.mline_0_candidates_.size());
2318 EXPECT_EQ(0u, observer_.mline_1_candidates_.size());
2319 CreateAndSetRemoteOfferAndLocalAnswer();
2320 // Wait until at least one local candidate has been collected.
2321 EXPECT_TRUE_WAIT(0u < observer_.mline_0_candidates_.size(),
2322 kIceCandidatesTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323
kwibergd1fe2812016-04-27 06:47:29 -07002324 std::unique_ptr<SessionDescriptionInterface> local_offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002325
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326 ASSERT_TRUE(local_offer->candidates(kMediaContentIndex0) != NULL);
2327 EXPECT_LT(0u, local_offer->candidates(kMediaContentIndex0)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328
2329 SessionDescriptionInterface* remote_offer(CreateRemoteOffer());
2330 SetRemoteDescriptionWithoutError(remote_offer);
htaa2a49d92016-03-04 02:51:39 -08002331 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332 ASSERT_TRUE(answer->candidates(kMediaContentIndex0) != NULL);
2333 EXPECT_LT(0u, answer->candidates(kMediaContentIndex0)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334 SetLocalDescriptionWithoutError(answer);
2335}
2336
2337// Verifies TransportProxy and media channels are created with content names
2338// present in the SessionDescription.
2339TEST_F(WebRtcSessionTest, TestChannelCreationsWithContentNames) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002340 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002341 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002342 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343
2344 // CreateOffer creates session description with the content names "audio" and
deadbeefd59daf82015-10-14 15:02:44 -07002345 // "video". Goal is to modify these content names and verify transport
2346 // channels
2347 // in the WebRtcSession, as channels are created with the content names
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 // present in SDP.
2349 std::string sdp;
2350 EXPECT_TRUE(offer->ToString(&sdp));
2351 const std::string kAudioMid = "a=mid:audio";
2352 const std::string kAudioMidReplaceStr = "a=mid:audio_content_name";
2353 const std::string kVideoMid = "a=mid:video";
2354 const std::string kVideoMidReplaceStr = "a=mid:video_content_name";
2355
2356 // Replacing |audio| with |audio_content_name|.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002357 rtc::replace_substrs(kAudioMid.c_str(), kAudioMid.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358 kAudioMidReplaceStr.c_str(),
2359 kAudioMidReplaceStr.length(),
2360 &sdp);
2361 // Replacing |video| with |video_content_name|.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002362 rtc::replace_substrs(kVideoMid.c_str(), kVideoMid.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363 kVideoMidReplaceStr.c_str(),
2364 kVideoMidReplaceStr.length(),
2365 &sdp);
2366
2367 SessionDescriptionInterface* modified_offer =
2368 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
2369
2370 SetRemoteDescriptionWithoutError(modified_offer);
2371
htaa2a49d92016-03-04 02:51:39 -08002372 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002373 SetLocalDescriptionWithoutError(answer);
2374
deadbeefcbecd352015-09-23 11:50:27 -07002375 cricket::TransportChannel* voice_transport_channel =
2376 session_->voice_rtp_transport_channel();
2377 EXPECT_TRUE(voice_transport_channel != NULL);
2378 EXPECT_EQ(voice_transport_channel->transport_name(), "audio_content_name");
2379 cricket::TransportChannel* video_transport_channel =
2380 session_->video_rtp_transport_channel();
htaa2a49d92016-03-04 02:51:39 -08002381 ASSERT_TRUE(video_transport_channel != NULL);
deadbeefcbecd352015-09-23 11:50:27 -07002382 EXPECT_EQ(video_transport_channel->transport_name(), "video_content_name");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002383 EXPECT_TRUE((video_channel_ = media_engine_->GetVideoChannel(0)) != NULL);
2384 EXPECT_TRUE((voice_channel_ = media_engine_->GetVoiceChannel(0)) != NULL);
2385}
2386
2387// Test that an offer contains the correct media content descriptions based on
2388// the send streams when no constraints have been set.
2389TEST_F(WebRtcSessionTest, CreateOfferWithoutConstraintsOrStreams) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002390 Init();
kwibergd1fe2812016-04-27 06:47:29 -07002391 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002392
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393 ASSERT_TRUE(offer != NULL);
2394 const cricket::ContentInfo* content =
2395 cricket::GetFirstAudioContent(offer->description());
2396 EXPECT_TRUE(content != NULL);
2397 content = cricket::GetFirstVideoContent(offer->description());
2398 EXPECT_TRUE(content == NULL);
2399}
2400
2401// Test that an offer contains the correct media content descriptions based on
2402// the send streams when no constraints have been set.
2403TEST_F(WebRtcSessionTest, CreateOfferWithoutConstraints) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002404 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002405 // Test Audio only offer.
deadbeefab9b2d12015-10-14 11:33:11 -07002406 SendAudioOnlyStream2();
kwibergd1fe2812016-04-27 06:47:29 -07002407 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002408
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002409 const cricket::ContentInfo* content =
2410 cricket::GetFirstAudioContent(offer->description());
2411 EXPECT_TRUE(content != NULL);
2412 content = cricket::GetFirstVideoContent(offer->description());
2413 EXPECT_TRUE(content == NULL);
2414
2415 // Test Audio / Video offer.
deadbeefab9b2d12015-10-14 11:33:11 -07002416 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002417 offer.reset(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418 content = cricket::GetFirstAudioContent(offer->description());
2419 EXPECT_TRUE(content != NULL);
2420 content = cricket::GetFirstVideoContent(offer->description());
2421 EXPECT_TRUE(content != NULL);
2422}
2423
2424// Test that an offer contains no media content descriptions if
2425// kOfferToReceiveVideo and kOfferToReceiveAudio constraints are set to false.
2426TEST_F(WebRtcSessionTest, CreateOfferWithConstraintsWithoutStreams) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002427 Init();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002428 PeerConnectionInterface::RTCOfferAnswerOptions options;
2429 options.offer_to_receive_audio = 0;
2430 options.offer_to_receive_video = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002431
kwibergd1fe2812016-04-27 06:47:29 -07002432 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer(options));
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002433
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002434 ASSERT_TRUE(offer != NULL);
2435 const cricket::ContentInfo* content =
2436 cricket::GetFirstAudioContent(offer->description());
2437 EXPECT_TRUE(content == NULL);
2438 content = cricket::GetFirstVideoContent(offer->description());
2439 EXPECT_TRUE(content == NULL);
2440}
2441
2442// Test that an offer contains only audio media content descriptions if
2443// kOfferToReceiveAudio constraints are set to true.
2444TEST_F(WebRtcSessionTest, CreateAudioOnlyOfferWithConstraints) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002445 Init();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002446 PeerConnectionInterface::RTCOfferAnswerOptions options;
2447 options.offer_to_receive_audio =
2448 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
2449
kwibergd1fe2812016-04-27 06:47:29 -07002450 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer(options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002451
2452 const cricket::ContentInfo* content =
2453 cricket::GetFirstAudioContent(offer->description());
2454 EXPECT_TRUE(content != NULL);
2455 content = cricket::GetFirstVideoContent(offer->description());
2456 EXPECT_TRUE(content == NULL);
2457}
2458
2459// Test that an offer contains audio and video media content descriptions if
2460// kOfferToReceiveAudio and kOfferToReceiveVideo constraints are set to true.
2461TEST_F(WebRtcSessionTest, CreateOfferWithConstraints) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002462 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463 // Test Audio / Video offer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002464 PeerConnectionInterface::RTCOfferAnswerOptions options;
2465 options.offer_to_receive_audio =
2466 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
2467 options.offer_to_receive_video =
2468 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
2469
kwibergd1fe2812016-04-27 06:47:29 -07002470 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer(options));
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002471
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472 const cricket::ContentInfo* content =
2473 cricket::GetFirstAudioContent(offer->description());
jiayl@webrtc.orgc1723202014-09-08 20:44:36 +00002474 EXPECT_TRUE(content != NULL);
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00002475
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476 content = cricket::GetFirstVideoContent(offer->description());
2477 EXPECT_TRUE(content != NULL);
2478
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00002479 // Sets constraints to false and verifies that audio/video contents are
2480 // removed.
2481 options.offer_to_receive_audio = 0;
2482 options.offer_to_receive_video = 0;
2483 offer.reset(CreateOffer(options));
2484
2485 content = cricket::GetFirstAudioContent(offer->description());
2486 EXPECT_TRUE(content == NULL);
2487 content = cricket::GetFirstVideoContent(offer->description());
2488 EXPECT_TRUE(content == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002489}
2490
2491// Test that an answer can not be created if the last remote description is not
2492// an offer.
2493TEST_F(WebRtcSessionTest, CreateAnswerWithoutAnOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002494 Init();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002495 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002496 SetLocalDescriptionWithoutError(offer);
2497 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
2498 SetRemoteDescriptionWithoutError(answer);
htaa2a49d92016-03-04 02:51:39 -08002499 EXPECT_TRUE(CreateAnswer() == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002500}
2501
2502// Test that an answer contains the correct media content descriptions when no
2503// constraints have been set.
2504TEST_F(WebRtcSessionTest, CreateAnswerWithoutConstraintsOrStreams) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002505 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002506 // Create a remote offer with audio and video content.
kwibergd1fe2812016-04-27 06:47:29 -07002507 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002508 SetRemoteDescriptionWithoutError(offer.release());
kwibergd1fe2812016-04-27 06:47:29 -07002509 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002510 const cricket::ContentInfo* content =
2511 cricket::GetFirstAudioContent(answer->description());
2512 ASSERT_TRUE(content != NULL);
2513 EXPECT_FALSE(content->rejected);
2514
2515 content = cricket::GetFirstVideoContent(answer->description());
2516 ASSERT_TRUE(content != NULL);
2517 EXPECT_FALSE(content->rejected);
2518}
2519
2520// Test that an answer contains the correct media content descriptions when no
2521// constraints have been set and the offer only contain audio.
2522TEST_F(WebRtcSessionTest, CreateAudioAnswerWithoutConstraintsOrStreams) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002523 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002524 // Create a remote offer with audio only.
2525 cricket::MediaSessionOptions options;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00002526
kwibergd1fe2812016-04-27 06:47:29 -07002527 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer(options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002528 ASSERT_TRUE(cricket::GetFirstVideoContent(offer->description()) == NULL);
2529 ASSERT_TRUE(cricket::GetFirstAudioContent(offer->description()) != NULL);
2530
2531 SetRemoteDescriptionWithoutError(offer.release());
kwibergd1fe2812016-04-27 06:47:29 -07002532 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002533 const cricket::ContentInfo* content =
2534 cricket::GetFirstAudioContent(answer->description());
2535 ASSERT_TRUE(content != NULL);
2536 EXPECT_FALSE(content->rejected);
2537
2538 EXPECT_TRUE(cricket::GetFirstVideoContent(answer->description()) == NULL);
2539}
2540
2541// Test that an answer contains the correct media content descriptions when no
2542// constraints have been set.
2543TEST_F(WebRtcSessionTest, CreateAnswerWithoutConstraints) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002544 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002545 // Create a remote offer with audio and video content.
kwibergd1fe2812016-04-27 06:47:29 -07002546 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002547 SetRemoteDescriptionWithoutError(offer.release());
2548 // Test with a stream with tracks.
deadbeefab9b2d12015-10-14 11:33:11 -07002549 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002550 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002551 const cricket::ContentInfo* content =
2552 cricket::GetFirstAudioContent(answer->description());
2553 ASSERT_TRUE(content != NULL);
2554 EXPECT_FALSE(content->rejected);
2555
2556 content = cricket::GetFirstVideoContent(answer->description());
2557 ASSERT_TRUE(content != NULL);
2558 EXPECT_FALSE(content->rejected);
2559}
2560
2561// Test that an answer contains the correct media content descriptions when
2562// constraints have been set but no stream is sent.
2563TEST_F(WebRtcSessionTest, CreateAnswerWithConstraintsWithoutStreams) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002564 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002565 // Create a remote offer with audio and video content.
kwibergd1fe2812016-04-27 06:47:29 -07002566 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002567 SetRemoteDescriptionWithoutError(offer.release());
2568
htaa2a49d92016-03-04 02:51:39 -08002569 cricket::MediaSessionOptions session_options;
2570 session_options.recv_audio = false;
2571 session_options.recv_video = false;
kwibergd1fe2812016-04-27 06:47:29 -07002572 std::unique_ptr<SessionDescriptionInterface> answer(
htaa2a49d92016-03-04 02:51:39 -08002573 CreateAnswer(session_options));
2574
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002575 const cricket::ContentInfo* content =
2576 cricket::GetFirstAudioContent(answer->description());
2577 ASSERT_TRUE(content != NULL);
2578 EXPECT_TRUE(content->rejected);
2579
2580 content = cricket::GetFirstVideoContent(answer->description());
2581 ASSERT_TRUE(content != NULL);
2582 EXPECT_TRUE(content->rejected);
2583}
2584
2585// Test that an answer contains the correct media content descriptions when
2586// constraints have been set and streams are sent.
2587TEST_F(WebRtcSessionTest, CreateAnswerWithConstraints) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002588 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002589 // Create a remote offer with audio and video content.
kwibergd1fe2812016-04-27 06:47:29 -07002590 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002591 SetRemoteDescriptionWithoutError(offer.release());
2592
htaa2a49d92016-03-04 02:51:39 -08002593 cricket::MediaSessionOptions options;
2594 options.recv_audio = false;
2595 options.recv_video = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002596
2597 // Test with a stream with tracks.
deadbeefab9b2d12015-10-14 11:33:11 -07002598 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002599 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer(options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002600
2601 // TODO(perkj): Should the direction be set to SEND_ONLY?
2602 const cricket::ContentInfo* content =
2603 cricket::GetFirstAudioContent(answer->description());
2604 ASSERT_TRUE(content != NULL);
2605 EXPECT_FALSE(content->rejected);
2606
2607 // TODO(perkj): Should the direction be set to SEND_ONLY?
2608 content = cricket::GetFirstVideoContent(answer->description());
2609 ASSERT_TRUE(content != NULL);
2610 EXPECT_FALSE(content->rejected);
2611}
2612
2613TEST_F(WebRtcSessionTest, CreateOfferWithoutCNCodecs) {
2614 AddCNCodecs();
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002615 Init();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002616 PeerConnectionInterface::RTCOfferAnswerOptions options;
2617 options.offer_to_receive_audio =
2618 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
2619 options.voice_activity_detection = false;
2620
kwibergd1fe2812016-04-27 06:47:29 -07002621 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer(options));
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002622
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002623 const cricket::ContentInfo* content =
2624 cricket::GetFirstAudioContent(offer->description());
2625 EXPECT_TRUE(content != NULL);
2626 EXPECT_TRUE(VerifyNoCNCodecs(content));
2627}
2628
2629TEST_F(WebRtcSessionTest, CreateAnswerWithoutCNCodecs) {
2630 AddCNCodecs();
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002631 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002632 // Create a remote offer with audio and video content.
kwibergd1fe2812016-04-27 06:47:29 -07002633 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002634 SetRemoteDescriptionWithoutError(offer.release());
2635
htaa2a49d92016-03-04 02:51:39 -08002636 cricket::MediaSessionOptions options;
2637 options.vad_enabled = false;
kwibergd1fe2812016-04-27 06:47:29 -07002638 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer(options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002639 const cricket::ContentInfo* content =
2640 cricket::GetFirstAudioContent(answer->description());
2641 ASSERT_TRUE(content != NULL);
2642 EXPECT_TRUE(VerifyNoCNCodecs(content));
2643}
2644
2645// This test verifies the call setup when remote answer with audio only and
2646// later updates with video.
2647TEST_F(WebRtcSessionTest, TestAVOfferWithAudioOnlyAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002648 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002649 EXPECT_TRUE(media_engine_->GetVideoChannel(0) == NULL);
2650 EXPECT_TRUE(media_engine_->GetVoiceChannel(0) == NULL);
2651
deadbeefab9b2d12015-10-14 11:33:11 -07002652 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002653 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002654
2655 cricket::MediaSessionOptions options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002656 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer, options);
2657
2658 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
2659 // and answer;
2660 SetLocalDescriptionWithoutError(offer);
2661 SetRemoteDescriptionWithoutError(answer);
2662
2663 video_channel_ = media_engine_->GetVideoChannel(0);
2664 voice_channel_ = media_engine_->GetVoiceChannel(0);
2665
2666 ASSERT_TRUE(video_channel_ == NULL);
2667
2668 ASSERT_EQ(0u, voice_channel_->recv_streams().size());
2669 ASSERT_EQ(1u, voice_channel_->send_streams().size());
2670 EXPECT_EQ(kAudioTrack1, voice_channel_->send_streams()[0].id);
2671
2672 // Let the remote end update the session descriptions, with Audio and Video.
deadbeefab9b2d12015-10-14 11:33:11 -07002673 SendAudioVideoStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002674 CreateAndSetRemoteOfferAndLocalAnswer();
2675
2676 video_channel_ = media_engine_->GetVideoChannel(0);
2677 voice_channel_ = media_engine_->GetVoiceChannel(0);
2678
2679 ASSERT_TRUE(video_channel_ != NULL);
2680 ASSERT_TRUE(voice_channel_ != NULL);
2681
2682 ASSERT_EQ(1u, video_channel_->recv_streams().size());
2683 ASSERT_EQ(1u, video_channel_->send_streams().size());
2684 EXPECT_EQ(kVideoTrack2, video_channel_->recv_streams()[0].id);
2685 EXPECT_EQ(kVideoTrack2, video_channel_->send_streams()[0].id);
2686 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
2687 ASSERT_EQ(1u, voice_channel_->send_streams().size());
2688 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
2689 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
2690
2691 // Change session back to audio only.
deadbeefab9b2d12015-10-14 11:33:11 -07002692 SendAudioOnlyStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002693 CreateAndSetRemoteOfferAndLocalAnswer();
2694
2695 EXPECT_EQ(0u, video_channel_->recv_streams().size());
2696 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
2697 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
2698 ASSERT_EQ(1u, voice_channel_->send_streams().size());
2699 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
2700}
2701
2702// This test verifies the call setup when remote answer with video only and
2703// later updates with audio.
2704TEST_F(WebRtcSessionTest, TestAVOfferWithVideoOnlyAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002705 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002706 EXPECT_TRUE(media_engine_->GetVideoChannel(0) == NULL);
2707 EXPECT_TRUE(media_engine_->GetVoiceChannel(0) == NULL);
deadbeefab9b2d12015-10-14 11:33:11 -07002708 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002709 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002710
2711 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002712 options.recv_audio = false;
2713 options.recv_video = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002714 SessionDescriptionInterface* answer = CreateRemoteAnswer(
2715 offer, options, cricket::SEC_ENABLED);
2716
2717 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
2718 // and answer.
2719 SetLocalDescriptionWithoutError(offer);
2720 SetRemoteDescriptionWithoutError(answer);
2721
2722 video_channel_ = media_engine_->GetVideoChannel(0);
2723 voice_channel_ = media_engine_->GetVoiceChannel(0);
2724
2725 ASSERT_TRUE(voice_channel_ == NULL);
2726 ASSERT_TRUE(video_channel_ != NULL);
2727
2728 EXPECT_EQ(0u, video_channel_->recv_streams().size());
2729 ASSERT_EQ(1u, video_channel_->send_streams().size());
2730 EXPECT_EQ(kVideoTrack1, video_channel_->send_streams()[0].id);
2731
2732 // Update the session descriptions, with Audio and Video.
deadbeefab9b2d12015-10-14 11:33:11 -07002733 SendAudioVideoStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002734 CreateAndSetRemoteOfferAndLocalAnswer();
2735
2736 voice_channel_ = media_engine_->GetVoiceChannel(0);
2737 ASSERT_TRUE(voice_channel_ != NULL);
2738
2739 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
2740 ASSERT_EQ(1u, voice_channel_->send_streams().size());
2741 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
2742 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
2743
2744 // Change session back to video only.
deadbeefab9b2d12015-10-14 11:33:11 -07002745 SendVideoOnlyStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002746 CreateAndSetRemoteOfferAndLocalAnswer();
2747
2748 video_channel_ = media_engine_->GetVideoChannel(0);
2749 voice_channel_ = media_engine_->GetVoiceChannel(0);
2750
2751 ASSERT_EQ(1u, video_channel_->recv_streams().size());
2752 EXPECT_EQ(kVideoTrack2, video_channel_->recv_streams()[0].id);
2753 ASSERT_EQ(1u, video_channel_->send_streams().size());
2754 EXPECT_EQ(kVideoTrack2, video_channel_->send_streams()[0].id);
2755}
2756
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002757TEST_F(WebRtcSessionTest, VerifyCryptoParamsInSDP) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002758 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002759 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002760 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002761 VerifyCryptoParams(offer->description());
2762 SetRemoteDescriptionWithoutError(offer.release());
kwibergd1fe2812016-04-27 06:47:29 -07002763 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002764 VerifyCryptoParams(answer->description());
2765}
2766
2767TEST_F(WebRtcSessionTest, VerifyNoCryptoParamsInSDP) {
wu@webrtc.org97077a32013-10-25 21:18:33 +00002768 options_.disable_encryption = true;
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002769 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002770 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002771 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002772 VerifyNoCryptoParams(offer->description(), false);
2773}
2774
2775TEST_F(WebRtcSessionTest, VerifyAnswerFromNonCryptoOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002776 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002777 VerifyAnswerFromNonCryptoOffer();
2778}
2779
2780TEST_F(WebRtcSessionTest, VerifyAnswerFromCryptoOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002781 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002782 VerifyAnswerFromCryptoOffer();
2783}
2784
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002785// This test verifies that setLocalDescription fails if
2786// no a=ice-ufrag and a=ice-pwd lines are present in the SDP.
2787TEST_F(WebRtcSessionTest, TestSetLocalDescriptionWithoutIce) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002788 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002789 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002790 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002791
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002792 std::string sdp;
2793 RemoveIceUfragPwdLines(offer.get(), &sdp);
2794 SessionDescriptionInterface* modified_offer =
2795 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002796 SetLocalDescriptionOfferExpectError(kSdpWithoutIceUfragPwd, modified_offer);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002797}
2798
2799// This test verifies that setRemoteDescription fails if
2800// no a=ice-ufrag and a=ice-pwd lines are present in the SDP.
2801TEST_F(WebRtcSessionTest, TestSetRemoteDescriptionWithoutIce) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002802 Init();
kwibergd1fe2812016-04-27 06:47:29 -07002803 std::unique_ptr<SessionDescriptionInterface> offer(CreateRemoteOffer());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002804 std::string sdp;
2805 RemoveIceUfragPwdLines(offer.get(), &sdp);
2806 SessionDescriptionInterface* modified_offer =
2807 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002808 SetRemoteDescriptionOfferExpectError(kSdpWithoutIceUfragPwd, modified_offer);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002809}
2810
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002811// This test verifies that setLocalDescription fails if local offer has
2812// too short ice ufrag and pwd strings.
2813TEST_F(WebRtcSessionTest, TestSetLocalDescriptionInvalidIceCredentials) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002814 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002815 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002816 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002817 // Modifying ice ufrag and pwd in local offer with strings smaller than the
2818 // recommended values of 4 and 22 bytes respectively.
deadbeef0ed85b22016-02-23 17:24:52 -08002819 SetIceUfragPwd(offer.get(), "ice", "icepwd");
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002820 std::string error;
deadbeef0ed85b22016-02-23 17:24:52 -08002821 EXPECT_FALSE(session_->SetLocalDescription(offer.release(), &error));
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002822
2823 // Test with string greater than 256.
deadbeef0ed85b22016-02-23 17:24:52 -08002824 offer.reset(CreateOffer());
2825 SetIceUfragPwd(offer.get(), kTooLongIceUfragPwd, kTooLongIceUfragPwd);
2826 EXPECT_FALSE(session_->SetLocalDescription(offer.release(), &error));
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002827}
2828
2829// This test verifies that setRemoteDescription fails if remote offer has
2830// too short ice ufrag and pwd strings.
2831TEST_F(WebRtcSessionTest, TestSetRemoteDescriptionInvalidIceCredentials) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002832 Init();
kwibergd1fe2812016-04-27 06:47:29 -07002833 std::unique_ptr<SessionDescriptionInterface> offer(CreateRemoteOffer());
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002834 // Modifying ice ufrag and pwd in remote offer with strings smaller than the
2835 // recommended values of 4 and 22 bytes respectively.
deadbeef0ed85b22016-02-23 17:24:52 -08002836 SetIceUfragPwd(offer.get(), "ice", "icepwd");
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002837 std::string error;
deadbeef0ed85b22016-02-23 17:24:52 -08002838 EXPECT_FALSE(session_->SetRemoteDescription(offer.release(), &error));
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002839
deadbeef0ed85b22016-02-23 17:24:52 -08002840 offer.reset(CreateRemoteOffer());
2841 SetIceUfragPwd(offer.get(), kTooLongIceUfragPwd, kTooLongIceUfragPwd);
2842 EXPECT_FALSE(session_->SetRemoteDescription(offer.release(), &error));
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002843}
2844
Honghai Zhang04e91462015-12-11 14:26:22 -08002845// Test that if the remote offer indicates the peer requested ICE restart (via
2846// a new ufrag or pwd), the old ICE candidates are not copied, and vice versa.
2847TEST_F(WebRtcSessionTest, TestSetRemoteOfferWithIceRestart) {
honghaiz503726c2015-07-31 12:37:38 -07002848 Init();
honghaiz503726c2015-07-31 12:37:38 -07002849
2850 // Create the first offer.
kwibergd1fe2812016-04-27 06:47:29 -07002851 std::unique_ptr<SessionDescriptionInterface> offer(CreateRemoteOffer());
deadbeef0ed85b22016-02-23 17:24:52 -08002852 SetIceUfragPwd(offer.get(), "0123456789012345", "abcdefghijklmnopqrstuvwx");
honghaiz503726c2015-07-31 12:37:38 -07002853 cricket::Candidate candidate1(1, "udp", rtc::SocketAddress("1.1.1.1", 5000),
2854 0, "", "", "relay", 0, "");
2855 JsepIceCandidate ice_candidate1(kMediaContentName0, kMediaContentIndex0,
2856 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002857 EXPECT_TRUE(offer->AddCandidate(&ice_candidate1));
2858 SetRemoteDescriptionWithoutError(offer.release());
honghaiz503726c2015-07-31 12:37:38 -07002859 EXPECT_EQ(1, session_->remote_description()->candidates(0)->count());
2860
2861 // The second offer has the same ufrag and pwd but different address.
deadbeef0ed85b22016-02-23 17:24:52 -08002862 offer.reset(CreateRemoteOffer());
2863 SetIceUfragPwd(offer.get(), "0123456789012345", "abcdefghijklmnopqrstuvwx");
honghaiz503726c2015-07-31 12:37:38 -07002864 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 6000));
2865 JsepIceCandidate ice_candidate2(kMediaContentName0, kMediaContentIndex0,
2866 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002867 EXPECT_TRUE(offer->AddCandidate(&ice_candidate2));
2868 SetRemoteDescriptionWithoutError(offer.release());
honghaiz503726c2015-07-31 12:37:38 -07002869 EXPECT_EQ(2, session_->remote_description()->candidates(0)->count());
2870
2871 // The third offer has a different ufrag and different address.
deadbeef0ed85b22016-02-23 17:24:52 -08002872 offer.reset(CreateRemoteOffer());
2873 SetIceUfragPwd(offer.get(), "0123456789012333", "abcdefghijklmnopqrstuvwx");
honghaiz503726c2015-07-31 12:37:38 -07002874 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 7000));
2875 JsepIceCandidate ice_candidate3(kMediaContentName0, kMediaContentIndex0,
2876 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002877 EXPECT_TRUE(offer->AddCandidate(&ice_candidate3));
2878 SetRemoteDescriptionWithoutError(offer.release());
honghaiz503726c2015-07-31 12:37:38 -07002879 EXPECT_EQ(1, session_->remote_description()->candidates(0)->count());
2880
2881 // The fourth offer has no candidate but a different ufrag/pwd.
deadbeef0ed85b22016-02-23 17:24:52 -08002882 offer.reset(CreateRemoteOffer());
2883 SetIceUfragPwd(offer.get(), "0123456789012444", "abcdefghijklmnopqrstuvyz");
2884 SetRemoteDescriptionWithoutError(offer.release());
honghaiz503726c2015-07-31 12:37:38 -07002885 EXPECT_EQ(0, session_->remote_description()->candidates(0)->count());
2886}
2887
Honghai Zhang04e91462015-12-11 14:26:22 -08002888// Test that if the remote answer indicates the peer requested ICE restart (via
2889// a new ufrag or pwd), the old ICE candidates are not copied, and vice versa.
2890TEST_F(WebRtcSessionTest, TestSetRemoteAnswerWithIceRestart) {
2891 Init();
2892 SessionDescriptionInterface* offer = CreateOffer();
2893 SetLocalDescriptionWithoutError(offer);
Honghai Zhang04e91462015-12-11 14:26:22 -08002894
2895 // Create the first answer.
kwibergd1fe2812016-04-27 06:47:29 -07002896 std::unique_ptr<JsepSessionDescription> answer(CreateRemoteAnswer(offer));
deadbeef0ed85b22016-02-23 17:24:52 -08002897 answer->set_type(JsepSessionDescription::kPrAnswer);
2898 SetIceUfragPwd(answer.get(), "0123456789012345", "abcdefghijklmnopqrstuvwx");
Honghai Zhang04e91462015-12-11 14:26:22 -08002899 cricket::Candidate candidate1(1, "udp", rtc::SocketAddress("1.1.1.1", 5000),
2900 0, "", "", "relay", 0, "");
2901 JsepIceCandidate ice_candidate1(kMediaContentName0, kMediaContentIndex0,
2902 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002903 EXPECT_TRUE(answer->AddCandidate(&ice_candidate1));
2904 SetRemoteDescriptionWithoutError(answer.release());
Honghai Zhang04e91462015-12-11 14:26:22 -08002905 EXPECT_EQ(1, session_->remote_description()->candidates(0)->count());
2906
2907 // The second answer has the same ufrag and pwd but different address.
deadbeef0ed85b22016-02-23 17:24:52 -08002908 answer.reset(CreateRemoteAnswer(offer));
2909 answer->set_type(JsepSessionDescription::kPrAnswer);
2910 SetIceUfragPwd(answer.get(), "0123456789012345", "abcdefghijklmnopqrstuvwx");
Honghai Zhang04e91462015-12-11 14:26:22 -08002911 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 6000));
2912 JsepIceCandidate ice_candidate2(kMediaContentName0, kMediaContentIndex0,
2913 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002914 EXPECT_TRUE(answer->AddCandidate(&ice_candidate2));
2915 SetRemoteDescriptionWithoutError(answer.release());
Honghai Zhang04e91462015-12-11 14:26:22 -08002916 EXPECT_EQ(2, session_->remote_description()->candidates(0)->count());
2917
2918 // The third answer has a different ufrag and different address.
deadbeef0ed85b22016-02-23 17:24:52 -08002919 answer.reset(CreateRemoteAnswer(offer));
2920 answer->set_type(JsepSessionDescription::kPrAnswer);
2921 SetIceUfragPwd(answer.get(), "0123456789012333", "abcdefghijklmnopqrstuvwx");
Honghai Zhang04e91462015-12-11 14:26:22 -08002922 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 7000));
2923 JsepIceCandidate ice_candidate3(kMediaContentName0, kMediaContentIndex0,
2924 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002925 EXPECT_TRUE(answer->AddCandidate(&ice_candidate3));
2926 SetRemoteDescriptionWithoutError(answer.release());
Honghai Zhang04e91462015-12-11 14:26:22 -08002927 EXPECT_EQ(1, session_->remote_description()->candidates(0)->count());
2928
2929 // The fourth answer has no candidate but a different ufrag/pwd.
deadbeef0ed85b22016-02-23 17:24:52 -08002930 answer.reset(CreateRemoteAnswer(offer));
2931 answer->set_type(JsepSessionDescription::kPrAnswer);
2932 SetIceUfragPwd(answer.get(), "0123456789012444", "abcdefghijklmnopqrstuvyz");
2933 SetRemoteDescriptionWithoutError(answer.release());
Honghai Zhang04e91462015-12-11 14:26:22 -08002934 EXPECT_EQ(0, session_->remote_description()->candidates(0)->count());
2935}
2936
Donald Curtisd4f769d2015-05-28 09:48:21 -07002937// Test that candidates sent to the "video" transport do not get pushed down to
deadbeefd59daf82015-10-14 15:02:44 -07002938// the "audio" transport channel when bundling.
Donald Curtisd4f769d2015-05-28 09:48:21 -07002939TEST_F(WebRtcSessionTest, TestIgnoreCandidatesForUnusedTransportWhenBundling) {
2940 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
2941
2942 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyBalanced);
deadbeefab9b2d12015-10-14 11:33:11 -07002943 SendAudioVideoStream1();
Donald Curtisd4f769d2015-05-28 09:48:21 -07002944
2945 PeerConnectionInterface::RTCOfferAnswerOptions options;
2946 options.use_rtp_mux = true;
2947
2948 SessionDescriptionInterface* offer = CreateRemoteOffer();
2949 SetRemoteDescriptionWithoutError(offer);
2950
htaa2a49d92016-03-04 02:51:39 -08002951 SessionDescriptionInterface* answer = CreateAnswer();
Donald Curtisd4f769d2015-05-28 09:48:21 -07002952 SetLocalDescriptionWithoutError(answer);
2953
deadbeefcbecd352015-09-23 11:50:27 -07002954 EXPECT_EQ(session_->voice_rtp_transport_channel(),
2955 session_->video_rtp_transport_channel());
Donald Curtisd4f769d2015-05-28 09:48:21 -07002956
deadbeefcbecd352015-09-23 11:50:27 -07002957 cricket::BaseChannel* voice_channel = session_->voice_channel();
2958 ASSERT(voice_channel != NULL);
Donald Curtisd4f769d2015-05-28 09:48:21 -07002959
2960 // Checks if one of the transport channels contains a connection using a given
2961 // port.
deadbeefcbecd352015-09-23 11:50:27 -07002962 auto connection_with_remote_port = [this, voice_channel](int port) {
deadbeefd59daf82015-10-14 15:02:44 -07002963 SessionStats stats;
deadbeefcbecd352015-09-23 11:50:27 -07002964 session_->GetChannelTransportStats(voice_channel, &stats);
2965 for (auto& kv : stats.transport_stats) {
2966 for (auto& chan_stat : kv.second.channel_stats) {
2967 for (auto& conn_info : chan_stat.connection_infos) {
2968 if (conn_info.remote_candidate.address().port() == port) {
2969 return true;
2970 }
Donald Curtisd4f769d2015-05-28 09:48:21 -07002971 }
2972 }
2973 }
2974 return false;
2975 };
2976
2977 EXPECT_FALSE(connection_with_remote_port(5000));
2978 EXPECT_FALSE(connection_with_remote_port(5001));
2979 EXPECT_FALSE(connection_with_remote_port(6000));
2980
2981 // The way the *_WAIT checks work is they only wait if the condition fails,
2982 // which does not help in the case where state is not changing. This is
2983 // problematic in this test since we want to verify that adding a video
2984 // candidate does _not_ change state. So we interleave candidates and assume
2985 // that messages are executed in the order they were posted.
2986
2987 // First audio candidate.
2988 cricket::Candidate candidate0;
2989 candidate0.set_address(rtc::SocketAddress("1.1.1.1", 5000));
2990 candidate0.set_component(1);
2991 candidate0.set_protocol("udp");
2992 JsepIceCandidate ice_candidate0(kMediaContentName0, kMediaContentIndex0,
2993 candidate0);
2994 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate0));
2995
2996 // Video candidate.
2997 cricket::Candidate candidate1;
2998 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 6000));
2999 candidate1.set_component(1);
3000 candidate1.set_protocol("udp");
3001 JsepIceCandidate ice_candidate1(kMediaContentName1, kMediaContentIndex1,
3002 candidate1);
3003 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate1));
3004
3005 // Second audio candidate.
3006 cricket::Candidate candidate2;
3007 candidate2.set_address(rtc::SocketAddress("1.1.1.1", 5001));
3008 candidate2.set_component(1);
3009 candidate2.set_protocol("udp");
3010 JsepIceCandidate ice_candidate2(kMediaContentName0, kMediaContentIndex0,
3011 candidate2);
3012 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate2));
3013
3014 EXPECT_TRUE_WAIT(connection_with_remote_port(5000), 1000);
3015 EXPECT_TRUE_WAIT(connection_with_remote_port(5001), 1000);
3016
3017 // No need here for a _WAIT check since we are checking that state hasn't
3018 // changed: if this is false we would be doing waits for nothing and if this
3019 // is true then there will be no messages processed anyways.
3020 EXPECT_FALSE(connection_with_remote_port(6000));
3021}
3022
deadbeefcbecd352015-09-23 11:50:27 -07003023// kBundlePolicyBalanced BUNDLE policy and answer contains BUNDLE.
Donald Curtis0e209b02015-03-24 09:29:54 -07003024TEST_F(WebRtcSessionTest, TestBalancedBundleInAnswer) {
3025 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyBalanced);
deadbeefab9b2d12015-10-14 11:33:11 -07003026 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00003027
3028 PeerConnectionInterface::RTCOfferAnswerOptions options;
3029 options.use_rtp_mux = true;
3030
3031 SessionDescriptionInterface* offer = CreateOffer(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003032 SetLocalDescriptionWithoutError(offer);
Donald Curtis0e209b02015-03-24 09:29:54 -07003033
deadbeefcbecd352015-09-23 11:50:27 -07003034 EXPECT_NE(session_->voice_rtp_transport_channel(),
3035 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003036
deadbeefab9b2d12015-10-14 11:33:11 -07003037 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003038 SessionDescriptionInterface* answer =
3039 CreateRemoteAnswer(session_->local_description());
3040 SetRemoteDescriptionWithoutError(answer);
3041
deadbeefcbecd352015-09-23 11:50:27 -07003042 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3043 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003044}
3045
deadbeefcbecd352015-09-23 11:50:27 -07003046// kBundlePolicyBalanced BUNDLE policy but no BUNDLE in the answer.
Donald Curtis0e209b02015-03-24 09:29:54 -07003047TEST_F(WebRtcSessionTest, TestBalancedNoBundleInAnswer) {
3048 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyBalanced);
deadbeefab9b2d12015-10-14 11:33:11 -07003049 SendAudioVideoStream1();
Peter Thatcher4eddf182015-04-30 10:55:59 -07003050
Donald Curtis0e209b02015-03-24 09:29:54 -07003051 PeerConnectionInterface::RTCOfferAnswerOptions options;
3052 options.use_rtp_mux = true;
3053
3054 SessionDescriptionInterface* offer = CreateOffer(options);
3055 SetLocalDescriptionWithoutError(offer);
3056
deadbeefcbecd352015-09-23 11:50:27 -07003057 EXPECT_NE(session_->voice_rtp_transport_channel(),
3058 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003059
deadbeefab9b2d12015-10-14 11:33:11 -07003060 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003061
3062 // Remove BUNDLE from the answer.
kwibergd1fe2812016-04-27 06:47:29 -07003063 std::unique_ptr<SessionDescriptionInterface> answer(
Donald Curtis0e209b02015-03-24 09:29:54 -07003064 CreateRemoteAnswer(session_->local_description()));
3065 cricket::SessionDescription* answer_copy = answer->description()->Copy();
3066 answer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
3067 JsepSessionDescription* modified_answer =
3068 new JsepSessionDescription(JsepSessionDescription::kAnswer);
3069 modified_answer->Initialize(answer_copy, "1", "1");
3070 SetRemoteDescriptionWithoutError(modified_answer); //
3071
deadbeefcbecd352015-09-23 11:50:27 -07003072 EXPECT_NE(session_->voice_rtp_transport_channel(),
3073 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003074}
3075
3076// kBundlePolicyMaxBundle policy with BUNDLE in the answer.
3077TEST_F(WebRtcSessionTest, TestMaxBundleBundleInAnswer) {
3078 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003079 SendAudioVideoStream1();
Donald Curtis0e209b02015-03-24 09:29:54 -07003080
3081 PeerConnectionInterface::RTCOfferAnswerOptions options;
3082 options.use_rtp_mux = true;
3083
3084 SessionDescriptionInterface* offer = CreateOffer(options);
3085 SetLocalDescriptionWithoutError(offer);
3086
deadbeefcbecd352015-09-23 11:50:27 -07003087 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3088 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003089
deadbeefab9b2d12015-10-14 11:33:11 -07003090 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003091 SessionDescriptionInterface* answer =
3092 CreateRemoteAnswer(session_->local_description());
3093 SetRemoteDescriptionWithoutError(answer);
3094
deadbeefcbecd352015-09-23 11:50:27 -07003095 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3096 session_->video_rtp_transport_channel());
3097}
3098
3099// kBundlePolicyMaxBundle policy with BUNDLE in the answer, but no
3100// audio content in the answer.
3101TEST_F(WebRtcSessionTest, TestMaxBundleRejectAudio) {
3102 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003103 SendAudioVideoStream1();
deadbeefcbecd352015-09-23 11:50:27 -07003104
3105 PeerConnectionInterface::RTCOfferAnswerOptions options;
3106 options.use_rtp_mux = true;
3107
3108 SessionDescriptionInterface* offer = CreateOffer(options);
3109 SetLocalDescriptionWithoutError(offer);
3110
3111 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3112 session_->video_rtp_transport_channel());
3113
deadbeefab9b2d12015-10-14 11:33:11 -07003114 SendAudioVideoStream2();
deadbeefcbecd352015-09-23 11:50:27 -07003115 cricket::MediaSessionOptions recv_options;
3116 recv_options.recv_audio = false;
3117 recv_options.recv_video = true;
3118 SessionDescriptionInterface* answer =
3119 CreateRemoteAnswer(session_->local_description(), recv_options);
3120 SetRemoteDescriptionWithoutError(answer);
3121
deadbeefd59daf82015-10-14 15:02:44 -07003122 EXPECT_TRUE(nullptr == session_->voice_channel());
3123 EXPECT_TRUE(nullptr != session_->video_rtp_transport_channel());
deadbeefcbecd352015-09-23 11:50:27 -07003124
deadbeefd59daf82015-10-14 15:02:44 -07003125 session_->Close();
3126 EXPECT_TRUE(nullptr == session_->voice_rtp_transport_channel());
3127 EXPECT_TRUE(nullptr == session_->voice_rtcp_transport_channel());
3128 EXPECT_TRUE(nullptr == session_->video_rtp_transport_channel());
3129 EXPECT_TRUE(nullptr == session_->video_rtcp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003130}
3131
3132// kBundlePolicyMaxBundle policy but no BUNDLE in the answer.
3133TEST_F(WebRtcSessionTest, TestMaxBundleNoBundleInAnswer) {
3134 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003135 SendAudioVideoStream1();
Peter Thatcher4eddf182015-04-30 10:55:59 -07003136
Donald Curtis0e209b02015-03-24 09:29:54 -07003137 PeerConnectionInterface::RTCOfferAnswerOptions options;
3138 options.use_rtp_mux = true;
3139
3140 SessionDescriptionInterface* offer = CreateOffer(options);
3141 SetLocalDescriptionWithoutError(offer);
3142
deadbeefcbecd352015-09-23 11:50:27 -07003143 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3144 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003145
deadbeefab9b2d12015-10-14 11:33:11 -07003146 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003147
3148 // Remove BUNDLE from the answer.
kwibergd1fe2812016-04-27 06:47:29 -07003149 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003150 CreateRemoteAnswer(session_->local_description()));
3151 cricket::SessionDescription* answer_copy = answer->description()->Copy();
3152 answer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
3153 JsepSessionDescription* modified_answer =
3154 new JsepSessionDescription(JsepSessionDescription::kAnswer);
3155 modified_answer->Initialize(answer_copy, "1", "1");
3156 SetRemoteDescriptionWithoutError(modified_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003157
deadbeefcbecd352015-09-23 11:50:27 -07003158 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3159 session_->video_rtp_transport_channel());
3160}
3161
3162// kBundlePolicyMaxBundle policy with BUNDLE in the remote offer.
3163TEST_F(WebRtcSessionTest, TestMaxBundleBundleInRemoteOffer) {
3164 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003165 SendAudioVideoStream1();
deadbeefcbecd352015-09-23 11:50:27 -07003166
3167 SessionDescriptionInterface* offer = CreateRemoteOffer();
3168 SetRemoteDescriptionWithoutError(offer);
3169
3170 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3171 session_->video_rtp_transport_channel());
3172
deadbeefab9b2d12015-10-14 11:33:11 -07003173 SendAudioVideoStream2();
htaa2a49d92016-03-04 02:51:39 -08003174 SessionDescriptionInterface* answer = CreateAnswer();
deadbeefcbecd352015-09-23 11:50:27 -07003175 SetLocalDescriptionWithoutError(answer);
3176
3177 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3178 session_->video_rtp_transport_channel());
3179}
3180
3181// kBundlePolicyMaxBundle policy but no BUNDLE in the remote offer.
3182TEST_F(WebRtcSessionTest, TestMaxBundleNoBundleInRemoteOffer) {
3183 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003184 SendAudioVideoStream1();
deadbeefcbecd352015-09-23 11:50:27 -07003185
3186 // Remove BUNDLE from the offer.
kwibergd1fe2812016-04-27 06:47:29 -07003187 std::unique_ptr<SessionDescriptionInterface> offer(CreateRemoteOffer());
deadbeefcbecd352015-09-23 11:50:27 -07003188 cricket::SessionDescription* offer_copy = offer->description()->Copy();
3189 offer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
3190 JsepSessionDescription* modified_offer =
3191 new JsepSessionDescription(JsepSessionDescription::kOffer);
3192 modified_offer->Initialize(offer_copy, "1", "1");
3193
3194 // Expect an error when applying the remote description
3195 SetRemoteDescriptionExpectError(JsepSessionDescription::kOffer,
3196 kCreateChannelFailed, modified_offer);
Donald Curtis0e209b02015-03-24 09:29:54 -07003197}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003198
Peter Thatcher4eddf182015-04-30 10:55:59 -07003199// kBundlePolicyMaxCompat bundle policy and answer contains BUNDLE.
Donald Curtis0e209b02015-03-24 09:29:54 -07003200TEST_F(WebRtcSessionTest, TestMaxCompatBundleInAnswer) {
3201 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxCompat);
deadbeefab9b2d12015-10-14 11:33:11 -07003202 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003203
Donald Curtis0e209b02015-03-24 09:29:54 -07003204 PeerConnectionInterface::RTCOfferAnswerOptions options;
3205 options.use_rtp_mux = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003206
Donald Curtis0e209b02015-03-24 09:29:54 -07003207 SessionDescriptionInterface* offer = CreateOffer(options);
3208 SetLocalDescriptionWithoutError(offer);
3209
deadbeefcbecd352015-09-23 11:50:27 -07003210 EXPECT_NE(session_->voice_rtp_transport_channel(),
3211 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003212
deadbeefab9b2d12015-10-14 11:33:11 -07003213 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003214 SessionDescriptionInterface* answer =
3215 CreateRemoteAnswer(session_->local_description());
3216 SetRemoteDescriptionWithoutError(answer);
3217
3218 // This should lead to an audio-only call but isn't implemented
3219 // correctly yet.
deadbeefcbecd352015-09-23 11:50:27 -07003220 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3221 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003222}
3223
deadbeefcbecd352015-09-23 11:50:27 -07003224// kBundlePolicyMaxCompat BUNDLE policy but no BUNDLE in the answer.
Donald Curtis0e209b02015-03-24 09:29:54 -07003225TEST_F(WebRtcSessionTest, TestMaxCompatNoBundleInAnswer) {
3226 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxCompat);
deadbeefab9b2d12015-10-14 11:33:11 -07003227 SendAudioVideoStream1();
Donald Curtis0e209b02015-03-24 09:29:54 -07003228 PeerConnectionInterface::RTCOfferAnswerOptions options;
3229 options.use_rtp_mux = true;
3230
3231 SessionDescriptionInterface* offer = CreateOffer(options);
3232 SetLocalDescriptionWithoutError(offer);
3233
deadbeefcbecd352015-09-23 11:50:27 -07003234 EXPECT_NE(session_->voice_rtp_transport_channel(),
3235 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003236
deadbeefab9b2d12015-10-14 11:33:11 -07003237 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003238
3239 // Remove BUNDLE from the answer.
kwibergd1fe2812016-04-27 06:47:29 -07003240 std::unique_ptr<SessionDescriptionInterface> answer(
Donald Curtis0e209b02015-03-24 09:29:54 -07003241 CreateRemoteAnswer(session_->local_description()));
3242 cricket::SessionDescription* answer_copy = answer->description()->Copy();
3243 answer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
3244 JsepSessionDescription* modified_answer =
3245 new JsepSessionDescription(JsepSessionDescription::kAnswer);
3246 modified_answer->Initialize(answer_copy, "1", "1");
3247 SetRemoteDescriptionWithoutError(modified_answer); //
3248
deadbeefcbecd352015-09-23 11:50:27 -07003249 EXPECT_NE(session_->voice_rtp_transport_channel(),
3250 session_->video_rtp_transport_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003251}
3252
Peter Thatcher4eddf182015-04-30 10:55:59 -07003253// kBundlePolicyMaxbundle and then we call SetRemoteDescription first.
3254TEST_F(WebRtcSessionTest, TestMaxBundleWithSetRemoteDescriptionFirst) {
3255 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003256 SendAudioVideoStream1();
Peter Thatcher4eddf182015-04-30 10:55:59 -07003257
3258 PeerConnectionInterface::RTCOfferAnswerOptions options;
3259 options.use_rtp_mux = true;
3260
3261 SessionDescriptionInterface* offer = CreateOffer(options);
3262 SetRemoteDescriptionWithoutError(offer);
3263
deadbeefcbecd352015-09-23 11:50:27 -07003264 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3265 session_->video_rtp_transport_channel());
Peter Thatcher4eddf182015-04-30 10:55:59 -07003266}
3267
skvlad6c87a672016-05-17 17:49:52 -07003268// Adding a new channel to a BUNDLE which is already connected should directly
3269// assign the bundle transport to the channel, without first setting a
3270// disconnected non-bundle transport and then replacing it. The application
3271// should not receive any changes in the ICE state.
3272TEST_F(WebRtcSessionTest, TestAddChannelToConnectedBundle) {
3273 LoopbackNetworkConfiguration config;
3274 LoopbackNetworkManager loopback_network_manager(this, config);
3275 // Both BUNDLE and RTCP-mux need to be enabled for the ICE state to remain
3276 // connected. Disabling either of these two means that we need to wait for the
3277 // answer to find out if more transports are needed.
3278 configuration_.bundle_policy =
3279 PeerConnectionInterface::kBundlePolicyMaxBundle;
3280 configuration_.rtcp_mux_policy =
3281 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3282 options_.disable_encryption = true;
3283 Init();
3284
3285 // Negotiate an audio channel with MAX_BUNDLE enabled.
3286 SendAudioOnlyStream2();
3287 SessionDescriptionInterface* offer = CreateOffer();
3288 SetLocalDescriptionWithoutError(offer);
3289 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3290 observer_.ice_gathering_state_, kIceCandidatesTimeout);
3291 std::string sdp;
3292 offer->ToString(&sdp);
3293 SessionDescriptionInterface* answer = webrtc::CreateSessionDescription(
3294 JsepSessionDescription::kAnswer, sdp, nullptr);
3295 ASSERT_TRUE(answer != NULL);
3296 SetRemoteDescriptionWithoutError(answer);
3297
3298 // Wait for the ICE state to stabilize.
3299 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3300 observer_.ice_connection_state_, kIceCandidatesTimeout);
3301 observer_.ice_connection_state_history_.clear();
3302
3303 // Now add a video channel which should be using the same bundle transport.
3304 SendAudioVideoStream2();
3305 offer = CreateOffer();
3306 offer->ToString(&sdp);
3307 SetLocalDescriptionWithoutError(offer);
3308 answer = webrtc::CreateSessionDescription(JsepSessionDescription::kAnswer,
3309 sdp, nullptr);
3310 ASSERT_TRUE(answer != NULL);
3311 SetRemoteDescriptionWithoutError(answer);
3312
3313 // Wait for ICE state to stabilize
3314 rtc::Thread::Current()->ProcessMessages(0);
3315 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3316 observer_.ice_connection_state_, kIceCandidatesTimeout);
3317
3318 // No ICE state changes are expected to happen.
3319 EXPECT_EQ(0, observer_.ice_connection_state_history_.size());
3320}
3321
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003322TEST_F(WebRtcSessionTest, TestRequireRtcpMux) {
3323 InitWithRtcpMuxPolicy(PeerConnectionInterface::kRtcpMuxPolicyRequire);
deadbeefab9b2d12015-10-14 11:33:11 -07003324 SendAudioVideoStream1();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003325
3326 PeerConnectionInterface::RTCOfferAnswerOptions options;
3327 SessionDescriptionInterface* offer = CreateOffer(options);
3328 SetLocalDescriptionWithoutError(offer);
3329
deadbeefcbecd352015-09-23 11:50:27 -07003330 EXPECT_TRUE(session_->voice_rtcp_transport_channel() == NULL);
3331 EXPECT_TRUE(session_->video_rtcp_transport_channel() == NULL);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003332
deadbeefab9b2d12015-10-14 11:33:11 -07003333 SendAudioVideoStream2();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003334 SessionDescriptionInterface* answer =
3335 CreateRemoteAnswer(session_->local_description());
3336 SetRemoteDescriptionWithoutError(answer);
3337
deadbeefcbecd352015-09-23 11:50:27 -07003338 EXPECT_TRUE(session_->voice_rtcp_transport_channel() == NULL);
3339 EXPECT_TRUE(session_->video_rtcp_transport_channel() == NULL);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003340}
3341
3342TEST_F(WebRtcSessionTest, TestNegotiateRtcpMux) {
3343 InitWithRtcpMuxPolicy(PeerConnectionInterface::kRtcpMuxPolicyNegotiate);
deadbeefab9b2d12015-10-14 11:33:11 -07003344 SendAudioVideoStream1();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003345
3346 PeerConnectionInterface::RTCOfferAnswerOptions options;
3347 SessionDescriptionInterface* offer = CreateOffer(options);
3348 SetLocalDescriptionWithoutError(offer);
3349
deadbeefcbecd352015-09-23 11:50:27 -07003350 EXPECT_TRUE(session_->voice_rtcp_transport_channel() != NULL);
3351 EXPECT_TRUE(session_->video_rtcp_transport_channel() != NULL);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003352
deadbeefab9b2d12015-10-14 11:33:11 -07003353 SendAudioVideoStream2();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003354 SessionDescriptionInterface* answer =
3355 CreateRemoteAnswer(session_->local_description());
3356 SetRemoteDescriptionWithoutError(answer);
3357
deadbeefcbecd352015-09-23 11:50:27 -07003358 EXPECT_TRUE(session_->voice_rtcp_transport_channel() == NULL);
3359 EXPECT_TRUE(session_->video_rtcp_transport_channel() == NULL);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003360}
3361
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003362// This test verifies that SetLocalDescription and SetRemoteDescription fails
3363// if BUNDLE is enabled but rtcp-mux is disabled in m-lines.
3364TEST_F(WebRtcSessionTest, TestDisabledRtcpMuxWithBundleEnabled) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003365 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003366 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00003367
3368 PeerConnectionInterface::RTCOfferAnswerOptions options;
3369 options.use_rtp_mux = true;
3370
3371 SessionDescriptionInterface* offer = CreateOffer(options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003372 std::string offer_str;
3373 offer->ToString(&offer_str);
3374 // Disable rtcp-mux
3375 const std::string rtcp_mux = "rtcp-mux";
3376 const std::string xrtcp_mux = "xrtcp-mux";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003377 rtc::replace_substrs(rtcp_mux.c_str(), rtcp_mux.length(),
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003378 xrtcp_mux.c_str(), xrtcp_mux.length(),
3379 &offer_str);
deadbeefcbecd352015-09-23 11:50:27 -07003380 JsepSessionDescription* local_offer =
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003381 new JsepSessionDescription(JsepSessionDescription::kOffer);
3382 EXPECT_TRUE((local_offer)->Initialize(offer_str, NULL));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003383 SetLocalDescriptionOfferExpectError(kBundleWithoutRtcpMux, local_offer);
deadbeefcbecd352015-09-23 11:50:27 -07003384 JsepSessionDescription* remote_offer =
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003385 new JsepSessionDescription(JsepSessionDescription::kOffer);
3386 EXPECT_TRUE((remote_offer)->Initialize(offer_str, NULL));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003387 SetRemoteDescriptionOfferExpectError(kBundleWithoutRtcpMux, remote_offer);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003388 // Trying unmodified SDP.
3389 SetLocalDescriptionWithoutError(offer);
3390}
3391
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003392TEST_F(WebRtcSessionTest, SetAudioPlayout) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003393 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003394 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003395 CreateAndSetRemoteOfferAndLocalAnswer();
3396 cricket::FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
3397 ASSERT_TRUE(channel != NULL);
3398 ASSERT_EQ(1u, channel->recv_streams().size());
Peter Boström0c4e06b2015-10-07 12:23:21 +02003399 uint32_t receive_ssrc = channel->recv_streams()[0].first_ssrc();
solenberg4bac9c52015-10-09 02:32:53 -07003400 double volume;
3401 EXPECT_TRUE(channel->GetOutputVolume(receive_ssrc, &volume));
3402 EXPECT_EQ(1, volume);
solenbergd4cec0d2015-10-09 08:55:48 -07003403 session_->SetAudioPlayout(receive_ssrc, false);
solenberg4bac9c52015-10-09 02:32:53 -07003404 EXPECT_TRUE(channel->GetOutputVolume(receive_ssrc, &volume));
3405 EXPECT_EQ(0, volume);
solenbergd4cec0d2015-10-09 08:55:48 -07003406 session_->SetAudioPlayout(receive_ssrc, true);
solenberg4bac9c52015-10-09 02:32:53 -07003407 EXPECT_TRUE(channel->GetOutputVolume(receive_ssrc, &volume));
3408 EXPECT_EQ(1, volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003409}
3410
skvlade0d46372016-04-07 22:59:22 -07003411TEST_F(WebRtcSessionTest, SetAudioMaxSendBitrate) {
skvladdc1c62c2016-03-16 19:07:43 -07003412 Init();
3413 SendAudioVideoStream1();
3414 CreateAndSetRemoteOfferAndLocalAnswer();
3415 cricket::FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
3416 ASSERT_TRUE(channel != NULL);
3417 uint32_t send_ssrc = channel->send_streams()[0].first_ssrc();
skvlade0d46372016-04-07 22:59:22 -07003418 EXPECT_EQ(-1, channel->max_bps());
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07003419 webrtc::RtpParameters params = session_->GetAudioRtpSendParameters(send_ssrc);
skvlade0d46372016-04-07 22:59:22 -07003420 EXPECT_EQ(1, params.encodings.size());
3421 EXPECT_EQ(-1, params.encodings[0].max_bitrate_bps);
3422 params.encodings[0].max_bitrate_bps = 1000;
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07003423 EXPECT_TRUE(session_->SetAudioRtpSendParameters(send_ssrc, params));
skvladdc1c62c2016-03-16 19:07:43 -07003424
skvlade0d46372016-04-07 22:59:22 -07003425 // Read back the parameters and verify they have been changed.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07003426 params = session_->GetAudioRtpSendParameters(send_ssrc);
skvlade0d46372016-04-07 22:59:22 -07003427 EXPECT_EQ(1, params.encodings.size());
3428 EXPECT_EQ(1000, params.encodings[0].max_bitrate_bps);
3429
3430 // Verify that the audio channel received the new parameters.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07003431 params = channel->GetRtpSendParameters(send_ssrc);
skvlade0d46372016-04-07 22:59:22 -07003432 EXPECT_EQ(1, params.encodings.size());
3433 EXPECT_EQ(1000, params.encodings[0].max_bitrate_bps);
3434
3435 // Verify that the global bitrate limit has not been changed.
3436 EXPECT_EQ(-1, channel->max_bps());
skvladdc1c62c2016-03-16 19:07:43 -07003437}
3438
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003439TEST_F(WebRtcSessionTest, SetAudioSend) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003440 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003441 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003442 CreateAndSetRemoteOfferAndLocalAnswer();
3443 cricket::FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
3444 ASSERT_TRUE(channel != NULL);
3445 ASSERT_EQ(1u, channel->send_streams().size());
Peter Boström0c4e06b2015-10-07 12:23:21 +02003446 uint32_t send_ssrc = channel->send_streams()[0].first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003447 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
3448
3449 cricket::AudioOptions options;
Karl Wibergbe579832015-11-10 22:34:18 +01003450 options.echo_cancellation = rtc::Optional<bool>(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003451
kwibergd1fe2812016-04-27 06:47:29 -07003452 std::unique_ptr<FakeAudioSource> source(new FakeAudioSource());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08003453 session_->SetAudioSend(send_ssrc, false, options, source.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003454 EXPECT_TRUE(channel->IsStreamMuted(send_ssrc));
Karl Wibergbe579832015-11-10 22:34:18 +01003455 EXPECT_EQ(rtc::Optional<bool>(), channel->options().echo_cancellation);
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08003456 EXPECT_TRUE(source->sink() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003457
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08003458 // This will trigger SetSink(nullptr) to the |source|.
3459 session_->SetAudioSend(send_ssrc, true, options, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003460 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
Karl Wibergbe579832015-11-10 22:34:18 +01003461 EXPECT_EQ(rtc::Optional<bool>(true), channel->options().echo_cancellation);
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08003462 EXPECT_TRUE(source->sink() == nullptr);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00003463}
3464
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08003465TEST_F(WebRtcSessionTest, AudioSourceForLocalStream) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003466 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003467 SendAudioVideoStream1();
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00003468 CreateAndSetRemoteOfferAndLocalAnswer();
3469 cricket::FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
3470 ASSERT_TRUE(channel != NULL);
3471 ASSERT_EQ(1u, channel->send_streams().size());
Peter Boström0c4e06b2015-10-07 12:23:21 +02003472 uint32_t send_ssrc = channel->send_streams()[0].first_ssrc();
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00003473
kwibergd1fe2812016-04-27 06:47:29 -07003474 std::unique_ptr<FakeAudioSource> source(new FakeAudioSource());
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00003475 cricket::AudioOptions options;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08003476 session_->SetAudioSend(send_ssrc, true, options, source.get());
3477 EXPECT_TRUE(source->sink() != nullptr);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00003478
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08003479 // Delete the |source| and it will trigger OnClose() to the sink, and this
3480 // will invalidate the |source_| pointer in the sink and prevent getting a
3481 // SetSink(nullptr) callback afterwards.
3482 source.reset();
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00003483
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08003484 // This will trigger SetSink(nullptr) if no OnClose() callback.
3485 session_->SetAudioSend(send_ssrc, true, options, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003486}
3487
3488TEST_F(WebRtcSessionTest, SetVideoPlayout) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003489 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003490 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003491 CreateAndSetRemoteOfferAndLocalAnswer();
3492 cricket::FakeVideoMediaChannel* channel = media_engine_->GetVideoChannel(0);
3493 ASSERT_TRUE(channel != NULL);
nisse08582ff2016-02-04 01:24:52 -08003494 ASSERT_LT(0u, channel->sinks().size());
3495 EXPECT_TRUE(channel->sinks().begin()->second == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003496 ASSERT_EQ(1u, channel->recv_streams().size());
Peter Boström0c4e06b2015-10-07 12:23:21 +02003497 uint32_t receive_ssrc = channel->recv_streams()[0].first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003498 cricket::FakeVideoRenderer renderer;
3499 session_->SetVideoPlayout(receive_ssrc, true, &renderer);
nisse08582ff2016-02-04 01:24:52 -08003500 EXPECT_TRUE(channel->sinks().begin()->second == &renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003501 session_->SetVideoPlayout(receive_ssrc, false, &renderer);
nisse08582ff2016-02-04 01:24:52 -08003502 EXPECT_TRUE(channel->sinks().begin()->second == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003503}
3504
skvladdc1c62c2016-03-16 19:07:43 -07003505TEST_F(WebRtcSessionTest, SetVideoMaxSendBitrate) {
3506 Init();
3507 SendAudioVideoStream1();
3508 CreateAndSetRemoteOfferAndLocalAnswer();
3509 cricket::FakeVideoMediaChannel* channel = media_engine_->GetVideoChannel(0);
3510 ASSERT_TRUE(channel != NULL);
3511 uint32_t send_ssrc = channel->send_streams()[0].first_ssrc();
3512 EXPECT_EQ(-1, channel->max_bps());
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07003513 webrtc::RtpParameters params = session_->GetVideoRtpSendParameters(send_ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07003514 EXPECT_EQ(1, params.encodings.size());
3515 EXPECT_EQ(-1, params.encodings[0].max_bitrate_bps);
3516 params.encodings[0].max_bitrate_bps = 1000;
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07003517 EXPECT_TRUE(session_->SetVideoRtpSendParameters(send_ssrc, params));
skvladdc1c62c2016-03-16 19:07:43 -07003518
3519 // Read back the parameters and verify they have been changed.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07003520 params = session_->GetVideoRtpSendParameters(send_ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07003521 EXPECT_EQ(1, params.encodings.size());
3522 EXPECT_EQ(1000, params.encodings[0].max_bitrate_bps);
3523
3524 // Verify that the video channel received the new parameters.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07003525 params = channel->GetRtpSendParameters(send_ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07003526 EXPECT_EQ(1, params.encodings.size());
3527 EXPECT_EQ(1000, params.encodings[0].max_bitrate_bps);
3528
3529 // Verify that the global bitrate limit has not been changed.
3530 EXPECT_EQ(-1, channel->max_bps());
3531}
3532
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003533TEST_F(WebRtcSessionTest, SetVideoSend) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003534 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003535 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003536 CreateAndSetRemoteOfferAndLocalAnswer();
3537 cricket::FakeVideoMediaChannel* channel = media_engine_->GetVideoChannel(0);
3538 ASSERT_TRUE(channel != NULL);
3539 ASSERT_EQ(1u, channel->send_streams().size());
Peter Boström0c4e06b2015-10-07 12:23:21 +02003540 uint32_t send_ssrc = channel->send_streams()[0].first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003541 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
3542 cricket::VideoOptions* options = NULL;
3543 session_->SetVideoSend(send_ssrc, false, options);
3544 EXPECT_TRUE(channel->IsStreamMuted(send_ssrc));
3545 session_->SetVideoSend(send_ssrc, true, options);
3546 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
3547}
3548
3549TEST_F(WebRtcSessionTest, CanNotInsertDtmf) {
3550 TestCanInsertDtmf(false);
3551}
3552
3553TEST_F(WebRtcSessionTest, CanInsertDtmf) {
3554 TestCanInsertDtmf(true);
3555}
3556
3557TEST_F(WebRtcSessionTest, InsertDtmf) {
3558 // Setup
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003559 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003560 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003561 CreateAndSetRemoteOfferAndLocalAnswer();
3562 FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
3563 EXPECT_EQ(0U, channel->dtmf_info_queue().size());
3564
3565 // Insert DTMF
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003566 const int expected_duration = 90;
3567 session_->InsertDtmf(kAudioTrack1, 0, expected_duration);
3568 session_->InsertDtmf(kAudioTrack1, 1, expected_duration);
3569 session_->InsertDtmf(kAudioTrack1, 2, expected_duration);
3570
3571 // Verify
3572 ASSERT_EQ(3U, channel->dtmf_info_queue().size());
Peter Boström0c4e06b2015-10-07 12:23:21 +02003573 const uint32_t send_ssrc = channel->send_streams()[0].first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003574 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[0], send_ssrc, 0,
solenberg1d63dd02015-12-02 12:35:09 -08003575 expected_duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003576 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[1], send_ssrc, 1,
solenberg1d63dd02015-12-02 12:35:09 -08003577 expected_duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003578 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[2], send_ssrc, 2,
solenberg1d63dd02015-12-02 12:35:09 -08003579 expected_duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003580}
3581
deadbeefd59daf82015-10-14 15:02:44 -07003582// This test verifies the |initial_offerer| flag when session initiates the
3583// call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003584TEST_F(WebRtcSessionTest, TestInitiatorFlagAsOriginator) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003585 Init();
deadbeefd59daf82015-10-14 15:02:44 -07003586 EXPECT_FALSE(session_->initial_offerer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00003587 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003588 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
3589 SetLocalDescriptionWithoutError(offer);
deadbeefd59daf82015-10-14 15:02:44 -07003590 EXPECT_TRUE(session_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003591 SetRemoteDescriptionWithoutError(answer);
deadbeefd59daf82015-10-14 15:02:44 -07003592 EXPECT_TRUE(session_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003593}
3594
deadbeefd59daf82015-10-14 15:02:44 -07003595// This test verifies the |initial_offerer| flag when session receives the call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003596TEST_F(WebRtcSessionTest, TestInitiatorFlagAsReceiver) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003597 Init();
deadbeefd59daf82015-10-14 15:02:44 -07003598 EXPECT_FALSE(session_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003599 SessionDescriptionInterface* offer = CreateRemoteOffer();
3600 SetRemoteDescriptionWithoutError(offer);
htaa2a49d92016-03-04 02:51:39 -08003601 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003602
deadbeefd59daf82015-10-14 15:02:44 -07003603 EXPECT_FALSE(session_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003604 SetLocalDescriptionWithoutError(answer);
deadbeefd59daf82015-10-14 15:02:44 -07003605 EXPECT_FALSE(session_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003606}
3607
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003608// Verifing local offer and remote answer have matching m-lines as per RFC 3264.
3609TEST_F(WebRtcSessionTest, TestIncorrectMLinesInRemoteAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003610 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003611 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00003612 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003613 SetLocalDescriptionWithoutError(offer);
kwibergd1fe2812016-04-27 06:47:29 -07003614 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003615 CreateRemoteAnswer(session_->local_description()));
3616
3617 cricket::SessionDescription* answer_copy = answer->description()->Copy();
3618 answer_copy->RemoveContentByName("video");
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003619 JsepSessionDescription* modified_answer =
3620 new JsepSessionDescription(JsepSessionDescription::kAnswer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003621
3622 EXPECT_TRUE(modified_answer->Initialize(answer_copy,
3623 answer->session_id(),
3624 answer->session_version()));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003625 SetRemoteDescriptionAnswerExpectError(kMlineMismatch, modified_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003626
wu@webrtc.org4e393072014-04-07 17:04:35 +00003627 // Different content names.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003628 std::string sdp;
3629 EXPECT_TRUE(answer->ToString(&sdp));
3630 const std::string kAudioMid = "a=mid:audio";
3631 const std::string kAudioMidReplaceStr = "a=mid:audio_content_name";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003632 rtc::replace_substrs(kAudioMid.c_str(), kAudioMid.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003633 kAudioMidReplaceStr.c_str(),
3634 kAudioMidReplaceStr.length(),
3635 &sdp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003636 SessionDescriptionInterface* modified_answer1 =
3637 CreateSessionDescription(JsepSessionDescription::kAnswer, sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003638 SetRemoteDescriptionAnswerExpectError(kMlineMismatch, modified_answer1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003639
wu@webrtc.org4e393072014-04-07 17:04:35 +00003640 // Different media types.
3641 EXPECT_TRUE(answer->ToString(&sdp));
3642 const std::string kAudioMline = "m=audio";
3643 const std::string kAudioMlineReplaceStr = "m=video";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003644 rtc::replace_substrs(kAudioMline.c_str(), kAudioMline.length(),
wu@webrtc.org4e393072014-04-07 17:04:35 +00003645 kAudioMlineReplaceStr.c_str(),
3646 kAudioMlineReplaceStr.length(),
3647 &sdp);
3648 SessionDescriptionInterface* modified_answer2 =
3649 CreateSessionDescription(JsepSessionDescription::kAnswer, sdp, NULL);
3650 SetRemoteDescriptionAnswerExpectError(kMlineMismatch, modified_answer2);
3651
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003652 SetRemoteDescriptionWithoutError(answer.release());
3653}
3654
3655// Verifying remote offer and local answer have matching m-lines as per
3656// RFC 3264.
3657TEST_F(WebRtcSessionTest, TestIncorrectMLinesInLocalAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003658 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003659 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003660 SessionDescriptionInterface* offer = CreateRemoteOffer();
3661 SetRemoteDescriptionWithoutError(offer);
htaa2a49d92016-03-04 02:51:39 -08003662 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003663
3664 cricket::SessionDescription* answer_copy = answer->description()->Copy();
3665 answer_copy->RemoveContentByName("video");
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003666 JsepSessionDescription* modified_answer =
3667 new JsepSessionDescription(JsepSessionDescription::kAnswer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003668
3669 EXPECT_TRUE(modified_answer->Initialize(answer_copy,
3670 answer->session_id(),
3671 answer->session_version()));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003672 SetLocalDescriptionAnswerExpectError(kMlineMismatch, modified_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003673 SetLocalDescriptionWithoutError(answer);
3674}
3675
3676// This test verifies that WebRtcSession does not start candidate allocation
3677// before SetLocalDescription is called.
3678TEST_F(WebRtcSessionTest, TestIceStartAfterSetLocalDescriptionOnly) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003679 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003680 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003681 SessionDescriptionInterface* offer = CreateRemoteOffer();
3682 cricket::Candidate candidate;
3683 candidate.set_component(1);
3684 JsepIceCandidate ice_candidate(kMediaContentName0, kMediaContentIndex0,
3685 candidate);
3686 EXPECT_TRUE(offer->AddCandidate(&ice_candidate));
3687 cricket::Candidate candidate1;
3688 candidate1.set_component(1);
3689 JsepIceCandidate ice_candidate1(kMediaContentName1, kMediaContentIndex1,
3690 candidate1);
3691 EXPECT_TRUE(offer->AddCandidate(&ice_candidate1));
3692 SetRemoteDescriptionWithoutError(offer);
deadbeefcbecd352015-09-23 11:50:27 -07003693 ASSERT_TRUE(session_->voice_rtp_transport_channel() != NULL);
3694 ASSERT_TRUE(session_->video_rtp_transport_channel() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003695
3696 // Pump for 1 second and verify that no candidates are generated.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003697 rtc::Thread::Current()->ProcessMessages(1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003698 EXPECT_TRUE(observer_.mline_0_candidates_.empty());
3699 EXPECT_TRUE(observer_.mline_1_candidates_.empty());
3700
htaa2a49d92016-03-04 02:51:39 -08003701 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003702 SetLocalDescriptionWithoutError(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003703 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
3704}
3705
3706// This test verifies that crypto parameter is updated in local session
3707// description as per security policy set in MediaSessionDescriptionFactory.
3708TEST_F(WebRtcSessionTest, TestCryptoAfterSetLocalDescription) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003709 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003710 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003711 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003712
3713 // Making sure SetLocalDescription correctly sets crypto value in
3714 // SessionDescription object after de-serialization of sdp string. The value
3715 // will be set as per MediaSessionDescriptionFactory.
3716 std::string offer_str;
3717 offer->ToString(&offer_str);
3718 SessionDescriptionInterface* jsep_offer_str =
3719 CreateSessionDescription(JsepSessionDescription::kOffer, offer_str, NULL);
3720 SetLocalDescriptionWithoutError(jsep_offer_str);
3721 EXPECT_TRUE(session_->voice_channel()->secure_required());
3722 EXPECT_TRUE(session_->video_channel()->secure_required());
3723}
3724
3725// This test verifies the crypto parameter when security is disabled.
3726TEST_F(WebRtcSessionTest, TestCryptoAfterSetLocalDescriptionWithDisabled) {
wu@webrtc.org97077a32013-10-25 21:18:33 +00003727 options_.disable_encryption = true;
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003728 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003729 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003730 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003731
3732 // Making sure SetLocalDescription correctly sets crypto value in
3733 // SessionDescription object after de-serialization of sdp string. The value
3734 // will be set as per MediaSessionDescriptionFactory.
3735 std::string offer_str;
3736 offer->ToString(&offer_str);
deadbeefcbecd352015-09-23 11:50:27 -07003737 SessionDescriptionInterface* jsep_offer_str =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003738 CreateSessionDescription(JsepSessionDescription::kOffer, offer_str, NULL);
3739 SetLocalDescriptionWithoutError(jsep_offer_str);
3740 EXPECT_FALSE(session_->voice_channel()->secure_required());
3741 EXPECT_FALSE(session_->video_channel()->secure_required());
3742}
3743
3744// This test verifies that an answer contains new ufrag and password if an offer
3745// with new ufrag and password is received.
3746TEST_F(WebRtcSessionTest, TestCreateAnswerWithNewUfragAndPassword) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003747 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003748 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00003749 options.recv_video = true;
kwibergd1fe2812016-04-27 06:47:29 -07003750 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer(options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003751 SetRemoteDescriptionWithoutError(offer.release());
3752
deadbeefab9b2d12015-10-14 11:33:11 -07003753 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003754 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003755 SetLocalDescriptionWithoutError(answer.release());
3756
3757 // Receive an offer with new ufrag and password.
deadbeef0ed85b22016-02-23 17:24:52 -08003758 for (const cricket::ContentInfo& content :
3759 session_->local_description()->description()->contents()) {
3760 options.transport_options[content.name].ice_restart = true;
3761 }
kwibergd1fe2812016-04-27 06:47:29 -07003762 std::unique_ptr<JsepSessionDescription> updated_offer1(
wu@webrtc.org91053e72013-08-10 07:18:04 +00003763 CreateRemoteOffer(options, session_->remote_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003764 SetRemoteDescriptionWithoutError(updated_offer1.release());
3765
kwibergd1fe2812016-04-27 06:47:29 -07003766 std::unique_ptr<SessionDescriptionInterface> updated_answer1(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003767
deadbeef0ed85b22016-02-23 17:24:52 -08003768 EXPECT_FALSE(IceUfragPwdEqual(updated_answer1->description(),
3769 session_->local_description()->description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003770
deadbeef0ed85b22016-02-23 17:24:52 -08003771 // Even a second answer (created before the description is set) should have
3772 // a new ufrag/password.
kwibergd1fe2812016-04-27 06:47:29 -07003773 std::unique_ptr<SessionDescriptionInterface> updated_answer2(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003774
3775 EXPECT_FALSE(IceUfragPwdEqual(updated_answer2->description(),
3776 session_->local_description()->description()));
3777
3778 SetLocalDescriptionWithoutError(updated_answer2.release());
3779}
3780
3781// This test verifies that an answer contains new ufrag and password if an offer
3782// that changes either the ufrag or password (but not both) is received.
3783// RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or
3784// a=ice-pwd attributes compared to the previous SDP from the peer, it
3785// indicates that ICE is restarting for this media stream."
3786TEST_F(WebRtcSessionTest, TestOfferChangingOnlyUfragOrPassword) {
3787 Init();
3788 cricket::MediaSessionOptions options;
3789 options.recv_audio = true;
3790 options.recv_video = true;
3791 // Create an offer with audio and video.
kwibergd1fe2812016-04-27 06:47:29 -07003792 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer(options));
deadbeef0ed85b22016-02-23 17:24:52 -08003793 SetIceUfragPwd(offer.get(), "original_ufrag", "original_password12345");
3794 SetRemoteDescriptionWithoutError(offer.release());
3795
3796 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003797 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003798 SetLocalDescriptionWithoutError(answer.release());
3799
3800 // Receive an offer with a new ufrag but stale password.
kwibergd1fe2812016-04-27 06:47:29 -07003801 std::unique_ptr<JsepSessionDescription> ufrag_changed_offer(
deadbeef0ed85b22016-02-23 17:24:52 -08003802 CreateRemoteOffer(options, session_->remote_description()));
3803 SetIceUfragPwd(ufrag_changed_offer.get(), "modified_ufrag",
3804 "original_password12345");
3805 SetRemoteDescriptionWithoutError(ufrag_changed_offer.release());
3806
kwibergd1fe2812016-04-27 06:47:29 -07003807 std::unique_ptr<SessionDescriptionInterface> updated_answer1(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003808 EXPECT_FALSE(IceUfragPwdEqual(updated_answer1->description(),
3809 session_->local_description()->description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003810 SetLocalDescriptionWithoutError(updated_answer1.release());
deadbeef0ed85b22016-02-23 17:24:52 -08003811
3812 // Receive an offer with a new password but stale ufrag.
kwibergd1fe2812016-04-27 06:47:29 -07003813 std::unique_ptr<JsepSessionDescription> password_changed_offer(
deadbeef0ed85b22016-02-23 17:24:52 -08003814 CreateRemoteOffer(options, session_->remote_description()));
3815 SetIceUfragPwd(password_changed_offer.get(), "modified_ufrag",
3816 "modified_password12345");
3817 SetRemoteDescriptionWithoutError(password_changed_offer.release());
3818
kwibergd1fe2812016-04-27 06:47:29 -07003819 std::unique_ptr<SessionDescriptionInterface> updated_answer2(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003820 EXPECT_FALSE(IceUfragPwdEqual(updated_answer2->description(),
3821 session_->local_description()->description()));
3822 SetLocalDescriptionWithoutError(updated_answer2.release());
wu@webrtc.org91053e72013-08-10 07:18:04 +00003823}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003824
wu@webrtc.org91053e72013-08-10 07:18:04 +00003825// This test verifies that an answer contains old ufrag and password if an offer
3826// with old ufrag and password is received.
3827TEST_F(WebRtcSessionTest, TestCreateAnswerWithOldUfragAndPassword) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003828 Init();
wu@webrtc.org91053e72013-08-10 07:18:04 +00003829 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00003830 options.recv_video = true;
kwibergd1fe2812016-04-27 06:47:29 -07003831 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer(options));
wu@webrtc.org91053e72013-08-10 07:18:04 +00003832 SetRemoteDescriptionWithoutError(offer.release());
3833
deadbeefab9b2d12015-10-14 11:33:11 -07003834 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003835 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
wu@webrtc.org91053e72013-08-10 07:18:04 +00003836 SetLocalDescriptionWithoutError(answer.release());
3837
3838 // Receive an offer without changed ufrag or password.
kwibergd1fe2812016-04-27 06:47:29 -07003839 std::unique_ptr<JsepSessionDescription> updated_offer2(
wu@webrtc.org91053e72013-08-10 07:18:04 +00003840 CreateRemoteOffer(options, session_->remote_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003841 SetRemoteDescriptionWithoutError(updated_offer2.release());
3842
kwibergd1fe2812016-04-27 06:47:29 -07003843 std::unique_ptr<SessionDescriptionInterface> updated_answer2(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003844
deadbeef0ed85b22016-02-23 17:24:52 -08003845 EXPECT_TRUE(IceUfragPwdEqual(updated_answer2->description(),
3846 session_->local_description()->description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003847
3848 SetLocalDescriptionWithoutError(updated_answer2.release());
3849}
3850
deadbeef0ed85b22016-02-23 17:24:52 -08003851// This test verifies that if an offer does an ICE restart on some, but not all
3852// media sections, the answer will change the ufrag/password in the correct
3853// media sections.
3854TEST_F(WebRtcSessionTest, TestCreateAnswerWithNewAndOldUfragAndPassword) {
3855 Init();
3856 cricket::MediaSessionOptions options;
3857 options.recv_video = true;
3858 options.recv_audio = true;
3859 options.bundle_enabled = false;
kwibergd1fe2812016-04-27 06:47:29 -07003860 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer(options));
deadbeef0ed85b22016-02-23 17:24:52 -08003861
3862 SetIceUfragPwd(offer.get(), cricket::MEDIA_TYPE_AUDIO, "aaaa",
3863 "aaaaaaaaaaaaaaaaaaaaaa");
3864 SetIceUfragPwd(offer.get(), cricket::MEDIA_TYPE_VIDEO, "bbbb",
3865 "bbbbbbbbbbbbbbbbbbbbbb");
3866 SetRemoteDescriptionWithoutError(offer.release());
3867
3868 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003869 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003870 SetLocalDescriptionWithoutError(answer.release());
3871
3872 // Receive an offer with new ufrag and password, but only for the video media
3873 // section.
kwibergd1fe2812016-04-27 06:47:29 -07003874 std::unique_ptr<JsepSessionDescription> updated_offer(
deadbeef0ed85b22016-02-23 17:24:52 -08003875 CreateRemoteOffer(options, session_->remote_description()));
3876 SetIceUfragPwd(updated_offer.get(), cricket::MEDIA_TYPE_VIDEO, "cccc",
3877 "cccccccccccccccccccccc");
3878 SetRemoteDescriptionWithoutError(updated_offer.release());
3879
kwibergd1fe2812016-04-27 06:47:29 -07003880 std::unique_ptr<SessionDescriptionInterface> updated_answer(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003881
3882 EXPECT_TRUE(IceUfragPwdEqual(updated_answer->description(),
3883 session_->local_description()->description(),
3884 cricket::MEDIA_TYPE_AUDIO));
3885
3886 EXPECT_FALSE(IceUfragPwdEqual(updated_answer->description(),
3887 session_->local_description()->description(),
3888 cricket::MEDIA_TYPE_VIDEO));
3889
3890 SetLocalDescriptionWithoutError(updated_answer.release());
3891}
3892
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003893TEST_F(WebRtcSessionTest, TestSessionContentError) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003894 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003895 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00003896 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003897 const std::string session_id_orig = offer->session_id();
3898 const std::string session_version_orig = offer->session_version();
3899 SetLocalDescriptionWithoutError(offer);
3900
3901 video_channel_ = media_engine_->GetVideoChannel(0);
3902 video_channel_->set_fail_set_send_codecs(true);
3903
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003904 SessionDescriptionInterface* answer =
3905 CreateRemoteAnswer(session_->local_description());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003906 SetRemoteDescriptionAnswerExpectError("ERROR_CONTENT", answer);
deadbeefd59daf82015-10-14 15:02:44 -07003907
3908 // Test that after a content error, setting any description will
3909 // result in an error.
3910 video_channel_->set_fail_set_send_codecs(false);
3911 answer = CreateRemoteAnswer(session_->local_description());
3912 SetRemoteDescriptionExpectError("", "ERROR_CONTENT", answer);
3913 offer = CreateRemoteOffer();
3914 SetLocalDescriptionExpectError("", "ERROR_CONTENT", offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003915}
3916
3917// Runs the loopback call test with BUNDLE and STUN disabled.
3918TEST_F(WebRtcSessionTest, TestIceStatesBasic) {
3919 // Lets try with only UDP ports.
Peter Thatcher7cbd1882015-09-17 18:54:52 -07003920 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00003921 cricket::PORTALLOCATOR_DISABLE_STUN |
3922 cricket::PORTALLOCATOR_DISABLE_RELAY);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003923 TestLoopbackCall();
3924}
3925
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00003926TEST_F(WebRtcSessionTest, TestIceStatesBasicIPv6) {
Peter Thatcher7cbd1882015-09-17 18:54:52 -07003927 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00003928 cricket::PORTALLOCATOR_DISABLE_STUN |
3929 cricket::PORTALLOCATOR_ENABLE_IPV6 |
3930 cricket::PORTALLOCATOR_DISABLE_RELAY);
3931
3932 // best connection is IPv6 since it has higher network preference.
3933 LoopbackNetworkConfiguration config;
3934 config.test_ipv6_network_ = true;
3935 config.best_connection_after_initial_ice_converged_ =
3936 LoopbackNetworkConfiguration::ExpectedBestConnection(0, 1);
3937
3938 TestLoopbackCall(config);
3939}
3940
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00003941// Runs the loopback call test with BUNDLE and STUN enabled.
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00003942TEST_F(WebRtcSessionTest, TestIceStatesBundle) {
Peter Thatcher7cbd1882015-09-17 18:54:52 -07003943 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
3944 cricket::PORTALLOCATOR_DISABLE_RELAY);
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00003945 TestLoopbackCall();
3946}
3947
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003948TEST_F(WebRtcSessionTest, TestRtpDataChannel) {
htaa2a49d92016-03-04 02:51:39 -08003949 configuration_.enable_rtp_data_channel = true;
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003950 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003951 SetLocalDescriptionWithDataChannel();
htaa2a49d92016-03-04 02:51:39 -08003952 ASSERT_TRUE(data_engine_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003953 EXPECT_EQ(cricket::DCT_RTP, data_engine_->last_channel_type());
3954}
3955
Henrik Boström87713d02015-08-25 09:53:21 +02003956TEST_P(WebRtcSessionTest, TestRtpDataChannelConstraintTakesPrecedence) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003957 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003958
htaa2a49d92016-03-04 02:51:39 -08003959 configuration_.enable_rtp_data_channel = true;
wu@webrtc.org97077a32013-10-25 21:18:33 +00003960 options_.disable_sctp_data_channels = false;
3961
Henrik Boström87713d02015-08-25 09:53:21 +02003962 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003963
3964 SetLocalDescriptionWithDataChannel();
3965 EXPECT_EQ(cricket::DCT_RTP, data_engine_->last_channel_type());
3966}
3967
Henrik Boström87713d02015-08-25 09:53:21 +02003968TEST_P(WebRtcSessionTest, TestCreateOfferWithSctpEnabledWithoutStreams) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003969 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
wu@webrtc.org967bfff2013-09-19 05:49:50 +00003970
Henrik Boström87713d02015-08-25 09:53:21 +02003971 InitWithDtls(GetParam());
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00003972
kwibergd1fe2812016-04-27 06:47:29 -07003973 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00003974 EXPECT_TRUE(offer->description()->GetContentByName("data") == NULL);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00003975 EXPECT_TRUE(offer->description()->GetTransportInfoByName("data") == NULL);
3976}
3977
Henrik Boström87713d02015-08-25 09:53:21 +02003978TEST_P(WebRtcSessionTest, TestCreateAnswerWithSctpInOfferAndNoStreams) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003979 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00003980 SetFactoryDtlsSrtp();
Henrik Boström87713d02015-08-25 09:53:21 +02003981 InitWithDtls(GetParam());
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00003982
3983 // Create remote offer with SCTP.
3984 cricket::MediaSessionOptions options;
3985 options.data_channel_type = cricket::DCT_SCTP;
3986 JsepSessionDescription* offer =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00003987 CreateRemoteOffer(options, cricket::SEC_DISABLED);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00003988 SetRemoteDescriptionWithoutError(offer);
3989
3990 // Verifies the answer contains SCTP.
kwibergd1fe2812016-04-27 06:47:29 -07003991 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00003992 EXPECT_TRUE(answer != NULL);
3993 EXPECT_TRUE(answer->description()->GetContentByName("data") != NULL);
3994 EXPECT_TRUE(answer->description()->GetTransportInfoByName("data") != NULL);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00003995}
3996
Henrik Boström87713d02015-08-25 09:53:21 +02003997TEST_P(WebRtcSessionTest, TestSctpDataChannelWithoutDtls) {
htaa2a49d92016-03-04 02:51:39 -08003998 configuration_.enable_dtls_srtp = rtc::Optional<bool>(false);
Henrik Boström87713d02015-08-25 09:53:21 +02003999 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004000
4001 SetLocalDescriptionWithDataChannel();
4002 EXPECT_EQ(cricket::DCT_NONE, data_engine_->last_channel_type());
4003}
4004
Henrik Boström87713d02015-08-25 09:53:21 +02004005TEST_P(WebRtcSessionTest, TestSctpDataChannelWithDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004006 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004007
Henrik Boström87713d02015-08-25 09:53:21 +02004008 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004009
4010 SetLocalDescriptionWithDataChannel();
4011 EXPECT_EQ(cricket::DCT_SCTP, data_engine_->last_channel_type());
4012}
wu@webrtc.org91053e72013-08-10 07:18:04 +00004013
Henrik Boström87713d02015-08-25 09:53:21 +02004014TEST_P(WebRtcSessionTest, TestDisableSctpDataChannels) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004015 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
wu@webrtc.org97077a32013-10-25 21:18:33 +00004016 options_.disable_sctp_data_channels = true;
Henrik Boström87713d02015-08-25 09:53:21 +02004017 InitWithDtls(GetParam());
wu@webrtc.org97077a32013-10-25 21:18:33 +00004018
4019 SetLocalDescriptionWithDataChannel();
4020 EXPECT_EQ(cricket::DCT_NONE, data_engine_->last_channel_type());
4021}
4022
Henrik Boström87713d02015-08-25 09:53:21 +02004023TEST_P(WebRtcSessionTest, TestSctpDataChannelSendPortParsing) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004024 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00004025 const int new_send_port = 9998;
4026 const int new_recv_port = 7775;
4027
Henrik Boström87713d02015-08-25 09:53:21 +02004028 InitWithDtls(GetParam());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00004029 SetFactoryDtlsSrtp();
4030
4031 // By default, don't actually add the codecs to desc_factory_; they don't
4032 // actually get serialized for SCTP in BuildMediaDescription(). Instead,
4033 // let the session description get parsed. That'll get the proper codecs
4034 // into the stream.
4035 cricket::MediaSessionOptions options;
4036 JsepSessionDescription* offer = CreateRemoteOfferWithSctpPort(
4037 "stream1", new_send_port, options);
4038
4039 // SetRemoteDescription will take the ownership of the offer.
4040 SetRemoteDescriptionWithoutError(offer);
4041
htaa2a49d92016-03-04 02:51:39 -08004042 SessionDescriptionInterface* answer =
4043 ChangeSDPSctpPort(new_recv_port, CreateAnswer());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00004044 ASSERT_TRUE(answer != NULL);
4045
4046 // Now set the local description, which'll take ownership of the answer.
4047 SetLocalDescriptionWithoutError(answer);
4048
4049 // TEST PLAN: Set the port number to something new, set it in the SDP,
4050 // and pass it all the way down.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00004051 EXPECT_EQ(cricket::DCT_SCTP, data_engine_->last_channel_type());
deadbeefab9b2d12015-10-14 11:33:11 -07004052 CreateDataChannel();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00004053
4054 cricket::FakeDataMediaChannel* ch = data_engine_->GetChannel(0);
4055 int portnum = -1;
4056 ASSERT_TRUE(ch != NULL);
4057 ASSERT_EQ(1UL, ch->send_codecs().size());
4058 EXPECT_EQ(cricket::kGoogleSctpDataCodecId, ch->send_codecs()[0].id);
Donald Curtisd4f769d2015-05-28 09:48:21 -07004059 EXPECT_EQ(0, strcmp(cricket::kGoogleSctpDataCodecName,
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00004060 ch->send_codecs()[0].name.c_str()));
4061 EXPECT_TRUE(ch->send_codecs()[0].GetParam(cricket::kCodecParamPort,
4062 &portnum));
4063 EXPECT_EQ(new_send_port, portnum);
4064
4065 ASSERT_EQ(1UL, ch->recv_codecs().size());
4066 EXPECT_EQ(cricket::kGoogleSctpDataCodecId, ch->recv_codecs()[0].id);
Donald Curtisd4f769d2015-05-28 09:48:21 -07004067 EXPECT_EQ(0, strcmp(cricket::kGoogleSctpDataCodecName,
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00004068 ch->recv_codecs()[0].name.c_str()));
4069 EXPECT_TRUE(ch->recv_codecs()[0].GetParam(cricket::kCodecParamPort,
4070 &portnum));
4071 EXPECT_EQ(new_recv_port, portnum);
4072}
4073
deadbeefab9b2d12015-10-14 11:33:11 -07004074// Verifies that when a session's DataChannel receives an OPEN message,
4075// WebRtcSession signals the DataChannel creation request with the expected
4076// config.
4077TEST_P(WebRtcSessionTest, TestSctpDataChannelOpenMessage) {
4078 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
4079
4080 InitWithDtls(GetParam());
4081
4082 SetLocalDescriptionWithDataChannel();
4083 EXPECT_EQ(cricket::DCT_SCTP, data_engine_->last_channel_type());
4084
4085 webrtc::DataChannelInit config;
4086 config.id = 1;
jbaucheec21bd2016-03-20 06:15:43 -07004087 rtc::CopyOnWriteBuffer payload;
deadbeefab9b2d12015-10-14 11:33:11 -07004088 webrtc::WriteDataChannelOpenMessage("a", config, &payload);
4089 cricket::ReceiveDataParams params;
4090 params.ssrc = config.id;
4091 params.type = cricket::DMT_CONTROL;
4092
4093 cricket::DataChannel* data_channel = session_->data_channel();
4094 data_channel->SignalDataReceived(data_channel, params, payload);
4095
4096 EXPECT_EQ("a", last_data_channel_label_);
4097 EXPECT_EQ(config.id, last_data_channel_config_.id);
4098 EXPECT_FALSE(last_data_channel_config_.negotiated);
4099 EXPECT_EQ(webrtc::InternalDataChannelInit::kAcker,
4100 last_data_channel_config_.open_handshake_role);
4101}
4102
4103TEST_P(WebRtcSessionTest, TestUsesProvidedCertificate) {
Henrik Boströmd8281982015-08-27 10:12:24 +02004104 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Henrik Boströmd79599d2016-06-01 13:58:50 +02004105 FakeRTCCertificateGenerator::GenerateCertificate();
Henrik Boströmd8281982015-08-27 10:12:24 +02004106
htaa2a49d92016-03-04 02:51:39 -08004107 configuration_.certificates.push_back(certificate);
4108 Init();
Henrik Boströmd8281982015-08-27 10:12:24 +02004109 EXPECT_TRUE_WAIT(!session_->waiting_for_certificate_for_testing(), 1000);
4110
4111 EXPECT_EQ(session_->certificate_for_testing(), certificate);
4112}
wu@webrtc.org91053e72013-08-10 07:18:04 +00004113
Henrik Boström87713d02015-08-25 09:53:21 +02004114// Verifies that CreateOffer succeeds when CreateOffer is called before async
4115// identity generation is finished (even if a certificate is provided this is
4116// an async op).
4117TEST_P(WebRtcSessionTest, TestCreateOfferBeforeIdentityRequestReturnSuccess) {
4118 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
4119 InitWithDtls(GetParam());
4120
Henrik Boströmd8281982015-08-27 10:12:24 +02004121 EXPECT_TRUE(session_->waiting_for_certificate_for_testing());
deadbeefab9b2d12015-10-14 11:33:11 -07004122 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07004123 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00004124
wu@webrtc.org91053e72013-08-10 07:18:04 +00004125 EXPECT_TRUE(offer != NULL);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00004126 VerifyNoCryptoParams(offer->description(), true);
4127 VerifyFingerprintStatus(offer->description(), true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004128}
4129
4130// Verifies that CreateAnswer succeeds when CreateOffer is called before async
Henrik Boström87713d02015-08-25 09:53:21 +02004131// identity generation is finished (even if a certificate is provided this is
4132// an async op).
4133TEST_P(WebRtcSessionTest, TestCreateAnswerBeforeIdentityRequestReturnSuccess) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004134 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004135 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00004136 SetFactoryDtlsSrtp();
wu@webrtc.org91053e72013-08-10 07:18:04 +00004137
4138 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00004139 options.recv_video = true;
kwibergd1fe2812016-04-27 06:47:29 -07004140 std::unique_ptr<JsepSessionDescription> offer(
4141 CreateRemoteOffer(options, cricket::SEC_DISABLED));
wu@webrtc.org91053e72013-08-10 07:18:04 +00004142 ASSERT_TRUE(offer.get() != NULL);
4143 SetRemoteDescriptionWithoutError(offer.release());
4144
kwibergd1fe2812016-04-27 06:47:29 -07004145 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
wu@webrtc.org91053e72013-08-10 07:18:04 +00004146 EXPECT_TRUE(answer != NULL);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00004147 VerifyNoCryptoParams(answer->description(), true);
4148 VerifyFingerprintStatus(answer->description(), true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004149}
4150
4151// Verifies that CreateOffer succeeds when CreateOffer is called after async
Henrik Boström87713d02015-08-25 09:53:21 +02004152// identity generation is finished (even if a certificate is provided this is
4153// an async op).
4154TEST_P(WebRtcSessionTest, TestCreateOfferAfterIdentityRequestReturnSuccess) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004155 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004156 InitWithDtls(GetParam());
wu@webrtc.org91053e72013-08-10 07:18:04 +00004157
Henrik Boströmd8281982015-08-27 10:12:24 +02004158 EXPECT_TRUE_WAIT(!session_->waiting_for_certificate_for_testing(), 1000);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00004159
kwibergd1fe2812016-04-27 06:47:29 -07004160 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
wu@webrtc.org91053e72013-08-10 07:18:04 +00004161 EXPECT_TRUE(offer != NULL);
4162}
4163
4164// Verifies that CreateOffer fails when CreateOffer is called after async
4165// identity generation fails.
4166TEST_F(WebRtcSessionTest, TestCreateOfferAfterIdentityRequestReturnFailure) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004167 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004168 InitWithDtlsIdentityGenFail();
wu@webrtc.org91053e72013-08-10 07:18:04 +00004169
Henrik Boströmd8281982015-08-27 10:12:24 +02004170 EXPECT_TRUE_WAIT(!session_->waiting_for_certificate_for_testing(), 1000);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00004171
kwibergd1fe2812016-04-27 06:47:29 -07004172 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
wu@webrtc.org91053e72013-08-10 07:18:04 +00004173 EXPECT_TRUE(offer == NULL);
4174}
4175
4176// Verifies that CreateOffer succeeds when Multiple CreateOffer calls are made
4177// before async identity generation is finished.
Henrik Boström87713d02015-08-25 09:53:21 +02004178TEST_P(WebRtcSessionTest,
wu@webrtc.org91053e72013-08-10 07:18:04 +00004179 TestMultipleCreateOfferBeforeIdentityRequestReturnSuccess) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004180 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
deadbeefcbecd352015-09-23 11:50:27 -07004181 VerifyMultipleAsyncCreateDescription(GetParam(),
4182 CreateSessionDescriptionRequest::kOffer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004183}
4184
4185// Verifies that CreateOffer fails when Multiple CreateOffer calls are made
4186// before async identity generation fails.
4187TEST_F(WebRtcSessionTest,
4188 TestMultipleCreateOfferBeforeIdentityRequestReturnFailure) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004189 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004190 VerifyMultipleAsyncCreateDescriptionIdentityGenFailure(
4191 CreateSessionDescriptionRequest::kOffer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004192}
4193
4194// Verifies that CreateAnswer succeeds when Multiple CreateAnswer calls are made
4195// before async identity generation is finished.
Henrik Boström87713d02015-08-25 09:53:21 +02004196TEST_P(WebRtcSessionTest,
wu@webrtc.org91053e72013-08-10 07:18:04 +00004197 TestMultipleCreateAnswerBeforeIdentityRequestReturnSuccess) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004198 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004199 VerifyMultipleAsyncCreateDescription(
Henrik Boström87713d02015-08-25 09:53:21 +02004200 GetParam(), CreateSessionDescriptionRequest::kAnswer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004201}
4202
4203// Verifies that CreateAnswer fails when Multiple CreateAnswer calls are made
4204// before async identity generation fails.
4205TEST_F(WebRtcSessionTest,
4206 TestMultipleCreateAnswerBeforeIdentityRequestReturnFailure) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004207 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004208 VerifyMultipleAsyncCreateDescriptionIdentityGenFailure(
4209 CreateSessionDescriptionRequest::kAnswer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004210}
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00004211
4212// Verifies that setRemoteDescription fails when DTLS is disabled and the remote
4213// offer has no SDES crypto but only DTLS fingerprint.
4214TEST_F(WebRtcSessionTest, TestSetRemoteOfferFailIfDtlsDisabledAndNoCrypto) {
4215 // Init without DTLS.
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00004216 Init();
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00004217 // Create a remote offer with secured transport disabled.
4218 cricket::MediaSessionOptions options;
4219 JsepSessionDescription* offer(CreateRemoteOffer(
4220 options, cricket::SEC_DISABLED));
4221 // Adds a DTLS fingerprint to the remote offer.
4222 cricket::SessionDescription* sdp = offer->description();
4223 TransportInfo* audio = sdp->GetTransportInfoByName("audio");
4224 ASSERT_TRUE(audio != NULL);
4225 ASSERT_TRUE(audio->description.identity_fingerprint.get() == NULL);
4226 audio->description.identity_fingerprint.reset(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004227 rtc::SSLFingerprint::CreateFromRfc4572(
4228 rtc::DIGEST_SHA_256, kFakeDtlsFingerprint));
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00004229 SetRemoteDescriptionOfferExpectError(kSdpWithoutSdesCrypto,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00004230 offer);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00004231}
4232
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00004233TEST_F(WebRtcSessionTest, TestCombinedAudioVideoBweConstraint) {
htaa2a49d92016-03-04 02:51:39 -08004234 configuration_.combined_audio_video_bwe = rtc::Optional<bool>(true);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00004235 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07004236 SendAudioVideoStream1();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00004237 SessionDescriptionInterface* offer = CreateOffer();
4238
4239 SetLocalDescriptionWithoutError(offer);
4240
4241 voice_channel_ = media_engine_->GetVoiceChannel(0);
4242
4243 ASSERT_TRUE(voice_channel_ != NULL);
solenberg66f43392015-09-09 01:36:22 -07004244 const cricket::AudioOptions& audio_options = voice_channel_->options();
Karl Wibergbe579832015-11-10 22:34:18 +01004245 EXPECT_EQ(rtc::Optional<bool>(true), audio_options.combined_audio_video_bwe);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00004246}
4247
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004248// Tests that we can renegotiate new media content with ICE candidates in the
4249// new remote SDP.
Henrik Boström87713d02015-08-25 09:53:21 +02004250TEST_P(WebRtcSessionTest, TestRenegotiateNewMediaWithCandidatesInSdp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004251 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004252 InitWithDtls(GetParam());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004253 SetFactoryDtlsSrtp();
4254
deadbeefab9b2d12015-10-14 11:33:11 -07004255 SendAudioOnlyStream2();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00004256 SessionDescriptionInterface* offer = CreateOffer();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004257 SetLocalDescriptionWithoutError(offer);
4258
4259 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
4260 SetRemoteDescriptionWithoutError(answer);
4261
4262 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00004263 options.recv_video = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004264 offer = CreateRemoteOffer(options, cricket::SEC_DISABLED);
4265
4266 cricket::Candidate candidate1;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004267 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 5000));
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004268 candidate1.set_component(1);
4269 JsepIceCandidate ice_candidate(kMediaContentName1, kMediaContentIndex1,
4270 candidate1);
4271 EXPECT_TRUE(offer->AddCandidate(&ice_candidate));
4272 SetRemoteDescriptionWithoutError(offer);
4273
htaa2a49d92016-03-04 02:51:39 -08004274 answer = CreateAnswer();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004275 SetLocalDescriptionWithoutError(answer);
4276}
4277
4278// Tests that we can renegotiate new media content with ICE candidates separated
4279// from the remote SDP.
Henrik Boström87713d02015-08-25 09:53:21 +02004280TEST_P(WebRtcSessionTest, TestRenegotiateNewMediaWithCandidatesSeparated) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004281 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004282 InitWithDtls(GetParam());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004283 SetFactoryDtlsSrtp();
4284
deadbeefab9b2d12015-10-14 11:33:11 -07004285 SendAudioOnlyStream2();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00004286 SessionDescriptionInterface* offer = CreateOffer();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004287 SetLocalDescriptionWithoutError(offer);
4288
4289 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
4290 SetRemoteDescriptionWithoutError(answer);
4291
4292 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00004293 options.recv_video = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004294 offer = CreateRemoteOffer(options, cricket::SEC_DISABLED);
4295 SetRemoteDescriptionWithoutError(offer);
4296
4297 cricket::Candidate candidate1;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004298 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 5000));
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004299 candidate1.set_component(1);
4300 JsepIceCandidate ice_candidate(kMediaContentName1, kMediaContentIndex1,
4301 candidate1);
4302 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate));
4303
htaa2a49d92016-03-04 02:51:39 -08004304 answer = CreateAnswer();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004305 SetLocalDescriptionWithoutError(answer);
4306}
honghaiz7f777492016-02-02 21:54:01 -08004307
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00004308// Tests that RTX codec is removed from the answer when it isn't supported
4309// by local side.
zhihuang3a334652016-05-05 18:37:49 -07004310TEST_F(WebRtcSessionTest, TestRtxRemovedByCreateAnswer) {
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00004311 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07004312 SendAudioVideoStream1();
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00004313 std::string offer_sdp(kSdpWithRtx);
4314
4315 SessionDescriptionInterface* offer =
4316 CreateSessionDescription(JsepSessionDescription::kOffer, offer_sdp, NULL);
4317 EXPECT_TRUE(offer->ToString(&offer_sdp));
4318
4319 // Offer SDP contains the RTX codec.
zhihuang3a334652016-05-05 18:37:49 -07004320 EXPECT_TRUE(ContainsVideoCodecWithName(offer, "rtx"));
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00004321 SetRemoteDescriptionWithoutError(offer);
4322
htaa2a49d92016-03-04 02:51:39 -08004323 SessionDescriptionInterface* answer = CreateAnswer();
zhihuang3a334652016-05-05 18:37:49 -07004324 // Answer SDP does not contain the RTX codec.
4325 EXPECT_FALSE(ContainsVideoCodecWithName(answer, "rtx"));
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00004326 SetLocalDescriptionWithoutError(answer);
4327}
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004328
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004329// This verifies that the voice channel after bundle has both options from video
4330// and voice channels.
4331TEST_F(WebRtcSessionTest, TestSetSocketOptionBeforeBundle) {
4332 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyBalanced);
deadbeefab9b2d12015-10-14 11:33:11 -07004333 SendAudioVideoStream1();
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004334
4335 PeerConnectionInterface::RTCOfferAnswerOptions options;
4336 options.use_rtp_mux = true;
4337
4338 SessionDescriptionInterface* offer = CreateOffer(options);
4339 SetLocalDescriptionWithoutError(offer);
4340
4341 session_->video_channel()->SetOption(cricket::BaseChannel::ST_RTP,
4342 rtc::Socket::Option::OPT_SNDBUF, 4000);
4343
4344 session_->voice_channel()->SetOption(cricket::BaseChannel::ST_RTP,
4345 rtc::Socket::Option::OPT_RCVBUF, 8000);
4346
4347 int option_val;
deadbeefcbecd352015-09-23 11:50:27 -07004348 EXPECT_TRUE(session_->video_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004349 rtc::Socket::Option::OPT_SNDBUF, &option_val));
4350 EXPECT_EQ(4000, option_val);
deadbeefcbecd352015-09-23 11:50:27 -07004351 EXPECT_FALSE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004352 rtc::Socket::Option::OPT_SNDBUF, &option_val));
4353
deadbeefcbecd352015-09-23 11:50:27 -07004354 EXPECT_TRUE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004355 rtc::Socket::Option::OPT_RCVBUF, &option_val));
4356 EXPECT_EQ(8000, option_val);
deadbeefcbecd352015-09-23 11:50:27 -07004357 EXPECT_FALSE(session_->video_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004358 rtc::Socket::Option::OPT_RCVBUF, &option_val));
4359
deadbeefcbecd352015-09-23 11:50:27 -07004360 EXPECT_NE(session_->voice_rtp_transport_channel(),
4361 session_->video_rtp_transport_channel());
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004362
deadbeefab9b2d12015-10-14 11:33:11 -07004363 SendAudioVideoStream2();
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004364 SessionDescriptionInterface* answer =
4365 CreateRemoteAnswer(session_->local_description());
4366 SetRemoteDescriptionWithoutError(answer);
4367
deadbeefcbecd352015-09-23 11:50:27 -07004368 EXPECT_TRUE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004369 rtc::Socket::Option::OPT_SNDBUF, &option_val));
4370 EXPECT_EQ(4000, option_val);
4371
deadbeefcbecd352015-09-23 11:50:27 -07004372 EXPECT_TRUE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004373 rtc::Socket::Option::OPT_RCVBUF, &option_val));
4374 EXPECT_EQ(8000, option_val);
4375}
4376
tommi0f620f42015-07-09 03:25:02 -07004377// Test creating a session, request multiple offers, destroy the session
4378// and make sure we got success/failure callbacks for all of the requests.
4379// Background: crbug.com/507307
4380TEST_F(WebRtcSessionTest, CreateOffersAndShutdown) {
4381 Init();
4382
4383 rtc::scoped_refptr<WebRtcSessionCreateSDPObserverForTest> observers[100];
4384 PeerConnectionInterface::RTCOfferAnswerOptions options;
4385 options.offer_to_receive_audio =
4386 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
deadbeefab9b2d12015-10-14 11:33:11 -07004387 cricket::MediaSessionOptions session_options;
4388 session_options.recv_audio = true;
tommi0f620f42015-07-09 03:25:02 -07004389
4390 for (auto& o : observers) {
4391 o = new WebRtcSessionCreateSDPObserverForTest();
deadbeefab9b2d12015-10-14 11:33:11 -07004392 session_->CreateOffer(o, options, session_options);
tommi0f620f42015-07-09 03:25:02 -07004393 }
4394
4395 session_.reset();
4396
tommi0f620f42015-07-09 03:25:02 -07004397 for (auto& o : observers) {
4398 // We expect to have received a notification now even if the session was
4399 // terminated. The offer creation may or may not have succeeded, but we
4400 // must have received a notification which, so the only invalid state
4401 // is kInit.
4402 EXPECT_NE(WebRtcSessionCreateSDPObserverForTest::kInit, o->state());
4403 }
4404}
4405
stefanc1aeaf02015-10-15 07:26:07 -07004406TEST_F(WebRtcSessionTest, TestPacketOptionsAndOnPacketSent) {
4407 TestPacketOptions();
4408}
4409
deadbeef057ecf02016-01-20 14:30:43 -08004410// Make sure the signal from "GetOnDestroyedSignal()" fires when the session
4411// is destroyed.
4412TEST_F(WebRtcSessionTest, TestOnDestroyedSignal) {
4413 Init();
4414 session_.reset();
4415 EXPECT_TRUE(session_destroyed_);
4416}
4417
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004418// TODO(bemasc): Add a TestIceStatesBundle with BUNDLE enabled. That test
4419// currently fails because upon disconnection and reconnection OnIceComplete is
4420// called more than once without returning to IceGatheringGathering.
Henrik Boström87713d02015-08-25 09:53:21 +02004421
deadbeefcbecd352015-09-23 11:50:27 -07004422INSTANTIATE_TEST_CASE_P(WebRtcSessionTests,
4423 WebRtcSessionTest,
4424 testing::Values(ALREADY_GENERATED,
4425 DTLS_IDENTITY_STORE));