blob: c0d8a3bbdfe17ee7a11cbeab172e16aae0d56851 [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;
jbauchac8869e2015-07-03 01:36:14 -070064using webrtc::FakeMetricsObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065using webrtc::IceCandidateCollection;
deadbeefab9b2d12015-10-14 11:33:11 -070066using webrtc::InternalDataChannelInit;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067using webrtc::JsepIceCandidate;
68using webrtc::JsepSessionDescription;
wu@webrtc.org97077a32013-10-25 21:18:33 +000069using webrtc::PeerConnectionFactoryInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070using webrtc::PeerConnectionInterface;
71using webrtc::SessionDescriptionInterface;
deadbeefd59daf82015-10-14 15:02:44 -070072using webrtc::SessionStats;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using webrtc::StreamCollection;
wu@webrtc.org91053e72013-08-10 07:18:04 +000074using webrtc::WebRtcSession;
wu@webrtc.org364f2042013-11-20 21:49:41 +000075using webrtc::kBundleWithoutRtcpMux;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000076using webrtc::kCreateChannelFailed;
77using webrtc::kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078using webrtc::kMlineMismatch;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000079using webrtc::kPushDownTDFailed;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000080using webrtc::kSdpWithoutIceUfragPwd;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000081using webrtc::kSdpWithoutDtlsFingerprint;
82using webrtc::kSdpWithoutSdesCrypto;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083using webrtc::kSessionError;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000084using webrtc::kSessionErrorDesc;
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000085using webrtc::kMaxUnsignalledRecvStreams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +000087typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
88
wu@webrtc.org364f2042013-11-20 21:49:41 +000089static const int kClientAddrPort = 0;
90static const char kClientAddrHost1[] = "11.11.11.11";
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +000091static const char kClientIPv6AddrHost1[] =
92 "2620:0:aaaa:bbbb:cccc:dddd:eeee:ffff";
wu@webrtc.org364f2042013-11-20 21:49:41 +000093static const char kClientAddrHost2[] = "22.22.22.22";
94static const char kStunAddrHost[] = "99.99.99.1";
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000095static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
96static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +000097static const char kTurnUsername[] = "test";
98static const char kTurnPassword[] = "test";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
100static const char kSessionVersion[] = "1";
101
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102// Media index of candidates belonging to the first media content.
103static const int kMediaContentIndex0 = 0;
104static const char kMediaContentName0[] = "audio";
105
106// Media index of candidates belonging to the second media content.
107static const int kMediaContentIndex1 = 1;
108static const char kMediaContentName1[] = "video";
109
110static const int kIceCandidatesTimeout = 10000;
deadbeeff5f03e82016-06-06 11:16:06 -0700111// STUN timeout with all retransmissions is a total of 9500ms.
112static const int kStunTimeout = 9500;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000114static const char kFakeDtlsFingerprint[] =
115 "BB:CD:72:F7:2F:D0:BA:43:F3:68:B1:0C:23:72:B6:4A:"
116 "0F:DE:34:06:BC:E0:FE:01:BC:73:C8:6D:F4:65:D5:24";
117
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +0000118static const char kTooLongIceUfragPwd[] =
119 "IceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfrag"
120 "IceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfrag"
121 "IceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfrag"
122 "IceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfragIceUfrag";
123
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000124static const char kSdpWithRtx[] =
125 "v=0\r\n"
126 "o=- 4104004319237231850 2 IN IP4 127.0.0.1\r\n"
127 "s=-\r\n"
128 "t=0 0\r\n"
129 "a=msid-semantic: WMS stream1\r\n"
130 "m=video 9 RTP/SAVPF 0 96\r\n"
131 "c=IN IP4 0.0.0.0\r\n"
132 "a=rtcp:9 IN IP4 0.0.0.0\r\n"
133 "a=ice-ufrag:CerjGp19G7wpXwl7\r\n"
134 "a=ice-pwd:cMvOlFvQ6ochez1ZOoC2uBEC\r\n"
135 "a=mid:video\r\n"
136 "a=sendrecv\r\n"
137 "a=rtcp-mux\r\n"
138 "a=crypto:1 AES_CM_128_HMAC_SHA1_80 "
139 "inline:5/4N5CDvMiyDArHtBByUM71VIkguH17ZNoX60GrA\r\n"
140 "a=rtpmap:0 fake_video_codec/90000\r\n"
141 "a=rtpmap:96 rtx/90000\r\n"
142 "a=fmtp:96 apt=0\r\n";
143
deadbeefab9b2d12015-10-14 11:33:11 -0700144static const char kStream1[] = "stream1";
145static const char kVideoTrack1[] = "video1";
146static const char kAudioTrack1[] = "audio1";
147
148static const char kStream2[] = "stream2";
149static const char kVideoTrack2[] = "video2";
150static const char kAudioTrack2[] = "audio2";
151
Henrik Boström87713d02015-08-25 09:53:21 +0200152enum RTCCertificateGenerationMethod { ALREADY_GENERATED, DTLS_IDENTITY_STORE };
153
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154class MockIceObserver : public webrtc::IceObserver {
155 public:
156 MockIceObserver()
157 : oncandidatesready_(false),
158 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
159 ice_gathering_state_(PeerConnectionInterface::kIceGatheringNew) {
160 }
161
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200162 virtual ~MockIceObserver() = default;
163
perkjdfb769d2016-02-09 03:09:43 -0800164 void OnIceConnectionChange(
165 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 ice_connection_state_ = new_state;
skvlad6c87a672016-05-17 17:49:52 -0700167 ice_connection_state_history_.push_back(new_state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 }
perkjdfb769d2016-02-09 03:09:43 -0800169 void OnIceGatheringChange(
170 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 // We can never transition back to "new".
172 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, new_state);
173 ice_gathering_state_ = new_state;
perkjdfb769d2016-02-09 03:09:43 -0800174 oncandidatesready_ =
175 new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 }
177
178 // Found a new candidate.
perkjdfb769d2016-02-09 03:09:43 -0800179 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000180 switch (candidate->sdp_mline_index()) {
181 case kMediaContentIndex0:
182 mline_0_candidates_.push_back(candidate->candidate());
183 break;
184 case kMediaContentIndex1:
185 mline_1_candidates_.push_back(candidate->candidate());
186 break;
187 default:
188 ASSERT(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 }
wu@webrtc.org364f2042013-11-20 21:49:41 +0000190
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 // The ICE gathering state should always be Gathering when a candidate is
192 // received (or possibly Completed in the case of the final candidate).
193 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, ice_gathering_state_);
194 }
195
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700196 // Some local candidates are removed.
197 void OnIceCandidatesRemoved(
nisseef8b61e2016-04-29 06:09:15 -0700198 const std::vector<cricket::Candidate>& candidates) override {
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700199 num_candidates_removed_ += candidates.size();
200 }
201
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 bool oncandidatesready_;
203 std::vector<cricket::Candidate> mline_0_candidates_;
204 std::vector<cricket::Candidate> mline_1_candidates_;
205 PeerConnectionInterface::IceConnectionState ice_connection_state_;
206 PeerConnectionInterface::IceGatheringState ice_gathering_state_;
skvlad6c87a672016-05-17 17:49:52 -0700207 std::vector<PeerConnectionInterface::IceConnectionState>
208 ice_connection_state_history_;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700209 size_t num_candidates_removed_ = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210};
211
212class WebRtcSessionForTest : public webrtc::WebRtcSession {
213 public:
zhihuang29ff8442016-07-27 11:07:25 -0700214 WebRtcSessionForTest(
215 webrtc::MediaControllerInterface* media_controller,
216 rtc::Thread* network_thread,
217 rtc::Thread* worker_thread,
218 rtc::Thread* signaling_thread,
219 cricket::PortAllocator* port_allocator,
220 webrtc::IceObserver* ice_observer,
221 std::unique_ptr<cricket::TransportController> transport_controller)
stefanc1aeaf02015-10-15 07:26:07 -0700222 : WebRtcSession(media_controller,
danilchape9021a32016-05-17 01:52:02 -0700223 network_thread,
stefanc1aeaf02015-10-15 07:26:07 -0700224 worker_thread,
danilchape9021a32016-05-17 01:52:02 -0700225 signaling_thread,
zhihuang29ff8442016-07-27 11:07:25 -0700226 port_allocator,
227 std::move(transport_controller)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 RegisterIceObserver(ice_observer);
229 }
230 virtual ~WebRtcSessionForTest() {}
231
deadbeefcbecd352015-09-23 11:50:27 -0700232 // Note that these methods are only safe to use if the signaling thread
233 // is the same as the worker thread
234 cricket::TransportChannel* voice_rtp_transport_channel() {
235 return rtp_transport_channel(voice_channel());
236 }
237
238 cricket::TransportChannel* voice_rtcp_transport_channel() {
239 return rtcp_transport_channel(voice_channel());
240 }
241
242 cricket::TransportChannel* video_rtp_transport_channel() {
243 return rtp_transport_channel(video_channel());
244 }
245
246 cricket::TransportChannel* video_rtcp_transport_channel() {
247 return rtcp_transport_channel(video_channel());
248 }
249
250 cricket::TransportChannel* data_rtp_transport_channel() {
251 return rtp_transport_channel(data_channel());
252 }
253
254 cricket::TransportChannel* data_rtcp_transport_channel() {
255 return rtcp_transport_channel(data_channel());
256 }
257
deadbeefcbecd352015-09-23 11:50:27 -0700258 private:
259 cricket::TransportChannel* rtp_transport_channel(cricket::BaseChannel* ch) {
260 if (!ch) {
261 return nullptr;
262 }
263 return ch->transport_channel();
264 }
265
266 cricket::TransportChannel* rtcp_transport_channel(cricket::BaseChannel* ch) {
267 if (!ch) {
268 return nullptr;
269 }
270 return ch->rtcp_transport_channel();
271 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272};
273
wu@webrtc.org91053e72013-08-10 07:18:04 +0000274class WebRtcSessionCreateSDPObserverForTest
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000275 : public rtc::RefCountedObject<CreateSessionDescriptionObserver> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 public:
wu@webrtc.org91053e72013-08-10 07:18:04 +0000277 enum State {
278 kInit,
279 kFailed,
280 kSucceeded,
281 };
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000282 WebRtcSessionCreateSDPObserverForTest() : state_(kInit) {}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000283
284 // CreateSessionDescriptionObserver implementation.
285 virtual void OnSuccess(SessionDescriptionInterface* desc) {
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000286 description_.reset(desc);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000287 state_ = kSucceeded;
288 }
289 virtual void OnFailure(const std::string& error) {
290 state_ = kFailed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 }
292
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000293 SessionDescriptionInterface* description() { return description_.get(); }
294
295 SessionDescriptionInterface* ReleaseDescription() {
296 return description_.release();
297 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298
wu@webrtc.org91053e72013-08-10 07:18:04 +0000299 State state() const { return state_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300
wu@webrtc.org91053e72013-08-10 07:18:04 +0000301 protected:
302 ~WebRtcSessionCreateSDPObserverForTest() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
304 private:
kwibergd1fe2812016-04-27 06:47:29 -0700305 std::unique_ptr<SessionDescriptionInterface> description_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000306 State state_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307};
308
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800309class FakeAudioSource : public cricket::AudioSource {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000310 public:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800311 FakeAudioSource() : sink_(NULL) {}
312 virtual ~FakeAudioSource() {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000313 if (sink_)
314 sink_->OnClose();
315 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000316
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000317 void SetSink(Sink* sink) override { sink_ = sink; }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000318
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800319 const cricket::AudioSource::Sink* sink() const { return sink_; }
320
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000321 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800322 cricket::AudioSource::Sink* sink_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000323};
324
Henrik Boström87713d02015-08-25 09:53:21 +0200325class WebRtcSessionTest
deadbeefab9b2d12015-10-14 11:33:11 -0700326 : public testing::TestWithParam<RTCCertificateGenerationMethod>,
327 public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 protected:
329 // TODO Investigate why ChannelManager crashes, if it's created
330 // after stun_server.
331 WebRtcSessionTest()
stefanc1aeaf02015-10-15 07:26:07 -0700332 : media_engine_(new cricket::FakeMediaEngine()),
333 data_engine_(new cricket::FakeDataEngine()),
334 channel_manager_(
335 new cricket::ChannelManager(media_engine_,
336 data_engine_,
stefanc1aeaf02015-10-15 07:26:07 -0700337 rtc::Thread::Current())),
338 fake_call_(webrtc::Call::Config()),
339 media_controller_(
nisse51542be2016-02-12 02:27:06 -0800340 webrtc::MediaControllerInterface::Create(cricket::MediaConfig(),
341 rtc::Thread::Current(),
stefanc1aeaf02015-10-15 07:26:07 -0700342 channel_manager_.get())),
343 tdesc_factory_(new cricket::TransportDescriptionFactory()),
344 desc_factory_(
345 new cricket::MediaSessionDescriptionFactory(channel_manager_.get(),
346 tdesc_factory_.get())),
347 pss_(new rtc::PhysicalSocketServer),
348 vss_(new rtc::VirtualSocketServer(pss_.get())),
349 fss_(new rtc::FirewallSocketServer(vss_.get())),
350 ss_scope_(fss_.get()),
351 stun_socket_addr_(
352 rtc::SocketAddress(kStunAddrHost, cricket::STUN_SERVER_PORT)),
353 stun_server_(cricket::TestStunServer::Create(Thread::Current(),
354 stun_socket_addr_)),
355 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
356 metrics_observer_(new rtc::RefCountedObject<FakeMetricsObserver>()) {
buildbot@webrtc.org51c55082014-07-28 22:26:15 +0000357 cricket::ServerAddresses stun_servers;
358 stun_servers.insert(stun_socket_addr_);
359 allocator_.reset(new cricket::BasicPortAllocator(
360 &network_manager_,
361 stun_servers,
362 SocketAddress(), SocketAddress(), SocketAddress()));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000363 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700364 cricket::PORTALLOCATOR_DISABLE_RELAY);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 EXPECT_TRUE(channel_manager_->Init());
366 desc_factory_->set_add_legacy_streams(false);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000367 allocator_->set_step_delay(cricket::kMinimumStepDelay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 }
369
370 void AddInterface(const SocketAddress& addr) {
371 network_manager_.AddInterface(addr);
372 }
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700373 void RemoveInterface(const SocketAddress& addr) {
374 network_manager_.RemoveInterface(addr);
375 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376
Henrik Boströmd79599d2016-06-01 13:58:50 +0200377 // If |cert_generator| != null or |rtc_configuration| contains |certificates|
378 // then DTLS will be enabled unless explicitly disabled by |rtc_configuration|
379 // options. When DTLS is enabled a certificate will be used if provided,
380 // otherwise one will be generated using the |cert_generator|.
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000381 void Init(
Henrik Boströmd79599d2016-06-01 13:58:50 +0200382 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 ASSERT_TRUE(session_.get() == NULL);
384 session_.reset(new WebRtcSessionForTest(
stefanc1aeaf02015-10-15 07:26:07 -0700385 media_controller_.get(), rtc::Thread::Current(), rtc::Thread::Current(),
zhihuang29ff8442016-07-27 11:07:25 -0700386 rtc::Thread::Current(), allocator_.get(), &observer_,
387 std::unique_ptr<cricket::TransportController>(
388 new cricket::TransportController(rtc::Thread::Current(),
389 rtc::Thread::Current(),
390 allocator_.get()))));
deadbeefab9b2d12015-10-14 11:33:11 -0700391 session_->SignalDataChannelOpenMessage.connect(
392 this, &WebRtcSessionTest::OnDataChannelOpenMessage);
deadbeef057ecf02016-01-20 14:30:43 -0800393 session_->GetOnDestroyedSignal()->connect(
394 this, &WebRtcSessionTest::OnSessionDestroyed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395
396 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
397 observer_.ice_connection_state_);
398 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
399 observer_.ice_gathering_state_);
400
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200401 EXPECT_TRUE(session_->Initialize(options_, std::move(cert_generator),
htaa2a49d92016-03-04 02:51:39 -0800402 configuration_));
jbauchac8869e2015-07-03 01:36:14 -0700403 session_->set_metrics_observer(metrics_observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 }
405
deadbeefab9b2d12015-10-14 11:33:11 -0700406 void OnDataChannelOpenMessage(const std::string& label,
407 const InternalDataChannelInit& config) {
408 last_data_channel_label_ = label;
409 last_data_channel_config_ = config;
410 }
411
deadbeef057ecf02016-01-20 14:30:43 -0800412 void OnSessionDestroyed() { session_destroyed_ = true; }
413
htaa2a49d92016-03-04 02:51:39 -0800414 void Init() { Init(nullptr); }
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000415
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000416 void InitWithBundlePolicy(
417 PeerConnectionInterface::BundlePolicy bundle_policy) {
htaa2a49d92016-03-04 02:51:39 -0800418 configuration_.bundle_policy = bundle_policy;
419 Init();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700420 }
421
422 void InitWithRtcpMuxPolicy(
423 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy) {
424 PeerConnectionInterface::RTCConfiguration configuration;
htaa2a49d92016-03-04 02:51:39 -0800425 configuration_.rtcp_mux_policy = rtcp_mux_policy;
426 Init();
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000427 }
428
Henrik Boström87713d02015-08-25 09:53:21 +0200429 // Successfully init with DTLS; with a certificate generated and supplied or
430 // with a store that generates it for us.
431 void InitWithDtls(RTCCertificateGenerationMethod cert_gen_method) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200432 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator;
Henrik Boström87713d02015-08-25 09:53:21 +0200433 if (cert_gen_method == ALREADY_GENERATED) {
htaa2a49d92016-03-04 02:51:39 -0800434 configuration_.certificates.push_back(
Henrik Boströmd79599d2016-06-01 13:58:50 +0200435 FakeRTCCertificateGenerator::GenerateCertificate());
Henrik Boström87713d02015-08-25 09:53:21 +0200436 } else if (cert_gen_method == DTLS_IDENTITY_STORE) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200437 cert_generator.reset(new FakeRTCCertificateGenerator());
438 cert_generator->set_should_fail(false);
Henrik Boström87713d02015-08-25 09:53:21 +0200439 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700440 RTC_CHECK(false);
Henrik Boström87713d02015-08-25 09:53:21 +0200441 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200442 Init(std::move(cert_generator));
Henrik Boström87713d02015-08-25 09:53:21 +0200443 }
444
445 // Init with DTLS with a store that will fail to generate a certificate.
446 void InitWithDtlsIdentityGenFail() {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200447 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
448 new FakeRTCCertificateGenerator());
449 cert_generator->set_should_fail(true);
450 Init(std::move(cert_generator));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000451 }
452
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 void InitWithDtmfCodec() {
454 // Add kTelephoneEventCodec for dtmf test.
deadbeef67cf2c12016-04-13 10:07:16 -0700455 const cricket::AudioCodec kTelephoneEventCodec(106, "telephone-event", 8000,
456 0, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 std::vector<cricket::AudioCodec> codecs;
458 codecs.push_back(kTelephoneEventCodec);
459 media_engine_->SetAudioCodecs(codecs);
ossu075af922016-06-14 03:29:38 -0700460 desc_factory_->set_audio_codecs(codecs, codecs);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000461 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 }
463
jbauchcb560652016-08-04 05:20:32 -0700464 void InitWithGcm() {
465 rtc::CryptoOptions crypto_options;
466 crypto_options.enable_gcm_crypto_suites = true;
467 channel_manager_->SetCryptoOptions(crypto_options);
468 with_gcm_ = true;
469 Init();
470 }
471
deadbeefab9b2d12015-10-14 11:33:11 -0700472 void SendAudioVideoStream1() {
473 send_stream_1_ = true;
474 send_stream_2_ = false;
475 send_audio_ = true;
476 send_video_ = true;
477 }
478
479 void SendAudioVideoStream2() {
480 send_stream_1_ = false;
481 send_stream_2_ = true;
482 send_audio_ = true;
483 send_video_ = true;
484 }
485
486 void SendAudioVideoStream1And2() {
487 send_stream_1_ = true;
488 send_stream_2_ = true;
489 send_audio_ = true;
490 send_video_ = true;
491 }
492
493 void SendNothing() {
494 send_stream_1_ = false;
495 send_stream_2_ = false;
496 send_audio_ = false;
497 send_video_ = false;
498 }
499
500 void SendAudioOnlyStream2() {
501 send_stream_1_ = false;
502 send_stream_2_ = true;
503 send_audio_ = true;
504 send_video_ = false;
505 }
506
507 void SendVideoOnlyStream2() {
508 send_stream_1_ = false;
509 send_stream_2_ = true;
510 send_audio_ = false;
511 send_video_ = true;
512 }
513
514 void AddStreamsToOptions(cricket::MediaSessionOptions* session_options) {
515 if (send_stream_1_ && send_audio_) {
516 session_options->AddSendStream(cricket::MEDIA_TYPE_AUDIO, kAudioTrack1,
517 kStream1);
518 }
519 if (send_stream_1_ && send_video_) {
520 session_options->AddSendStream(cricket::MEDIA_TYPE_VIDEO, kVideoTrack1,
521 kStream1);
522 }
523 if (send_stream_2_ && send_audio_) {
524 session_options->AddSendStream(cricket::MEDIA_TYPE_AUDIO, kAudioTrack2,
525 kStream2);
526 }
527 if (send_stream_2_ && send_video_) {
528 session_options->AddSendStream(cricket::MEDIA_TYPE_VIDEO, kVideoTrack2,
529 kStream2);
530 }
531 if (data_channel_ && session_->data_channel_type() == cricket::DCT_RTP) {
532 session_options->AddSendStream(cricket::MEDIA_TYPE_DATA,
533 data_channel_->label(),
534 data_channel_->label());
535 }
536 }
537
538 void GetOptionsForOffer(
539 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
540 cricket::MediaSessionOptions* session_options) {
htaaac2dea2016-03-10 13:35:55 -0800541 ASSERT_TRUE(ExtractMediaSessionOptions(rtc_options, true, session_options));
deadbeefab9b2d12015-10-14 11:33:11 -0700542
deadbeefc80741f2015-10-22 13:14:45 -0700543 AddStreamsToOptions(session_options);
544 if (rtc_options.offer_to_receive_audio ==
545 RTCOfferAnswerOptions::kUndefined) {
546 session_options->recv_audio =
547 session_options->HasSendMediaStream(cricket::MEDIA_TYPE_AUDIO);
548 }
549 if (rtc_options.offer_to_receive_video ==
550 RTCOfferAnswerOptions::kUndefined) {
551 session_options->recv_video =
552 session_options->HasSendMediaStream(cricket::MEDIA_TYPE_VIDEO);
553 }
554 session_options->bundle_enabled =
555 session_options->bundle_enabled &&
556 (session_options->has_audio() || session_options->has_video() ||
557 session_options->has_data());
558
deadbeefab9b2d12015-10-14 11:33:11 -0700559 if (session_->data_channel_type() == cricket::DCT_SCTP && data_channel_) {
560 session_options->data_channel_type = cricket::DCT_SCTP;
zhihuang9763d562016-08-05 11:14:50 -0700561 } else if (session_->data_channel_type() == cricket::DCT_QUIC) {
562 session_options->data_channel_type = cricket::DCT_QUIC;
deadbeefab9b2d12015-10-14 11:33:11 -0700563 }
jbauchcb560652016-08-04 05:20:32 -0700564
565 if (with_gcm_) {
566 session_options->crypto_options.enable_gcm_crypto_suites = true;
567 }
deadbeefab9b2d12015-10-14 11:33:11 -0700568 }
569
htaa2a49d92016-03-04 02:51:39 -0800570 void GetOptionsForAnswer(cricket::MediaSessionOptions* session_options) {
571 // ParseConstraintsForAnswer is used to set some defaults.
572 ASSERT_TRUE(webrtc::ParseConstraintsForAnswer(nullptr, session_options));
deadbeefab9b2d12015-10-14 11:33:11 -0700573
deadbeefc80741f2015-10-22 13:14:45 -0700574 AddStreamsToOptions(session_options);
575 session_options->bundle_enabled =
576 session_options->bundle_enabled &&
577 (session_options->has_audio() || session_options->has_video() ||
578 session_options->has_data());
579
zhihuang9763d562016-08-05 11:14:50 -0700580 if (session_->data_channel_type() != cricket::DCT_RTP) {
581 session_options->data_channel_type = session_->data_channel_type();
deadbeef907abe42016-08-04 12:22:18 -0700582 }
jbauchcb560652016-08-04 05:20:32 -0700583
584 if (with_gcm_) {
585 session_options->crypto_options.enable_gcm_crypto_suites = true;
586 }
deadbeefab9b2d12015-10-14 11:33:11 -0700587 }
588
589 // Creates a local offer and applies it. Starts ICE.
590 // Call SendAudioVideoStreamX() before this function
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 // to decide which streams to create.
592 void InitiateCall() {
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000593 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 SetLocalDescriptionWithoutError(offer);
595 EXPECT_TRUE_WAIT(PeerConnectionInterface::kIceGatheringNew !=
596 observer_.ice_gathering_state_,
597 kIceCandidatesTimeout);
598 }
599
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000600 SessionDescriptionInterface* CreateOffer() {
601 PeerConnectionInterface::RTCOfferAnswerOptions options;
602 options.offer_to_receive_audio =
603 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
604
605 return CreateOffer(options);
606 }
607
wu@webrtc.org91053e72013-08-10 07:18:04 +0000608 SessionDescriptionInterface* CreateOffer(
htaa2a49d92016-03-04 02:51:39 -0800609 const PeerConnectionInterface::RTCOfferAnswerOptions options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000610 rtc::scoped_refptr<WebRtcSessionCreateSDPObserverForTest>
wu@webrtc.org91053e72013-08-10 07:18:04 +0000611 observer = new WebRtcSessionCreateSDPObserverForTest();
deadbeefab9b2d12015-10-14 11:33:11 -0700612 cricket::MediaSessionOptions session_options;
613 GetOptionsForOffer(options, &session_options);
614 session_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000615 EXPECT_TRUE_WAIT(
616 observer->state() != WebRtcSessionCreateSDPObserverForTest::kInit,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000617 2000);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000618 return observer->ReleaseDescription();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000619 }
620
621 SessionDescriptionInterface* CreateAnswer(
htaa2a49d92016-03-04 02:51:39 -0800622 const cricket::MediaSessionOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000623 rtc::scoped_refptr<WebRtcSessionCreateSDPObserverForTest> observer
wu@webrtc.org91053e72013-08-10 07:18:04 +0000624 = new WebRtcSessionCreateSDPObserverForTest();
htaa2a49d92016-03-04 02:51:39 -0800625 cricket::MediaSessionOptions session_options = options;
626 GetOptionsForAnswer(&session_options);
627 // Overwrite recv_audio and recv_video with passed-in values.
628 session_options.recv_video = options.recv_video;
629 session_options.recv_audio = options.recv_audio;
630 session_->CreateAnswer(observer, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000631 EXPECT_TRUE_WAIT(
632 observer->state() != WebRtcSessionCreateSDPObserverForTest::kInit,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000633 2000);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000634 return observer->ReleaseDescription();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000635 }
636
htaa2a49d92016-03-04 02:51:39 -0800637 SessionDescriptionInterface* CreateAnswer() {
638 cricket::MediaSessionOptions options;
639 options.recv_video = true;
640 options.recv_audio = true;
641 return CreateAnswer(options);
642 }
643
wu@webrtc.org364f2042013-11-20 21:49:41 +0000644 bool ChannelsExist() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 return (session_->voice_channel() != NULL &&
646 session_->video_channel() != NULL);
647 }
648
jbauchcb560652016-08-04 05:20:32 -0700649 void VerifyCryptoParams(const cricket::SessionDescription* sdp,
650 bool gcm_enabled = false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 ASSERT_TRUE(session_.get() != NULL);
652 const cricket::ContentInfo* content = cricket::GetFirstAudioContent(sdp);
653 ASSERT_TRUE(content != NULL);
654 const cricket::AudioContentDescription* audio_content =
655 static_cast<const cricket::AudioContentDescription*>(
656 content->description);
657 ASSERT_TRUE(audio_content != NULL);
jbauchcb560652016-08-04 05:20:32 -0700658 if (!gcm_enabled) {
659 ASSERT_EQ(1U, audio_content->cryptos().size());
660 ASSERT_EQ(47U, audio_content->cryptos()[0].key_params.size());
661 ASSERT_EQ("AES_CM_128_HMAC_SHA1_80",
662 audio_content->cryptos()[0].cipher_suite);
663 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
664 audio_content->protocol());
665 } else {
666 // The offer contains 3 possible crypto suites, the answer 1.
667 EXPECT_LE(1U, audio_content->cryptos().size());
668 EXPECT_NE(2U, audio_content->cryptos().size());
669 EXPECT_GE(3U, audio_content->cryptos().size());
670 ASSERT_EQ(67U, audio_content->cryptos()[0].key_params.size());
671 ASSERT_EQ("AEAD_AES_256_GCM",
672 audio_content->cryptos()[0].cipher_suite);
673 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
674 audio_content->protocol());
675 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676
677 content = cricket::GetFirstVideoContent(sdp);
678 ASSERT_TRUE(content != NULL);
679 const cricket::VideoContentDescription* video_content =
680 static_cast<const cricket::VideoContentDescription*>(
681 content->description);
682 ASSERT_TRUE(video_content != NULL);
jbauchcb560652016-08-04 05:20:32 -0700683 if (!gcm_enabled) {
684 ASSERT_EQ(1U, video_content->cryptos().size());
685 ASSERT_EQ("AES_CM_128_HMAC_SHA1_80",
686 video_content->cryptos()[0].cipher_suite);
687 ASSERT_EQ(47U, video_content->cryptos()[0].key_params.size());
688 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
689 video_content->protocol());
690 } else {
691 // The offer contains 3 possible crypto suites, the answer 1.
692 EXPECT_LE(1U, video_content->cryptos().size());
693 EXPECT_NE(2U, video_content->cryptos().size());
694 EXPECT_GE(3U, video_content->cryptos().size());
695 ASSERT_EQ("AEAD_AES_256_GCM",
696 video_content->cryptos()[0].cipher_suite);
697 ASSERT_EQ(67U, video_content->cryptos()[0].key_params.size());
698 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
699 video_content->protocol());
700 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 }
702
703 void VerifyNoCryptoParams(const cricket::SessionDescription* sdp, bool dtls) {
704 const cricket::ContentInfo* content = cricket::GetFirstAudioContent(sdp);
705 ASSERT_TRUE(content != NULL);
706 const cricket::AudioContentDescription* audio_content =
707 static_cast<const cricket::AudioContentDescription*>(
708 content->description);
709 ASSERT_TRUE(audio_content != NULL);
710 ASSERT_EQ(0U, audio_content->cryptos().size());
711
712 content = cricket::GetFirstVideoContent(sdp);
713 ASSERT_TRUE(content != NULL);
714 const cricket::VideoContentDescription* video_content =
715 static_cast<const cricket::VideoContentDescription*>(
716 content->description);
717 ASSERT_TRUE(video_content != NULL);
718 ASSERT_EQ(0U, video_content->cryptos().size());
719
720 if (dtls) {
deadbeeff3938292015-07-15 12:20:53 -0700721 EXPECT_EQ(std::string(cricket::kMediaProtocolDtlsSavpf),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 audio_content->protocol());
deadbeeff3938292015-07-15 12:20:53 -0700723 EXPECT_EQ(std::string(cricket::kMediaProtocolDtlsSavpf),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 video_content->protocol());
725 } else {
726 EXPECT_EQ(std::string(cricket::kMediaProtocolAvpf),
727 audio_content->protocol());
728 EXPECT_EQ(std::string(cricket::kMediaProtocolAvpf),
729 video_content->protocol());
730 }
731 }
732
733 // Set the internal fake description factories to do DTLS-SRTP.
734 void SetFactoryDtlsSrtp() {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000735 desc_factory_->set_secure(cricket::SEC_DISABLED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 std::string identity_name = "WebRTC" +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000737 rtc::ToString(rtc::CreateRandomId());
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200738 // Confirmed to work with KT_RSA and KT_ECDSA.
kwiberg0eb15ed2015-12-17 03:04:15 -0800739 tdesc_factory_->set_certificate(
jbauch555604a2016-04-26 03:13:22 -0700740 rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>(
kwiberg0eb15ed2015-12-17 03:04:15 -0800741 rtc::SSLIdentity::Generate(identity_name, rtc::KT_DEFAULT))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 tdesc_factory_->set_secure(cricket::SEC_REQUIRED);
743 }
744
745 void VerifyFingerprintStatus(const cricket::SessionDescription* sdp,
746 bool expected) {
747 const TransportInfo* audio = sdp->GetTransportInfoByName("audio");
748 ASSERT_TRUE(audio != NULL);
749 ASSERT_EQ(expected, audio->description.identity_fingerprint.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 const TransportInfo* video = sdp->GetTransportInfoByName("video");
751 ASSERT_TRUE(video != NULL);
752 ASSERT_EQ(expected, video->description.identity_fingerprint.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 }
754
755 void VerifyAnswerFromNonCryptoOffer() {
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +0000756 // Create an SDP without Crypto.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +0000758 options.recv_video = true;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000759 JsepSessionDescription* offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 CreateRemoteOffer(options, cricket::SEC_DISABLED));
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000761 ASSERT_TRUE(offer != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 VerifyNoCryptoParams(offer->description(), false);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000763 SetRemoteDescriptionOfferExpectError(kSdpWithoutSdesCrypto,
764 offer);
htaa2a49d92016-03-04 02:51:39 -0800765 const webrtc::SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 // Answer should be NULL as no crypto params in offer.
767 ASSERT_TRUE(answer == NULL);
768 }
769
770 void VerifyAnswerFromCryptoOffer() {
771 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +0000772 options.recv_video = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 options.bundle_enabled = true;
kwibergd1fe2812016-04-27 06:47:29 -0700774 std::unique_ptr<JsepSessionDescription> offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 CreateRemoteOffer(options, cricket::SEC_REQUIRED));
776 ASSERT_TRUE(offer.get() != NULL);
777 VerifyCryptoParams(offer->description());
778 SetRemoteDescriptionWithoutError(offer.release());
kwibergd1fe2812016-04-27 06:47:29 -0700779 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 ASSERT_TRUE(answer.get() != NULL);
781 VerifyCryptoParams(answer->description());
782 }
783
deadbeef0ed85b22016-02-23 17:24:52 -0800784 bool IceUfragPwdEqual(const cricket::SessionDescription* desc1,
785 const cricket::SessionDescription* desc2) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 if (desc1->contents().size() != desc2->contents().size()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800787 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 }
789
790 const cricket::ContentInfos& contents = desc1->contents();
791 cricket::ContentInfos::const_iterator it = contents.begin();
792
793 for (; it != contents.end(); ++it) {
794 const cricket::TransportDescription* transport_desc1 =
795 desc1->GetTransportDescriptionByName(it->name);
796 const cricket::TransportDescription* transport_desc2 =
797 desc2->GetTransportDescriptionByName(it->name);
798 if (!transport_desc1 || !transport_desc2) {
deadbeef0ed85b22016-02-23 17:24:52 -0800799 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 }
801 if (transport_desc1->ice_pwd != transport_desc2->ice_pwd ||
802 transport_desc1->ice_ufrag != transport_desc2->ice_ufrag) {
deadbeef0ed85b22016-02-23 17:24:52 -0800803 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 }
805 }
deadbeef0ed85b22016-02-23 17:24:52 -0800806 return true;
807 }
808
809 // Compares ufrag/password only for the specified |media_type|.
810 bool IceUfragPwdEqual(const cricket::SessionDescription* desc1,
811 const cricket::SessionDescription* desc2,
812 cricket::MediaType media_type) {
813 if (desc1->contents().size() != desc2->contents().size()) {
814 return false;
815 }
816
817 const cricket::ContentInfo* cinfo =
818 cricket::GetFirstMediaContent(desc1->contents(), media_type);
819 const cricket::TransportDescription* transport_desc1 =
820 desc1->GetTransportDescriptionByName(cinfo->name);
821 const cricket::TransportDescription* transport_desc2 =
822 desc2->GetTransportDescriptionByName(cinfo->name);
823 if (!transport_desc1 || !transport_desc2) {
824 return false;
825 }
826 if (transport_desc1->ice_pwd != transport_desc2->ice_pwd ||
827 transport_desc1->ice_ufrag != transport_desc2->ice_ufrag) {
828 return false;
829 }
830 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000832
833 void RemoveIceUfragPwdLines(const SessionDescriptionInterface* current_desc,
834 std::string *sdp) {
835 const cricket::SessionDescription* desc = current_desc->description();
836 EXPECT_TRUE(current_desc->ToString(sdp));
837
838 const cricket::ContentInfos& contents = desc->contents();
839 cricket::ContentInfos::const_iterator it = contents.begin();
840 // Replace ufrag and pwd lines with empty strings.
841 for (; it != contents.end(); ++it) {
842 const cricket::TransportDescription* transport_desc =
843 desc->GetTransportDescriptionByName(it->name);
844 std::string ufrag_line = "a=ice-ufrag:" + transport_desc->ice_ufrag
845 + "\r\n";
846 std::string pwd_line = "a=ice-pwd:" + transport_desc->ice_pwd
847 + "\r\n";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000848 rtc::replace_substrs(ufrag_line.c_str(), ufrag_line.length(),
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000849 "", 0,
850 sdp);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000851 rtc::replace_substrs(pwd_line.c_str(), pwd_line.length(),
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000852 "", 0,
853 sdp);
854 }
855 }
856
deadbeef0ed85b22016-02-23 17:24:52 -0800857 void SetIceUfragPwd(SessionDescriptionInterface* current_desc,
858 const std::string& ufrag,
859 const std::string& pwd) {
860 cricket::SessionDescription* desc = current_desc->description();
861 for (TransportInfo& transport_info : desc->transport_infos()) {
862 cricket::TransportDescription& transport_desc =
863 transport_info.description;
864 transport_desc.ice_ufrag = ufrag;
865 transport_desc.ice_pwd = pwd;
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +0000866 }
867 }
868
deadbeef0ed85b22016-02-23 17:24:52 -0800869 // Sets ufrag/pwd for specified |media_type|.
870 void SetIceUfragPwd(SessionDescriptionInterface* current_desc,
871 cricket::MediaType media_type,
872 const std::string& ufrag,
873 const std::string& pwd) {
874 cricket::SessionDescription* desc = current_desc->description();
875 const cricket::ContentInfo* cinfo =
876 cricket::GetFirstMediaContent(desc->contents(), media_type);
877 TransportInfo* transport_info = desc->GetTransportInfoByName(cinfo->name);
878 cricket::TransportDescription* transport_desc =
879 &transport_info->description;
880 transport_desc->ice_ufrag = ufrag;
881 transport_desc->ice_pwd = pwd;
882 }
883
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 // Creates a remote offer and and applies it as a remote description,
885 // creates a local answer and applies is as a local description.
deadbeefab9b2d12015-10-14 11:33:11 -0700886 // Call SendAudioVideoStreamX() before this function
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 // to decide which local and remote streams to create.
888 void CreateAndSetRemoteOfferAndLocalAnswer() {
889 SessionDescriptionInterface* offer = CreateRemoteOffer();
890 SetRemoteDescriptionWithoutError(offer);
htaa2a49d92016-03-04 02:51:39 -0800891 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 SetLocalDescriptionWithoutError(answer);
893 }
894 void SetLocalDescriptionWithoutError(SessionDescriptionInterface* desc) {
895 EXPECT_TRUE(session_->SetLocalDescription(desc, NULL));
deadbeefcbecd352015-09-23 11:50:27 -0700896 session_->MaybeStartGathering();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 }
898 void SetLocalDescriptionExpectState(SessionDescriptionInterface* desc,
deadbeefd59daf82015-10-14 15:02:44 -0700899 WebRtcSession::State expected_state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 SetLocalDescriptionWithoutError(desc);
901 EXPECT_EQ(expected_state, session_->state());
902 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000903 void SetLocalDescriptionExpectError(const std::string& action,
904 const std::string& expected_error,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 SessionDescriptionInterface* desc) {
906 std::string error;
907 EXPECT_FALSE(session_->SetLocalDescription(desc, &error));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000908 std::string sdp_type = "local ";
909 sdp_type.append(action);
910 EXPECT_NE(std::string::npos, error.find(sdp_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 EXPECT_NE(std::string::npos, error.find(expected_error));
912 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000913 void SetLocalDescriptionOfferExpectError(const std::string& expected_error,
914 SessionDescriptionInterface* desc) {
915 SetLocalDescriptionExpectError(SessionDescriptionInterface::kOffer,
916 expected_error, desc);
917 }
918 void SetLocalDescriptionAnswerExpectError(const std::string& expected_error,
919 SessionDescriptionInterface* desc) {
920 SetLocalDescriptionExpectError(SessionDescriptionInterface::kAnswer,
921 expected_error, desc);
922 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 void SetRemoteDescriptionWithoutError(SessionDescriptionInterface* desc) {
924 EXPECT_TRUE(session_->SetRemoteDescription(desc, NULL));
925 }
926 void SetRemoteDescriptionExpectState(SessionDescriptionInterface* desc,
deadbeefd59daf82015-10-14 15:02:44 -0700927 WebRtcSession::State expected_state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 SetRemoteDescriptionWithoutError(desc);
929 EXPECT_EQ(expected_state, session_->state());
930 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000931 void SetRemoteDescriptionExpectError(const std::string& action,
932 const std::string& expected_error,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 SessionDescriptionInterface* desc) {
934 std::string error;
935 EXPECT_FALSE(session_->SetRemoteDescription(desc, &error));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000936 std::string sdp_type = "remote ";
937 sdp_type.append(action);
938 EXPECT_NE(std::string::npos, error.find(sdp_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 EXPECT_NE(std::string::npos, error.find(expected_error));
940 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000941 void SetRemoteDescriptionOfferExpectError(
942 const std::string& expected_error, SessionDescriptionInterface* desc) {
943 SetRemoteDescriptionExpectError(SessionDescriptionInterface::kOffer,
944 expected_error, desc);
945 }
946 void SetRemoteDescriptionPranswerExpectError(
947 const std::string& expected_error, SessionDescriptionInterface* desc) {
948 SetRemoteDescriptionExpectError(SessionDescriptionInterface::kPrAnswer,
949 expected_error, desc);
950 }
951 void SetRemoteDescriptionAnswerExpectError(
952 const std::string& expected_error, SessionDescriptionInterface* desc) {
953 SetRemoteDescriptionExpectError(SessionDescriptionInterface::kAnswer,
954 expected_error, desc);
955 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956
957 void CreateCryptoOfferAndNonCryptoAnswer(SessionDescriptionInterface** offer,
958 SessionDescriptionInterface** nocrypto_answer) {
959 // Create a SDP without Crypto.
960 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +0000961 options.recv_video = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 options.bundle_enabled = true;
963 *offer = CreateRemoteOffer(options, cricket::SEC_ENABLED);
964 ASSERT_TRUE(*offer != NULL);
965 VerifyCryptoParams((*offer)->description());
966
967 *nocrypto_answer = CreateRemoteAnswer(*offer, options,
968 cricket::SEC_DISABLED);
969 EXPECT_TRUE(*nocrypto_answer != NULL);
970 }
971
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000972 void CreateDtlsOfferAndNonDtlsAnswer(SessionDescriptionInterface** offer,
973 SessionDescriptionInterface** nodtls_answer) {
974 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +0000975 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000976 options.bundle_enabled = true;
977
kwibergd1fe2812016-04-27 06:47:29 -0700978 std::unique_ptr<SessionDescriptionInterface> temp_offer(
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000979 CreateRemoteOffer(options, cricket::SEC_ENABLED));
980
981 *nodtls_answer =
982 CreateRemoteAnswer(temp_offer.get(), options, cricket::SEC_ENABLED);
983 EXPECT_TRUE(*nodtls_answer != NULL);
984 VerifyFingerprintStatus((*nodtls_answer)->description(), false);
985 VerifyCryptoParams((*nodtls_answer)->description());
986
987 SetFactoryDtlsSrtp();
988 *offer = CreateRemoteOffer(options, cricket::SEC_ENABLED);
989 ASSERT_TRUE(*offer != NULL);
990 VerifyFingerprintStatus((*offer)->description(), true);
991 VerifyCryptoParams((*offer)->description());
992 }
993
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 JsepSessionDescription* CreateRemoteOfferWithVersion(
995 cricket::MediaSessionOptions options,
996 cricket::SecurePolicy secure_policy,
997 const std::string& session_version,
998 const SessionDescriptionInterface* current_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000999 std::string session_id = rtc::ToString(rtc::CreateRandomId64());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 const cricket::SessionDescription* cricket_desc = NULL;
1001 if (current_desc) {
1002 cricket_desc = current_desc->description();
1003 session_id = current_desc->session_id();
1004 }
1005
1006 desc_factory_->set_secure(secure_policy);
1007 JsepSessionDescription* offer(
1008 new JsepSessionDescription(JsepSessionDescription::kOffer));
1009 if (!offer->Initialize(desc_factory_->CreateOffer(options, cricket_desc),
1010 session_id, session_version)) {
1011 delete offer;
1012 offer = NULL;
1013 }
1014 return offer;
1015 }
1016 JsepSessionDescription* CreateRemoteOffer(
1017 cricket::MediaSessionOptions options) {
1018 return CreateRemoteOfferWithVersion(options, cricket::SEC_ENABLED,
1019 kSessionVersion, NULL);
1020 }
1021 JsepSessionDescription* CreateRemoteOffer(
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001022 cricket::MediaSessionOptions options, cricket::SecurePolicy sdes_policy) {
1023 return CreateRemoteOfferWithVersion(
1024 options, sdes_policy, kSessionVersion, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 }
1026 JsepSessionDescription* CreateRemoteOffer(
1027 cricket::MediaSessionOptions options,
1028 const SessionDescriptionInterface* current_desc) {
1029 return CreateRemoteOfferWithVersion(options, cricket::SEC_ENABLED,
1030 kSessionVersion, current_desc);
1031 }
1032
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001033 JsepSessionDescription* CreateRemoteOfferWithSctpPort(
1034 const char* sctp_stream_name, int new_port,
1035 cricket::MediaSessionOptions options) {
1036 options.data_channel_type = cricket::DCT_SCTP;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001037 options.AddSendStream(cricket::MEDIA_TYPE_DATA, "datachannel",
1038 sctp_stream_name);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001039 return ChangeSDPSctpPort(new_port, CreateRemoteOffer(options));
1040 }
1041
1042 // Takes ownership of offer_basis (and deletes it).
1043 JsepSessionDescription* ChangeSDPSctpPort(
1044 int new_port, webrtc::SessionDescriptionInterface *offer_basis) {
1045 // Stringify the input SDP, swap the 5000 for 'new_port' and create a new
1046 // SessionDescription from the mutated string.
1047 const char* default_port_str = "5000";
1048 char new_port_str[16];
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001049 rtc::sprintfn(new_port_str, sizeof(new_port_str), "%d", new_port);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001050 std::string offer_str;
1051 offer_basis->ToString(&offer_str);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001052 rtc::replace_substrs(default_port_str, strlen(default_port_str),
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001053 new_port_str, strlen(new_port_str),
1054 &offer_str);
1055 JsepSessionDescription* offer = new JsepSessionDescription(
1056 offer_basis->type());
1057 delete offer_basis;
1058 offer->Initialize(offer_str, NULL);
1059 return offer;
1060 }
1061
deadbeefab9b2d12015-10-14 11:33:11 -07001062 // Create a remote offer. Call SendAudioVideoStreamX()
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 // before this function to decide which streams to create.
1064 JsepSessionDescription* CreateRemoteOffer() {
1065 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -08001066 GetOptionsForAnswer(&options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 return CreateRemoteOffer(options, session_->remote_description());
1068 }
1069
1070 JsepSessionDescription* CreateRemoteAnswer(
1071 const SessionDescriptionInterface* offer,
1072 cricket::MediaSessionOptions options,
1073 cricket::SecurePolicy policy) {
1074 desc_factory_->set_secure(policy);
1075 const std::string session_id =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001076 rtc::ToString(rtc::CreateRandomId64());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 JsepSessionDescription* answer(
1078 new JsepSessionDescription(JsepSessionDescription::kAnswer));
1079 if (!answer->Initialize(desc_factory_->CreateAnswer(offer->description(),
1080 options, NULL),
1081 session_id, kSessionVersion)) {
1082 delete answer;
1083 answer = NULL;
1084 }
1085 return answer;
1086 }
1087
1088 JsepSessionDescription* CreateRemoteAnswer(
1089 const SessionDescriptionInterface* offer,
1090 cricket::MediaSessionOptions options) {
1091 return CreateRemoteAnswer(offer, options, cricket::SEC_REQUIRED);
1092 }
1093
deadbeefab9b2d12015-10-14 11:33:11 -07001094 // Creates an answer session description.
1095 // Call SendAudioVideoStreamX() before this function
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 // to decide which streams to create.
1097 JsepSessionDescription* CreateRemoteAnswer(
1098 const SessionDescriptionInterface* offer) {
1099 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -08001100 GetOptionsForAnswer(&options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 return CreateRemoteAnswer(offer, options, cricket::SEC_REQUIRED);
1102 }
1103
1104 void TestSessionCandidatesWithBundleRtcpMux(bool bundle, bool rtcp_mux) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001105 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001106 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001107 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001108
1109 PeerConnectionInterface::RTCOfferAnswerOptions options;
1110 options.use_rtp_mux = bundle;
1111
1112 SessionDescriptionInterface* offer = CreateOffer(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
1114 // and answer.
1115 SetLocalDescriptionWithoutError(offer);
1116
kwibergd1fe2812016-04-27 06:47:29 -07001117 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org723d6832013-07-12 16:04:50 +00001118 CreateRemoteAnswer(session_->local_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 std::string sdp;
1120 EXPECT_TRUE(answer->ToString(&sdp));
1121
1122 size_t expected_candidate_num = 2;
1123 if (!rtcp_mux) {
1124 // If rtcp_mux is enabled we should expect 4 candidates - host and srflex
1125 // for rtp and rtcp.
1126 expected_candidate_num = 4;
1127 // Disable rtcp-mux from the answer
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 const std::string kRtcpMux = "a=rtcp-mux";
1129 const std::string kXRtcpMux = "a=xrtcp-mux";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001130 rtc::replace_substrs(kRtcpMux.c_str(), kRtcpMux.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 kXRtcpMux.c_str(), kXRtcpMux.length(),
1132 &sdp);
1133 }
1134
1135 SessionDescriptionInterface* new_answer = CreateSessionDescription(
1136 JsepSessionDescription::kAnswer, sdp, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137
1138 // SetRemoteDescription to enable rtcp mux.
henrike@webrtc.org723d6832013-07-12 16:04:50 +00001139 SetRemoteDescriptionWithoutError(new_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1141 EXPECT_EQ(expected_candidate_num, observer_.mline_0_candidates_.size());
deadbeefcbecd352015-09-23 11:50:27 -07001142 if (bundle) {
1143 EXPECT_EQ(0, observer_.mline_1_candidates_.size());
1144 } else {
1145 EXPECT_EQ(expected_candidate_num, observer_.mline_1_candidates_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 }
1147 }
1148 // Tests that we can only send DTMF when the dtmf codec is supported.
1149 void TestCanInsertDtmf(bool can) {
1150 if (can) {
1151 InitWithDtmfCodec();
1152 } else {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001153 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 }
deadbeefab9b2d12015-10-14 11:33:11 -07001155 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 CreateAndSetRemoteOfferAndLocalAnswer();
1157 EXPECT_FALSE(session_->CanInsertDtmf(""));
1158 EXPECT_EQ(can, session_->CanInsertDtmf(kAudioTrack1));
1159 }
1160
zhihuang3a334652016-05-05 18:37:49 -07001161 bool ContainsVideoCodecWithName(const SessionDescriptionInterface* desc,
1162 const std::string& codec_name) {
1163 for (const auto& content : desc->description()->contents()) {
1164 if (static_cast<cricket::MediaContentDescription*>(content.description)
1165 ->type() == cricket::MEDIA_TYPE_VIDEO) {
1166 const auto* mdesc =
1167 static_cast<cricket::VideoContentDescription*>(content.description);
1168 for (const auto& codec : mdesc->codecs()) {
1169 if (codec.name == codec_name) {
1170 return true;
1171 }
1172 }
1173 }
1174 }
1175 return false;
1176 }
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001177 // Helper class to configure loopback network and verify Best
1178 // Connection using right IP protocol for TestLoopbackCall
1179 // method. LoopbackNetworkManager applies firewall rules to block
1180 // all ping traffic once ICE completed, and remove them to observe
1181 // ICE reconnected again. This LoopbackNetworkConfiguration struct
1182 // verifies the best connection is using the right IP protocol after
1183 // initial ICE convergences.
1184
1185 class LoopbackNetworkConfiguration {
deadbeefcbecd352015-09-23 11:50:27 -07001186 public:
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001187 LoopbackNetworkConfiguration()
1188 : test_ipv6_network_(false),
1189 test_extra_ipv4_network_(false),
1190 best_connection_after_initial_ice_converged_(1, 0) {}
1191
1192 // Used to track the expected best connection count in each IP protocol.
1193 struct ExpectedBestConnection {
1194 ExpectedBestConnection(int ipv4_count, int ipv6_count)
1195 : ipv4_count_(ipv4_count),
1196 ipv6_count_(ipv6_count) {}
1197
1198 int ipv4_count_;
1199 int ipv6_count_;
1200 };
1201
1202 bool test_ipv6_network_;
1203 bool test_extra_ipv4_network_;
1204 ExpectedBestConnection best_connection_after_initial_ice_converged_;
1205
1206 void VerifyBestConnectionAfterIceConverge(
jbauchac8869e2015-07-03 01:36:14 -07001207 const rtc::scoped_refptr<FakeMetricsObserver> metrics_observer) const {
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001208 Verify(metrics_observer, best_connection_after_initial_ice_converged_);
1209 }
1210
1211 private:
jbauchac8869e2015-07-03 01:36:14 -07001212 void Verify(const rtc::scoped_refptr<FakeMetricsObserver> metrics_observer,
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001213 const ExpectedBestConnection& expected) const {
1214 EXPECT_EQ(
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001215 metrics_observer->GetEnumCounter(webrtc::kEnumCounterAddressFamily,
1216 webrtc::kBestConnections_IPv4),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001217 expected.ipv4_count_);
1218 EXPECT_EQ(
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001219 metrics_observer->GetEnumCounter(webrtc::kEnumCounterAddressFamily,
1220 webrtc::kBestConnections_IPv6),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001221 expected.ipv6_count_);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001222 // This is used in the loopback call so there is only single host to host
1223 // candidate pair.
1224 EXPECT_EQ(metrics_observer->GetEnumCounter(
1225 webrtc::kEnumCounterIceCandidatePairTypeUdp,
1226 webrtc::kIceCandidatePairHostHost),
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -07001227 0);
1228 EXPECT_EQ(metrics_observer->GetEnumCounter(
1229 webrtc::kEnumCounterIceCandidatePairTypeUdp,
1230 webrtc::kIceCandidatePairHostPublicHostPublic),
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001231 1);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001232 }
1233 };
1234
1235 class LoopbackNetworkManager {
1236 public:
1237 LoopbackNetworkManager(WebRtcSessionTest* session,
1238 const LoopbackNetworkConfiguration& config)
1239 : config_(config) {
1240 session->AddInterface(
1241 rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
1242 if (config_.test_extra_ipv4_network_) {
1243 session->AddInterface(
1244 rtc::SocketAddress(kClientAddrHost2, kClientAddrPort));
1245 }
1246 if (config_.test_ipv6_network_) {
1247 session->AddInterface(
1248 rtc::SocketAddress(kClientIPv6AddrHost1, kClientAddrPort));
1249 }
1250 }
1251
1252 void ApplyFirewallRules(rtc::FirewallSocketServer* fss) {
1253 fss->AddRule(false, rtc::FP_ANY, rtc::FD_ANY,
1254 rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
1255 if (config_.test_extra_ipv4_network_) {
1256 fss->AddRule(false, rtc::FP_ANY, rtc::FD_ANY,
1257 rtc::SocketAddress(kClientAddrHost2, kClientAddrPort));
1258 }
1259 if (config_.test_ipv6_network_) {
1260 fss->AddRule(false, rtc::FP_ANY, rtc::FD_ANY,
1261 rtc::SocketAddress(kClientIPv6AddrHost1, kClientAddrPort));
1262 }
1263 }
1264
1265 void ClearRules(rtc::FirewallSocketServer* fss) { fss->ClearRules(); }
1266
1267 private:
1268 LoopbackNetworkConfiguration config_;
1269 };
1270
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 // The method sets up a call from the session to itself, in a loopback
1272 // arrangement. It also uses a firewall rule to create a temporary
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001273 // disconnection, and then a permanent disconnection.
1274 // This code is placed in a method so that it can be invoked
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 // by multiple tests with different allocators (e.g. with and without BUNDLE).
1276 // While running the call, this method also checks if the session goes through
1277 // the correct sequence of ICE states when a connection is established,
1278 // broken, and re-established.
1279 // The Connection state should go:
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001280 // New -> Checking -> (Connected) -> Completed -> Disconnected -> Completed
1281 // -> Failed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 // The Gathering state should go: New -> Gathering -> Completed.
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001283
stefanc1aeaf02015-10-15 07:26:07 -07001284 void SetupLoopbackCall() {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001285 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001286 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001287 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288
1289 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
1290 observer_.ice_gathering_state_);
1291 SetLocalDescriptionWithoutError(offer);
1292 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
1293 observer_.ice_connection_state_);
1294 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringGathering,
stefanc1aeaf02015-10-15 07:26:07 -07001295 observer_.ice_gathering_state_, kIceCandidatesTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1297 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
stefanc1aeaf02015-10-15 07:26:07 -07001298 observer_.ice_gathering_state_, kIceCandidatesTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299
1300 std::string sdp;
1301 offer->ToString(&sdp);
stefanc1aeaf02015-10-15 07:26:07 -07001302 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
1303 JsepSessionDescription::kAnswer, sdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 ASSERT_TRUE(desc != NULL);
1305 SetRemoteDescriptionWithoutError(desc);
1306
1307 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionChecking,
stefanc1aeaf02015-10-15 07:26:07 -07001308 observer_.ice_connection_state_, kIceCandidatesTimeout);
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001309
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001310 // The ice connection state is "Connected" too briefly to catch in a test.
1311 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
stefanc1aeaf02015-10-15 07:26:07 -07001312 observer_.ice_connection_state_, kIceCandidatesTimeout);
1313 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314
stefanc1aeaf02015-10-15 07:26:07 -07001315 void TestLoopbackCall(const LoopbackNetworkConfiguration& config) {
1316 LoopbackNetworkManager loopback_network_manager(this, config);
1317 SetupLoopbackCall();
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001318 config.VerifyBestConnectionAfterIceConverge(metrics_observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 // Adding firewall rule to block ping requests, which should cause
1320 // transport channel failure.
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001321
1322 loopback_network_manager.ApplyFirewallRules(fss_.get());
1323
1324 LOG(LS_INFO) << "Firewall Rules applied";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
1326 observer_.ice_connection_state_,
1327 kIceCandidatesTimeout);
1328
jbauchac8869e2015-07-03 01:36:14 -07001329 metrics_observer_->Reset();
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001330
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 // Clearing the rules, session should move back to completed state.
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001332 loopback_network_manager.ClearRules(fss_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001334 LOG(LS_INFO) << "Firewall Rules cleared";
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001335 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336 observer_.ice_connection_state_,
1337 kIceCandidatesTimeout);
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001338
1339 // Now we block ping requests and wait until the ICE connection transitions
1340 // to the Failed state. This will take at least 30 seconds because it must
1341 // wait for the Port to timeout.
1342 int port_timeout = 30000;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001343
1344 loopback_network_manager.ApplyFirewallRules(fss_.get());
1345 LOG(LS_INFO) << "Firewall Rules applied again";
jlmiller@webrtc.org804eb462015-02-20 02:20:03 +00001346 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001347 observer_.ice_connection_state_,
1348 kIceCandidatesTimeout + port_timeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349 }
1350
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001351 void TestLoopbackCall() {
1352 LoopbackNetworkConfiguration config;
1353 TestLoopbackCall(config);
1354 }
1355
stefanc1aeaf02015-10-15 07:26:07 -07001356 void TestPacketOptions() {
1357 media_controller_.reset(
1358 new cricket::FakeMediaController(channel_manager_.get(), &fake_call_));
1359 LoopbackNetworkConfiguration config;
1360 LoopbackNetworkManager loopback_network_manager(this, config);
1361
1362 SetupLoopbackCall();
1363
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001364 // Wait for channel to be ready for sending.
1365 EXPECT_TRUE_WAIT(media_engine_->GetVideoChannel(0)->sending(), 100);
stefanc1aeaf02015-10-15 07:26:07 -07001366 uint8_t test_packet[15] = {0};
1367 rtc::PacketOptions options;
1368 options.packet_id = 10;
1369 media_engine_->GetVideoChannel(0)
1370 ->SendRtp(test_packet, sizeof(test_packet), options);
1371
1372 const int kPacketTimeout = 2000;
deadbeef14461d42016-06-15 11:06:57 -07001373 EXPECT_EQ_WAIT(10, fake_call_.last_sent_nonnegative_packet_id(),
1374 kPacketTimeout);
stefanc1aeaf02015-10-15 07:26:07 -07001375 EXPECT_GT(fake_call_.last_sent_packet().send_time_ms, -1);
1376 }
1377
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378 // Adds CN codecs to FakeMediaEngine and MediaDescriptionFactory.
1379 void AddCNCodecs() {
deadbeef67cf2c12016-04-13 10:07:16 -07001380 const cricket::AudioCodec kCNCodec1(102, "CN", 8000, 0, 1);
1381 const cricket::AudioCodec kCNCodec2(103, "CN", 16000, 0, 1);
wu@webrtc.org364f2042013-11-20 21:49:41 +00001382
1383 // Add kCNCodec for dtmf test.
ossudedfd282016-06-14 07:12:39 -07001384 std::vector<cricket::AudioCodec> codecs =
1385 media_engine_->audio_send_codecs();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386 codecs.push_back(kCNCodec1);
1387 codecs.push_back(kCNCodec2);
1388 media_engine_->SetAudioCodecs(codecs);
ossu075af922016-06-14 03:29:38 -07001389 desc_factory_->set_audio_codecs(codecs, codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390 }
1391
1392 bool VerifyNoCNCodecs(const cricket::ContentInfo* content) {
1393 const cricket::ContentDescription* description = content->description;
1394 ASSERT(description != NULL);
1395 const cricket::AudioContentDescription* audio_content_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001396 static_cast<const cricket::AudioContentDescription*>(description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 ASSERT(audio_content_desc != NULL);
1398 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1399 if (audio_content_desc->codecs()[i].name == "CN")
1400 return false;
1401 }
1402 return true;
1403 }
1404
deadbeefab9b2d12015-10-14 11:33:11 -07001405 void CreateDataChannel() {
deadbeeffc648b62015-10-13 16:42:33 -07001406 webrtc::InternalDataChannelInit dci;
htaa2a49d92016-03-04 02:51:39 -08001407 ASSERT(session_.get());
deadbeefab9b2d12015-10-14 11:33:11 -07001408 dci.reliable = session_->data_channel_type() == cricket::DCT_SCTP;
1409 data_channel_ = DataChannel::Create(
1410 session_.get(), session_->data_channel_type(), "datachannel", dci);
1411 }
1412
1413 void SetLocalDescriptionWithDataChannel() {
1414 CreateDataChannel();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001415 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 SetLocalDescriptionWithoutError(offer);
1417 }
1418
wu@webrtc.org91053e72013-08-10 07:18:04 +00001419 void VerifyMultipleAsyncCreateDescription(
Henrik Boström87713d02015-08-25 09:53:21 +02001420 RTCCertificateGenerationMethod cert_gen_method,
1421 CreateSessionDescriptionRequest::Type type) {
1422 InitWithDtls(cert_gen_method);
1423 VerifyMultipleAsyncCreateDescriptionAfterInit(true, type);
1424 }
1425
1426 void VerifyMultipleAsyncCreateDescriptionIdentityGenFailure(
1427 CreateSessionDescriptionRequest::Type type) {
1428 InitWithDtlsIdentityGenFail();
1429 VerifyMultipleAsyncCreateDescriptionAfterInit(false, type);
1430 }
1431
1432 void VerifyMultipleAsyncCreateDescriptionAfterInit(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001433 bool success, CreateSessionDescriptionRequest::Type type) {
henrikg91d6ede2015-09-17 00:24:34 -07001434 RTC_CHECK(session_);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001435 SetFactoryDtlsSrtp();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001436 if (type == CreateSessionDescriptionRequest::kAnswer) {
1437 cricket::MediaSessionOptions options;
kwibergd1fe2812016-04-27 06:47:29 -07001438 std::unique_ptr<JsepSessionDescription> offer(
1439 CreateRemoteOffer(options, cricket::SEC_DISABLED));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001440 ASSERT_TRUE(offer.get() != NULL);
1441 SetRemoteDescriptionWithoutError(offer.release());
1442 }
1443
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001444 PeerConnectionInterface::RTCOfferAnswerOptions options;
deadbeefab9b2d12015-10-14 11:33:11 -07001445 cricket::MediaSessionOptions session_options;
wu@webrtc.org91053e72013-08-10 07:18:04 +00001446 const int kNumber = 3;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001447 rtc::scoped_refptr<WebRtcSessionCreateSDPObserverForTest>
wu@webrtc.org91053e72013-08-10 07:18:04 +00001448 observers[kNumber];
1449 for (int i = 0; i < kNumber; ++i) {
1450 observers[i] = new WebRtcSessionCreateSDPObserverForTest();
1451 if (type == CreateSessionDescriptionRequest::kOffer) {
deadbeefab9b2d12015-10-14 11:33:11 -07001452 session_->CreateOffer(observers[i], options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001453 } else {
htaa2a49d92016-03-04 02:51:39 -08001454 session_->CreateAnswer(observers[i], session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001455 }
1456 }
1457
1458 WebRtcSessionCreateSDPObserverForTest::State expected_state =
1459 success ? WebRtcSessionCreateSDPObserverForTest::kSucceeded :
1460 WebRtcSessionCreateSDPObserverForTest::kFailed;
1461
1462 for (int i = 0; i < kNumber; ++i) {
1463 EXPECT_EQ_WAIT(expected_state, observers[i]->state(), 1000);
1464 if (success) {
1465 EXPECT_TRUE(observers[i]->description() != NULL);
1466 } else {
1467 EXPECT_TRUE(observers[i]->description() == NULL);
1468 }
1469 }
1470 }
1471
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001472 void ConfigureAllocatorWithTurn() {
deadbeef653b8e02015-11-11 12:55:10 -08001473 cricket::RelayServerConfig turn_server(cricket::RELAY_TURN);
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001474 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001475 turn_server.credentials = credentials;
1476 turn_server.ports.push_back(
1477 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
1478 allocator_->AddTurnServer(turn_server);
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001479 allocator_->set_step_delay(cricket::kMinimumStepDelay);
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001480 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP);
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001481 }
1482
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 cricket::FakeMediaEngine* media_engine_;
1484 cricket::FakeDataEngine* data_engine_;
kwibergd1fe2812016-04-27 06:47:29 -07001485 std::unique_ptr<cricket::ChannelManager> channel_manager_;
stefanc1aeaf02015-10-15 07:26:07 -07001486 cricket::FakeCall fake_call_;
kwibergd1fe2812016-04-27 06:47:29 -07001487 std::unique_ptr<webrtc::MediaControllerInterface> media_controller_;
1488 std::unique_ptr<cricket::TransportDescriptionFactory> tdesc_factory_;
1489 std::unique_ptr<cricket::MediaSessionDescriptionFactory> desc_factory_;
1490 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
1491 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1492 std::unique_ptr<rtc::FirewallSocketServer> fss_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001493 rtc::SocketServerScope ss_scope_;
1494 rtc::SocketAddress stun_socket_addr_;
kwibergd1fe2812016-04-27 06:47:29 -07001495 std::unique_ptr<cricket::TestStunServer> stun_server_;
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001496 cricket::TestTurnServer turn_server_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001497 rtc::FakeNetworkManager network_manager_;
kwibergd1fe2812016-04-27 06:47:29 -07001498 std::unique_ptr<cricket::BasicPortAllocator> allocator_;
wu@webrtc.org97077a32013-10-25 21:18:33 +00001499 PeerConnectionFactoryInterface::Options options_;
htaa2a49d92016-03-04 02:51:39 -08001500 PeerConnectionInterface::RTCConfiguration configuration_;
kwibergd1fe2812016-04-27 06:47:29 -07001501 std::unique_ptr<WebRtcSessionForTest> session_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 MockIceObserver observer_;
1503 cricket::FakeVideoMediaChannel* video_channel_;
1504 cricket::FakeVoiceMediaChannel* voice_channel_;
jbauchac8869e2015-07-03 01:36:14 -07001505 rtc::scoped_refptr<FakeMetricsObserver> metrics_observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001506 // The following flags affect options created for CreateOffer/CreateAnswer.
1507 bool send_stream_1_ = false;
1508 bool send_stream_2_ = false;
1509 bool send_audio_ = false;
1510 bool send_video_ = false;
1511 rtc::scoped_refptr<DataChannel> data_channel_;
1512 // Last values received from data channel creation signal.
1513 std::string last_data_channel_label_;
1514 InternalDataChannelInit last_data_channel_config_;
deadbeef8947a012016-01-21 10:26:38 -08001515 bool session_destroyed_ = false;
jbauchcb560652016-08-04 05:20:32 -07001516 bool with_gcm_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517};
1518
Henrik Boström87713d02015-08-25 09:53:21 +02001519TEST_P(WebRtcSessionTest, TestInitializeWithDtls) {
1520 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001521 // SDES is disabled when DTLS is on.
1522 EXPECT_EQ(cricket::SEC_DISABLED, session_->SdesPolicy());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523}
1524
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001525TEST_F(WebRtcSessionTest, TestInitializeWithoutDtls) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001526 Init();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001527 // SDES is required if DTLS is off.
1528 EXPECT_EQ(cricket::SEC_REQUIRED, session_->SdesPolicy());
wu@webrtc.org91053e72013-08-10 07:18:04 +00001529}
1530
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531TEST_F(WebRtcSessionTest, TestSessionCandidates) {
1532 TestSessionCandidatesWithBundleRtcpMux(false, false);
1533}
1534
1535// Below test cases (TestSessionCandidatesWith*) verify the candidates gathered
1536// with rtcp-mux and/or bundle.
1537TEST_F(WebRtcSessionTest, TestSessionCandidatesWithRtcpMux) {
1538 TestSessionCandidatesWithBundleRtcpMux(false, true);
1539}
1540
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541TEST_F(WebRtcSessionTest, TestSessionCandidatesWithBundleRtcpMux) {
1542 TestSessionCandidatesWithBundleRtcpMux(true, true);
1543}
1544
1545TEST_F(WebRtcSessionTest, TestMultihomeCandidates) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001546 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
1547 AddInterface(rtc::SocketAddress(kClientAddrHost2, kClientAddrPort));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001548 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001549 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 InitiateCall();
1551 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1552 EXPECT_EQ(8u, observer_.mline_0_candidates_.size());
1553 EXPECT_EQ(8u, observer_.mline_1_candidates_.size());
1554}
1555
deadbeeff5f03e82016-06-06 11:16:06 -07001556TEST_F(WebRtcSessionTest, TestStunError) {
1557 rtc::ScopedFakeClock clock;
1558
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001559 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
1560 AddInterface(rtc::SocketAddress(kClientAddrHost2, kClientAddrPort));
wu@webrtc.org364f2042013-11-20 21:49:41 +00001561 fss_->AddRule(false,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001562 rtc::FP_UDP,
1563 rtc::FD_ANY,
1564 rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001565 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001566 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 InitiateCall();
wu@webrtc.org364f2042013-11-20 21:49:41 +00001568 // Since kClientAddrHost1 is blocked, not expecting stun candidates for it.
deadbeeff5f03e82016-06-06 11:16:06 -07001569 EXPECT_TRUE_SIMULATED_WAIT(observer_.oncandidatesready_, kStunTimeout, clock);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 EXPECT_EQ(6u, observer_.mline_0_candidates_.size());
1571 EXPECT_EQ(6u, observer_.mline_1_candidates_.size());
deadbeeff5f03e82016-06-06 11:16:06 -07001572 // Destroy session before scoped fake clock goes out of scope to avoid TSan
1573 // warning.
1574 session_->Close();
1575 session_.reset(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576}
1577
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001578TEST_F(WebRtcSessionTest, SetSdpFailedOnInvalidSdp) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001579 Init();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001580 SessionDescriptionInterface* offer = NULL;
1581 // Since |offer| is NULL, there's no way to tell if it's an offer or answer.
1582 std::string unknown_action;
1583 SetLocalDescriptionExpectError(unknown_action, kInvalidSdp, offer);
1584 SetRemoteDescriptionExpectError(unknown_action, kInvalidSdp, offer);
1585}
1586
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587// Test creating offers and receive answers and make sure the
1588// media engine creates the expected send and receive streams.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001589TEST_F(WebRtcSessionTest, TestCreateSdesOfferReceiveSdesAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001590 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001591 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001592 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 const std::string session_id_orig = offer->session_id();
1594 const std::string session_version_orig = offer->session_version();
1595 SetLocalDescriptionWithoutError(offer);
1596
deadbeefab9b2d12015-10-14 11:33:11 -07001597 SendAudioVideoStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 SessionDescriptionInterface* answer =
1599 CreateRemoteAnswer(session_->local_description());
1600 SetRemoteDescriptionWithoutError(answer);
1601
1602 video_channel_ = media_engine_->GetVideoChannel(0);
1603 voice_channel_ = media_engine_->GetVoiceChannel(0);
1604
1605 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1606 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1607
1608 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1609 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1610
1611 ASSERT_EQ(1u, video_channel_->send_streams().size());
1612 EXPECT_TRUE(kVideoTrack1 == video_channel_->send_streams()[0].id);
1613 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1614 EXPECT_TRUE(kAudioTrack1 == voice_channel_->send_streams()[0].id);
1615
1616 // Create new offer without send streams.
deadbeefab9b2d12015-10-14 11:33:11 -07001617 SendNothing();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001618 offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619
1620 // Verify the session id is the same and the session version is
1621 // increased.
1622 EXPECT_EQ(session_id_orig, offer->session_id());
Peter Boström0c4e06b2015-10-07 12:23:21 +02001623 EXPECT_LT(rtc::FromString<uint64_t>(session_version_orig),
1624 rtc::FromString<uint64_t>(offer->session_version()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625
1626 SetLocalDescriptionWithoutError(offer);
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001627 EXPECT_EQ(0u, video_channel_->send_streams().size());
1628 EXPECT_EQ(0u, voice_channel_->send_streams().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629
deadbeefab9b2d12015-10-14 11:33:11 -07001630 SendAudioVideoStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 answer = CreateRemoteAnswer(session_->local_description());
1632 SetRemoteDescriptionWithoutError(answer);
1633
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 // Make sure the receive streams have not changed.
1635 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1636 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1637 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1638 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1639}
1640
1641// Test receiving offers and creating answers and make sure the
1642// media engine creates the expected send and receive streams.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001643TEST_F(WebRtcSessionTest, TestReceiveSdesOfferCreateSdesAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001644 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07001645 SendAudioVideoStream2();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001646 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001647 VerifyCryptoParams(offer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 SetRemoteDescriptionWithoutError(offer);
1649
deadbeefab9b2d12015-10-14 11:33:11 -07001650 SendAudioVideoStream1();
htaa2a49d92016-03-04 02:51:39 -08001651 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001652 VerifyCryptoParams(answer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 SetLocalDescriptionWithoutError(answer);
1654
1655 const std::string session_id_orig = answer->session_id();
1656 const std::string session_version_orig = answer->session_version();
1657
1658 video_channel_ = media_engine_->GetVideoChannel(0);
1659 voice_channel_ = media_engine_->GetVoiceChannel(0);
1660
htaa2a49d92016-03-04 02:51:39 -08001661 ASSERT_TRUE(video_channel_);
1662 ASSERT_TRUE(voice_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1664 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1665
1666 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1667 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1668
1669 ASSERT_EQ(1u, video_channel_->send_streams().size());
1670 EXPECT_TRUE(kVideoTrack1 == video_channel_->send_streams()[0].id);
1671 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1672 EXPECT_TRUE(kAudioTrack1 == voice_channel_->send_streams()[0].id);
1673
deadbeefab9b2d12015-10-14 11:33:11 -07001674 SendAudioVideoStream1And2();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001675 offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 SetRemoteDescriptionWithoutError(offer);
1677
1678 // Answer by turning off all send streams.
deadbeefab9b2d12015-10-14 11:33:11 -07001679 SendNothing();
htaa2a49d92016-03-04 02:51:39 -08001680 answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681
1682 // Verify the session id is the same and the session version is
1683 // increased.
1684 EXPECT_EQ(session_id_orig, answer->session_id());
Peter Boström0c4e06b2015-10-07 12:23:21 +02001685 EXPECT_LT(rtc::FromString<uint64_t>(session_version_orig),
1686 rtc::FromString<uint64_t>(answer->session_version()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687 SetLocalDescriptionWithoutError(answer);
1688
1689 ASSERT_EQ(2u, video_channel_->recv_streams().size());
1690 EXPECT_TRUE(kVideoTrack1 == video_channel_->recv_streams()[0].id);
1691 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[1].id);
1692 ASSERT_EQ(2u, voice_channel_->recv_streams().size());
1693 EXPECT_TRUE(kAudioTrack1 == voice_channel_->recv_streams()[0].id);
1694 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[1].id);
1695
1696 // Make sure we have no send streams.
1697 EXPECT_EQ(0u, video_channel_->send_streams().size());
1698 EXPECT_EQ(0u, voice_channel_->send_streams().size());
1699}
1700
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001701TEST_F(WebRtcSessionTest, SetLocalSdpFailedOnCreateChannel) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001702 Init();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001703 media_engine_->set_fail_create_channel(true);
1704
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001705 SessionDescriptionInterface* offer = CreateOffer();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001706 ASSERT_TRUE(offer != NULL);
1707 // SetRemoteDescription and SetLocalDescription will take the ownership of
1708 // the offer.
1709 SetRemoteDescriptionOfferExpectError(kCreateChannelFailed, offer);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001710 offer = CreateOffer();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001711 ASSERT_TRUE(offer != NULL);
1712 SetLocalDescriptionOfferExpectError(kCreateChannelFailed, offer);
1713}
1714
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001715//
1716// Tests for creating/setting SDP under different SDES/DTLS polices:
1717//
1718// --DTLS off and SDES on
1719// TestCreateSdesOfferReceiveSdesAnswer/TestReceiveSdesOfferCreateSdesAnswer:
1720// set local/remote offer/answer with crypto --> success
1721// TestSetNonSdesOfferWhenSdesOn: set local/remote offer without crypto --->
1722// failure
1723// TestSetLocalNonSdesAnswerWhenSdesOn: set local answer without crypto -->
1724// failure
1725// TestSetRemoteNonSdesAnswerWhenSdesOn: set remote answer without crypto -->
1726// failure
1727//
1728// --DTLS on and SDES off
1729// TestCreateDtlsOfferReceiveDtlsAnswer/TestReceiveDtlsOfferCreateDtlsAnswer:
1730// set local/remote offer/answer with DTLS fingerprint --> success
1731// TestReceiveNonDtlsOfferWhenDtlsOn: set local/remote offer without DTLS
1732// fingerprint --> failure
1733// TestSetLocalNonDtlsAnswerWhenDtlsOn: set local answer without fingerprint
1734// --> failure
1735// TestSetRemoteNonDtlsAnswerWhenDtlsOn: set remote answer without fingerprint
1736// --> failure
1737//
1738// --Encryption disabled: DTLS off and SDES off
1739// TestCreateOfferReceiveAnswerWithoutEncryption: set local offer and remote
1740// answer without SDES or DTLS --> success
1741// TestCreateAnswerReceiveOfferWithoutEncryption: set remote offer and local
1742// answer without SDES or DTLS --> success
1743//
1744
1745// Test that we return a failure when applying a remote/local offer that doesn't
1746// have cryptos enabled when DTLS is off.
1747TEST_F(WebRtcSessionTest, TestSetNonSdesOfferWhenSdesOn) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001748 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001750 options.recv_video = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 JsepSessionDescription* offer = CreateRemoteOffer(
1752 options, cricket::SEC_DISABLED);
1753 ASSERT_TRUE(offer != NULL);
1754 VerifyNoCryptoParams(offer->description(), false);
1755 // SetRemoteDescription and SetLocalDescription will take the ownership of
1756 // the offer.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001757 SetRemoteDescriptionOfferExpectError(kSdpWithoutSdesCrypto, offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 offer = CreateRemoteOffer(options, cricket::SEC_DISABLED);
1759 ASSERT_TRUE(offer != NULL);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001760 SetLocalDescriptionOfferExpectError(kSdpWithoutSdesCrypto, offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761}
1762
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001763// Test that we return a failure when applying a local answer that doesn't have
1764// cryptos enabled when DTLS is off.
1765TEST_F(WebRtcSessionTest, TestSetLocalNonSdesAnswerWhenSdesOn) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001766 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 SessionDescriptionInterface* offer = NULL;
1768 SessionDescriptionInterface* answer = NULL;
1769 CreateCryptoOfferAndNonCryptoAnswer(&offer, &answer);
1770 // SetRemoteDescription and SetLocalDescription will take the ownership of
1771 // the offer.
1772 SetRemoteDescriptionWithoutError(offer);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001773 SetLocalDescriptionAnswerExpectError(kSdpWithoutSdesCrypto, answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774}
1775
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001776// Test we will return fail when apply an remote answer that doesn't have
1777// crypto enabled when DTLS is off.
1778TEST_F(WebRtcSessionTest, TestSetRemoteNonSdesAnswerWhenSdesOn) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001779 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 SessionDescriptionInterface* offer = NULL;
1781 SessionDescriptionInterface* answer = NULL;
1782 CreateCryptoOfferAndNonCryptoAnswer(&offer, &answer);
1783 // SetRemoteDescription and SetLocalDescription will take the ownership of
1784 // the offer.
1785 SetLocalDescriptionWithoutError(offer);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001786 SetRemoteDescriptionAnswerExpectError(kSdpWithoutSdesCrypto, answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787}
1788
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001789// Test that we accept an offer with a DTLS fingerprint when DTLS is on
1790// and that we return an answer with a DTLS fingerprint.
Henrik Boström87713d02015-08-25 09:53:21 +02001791TEST_P(WebRtcSessionTest, TestReceiveDtlsOfferCreateDtlsAnswer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001792 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
deadbeefab9b2d12015-10-14 11:33:11 -07001793 SendAudioVideoStream1();
Henrik Boström87713d02015-08-25 09:53:21 +02001794 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 SetFactoryDtlsSrtp();
1796 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001797 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001798 JsepSessionDescription* offer =
1799 CreateRemoteOffer(options, cricket::SEC_DISABLED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800 ASSERT_TRUE(offer != NULL);
1801 VerifyFingerprintStatus(offer->description(), true);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001802 VerifyNoCryptoParams(offer->description(), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803
1804 // SetRemoteDescription will take the ownership of the offer.
1805 SetRemoteDescriptionWithoutError(offer);
1806
1807 // Verify that we get a crypto fingerprint in the answer.
htaa2a49d92016-03-04 02:51:39 -08001808 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 ASSERT_TRUE(answer != NULL);
1810 VerifyFingerprintStatus(answer->description(), true);
1811 // Check that we don't have an a=crypto line in the answer.
1812 VerifyNoCryptoParams(answer->description(), true);
1813
1814 // Now set the local description, which should work, even without a=crypto.
1815 SetLocalDescriptionWithoutError(answer);
1816}
1817
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001818// Test that we set a local offer with a DTLS fingerprint when DTLS is on
1819// and then we accept a remote answer with a DTLS fingerprint successfully.
Henrik Boström87713d02015-08-25 09:53:21 +02001820TEST_P(WebRtcSessionTest, TestCreateDtlsOfferReceiveDtlsAnswer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001821 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
deadbeefab9b2d12015-10-14 11:33:11 -07001822 SendAudioVideoStream1();
Henrik Boström87713d02015-08-25 09:53:21 +02001823 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001824 SetFactoryDtlsSrtp();
1825
1826 // Verify that we get a crypto fingerprint in the answer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001827 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001828 ASSERT_TRUE(offer != NULL);
1829 VerifyFingerprintStatus(offer->description(), true);
1830 // Check that we don't have an a=crypto line in the offer.
1831 VerifyNoCryptoParams(offer->description(), true);
1832
1833 // Now set the local description, which should work, even without a=crypto.
1834 SetLocalDescriptionWithoutError(offer);
1835
1836 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001837 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001838 JsepSessionDescription* answer =
1839 CreateRemoteAnswer(offer, options, cricket::SEC_DISABLED);
1840 ASSERT_TRUE(answer != NULL);
1841 VerifyFingerprintStatus(answer->description(), true);
1842 VerifyNoCryptoParams(answer->description(), true);
1843
1844 // SetRemoteDescription will take the ownership of the answer.
1845 SetRemoteDescriptionWithoutError(answer);
1846}
1847
1848// Test that if we support DTLS and the other side didn't offer a fingerprint,
1849// we will fail to set the remote description.
Henrik Boström87713d02015-08-25 09:53:21 +02001850TEST_P(WebRtcSessionTest, TestReceiveNonDtlsOfferWhenDtlsOn) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001851 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02001852 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001854 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001855 options.bundle_enabled = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 JsepSessionDescription* offer = CreateRemoteOffer(
1857 options, cricket::SEC_REQUIRED);
1858 ASSERT_TRUE(offer != NULL);
1859 VerifyFingerprintStatus(offer->description(), false);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001860 VerifyCryptoParams(offer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001862 // SetRemoteDescription will take the ownership of the offer.
1863 SetRemoteDescriptionOfferExpectError(
1864 kSdpWithoutDtlsFingerprint, offer);
1865
1866 offer = CreateRemoteOffer(options, cricket::SEC_REQUIRED);
1867 // SetLocalDescription will take the ownership of the offer.
1868 SetLocalDescriptionOfferExpectError(
1869 kSdpWithoutDtlsFingerprint, offer);
1870}
1871
1872// Test that we return a failure when applying a local answer that doesn't have
1873// a DTLS fingerprint when DTLS is required.
Henrik Boström87713d02015-08-25 09:53:21 +02001874TEST_P(WebRtcSessionTest, TestSetLocalNonDtlsAnswerWhenDtlsOn) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001875 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02001876 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001877 SessionDescriptionInterface* offer = NULL;
1878 SessionDescriptionInterface* answer = NULL;
1879 CreateDtlsOfferAndNonDtlsAnswer(&offer, &answer);
1880
1881 // SetRemoteDescription and SetLocalDescription will take the ownership of
1882 // the offer and answer.
1883 SetRemoteDescriptionWithoutError(offer);
1884 SetLocalDescriptionAnswerExpectError(
1885 kSdpWithoutDtlsFingerprint, answer);
1886}
1887
1888// Test that we return a failure when applying a remote answer that doesn't have
1889// a DTLS fingerprint when DTLS is required.
Henrik Boström87713d02015-08-25 09:53:21 +02001890TEST_P(WebRtcSessionTest, TestSetRemoteNonDtlsAnswerWhenDtlsOn) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001891 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02001892 InitWithDtls(GetParam());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001893 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001894 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001895 options.recv_video = true;
kwibergd1fe2812016-04-27 06:47:29 -07001896 std::unique_ptr<SessionDescriptionInterface> temp_offer(
deadbeefcbecd352015-09-23 11:50:27 -07001897 CreateRemoteOffer(options, cricket::SEC_ENABLED));
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001898 JsepSessionDescription* answer =
deadbeefcbecd352015-09-23 11:50:27 -07001899 CreateRemoteAnswer(temp_offer.get(), options, cricket::SEC_ENABLED);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001900
1901 // SetRemoteDescription and SetLocalDescription will take the ownership of
1902 // the offer and answer.
1903 SetLocalDescriptionWithoutError(offer);
1904 SetRemoteDescriptionAnswerExpectError(
1905 kSdpWithoutDtlsFingerprint, answer);
1906}
1907
1908// Test that we create a local offer without SDES or DTLS and accept a remote
1909// answer without SDES or DTLS when encryption is disabled.
Henrik Boström87713d02015-08-25 09:53:21 +02001910TEST_P(WebRtcSessionTest, TestCreateOfferReceiveAnswerWithoutEncryption) {
deadbeefab9b2d12015-10-14 11:33:11 -07001911 SendAudioVideoStream1();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001912 options_.disable_encryption = true;
Henrik Boström87713d02015-08-25 09:53:21 +02001913 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001914
1915 // Verify that we get a crypto fingerprint in the answer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001916 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001917 ASSERT_TRUE(offer != NULL);
1918 VerifyFingerprintStatus(offer->description(), false);
1919 // Check that we don't have an a=crypto line in the offer.
1920 VerifyNoCryptoParams(offer->description(), false);
1921
1922 // Now set the local description, which should work, even without a=crypto.
1923 SetLocalDescriptionWithoutError(offer);
1924
1925 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001926 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001927 JsepSessionDescription* answer =
1928 CreateRemoteAnswer(offer, options, cricket::SEC_DISABLED);
1929 ASSERT_TRUE(answer != NULL);
1930 VerifyFingerprintStatus(answer->description(), false);
1931 VerifyNoCryptoParams(answer->description(), false);
1932
1933 // SetRemoteDescription will take the ownership of the answer.
1934 SetRemoteDescriptionWithoutError(answer);
1935}
1936
1937// Test that we create a local answer without SDES or DTLS and accept a remote
1938// offer without SDES or DTLS when encryption is disabled.
Henrik Boström87713d02015-08-25 09:53:21 +02001939TEST_P(WebRtcSessionTest, TestCreateAnswerReceiveOfferWithoutEncryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001940 options_.disable_encryption = true;
Henrik Boström87713d02015-08-25 09:53:21 +02001941 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001942
1943 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001944 options.recv_video = true;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001945 JsepSessionDescription* offer =
1946 CreateRemoteOffer(options, cricket::SEC_DISABLED);
1947 ASSERT_TRUE(offer != NULL);
1948 VerifyFingerprintStatus(offer->description(), false);
1949 VerifyNoCryptoParams(offer->description(), false);
1950
1951 // SetRemoteDescription will take the ownership of the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 SetRemoteDescriptionWithoutError(offer);
1953
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001954 // Verify that we get a crypto fingerprint in the answer.
htaa2a49d92016-03-04 02:51:39 -08001955 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 ASSERT_TRUE(answer != NULL);
1957 VerifyFingerprintStatus(answer->description(), false);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001958 // Check that we don't have an a=crypto line in the answer.
1959 VerifyNoCryptoParams(answer->description(), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001961 // Now set the local description, which should work, even without a=crypto.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 SetLocalDescriptionWithoutError(answer);
1963}
1964
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001965// Test that we can create and set an answer correctly when different
1966// SSL roles have been negotiated for different transports.
1967// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4525
1968TEST_P(WebRtcSessionTest, TestCreateAnswerWithDifferentSslRoles) {
1969 SendAudioVideoStream1();
1970 InitWithDtls(GetParam());
1971 SetFactoryDtlsSrtp();
1972
1973 SessionDescriptionInterface* offer = CreateOffer();
1974 SetLocalDescriptionWithoutError(offer);
1975
1976 cricket::MediaSessionOptions options;
1977 options.recv_video = true;
1978
1979 // First, negotiate different SSL roles.
1980 SessionDescriptionInterface* answer =
1981 CreateRemoteAnswer(offer, options, cricket::SEC_DISABLED);
1982 TransportInfo* audio_transport_info =
1983 answer->description()->GetTransportInfoByName("audio");
1984 audio_transport_info->description.connection_role =
1985 cricket::CONNECTIONROLE_ACTIVE;
1986 TransportInfo* video_transport_info =
1987 answer->description()->GetTransportInfoByName("video");
1988 video_transport_info->description.connection_role =
1989 cricket::CONNECTIONROLE_PASSIVE;
1990 SetRemoteDescriptionWithoutError(answer);
1991
1992 // Now create an offer in the reverse direction, and ensure the initial
1993 // offerer responds with an answer with correct SSL roles.
1994 offer = CreateRemoteOfferWithVersion(options, cricket::SEC_DISABLED,
1995 kSessionVersion,
1996 session_->remote_description());
1997 SetRemoteDescriptionWithoutError(offer);
1998
htaa2a49d92016-03-04 02:51:39 -08001999 answer = CreateAnswer();
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002000 audio_transport_info = answer->description()->GetTransportInfoByName("audio");
2001 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
2002 audio_transport_info->description.connection_role);
2003 video_transport_info = answer->description()->GetTransportInfoByName("video");
2004 EXPECT_EQ(cricket::CONNECTIONROLE_ACTIVE,
2005 video_transport_info->description.connection_role);
2006 SetLocalDescriptionWithoutError(answer);
2007
2008 // Lastly, start BUNDLE-ing on "audio", expecting that the "passive" role of
2009 // audio is transferred over to video in the answer that completes the BUNDLE
2010 // negotiation.
2011 options.bundle_enabled = true;
2012 offer = CreateRemoteOfferWithVersion(options, cricket::SEC_DISABLED,
2013 kSessionVersion,
2014 session_->remote_description());
2015 SetRemoteDescriptionWithoutError(offer);
htaa2a49d92016-03-04 02:51:39 -08002016 answer = CreateAnswer();
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002017 audio_transport_info = answer->description()->GetTransportInfoByName("audio");
2018 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
2019 audio_transport_info->description.connection_role);
2020 video_transport_info = answer->description()->GetTransportInfoByName("video");
2021 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
2022 video_transport_info->description.connection_role);
2023 SetLocalDescriptionWithoutError(answer);
2024}
2025
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026TEST_F(WebRtcSessionTest, TestSetLocalOfferTwice) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002027 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002028 SendNothing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 // SetLocalDescription take ownership of offer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002030 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031 SetLocalDescriptionWithoutError(offer);
2032
2033 // SetLocalDescription take ownership of offer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002034 SessionDescriptionInterface* offer2 = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 SetLocalDescriptionWithoutError(offer2);
2036}
2037
2038TEST_F(WebRtcSessionTest, TestSetRemoteOfferTwice) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002039 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002040 SendNothing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041 // SetLocalDescription take ownership of offer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002042 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043 SetRemoteDescriptionWithoutError(offer);
2044
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002045 SessionDescriptionInterface* offer2 = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046 SetRemoteDescriptionWithoutError(offer2);
2047}
2048
2049TEST_F(WebRtcSessionTest, TestSetLocalAndRemoteOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002050 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002051 SendNothing();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002052 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053 SetLocalDescriptionWithoutError(offer);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002054 offer = CreateOffer();
deadbeefd59daf82015-10-14 15:02:44 -07002055 SetRemoteDescriptionOfferExpectError("Called in wrong state: STATE_SENTOFFER",
2056 offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057}
2058
2059TEST_F(WebRtcSessionTest, TestSetRemoteAndLocalOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002060 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002061 SendNothing();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002062 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 SetRemoteDescriptionWithoutError(offer);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002064 offer = CreateOffer();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002065 SetLocalDescriptionOfferExpectError(
deadbeefd59daf82015-10-14 15:02:44 -07002066 "Called in wrong state: STATE_RECEIVEDOFFER", offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067}
2068
2069TEST_F(WebRtcSessionTest, TestSetLocalPrAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002070 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002071 SendNothing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072 SessionDescriptionInterface* offer = CreateRemoteOffer();
deadbeefd59daf82015-10-14 15:02:44 -07002073 SetRemoteDescriptionExpectState(offer, WebRtcSession::STATE_RECEIVEDOFFER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074
htaa2a49d92016-03-04 02:51:39 -08002075 JsepSessionDescription* pranswer =
2076 static_cast<JsepSessionDescription*>(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077 pranswer->set_type(SessionDescriptionInterface::kPrAnswer);
deadbeefd59daf82015-10-14 15:02:44 -07002078 SetLocalDescriptionExpectState(pranswer, WebRtcSession::STATE_SENTPRANSWER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079
deadbeefab9b2d12015-10-14 11:33:11 -07002080 SendAudioVideoStream1();
htaa2a49d92016-03-04 02:51:39 -08002081 JsepSessionDescription* pranswer2 =
2082 static_cast<JsepSessionDescription*>(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083 pranswer2->set_type(SessionDescriptionInterface::kPrAnswer);
2084
deadbeefd59daf82015-10-14 15:02:44 -07002085 SetLocalDescriptionExpectState(pranswer2, WebRtcSession::STATE_SENTPRANSWER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086
deadbeefab9b2d12015-10-14 11:33:11 -07002087 SendAudioVideoStream2();
htaa2a49d92016-03-04 02:51:39 -08002088 SessionDescriptionInterface* answer = CreateAnswer();
deadbeefd59daf82015-10-14 15:02:44 -07002089 SetLocalDescriptionExpectState(answer, WebRtcSession::STATE_INPROGRESS);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090}
2091
2092TEST_F(WebRtcSessionTest, TestSetRemotePrAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002093 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002094 SendNothing();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002095 SessionDescriptionInterface* offer = CreateOffer();
deadbeefd59daf82015-10-14 15:02:44 -07002096 SetLocalDescriptionExpectState(offer, WebRtcSession::STATE_SENTOFFER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097
2098 JsepSessionDescription* pranswer =
2099 CreateRemoteAnswer(session_->local_description());
2100 pranswer->set_type(SessionDescriptionInterface::kPrAnswer);
2101
2102 SetRemoteDescriptionExpectState(pranswer,
deadbeefd59daf82015-10-14 15:02:44 -07002103 WebRtcSession::STATE_RECEIVEDPRANSWER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104
deadbeefab9b2d12015-10-14 11:33:11 -07002105 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 JsepSessionDescription* pranswer2 =
2107 CreateRemoteAnswer(session_->local_description());
2108 pranswer2->set_type(SessionDescriptionInterface::kPrAnswer);
2109
2110 SetRemoteDescriptionExpectState(pranswer2,
deadbeefd59daf82015-10-14 15:02:44 -07002111 WebRtcSession::STATE_RECEIVEDPRANSWER);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112
deadbeefab9b2d12015-10-14 11:33:11 -07002113 SendAudioVideoStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 SessionDescriptionInterface* answer =
2115 CreateRemoteAnswer(session_->local_description());
deadbeefd59daf82015-10-14 15:02:44 -07002116 SetRemoteDescriptionExpectState(answer, WebRtcSession::STATE_INPROGRESS);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117}
2118
2119TEST_F(WebRtcSessionTest, TestSetLocalAnswerWithoutOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002120 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002121 SendNothing();
kwibergd1fe2812016-04-27 06:47:29 -07002122 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002123
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124 SessionDescriptionInterface* answer =
2125 CreateRemoteAnswer(offer.get());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002126 SetLocalDescriptionAnswerExpectError("Called in wrong state: STATE_INIT",
2127 answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128}
2129
2130TEST_F(WebRtcSessionTest, TestSetRemoteAnswerWithoutOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002131 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002132 SendNothing();
kwibergd1fe2812016-04-27 06:47:29 -07002133 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002134
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 SessionDescriptionInterface* answer =
2136 CreateRemoteAnswer(offer.get());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002137 SetRemoteDescriptionAnswerExpectError(
2138 "Called in wrong state: STATE_INIT", answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139}
2140
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002141// Tests that the remote candidates are added and removed successfully.
2142TEST_F(WebRtcSessionTest, TestAddAndRemoveRemoteCandidates) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002143 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002144 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002146 cricket::Candidate candidate(1, "udp", rtc::SocketAddress("1.1.1.1", 5000), 0,
2147 "", "", "host", 0, "");
2148 candidate.set_transport_name("audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 JsepIceCandidate ice_candidate1(kMediaContentName0, 0, candidate);
2150
deadbeefd59daf82015-10-14 15:02:44 -07002151 // Fail since we have not set a remote description.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152 EXPECT_FALSE(session_->ProcessIceMessage(&ice_candidate1));
2153
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002154 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155 SetLocalDescriptionWithoutError(offer);
deadbeefd59daf82015-10-14 15:02:44 -07002156
2157 // Fail since we have not set a remote description.
2158 EXPECT_FALSE(session_->ProcessIceMessage(&ice_candidate1));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159
2160 SessionDescriptionInterface* answer = CreateRemoteAnswer(
2161 session_->local_description());
2162 SetRemoteDescriptionWithoutError(answer);
2163
deadbeefd59daf82015-10-14 15:02:44 -07002164 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate1));
2165 candidate.set_component(2);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002166 candidate.set_address(rtc::SocketAddress("2.2.2.2", 6000));
deadbeefd59daf82015-10-14 15:02:44 -07002167 JsepIceCandidate ice_candidate2(kMediaContentName0, 0, candidate);
2168 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate2));
2169
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170 // Verifying the candidates are copied properly from internal vector.
2171 const SessionDescriptionInterface* remote_desc =
2172 session_->remote_description();
2173 ASSERT_TRUE(remote_desc != NULL);
2174 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
2175 const IceCandidateCollection* candidates =
2176 remote_desc->candidates(kMediaContentIndex0);
2177 ASSERT_EQ(2u, candidates->count());
2178 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
2179 EXPECT_EQ(kMediaContentName0, candidates->at(0)->sdp_mid());
2180 EXPECT_EQ(1, candidates->at(0)->candidate().component());
2181 EXPECT_EQ(2, candidates->at(1)->candidate().component());
2182
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002183 // |ice_candidate3| is identical to |ice_candidate2|. It can be added
2184 // successfully, but the total count of candidates will not increase.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185 candidate.set_component(2);
2186 JsepIceCandidate ice_candidate3(kMediaContentName0, 0, candidate);
2187 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate3));
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002188 ASSERT_EQ(2u, candidates->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189
2190 JsepIceCandidate bad_ice_candidate("bad content name", 99, candidate);
2191 EXPECT_FALSE(session_->ProcessIceMessage(&bad_ice_candidate));
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002192
2193 // Remove candidate1 and candidate2
2194 std::vector<cricket::Candidate> remote_candidates;
2195 remote_candidates.push_back(ice_candidate1.candidate());
2196 remote_candidates.push_back(ice_candidate2.candidate());
2197 EXPECT_TRUE(session_->RemoveRemoteIceCandidates(remote_candidates));
2198 EXPECT_EQ(0u, candidates->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199}
2200
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002201// Tests that a remote candidate is added to the remote session description and
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202// that it is retained if the remote session description is changed.
2203TEST_F(WebRtcSessionTest, TestRemoteCandidatesAddedToSessionDescription) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002204 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 cricket::Candidate candidate1;
2206 candidate1.set_component(1);
2207 JsepIceCandidate ice_candidate1(kMediaContentName0, kMediaContentIndex0,
2208 candidate1);
deadbeefab9b2d12015-10-14 11:33:11 -07002209 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210 CreateAndSetRemoteOfferAndLocalAnswer();
2211
2212 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate1));
2213 const SessionDescriptionInterface* remote_desc =
2214 session_->remote_description();
2215 ASSERT_TRUE(remote_desc != NULL);
2216 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
2217 const IceCandidateCollection* candidates =
2218 remote_desc->candidates(kMediaContentIndex0);
2219 ASSERT_EQ(1u, candidates->count());
2220 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
2221
2222 // Update the RemoteSessionDescription with a new session description and
2223 // a candidate and check that the new remote session description contains both
2224 // candidates.
2225 SessionDescriptionInterface* offer = CreateRemoteOffer();
2226 cricket::Candidate candidate2;
2227 JsepIceCandidate ice_candidate2(kMediaContentName0, kMediaContentIndex0,
2228 candidate2);
2229 EXPECT_TRUE(offer->AddCandidate(&ice_candidate2));
2230 SetRemoteDescriptionWithoutError(offer);
2231
2232 remote_desc = session_->remote_description();
2233 ASSERT_TRUE(remote_desc != NULL);
2234 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
2235 candidates = remote_desc->candidates(kMediaContentIndex0);
2236 ASSERT_EQ(2u, candidates->count());
2237 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
2238 // Username and password have be updated with the TransportInfo of the
2239 // SessionDescription, won't be equal to the original one.
2240 candidate2.set_username(candidates->at(0)->candidate().username());
2241 candidate2.set_password(candidates->at(0)->candidate().password());
2242 EXPECT_TRUE(candidate2.IsEquivalent(candidates->at(0)->candidate()));
2243 EXPECT_EQ(kMediaContentIndex0, candidates->at(1)->sdp_mline_index());
2244 // No need to verify the username and password.
2245 candidate1.set_username(candidates->at(1)->candidate().username());
2246 candidate1.set_password(candidates->at(1)->candidate().password());
2247 EXPECT_TRUE(candidate1.IsEquivalent(candidates->at(1)->candidate()));
2248
2249 // Test that the candidate is ignored if we can add the same candidate again.
2250 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate2));
2251}
2252
2253// Test that local candidates are added to the local session description and
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002254// that they are retained if the local session description is changed. And if
2255// continual gathering is enabled, they are removed from the local session
2256// description when the network is down.
2257TEST_F(WebRtcSessionTest,
2258 TestLocalCandidatesAddedAndRemovedIfGatherContinually) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002259 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002260 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002261 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002262 CreateAndSetRemoteOfferAndLocalAnswer();
2263
2264 const SessionDescriptionInterface* local_desc = session_->local_description();
2265 const IceCandidateCollection* candidates =
2266 local_desc->candidates(kMediaContentIndex0);
2267 ASSERT_TRUE(candidates != NULL);
2268 EXPECT_EQ(0u, candidates->count());
2269
2270 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
2271
2272 local_desc = session_->local_description();
2273 candidates = local_desc->candidates(kMediaContentIndex0);
2274 ASSERT_TRUE(candidates != NULL);
2275 EXPECT_LT(0u, candidates->count());
2276 candidates = local_desc->candidates(1);
2277 ASSERT_TRUE(candidates != NULL);
deadbeefcbecd352015-09-23 11:50:27 -07002278 EXPECT_EQ(0u, candidates->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279
2280 // Update the session descriptions.
deadbeefab9b2d12015-10-14 11:33:11 -07002281 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 CreateAndSetRemoteOfferAndLocalAnswer();
2283
2284 local_desc = session_->local_description();
2285 candidates = local_desc->candidates(kMediaContentIndex0);
2286 ASSERT_TRUE(candidates != NULL);
2287 EXPECT_LT(0u, candidates->count());
2288 candidates = local_desc->candidates(1);
2289 ASSERT_TRUE(candidates != NULL);
deadbeefcbecd352015-09-23 11:50:27 -07002290 EXPECT_EQ(0u, candidates->count());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002291
2292 candidates = local_desc->candidates(kMediaContentIndex0);
2293 size_t num_local_candidates = candidates->count();
2294 // Enable Continual Gathering
Honghai Zhang5622c5e2016-07-01 13:59:29 -07002295 cricket::IceConfig config;
2296 config.continual_gathering_policy = cricket::GATHER_CONTINUALLY;
2297 session_->SetIceConfig(config);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002298 // Bring down the network interface to trigger candidate removals.
2299 RemoveInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
2300 // Verify that all local candidates are removed.
2301 EXPECT_EQ(0, observer_.num_candidates_removed_);
2302 EXPECT_EQ_WAIT(num_local_candidates, observer_.num_candidates_removed_,
2303 kIceCandidatesTimeout);
2304 EXPECT_EQ_WAIT(0u, candidates->count(), kIceCandidatesTimeout);
2305}
2306
2307// Tests that if continual gathering is disabled, local candidates won't be
2308// removed when the interface is turned down.
2309TEST_F(WebRtcSessionTest, TestLocalCandidatesNotRemovedIfNotGatherContinually) {
2310 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
2311 Init();
2312 SendAudioVideoStream1();
2313 CreateAndSetRemoteOfferAndLocalAnswer();
2314
2315 const SessionDescriptionInterface* local_desc = session_->local_description();
2316 const IceCandidateCollection* candidates =
2317 local_desc->candidates(kMediaContentIndex0);
2318 ASSERT_TRUE(candidates != NULL);
2319 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
2320
2321 size_t num_local_candidates = candidates->count();
2322 EXPECT_LT(0u, num_local_candidates);
2323 // By default, Continual Gathering is disabled.
2324 // Bring down the network interface.
2325 RemoveInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
2326 // Verify that the local candidates are not removed.
2327 rtc::Thread::Current()->ProcessMessages(1000);
2328 EXPECT_EQ(0, observer_.num_candidates_removed_);
2329 EXPECT_EQ(num_local_candidates, candidates->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330}
2331
2332// Test that we can set a remote session description with remote candidates.
2333TEST_F(WebRtcSessionTest, TestSetRemoteSessionDescriptionWithCandidates) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002334 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002335
2336 cricket::Candidate candidate1;
2337 candidate1.set_component(1);
2338 JsepIceCandidate ice_candidate(kMediaContentName0, kMediaContentIndex0,
2339 candidate1);
deadbeefab9b2d12015-10-14 11:33:11 -07002340 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002341 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342
2343 EXPECT_TRUE(offer->AddCandidate(&ice_candidate));
2344 SetRemoteDescriptionWithoutError(offer);
2345
2346 const SessionDescriptionInterface* remote_desc =
2347 session_->remote_description();
2348 ASSERT_TRUE(remote_desc != NULL);
2349 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
2350 const IceCandidateCollection* candidates =
2351 remote_desc->candidates(kMediaContentIndex0);
2352 ASSERT_EQ(1u, candidates->count());
2353 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
2354
htaa2a49d92016-03-04 02:51:39 -08002355 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 SetLocalDescriptionWithoutError(answer);
2357}
2358
2359// Test that offers and answers contains ice candidates when Ice candidates have
2360// been gathered.
2361TEST_F(WebRtcSessionTest, TestSetLocalAndRemoteDescriptionWithCandidates) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002362 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002363 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002364 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002365 // Ice is started but candidates are not provided until SetLocalDescription
2366 // is called.
2367 EXPECT_EQ(0u, observer_.mline_0_candidates_.size());
2368 EXPECT_EQ(0u, observer_.mline_1_candidates_.size());
2369 CreateAndSetRemoteOfferAndLocalAnswer();
2370 // Wait until at least one local candidate has been collected.
2371 EXPECT_TRUE_WAIT(0u < observer_.mline_0_candidates_.size(),
2372 kIceCandidatesTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002373
kwibergd1fe2812016-04-27 06:47:29 -07002374 std::unique_ptr<SessionDescriptionInterface> local_offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002375
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002376 ASSERT_TRUE(local_offer->candidates(kMediaContentIndex0) != NULL);
2377 EXPECT_LT(0u, local_offer->candidates(kMediaContentIndex0)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002378
2379 SessionDescriptionInterface* remote_offer(CreateRemoteOffer());
2380 SetRemoteDescriptionWithoutError(remote_offer);
htaa2a49d92016-03-04 02:51:39 -08002381 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002382 ASSERT_TRUE(answer->candidates(kMediaContentIndex0) != NULL);
2383 EXPECT_LT(0u, answer->candidates(kMediaContentIndex0)->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384 SetLocalDescriptionWithoutError(answer);
2385}
2386
2387// Verifies TransportProxy and media channels are created with content names
2388// present in the SessionDescription.
2389TEST_F(WebRtcSessionTest, TestChannelCreationsWithContentNames) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002390 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002391 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002392 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393
2394 // CreateOffer creates session description with the content names "audio" and
deadbeefd59daf82015-10-14 15:02:44 -07002395 // "video". Goal is to modify these content names and verify transport
2396 // channels
2397 // in the WebRtcSession, as channels are created with the content names
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002398 // present in SDP.
2399 std::string sdp;
2400 EXPECT_TRUE(offer->ToString(&sdp));
2401 const std::string kAudioMid = "a=mid:audio";
2402 const std::string kAudioMidReplaceStr = "a=mid:audio_content_name";
2403 const std::string kVideoMid = "a=mid:video";
2404 const std::string kVideoMidReplaceStr = "a=mid:video_content_name";
2405
2406 // Replacing |audio| with |audio_content_name|.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002407 rtc::replace_substrs(kAudioMid.c_str(), kAudioMid.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408 kAudioMidReplaceStr.c_str(),
2409 kAudioMidReplaceStr.length(),
2410 &sdp);
2411 // Replacing |video| with |video_content_name|.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002412 rtc::replace_substrs(kVideoMid.c_str(), kVideoMid.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 kVideoMidReplaceStr.c_str(),
2414 kVideoMidReplaceStr.length(),
2415 &sdp);
2416
2417 SessionDescriptionInterface* modified_offer =
2418 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
2419
2420 SetRemoteDescriptionWithoutError(modified_offer);
2421
htaa2a49d92016-03-04 02:51:39 -08002422 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423 SetLocalDescriptionWithoutError(answer);
2424
deadbeefcbecd352015-09-23 11:50:27 -07002425 cricket::TransportChannel* voice_transport_channel =
2426 session_->voice_rtp_transport_channel();
2427 EXPECT_TRUE(voice_transport_channel != NULL);
2428 EXPECT_EQ(voice_transport_channel->transport_name(), "audio_content_name");
2429 cricket::TransportChannel* video_transport_channel =
2430 session_->video_rtp_transport_channel();
htaa2a49d92016-03-04 02:51:39 -08002431 ASSERT_TRUE(video_transport_channel != NULL);
deadbeefcbecd352015-09-23 11:50:27 -07002432 EXPECT_EQ(video_transport_channel->transport_name(), "video_content_name");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002433 EXPECT_TRUE((video_channel_ = media_engine_->GetVideoChannel(0)) != NULL);
2434 EXPECT_TRUE((voice_channel_ = media_engine_->GetVoiceChannel(0)) != NULL);
2435}
2436
2437// Test that an offer contains the correct media content descriptions based on
2438// the send streams when no constraints have been set.
2439TEST_F(WebRtcSessionTest, CreateOfferWithoutConstraintsOrStreams) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002440 Init();
kwibergd1fe2812016-04-27 06:47:29 -07002441 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002442
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002443 ASSERT_TRUE(offer != NULL);
2444 const cricket::ContentInfo* content =
2445 cricket::GetFirstAudioContent(offer->description());
2446 EXPECT_TRUE(content != NULL);
2447 content = cricket::GetFirstVideoContent(offer->description());
2448 EXPECT_TRUE(content == NULL);
2449}
2450
2451// Test that an offer contains the correct media content descriptions based on
2452// the send streams when no constraints have been set.
2453TEST_F(WebRtcSessionTest, CreateOfferWithoutConstraints) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002454 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002455 // Test Audio only offer.
deadbeefab9b2d12015-10-14 11:33:11 -07002456 SendAudioOnlyStream2();
kwibergd1fe2812016-04-27 06:47:29 -07002457 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002458
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459 const cricket::ContentInfo* content =
2460 cricket::GetFirstAudioContent(offer->description());
2461 EXPECT_TRUE(content != NULL);
2462 content = cricket::GetFirstVideoContent(offer->description());
2463 EXPECT_TRUE(content == NULL);
2464
2465 // Test Audio / Video offer.
deadbeefab9b2d12015-10-14 11:33:11 -07002466 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002467 offer.reset(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002468 content = cricket::GetFirstAudioContent(offer->description());
2469 EXPECT_TRUE(content != NULL);
2470 content = cricket::GetFirstVideoContent(offer->description());
2471 EXPECT_TRUE(content != NULL);
2472}
2473
2474// Test that an offer contains no media content descriptions if
2475// kOfferToReceiveVideo and kOfferToReceiveAudio constraints are set to false.
2476TEST_F(WebRtcSessionTest, CreateOfferWithConstraintsWithoutStreams) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002477 Init();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002478 PeerConnectionInterface::RTCOfferAnswerOptions options;
2479 options.offer_to_receive_audio = 0;
2480 options.offer_to_receive_video = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002481
kwibergd1fe2812016-04-27 06:47:29 -07002482 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer(options));
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002483
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002484 ASSERT_TRUE(offer != NULL);
2485 const cricket::ContentInfo* content =
2486 cricket::GetFirstAudioContent(offer->description());
2487 EXPECT_TRUE(content == NULL);
2488 content = cricket::GetFirstVideoContent(offer->description());
2489 EXPECT_TRUE(content == NULL);
2490}
2491
2492// Test that an offer contains only audio media content descriptions if
2493// kOfferToReceiveAudio constraints are set to true.
2494TEST_F(WebRtcSessionTest, CreateAudioOnlyOfferWithConstraints) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002495 Init();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002496 PeerConnectionInterface::RTCOfferAnswerOptions options;
2497 options.offer_to_receive_audio =
2498 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
2499
kwibergd1fe2812016-04-27 06:47:29 -07002500 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer(options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002501
2502 const cricket::ContentInfo* content =
2503 cricket::GetFirstAudioContent(offer->description());
2504 EXPECT_TRUE(content != NULL);
2505 content = cricket::GetFirstVideoContent(offer->description());
2506 EXPECT_TRUE(content == NULL);
2507}
2508
2509// Test that an offer contains audio and video media content descriptions if
2510// kOfferToReceiveAudio and kOfferToReceiveVideo constraints are set to true.
2511TEST_F(WebRtcSessionTest, CreateOfferWithConstraints) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002512 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002513 // Test Audio / Video offer.
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002514 PeerConnectionInterface::RTCOfferAnswerOptions options;
2515 options.offer_to_receive_audio =
2516 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
2517 options.offer_to_receive_video =
2518 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
2519
kwibergd1fe2812016-04-27 06:47:29 -07002520 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer(options));
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002521
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002522 const cricket::ContentInfo* content =
2523 cricket::GetFirstAudioContent(offer->description());
jiayl@webrtc.orgc1723202014-09-08 20:44:36 +00002524 EXPECT_TRUE(content != NULL);
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00002525
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002526 content = cricket::GetFirstVideoContent(offer->description());
2527 EXPECT_TRUE(content != NULL);
2528
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00002529 // Sets constraints to false and verifies that audio/video contents are
2530 // removed.
2531 options.offer_to_receive_audio = 0;
2532 options.offer_to_receive_video = 0;
2533 offer.reset(CreateOffer(options));
2534
2535 content = cricket::GetFirstAudioContent(offer->description());
2536 EXPECT_TRUE(content == NULL);
2537 content = cricket::GetFirstVideoContent(offer->description());
2538 EXPECT_TRUE(content == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002539}
2540
2541// Test that an answer can not be created if the last remote description is not
2542// an offer.
2543TEST_F(WebRtcSessionTest, CreateAnswerWithoutAnOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002544 Init();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002545 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002546 SetLocalDescriptionWithoutError(offer);
2547 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
2548 SetRemoteDescriptionWithoutError(answer);
htaa2a49d92016-03-04 02:51:39 -08002549 EXPECT_TRUE(CreateAnswer() == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002550}
2551
2552// Test that an answer contains the correct media content descriptions when no
2553// constraints have been set.
2554TEST_F(WebRtcSessionTest, CreateAnswerWithoutConstraintsOrStreams) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002555 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002556 // Create a remote offer with audio and video content.
kwibergd1fe2812016-04-27 06:47:29 -07002557 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002558 SetRemoteDescriptionWithoutError(offer.release());
kwibergd1fe2812016-04-27 06:47:29 -07002559 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002560 const cricket::ContentInfo* content =
2561 cricket::GetFirstAudioContent(answer->description());
2562 ASSERT_TRUE(content != NULL);
2563 EXPECT_FALSE(content->rejected);
2564
2565 content = cricket::GetFirstVideoContent(answer->description());
2566 ASSERT_TRUE(content != NULL);
2567 EXPECT_FALSE(content->rejected);
2568}
2569
2570// Test that an answer contains the correct media content descriptions when no
2571// constraints have been set and the offer only contain audio.
2572TEST_F(WebRtcSessionTest, CreateAudioAnswerWithoutConstraintsOrStreams) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002573 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002574 // Create a remote offer with audio only.
2575 cricket::MediaSessionOptions options;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00002576
kwibergd1fe2812016-04-27 06:47:29 -07002577 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer(options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002578 ASSERT_TRUE(cricket::GetFirstVideoContent(offer->description()) == NULL);
2579 ASSERT_TRUE(cricket::GetFirstAudioContent(offer->description()) != NULL);
2580
2581 SetRemoteDescriptionWithoutError(offer.release());
kwibergd1fe2812016-04-27 06:47:29 -07002582 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002583 const cricket::ContentInfo* content =
2584 cricket::GetFirstAudioContent(answer->description());
2585 ASSERT_TRUE(content != NULL);
2586 EXPECT_FALSE(content->rejected);
2587
2588 EXPECT_TRUE(cricket::GetFirstVideoContent(answer->description()) == NULL);
2589}
2590
2591// Test that an answer contains the correct media content descriptions when no
2592// constraints have been set.
2593TEST_F(WebRtcSessionTest, CreateAnswerWithoutConstraints) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002594 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002595 // Create a remote offer with audio and video content.
kwibergd1fe2812016-04-27 06:47:29 -07002596 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002597 SetRemoteDescriptionWithoutError(offer.release());
2598 // Test with a stream with tracks.
deadbeefab9b2d12015-10-14 11:33:11 -07002599 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002600 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002601 const cricket::ContentInfo* content =
2602 cricket::GetFirstAudioContent(answer->description());
2603 ASSERT_TRUE(content != NULL);
2604 EXPECT_FALSE(content->rejected);
2605
2606 content = cricket::GetFirstVideoContent(answer->description());
2607 ASSERT_TRUE(content != NULL);
2608 EXPECT_FALSE(content->rejected);
2609}
2610
2611// Test that an answer contains the correct media content descriptions when
2612// constraints have been set but no stream is sent.
2613TEST_F(WebRtcSessionTest, CreateAnswerWithConstraintsWithoutStreams) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002614 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002615 // Create a remote offer with audio and video content.
kwibergd1fe2812016-04-27 06:47:29 -07002616 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002617 SetRemoteDescriptionWithoutError(offer.release());
2618
htaa2a49d92016-03-04 02:51:39 -08002619 cricket::MediaSessionOptions session_options;
2620 session_options.recv_audio = false;
2621 session_options.recv_video = false;
kwibergd1fe2812016-04-27 06:47:29 -07002622 std::unique_ptr<SessionDescriptionInterface> answer(
htaa2a49d92016-03-04 02:51:39 -08002623 CreateAnswer(session_options));
2624
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002625 const cricket::ContentInfo* content =
2626 cricket::GetFirstAudioContent(answer->description());
2627 ASSERT_TRUE(content != NULL);
2628 EXPECT_TRUE(content->rejected);
2629
2630 content = cricket::GetFirstVideoContent(answer->description());
2631 ASSERT_TRUE(content != NULL);
2632 EXPECT_TRUE(content->rejected);
2633}
2634
2635// Test that an answer contains the correct media content descriptions when
2636// constraints have been set and streams are sent.
2637TEST_F(WebRtcSessionTest, CreateAnswerWithConstraints) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002638 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002639 // Create a remote offer with audio and video content.
kwibergd1fe2812016-04-27 06:47:29 -07002640 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002641 SetRemoteDescriptionWithoutError(offer.release());
2642
htaa2a49d92016-03-04 02:51:39 -08002643 cricket::MediaSessionOptions options;
2644 options.recv_audio = false;
2645 options.recv_video = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002646
2647 // Test with a stream with tracks.
deadbeefab9b2d12015-10-14 11:33:11 -07002648 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002649 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer(options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002650
2651 // TODO(perkj): Should the direction be set to SEND_ONLY?
2652 const cricket::ContentInfo* content =
2653 cricket::GetFirstAudioContent(answer->description());
2654 ASSERT_TRUE(content != NULL);
2655 EXPECT_FALSE(content->rejected);
2656
2657 // TODO(perkj): Should the direction be set to SEND_ONLY?
2658 content = cricket::GetFirstVideoContent(answer->description());
2659 ASSERT_TRUE(content != NULL);
2660 EXPECT_FALSE(content->rejected);
2661}
2662
2663TEST_F(WebRtcSessionTest, CreateOfferWithoutCNCodecs) {
2664 AddCNCodecs();
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002665 Init();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002666 PeerConnectionInterface::RTCOfferAnswerOptions options;
2667 options.offer_to_receive_audio =
2668 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
2669 options.voice_activity_detection = false;
2670
kwibergd1fe2812016-04-27 06:47:29 -07002671 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer(options));
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002672
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002673 const cricket::ContentInfo* content =
2674 cricket::GetFirstAudioContent(offer->description());
2675 EXPECT_TRUE(content != NULL);
2676 EXPECT_TRUE(VerifyNoCNCodecs(content));
2677}
2678
2679TEST_F(WebRtcSessionTest, CreateAnswerWithoutCNCodecs) {
2680 AddCNCodecs();
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002681 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002682 // Create a remote offer with audio and video content.
kwibergd1fe2812016-04-27 06:47:29 -07002683 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002684 SetRemoteDescriptionWithoutError(offer.release());
2685
htaa2a49d92016-03-04 02:51:39 -08002686 cricket::MediaSessionOptions options;
2687 options.vad_enabled = false;
kwibergd1fe2812016-04-27 06:47:29 -07002688 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer(options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002689 const cricket::ContentInfo* content =
2690 cricket::GetFirstAudioContent(answer->description());
2691 ASSERT_TRUE(content != NULL);
2692 EXPECT_TRUE(VerifyNoCNCodecs(content));
2693}
2694
2695// This test verifies the call setup when remote answer with audio only and
2696// later updates with video.
2697TEST_F(WebRtcSessionTest, TestAVOfferWithAudioOnlyAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002698 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002699 EXPECT_TRUE(media_engine_->GetVideoChannel(0) == NULL);
2700 EXPECT_TRUE(media_engine_->GetVoiceChannel(0) == NULL);
2701
deadbeefab9b2d12015-10-14 11:33:11 -07002702 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002703 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002704
2705 cricket::MediaSessionOptions options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002706 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer, options);
2707
2708 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
2709 // and answer;
2710 SetLocalDescriptionWithoutError(offer);
2711 SetRemoteDescriptionWithoutError(answer);
2712
2713 video_channel_ = media_engine_->GetVideoChannel(0);
2714 voice_channel_ = media_engine_->GetVoiceChannel(0);
2715
2716 ASSERT_TRUE(video_channel_ == NULL);
2717
2718 ASSERT_EQ(0u, voice_channel_->recv_streams().size());
2719 ASSERT_EQ(1u, voice_channel_->send_streams().size());
2720 EXPECT_EQ(kAudioTrack1, voice_channel_->send_streams()[0].id);
2721
2722 // Let the remote end update the session descriptions, with Audio and Video.
deadbeefab9b2d12015-10-14 11:33:11 -07002723 SendAudioVideoStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002724 CreateAndSetRemoteOfferAndLocalAnswer();
2725
2726 video_channel_ = media_engine_->GetVideoChannel(0);
2727 voice_channel_ = media_engine_->GetVoiceChannel(0);
2728
2729 ASSERT_TRUE(video_channel_ != NULL);
2730 ASSERT_TRUE(voice_channel_ != NULL);
2731
2732 ASSERT_EQ(1u, video_channel_->recv_streams().size());
2733 ASSERT_EQ(1u, video_channel_->send_streams().size());
2734 EXPECT_EQ(kVideoTrack2, video_channel_->recv_streams()[0].id);
2735 EXPECT_EQ(kVideoTrack2, video_channel_->send_streams()[0].id);
2736 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
2737 ASSERT_EQ(1u, voice_channel_->send_streams().size());
2738 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
2739 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
2740
2741 // Change session back to audio only.
deadbeefab9b2d12015-10-14 11:33:11 -07002742 SendAudioOnlyStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002743 CreateAndSetRemoteOfferAndLocalAnswer();
2744
2745 EXPECT_EQ(0u, video_channel_->recv_streams().size());
2746 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
2747 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
2748 ASSERT_EQ(1u, voice_channel_->send_streams().size());
2749 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
2750}
2751
2752// This test verifies the call setup when remote answer with video only and
2753// later updates with audio.
2754TEST_F(WebRtcSessionTest, TestAVOfferWithVideoOnlyAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002755 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002756 EXPECT_TRUE(media_engine_->GetVideoChannel(0) == NULL);
2757 EXPECT_TRUE(media_engine_->GetVoiceChannel(0) == NULL);
deadbeefab9b2d12015-10-14 11:33:11 -07002758 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002759 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002760
2761 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002762 options.recv_audio = false;
2763 options.recv_video = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002764 SessionDescriptionInterface* answer = CreateRemoteAnswer(
2765 offer, options, cricket::SEC_ENABLED);
2766
2767 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
2768 // and answer.
2769 SetLocalDescriptionWithoutError(offer);
2770 SetRemoteDescriptionWithoutError(answer);
2771
2772 video_channel_ = media_engine_->GetVideoChannel(0);
2773 voice_channel_ = media_engine_->GetVoiceChannel(0);
2774
2775 ASSERT_TRUE(voice_channel_ == NULL);
2776 ASSERT_TRUE(video_channel_ != NULL);
2777
2778 EXPECT_EQ(0u, video_channel_->recv_streams().size());
2779 ASSERT_EQ(1u, video_channel_->send_streams().size());
2780 EXPECT_EQ(kVideoTrack1, video_channel_->send_streams()[0].id);
2781
2782 // Update the session descriptions, with Audio and Video.
deadbeefab9b2d12015-10-14 11:33:11 -07002783 SendAudioVideoStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002784 CreateAndSetRemoteOfferAndLocalAnswer();
2785
2786 voice_channel_ = media_engine_->GetVoiceChannel(0);
2787 ASSERT_TRUE(voice_channel_ != NULL);
2788
2789 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
2790 ASSERT_EQ(1u, voice_channel_->send_streams().size());
2791 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
2792 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
2793
2794 // Change session back to video only.
deadbeefab9b2d12015-10-14 11:33:11 -07002795 SendVideoOnlyStream2();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002796 CreateAndSetRemoteOfferAndLocalAnswer();
2797
2798 video_channel_ = media_engine_->GetVideoChannel(0);
2799 voice_channel_ = media_engine_->GetVoiceChannel(0);
2800
2801 ASSERT_EQ(1u, video_channel_->recv_streams().size());
2802 EXPECT_EQ(kVideoTrack2, video_channel_->recv_streams()[0].id);
2803 ASSERT_EQ(1u, video_channel_->send_streams().size());
2804 EXPECT_EQ(kVideoTrack2, video_channel_->send_streams()[0].id);
2805}
2806
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002807TEST_F(WebRtcSessionTest, VerifyCryptoParamsInSDP) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002808 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002809 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002810 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002811 VerifyCryptoParams(offer->description());
2812 SetRemoteDescriptionWithoutError(offer.release());
kwibergd1fe2812016-04-27 06:47:29 -07002813 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002814 VerifyCryptoParams(answer->description());
2815}
2816
jbauchcb560652016-08-04 05:20:32 -07002817TEST_F(WebRtcSessionTest, VerifyCryptoParamsInSDPGcm) {
2818 InitWithGcm();
2819 SendAudioVideoStream1();
2820 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
2821 VerifyCryptoParams(offer->description(), true);
2822 SetRemoteDescriptionWithoutError(offer.release());
2823 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
2824 VerifyCryptoParams(answer->description(), true);
2825}
2826
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002827TEST_F(WebRtcSessionTest, VerifyNoCryptoParamsInSDP) {
wu@webrtc.org97077a32013-10-25 21:18:33 +00002828 options_.disable_encryption = true;
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002829 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002830 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002831 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002832 VerifyNoCryptoParams(offer->description(), false);
2833}
2834
2835TEST_F(WebRtcSessionTest, VerifyAnswerFromNonCryptoOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002836 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002837 VerifyAnswerFromNonCryptoOffer();
2838}
2839
2840TEST_F(WebRtcSessionTest, VerifyAnswerFromCryptoOffer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002841 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002842 VerifyAnswerFromCryptoOffer();
2843}
2844
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002845// This test verifies that setLocalDescription fails if
2846// no a=ice-ufrag and a=ice-pwd lines are present in the SDP.
2847TEST_F(WebRtcSessionTest, TestSetLocalDescriptionWithoutIce) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002848 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002849 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002850 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002851
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002852 std::string sdp;
2853 RemoveIceUfragPwdLines(offer.get(), &sdp);
2854 SessionDescriptionInterface* modified_offer =
2855 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002856 SetLocalDescriptionOfferExpectError(kSdpWithoutIceUfragPwd, modified_offer);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002857}
2858
2859// This test verifies that setRemoteDescription fails if
2860// no a=ice-ufrag and a=ice-pwd lines are present in the SDP.
2861TEST_F(WebRtcSessionTest, TestSetRemoteDescriptionWithoutIce) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002862 Init();
kwibergd1fe2812016-04-27 06:47:29 -07002863 std::unique_ptr<SessionDescriptionInterface> offer(CreateRemoteOffer());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002864 std::string sdp;
2865 RemoveIceUfragPwdLines(offer.get(), &sdp);
2866 SessionDescriptionInterface* modified_offer =
2867 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002868 SetRemoteDescriptionOfferExpectError(kSdpWithoutIceUfragPwd, modified_offer);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002869}
2870
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002871// This test verifies that setLocalDescription fails if local offer has
2872// too short ice ufrag and pwd strings.
2873TEST_F(WebRtcSessionTest, TestSetLocalDescriptionInvalidIceCredentials) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002874 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07002875 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07002876 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002877 // Modifying ice ufrag and pwd in local offer with strings smaller than the
2878 // recommended values of 4 and 22 bytes respectively.
deadbeef0ed85b22016-02-23 17:24:52 -08002879 SetIceUfragPwd(offer.get(), "ice", "icepwd");
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002880 std::string error;
deadbeef0ed85b22016-02-23 17:24:52 -08002881 EXPECT_FALSE(session_->SetLocalDescription(offer.release(), &error));
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002882
2883 // Test with string greater than 256.
deadbeef0ed85b22016-02-23 17:24:52 -08002884 offer.reset(CreateOffer());
2885 SetIceUfragPwd(offer.get(), kTooLongIceUfragPwd, kTooLongIceUfragPwd);
2886 EXPECT_FALSE(session_->SetLocalDescription(offer.release(), &error));
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002887}
2888
2889// This test verifies that setRemoteDescription fails if remote offer has
2890// too short ice ufrag and pwd strings.
2891TEST_F(WebRtcSessionTest, TestSetRemoteDescriptionInvalidIceCredentials) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00002892 Init();
kwibergd1fe2812016-04-27 06:47:29 -07002893 std::unique_ptr<SessionDescriptionInterface> offer(CreateRemoteOffer());
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002894 // Modifying ice ufrag and pwd in remote offer with strings smaller than the
2895 // recommended values of 4 and 22 bytes respectively.
deadbeef0ed85b22016-02-23 17:24:52 -08002896 SetIceUfragPwd(offer.get(), "ice", "icepwd");
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002897 std::string error;
deadbeef0ed85b22016-02-23 17:24:52 -08002898 EXPECT_FALSE(session_->SetRemoteDescription(offer.release(), &error));
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002899
deadbeef0ed85b22016-02-23 17:24:52 -08002900 offer.reset(CreateRemoteOffer());
2901 SetIceUfragPwd(offer.get(), kTooLongIceUfragPwd, kTooLongIceUfragPwd);
2902 EXPECT_FALSE(session_->SetRemoteDescription(offer.release(), &error));
buildbot@webrtc.org7aa1a472014-05-23 17:33:05 +00002903}
2904
Honghai Zhang04e91462015-12-11 14:26:22 -08002905// Test that if the remote offer indicates the peer requested ICE restart (via
2906// a new ufrag or pwd), the old ICE candidates are not copied, and vice versa.
2907TEST_F(WebRtcSessionTest, TestSetRemoteOfferWithIceRestart) {
honghaiz503726c2015-07-31 12:37:38 -07002908 Init();
honghaiz503726c2015-07-31 12:37:38 -07002909
2910 // Create the first offer.
kwibergd1fe2812016-04-27 06:47:29 -07002911 std::unique_ptr<SessionDescriptionInterface> offer(CreateRemoteOffer());
deadbeef0ed85b22016-02-23 17:24:52 -08002912 SetIceUfragPwd(offer.get(), "0123456789012345", "abcdefghijklmnopqrstuvwx");
honghaiz503726c2015-07-31 12:37:38 -07002913 cricket::Candidate candidate1(1, "udp", rtc::SocketAddress("1.1.1.1", 5000),
2914 0, "", "", "relay", 0, "");
2915 JsepIceCandidate ice_candidate1(kMediaContentName0, kMediaContentIndex0,
2916 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002917 EXPECT_TRUE(offer->AddCandidate(&ice_candidate1));
2918 SetRemoteDescriptionWithoutError(offer.release());
honghaiz503726c2015-07-31 12:37:38 -07002919 EXPECT_EQ(1, session_->remote_description()->candidates(0)->count());
2920
2921 // The second offer has the same ufrag and pwd but different address.
deadbeef0ed85b22016-02-23 17:24:52 -08002922 offer.reset(CreateRemoteOffer());
2923 SetIceUfragPwd(offer.get(), "0123456789012345", "abcdefghijklmnopqrstuvwx");
honghaiz503726c2015-07-31 12:37:38 -07002924 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 6000));
2925 JsepIceCandidate ice_candidate2(kMediaContentName0, kMediaContentIndex0,
2926 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002927 EXPECT_TRUE(offer->AddCandidate(&ice_candidate2));
2928 SetRemoteDescriptionWithoutError(offer.release());
honghaiz503726c2015-07-31 12:37:38 -07002929 EXPECT_EQ(2, session_->remote_description()->candidates(0)->count());
2930
2931 // The third offer has a different ufrag and different address.
deadbeef0ed85b22016-02-23 17:24:52 -08002932 offer.reset(CreateRemoteOffer());
2933 SetIceUfragPwd(offer.get(), "0123456789012333", "abcdefghijklmnopqrstuvwx");
honghaiz503726c2015-07-31 12:37:38 -07002934 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 7000));
2935 JsepIceCandidate ice_candidate3(kMediaContentName0, kMediaContentIndex0,
2936 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002937 EXPECT_TRUE(offer->AddCandidate(&ice_candidate3));
2938 SetRemoteDescriptionWithoutError(offer.release());
honghaiz503726c2015-07-31 12:37:38 -07002939 EXPECT_EQ(1, session_->remote_description()->candidates(0)->count());
2940
2941 // The fourth offer has no candidate but a different ufrag/pwd.
deadbeef0ed85b22016-02-23 17:24:52 -08002942 offer.reset(CreateRemoteOffer());
2943 SetIceUfragPwd(offer.get(), "0123456789012444", "abcdefghijklmnopqrstuvyz");
2944 SetRemoteDescriptionWithoutError(offer.release());
honghaiz503726c2015-07-31 12:37:38 -07002945 EXPECT_EQ(0, session_->remote_description()->candidates(0)->count());
2946}
2947
Honghai Zhang04e91462015-12-11 14:26:22 -08002948// Test that if the remote answer indicates the peer requested ICE restart (via
2949// a new ufrag or pwd), the old ICE candidates are not copied, and vice versa.
2950TEST_F(WebRtcSessionTest, TestSetRemoteAnswerWithIceRestart) {
2951 Init();
2952 SessionDescriptionInterface* offer = CreateOffer();
2953 SetLocalDescriptionWithoutError(offer);
Honghai Zhang04e91462015-12-11 14:26:22 -08002954
2955 // Create the first answer.
kwibergd1fe2812016-04-27 06:47:29 -07002956 std::unique_ptr<JsepSessionDescription> answer(CreateRemoteAnswer(offer));
deadbeef0ed85b22016-02-23 17:24:52 -08002957 answer->set_type(JsepSessionDescription::kPrAnswer);
2958 SetIceUfragPwd(answer.get(), "0123456789012345", "abcdefghijklmnopqrstuvwx");
Honghai Zhang04e91462015-12-11 14:26:22 -08002959 cricket::Candidate candidate1(1, "udp", rtc::SocketAddress("1.1.1.1", 5000),
2960 0, "", "", "relay", 0, "");
2961 JsepIceCandidate ice_candidate1(kMediaContentName0, kMediaContentIndex0,
2962 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002963 EXPECT_TRUE(answer->AddCandidate(&ice_candidate1));
2964 SetRemoteDescriptionWithoutError(answer.release());
Honghai Zhang04e91462015-12-11 14:26:22 -08002965 EXPECT_EQ(1, session_->remote_description()->candidates(0)->count());
2966
2967 // The second answer has the same ufrag and pwd but different address.
deadbeef0ed85b22016-02-23 17:24:52 -08002968 answer.reset(CreateRemoteAnswer(offer));
2969 answer->set_type(JsepSessionDescription::kPrAnswer);
2970 SetIceUfragPwd(answer.get(), "0123456789012345", "abcdefghijklmnopqrstuvwx");
Honghai Zhang04e91462015-12-11 14:26:22 -08002971 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 6000));
2972 JsepIceCandidate ice_candidate2(kMediaContentName0, kMediaContentIndex0,
2973 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002974 EXPECT_TRUE(answer->AddCandidate(&ice_candidate2));
2975 SetRemoteDescriptionWithoutError(answer.release());
Honghai Zhang04e91462015-12-11 14:26:22 -08002976 EXPECT_EQ(2, session_->remote_description()->candidates(0)->count());
2977
2978 // The third answer has a different ufrag and different address.
deadbeef0ed85b22016-02-23 17:24:52 -08002979 answer.reset(CreateRemoteAnswer(offer));
2980 answer->set_type(JsepSessionDescription::kPrAnswer);
2981 SetIceUfragPwd(answer.get(), "0123456789012333", "abcdefghijklmnopqrstuvwx");
Honghai Zhang04e91462015-12-11 14:26:22 -08002982 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 7000));
2983 JsepIceCandidate ice_candidate3(kMediaContentName0, kMediaContentIndex0,
2984 candidate1);
deadbeef0ed85b22016-02-23 17:24:52 -08002985 EXPECT_TRUE(answer->AddCandidate(&ice_candidate3));
2986 SetRemoteDescriptionWithoutError(answer.release());
Honghai Zhang04e91462015-12-11 14:26:22 -08002987 EXPECT_EQ(1, session_->remote_description()->candidates(0)->count());
2988
2989 // The fourth answer has no candidate but a different ufrag/pwd.
deadbeef0ed85b22016-02-23 17:24:52 -08002990 answer.reset(CreateRemoteAnswer(offer));
2991 answer->set_type(JsepSessionDescription::kPrAnswer);
2992 SetIceUfragPwd(answer.get(), "0123456789012444", "abcdefghijklmnopqrstuvyz");
2993 SetRemoteDescriptionWithoutError(answer.release());
Honghai Zhang04e91462015-12-11 14:26:22 -08002994 EXPECT_EQ(0, session_->remote_description()->candidates(0)->count());
2995}
2996
Donald Curtisd4f769d2015-05-28 09:48:21 -07002997// Test that candidates sent to the "video" transport do not get pushed down to
deadbeefd59daf82015-10-14 15:02:44 -07002998// the "audio" transport channel when bundling.
Donald Curtisd4f769d2015-05-28 09:48:21 -07002999TEST_F(WebRtcSessionTest, TestIgnoreCandidatesForUnusedTransportWhenBundling) {
3000 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
3001
3002 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyBalanced);
deadbeefab9b2d12015-10-14 11:33:11 -07003003 SendAudioVideoStream1();
Donald Curtisd4f769d2015-05-28 09:48:21 -07003004
3005 PeerConnectionInterface::RTCOfferAnswerOptions options;
3006 options.use_rtp_mux = true;
3007
3008 SessionDescriptionInterface* offer = CreateRemoteOffer();
3009 SetRemoteDescriptionWithoutError(offer);
3010
htaa2a49d92016-03-04 02:51:39 -08003011 SessionDescriptionInterface* answer = CreateAnswer();
Donald Curtisd4f769d2015-05-28 09:48:21 -07003012 SetLocalDescriptionWithoutError(answer);
3013
deadbeefcbecd352015-09-23 11:50:27 -07003014 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3015 session_->video_rtp_transport_channel());
Donald Curtisd4f769d2015-05-28 09:48:21 -07003016
deadbeefcbecd352015-09-23 11:50:27 -07003017 cricket::BaseChannel* voice_channel = session_->voice_channel();
3018 ASSERT(voice_channel != NULL);
Donald Curtisd4f769d2015-05-28 09:48:21 -07003019
3020 // Checks if one of the transport channels contains a connection using a given
3021 // port.
deadbeefcbecd352015-09-23 11:50:27 -07003022 auto connection_with_remote_port = [this, voice_channel](int port) {
deadbeefd59daf82015-10-14 15:02:44 -07003023 SessionStats stats;
deadbeefcbecd352015-09-23 11:50:27 -07003024 session_->GetChannelTransportStats(voice_channel, &stats);
3025 for (auto& kv : stats.transport_stats) {
3026 for (auto& chan_stat : kv.second.channel_stats) {
3027 for (auto& conn_info : chan_stat.connection_infos) {
3028 if (conn_info.remote_candidate.address().port() == port) {
3029 return true;
3030 }
Donald Curtisd4f769d2015-05-28 09:48:21 -07003031 }
3032 }
3033 }
3034 return false;
3035 };
3036
3037 EXPECT_FALSE(connection_with_remote_port(5000));
3038 EXPECT_FALSE(connection_with_remote_port(5001));
3039 EXPECT_FALSE(connection_with_remote_port(6000));
3040
3041 // The way the *_WAIT checks work is they only wait if the condition fails,
3042 // which does not help in the case where state is not changing. This is
3043 // problematic in this test since we want to verify that adding a video
3044 // candidate does _not_ change state. So we interleave candidates and assume
3045 // that messages are executed in the order they were posted.
3046
3047 // First audio candidate.
3048 cricket::Candidate candidate0;
3049 candidate0.set_address(rtc::SocketAddress("1.1.1.1", 5000));
3050 candidate0.set_component(1);
3051 candidate0.set_protocol("udp");
3052 JsepIceCandidate ice_candidate0(kMediaContentName0, kMediaContentIndex0,
3053 candidate0);
3054 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate0));
3055
3056 // Video candidate.
3057 cricket::Candidate candidate1;
3058 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 6000));
3059 candidate1.set_component(1);
3060 candidate1.set_protocol("udp");
3061 JsepIceCandidate ice_candidate1(kMediaContentName1, kMediaContentIndex1,
3062 candidate1);
3063 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate1));
3064
3065 // Second audio candidate.
3066 cricket::Candidate candidate2;
3067 candidate2.set_address(rtc::SocketAddress("1.1.1.1", 5001));
3068 candidate2.set_component(1);
3069 candidate2.set_protocol("udp");
3070 JsepIceCandidate ice_candidate2(kMediaContentName0, kMediaContentIndex0,
3071 candidate2);
3072 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate2));
3073
3074 EXPECT_TRUE_WAIT(connection_with_remote_port(5000), 1000);
3075 EXPECT_TRUE_WAIT(connection_with_remote_port(5001), 1000);
3076
3077 // No need here for a _WAIT check since we are checking that state hasn't
3078 // changed: if this is false we would be doing waits for nothing and if this
3079 // is true then there will be no messages processed anyways.
3080 EXPECT_FALSE(connection_with_remote_port(6000));
3081}
3082
deadbeefcbecd352015-09-23 11:50:27 -07003083// kBundlePolicyBalanced BUNDLE policy and answer contains BUNDLE.
Donald Curtis0e209b02015-03-24 09:29:54 -07003084TEST_F(WebRtcSessionTest, TestBalancedBundleInAnswer) {
3085 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyBalanced);
deadbeefab9b2d12015-10-14 11:33:11 -07003086 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00003087
3088 PeerConnectionInterface::RTCOfferAnswerOptions options;
3089 options.use_rtp_mux = true;
3090
3091 SessionDescriptionInterface* offer = CreateOffer(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003092 SetLocalDescriptionWithoutError(offer);
Donald Curtis0e209b02015-03-24 09:29:54 -07003093
deadbeefcbecd352015-09-23 11:50:27 -07003094 EXPECT_NE(session_->voice_rtp_transport_channel(),
3095 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003096
deadbeefab9b2d12015-10-14 11:33:11 -07003097 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003098 SessionDescriptionInterface* answer =
3099 CreateRemoteAnswer(session_->local_description());
3100 SetRemoteDescriptionWithoutError(answer);
3101
deadbeefcbecd352015-09-23 11:50:27 -07003102 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3103 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003104}
3105
deadbeefcbecd352015-09-23 11:50:27 -07003106// kBundlePolicyBalanced BUNDLE policy but no BUNDLE in the answer.
Donald Curtis0e209b02015-03-24 09:29:54 -07003107TEST_F(WebRtcSessionTest, TestBalancedNoBundleInAnswer) {
3108 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyBalanced);
deadbeefab9b2d12015-10-14 11:33:11 -07003109 SendAudioVideoStream1();
Peter Thatcher4eddf182015-04-30 10:55:59 -07003110
Donald Curtis0e209b02015-03-24 09:29:54 -07003111 PeerConnectionInterface::RTCOfferAnswerOptions options;
3112 options.use_rtp_mux = true;
3113
3114 SessionDescriptionInterface* offer = CreateOffer(options);
3115 SetLocalDescriptionWithoutError(offer);
3116
deadbeefcbecd352015-09-23 11:50:27 -07003117 EXPECT_NE(session_->voice_rtp_transport_channel(),
3118 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003119
deadbeefab9b2d12015-10-14 11:33:11 -07003120 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003121
3122 // Remove BUNDLE from the answer.
kwibergd1fe2812016-04-27 06:47:29 -07003123 std::unique_ptr<SessionDescriptionInterface> answer(
Donald Curtis0e209b02015-03-24 09:29:54 -07003124 CreateRemoteAnswer(session_->local_description()));
3125 cricket::SessionDescription* answer_copy = answer->description()->Copy();
3126 answer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
3127 JsepSessionDescription* modified_answer =
3128 new JsepSessionDescription(JsepSessionDescription::kAnswer);
3129 modified_answer->Initialize(answer_copy, "1", "1");
3130 SetRemoteDescriptionWithoutError(modified_answer); //
3131
deadbeefcbecd352015-09-23 11:50:27 -07003132 EXPECT_NE(session_->voice_rtp_transport_channel(),
3133 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003134}
3135
3136// kBundlePolicyMaxBundle policy with BUNDLE in the answer.
3137TEST_F(WebRtcSessionTest, TestMaxBundleBundleInAnswer) {
3138 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003139 SendAudioVideoStream1();
Donald Curtis0e209b02015-03-24 09:29:54 -07003140
3141 PeerConnectionInterface::RTCOfferAnswerOptions options;
3142 options.use_rtp_mux = true;
3143
3144 SessionDescriptionInterface* offer = CreateOffer(options);
3145 SetLocalDescriptionWithoutError(offer);
3146
deadbeefcbecd352015-09-23 11:50:27 -07003147 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3148 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003149
deadbeefab9b2d12015-10-14 11:33:11 -07003150 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003151 SessionDescriptionInterface* answer =
3152 CreateRemoteAnswer(session_->local_description());
3153 SetRemoteDescriptionWithoutError(answer);
3154
deadbeefcbecd352015-09-23 11:50:27 -07003155 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3156 session_->video_rtp_transport_channel());
3157}
3158
3159// kBundlePolicyMaxBundle policy with BUNDLE in the answer, but no
3160// audio content in the answer.
3161TEST_F(WebRtcSessionTest, TestMaxBundleRejectAudio) {
3162 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003163 SendAudioVideoStream1();
deadbeefcbecd352015-09-23 11:50:27 -07003164
3165 PeerConnectionInterface::RTCOfferAnswerOptions options;
3166 options.use_rtp_mux = true;
3167
3168 SessionDescriptionInterface* offer = CreateOffer(options);
3169 SetLocalDescriptionWithoutError(offer);
3170
3171 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3172 session_->video_rtp_transport_channel());
3173
deadbeefab9b2d12015-10-14 11:33:11 -07003174 SendAudioVideoStream2();
deadbeefcbecd352015-09-23 11:50:27 -07003175 cricket::MediaSessionOptions recv_options;
3176 recv_options.recv_audio = false;
3177 recv_options.recv_video = true;
3178 SessionDescriptionInterface* answer =
3179 CreateRemoteAnswer(session_->local_description(), recv_options);
3180 SetRemoteDescriptionWithoutError(answer);
3181
deadbeefd59daf82015-10-14 15:02:44 -07003182 EXPECT_TRUE(nullptr == session_->voice_channel());
3183 EXPECT_TRUE(nullptr != session_->video_rtp_transport_channel());
deadbeefcbecd352015-09-23 11:50:27 -07003184
deadbeefd59daf82015-10-14 15:02:44 -07003185 session_->Close();
3186 EXPECT_TRUE(nullptr == session_->voice_rtp_transport_channel());
3187 EXPECT_TRUE(nullptr == session_->voice_rtcp_transport_channel());
3188 EXPECT_TRUE(nullptr == session_->video_rtp_transport_channel());
3189 EXPECT_TRUE(nullptr == session_->video_rtcp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003190}
3191
3192// kBundlePolicyMaxBundle policy but no BUNDLE in the answer.
3193TEST_F(WebRtcSessionTest, TestMaxBundleNoBundleInAnswer) {
3194 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003195 SendAudioVideoStream1();
Peter Thatcher4eddf182015-04-30 10:55:59 -07003196
Donald Curtis0e209b02015-03-24 09:29:54 -07003197 PeerConnectionInterface::RTCOfferAnswerOptions options;
3198 options.use_rtp_mux = true;
3199
3200 SessionDescriptionInterface* offer = CreateOffer(options);
3201 SetLocalDescriptionWithoutError(offer);
3202
deadbeefcbecd352015-09-23 11:50:27 -07003203 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3204 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003205
deadbeefab9b2d12015-10-14 11:33:11 -07003206 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003207
3208 // Remove BUNDLE from the answer.
kwibergd1fe2812016-04-27 06:47:29 -07003209 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003210 CreateRemoteAnswer(session_->local_description()));
3211 cricket::SessionDescription* answer_copy = answer->description()->Copy();
3212 answer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
3213 JsepSessionDescription* modified_answer =
3214 new JsepSessionDescription(JsepSessionDescription::kAnswer);
3215 modified_answer->Initialize(answer_copy, "1", "1");
3216 SetRemoteDescriptionWithoutError(modified_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003217
deadbeefcbecd352015-09-23 11:50:27 -07003218 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3219 session_->video_rtp_transport_channel());
3220}
3221
3222// kBundlePolicyMaxBundle policy with BUNDLE in the remote offer.
3223TEST_F(WebRtcSessionTest, TestMaxBundleBundleInRemoteOffer) {
3224 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003225 SendAudioVideoStream1();
deadbeefcbecd352015-09-23 11:50:27 -07003226
3227 SessionDescriptionInterface* offer = CreateRemoteOffer();
3228 SetRemoteDescriptionWithoutError(offer);
3229
3230 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3231 session_->video_rtp_transport_channel());
3232
deadbeefab9b2d12015-10-14 11:33:11 -07003233 SendAudioVideoStream2();
htaa2a49d92016-03-04 02:51:39 -08003234 SessionDescriptionInterface* answer = CreateAnswer();
deadbeefcbecd352015-09-23 11:50:27 -07003235 SetLocalDescriptionWithoutError(answer);
3236
3237 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3238 session_->video_rtp_transport_channel());
3239}
3240
3241// kBundlePolicyMaxBundle policy but no BUNDLE in the remote offer.
3242TEST_F(WebRtcSessionTest, TestMaxBundleNoBundleInRemoteOffer) {
3243 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003244 SendAudioVideoStream1();
deadbeefcbecd352015-09-23 11:50:27 -07003245
3246 // Remove BUNDLE from the offer.
kwibergd1fe2812016-04-27 06:47:29 -07003247 std::unique_ptr<SessionDescriptionInterface> offer(CreateRemoteOffer());
deadbeefcbecd352015-09-23 11:50:27 -07003248 cricket::SessionDescription* offer_copy = offer->description()->Copy();
3249 offer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
3250 JsepSessionDescription* modified_offer =
3251 new JsepSessionDescription(JsepSessionDescription::kOffer);
3252 modified_offer->Initialize(offer_copy, "1", "1");
3253
3254 // Expect an error when applying the remote description
3255 SetRemoteDescriptionExpectError(JsepSessionDescription::kOffer,
3256 kCreateChannelFailed, modified_offer);
Donald Curtis0e209b02015-03-24 09:29:54 -07003257}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003258
Peter Thatcher4eddf182015-04-30 10:55:59 -07003259// kBundlePolicyMaxCompat bundle policy and answer contains BUNDLE.
Donald Curtis0e209b02015-03-24 09:29:54 -07003260TEST_F(WebRtcSessionTest, TestMaxCompatBundleInAnswer) {
3261 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxCompat);
deadbeefab9b2d12015-10-14 11:33:11 -07003262 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003263
Donald Curtis0e209b02015-03-24 09:29:54 -07003264 PeerConnectionInterface::RTCOfferAnswerOptions options;
3265 options.use_rtp_mux = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003266
Donald Curtis0e209b02015-03-24 09:29:54 -07003267 SessionDescriptionInterface* offer = CreateOffer(options);
3268 SetLocalDescriptionWithoutError(offer);
3269
deadbeefcbecd352015-09-23 11:50:27 -07003270 EXPECT_NE(session_->voice_rtp_transport_channel(),
3271 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003272
deadbeefab9b2d12015-10-14 11:33:11 -07003273 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003274 SessionDescriptionInterface* answer =
3275 CreateRemoteAnswer(session_->local_description());
3276 SetRemoteDescriptionWithoutError(answer);
3277
3278 // This should lead to an audio-only call but isn't implemented
3279 // correctly yet.
deadbeefcbecd352015-09-23 11:50:27 -07003280 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3281 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003282}
3283
deadbeefcbecd352015-09-23 11:50:27 -07003284// kBundlePolicyMaxCompat BUNDLE policy but no BUNDLE in the answer.
Donald Curtis0e209b02015-03-24 09:29:54 -07003285TEST_F(WebRtcSessionTest, TestMaxCompatNoBundleInAnswer) {
3286 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxCompat);
deadbeefab9b2d12015-10-14 11:33:11 -07003287 SendAudioVideoStream1();
Donald Curtis0e209b02015-03-24 09:29:54 -07003288 PeerConnectionInterface::RTCOfferAnswerOptions options;
3289 options.use_rtp_mux = true;
3290
3291 SessionDescriptionInterface* offer = CreateOffer(options);
3292 SetLocalDescriptionWithoutError(offer);
3293
deadbeefcbecd352015-09-23 11:50:27 -07003294 EXPECT_NE(session_->voice_rtp_transport_channel(),
3295 session_->video_rtp_transport_channel());
Donald Curtis0e209b02015-03-24 09:29:54 -07003296
deadbeefab9b2d12015-10-14 11:33:11 -07003297 SendAudioVideoStream2();
Donald Curtis0e209b02015-03-24 09:29:54 -07003298
3299 // Remove BUNDLE from the answer.
kwibergd1fe2812016-04-27 06:47:29 -07003300 std::unique_ptr<SessionDescriptionInterface> answer(
Donald Curtis0e209b02015-03-24 09:29:54 -07003301 CreateRemoteAnswer(session_->local_description()));
3302 cricket::SessionDescription* answer_copy = answer->description()->Copy();
3303 answer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
3304 JsepSessionDescription* modified_answer =
3305 new JsepSessionDescription(JsepSessionDescription::kAnswer);
3306 modified_answer->Initialize(answer_copy, "1", "1");
3307 SetRemoteDescriptionWithoutError(modified_answer); //
3308
deadbeefcbecd352015-09-23 11:50:27 -07003309 EXPECT_NE(session_->voice_rtp_transport_channel(),
3310 session_->video_rtp_transport_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003311}
3312
Peter Thatcher4eddf182015-04-30 10:55:59 -07003313// kBundlePolicyMaxbundle and then we call SetRemoteDescription first.
3314TEST_F(WebRtcSessionTest, TestMaxBundleWithSetRemoteDescriptionFirst) {
3315 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyMaxBundle);
deadbeefab9b2d12015-10-14 11:33:11 -07003316 SendAudioVideoStream1();
Peter Thatcher4eddf182015-04-30 10:55:59 -07003317
3318 PeerConnectionInterface::RTCOfferAnswerOptions options;
3319 options.use_rtp_mux = true;
3320
3321 SessionDescriptionInterface* offer = CreateOffer(options);
3322 SetRemoteDescriptionWithoutError(offer);
3323
deadbeefcbecd352015-09-23 11:50:27 -07003324 EXPECT_EQ(session_->voice_rtp_transport_channel(),
3325 session_->video_rtp_transport_channel());
Peter Thatcher4eddf182015-04-30 10:55:59 -07003326}
3327
skvlad6c87a672016-05-17 17:49:52 -07003328// Adding a new channel to a BUNDLE which is already connected should directly
3329// assign the bundle transport to the channel, without first setting a
3330// disconnected non-bundle transport and then replacing it. The application
3331// should not receive any changes in the ICE state.
3332TEST_F(WebRtcSessionTest, TestAddChannelToConnectedBundle) {
3333 LoopbackNetworkConfiguration config;
3334 LoopbackNetworkManager loopback_network_manager(this, config);
3335 // Both BUNDLE and RTCP-mux need to be enabled for the ICE state to remain
3336 // connected. Disabling either of these two means that we need to wait for the
3337 // answer to find out if more transports are needed.
3338 configuration_.bundle_policy =
3339 PeerConnectionInterface::kBundlePolicyMaxBundle;
3340 configuration_.rtcp_mux_policy =
3341 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3342 options_.disable_encryption = true;
3343 Init();
3344
3345 // Negotiate an audio channel with MAX_BUNDLE enabled.
3346 SendAudioOnlyStream2();
3347 SessionDescriptionInterface* offer = CreateOffer();
3348 SetLocalDescriptionWithoutError(offer);
3349 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3350 observer_.ice_gathering_state_, kIceCandidatesTimeout);
3351 std::string sdp;
3352 offer->ToString(&sdp);
3353 SessionDescriptionInterface* answer = webrtc::CreateSessionDescription(
3354 JsepSessionDescription::kAnswer, sdp, nullptr);
3355 ASSERT_TRUE(answer != NULL);
3356 SetRemoteDescriptionWithoutError(answer);
3357
3358 // Wait for the ICE state to stabilize.
3359 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3360 observer_.ice_connection_state_, kIceCandidatesTimeout);
3361 observer_.ice_connection_state_history_.clear();
3362
3363 // Now add a video channel which should be using the same bundle transport.
3364 SendAudioVideoStream2();
3365 offer = CreateOffer();
3366 offer->ToString(&sdp);
3367 SetLocalDescriptionWithoutError(offer);
3368 answer = webrtc::CreateSessionDescription(JsepSessionDescription::kAnswer,
3369 sdp, nullptr);
3370 ASSERT_TRUE(answer != NULL);
3371 SetRemoteDescriptionWithoutError(answer);
3372
3373 // Wait for ICE state to stabilize
3374 rtc::Thread::Current()->ProcessMessages(0);
3375 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3376 observer_.ice_connection_state_, kIceCandidatesTimeout);
3377
3378 // No ICE state changes are expected to happen.
3379 EXPECT_EQ(0, observer_.ice_connection_state_history_.size());
3380}
3381
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003382TEST_F(WebRtcSessionTest, TestRequireRtcpMux) {
3383 InitWithRtcpMuxPolicy(PeerConnectionInterface::kRtcpMuxPolicyRequire);
deadbeefab9b2d12015-10-14 11:33:11 -07003384 SendAudioVideoStream1();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003385
3386 PeerConnectionInterface::RTCOfferAnswerOptions options;
3387 SessionDescriptionInterface* offer = CreateOffer(options);
3388 SetLocalDescriptionWithoutError(offer);
3389
deadbeefcbecd352015-09-23 11:50:27 -07003390 EXPECT_TRUE(session_->voice_rtcp_transport_channel() == NULL);
3391 EXPECT_TRUE(session_->video_rtcp_transport_channel() == NULL);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003392
deadbeefab9b2d12015-10-14 11:33:11 -07003393 SendAudioVideoStream2();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003394 SessionDescriptionInterface* answer =
3395 CreateRemoteAnswer(session_->local_description());
3396 SetRemoteDescriptionWithoutError(answer);
3397
deadbeefcbecd352015-09-23 11:50:27 -07003398 EXPECT_TRUE(session_->voice_rtcp_transport_channel() == NULL);
3399 EXPECT_TRUE(session_->video_rtcp_transport_channel() == NULL);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003400}
3401
3402TEST_F(WebRtcSessionTest, TestNegotiateRtcpMux) {
3403 InitWithRtcpMuxPolicy(PeerConnectionInterface::kRtcpMuxPolicyNegotiate);
deadbeefab9b2d12015-10-14 11:33:11 -07003404 SendAudioVideoStream1();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003405
3406 PeerConnectionInterface::RTCOfferAnswerOptions options;
3407 SessionDescriptionInterface* offer = CreateOffer(options);
3408 SetLocalDescriptionWithoutError(offer);
3409
deadbeefcbecd352015-09-23 11:50:27 -07003410 EXPECT_TRUE(session_->voice_rtcp_transport_channel() != NULL);
3411 EXPECT_TRUE(session_->video_rtcp_transport_channel() != NULL);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003412
deadbeefab9b2d12015-10-14 11:33:11 -07003413 SendAudioVideoStream2();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003414 SessionDescriptionInterface* answer =
3415 CreateRemoteAnswer(session_->local_description());
3416 SetRemoteDescriptionWithoutError(answer);
3417
deadbeefcbecd352015-09-23 11:50:27 -07003418 EXPECT_TRUE(session_->voice_rtcp_transport_channel() == NULL);
3419 EXPECT_TRUE(session_->video_rtcp_transport_channel() == NULL);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07003420}
3421
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003422// This test verifies that SetLocalDescription and SetRemoteDescription fails
3423// if BUNDLE is enabled but rtcp-mux is disabled in m-lines.
3424TEST_F(WebRtcSessionTest, TestDisabledRtcpMuxWithBundleEnabled) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003425 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003426 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00003427
3428 PeerConnectionInterface::RTCOfferAnswerOptions options;
3429 options.use_rtp_mux = true;
3430
3431 SessionDescriptionInterface* offer = CreateOffer(options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003432 std::string offer_str;
3433 offer->ToString(&offer_str);
3434 // Disable rtcp-mux
3435 const std::string rtcp_mux = "rtcp-mux";
3436 const std::string xrtcp_mux = "xrtcp-mux";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003437 rtc::replace_substrs(rtcp_mux.c_str(), rtcp_mux.length(),
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003438 xrtcp_mux.c_str(), xrtcp_mux.length(),
3439 &offer_str);
deadbeefcbecd352015-09-23 11:50:27 -07003440 JsepSessionDescription* local_offer =
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003441 new JsepSessionDescription(JsepSessionDescription::kOffer);
3442 EXPECT_TRUE((local_offer)->Initialize(offer_str, NULL));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003443 SetLocalDescriptionOfferExpectError(kBundleWithoutRtcpMux, local_offer);
deadbeefcbecd352015-09-23 11:50:27 -07003444 JsepSessionDescription* remote_offer =
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003445 new JsepSessionDescription(JsepSessionDescription::kOffer);
3446 EXPECT_TRUE((remote_offer)->Initialize(offer_str, NULL));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003447 SetRemoteDescriptionOfferExpectError(kBundleWithoutRtcpMux, remote_offer);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003448 // Trying unmodified SDP.
3449 SetLocalDescriptionWithoutError(offer);
3450}
3451
jbauchcb560652016-08-04 05:20:32 -07003452TEST_F(WebRtcSessionTest, SetSetupGcm) {
3453 InitWithGcm();
3454 SendAudioVideoStream1();
3455 CreateAndSetRemoteOfferAndLocalAnswer();
3456}
3457
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003458TEST_F(WebRtcSessionTest, CanNotInsertDtmf) {
3459 TestCanInsertDtmf(false);
3460}
3461
3462TEST_F(WebRtcSessionTest, CanInsertDtmf) {
3463 TestCanInsertDtmf(true);
3464}
3465
3466TEST_F(WebRtcSessionTest, InsertDtmf) {
3467 // Setup
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003468 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003469 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003470 CreateAndSetRemoteOfferAndLocalAnswer();
3471 FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
3472 EXPECT_EQ(0U, channel->dtmf_info_queue().size());
3473
3474 // Insert DTMF
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003475 const int expected_duration = 90;
3476 session_->InsertDtmf(kAudioTrack1, 0, expected_duration);
3477 session_->InsertDtmf(kAudioTrack1, 1, expected_duration);
3478 session_->InsertDtmf(kAudioTrack1, 2, expected_duration);
3479
3480 // Verify
3481 ASSERT_EQ(3U, channel->dtmf_info_queue().size());
Peter Boström0c4e06b2015-10-07 12:23:21 +02003482 const uint32_t send_ssrc = channel->send_streams()[0].first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003483 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[0], send_ssrc, 0,
solenberg1d63dd02015-12-02 12:35:09 -08003484 expected_duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003485 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[1], send_ssrc, 1,
solenberg1d63dd02015-12-02 12:35:09 -08003486 expected_duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003487 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[2], send_ssrc, 2,
solenberg1d63dd02015-12-02 12:35:09 -08003488 expected_duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003489}
3490
deadbeefd59daf82015-10-14 15:02:44 -07003491// This test verifies the |initial_offerer| flag when session initiates the
3492// call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003493TEST_F(WebRtcSessionTest, TestInitiatorFlagAsOriginator) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003494 Init();
deadbeefd59daf82015-10-14 15:02:44 -07003495 EXPECT_FALSE(session_->initial_offerer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00003496 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003497 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
3498 SetLocalDescriptionWithoutError(offer);
deadbeefd59daf82015-10-14 15:02:44 -07003499 EXPECT_TRUE(session_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003500 SetRemoteDescriptionWithoutError(answer);
deadbeefd59daf82015-10-14 15:02:44 -07003501 EXPECT_TRUE(session_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003502}
3503
deadbeefd59daf82015-10-14 15:02:44 -07003504// This test verifies the |initial_offerer| flag when session receives the call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003505TEST_F(WebRtcSessionTest, TestInitiatorFlagAsReceiver) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003506 Init();
deadbeefd59daf82015-10-14 15:02:44 -07003507 EXPECT_FALSE(session_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003508 SessionDescriptionInterface* offer = CreateRemoteOffer();
3509 SetRemoteDescriptionWithoutError(offer);
htaa2a49d92016-03-04 02:51:39 -08003510 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003511
deadbeefd59daf82015-10-14 15:02:44 -07003512 EXPECT_FALSE(session_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003513 SetLocalDescriptionWithoutError(answer);
deadbeefd59daf82015-10-14 15:02:44 -07003514 EXPECT_FALSE(session_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003515}
3516
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003517// Verifing local offer and remote answer have matching m-lines as per RFC 3264.
3518TEST_F(WebRtcSessionTest, TestIncorrectMLinesInRemoteAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003519 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003520 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00003521 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003522 SetLocalDescriptionWithoutError(offer);
kwibergd1fe2812016-04-27 06:47:29 -07003523 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003524 CreateRemoteAnswer(session_->local_description()));
3525
3526 cricket::SessionDescription* answer_copy = answer->description()->Copy();
3527 answer_copy->RemoveContentByName("video");
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003528 JsepSessionDescription* modified_answer =
3529 new JsepSessionDescription(JsepSessionDescription::kAnswer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003530
3531 EXPECT_TRUE(modified_answer->Initialize(answer_copy,
3532 answer->session_id(),
3533 answer->session_version()));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003534 SetRemoteDescriptionAnswerExpectError(kMlineMismatch, modified_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003535
wu@webrtc.org4e393072014-04-07 17:04:35 +00003536 // Different content names.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003537 std::string sdp;
3538 EXPECT_TRUE(answer->ToString(&sdp));
3539 const std::string kAudioMid = "a=mid:audio";
3540 const std::string kAudioMidReplaceStr = "a=mid:audio_content_name";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003541 rtc::replace_substrs(kAudioMid.c_str(), kAudioMid.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003542 kAudioMidReplaceStr.c_str(),
3543 kAudioMidReplaceStr.length(),
3544 &sdp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003545 SessionDescriptionInterface* modified_answer1 =
3546 CreateSessionDescription(JsepSessionDescription::kAnswer, sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003547 SetRemoteDescriptionAnswerExpectError(kMlineMismatch, modified_answer1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003548
wu@webrtc.org4e393072014-04-07 17:04:35 +00003549 // Different media types.
3550 EXPECT_TRUE(answer->ToString(&sdp));
3551 const std::string kAudioMline = "m=audio";
3552 const std::string kAudioMlineReplaceStr = "m=video";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003553 rtc::replace_substrs(kAudioMline.c_str(), kAudioMline.length(),
wu@webrtc.org4e393072014-04-07 17:04:35 +00003554 kAudioMlineReplaceStr.c_str(),
3555 kAudioMlineReplaceStr.length(),
3556 &sdp);
3557 SessionDescriptionInterface* modified_answer2 =
3558 CreateSessionDescription(JsepSessionDescription::kAnswer, sdp, NULL);
3559 SetRemoteDescriptionAnswerExpectError(kMlineMismatch, modified_answer2);
3560
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003561 SetRemoteDescriptionWithoutError(answer.release());
3562}
3563
3564// Verifying remote offer and local answer have matching m-lines as per
3565// RFC 3264.
3566TEST_F(WebRtcSessionTest, TestIncorrectMLinesInLocalAnswer) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003567 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003568 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003569 SessionDescriptionInterface* offer = CreateRemoteOffer();
3570 SetRemoteDescriptionWithoutError(offer);
htaa2a49d92016-03-04 02:51:39 -08003571 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003572
3573 cricket::SessionDescription* answer_copy = answer->description()->Copy();
3574 answer_copy->RemoveContentByName("video");
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003575 JsepSessionDescription* modified_answer =
3576 new JsepSessionDescription(JsepSessionDescription::kAnswer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003577
3578 EXPECT_TRUE(modified_answer->Initialize(answer_copy,
3579 answer->session_id(),
3580 answer->session_version()));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003581 SetLocalDescriptionAnswerExpectError(kMlineMismatch, modified_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003582 SetLocalDescriptionWithoutError(answer);
3583}
3584
3585// This test verifies that WebRtcSession does not start candidate allocation
3586// before SetLocalDescription is called.
3587TEST_F(WebRtcSessionTest, TestIceStartAfterSetLocalDescriptionOnly) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003588 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003589 SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003590 SessionDescriptionInterface* offer = CreateRemoteOffer();
3591 cricket::Candidate candidate;
3592 candidate.set_component(1);
3593 JsepIceCandidate ice_candidate(kMediaContentName0, kMediaContentIndex0,
3594 candidate);
3595 EXPECT_TRUE(offer->AddCandidate(&ice_candidate));
3596 cricket::Candidate candidate1;
3597 candidate1.set_component(1);
3598 JsepIceCandidate ice_candidate1(kMediaContentName1, kMediaContentIndex1,
3599 candidate1);
3600 EXPECT_TRUE(offer->AddCandidate(&ice_candidate1));
3601 SetRemoteDescriptionWithoutError(offer);
deadbeefcbecd352015-09-23 11:50:27 -07003602 ASSERT_TRUE(session_->voice_rtp_transport_channel() != NULL);
3603 ASSERT_TRUE(session_->video_rtp_transport_channel() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003604
3605 // Pump for 1 second and verify that no candidates are generated.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003606 rtc::Thread::Current()->ProcessMessages(1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003607 EXPECT_TRUE(observer_.mline_0_candidates_.empty());
3608 EXPECT_TRUE(observer_.mline_1_candidates_.empty());
3609
htaa2a49d92016-03-04 02:51:39 -08003610 SessionDescriptionInterface* answer = CreateAnswer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003611 SetLocalDescriptionWithoutError(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003612 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
3613}
3614
3615// This test verifies that crypto parameter is updated in local session
3616// description as per security policy set in MediaSessionDescriptionFactory.
3617TEST_F(WebRtcSessionTest, TestCryptoAfterSetLocalDescription) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003618 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003619 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003620 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003621
3622 // Making sure SetLocalDescription correctly sets crypto value in
3623 // SessionDescription object after de-serialization of sdp string. The value
3624 // will be set as per MediaSessionDescriptionFactory.
3625 std::string offer_str;
3626 offer->ToString(&offer_str);
3627 SessionDescriptionInterface* jsep_offer_str =
3628 CreateSessionDescription(JsepSessionDescription::kOffer, offer_str, NULL);
3629 SetLocalDescriptionWithoutError(jsep_offer_str);
3630 EXPECT_TRUE(session_->voice_channel()->secure_required());
3631 EXPECT_TRUE(session_->video_channel()->secure_required());
3632}
3633
3634// This test verifies the crypto parameter when security is disabled.
3635TEST_F(WebRtcSessionTest, TestCryptoAfterSetLocalDescriptionWithDisabled) {
wu@webrtc.org97077a32013-10-25 21:18:33 +00003636 options_.disable_encryption = true;
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003637 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003638 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003639 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003640
3641 // Making sure SetLocalDescription correctly sets crypto value in
3642 // SessionDescription object after de-serialization of sdp string. The value
3643 // will be set as per MediaSessionDescriptionFactory.
3644 std::string offer_str;
3645 offer->ToString(&offer_str);
deadbeefcbecd352015-09-23 11:50:27 -07003646 SessionDescriptionInterface* jsep_offer_str =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003647 CreateSessionDescription(JsepSessionDescription::kOffer, offer_str, NULL);
3648 SetLocalDescriptionWithoutError(jsep_offer_str);
3649 EXPECT_FALSE(session_->voice_channel()->secure_required());
3650 EXPECT_FALSE(session_->video_channel()->secure_required());
3651}
3652
3653// This test verifies that an answer contains new ufrag and password if an offer
3654// with new ufrag and password is received.
3655TEST_F(WebRtcSessionTest, TestCreateAnswerWithNewUfragAndPassword) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003656 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003657 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00003658 options.recv_video = true;
kwibergd1fe2812016-04-27 06:47:29 -07003659 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer(options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003660 SetRemoteDescriptionWithoutError(offer.release());
3661
deadbeefab9b2d12015-10-14 11:33:11 -07003662 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003663 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003664 SetLocalDescriptionWithoutError(answer.release());
3665
3666 // Receive an offer with new ufrag and password.
deadbeef0ed85b22016-02-23 17:24:52 -08003667 for (const cricket::ContentInfo& content :
3668 session_->local_description()->description()->contents()) {
3669 options.transport_options[content.name].ice_restart = true;
3670 }
kwibergd1fe2812016-04-27 06:47:29 -07003671 std::unique_ptr<JsepSessionDescription> updated_offer1(
wu@webrtc.org91053e72013-08-10 07:18:04 +00003672 CreateRemoteOffer(options, session_->remote_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003673 SetRemoteDescriptionWithoutError(updated_offer1.release());
3674
kwibergd1fe2812016-04-27 06:47:29 -07003675 std::unique_ptr<SessionDescriptionInterface> updated_answer1(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003676
deadbeef0ed85b22016-02-23 17:24:52 -08003677 EXPECT_FALSE(IceUfragPwdEqual(updated_answer1->description(),
3678 session_->local_description()->description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003679
deadbeef0ed85b22016-02-23 17:24:52 -08003680 // Even a second answer (created before the description is set) should have
3681 // a new ufrag/password.
kwibergd1fe2812016-04-27 06:47:29 -07003682 std::unique_ptr<SessionDescriptionInterface> updated_answer2(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003683
3684 EXPECT_FALSE(IceUfragPwdEqual(updated_answer2->description(),
3685 session_->local_description()->description()));
3686
3687 SetLocalDescriptionWithoutError(updated_answer2.release());
3688}
3689
3690// This test verifies that an answer contains new ufrag and password if an offer
3691// that changes either the ufrag or password (but not both) is received.
3692// RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or
3693// a=ice-pwd attributes compared to the previous SDP from the peer, it
3694// indicates that ICE is restarting for this media stream."
3695TEST_F(WebRtcSessionTest, TestOfferChangingOnlyUfragOrPassword) {
3696 Init();
3697 cricket::MediaSessionOptions options;
3698 options.recv_audio = true;
3699 options.recv_video = true;
3700 // Create an offer with audio and video.
kwibergd1fe2812016-04-27 06:47:29 -07003701 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer(options));
deadbeef0ed85b22016-02-23 17:24:52 -08003702 SetIceUfragPwd(offer.get(), "original_ufrag", "original_password12345");
3703 SetRemoteDescriptionWithoutError(offer.release());
3704
3705 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003706 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003707 SetLocalDescriptionWithoutError(answer.release());
3708
3709 // Receive an offer with a new ufrag but stale password.
kwibergd1fe2812016-04-27 06:47:29 -07003710 std::unique_ptr<JsepSessionDescription> ufrag_changed_offer(
deadbeef0ed85b22016-02-23 17:24:52 -08003711 CreateRemoteOffer(options, session_->remote_description()));
3712 SetIceUfragPwd(ufrag_changed_offer.get(), "modified_ufrag",
3713 "original_password12345");
3714 SetRemoteDescriptionWithoutError(ufrag_changed_offer.release());
3715
kwibergd1fe2812016-04-27 06:47:29 -07003716 std::unique_ptr<SessionDescriptionInterface> updated_answer1(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003717 EXPECT_FALSE(IceUfragPwdEqual(updated_answer1->description(),
3718 session_->local_description()->description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003719 SetLocalDescriptionWithoutError(updated_answer1.release());
deadbeef0ed85b22016-02-23 17:24:52 -08003720
3721 // Receive an offer with a new password but stale ufrag.
kwibergd1fe2812016-04-27 06:47:29 -07003722 std::unique_ptr<JsepSessionDescription> password_changed_offer(
deadbeef0ed85b22016-02-23 17:24:52 -08003723 CreateRemoteOffer(options, session_->remote_description()));
3724 SetIceUfragPwd(password_changed_offer.get(), "modified_ufrag",
3725 "modified_password12345");
3726 SetRemoteDescriptionWithoutError(password_changed_offer.release());
3727
kwibergd1fe2812016-04-27 06:47:29 -07003728 std::unique_ptr<SessionDescriptionInterface> updated_answer2(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003729 EXPECT_FALSE(IceUfragPwdEqual(updated_answer2->description(),
3730 session_->local_description()->description()));
3731 SetLocalDescriptionWithoutError(updated_answer2.release());
wu@webrtc.org91053e72013-08-10 07:18:04 +00003732}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003733
wu@webrtc.org91053e72013-08-10 07:18:04 +00003734// This test verifies that an answer contains old ufrag and password if an offer
3735// with old ufrag and password is received.
3736TEST_F(WebRtcSessionTest, TestCreateAnswerWithOldUfragAndPassword) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003737 Init();
wu@webrtc.org91053e72013-08-10 07:18:04 +00003738 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00003739 options.recv_video = true;
kwibergd1fe2812016-04-27 06:47:29 -07003740 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer(options));
wu@webrtc.org91053e72013-08-10 07:18:04 +00003741 SetRemoteDescriptionWithoutError(offer.release());
3742
deadbeefab9b2d12015-10-14 11:33:11 -07003743 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003744 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
wu@webrtc.org91053e72013-08-10 07:18:04 +00003745 SetLocalDescriptionWithoutError(answer.release());
3746
3747 // Receive an offer without changed ufrag or password.
kwibergd1fe2812016-04-27 06:47:29 -07003748 std::unique_ptr<JsepSessionDescription> updated_offer2(
wu@webrtc.org91053e72013-08-10 07:18:04 +00003749 CreateRemoteOffer(options, session_->remote_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003750 SetRemoteDescriptionWithoutError(updated_offer2.release());
3751
kwibergd1fe2812016-04-27 06:47:29 -07003752 std::unique_ptr<SessionDescriptionInterface> updated_answer2(CreateAnswer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003753
deadbeef0ed85b22016-02-23 17:24:52 -08003754 EXPECT_TRUE(IceUfragPwdEqual(updated_answer2->description(),
3755 session_->local_description()->description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003756
3757 SetLocalDescriptionWithoutError(updated_answer2.release());
3758}
3759
deadbeef0ed85b22016-02-23 17:24:52 -08003760// This test verifies that if an offer does an ICE restart on some, but not all
3761// media sections, the answer will change the ufrag/password in the correct
3762// media sections.
3763TEST_F(WebRtcSessionTest, TestCreateAnswerWithNewAndOldUfragAndPassword) {
3764 Init();
3765 cricket::MediaSessionOptions options;
3766 options.recv_video = true;
3767 options.recv_audio = true;
3768 options.bundle_enabled = false;
kwibergd1fe2812016-04-27 06:47:29 -07003769 std::unique_ptr<JsepSessionDescription> offer(CreateRemoteOffer(options));
deadbeef0ed85b22016-02-23 17:24:52 -08003770
3771 SetIceUfragPwd(offer.get(), cricket::MEDIA_TYPE_AUDIO, "aaaa",
3772 "aaaaaaaaaaaaaaaaaaaaaa");
3773 SetIceUfragPwd(offer.get(), cricket::MEDIA_TYPE_VIDEO, "bbbb",
3774 "bbbbbbbbbbbbbbbbbbbbbb");
3775 SetRemoteDescriptionWithoutError(offer.release());
3776
3777 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07003778 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003779 SetLocalDescriptionWithoutError(answer.release());
3780
3781 // Receive an offer with new ufrag and password, but only for the video media
3782 // section.
kwibergd1fe2812016-04-27 06:47:29 -07003783 std::unique_ptr<JsepSessionDescription> updated_offer(
deadbeef0ed85b22016-02-23 17:24:52 -08003784 CreateRemoteOffer(options, session_->remote_description()));
3785 SetIceUfragPwd(updated_offer.get(), cricket::MEDIA_TYPE_VIDEO, "cccc",
3786 "cccccccccccccccccccccc");
3787 SetRemoteDescriptionWithoutError(updated_offer.release());
3788
kwibergd1fe2812016-04-27 06:47:29 -07003789 std::unique_ptr<SessionDescriptionInterface> updated_answer(CreateAnswer());
deadbeef0ed85b22016-02-23 17:24:52 -08003790
3791 EXPECT_TRUE(IceUfragPwdEqual(updated_answer->description(),
3792 session_->local_description()->description(),
3793 cricket::MEDIA_TYPE_AUDIO));
3794
3795 EXPECT_FALSE(IceUfragPwdEqual(updated_answer->description(),
3796 session_->local_description()->description(),
3797 cricket::MEDIA_TYPE_VIDEO));
3798
3799 SetLocalDescriptionWithoutError(updated_answer.release());
3800}
3801
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003802TEST_F(WebRtcSessionTest, TestSessionContentError) {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003803 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07003804 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00003805 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003806 const std::string session_id_orig = offer->session_id();
3807 const std::string session_version_orig = offer->session_version();
3808 SetLocalDescriptionWithoutError(offer);
3809
3810 video_channel_ = media_engine_->GetVideoChannel(0);
3811 video_channel_->set_fail_set_send_codecs(true);
3812
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003813 SessionDescriptionInterface* answer =
3814 CreateRemoteAnswer(session_->local_description());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00003815 SetRemoteDescriptionAnswerExpectError("ERROR_CONTENT", answer);
deadbeefd59daf82015-10-14 15:02:44 -07003816
3817 // Test that after a content error, setting any description will
3818 // result in an error.
3819 video_channel_->set_fail_set_send_codecs(false);
3820 answer = CreateRemoteAnswer(session_->local_description());
3821 SetRemoteDescriptionExpectError("", "ERROR_CONTENT", answer);
3822 offer = CreateRemoteOffer();
3823 SetLocalDescriptionExpectError("", "ERROR_CONTENT", offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003824}
3825
3826// Runs the loopback call test with BUNDLE and STUN disabled.
3827TEST_F(WebRtcSessionTest, TestIceStatesBasic) {
3828 // Lets try with only UDP ports.
Peter Thatcher7cbd1882015-09-17 18:54:52 -07003829 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00003830 cricket::PORTALLOCATOR_DISABLE_STUN |
3831 cricket::PORTALLOCATOR_DISABLE_RELAY);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003832 TestLoopbackCall();
3833}
3834
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00003835TEST_F(WebRtcSessionTest, TestIceStatesBasicIPv6) {
Peter Thatcher7cbd1882015-09-17 18:54:52 -07003836 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00003837 cricket::PORTALLOCATOR_DISABLE_STUN |
3838 cricket::PORTALLOCATOR_ENABLE_IPV6 |
3839 cricket::PORTALLOCATOR_DISABLE_RELAY);
3840
3841 // best connection is IPv6 since it has higher network preference.
3842 LoopbackNetworkConfiguration config;
3843 config.test_ipv6_network_ = true;
3844 config.best_connection_after_initial_ice_converged_ =
3845 LoopbackNetworkConfiguration::ExpectedBestConnection(0, 1);
3846
3847 TestLoopbackCall(config);
3848}
3849
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00003850// Runs the loopback call test with BUNDLE and STUN enabled.
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00003851TEST_F(WebRtcSessionTest, TestIceStatesBundle) {
Peter Thatcher7cbd1882015-09-17 18:54:52 -07003852 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
3853 cricket::PORTALLOCATOR_DISABLE_RELAY);
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00003854 TestLoopbackCall();
3855}
3856
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003857TEST_F(WebRtcSessionTest, TestRtpDataChannel) {
htaa2a49d92016-03-04 02:51:39 -08003858 configuration_.enable_rtp_data_channel = true;
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00003859 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003860 SetLocalDescriptionWithDataChannel();
htaa2a49d92016-03-04 02:51:39 -08003861 ASSERT_TRUE(data_engine_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003862 EXPECT_EQ(cricket::DCT_RTP, data_engine_->last_channel_type());
3863}
3864
Henrik Boström87713d02015-08-25 09:53:21 +02003865TEST_P(WebRtcSessionTest, TestRtpDataChannelConstraintTakesPrecedence) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003866 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003867
htaa2a49d92016-03-04 02:51:39 -08003868 configuration_.enable_rtp_data_channel = true;
wu@webrtc.org97077a32013-10-25 21:18:33 +00003869 options_.disable_sctp_data_channels = false;
3870
Henrik Boström87713d02015-08-25 09:53:21 +02003871 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003872
3873 SetLocalDescriptionWithDataChannel();
3874 EXPECT_EQ(cricket::DCT_RTP, data_engine_->last_channel_type());
3875}
3876
Henrik Boström87713d02015-08-25 09:53:21 +02003877TEST_P(WebRtcSessionTest, TestCreateOfferWithSctpEnabledWithoutStreams) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003878 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
wu@webrtc.org967bfff2013-09-19 05:49:50 +00003879
Henrik Boström87713d02015-08-25 09:53:21 +02003880 InitWithDtls(GetParam());
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00003881
kwibergd1fe2812016-04-27 06:47:29 -07003882 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00003883 EXPECT_TRUE(offer->description()->GetContentByName("data") == NULL);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00003884 EXPECT_TRUE(offer->description()->GetTransportInfoByName("data") == NULL);
3885}
3886
Henrik Boström87713d02015-08-25 09:53:21 +02003887TEST_P(WebRtcSessionTest, TestCreateAnswerWithSctpInOfferAndNoStreams) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003888 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00003889 SetFactoryDtlsSrtp();
Henrik Boström87713d02015-08-25 09:53:21 +02003890 InitWithDtls(GetParam());
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00003891
3892 // Create remote offer with SCTP.
3893 cricket::MediaSessionOptions options;
3894 options.data_channel_type = cricket::DCT_SCTP;
3895 JsepSessionDescription* offer =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00003896 CreateRemoteOffer(options, cricket::SEC_DISABLED);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00003897 SetRemoteDescriptionWithoutError(offer);
3898
3899 // Verifies the answer contains SCTP.
kwibergd1fe2812016-04-27 06:47:29 -07003900 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00003901 EXPECT_TRUE(answer != NULL);
3902 EXPECT_TRUE(answer->description()->GetContentByName("data") != NULL);
3903 EXPECT_TRUE(answer->description()->GetTransportInfoByName("data") != NULL);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00003904}
3905
Henrik Boström87713d02015-08-25 09:53:21 +02003906TEST_P(WebRtcSessionTest, TestSctpDataChannelWithoutDtls) {
htaa2a49d92016-03-04 02:51:39 -08003907 configuration_.enable_dtls_srtp = rtc::Optional<bool>(false);
Henrik Boström87713d02015-08-25 09:53:21 +02003908 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003909
3910 SetLocalDescriptionWithDataChannel();
3911 EXPECT_EQ(cricket::DCT_NONE, data_engine_->last_channel_type());
3912}
3913
Henrik Boström87713d02015-08-25 09:53:21 +02003914TEST_P(WebRtcSessionTest, TestSctpDataChannelWithDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003915 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003916
Henrik Boström87713d02015-08-25 09:53:21 +02003917 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003918
3919 SetLocalDescriptionWithDataChannel();
3920 EXPECT_EQ(cricket::DCT_SCTP, data_engine_->last_channel_type());
3921}
wu@webrtc.org91053e72013-08-10 07:18:04 +00003922
Henrik Boström87713d02015-08-25 09:53:21 +02003923TEST_P(WebRtcSessionTest, TestDisableSctpDataChannels) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003924 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
wu@webrtc.org97077a32013-10-25 21:18:33 +00003925 options_.disable_sctp_data_channels = true;
Henrik Boström87713d02015-08-25 09:53:21 +02003926 InitWithDtls(GetParam());
wu@webrtc.org97077a32013-10-25 21:18:33 +00003927
3928 SetLocalDescriptionWithDataChannel();
3929 EXPECT_EQ(cricket::DCT_NONE, data_engine_->last_channel_type());
3930}
3931
Henrik Boström87713d02015-08-25 09:53:21 +02003932TEST_P(WebRtcSessionTest, TestSctpDataChannelSendPortParsing) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003933 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00003934 const int new_send_port = 9998;
3935 const int new_recv_port = 7775;
3936
Henrik Boström87713d02015-08-25 09:53:21 +02003937 InitWithDtls(GetParam());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00003938 SetFactoryDtlsSrtp();
3939
3940 // By default, don't actually add the codecs to desc_factory_; they don't
3941 // actually get serialized for SCTP in BuildMediaDescription(). Instead,
3942 // let the session description get parsed. That'll get the proper codecs
3943 // into the stream.
3944 cricket::MediaSessionOptions options;
3945 JsepSessionDescription* offer = CreateRemoteOfferWithSctpPort(
3946 "stream1", new_send_port, options);
3947
3948 // SetRemoteDescription will take the ownership of the offer.
3949 SetRemoteDescriptionWithoutError(offer);
3950
htaa2a49d92016-03-04 02:51:39 -08003951 SessionDescriptionInterface* answer =
3952 ChangeSDPSctpPort(new_recv_port, CreateAnswer());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00003953 ASSERT_TRUE(answer != NULL);
3954
3955 // Now set the local description, which'll take ownership of the answer.
3956 SetLocalDescriptionWithoutError(answer);
3957
3958 // TEST PLAN: Set the port number to something new, set it in the SDP,
3959 // and pass it all the way down.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00003960 EXPECT_EQ(cricket::DCT_SCTP, data_engine_->last_channel_type());
deadbeefab9b2d12015-10-14 11:33:11 -07003961 CreateDataChannel();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00003962
3963 cricket::FakeDataMediaChannel* ch = data_engine_->GetChannel(0);
3964 int portnum = -1;
3965 ASSERT_TRUE(ch != NULL);
3966 ASSERT_EQ(1UL, ch->send_codecs().size());
3967 EXPECT_EQ(cricket::kGoogleSctpDataCodecId, ch->send_codecs()[0].id);
Donald Curtisd4f769d2015-05-28 09:48:21 -07003968 EXPECT_EQ(0, strcmp(cricket::kGoogleSctpDataCodecName,
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00003969 ch->send_codecs()[0].name.c_str()));
3970 EXPECT_TRUE(ch->send_codecs()[0].GetParam(cricket::kCodecParamPort,
3971 &portnum));
3972 EXPECT_EQ(new_send_port, portnum);
3973
3974 ASSERT_EQ(1UL, ch->recv_codecs().size());
3975 EXPECT_EQ(cricket::kGoogleSctpDataCodecId, ch->recv_codecs()[0].id);
Donald Curtisd4f769d2015-05-28 09:48:21 -07003976 EXPECT_EQ(0, strcmp(cricket::kGoogleSctpDataCodecName,
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00003977 ch->recv_codecs()[0].name.c_str()));
3978 EXPECT_TRUE(ch->recv_codecs()[0].GetParam(cricket::kCodecParamPort,
3979 &portnum));
3980 EXPECT_EQ(new_recv_port, portnum);
3981}
3982
deadbeefab9b2d12015-10-14 11:33:11 -07003983// Verifies that when a session's DataChannel receives an OPEN message,
3984// WebRtcSession signals the DataChannel creation request with the expected
3985// config.
3986TEST_P(WebRtcSessionTest, TestSctpDataChannelOpenMessage) {
3987 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
3988
3989 InitWithDtls(GetParam());
3990
3991 SetLocalDescriptionWithDataChannel();
3992 EXPECT_EQ(cricket::DCT_SCTP, data_engine_->last_channel_type());
3993
3994 webrtc::DataChannelInit config;
3995 config.id = 1;
jbaucheec21bd2016-03-20 06:15:43 -07003996 rtc::CopyOnWriteBuffer payload;
deadbeefab9b2d12015-10-14 11:33:11 -07003997 webrtc::WriteDataChannelOpenMessage("a", config, &payload);
3998 cricket::ReceiveDataParams params;
3999 params.ssrc = config.id;
4000 params.type = cricket::DMT_CONTROL;
4001
4002 cricket::DataChannel* data_channel = session_->data_channel();
4003 data_channel->SignalDataReceived(data_channel, params, payload);
4004
4005 EXPECT_EQ("a", last_data_channel_label_);
4006 EXPECT_EQ(config.id, last_data_channel_config_.id);
4007 EXPECT_FALSE(last_data_channel_config_.negotiated);
4008 EXPECT_EQ(webrtc::InternalDataChannelInit::kAcker,
4009 last_data_channel_config_.open_handshake_role);
4010}
4011
4012TEST_P(WebRtcSessionTest, TestUsesProvidedCertificate) {
Henrik Boströmd8281982015-08-27 10:12:24 +02004013 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Henrik Boströmd79599d2016-06-01 13:58:50 +02004014 FakeRTCCertificateGenerator::GenerateCertificate();
Henrik Boströmd8281982015-08-27 10:12:24 +02004015
htaa2a49d92016-03-04 02:51:39 -08004016 configuration_.certificates.push_back(certificate);
4017 Init();
Henrik Boströmd8281982015-08-27 10:12:24 +02004018 EXPECT_TRUE_WAIT(!session_->waiting_for_certificate_for_testing(), 1000);
4019
4020 EXPECT_EQ(session_->certificate_for_testing(), certificate);
4021}
wu@webrtc.org91053e72013-08-10 07:18:04 +00004022
Henrik Boström87713d02015-08-25 09:53:21 +02004023// Verifies that CreateOffer succeeds when CreateOffer is called before async
4024// identity generation is finished (even if a certificate is provided this is
4025// an async op).
4026TEST_P(WebRtcSessionTest, TestCreateOfferBeforeIdentityRequestReturnSuccess) {
4027 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
4028 InitWithDtls(GetParam());
4029
Henrik Boströmd8281982015-08-27 10:12:24 +02004030 EXPECT_TRUE(session_->waiting_for_certificate_for_testing());
deadbeefab9b2d12015-10-14 11:33:11 -07004031 SendAudioVideoStream1();
kwibergd1fe2812016-04-27 06:47:29 -07004032 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00004033
wu@webrtc.org91053e72013-08-10 07:18:04 +00004034 EXPECT_TRUE(offer != NULL);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00004035 VerifyNoCryptoParams(offer->description(), true);
4036 VerifyFingerprintStatus(offer->description(), true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004037}
4038
4039// Verifies that CreateAnswer succeeds when CreateOffer is called before async
Henrik Boström87713d02015-08-25 09:53:21 +02004040// identity generation is finished (even if a certificate is provided this is
4041// an async op).
4042TEST_P(WebRtcSessionTest, TestCreateAnswerBeforeIdentityRequestReturnSuccess) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004043 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004044 InitWithDtls(GetParam());
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00004045 SetFactoryDtlsSrtp();
wu@webrtc.org91053e72013-08-10 07:18:04 +00004046
4047 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00004048 options.recv_video = true;
kwibergd1fe2812016-04-27 06:47:29 -07004049 std::unique_ptr<JsepSessionDescription> offer(
4050 CreateRemoteOffer(options, cricket::SEC_DISABLED));
wu@webrtc.org91053e72013-08-10 07:18:04 +00004051 ASSERT_TRUE(offer.get() != NULL);
4052 SetRemoteDescriptionWithoutError(offer.release());
4053
kwibergd1fe2812016-04-27 06:47:29 -07004054 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
wu@webrtc.org91053e72013-08-10 07:18:04 +00004055 EXPECT_TRUE(answer != NULL);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00004056 VerifyNoCryptoParams(answer->description(), true);
4057 VerifyFingerprintStatus(answer->description(), true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004058}
4059
4060// Verifies that CreateOffer succeeds when CreateOffer is called after async
Henrik Boström87713d02015-08-25 09:53:21 +02004061// identity generation is finished (even if a certificate is provided this is
4062// an async op).
4063TEST_P(WebRtcSessionTest, TestCreateOfferAfterIdentityRequestReturnSuccess) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004064 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004065 InitWithDtls(GetParam());
wu@webrtc.org91053e72013-08-10 07:18:04 +00004066
Henrik Boströmd8281982015-08-27 10:12:24 +02004067 EXPECT_TRUE_WAIT(!session_->waiting_for_certificate_for_testing(), 1000);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00004068
kwibergd1fe2812016-04-27 06:47:29 -07004069 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
wu@webrtc.org91053e72013-08-10 07:18:04 +00004070 EXPECT_TRUE(offer != NULL);
4071}
4072
4073// Verifies that CreateOffer fails when CreateOffer is called after async
4074// identity generation fails.
4075TEST_F(WebRtcSessionTest, TestCreateOfferAfterIdentityRequestReturnFailure) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004076 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004077 InitWithDtlsIdentityGenFail();
wu@webrtc.org91053e72013-08-10 07:18:04 +00004078
Henrik Boströmd8281982015-08-27 10:12:24 +02004079 EXPECT_TRUE_WAIT(!session_->waiting_for_certificate_for_testing(), 1000);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00004080
kwibergd1fe2812016-04-27 06:47:29 -07004081 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
wu@webrtc.org91053e72013-08-10 07:18:04 +00004082 EXPECT_TRUE(offer == NULL);
4083}
4084
4085// Verifies that CreateOffer succeeds when Multiple CreateOffer calls are made
4086// before async identity generation is finished.
Henrik Boström87713d02015-08-25 09:53:21 +02004087TEST_P(WebRtcSessionTest,
wu@webrtc.org91053e72013-08-10 07:18:04 +00004088 TestMultipleCreateOfferBeforeIdentityRequestReturnSuccess) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004089 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
deadbeefcbecd352015-09-23 11:50:27 -07004090 VerifyMultipleAsyncCreateDescription(GetParam(),
4091 CreateSessionDescriptionRequest::kOffer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004092}
4093
4094// Verifies that CreateOffer fails when Multiple CreateOffer calls are made
4095// before async identity generation fails.
4096TEST_F(WebRtcSessionTest,
4097 TestMultipleCreateOfferBeforeIdentityRequestReturnFailure) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004098 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004099 VerifyMultipleAsyncCreateDescriptionIdentityGenFailure(
4100 CreateSessionDescriptionRequest::kOffer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004101}
4102
4103// Verifies that CreateAnswer succeeds when Multiple CreateAnswer calls are made
4104// before async identity generation is finished.
Henrik Boström87713d02015-08-25 09:53:21 +02004105TEST_P(WebRtcSessionTest,
wu@webrtc.org91053e72013-08-10 07:18:04 +00004106 TestMultipleCreateAnswerBeforeIdentityRequestReturnSuccess) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004107 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004108 VerifyMultipleAsyncCreateDescription(
Henrik Boström87713d02015-08-25 09:53:21 +02004109 GetParam(), CreateSessionDescriptionRequest::kAnswer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004110}
4111
4112// Verifies that CreateAnswer fails when Multiple CreateAnswer calls are made
4113// before async identity generation fails.
4114TEST_F(WebRtcSessionTest,
4115 TestMultipleCreateAnswerBeforeIdentityRequestReturnFailure) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004116 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004117 VerifyMultipleAsyncCreateDescriptionIdentityGenFailure(
4118 CreateSessionDescriptionRequest::kAnswer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00004119}
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00004120
4121// Verifies that setRemoteDescription fails when DTLS is disabled and the remote
4122// offer has no SDES crypto but only DTLS fingerprint.
4123TEST_F(WebRtcSessionTest, TestSetRemoteOfferFailIfDtlsDisabledAndNoCrypto) {
4124 // Init without DTLS.
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00004125 Init();
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00004126 // Create a remote offer with secured transport disabled.
4127 cricket::MediaSessionOptions options;
4128 JsepSessionDescription* offer(CreateRemoteOffer(
4129 options, cricket::SEC_DISABLED));
4130 // Adds a DTLS fingerprint to the remote offer.
4131 cricket::SessionDescription* sdp = offer->description();
4132 TransportInfo* audio = sdp->GetTransportInfoByName("audio");
4133 ASSERT_TRUE(audio != NULL);
4134 ASSERT_TRUE(audio->description.identity_fingerprint.get() == NULL);
4135 audio->description.identity_fingerprint.reset(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004136 rtc::SSLFingerprint::CreateFromRfc4572(
4137 rtc::DIGEST_SHA_256, kFakeDtlsFingerprint));
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00004138 SetRemoteDescriptionOfferExpectError(kSdpWithoutSdesCrypto,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00004139 offer);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00004140}
4141
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00004142TEST_F(WebRtcSessionTest, TestCombinedAudioVideoBweConstraint) {
htaa2a49d92016-03-04 02:51:39 -08004143 configuration_.combined_audio_video_bwe = rtc::Optional<bool>(true);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00004144 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07004145 SendAudioVideoStream1();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00004146 SessionDescriptionInterface* offer = CreateOffer();
4147
4148 SetLocalDescriptionWithoutError(offer);
4149
4150 voice_channel_ = media_engine_->GetVoiceChannel(0);
4151
4152 ASSERT_TRUE(voice_channel_ != NULL);
solenberg66f43392015-09-09 01:36:22 -07004153 const cricket::AudioOptions& audio_options = voice_channel_->options();
Karl Wibergbe579832015-11-10 22:34:18 +01004154 EXPECT_EQ(rtc::Optional<bool>(true), audio_options.combined_audio_video_bwe);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00004155}
4156
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004157// Tests that we can renegotiate new media content with ICE candidates in the
4158// new remote SDP.
Henrik Boström87713d02015-08-25 09:53:21 +02004159TEST_P(WebRtcSessionTest, TestRenegotiateNewMediaWithCandidatesInSdp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004160 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004161 InitWithDtls(GetParam());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004162 SetFactoryDtlsSrtp();
4163
deadbeefab9b2d12015-10-14 11:33:11 -07004164 SendAudioOnlyStream2();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00004165 SessionDescriptionInterface* offer = CreateOffer();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004166 SetLocalDescriptionWithoutError(offer);
4167
4168 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
4169 SetRemoteDescriptionWithoutError(answer);
4170
4171 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00004172 options.recv_video = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004173 offer = CreateRemoteOffer(options, cricket::SEC_DISABLED);
4174
4175 cricket::Candidate candidate1;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004176 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 5000));
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004177 candidate1.set_component(1);
4178 JsepIceCandidate ice_candidate(kMediaContentName1, kMediaContentIndex1,
4179 candidate1);
4180 EXPECT_TRUE(offer->AddCandidate(&ice_candidate));
4181 SetRemoteDescriptionWithoutError(offer);
4182
htaa2a49d92016-03-04 02:51:39 -08004183 answer = CreateAnswer();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004184 SetLocalDescriptionWithoutError(answer);
4185}
4186
4187// Tests that we can renegotiate new media content with ICE candidates separated
4188// from the remote SDP.
Henrik Boström87713d02015-08-25 09:53:21 +02004189TEST_P(WebRtcSessionTest, TestRenegotiateNewMediaWithCandidatesSeparated) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004190 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
Henrik Boström87713d02015-08-25 09:53:21 +02004191 InitWithDtls(GetParam());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004192 SetFactoryDtlsSrtp();
4193
deadbeefab9b2d12015-10-14 11:33:11 -07004194 SendAudioOnlyStream2();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00004195 SessionDescriptionInterface* offer = CreateOffer();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004196 SetLocalDescriptionWithoutError(offer);
4197
4198 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
4199 SetRemoteDescriptionWithoutError(answer);
4200
4201 cricket::MediaSessionOptions options;
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00004202 options.recv_video = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004203 offer = CreateRemoteOffer(options, cricket::SEC_DISABLED);
4204 SetRemoteDescriptionWithoutError(offer);
4205
4206 cricket::Candidate candidate1;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00004207 candidate1.set_address(rtc::SocketAddress("1.1.1.1", 5000));
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004208 candidate1.set_component(1);
4209 JsepIceCandidate ice_candidate(kMediaContentName1, kMediaContentIndex1,
4210 candidate1);
4211 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate));
4212
htaa2a49d92016-03-04 02:51:39 -08004213 answer = CreateAnswer();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004214 SetLocalDescriptionWithoutError(answer);
4215}
honghaiz7f777492016-02-02 21:54:01 -08004216
zhihuang9763d562016-08-05 11:14:50 -07004217#ifdef HAVE_QUIC
4218TEST_P(WebRtcSessionTest, TestNegotiateQuic) {
4219 configuration_.enable_quic = true;
4220 InitWithDtls(GetParam());
4221 EXPECT_TRUE(session_->data_channel_type() == cricket::DCT_QUIC);
4222 SessionDescriptionInterface* offer = CreateOffer();
4223 ASSERT_TRUE(offer);
4224 ASSERT_TRUE(offer->description());
4225 SetLocalDescriptionWithoutError(offer);
4226 cricket::MediaSessionOptions options;
4227 options.recv_audio = true;
4228 options.recv_video = true;
4229 SessionDescriptionInterface* answer =
4230 CreateRemoteAnswer(offer, options, cricket::SEC_DISABLED);
4231 ASSERT_TRUE(answer);
4232 ASSERT_TRUE(answer->description());
4233 SetRemoteDescriptionWithoutError(answer);
4234}
4235#endif // HAVE_QUIC
4236
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00004237// Tests that RTX codec is removed from the answer when it isn't supported
4238// by local side.
zhihuang3a334652016-05-05 18:37:49 -07004239TEST_F(WebRtcSessionTest, TestRtxRemovedByCreateAnswer) {
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00004240 Init();
deadbeefab9b2d12015-10-14 11:33:11 -07004241 SendAudioVideoStream1();
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00004242 std::string offer_sdp(kSdpWithRtx);
4243
4244 SessionDescriptionInterface* offer =
4245 CreateSessionDescription(JsepSessionDescription::kOffer, offer_sdp, NULL);
4246 EXPECT_TRUE(offer->ToString(&offer_sdp));
4247
4248 // Offer SDP contains the RTX codec.
zhihuang3a334652016-05-05 18:37:49 -07004249 EXPECT_TRUE(ContainsVideoCodecWithName(offer, "rtx"));
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00004250 SetRemoteDescriptionWithoutError(offer);
4251
htaa2a49d92016-03-04 02:51:39 -08004252 SessionDescriptionInterface* answer = CreateAnswer();
zhihuang3a334652016-05-05 18:37:49 -07004253 // Answer SDP does not contain the RTX codec.
4254 EXPECT_FALSE(ContainsVideoCodecWithName(answer, "rtx"));
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00004255 SetLocalDescriptionWithoutError(answer);
4256}
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00004257
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004258// This verifies that the voice channel after bundle has both options from video
4259// and voice channels.
4260TEST_F(WebRtcSessionTest, TestSetSocketOptionBeforeBundle) {
4261 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyBalanced);
deadbeefab9b2d12015-10-14 11:33:11 -07004262 SendAudioVideoStream1();
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004263
4264 PeerConnectionInterface::RTCOfferAnswerOptions options;
4265 options.use_rtp_mux = true;
4266
4267 SessionDescriptionInterface* offer = CreateOffer(options);
4268 SetLocalDescriptionWithoutError(offer);
4269
4270 session_->video_channel()->SetOption(cricket::BaseChannel::ST_RTP,
4271 rtc::Socket::Option::OPT_SNDBUF, 4000);
4272
4273 session_->voice_channel()->SetOption(cricket::BaseChannel::ST_RTP,
4274 rtc::Socket::Option::OPT_RCVBUF, 8000);
4275
4276 int option_val;
deadbeefcbecd352015-09-23 11:50:27 -07004277 EXPECT_TRUE(session_->video_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004278 rtc::Socket::Option::OPT_SNDBUF, &option_val));
4279 EXPECT_EQ(4000, option_val);
deadbeefcbecd352015-09-23 11:50:27 -07004280 EXPECT_FALSE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004281 rtc::Socket::Option::OPT_SNDBUF, &option_val));
4282
deadbeefcbecd352015-09-23 11:50:27 -07004283 EXPECT_TRUE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004284 rtc::Socket::Option::OPT_RCVBUF, &option_val));
4285 EXPECT_EQ(8000, option_val);
deadbeefcbecd352015-09-23 11:50:27 -07004286 EXPECT_FALSE(session_->video_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004287 rtc::Socket::Option::OPT_RCVBUF, &option_val));
4288
deadbeefcbecd352015-09-23 11:50:27 -07004289 EXPECT_NE(session_->voice_rtp_transport_channel(),
4290 session_->video_rtp_transport_channel());
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004291
deadbeefab9b2d12015-10-14 11:33:11 -07004292 SendAudioVideoStream2();
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004293 SessionDescriptionInterface* answer =
4294 CreateRemoteAnswer(session_->local_description());
4295 SetRemoteDescriptionWithoutError(answer);
4296
deadbeefcbecd352015-09-23 11:50:27 -07004297 EXPECT_TRUE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004298 rtc::Socket::Option::OPT_SNDBUF, &option_val));
4299 EXPECT_EQ(4000, option_val);
4300
deadbeefcbecd352015-09-23 11:50:27 -07004301 EXPECT_TRUE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00004302 rtc::Socket::Option::OPT_RCVBUF, &option_val));
4303 EXPECT_EQ(8000, option_val);
4304}
4305
tommi0f620f42015-07-09 03:25:02 -07004306// Test creating a session, request multiple offers, destroy the session
4307// and make sure we got success/failure callbacks for all of the requests.
4308// Background: crbug.com/507307
4309TEST_F(WebRtcSessionTest, CreateOffersAndShutdown) {
4310 Init();
4311
4312 rtc::scoped_refptr<WebRtcSessionCreateSDPObserverForTest> observers[100];
4313 PeerConnectionInterface::RTCOfferAnswerOptions options;
4314 options.offer_to_receive_audio =
4315 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
deadbeefab9b2d12015-10-14 11:33:11 -07004316 cricket::MediaSessionOptions session_options;
4317 session_options.recv_audio = true;
tommi0f620f42015-07-09 03:25:02 -07004318
4319 for (auto& o : observers) {
4320 o = new WebRtcSessionCreateSDPObserverForTest();
deadbeefab9b2d12015-10-14 11:33:11 -07004321 session_->CreateOffer(o, options, session_options);
tommi0f620f42015-07-09 03:25:02 -07004322 }
4323
4324 session_.reset();
4325
tommi0f620f42015-07-09 03:25:02 -07004326 for (auto& o : observers) {
4327 // We expect to have received a notification now even if the session was
4328 // terminated. The offer creation may or may not have succeeded, but we
4329 // must have received a notification which, so the only invalid state
4330 // is kInit.
4331 EXPECT_NE(WebRtcSessionCreateSDPObserverForTest::kInit, o->state());
4332 }
4333}
4334
stefanc1aeaf02015-10-15 07:26:07 -07004335TEST_F(WebRtcSessionTest, TestPacketOptionsAndOnPacketSent) {
4336 TestPacketOptions();
4337}
4338
deadbeef057ecf02016-01-20 14:30:43 -08004339// Make sure the signal from "GetOnDestroyedSignal()" fires when the session
4340// is destroyed.
4341TEST_F(WebRtcSessionTest, TestOnDestroyedSignal) {
4342 Init();
4343 session_.reset();
4344 EXPECT_TRUE(session_destroyed_);
4345}
4346
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004347// TODO(bemasc): Add a TestIceStatesBundle with BUNDLE enabled. That test
4348// currently fails because upon disconnection and reconnection OnIceComplete is
4349// called more than once without returning to IceGatheringGathering.
Henrik Boström87713d02015-08-25 09:53:21 +02004350
deadbeefcbecd352015-09-23 11:50:27 -07004351INSTANTIATE_TEST_CASE_P(WebRtcSessionTests,
4352 WebRtcSessionTest,
4353 testing::Values(ALREADY_GENERATED,
4354 DTLS_IDENTITY_STORE));