blob: 493294fcb197379093a54c43155a9932c853058a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/audiotrack.h"
29#include "talk/app/webrtc/jsepicecandidate.h"
30#include "talk/app/webrtc/jsepsessiondescription.h"
31#include "talk/app/webrtc/mediastreamsignaling.h"
32#include "talk/app/webrtc/streamcollection.h"
33#include "talk/app/webrtc/videotrack.h"
34#include "talk/app/webrtc/test/fakeconstraints.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000035#include "talk/app/webrtc/test/fakedtlsidentityservice.h"
36#include "talk/app/webrtc/test/fakemediastreamsignaling.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/app/webrtc/webrtcsession.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000038#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/base/fakenetwork.h"
40#include "talk/base/firewallsocketserver.h"
41#include "talk/base/gunit.h"
42#include "talk/base/logging.h"
43#include "talk/base/network.h"
44#include "talk/base/physicalsocketserver.h"
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000045#include "talk/base/ssladapter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046#include "talk/base/sslstreamadapter.h"
47#include "talk/base/stringutils.h"
48#include "talk/base/thread.h"
49#include "talk/base/virtualsocketserver.h"
50#include "talk/media/base/fakemediaengine.h"
51#include "talk/media/base/fakevideorenderer.h"
52#include "talk/media/base/mediachannel.h"
53#include "talk/media/devices/fakedevicemanager.h"
54#include "talk/p2p/base/stunserver.h"
55#include "talk/p2p/base/teststunserver.h"
56#include "talk/p2p/client/basicportallocator.h"
57#include "talk/session/media/channelmanager.h"
58#include "talk/session/media/mediasession.h"
59
60#define MAYBE_SKIP_TEST(feature) \
61 if (!(feature())) { \
62 LOG(LS_INFO) << "Feature disabled... skipping"; \
63 return; \
64 }
65
66using cricket::BaseSession;
67using cricket::DF_PLAY;
68using cricket::DF_SEND;
69using cricket::FakeVoiceMediaChannel;
70using cricket::NS_GINGLE_P2P;
71using cricket::NS_JINGLE_ICE_UDP;
72using cricket::TransportInfo;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using talk_base::SocketAddress;
74using talk_base::scoped_ptr;
75using webrtc::CreateSessionDescription;
wu@webrtc.org91053e72013-08-10 07:18:04 +000076using webrtc::CreateSessionDescriptionObserver;
77using webrtc::CreateSessionDescriptionRequest;
78using webrtc::DTLSIdentityRequestObserver;
79using webrtc::DTLSIdentityServiceInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080using webrtc::FakeConstraints;
81using webrtc::IceCandidateCollection;
82using webrtc::JsepIceCandidate;
83using webrtc::JsepSessionDescription;
84using webrtc::PeerConnectionInterface;
85using webrtc::SessionDescriptionInterface;
86using webrtc::StreamCollection;
wu@webrtc.org91053e72013-08-10 07:18:04 +000087using webrtc::WebRtcSession;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088using webrtc::kMlineMismatch;
89using webrtc::kSdpWithoutCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000090using webrtc::kSdpWithoutSdesAndDtlsDisabled;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000091using webrtc::kSdpWithoutIceUfragPwd;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092using webrtc::kSessionError;
93using webrtc::kSetLocalSdpFailed;
94using webrtc::kSetRemoteSdpFailed;
95using webrtc::kPushDownAnswerTDFailed;
96using webrtc::kPushDownPranswerTDFailed;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000097using webrtc::kBundleWithoutRtcpMux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098
99static const SocketAddress kClientAddr1("11.11.11.11", 0);
100static const SocketAddress kClientAddr2("22.22.22.22", 0);
101static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
102
103static const char kSessionVersion[] = "1";
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105// Media index of candidates belonging to the first media content.
106static const int kMediaContentIndex0 = 0;
107static const char kMediaContentName0[] = "audio";
108
109// Media index of candidates belonging to the second media content.
110static const int kMediaContentIndex1 = 1;
111static const char kMediaContentName1[] = "video";
112
113static const int kIceCandidatesTimeout = 10000;
114
115static const cricket::AudioCodec
116 kTelephoneEventCodec(106, "telephone-event", 8000, 0, 1, 0);
117static const cricket::AudioCodec kCNCodec1(102, "CN", 8000, 0, 1, 0);
118static const cricket::AudioCodec kCNCodec2(103, "CN", 16000, 0, 1, 0);
119
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000120static const char kFakeDtlsFingerprint[] =
121 "BB:CD:72:F7:2F:D0:BA:43:F3:68:B1:0C:23:72:B6:4A:"
122 "0F:DE:34:06:BC:E0:FE:01:BC:73:C8:6D:F4:65:D5:24";
123
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124// Add some extra |newlines| to the |message| after |line|.
125static void InjectAfter(const std::string& line,
126 const std::string& newlines,
127 std::string* message) {
128 const std::string tmp = line + newlines;
129 talk_base::replace_substrs(line.c_str(), line.length(),
130 tmp.c_str(), tmp.length(), message);
131}
132
133class MockIceObserver : public webrtc::IceObserver {
134 public:
135 MockIceObserver()
136 : oncandidatesready_(false),
137 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
138 ice_gathering_state_(PeerConnectionInterface::kIceGatheringNew) {
139 }
140
141 virtual void OnIceConnectionChange(
142 PeerConnectionInterface::IceConnectionState new_state) {
143 ice_connection_state_ = new_state;
144 }
145 virtual void OnIceGatheringChange(
146 PeerConnectionInterface::IceGatheringState new_state) {
147 // We can never transition back to "new".
148 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, new_state);
149 ice_gathering_state_ = new_state;
150
151 // oncandidatesready_ really means "ICE gathering is complete".
152 // This if statement ensures that this value remains correct when we
153 // transition from kIceGatheringComplete to kIceGatheringGathering.
154 if (new_state == PeerConnectionInterface::kIceGatheringGathering) {
155 oncandidatesready_ = false;
156 }
157 }
158
159 // Found a new candidate.
160 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
161 if (candidate->sdp_mline_index() == kMediaContentIndex0) {
162 mline_0_candidates_.push_back(candidate->candidate());
163 } else if (candidate->sdp_mline_index() == kMediaContentIndex1) {
164 mline_1_candidates_.push_back(candidate->candidate());
165 }
166 // The ICE gathering state should always be Gathering when a candidate is
167 // received (or possibly Completed in the case of the final candidate).
168 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, ice_gathering_state_);
169 }
170
171 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
172 virtual void OnIceComplete() {
173 EXPECT_FALSE(oncandidatesready_);
174 oncandidatesready_ = true;
175
176 // OnIceGatheringChange(IceGatheringCompleted) and OnIceComplete() should
177 // be called approximately simultaneously. For ease of testing, this
178 // check additionally requires that they be called in the above order.
179 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
180 ice_gathering_state_);
181 }
182
183 bool oncandidatesready_;
184 std::vector<cricket::Candidate> mline_0_candidates_;
185 std::vector<cricket::Candidate> mline_1_candidates_;
186 PeerConnectionInterface::IceConnectionState ice_connection_state_;
187 PeerConnectionInterface::IceGatheringState ice_gathering_state_;
188};
189
190class WebRtcSessionForTest : public webrtc::WebRtcSession {
191 public:
192 WebRtcSessionForTest(cricket::ChannelManager* cmgr,
193 talk_base::Thread* signaling_thread,
194 talk_base::Thread* worker_thread,
195 cricket::PortAllocator* port_allocator,
196 webrtc::IceObserver* ice_observer,
197 webrtc::MediaStreamSignaling* mediastream_signaling)
198 : WebRtcSession(cmgr, signaling_thread, worker_thread, port_allocator,
199 mediastream_signaling) {
200 RegisterIceObserver(ice_observer);
201 }
202 virtual ~WebRtcSessionForTest() {}
203
204 using cricket::BaseSession::GetTransportProxy;
205 using webrtc::WebRtcSession::SetAudioPlayout;
206 using webrtc::WebRtcSession::SetAudioSend;
207 using webrtc::WebRtcSession::SetCaptureDevice;
208 using webrtc::WebRtcSession::SetVideoPlayout;
209 using webrtc::WebRtcSession::SetVideoSend;
210};
211
wu@webrtc.org91053e72013-08-10 07:18:04 +0000212class WebRtcSessionCreateSDPObserverForTest
213 : public talk_base::RefCountedObject<CreateSessionDescriptionObserver> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 public:
wu@webrtc.org91053e72013-08-10 07:18:04 +0000215 enum State {
216 kInit,
217 kFailed,
218 kSucceeded,
219 };
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000220 WebRtcSessionCreateSDPObserverForTest() : state_(kInit) {}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000221
222 // CreateSessionDescriptionObserver implementation.
223 virtual void OnSuccess(SessionDescriptionInterface* desc) {
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000224 description_.reset(desc);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000225 state_ = kSucceeded;
226 }
227 virtual void OnFailure(const std::string& error) {
228 state_ = kFailed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 }
230
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000231 SessionDescriptionInterface* description() { return description_.get(); }
232
233 SessionDescriptionInterface* ReleaseDescription() {
234 return description_.release();
235 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236
wu@webrtc.org91053e72013-08-10 07:18:04 +0000237 State state() const { return state_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238
wu@webrtc.org91053e72013-08-10 07:18:04 +0000239 protected:
240 ~WebRtcSessionCreateSDPObserverForTest() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241
242 private:
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000243 talk_base::scoped_ptr<SessionDescriptionInterface> description_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000244 State state_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245};
246
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000247class FakeAudioRenderer : public cricket::AudioRenderer {
248 public:
249 FakeAudioRenderer() : channel_id_(-1) {}
250
251 virtual void AddChannel(int channel_id) OVERRIDE {
252 ASSERT(channel_id_ == -1);
253 channel_id_ = channel_id;
254 }
255 virtual void RemoveChannel(int channel_id) OVERRIDE {
256 ASSERT(channel_id == channel_id_);
257 channel_id_ = -1;
258 }
259
260 int channel_id() const { return channel_id_; }
261 private:
262 int channel_id_;
263};
264
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265class WebRtcSessionTest : public testing::Test {
266 protected:
267 // TODO Investigate why ChannelManager crashes, if it's created
268 // after stun_server.
269 WebRtcSessionTest()
270 : media_engine_(new cricket::FakeMediaEngine()),
271 data_engine_(new cricket::FakeDataEngine()),
272 device_manager_(new cricket::FakeDeviceManager()),
273 channel_manager_(new cricket::ChannelManager(
274 media_engine_, data_engine_, device_manager_,
275 new cricket::CaptureManager(), talk_base::Thread::Current())),
276 tdesc_factory_(new cricket::TransportDescriptionFactory()),
277 desc_factory_(new cricket::MediaSessionDescriptionFactory(
278 channel_manager_.get(), tdesc_factory_.get())),
279 pss_(new talk_base::PhysicalSocketServer),
280 vss_(new talk_base::VirtualSocketServer(pss_.get())),
281 fss_(new talk_base::FirewallSocketServer(vss_.get())),
282 ss_scope_(fss_.get()),
283 stun_server_(talk_base::Thread::Current(), kStunAddr),
284 allocator_(&network_manager_, kStunAddr,
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000285 SocketAddress(), SocketAddress(), SocketAddress()),
286 mediastream_signaling_(channel_manager_.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 tdesc_factory_->set_protocol(cricket::ICEPROTO_HYBRID);
288 allocator_.set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
289 cricket::PORTALLOCATOR_DISABLE_RELAY |
290 cricket::PORTALLOCATOR_ENABLE_BUNDLE);
291 EXPECT_TRUE(channel_manager_->Init());
292 desc_factory_->set_add_legacy_streams(false);
293 }
294
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000295 static void SetUpTestCase() {
296 talk_base::InitializeSSL();
297 }
298
299 static void TearDownTestCase() {
300 talk_base::CleanupSSL();
301 }
302
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 void AddInterface(const SocketAddress& addr) {
304 network_manager_.AddInterface(addr);
305 }
306
wu@webrtc.org91053e72013-08-10 07:18:04 +0000307 void Init(DTLSIdentityServiceInterface* identity_service) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 ASSERT_TRUE(session_.get() == NULL);
309 session_.reset(new WebRtcSessionForTest(
310 channel_manager_.get(), talk_base::Thread::Current(),
311 talk_base::Thread::Current(), &allocator_,
312 &observer_,
313 &mediastream_signaling_));
314
315 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
316 observer_.ice_connection_state_);
317 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
318 observer_.ice_gathering_state_);
319
wu@webrtc.org91053e72013-08-10 07:18:04 +0000320 EXPECT_TRUE(session_->Initialize(constraints_.get(), identity_service));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 }
322
323 void InitWithDtmfCodec() {
324 // Add kTelephoneEventCodec for dtmf test.
325 std::vector<cricket::AudioCodec> codecs;
326 codecs.push_back(kTelephoneEventCodec);
327 media_engine_->SetAudioCodecs(codecs);
328 desc_factory_->set_audio_codecs(codecs);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000329 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 }
331
henrike@webrtc.org7666db72013-08-22 14:45:42 +0000332 void InitWithDtls(bool identity_request_should_fail = false) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000333 FakeIdentityService* identity_service = new FakeIdentityService();
334 identity_service->set_should_fail(identity_request_should_fail);
335 Init(identity_service);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 }
337
338 // Creates a local offer and applies it. Starts ice.
339 // Call mediastream_signaling_.UseOptionsWithStreamX() before this function
340 // to decide which streams to create.
341 void InitiateCall() {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000342 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 SetLocalDescriptionWithoutError(offer);
344 EXPECT_TRUE_WAIT(PeerConnectionInterface::kIceGatheringNew !=
345 observer_.ice_gathering_state_,
346 kIceCandidatesTimeout);
347 }
348
wu@webrtc.org91053e72013-08-10 07:18:04 +0000349 SessionDescriptionInterface* CreateOffer(
350 const webrtc::MediaConstraintsInterface* constraints) {
351 talk_base::scoped_refptr<WebRtcSessionCreateSDPObserverForTest>
352 observer = new WebRtcSessionCreateSDPObserverForTest();
353 session_->CreateOffer(observer, constraints);
354 EXPECT_TRUE_WAIT(
355 observer->state() != WebRtcSessionCreateSDPObserverForTest::kInit,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000356 2000);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000357 return observer->ReleaseDescription();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000358 }
359
360 SessionDescriptionInterface* CreateAnswer(
361 const webrtc::MediaConstraintsInterface* constraints) {
362 talk_base::scoped_refptr<WebRtcSessionCreateSDPObserverForTest> observer
363 = new WebRtcSessionCreateSDPObserverForTest();
364 session_->CreateAnswer(observer, constraints);
365 EXPECT_TRUE_WAIT(
366 observer->state() != WebRtcSessionCreateSDPObserverForTest::kInit,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000367 2000);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000368 return observer->ReleaseDescription();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000369 }
370
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 bool ChannelsExist() {
372 return (session_->voice_channel() != NULL &&
373 session_->video_channel() != NULL);
374 }
375
376 void CheckTransportChannels() {
377 EXPECT_TRUE(session_->GetChannel(cricket::CN_AUDIO, 1) != NULL);
378 EXPECT_TRUE(session_->GetChannel(cricket::CN_AUDIO, 2) != NULL);
379 EXPECT_TRUE(session_->GetChannel(cricket::CN_VIDEO, 1) != NULL);
380 EXPECT_TRUE(session_->GetChannel(cricket::CN_VIDEO, 2) != NULL);
381 }
382
383 void VerifyCryptoParams(const cricket::SessionDescription* sdp) {
384 ASSERT_TRUE(session_.get() != NULL);
385 const cricket::ContentInfo* content = cricket::GetFirstAudioContent(sdp);
386 ASSERT_TRUE(content != NULL);
387 const cricket::AudioContentDescription* audio_content =
388 static_cast<const cricket::AudioContentDescription*>(
389 content->description);
390 ASSERT_TRUE(audio_content != NULL);
391 ASSERT_EQ(1U, audio_content->cryptos().size());
392 ASSERT_EQ(47U, audio_content->cryptos()[0].key_params.size());
393 ASSERT_EQ("AES_CM_128_HMAC_SHA1_80",
394 audio_content->cryptos()[0].cipher_suite);
395 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
396 audio_content->protocol());
397
398 content = cricket::GetFirstVideoContent(sdp);
399 ASSERT_TRUE(content != NULL);
400 const cricket::VideoContentDescription* video_content =
401 static_cast<const cricket::VideoContentDescription*>(
402 content->description);
403 ASSERT_TRUE(video_content != NULL);
404 ASSERT_EQ(1U, video_content->cryptos().size());
405 ASSERT_EQ("AES_CM_128_HMAC_SHA1_80",
406 video_content->cryptos()[0].cipher_suite);
407 ASSERT_EQ(47U, video_content->cryptos()[0].key_params.size());
408 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
409 video_content->protocol());
410 }
411
412 void VerifyNoCryptoParams(const cricket::SessionDescription* sdp, bool dtls) {
413 const cricket::ContentInfo* content = cricket::GetFirstAudioContent(sdp);
414 ASSERT_TRUE(content != NULL);
415 const cricket::AudioContentDescription* audio_content =
416 static_cast<const cricket::AudioContentDescription*>(
417 content->description);
418 ASSERT_TRUE(audio_content != NULL);
419 ASSERT_EQ(0U, audio_content->cryptos().size());
420
421 content = cricket::GetFirstVideoContent(sdp);
422 ASSERT_TRUE(content != NULL);
423 const cricket::VideoContentDescription* video_content =
424 static_cast<const cricket::VideoContentDescription*>(
425 content->description);
426 ASSERT_TRUE(video_content != NULL);
427 ASSERT_EQ(0U, video_content->cryptos().size());
428
429 if (dtls) {
430 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
431 audio_content->protocol());
432 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
433 video_content->protocol());
434 } else {
435 EXPECT_EQ(std::string(cricket::kMediaProtocolAvpf),
436 audio_content->protocol());
437 EXPECT_EQ(std::string(cricket::kMediaProtocolAvpf),
438 video_content->protocol());
439 }
440 }
441
442 // Set the internal fake description factories to do DTLS-SRTP.
443 void SetFactoryDtlsSrtp() {
444 desc_factory_->set_secure(cricket::SEC_ENABLED);
445 std::string identity_name = "WebRTC" +
446 talk_base::ToString(talk_base::CreateRandomId());
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000447 identity_.reset(talk_base::SSLIdentity::Generate(identity_name));
448 tdesc_factory_->set_identity(identity_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 tdesc_factory_->set_secure(cricket::SEC_REQUIRED);
450 }
451
452 void VerifyFingerprintStatus(const cricket::SessionDescription* sdp,
453 bool expected) {
454 const TransportInfo* audio = sdp->GetTransportInfoByName("audio");
455 ASSERT_TRUE(audio != NULL);
456 ASSERT_EQ(expected, audio->description.identity_fingerprint.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 const TransportInfo* video = sdp->GetTransportInfoByName("video");
458 ASSERT_TRUE(video != NULL);
459 ASSERT_EQ(expected, video->description.identity_fingerprint.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 }
461
462 void VerifyAnswerFromNonCryptoOffer() {
463 // Create a SDP without Crypto.
464 cricket::MediaSessionOptions options;
465 options.has_video = true;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000466 JsepSessionDescription* offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 CreateRemoteOffer(options, cricket::SEC_DISABLED));
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000468 ASSERT_TRUE(offer != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 VerifyNoCryptoParams(offer->description(), false);
470 SetRemoteDescriptionExpectError("Called with a SDP without crypto enabled",
wu@webrtc.org91053e72013-08-10 07:18:04 +0000471 offer);
472 const webrtc::SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 // Answer should be NULL as no crypto params in offer.
474 ASSERT_TRUE(answer == NULL);
475 }
476
477 void VerifyAnswerFromCryptoOffer() {
478 cricket::MediaSessionOptions options;
479 options.has_video = true;
480 options.bundle_enabled = true;
481 scoped_ptr<JsepSessionDescription> offer(
482 CreateRemoteOffer(options, cricket::SEC_REQUIRED));
483 ASSERT_TRUE(offer.get() != NULL);
484 VerifyCryptoParams(offer->description());
485 SetRemoteDescriptionWithoutError(offer.release());
wu@webrtc.org91053e72013-08-10 07:18:04 +0000486 scoped_ptr<SessionDescriptionInterface> answer(CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 ASSERT_TRUE(answer.get() != NULL);
488 VerifyCryptoParams(answer->description());
489 }
490
491 void CompareIceUfragAndPassword(const cricket::SessionDescription* desc1,
492 const cricket::SessionDescription* desc2,
493 bool expect_equal) {
494 if (desc1->contents().size() != desc2->contents().size()) {
495 EXPECT_FALSE(expect_equal);
496 return;
497 }
498
499 const cricket::ContentInfos& contents = desc1->contents();
500 cricket::ContentInfos::const_iterator it = contents.begin();
501
502 for (; it != contents.end(); ++it) {
503 const cricket::TransportDescription* transport_desc1 =
504 desc1->GetTransportDescriptionByName(it->name);
505 const cricket::TransportDescription* transport_desc2 =
506 desc2->GetTransportDescriptionByName(it->name);
507 if (!transport_desc1 || !transport_desc2) {
508 EXPECT_FALSE(expect_equal);
509 return;
510 }
511 if (transport_desc1->ice_pwd != transport_desc2->ice_pwd ||
512 transport_desc1->ice_ufrag != transport_desc2->ice_ufrag) {
513 EXPECT_FALSE(expect_equal);
514 return;
515 }
516 }
517 EXPECT_TRUE(expect_equal);
518 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000519
520 void RemoveIceUfragPwdLines(const SessionDescriptionInterface* current_desc,
521 std::string *sdp) {
522 const cricket::SessionDescription* desc = current_desc->description();
523 EXPECT_TRUE(current_desc->ToString(sdp));
524
525 const cricket::ContentInfos& contents = desc->contents();
526 cricket::ContentInfos::const_iterator it = contents.begin();
527 // Replace ufrag and pwd lines with empty strings.
528 for (; it != contents.end(); ++it) {
529 const cricket::TransportDescription* transport_desc =
530 desc->GetTransportDescriptionByName(it->name);
531 std::string ufrag_line = "a=ice-ufrag:" + transport_desc->ice_ufrag
532 + "\r\n";
533 std::string pwd_line = "a=ice-pwd:" + transport_desc->ice_pwd
534 + "\r\n";
535 talk_base::replace_substrs(ufrag_line.c_str(), ufrag_line.length(),
536 "", 0,
537 sdp);
538 talk_base::replace_substrs(pwd_line.c_str(), pwd_line.length(),
539 "", 0,
540 sdp);
541 }
542 }
543
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 // Creates a remote offer and and applies it as a remote description,
545 // creates a local answer and applies is as a local description.
546 // Call mediastream_signaling_.UseOptionsWithStreamX() before this function
547 // to decide which local and remote streams to create.
548 void CreateAndSetRemoteOfferAndLocalAnswer() {
549 SessionDescriptionInterface* offer = CreateRemoteOffer();
550 SetRemoteDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000551 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 SetLocalDescriptionWithoutError(answer);
553 }
554 void SetLocalDescriptionWithoutError(SessionDescriptionInterface* desc) {
555 EXPECT_TRUE(session_->SetLocalDescription(desc, NULL));
556 }
557 void SetLocalDescriptionExpectState(SessionDescriptionInterface* desc,
558 BaseSession::State expected_state) {
559 SetLocalDescriptionWithoutError(desc);
560 EXPECT_EQ(expected_state, session_->state());
561 }
562 void SetLocalDescriptionExpectError(const std::string& expected_error,
563 SessionDescriptionInterface* desc) {
564 std::string error;
565 EXPECT_FALSE(session_->SetLocalDescription(desc, &error));
566 EXPECT_NE(std::string::npos, error.find(kSetLocalSdpFailed));
567 EXPECT_NE(std::string::npos, error.find(expected_error));
568 }
569 void SetRemoteDescriptionWithoutError(SessionDescriptionInterface* desc) {
570 EXPECT_TRUE(session_->SetRemoteDescription(desc, NULL));
571 }
572 void SetRemoteDescriptionExpectState(SessionDescriptionInterface* desc,
573 BaseSession::State expected_state) {
574 SetRemoteDescriptionWithoutError(desc);
575 EXPECT_EQ(expected_state, session_->state());
576 }
577 void SetRemoteDescriptionExpectError(const std::string& expected_error,
578 SessionDescriptionInterface* desc) {
579 std::string error;
580 EXPECT_FALSE(session_->SetRemoteDescription(desc, &error));
581 EXPECT_NE(std::string::npos, error.find(kSetRemoteSdpFailed));
582 EXPECT_NE(std::string::npos, error.find(expected_error));
583 }
584
585 void CreateCryptoOfferAndNonCryptoAnswer(SessionDescriptionInterface** offer,
586 SessionDescriptionInterface** nocrypto_answer) {
587 // Create a SDP without Crypto.
588 cricket::MediaSessionOptions options;
589 options.has_video = true;
590 options.bundle_enabled = true;
591 *offer = CreateRemoteOffer(options, cricket::SEC_ENABLED);
592 ASSERT_TRUE(*offer != NULL);
593 VerifyCryptoParams((*offer)->description());
594
595 *nocrypto_answer = CreateRemoteAnswer(*offer, options,
596 cricket::SEC_DISABLED);
597 EXPECT_TRUE(*nocrypto_answer != NULL);
598 }
599
600 JsepSessionDescription* CreateRemoteOfferWithVersion(
601 cricket::MediaSessionOptions options,
602 cricket::SecurePolicy secure_policy,
603 const std::string& session_version,
604 const SessionDescriptionInterface* current_desc) {
605 std::string session_id = talk_base::ToString(talk_base::CreateRandomId64());
606 const cricket::SessionDescription* cricket_desc = NULL;
607 if (current_desc) {
608 cricket_desc = current_desc->description();
609 session_id = current_desc->session_id();
610 }
611
612 desc_factory_->set_secure(secure_policy);
613 JsepSessionDescription* offer(
614 new JsepSessionDescription(JsepSessionDescription::kOffer));
615 if (!offer->Initialize(desc_factory_->CreateOffer(options, cricket_desc),
616 session_id, session_version)) {
617 delete offer;
618 offer = NULL;
619 }
620 return offer;
621 }
622 JsepSessionDescription* CreateRemoteOffer(
623 cricket::MediaSessionOptions options) {
624 return CreateRemoteOfferWithVersion(options, cricket::SEC_ENABLED,
625 kSessionVersion, NULL);
626 }
627 JsepSessionDescription* CreateRemoteOffer(
628 cricket::MediaSessionOptions options, cricket::SecurePolicy policy) {
629 return CreateRemoteOfferWithVersion(options, policy, kSessionVersion, NULL);
630 }
631 JsepSessionDescription* CreateRemoteOffer(
632 cricket::MediaSessionOptions options,
633 const SessionDescriptionInterface* current_desc) {
634 return CreateRemoteOfferWithVersion(options, cricket::SEC_ENABLED,
635 kSessionVersion, current_desc);
636 }
637
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000638 JsepSessionDescription* CreateRemoteOfferWithSctpPort(
639 const char* sctp_stream_name, int new_port,
640 cricket::MediaSessionOptions options) {
641 options.data_channel_type = cricket::DCT_SCTP;
642 options.AddStream(cricket::MEDIA_TYPE_DATA, "datachannel",
643 sctp_stream_name);
644 return ChangeSDPSctpPort(new_port, CreateRemoteOffer(options));
645 }
646
647 // Takes ownership of offer_basis (and deletes it).
648 JsepSessionDescription* ChangeSDPSctpPort(
649 int new_port, webrtc::SessionDescriptionInterface *offer_basis) {
650 // Stringify the input SDP, swap the 5000 for 'new_port' and create a new
651 // SessionDescription from the mutated string.
652 const char* default_port_str = "5000";
653 char new_port_str[16];
654 talk_base::sprintfn(new_port_str, sizeof(new_port_str), "%d", new_port);
655 std::string offer_str;
656 offer_basis->ToString(&offer_str);
657 talk_base::replace_substrs(default_port_str, strlen(default_port_str),
658 new_port_str, strlen(new_port_str),
659 &offer_str);
660 JsepSessionDescription* offer = new JsepSessionDescription(
661 offer_basis->type());
662 delete offer_basis;
663 offer->Initialize(offer_str, NULL);
664 return offer;
665 }
666
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 // Create a remote offer. Call mediastream_signaling_.UseOptionsWithStreamX()
668 // before this function to decide which streams to create.
669 JsepSessionDescription* CreateRemoteOffer() {
670 cricket::MediaSessionOptions options;
671 mediastream_signaling_.GetOptionsForAnswer(NULL, &options);
672 return CreateRemoteOffer(options, session_->remote_description());
673 }
674
675 JsepSessionDescription* CreateRemoteAnswer(
676 const SessionDescriptionInterface* offer,
677 cricket::MediaSessionOptions options,
678 cricket::SecurePolicy policy) {
679 desc_factory_->set_secure(policy);
680 const std::string session_id =
681 talk_base::ToString(talk_base::CreateRandomId64());
682 JsepSessionDescription* answer(
683 new JsepSessionDescription(JsepSessionDescription::kAnswer));
684 if (!answer->Initialize(desc_factory_->CreateAnswer(offer->description(),
685 options, NULL),
686 session_id, kSessionVersion)) {
687 delete answer;
688 answer = NULL;
689 }
690 return answer;
691 }
692
693 JsepSessionDescription* CreateRemoteAnswer(
694 const SessionDescriptionInterface* offer,
695 cricket::MediaSessionOptions options) {
696 return CreateRemoteAnswer(offer, options, cricket::SEC_REQUIRED);
697 }
698
699 // Creates an answer session description with streams based on
700 // |mediastream_signaling_|. Call
701 // mediastream_signaling_.UseOptionsWithStreamX() before this function
702 // to decide which streams to create.
703 JsepSessionDescription* CreateRemoteAnswer(
704 const SessionDescriptionInterface* offer) {
705 cricket::MediaSessionOptions options;
706 mediastream_signaling_.GetOptionsForAnswer(NULL, &options);
707 return CreateRemoteAnswer(offer, options, cricket::SEC_REQUIRED);
708 }
709
710 void TestSessionCandidatesWithBundleRtcpMux(bool bundle, bool rtcp_mux) {
711 AddInterface(kClientAddr1);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000712 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 mediastream_signaling_.SendAudioVideoStream1();
714 FakeConstraints constraints;
715 constraints.SetMandatoryUseRtpMux(bundle);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000716 SessionDescriptionInterface* offer = CreateOffer(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
718 // and answer.
719 SetLocalDescriptionWithoutError(offer);
720
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000721 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
722 CreateRemoteAnswer(session_->local_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 std::string sdp;
724 EXPECT_TRUE(answer->ToString(&sdp));
725
726 size_t expected_candidate_num = 2;
727 if (!rtcp_mux) {
728 // If rtcp_mux is enabled we should expect 4 candidates - host and srflex
729 // for rtp and rtcp.
730 expected_candidate_num = 4;
731 // Disable rtcp-mux from the answer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 const std::string kRtcpMux = "a=rtcp-mux";
733 const std::string kXRtcpMux = "a=xrtcp-mux";
734 talk_base::replace_substrs(kRtcpMux.c_str(), kRtcpMux.length(),
735 kXRtcpMux.c_str(), kXRtcpMux.length(),
736 &sdp);
737 }
738
739 SessionDescriptionInterface* new_answer = CreateSessionDescription(
740 JsepSessionDescription::kAnswer, sdp, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741
742 // SetRemoteDescription to enable rtcp mux.
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000743 SetRemoteDescriptionWithoutError(new_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
745 EXPECT_EQ(expected_candidate_num, observer_.mline_0_candidates_.size());
746 EXPECT_EQ(expected_candidate_num, observer_.mline_1_candidates_.size());
747 for (size_t i = 0; i < observer_.mline_0_candidates_.size(); ++i) {
748 cricket::Candidate c0 = observer_.mline_0_candidates_[i];
749 cricket::Candidate c1 = observer_.mline_1_candidates_[i];
750 if (bundle) {
751 EXPECT_TRUE(c0.IsEquivalent(c1));
752 } else {
753 EXPECT_FALSE(c0.IsEquivalent(c1));
754 }
755 }
756 }
757 // Tests that we can only send DTMF when the dtmf codec is supported.
758 void TestCanInsertDtmf(bool can) {
759 if (can) {
760 InitWithDtmfCodec();
761 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000762 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 }
764 mediastream_signaling_.SendAudioVideoStream1();
765 CreateAndSetRemoteOfferAndLocalAnswer();
766 EXPECT_FALSE(session_->CanInsertDtmf(""));
767 EXPECT_EQ(can, session_->CanInsertDtmf(kAudioTrack1));
768 }
769
770 // The method sets up a call from the session to itself, in a loopback
771 // arrangement. It also uses a firewall rule to create a temporary
772 // disconnection. This code is placed as a method so that it can be invoked
773 // by multiple tests with different allocators (e.g. with and without BUNDLE).
774 // While running the call, this method also checks if the session goes through
775 // the correct sequence of ICE states when a connection is established,
776 // broken, and re-established.
777 // The Connection state should go:
778 // New -> Checking -> Connected -> Disconnected -> Connected.
779 // The Gathering state should go: New -> Gathering -> Completed.
780 void TestLoopbackCall() {
781 AddInterface(kClientAddr1);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000782 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000784 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785
786 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
787 observer_.ice_gathering_state_);
788 SetLocalDescriptionWithoutError(offer);
789 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
790 observer_.ice_connection_state_);
791 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringGathering,
792 observer_.ice_gathering_state_,
793 kIceCandidatesTimeout);
794 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
795 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
796 observer_.ice_gathering_state_,
797 kIceCandidatesTimeout);
798
799 std::string sdp;
800 offer->ToString(&sdp);
801 SessionDescriptionInterface* desc =
802 webrtc::CreateSessionDescription(JsepSessionDescription::kAnswer, sdp);
803 ASSERT_TRUE(desc != NULL);
804 SetRemoteDescriptionWithoutError(desc);
805
806 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionChecking,
807 observer_.ice_connection_state_,
808 kIceCandidatesTimeout);
809 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionConnected,
810 observer_.ice_connection_state_,
811 kIceCandidatesTimeout);
812 // TODO(bemasc): EXPECT(Completed) once the details are standardized.
813
814 // Adding firewall rule to block ping requests, which should cause
815 // transport channel failure.
816 fss_->AddRule(false, talk_base::FP_ANY, talk_base::FD_ANY, kClientAddr1);
817 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
818 observer_.ice_connection_state_,
819 kIceCandidatesTimeout);
820
821 // Clearing the rules, session should move back to completed state.
822 fss_->ClearRules();
823 // Session is automatically calling OnSignalingReady after creation of
824 // new portallocator session which will allocate new set of candidates.
825
826 // TODO(bemasc): Change this to Completed once the details are standardized.
827 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionConnected,
828 observer_.ice_connection_state_,
829 kIceCandidatesTimeout);
830 }
831
832 void VerifyTransportType(const std::string& content_name,
833 cricket::TransportProtocol protocol) {
834 const cricket::Transport* transport = session_->GetTransport(content_name);
835 ASSERT_TRUE(transport != NULL);
836 EXPECT_EQ(protocol, transport->protocol());
837 }
838
839 // Adds CN codecs to FakeMediaEngine and MediaDescriptionFactory.
840 void AddCNCodecs() {
841 // Add kTelephoneEventCodec for dtmf test.
842 std::vector<cricket::AudioCodec> codecs = media_engine_->audio_codecs();;
843 codecs.push_back(kCNCodec1);
844 codecs.push_back(kCNCodec2);
845 media_engine_->SetAudioCodecs(codecs);
846 desc_factory_->set_audio_codecs(codecs);
847 }
848
849 bool VerifyNoCNCodecs(const cricket::ContentInfo* content) {
850 const cricket::ContentDescription* description = content->description;
851 ASSERT(description != NULL);
852 const cricket::AudioContentDescription* audio_content_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000853 static_cast<const cricket::AudioContentDescription*>(description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 ASSERT(audio_content_desc != NULL);
855 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
856 if (audio_content_desc->codecs()[i].name == "CN")
857 return false;
858 }
859 return true;
860 }
861
862 void SetLocalDescriptionWithDataChannel() {
863 webrtc::DataChannelInit dci;
864 dci.reliable = false;
865 session_->CreateDataChannel("datachannel", &dci);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000866 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 SetLocalDescriptionWithoutError(offer);
868 }
869
wu@webrtc.org91053e72013-08-10 07:18:04 +0000870 void VerifyMultipleAsyncCreateDescription(
871 bool success, CreateSessionDescriptionRequest::Type type) {
henrike@webrtc.org7666db72013-08-22 14:45:42 +0000872 InitWithDtls(!success);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000873
874 if (type == CreateSessionDescriptionRequest::kAnswer) {
875 cricket::MediaSessionOptions options;
876 scoped_ptr<JsepSessionDescription> offer(
877 CreateRemoteOffer(options, cricket::SEC_REQUIRED));
878 ASSERT_TRUE(offer.get() != NULL);
879 SetRemoteDescriptionWithoutError(offer.release());
880 }
881
882 const int kNumber = 3;
883 talk_base::scoped_refptr<WebRtcSessionCreateSDPObserverForTest>
884 observers[kNumber];
885 for (int i = 0; i < kNumber; ++i) {
886 observers[i] = new WebRtcSessionCreateSDPObserverForTest();
887 if (type == CreateSessionDescriptionRequest::kOffer) {
888 session_->CreateOffer(observers[i], NULL);
889 } else {
890 session_->CreateAnswer(observers[i], NULL);
891 }
892 }
893
894 WebRtcSessionCreateSDPObserverForTest::State expected_state =
895 success ? WebRtcSessionCreateSDPObserverForTest::kSucceeded :
896 WebRtcSessionCreateSDPObserverForTest::kFailed;
897
898 for (int i = 0; i < kNumber; ++i) {
899 EXPECT_EQ_WAIT(expected_state, observers[i]->state(), 1000);
900 if (success) {
901 EXPECT_TRUE(observers[i]->description() != NULL);
902 } else {
903 EXPECT_TRUE(observers[i]->description() == NULL);
904 }
905 }
906 }
907
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 cricket::FakeMediaEngine* media_engine_;
909 cricket::FakeDataEngine* data_engine_;
910 cricket::FakeDeviceManager* device_manager_;
911 talk_base::scoped_ptr<cricket::ChannelManager> channel_manager_;
912 talk_base::scoped_ptr<cricket::TransportDescriptionFactory> tdesc_factory_;
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000913 talk_base::scoped_ptr<talk_base::SSLIdentity> identity_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 talk_base::scoped_ptr<cricket::MediaSessionDescriptionFactory> desc_factory_;
915 talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_;
916 talk_base::scoped_ptr<talk_base::VirtualSocketServer> vss_;
917 talk_base::scoped_ptr<talk_base::FirewallSocketServer> fss_;
918 talk_base::SocketServerScope ss_scope_;
919 cricket::TestStunServer stun_server_;
920 talk_base::FakeNetworkManager network_manager_;
921 cricket::BasicPortAllocator allocator_;
922 talk_base::scoped_ptr<FakeConstraints> constraints_;
923 FakeMediaStreamSignaling mediastream_signaling_;
924 talk_base::scoped_ptr<WebRtcSessionForTest> session_;
925 MockIceObserver observer_;
926 cricket::FakeVideoMediaChannel* video_channel_;
927 cricket::FakeVoiceMediaChannel* voice_channel_;
928};
929
930TEST_F(WebRtcSessionTest, TestInitialize) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000931 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932}
933
934TEST_F(WebRtcSessionTest, TestInitializeWithDtls) {
935 InitWithDtls();
936}
937
wu@webrtc.org91053e72013-08-10 07:18:04 +0000938// Verifies that WebRtcSession uses SEC_REQUIRED by default.
939TEST_F(WebRtcSessionTest, TestDefaultSetSecurePolicy) {
940 Init(NULL);
941 EXPECT_EQ(cricket::SEC_REQUIRED, session_->secure_policy());
942}
943
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944TEST_F(WebRtcSessionTest, TestSessionCandidates) {
945 TestSessionCandidatesWithBundleRtcpMux(false, false);
946}
947
948// Below test cases (TestSessionCandidatesWith*) verify the candidates gathered
949// with rtcp-mux and/or bundle.
950TEST_F(WebRtcSessionTest, TestSessionCandidatesWithRtcpMux) {
951 TestSessionCandidatesWithBundleRtcpMux(false, true);
952}
953
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954TEST_F(WebRtcSessionTest, TestSessionCandidatesWithBundleRtcpMux) {
955 TestSessionCandidatesWithBundleRtcpMux(true, true);
956}
957
958TEST_F(WebRtcSessionTest, TestMultihomeCandidates) {
959 AddInterface(kClientAddr1);
960 AddInterface(kClientAddr2);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000961 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 mediastream_signaling_.SendAudioVideoStream1();
963 InitiateCall();
964 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
965 EXPECT_EQ(8u, observer_.mline_0_candidates_.size());
966 EXPECT_EQ(8u, observer_.mline_1_candidates_.size());
967}
968
969TEST_F(WebRtcSessionTest, TestStunError) {
970 AddInterface(kClientAddr1);
971 AddInterface(kClientAddr2);
972 fss_->AddRule(false, talk_base::FP_UDP, talk_base::FD_ANY, kClientAddr1);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000973 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 mediastream_signaling_.SendAudioVideoStream1();
975 InitiateCall();
976 // Since kClientAddr1 is blocked, not expecting stun candidates for it.
977 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
978 EXPECT_EQ(6u, observer_.mline_0_candidates_.size());
979 EXPECT_EQ(6u, observer_.mline_1_candidates_.size());
980}
981
982// Test creating offers and receive answers and make sure the
983// media engine creates the expected send and receive streams.
984TEST_F(WebRtcSessionTest, TestCreateOfferReceiveAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000985 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000987 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 const std::string session_id_orig = offer->session_id();
989 const std::string session_version_orig = offer->session_version();
990 SetLocalDescriptionWithoutError(offer);
991
992 mediastream_signaling_.SendAudioVideoStream2();
993 SessionDescriptionInterface* answer =
994 CreateRemoteAnswer(session_->local_description());
995 SetRemoteDescriptionWithoutError(answer);
996
997 video_channel_ = media_engine_->GetVideoChannel(0);
998 voice_channel_ = media_engine_->GetVoiceChannel(0);
999
1000 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1001 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1002
1003 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1004 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1005
1006 ASSERT_EQ(1u, video_channel_->send_streams().size());
1007 EXPECT_TRUE(kVideoTrack1 == video_channel_->send_streams()[0].id);
1008 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1009 EXPECT_TRUE(kAudioTrack1 == voice_channel_->send_streams()[0].id);
1010
1011 // Create new offer without send streams.
1012 mediastream_signaling_.SendNothing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001013 offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014
1015 // Verify the session id is the same and the session version is
1016 // increased.
1017 EXPECT_EQ(session_id_orig, offer->session_id());
1018 EXPECT_LT(talk_base::FromString<uint64>(session_version_orig),
1019 talk_base::FromString<uint64>(offer->session_version()));
1020
1021 SetLocalDescriptionWithoutError(offer);
1022
1023 mediastream_signaling_.SendAudioVideoStream2();
1024 answer = CreateRemoteAnswer(session_->local_description());
1025 SetRemoteDescriptionWithoutError(answer);
1026
1027 EXPECT_EQ(0u, video_channel_->send_streams().size());
1028 EXPECT_EQ(0u, voice_channel_->send_streams().size());
1029
1030 // Make sure the receive streams have not changed.
1031 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1032 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1033 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1034 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1035}
1036
1037// Test receiving offers and creating answers and make sure the
1038// media engine creates the expected send and receive streams.
1039TEST_F(WebRtcSessionTest, TestReceiveOfferCreateAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001040 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 mediastream_signaling_.SendAudioVideoStream2();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001042 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043 SetRemoteDescriptionWithoutError(offer);
1044
1045 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001046 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 SetLocalDescriptionWithoutError(answer);
1048
1049 const std::string session_id_orig = answer->session_id();
1050 const std::string session_version_orig = answer->session_version();
1051
1052 video_channel_ = media_engine_->GetVideoChannel(0);
1053 voice_channel_ = media_engine_->GetVoiceChannel(0);
1054
1055 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1056 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1057
1058 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1059 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1060
1061 ASSERT_EQ(1u, video_channel_->send_streams().size());
1062 EXPECT_TRUE(kVideoTrack1 == video_channel_->send_streams()[0].id);
1063 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1064 EXPECT_TRUE(kAudioTrack1 == voice_channel_->send_streams()[0].id);
1065
1066 mediastream_signaling_.SendAudioVideoStream1And2();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001067 offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 SetRemoteDescriptionWithoutError(offer);
1069
1070 // Answer by turning off all send streams.
1071 mediastream_signaling_.SendNothing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001072 answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073
1074 // Verify the session id is the same and the session version is
1075 // increased.
1076 EXPECT_EQ(session_id_orig, answer->session_id());
1077 EXPECT_LT(talk_base::FromString<uint64>(session_version_orig),
1078 talk_base::FromString<uint64>(answer->session_version()));
1079 SetLocalDescriptionWithoutError(answer);
1080
1081 ASSERT_EQ(2u, video_channel_->recv_streams().size());
1082 EXPECT_TRUE(kVideoTrack1 == video_channel_->recv_streams()[0].id);
1083 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[1].id);
1084 ASSERT_EQ(2u, voice_channel_->recv_streams().size());
1085 EXPECT_TRUE(kAudioTrack1 == voice_channel_->recv_streams()[0].id);
1086 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[1].id);
1087
1088 // Make sure we have no send streams.
1089 EXPECT_EQ(0u, video_channel_->send_streams().size());
1090 EXPECT_EQ(0u, voice_channel_->send_streams().size());
1091}
1092
1093// Test we will return fail when apply an offer that doesn't have
1094// crypto enabled.
1095TEST_F(WebRtcSessionTest, SetNonCryptoOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001096 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 cricket::MediaSessionOptions options;
1098 options.has_video = true;
1099 JsepSessionDescription* offer = CreateRemoteOffer(
1100 options, cricket::SEC_DISABLED);
1101 ASSERT_TRUE(offer != NULL);
1102 VerifyNoCryptoParams(offer->description(), false);
1103 // SetRemoteDescription and SetLocalDescription will take the ownership of
1104 // the offer.
1105 SetRemoteDescriptionExpectError(kSdpWithoutCrypto, offer);
1106 offer = CreateRemoteOffer(options, cricket::SEC_DISABLED);
1107 ASSERT_TRUE(offer != NULL);
1108 SetLocalDescriptionExpectError(kSdpWithoutCrypto, offer);
1109}
1110
1111// Test we will return fail when apply an answer that doesn't have
1112// crypto enabled.
1113TEST_F(WebRtcSessionTest, SetLocalNonCryptoAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001114 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 SessionDescriptionInterface* offer = NULL;
1116 SessionDescriptionInterface* answer = NULL;
1117 CreateCryptoOfferAndNonCryptoAnswer(&offer, &answer);
1118 // SetRemoteDescription and SetLocalDescription will take the ownership of
1119 // the offer.
1120 SetRemoteDescriptionWithoutError(offer);
1121 SetLocalDescriptionExpectError(kSdpWithoutCrypto, answer);
1122}
1123
1124// Test we will return fail when apply an answer that doesn't have
1125// crypto enabled.
1126TEST_F(WebRtcSessionTest, SetRemoteNonCryptoAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001127 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 SessionDescriptionInterface* offer = NULL;
1129 SessionDescriptionInterface* answer = NULL;
1130 CreateCryptoOfferAndNonCryptoAnswer(&offer, &answer);
1131 // SetRemoteDescription and SetLocalDescription will take the ownership of
1132 // the offer.
1133 SetLocalDescriptionWithoutError(offer);
1134 SetRemoteDescriptionExpectError(kSdpWithoutCrypto, answer);
1135}
1136
1137// Test that we can create and set an offer with a DTLS fingerprint.
1138TEST_F(WebRtcSessionTest, CreateSetDtlsOffer) {
1139 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
1140 InitWithDtls();
1141 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001142 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 ASSERT_TRUE(offer != NULL);
1144 VerifyFingerprintStatus(offer->description(), true);
1145 // SetLocalDescription will take the ownership of the offer.
1146 SetLocalDescriptionWithoutError(offer);
1147}
1148
1149// Test that we can process an offer with a DTLS fingerprint
1150// and that we return an answer with a fingerprint.
1151TEST_F(WebRtcSessionTest, ReceiveDtlsOfferCreateAnswer) {
1152 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
1153 InitWithDtls();
1154 SetFactoryDtlsSrtp();
1155 cricket::MediaSessionOptions options;
1156 options.has_video = true;
1157 JsepSessionDescription* offer = CreateRemoteOffer(options);
1158 ASSERT_TRUE(offer != NULL);
1159 VerifyFingerprintStatus(offer->description(), true);
1160
1161 // SetRemoteDescription will take the ownership of the offer.
1162 SetRemoteDescriptionWithoutError(offer);
1163
1164 // Verify that we get a crypto fingerprint in the answer.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001165 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 ASSERT_TRUE(answer != NULL);
1167 VerifyFingerprintStatus(answer->description(), true);
1168 // Check that we don't have an a=crypto line in the answer.
1169 VerifyNoCryptoParams(answer->description(), true);
1170
1171 // Now set the local description, which should work, even without a=crypto.
1172 SetLocalDescriptionWithoutError(answer);
1173}
1174
1175// Test that even if we support DTLS, if the other side didn't offer a
1176// fingerprint, we don't either.
1177TEST_F(WebRtcSessionTest, ReceiveNoDtlsOfferCreateAnswer) {
1178 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
1179 InitWithDtls();
1180 cricket::MediaSessionOptions options;
1181 options.has_video = true;
1182 JsepSessionDescription* offer = CreateRemoteOffer(
1183 options, cricket::SEC_REQUIRED);
1184 ASSERT_TRUE(offer != NULL);
1185 VerifyFingerprintStatus(offer->description(), false);
1186
1187 // SetRemoteDescription will take the ownership of
1188 // the offer.
1189 SetRemoteDescriptionWithoutError(offer);
1190
1191 // Verify that we don't get a crypto fingerprint in the answer.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001192 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 ASSERT_TRUE(answer != NULL);
1194 VerifyFingerprintStatus(answer->description(), false);
1195
1196 // Now set the local description.
1197 SetLocalDescriptionWithoutError(answer);
1198}
1199
1200TEST_F(WebRtcSessionTest, TestSetLocalOfferTwice) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001201 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 mediastream_signaling_.SendNothing();
1203 // SetLocalDescription take ownership of offer.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001204 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 SetLocalDescriptionWithoutError(offer);
1206
1207 // SetLocalDescription take ownership of offer.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001208 SessionDescriptionInterface* offer2 = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 SetLocalDescriptionWithoutError(offer2);
1210}
1211
1212TEST_F(WebRtcSessionTest, TestSetRemoteOfferTwice) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001213 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 mediastream_signaling_.SendNothing();
1215 // SetLocalDescription take ownership of offer.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001216 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 SetRemoteDescriptionWithoutError(offer);
1218
wu@webrtc.org91053e72013-08-10 07:18:04 +00001219 SessionDescriptionInterface* offer2 = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 SetRemoteDescriptionWithoutError(offer2);
1221}
1222
1223TEST_F(WebRtcSessionTest, TestSetLocalAndRemoteOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001224 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 mediastream_signaling_.SendNothing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001226 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 SetLocalDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001228 offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229 SetRemoteDescriptionExpectError(
1230 "Called with type in wrong state, type: offer state: STATE_SENTINITIATE",
1231 offer);
1232}
1233
1234TEST_F(WebRtcSessionTest, TestSetRemoteAndLocalOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001235 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236 mediastream_signaling_.SendNothing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001237 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238 SetRemoteDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001239 offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 SetLocalDescriptionExpectError(
1241 "Called with type in wrong state, type: "
1242 "offer state: STATE_RECEIVEDINITIATE",
1243 offer);
1244}
1245
1246TEST_F(WebRtcSessionTest, TestSetLocalPrAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001247 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 mediastream_signaling_.SendNothing();
1249 SessionDescriptionInterface* offer = CreateRemoteOffer();
1250 SetRemoteDescriptionExpectState(offer, BaseSession::STATE_RECEIVEDINITIATE);
1251
1252 JsepSessionDescription* pranswer = static_cast<JsepSessionDescription*>(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001253 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 pranswer->set_type(SessionDescriptionInterface::kPrAnswer);
1255 SetLocalDescriptionExpectState(pranswer, BaseSession::STATE_SENTPRACCEPT);
1256
1257 mediastream_signaling_.SendAudioVideoStream1();
1258 JsepSessionDescription* pranswer2 = static_cast<JsepSessionDescription*>(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001259 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 pranswer2->set_type(SessionDescriptionInterface::kPrAnswer);
1261
1262 SetLocalDescriptionExpectState(pranswer2, BaseSession::STATE_SENTPRACCEPT);
1263
1264 mediastream_signaling_.SendAudioVideoStream2();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001265 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001266 SetLocalDescriptionExpectState(answer, BaseSession::STATE_SENTACCEPT);
1267}
1268
1269TEST_F(WebRtcSessionTest, TestSetRemotePrAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001270 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 mediastream_signaling_.SendNothing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001272 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273 SetLocalDescriptionExpectState(offer, BaseSession::STATE_SENTINITIATE);
1274
1275 JsepSessionDescription* pranswer =
1276 CreateRemoteAnswer(session_->local_description());
1277 pranswer->set_type(SessionDescriptionInterface::kPrAnswer);
1278
1279 SetRemoteDescriptionExpectState(pranswer,
1280 BaseSession::STATE_RECEIVEDPRACCEPT);
1281
1282 mediastream_signaling_.SendAudioVideoStream1();
1283 JsepSessionDescription* pranswer2 =
1284 CreateRemoteAnswer(session_->local_description());
1285 pranswer2->set_type(SessionDescriptionInterface::kPrAnswer);
1286
1287 SetRemoteDescriptionExpectState(pranswer2,
1288 BaseSession::STATE_RECEIVEDPRACCEPT);
1289
1290 mediastream_signaling_.SendAudioVideoStream2();
1291 SessionDescriptionInterface* answer =
1292 CreateRemoteAnswer(session_->local_description());
1293 SetRemoteDescriptionExpectState(answer, BaseSession::STATE_RECEIVEDACCEPT);
1294}
1295
1296TEST_F(WebRtcSessionTest, TestSetLocalAnswerWithoutOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001297 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 mediastream_signaling_.SendNothing();
1299 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001300 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 SessionDescriptionInterface* answer =
1302 CreateRemoteAnswer(offer.get());
1303 SetLocalDescriptionExpectError(
1304 "Called with type in wrong state, type: answer state: STATE_INIT",
1305 answer);
1306}
1307
1308TEST_F(WebRtcSessionTest, TestSetRemoteAnswerWithoutOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001309 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 mediastream_signaling_.SendNothing();
1311 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001312 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 SessionDescriptionInterface* answer =
1314 CreateRemoteAnswer(offer.get());
1315 SetRemoteDescriptionExpectError(
1316 "Called with type in wrong state, type: answer state: STATE_INIT",
1317 answer);
1318}
1319
1320TEST_F(WebRtcSessionTest, TestAddRemoteCandidate) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001321 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 mediastream_signaling_.SendAudioVideoStream1();
1323
1324 cricket::Candidate candidate;
1325 candidate.set_component(1);
1326 JsepIceCandidate ice_candidate1(kMediaContentName0, 0, candidate);
1327
1328 // Fail since we have not set a offer description.
1329 EXPECT_FALSE(session_->ProcessIceMessage(&ice_candidate1));
1330
wu@webrtc.org91053e72013-08-10 07:18:04 +00001331 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 SetLocalDescriptionWithoutError(offer);
1333 // Candidate should be allowed to add before remote description.
1334 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate1));
1335 candidate.set_component(2);
1336 JsepIceCandidate ice_candidate2(kMediaContentName0, 0, candidate);
1337 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate2));
1338
1339 SessionDescriptionInterface* answer = CreateRemoteAnswer(
1340 session_->local_description());
1341 SetRemoteDescriptionWithoutError(answer);
1342
1343 // Verifying the candidates are copied properly from internal vector.
1344 const SessionDescriptionInterface* remote_desc =
1345 session_->remote_description();
1346 ASSERT_TRUE(remote_desc != NULL);
1347 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
1348 const IceCandidateCollection* candidates =
1349 remote_desc->candidates(kMediaContentIndex0);
1350 ASSERT_EQ(2u, candidates->count());
1351 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
1352 EXPECT_EQ(kMediaContentName0, candidates->at(0)->sdp_mid());
1353 EXPECT_EQ(1, candidates->at(0)->candidate().component());
1354 EXPECT_EQ(2, candidates->at(1)->candidate().component());
1355
1356 candidate.set_component(2);
1357 JsepIceCandidate ice_candidate3(kMediaContentName0, 0, candidate);
1358 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate3));
1359 ASSERT_EQ(3u, candidates->count());
1360
1361 JsepIceCandidate bad_ice_candidate("bad content name", 99, candidate);
1362 EXPECT_FALSE(session_->ProcessIceMessage(&bad_ice_candidate));
1363}
1364
1365// Test that a remote candidate is added to the remote session description and
1366// that it is retained if the remote session description is changed.
1367TEST_F(WebRtcSessionTest, TestRemoteCandidatesAddedToSessionDescription) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001368 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 cricket::Candidate candidate1;
1370 candidate1.set_component(1);
1371 JsepIceCandidate ice_candidate1(kMediaContentName0, kMediaContentIndex0,
1372 candidate1);
1373 mediastream_signaling_.SendAudioVideoStream1();
1374 CreateAndSetRemoteOfferAndLocalAnswer();
1375
1376 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate1));
1377 const SessionDescriptionInterface* remote_desc =
1378 session_->remote_description();
1379 ASSERT_TRUE(remote_desc != NULL);
1380 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
1381 const IceCandidateCollection* candidates =
1382 remote_desc->candidates(kMediaContentIndex0);
1383 ASSERT_EQ(1u, candidates->count());
1384 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
1385
1386 // Update the RemoteSessionDescription with a new session description and
1387 // a candidate and check that the new remote session description contains both
1388 // candidates.
1389 SessionDescriptionInterface* offer = CreateRemoteOffer();
1390 cricket::Candidate candidate2;
1391 JsepIceCandidate ice_candidate2(kMediaContentName0, kMediaContentIndex0,
1392 candidate2);
1393 EXPECT_TRUE(offer->AddCandidate(&ice_candidate2));
1394 SetRemoteDescriptionWithoutError(offer);
1395
1396 remote_desc = session_->remote_description();
1397 ASSERT_TRUE(remote_desc != NULL);
1398 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
1399 candidates = remote_desc->candidates(kMediaContentIndex0);
1400 ASSERT_EQ(2u, candidates->count());
1401 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
1402 // Username and password have be updated with the TransportInfo of the
1403 // SessionDescription, won't be equal to the original one.
1404 candidate2.set_username(candidates->at(0)->candidate().username());
1405 candidate2.set_password(candidates->at(0)->candidate().password());
1406 EXPECT_TRUE(candidate2.IsEquivalent(candidates->at(0)->candidate()));
1407 EXPECT_EQ(kMediaContentIndex0, candidates->at(1)->sdp_mline_index());
1408 // No need to verify the username and password.
1409 candidate1.set_username(candidates->at(1)->candidate().username());
1410 candidate1.set_password(candidates->at(1)->candidate().password());
1411 EXPECT_TRUE(candidate1.IsEquivalent(candidates->at(1)->candidate()));
1412
1413 // Test that the candidate is ignored if we can add the same candidate again.
1414 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate2));
1415}
1416
1417// Test that local candidates are added to the local session description and
1418// that they are retained if the local session description is changed.
1419TEST_F(WebRtcSessionTest, TestLocalCandidatesAddedToSessionDescription) {
1420 AddInterface(kClientAddr1);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001421 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 mediastream_signaling_.SendAudioVideoStream1();
1423 CreateAndSetRemoteOfferAndLocalAnswer();
1424
1425 const SessionDescriptionInterface* local_desc = session_->local_description();
1426 const IceCandidateCollection* candidates =
1427 local_desc->candidates(kMediaContentIndex0);
1428 ASSERT_TRUE(candidates != NULL);
1429 EXPECT_EQ(0u, candidates->count());
1430
1431 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1432
1433 local_desc = session_->local_description();
1434 candidates = local_desc->candidates(kMediaContentIndex0);
1435 ASSERT_TRUE(candidates != NULL);
1436 EXPECT_LT(0u, candidates->count());
1437 candidates = local_desc->candidates(1);
1438 ASSERT_TRUE(candidates != NULL);
1439 EXPECT_LT(0u, candidates->count());
1440
1441 // Update the session descriptions.
1442 mediastream_signaling_.SendAudioVideoStream1();
1443 CreateAndSetRemoteOfferAndLocalAnswer();
1444
1445 local_desc = session_->local_description();
1446 candidates = local_desc->candidates(kMediaContentIndex0);
1447 ASSERT_TRUE(candidates != NULL);
1448 EXPECT_LT(0u, candidates->count());
1449 candidates = local_desc->candidates(1);
1450 ASSERT_TRUE(candidates != NULL);
1451 EXPECT_LT(0u, candidates->count());
1452}
1453
1454// Test that we can set a remote session description with remote candidates.
1455TEST_F(WebRtcSessionTest, TestSetRemoteSessionDescriptionWithCandidates) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001456 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457
1458 cricket::Candidate candidate1;
1459 candidate1.set_component(1);
1460 JsepIceCandidate ice_candidate(kMediaContentName0, kMediaContentIndex0,
1461 candidate1);
1462 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001463 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464
1465 EXPECT_TRUE(offer->AddCandidate(&ice_candidate));
1466 SetRemoteDescriptionWithoutError(offer);
1467
1468 const SessionDescriptionInterface* remote_desc =
1469 session_->remote_description();
1470 ASSERT_TRUE(remote_desc != NULL);
1471 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
1472 const IceCandidateCollection* candidates =
1473 remote_desc->candidates(kMediaContentIndex0);
1474 ASSERT_EQ(1u, candidates->count());
1475 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
1476
wu@webrtc.org91053e72013-08-10 07:18:04 +00001477 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 SetLocalDescriptionWithoutError(answer);
1479}
1480
1481// Test that offers and answers contains ice candidates when Ice candidates have
1482// been gathered.
1483TEST_F(WebRtcSessionTest, TestSetLocalAndRemoteDescriptionWithCandidates) {
1484 AddInterface(kClientAddr1);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001485 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486 mediastream_signaling_.SendAudioVideoStream1();
1487 // Ice is started but candidates are not provided until SetLocalDescription
1488 // is called.
1489 EXPECT_EQ(0u, observer_.mline_0_candidates_.size());
1490 EXPECT_EQ(0u, observer_.mline_1_candidates_.size());
1491 CreateAndSetRemoteOfferAndLocalAnswer();
1492 // Wait until at least one local candidate has been collected.
1493 EXPECT_TRUE_WAIT(0u < observer_.mline_0_candidates_.size(),
1494 kIceCandidatesTimeout);
1495 EXPECT_TRUE_WAIT(0u < observer_.mline_1_candidates_.size(),
1496 kIceCandidatesTimeout);
1497
1498 talk_base::scoped_ptr<SessionDescriptionInterface> local_offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001499 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 ASSERT_TRUE(local_offer->candidates(kMediaContentIndex0) != NULL);
1501 EXPECT_LT(0u, local_offer->candidates(kMediaContentIndex0)->count());
1502 ASSERT_TRUE(local_offer->candidates(kMediaContentIndex1) != NULL);
1503 EXPECT_LT(0u, local_offer->candidates(kMediaContentIndex1)->count());
1504
1505 SessionDescriptionInterface* remote_offer(CreateRemoteOffer());
1506 SetRemoteDescriptionWithoutError(remote_offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001507 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508 ASSERT_TRUE(answer->candidates(kMediaContentIndex0) != NULL);
1509 EXPECT_LT(0u, answer->candidates(kMediaContentIndex0)->count());
1510 ASSERT_TRUE(answer->candidates(kMediaContentIndex1) != NULL);
1511 EXPECT_LT(0u, answer->candidates(kMediaContentIndex1)->count());
1512 SetLocalDescriptionWithoutError(answer);
1513}
1514
1515// Verifies TransportProxy and media channels are created with content names
1516// present in the SessionDescription.
1517TEST_F(WebRtcSessionTest, TestChannelCreationsWithContentNames) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001518 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 mediastream_signaling_.SendAudioVideoStream1();
1520 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001521 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522
1523 // CreateOffer creates session description with the content names "audio" and
1524 // "video". Goal is to modify these content names and verify transport channel
1525 // proxy in the BaseSession, as proxies are created with the content names
1526 // present in SDP.
1527 std::string sdp;
1528 EXPECT_TRUE(offer->ToString(&sdp));
1529 const std::string kAudioMid = "a=mid:audio";
1530 const std::string kAudioMidReplaceStr = "a=mid:audio_content_name";
1531 const std::string kVideoMid = "a=mid:video";
1532 const std::string kVideoMidReplaceStr = "a=mid:video_content_name";
1533
1534 // Replacing |audio| with |audio_content_name|.
1535 talk_base::replace_substrs(kAudioMid.c_str(), kAudioMid.length(),
1536 kAudioMidReplaceStr.c_str(),
1537 kAudioMidReplaceStr.length(),
1538 &sdp);
1539 // Replacing |video| with |video_content_name|.
1540 talk_base::replace_substrs(kVideoMid.c_str(), kVideoMid.length(),
1541 kVideoMidReplaceStr.c_str(),
1542 kVideoMidReplaceStr.length(),
1543 &sdp);
1544
1545 SessionDescriptionInterface* modified_offer =
1546 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
1547
1548 SetRemoteDescriptionWithoutError(modified_offer);
1549
1550 SessionDescriptionInterface* answer =
wu@webrtc.org91053e72013-08-10 07:18:04 +00001551 CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 SetLocalDescriptionWithoutError(answer);
1553
1554 EXPECT_TRUE(session_->GetTransportProxy("audio_content_name") != NULL);
1555 EXPECT_TRUE(session_->GetTransportProxy("video_content_name") != NULL);
1556 EXPECT_TRUE((video_channel_ = media_engine_->GetVideoChannel(0)) != NULL);
1557 EXPECT_TRUE((voice_channel_ = media_engine_->GetVoiceChannel(0)) != NULL);
1558}
1559
1560// Test that an offer contains the correct media content descriptions based on
1561// the send streams when no constraints have been set.
1562TEST_F(WebRtcSessionTest, CreateOfferWithoutConstraintsOrStreams) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001563 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001565 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 ASSERT_TRUE(offer != NULL);
1567 const cricket::ContentInfo* content =
1568 cricket::GetFirstAudioContent(offer->description());
1569 EXPECT_TRUE(content != NULL);
1570 content = cricket::GetFirstVideoContent(offer->description());
1571 EXPECT_TRUE(content == NULL);
1572}
1573
1574// Test that an offer contains the correct media content descriptions based on
1575// the send streams when no constraints have been set.
1576TEST_F(WebRtcSessionTest, CreateOfferWithoutConstraints) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001577 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 // Test Audio only offer.
1579 mediastream_signaling_.UseOptionsAudioOnly();
1580 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001581 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 const cricket::ContentInfo* content =
1583 cricket::GetFirstAudioContent(offer->description());
1584 EXPECT_TRUE(content != NULL);
1585 content = cricket::GetFirstVideoContent(offer->description());
1586 EXPECT_TRUE(content == NULL);
1587
1588 // Test Audio / Video offer.
1589 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001590 offer.reset(CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 content = cricket::GetFirstAudioContent(offer->description());
1592 EXPECT_TRUE(content != NULL);
1593 content = cricket::GetFirstVideoContent(offer->description());
1594 EXPECT_TRUE(content != NULL);
1595}
1596
1597// Test that an offer contains no media content descriptions if
1598// kOfferToReceiveVideo and kOfferToReceiveAudio constraints are set to false.
1599TEST_F(WebRtcSessionTest, CreateOfferWithConstraintsWithoutStreams) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001600 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 webrtc::FakeConstraints constraints_no_receive;
1602 constraints_no_receive.SetMandatoryReceiveAudio(false);
1603 constraints_no_receive.SetMandatoryReceiveVideo(false);
1604
1605 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001606 CreateOffer(&constraints_no_receive));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 ASSERT_TRUE(offer != NULL);
1608 const cricket::ContentInfo* content =
1609 cricket::GetFirstAudioContent(offer->description());
1610 EXPECT_TRUE(content == NULL);
1611 content = cricket::GetFirstVideoContent(offer->description());
1612 EXPECT_TRUE(content == NULL);
1613}
1614
1615// Test that an offer contains only audio media content descriptions if
1616// kOfferToReceiveAudio constraints are set to true.
1617TEST_F(WebRtcSessionTest, CreateAudioOnlyOfferWithConstraints) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001618 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619 webrtc::FakeConstraints constraints_audio_only;
1620 constraints_audio_only.SetMandatoryReceiveAudio(true);
1621 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001622 CreateOffer(&constraints_audio_only));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623
1624 const cricket::ContentInfo* content =
1625 cricket::GetFirstAudioContent(offer->description());
1626 EXPECT_TRUE(content != NULL);
1627 content = cricket::GetFirstVideoContent(offer->description());
1628 EXPECT_TRUE(content == NULL);
1629}
1630
1631// Test that an offer contains audio and video media content descriptions if
1632// kOfferToReceiveAudio and kOfferToReceiveVideo constraints are set to true.
1633TEST_F(WebRtcSessionTest, CreateOfferWithConstraints) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001634 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 // Test Audio / Video offer.
1636 webrtc::FakeConstraints constraints_audio_video;
1637 constraints_audio_video.SetMandatoryReceiveAudio(true);
1638 constraints_audio_video.SetMandatoryReceiveVideo(true);
1639 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001640 CreateOffer(&constraints_audio_video));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 const cricket::ContentInfo* content =
1642 cricket::GetFirstAudioContent(offer->description());
1643
1644 EXPECT_TRUE(content != NULL);
1645 content = cricket::GetFirstVideoContent(offer->description());
1646 EXPECT_TRUE(content != NULL);
1647
1648 // TODO(perkj): Should the direction be set to SEND_ONLY if
1649 // The constraints is set to not receive audio or video but a track is added?
1650}
1651
1652// Test that an answer can not be created if the last remote description is not
1653// an offer.
1654TEST_F(WebRtcSessionTest, CreateAnswerWithoutAnOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001655 Init(NULL);
1656 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 SetLocalDescriptionWithoutError(offer);
1658 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
1659 SetRemoteDescriptionWithoutError(answer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001660 EXPECT_TRUE(CreateAnswer(NULL) == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661}
1662
1663// Test that an answer contains the correct media content descriptions when no
1664// constraints have been set.
1665TEST_F(WebRtcSessionTest, CreateAnswerWithoutConstraintsOrStreams) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001666 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 // Create a remote offer with audio and video content.
1668 talk_base::scoped_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
1669 SetRemoteDescriptionWithoutError(offer.release());
1670 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001671 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 const cricket::ContentInfo* content =
1673 cricket::GetFirstAudioContent(answer->description());
1674 ASSERT_TRUE(content != NULL);
1675 EXPECT_FALSE(content->rejected);
1676
1677 content = cricket::GetFirstVideoContent(answer->description());
1678 ASSERT_TRUE(content != NULL);
1679 EXPECT_FALSE(content->rejected);
1680}
1681
1682// Test that an answer contains the correct media content descriptions when no
1683// constraints have been set and the offer only contain audio.
1684TEST_F(WebRtcSessionTest, CreateAudioAnswerWithoutConstraintsOrStreams) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001685 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686 // Create a remote offer with audio only.
1687 cricket::MediaSessionOptions options;
1688 options.has_audio = true;
1689 options.has_video = false;
1690 talk_base::scoped_ptr<JsepSessionDescription> offer(
1691 CreateRemoteOffer(options));
1692 ASSERT_TRUE(cricket::GetFirstVideoContent(offer->description()) == NULL);
1693 ASSERT_TRUE(cricket::GetFirstAudioContent(offer->description()) != NULL);
1694
1695 SetRemoteDescriptionWithoutError(offer.release());
1696 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001697 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 const cricket::ContentInfo* content =
1699 cricket::GetFirstAudioContent(answer->description());
1700 ASSERT_TRUE(content != NULL);
1701 EXPECT_FALSE(content->rejected);
1702
1703 EXPECT_TRUE(cricket::GetFirstVideoContent(answer->description()) == NULL);
1704}
1705
1706// Test that an answer contains the correct media content descriptions when no
1707// constraints have been set.
1708TEST_F(WebRtcSessionTest, CreateAnswerWithoutConstraints) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001709 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 // Create a remote offer with audio and video content.
1711 talk_base::scoped_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
1712 SetRemoteDescriptionWithoutError(offer.release());
1713 // Test with a stream with tracks.
1714 mediastream_signaling_.SendAudioVideoStream1();
1715 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001716 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 const cricket::ContentInfo* content =
1718 cricket::GetFirstAudioContent(answer->description());
1719 ASSERT_TRUE(content != NULL);
1720 EXPECT_FALSE(content->rejected);
1721
1722 content = cricket::GetFirstVideoContent(answer->description());
1723 ASSERT_TRUE(content != NULL);
1724 EXPECT_FALSE(content->rejected);
1725}
1726
1727// Test that an answer contains the correct media content descriptions when
1728// constraints have been set but no stream is sent.
1729TEST_F(WebRtcSessionTest, CreateAnswerWithConstraintsWithoutStreams) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001730 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731 // Create a remote offer with audio and video content.
1732 talk_base::scoped_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
1733 SetRemoteDescriptionWithoutError(offer.release());
1734
1735 webrtc::FakeConstraints constraints_no_receive;
1736 constraints_no_receive.SetMandatoryReceiveAudio(false);
1737 constraints_no_receive.SetMandatoryReceiveVideo(false);
1738
1739 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001740 CreateAnswer(&constraints_no_receive));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 const cricket::ContentInfo* content =
1742 cricket::GetFirstAudioContent(answer->description());
1743 ASSERT_TRUE(content != NULL);
1744 EXPECT_TRUE(content->rejected);
1745
1746 content = cricket::GetFirstVideoContent(answer->description());
1747 ASSERT_TRUE(content != NULL);
1748 EXPECT_TRUE(content->rejected);
1749}
1750
1751// Test that an answer contains the correct media content descriptions when
1752// constraints have been set and streams are sent.
1753TEST_F(WebRtcSessionTest, CreateAnswerWithConstraints) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001754 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 // Create a remote offer with audio and video content.
1756 talk_base::scoped_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
1757 SetRemoteDescriptionWithoutError(offer.release());
1758
1759 webrtc::FakeConstraints constraints_no_receive;
1760 constraints_no_receive.SetMandatoryReceiveAudio(false);
1761 constraints_no_receive.SetMandatoryReceiveVideo(false);
1762
1763 // Test with a stream with tracks.
1764 mediastream_signaling_.SendAudioVideoStream1();
1765 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001766 CreateAnswer(&constraints_no_receive));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767
1768 // TODO(perkj): Should the direction be set to SEND_ONLY?
1769 const cricket::ContentInfo* content =
1770 cricket::GetFirstAudioContent(answer->description());
1771 ASSERT_TRUE(content != NULL);
1772 EXPECT_FALSE(content->rejected);
1773
1774 // TODO(perkj): Should the direction be set to SEND_ONLY?
1775 content = cricket::GetFirstVideoContent(answer->description());
1776 ASSERT_TRUE(content != NULL);
1777 EXPECT_FALSE(content->rejected);
1778}
1779
1780TEST_F(WebRtcSessionTest, CreateOfferWithoutCNCodecs) {
1781 AddCNCodecs();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001782 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 webrtc::FakeConstraints constraints;
1784 constraints.SetOptionalVAD(false);
1785 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001786 CreateOffer(&constraints));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 const cricket::ContentInfo* content =
1788 cricket::GetFirstAudioContent(offer->description());
1789 EXPECT_TRUE(content != NULL);
1790 EXPECT_TRUE(VerifyNoCNCodecs(content));
1791}
1792
1793TEST_F(WebRtcSessionTest, CreateAnswerWithoutCNCodecs) {
1794 AddCNCodecs();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001795 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 // Create a remote offer with audio and video content.
1797 talk_base::scoped_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
1798 SetRemoteDescriptionWithoutError(offer.release());
1799
1800 webrtc::FakeConstraints constraints;
1801 constraints.SetOptionalVAD(false);
1802 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001803 CreateAnswer(&constraints));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 const cricket::ContentInfo* content =
1805 cricket::GetFirstAudioContent(answer->description());
1806 ASSERT_TRUE(content != NULL);
1807 EXPECT_TRUE(VerifyNoCNCodecs(content));
1808}
1809
1810// This test verifies the call setup when remote answer with audio only and
1811// later updates with video.
1812TEST_F(WebRtcSessionTest, TestAVOfferWithAudioOnlyAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001813 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 EXPECT_TRUE(media_engine_->GetVideoChannel(0) == NULL);
1815 EXPECT_TRUE(media_engine_->GetVoiceChannel(0) == NULL);
1816
1817 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001818 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819
1820 cricket::MediaSessionOptions options;
1821 options.has_video = false;
1822 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer, options);
1823
1824 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
1825 // and answer;
1826 SetLocalDescriptionWithoutError(offer);
1827 SetRemoteDescriptionWithoutError(answer);
1828
1829 video_channel_ = media_engine_->GetVideoChannel(0);
1830 voice_channel_ = media_engine_->GetVoiceChannel(0);
1831
1832 ASSERT_TRUE(video_channel_ == NULL);
1833
1834 ASSERT_EQ(0u, voice_channel_->recv_streams().size());
1835 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1836 EXPECT_EQ(kAudioTrack1, voice_channel_->send_streams()[0].id);
1837
1838 // Let the remote end update the session descriptions, with Audio and Video.
1839 mediastream_signaling_.SendAudioVideoStream2();
1840 CreateAndSetRemoteOfferAndLocalAnswer();
1841
1842 video_channel_ = media_engine_->GetVideoChannel(0);
1843 voice_channel_ = media_engine_->GetVoiceChannel(0);
1844
1845 ASSERT_TRUE(video_channel_ != NULL);
1846 ASSERT_TRUE(voice_channel_ != NULL);
1847
1848 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1849 ASSERT_EQ(1u, video_channel_->send_streams().size());
1850 EXPECT_EQ(kVideoTrack2, video_channel_->recv_streams()[0].id);
1851 EXPECT_EQ(kVideoTrack2, video_channel_->send_streams()[0].id);
1852 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1853 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1854 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
1855 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
1856
1857 // Change session back to audio only.
1858 mediastream_signaling_.UseOptionsAudioOnly();
1859 CreateAndSetRemoteOfferAndLocalAnswer();
1860
1861 EXPECT_EQ(0u, video_channel_->recv_streams().size());
1862 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1863 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
1864 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1865 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
1866}
1867
1868// This test verifies the call setup when remote answer with video only and
1869// later updates with audio.
1870TEST_F(WebRtcSessionTest, TestAVOfferWithVideoOnlyAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001871 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 EXPECT_TRUE(media_engine_->GetVideoChannel(0) == NULL);
1873 EXPECT_TRUE(media_engine_->GetVoiceChannel(0) == NULL);
1874 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001875 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876
1877 cricket::MediaSessionOptions options;
1878 options.has_audio = false;
1879 options.has_video = true;
1880 SessionDescriptionInterface* answer = CreateRemoteAnswer(
1881 offer, options, cricket::SEC_ENABLED);
1882
1883 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
1884 // and answer.
1885 SetLocalDescriptionWithoutError(offer);
1886 SetRemoteDescriptionWithoutError(answer);
1887
1888 video_channel_ = media_engine_->GetVideoChannel(0);
1889 voice_channel_ = media_engine_->GetVoiceChannel(0);
1890
1891 ASSERT_TRUE(voice_channel_ == NULL);
1892 ASSERT_TRUE(video_channel_ != NULL);
1893
1894 EXPECT_EQ(0u, video_channel_->recv_streams().size());
1895 ASSERT_EQ(1u, video_channel_->send_streams().size());
1896 EXPECT_EQ(kVideoTrack1, video_channel_->send_streams()[0].id);
1897
1898 // Update the session descriptions, with Audio and Video.
1899 mediastream_signaling_.SendAudioVideoStream2();
1900 CreateAndSetRemoteOfferAndLocalAnswer();
1901
1902 voice_channel_ = media_engine_->GetVoiceChannel(0);
1903 ASSERT_TRUE(voice_channel_ != NULL);
1904
1905 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1906 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1907 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
1908 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
1909
1910 // Change session back to video only.
1911 mediastream_signaling_.UseOptionsVideoOnly();
1912 CreateAndSetRemoteOfferAndLocalAnswer();
1913
1914 video_channel_ = media_engine_->GetVideoChannel(0);
1915 voice_channel_ = media_engine_->GetVoiceChannel(0);
1916
1917 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1918 EXPECT_EQ(kVideoTrack2, video_channel_->recv_streams()[0].id);
1919 ASSERT_EQ(1u, video_channel_->send_streams().size());
1920 EXPECT_EQ(kVideoTrack2, video_channel_->send_streams()[0].id);
1921}
1922
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923TEST_F(WebRtcSessionTest, VerifyCryptoParamsInSDP) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001924 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001925 mediastream_signaling_.SendAudioVideoStream1();
1926 scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001927 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928 VerifyCryptoParams(offer->description());
1929 SetRemoteDescriptionWithoutError(offer.release());
wu@webrtc.org91053e72013-08-10 07:18:04 +00001930 scoped_ptr<SessionDescriptionInterface> answer(CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 VerifyCryptoParams(answer->description());
1932}
1933
1934TEST_F(WebRtcSessionTest, VerifyNoCryptoParamsInSDP) {
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +00001935 constraints_.reset(new FakeConstraints());
1936 constraints_->AddOptional(
1937 webrtc::MediaConstraintsInterface::kInternalDisableEncryption, true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001938 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 mediastream_signaling_.SendAudioVideoStream1();
1940 scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001941 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 VerifyNoCryptoParams(offer->description(), false);
1943}
1944
1945TEST_F(WebRtcSessionTest, VerifyAnswerFromNonCryptoOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001946 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947 VerifyAnswerFromNonCryptoOffer();
1948}
1949
1950TEST_F(WebRtcSessionTest, VerifyAnswerFromCryptoOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001951 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 VerifyAnswerFromCryptoOffer();
1953}
1954
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001955// This test verifies that setLocalDescription fails if
1956// no a=ice-ufrag and a=ice-pwd lines are present in the SDP.
1957TEST_F(WebRtcSessionTest, TestSetLocalDescriptionWithoutIce) {
1958 Init(NULL);
1959 mediastream_signaling_.SendAudioVideoStream1();
1960 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateOffer(NULL));
1961 std::string sdp;
1962 RemoveIceUfragPwdLines(offer.get(), &sdp);
1963 SessionDescriptionInterface* modified_offer =
1964 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
1965 SetLocalDescriptionExpectError(kSdpWithoutIceUfragPwd, modified_offer);
1966}
1967
1968// This test verifies that setRemoteDescription fails if
1969// no a=ice-ufrag and a=ice-pwd lines are present in the SDP.
1970TEST_F(WebRtcSessionTest, TestSetRemoteDescriptionWithoutIce) {
1971 Init(NULL);
1972 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateRemoteOffer());
1973 std::string sdp;
1974 RemoveIceUfragPwdLines(offer.get(), &sdp);
1975 SessionDescriptionInterface* modified_offer =
1976 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
1977 SetRemoteDescriptionExpectError(kSdpWithoutIceUfragPwd, modified_offer);
1978}
1979
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980TEST_F(WebRtcSessionTest, VerifyBundleFlagInPA) {
1981 // This test verifies BUNDLE flag in PortAllocator, if BUNDLE information in
1982 // local description is removed by the application, BUNDLE flag should be
1983 // disabled in PortAllocator. By default BUNDLE is enabled in the WebRtc.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001984 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 EXPECT_TRUE((cricket::PORTALLOCATOR_ENABLE_BUNDLE & allocator_.flags()) ==
1986 cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1987 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001988 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989 cricket::SessionDescription* offer_copy =
1990 offer->description()->Copy();
1991 offer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
1992 JsepSessionDescription* modified_offer =
1993 new JsepSessionDescription(JsepSessionDescription::kOffer);
1994 modified_offer->Initialize(offer_copy, "1", "1");
1995
1996 SetLocalDescriptionWithoutError(modified_offer);
1997 EXPECT_FALSE(allocator_.flags() & cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1998}
1999
2000TEST_F(WebRtcSessionTest, TestDisabledBundleInAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002001 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 mediastream_signaling_.SendAudioVideoStream1();
2003 EXPECT_TRUE((cricket::PORTALLOCATOR_ENABLE_BUNDLE & allocator_.flags()) ==
2004 cricket::PORTALLOCATOR_ENABLE_BUNDLE);
2005 FakeConstraints constraints;
2006 constraints.SetMandatoryUseRtpMux(true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002007 SessionDescriptionInterface* offer = CreateOffer(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 SetLocalDescriptionWithoutError(offer);
2009 mediastream_signaling_.SendAudioVideoStream2();
2010 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
2011 CreateRemoteAnswer(session_->local_description()));
2012 cricket::SessionDescription* answer_copy = answer->description()->Copy();
2013 answer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
2014 JsepSessionDescription* modified_answer =
2015 new JsepSessionDescription(JsepSessionDescription::kAnswer);
2016 modified_answer->Initialize(answer_copy, "1", "1");
2017 SetRemoteDescriptionWithoutError(modified_answer);
2018 EXPECT_TRUE((cricket::PORTALLOCATOR_ENABLE_BUNDLE & allocator_.flags()) ==
2019 cricket::PORTALLOCATOR_ENABLE_BUNDLE);
2020
2021 video_channel_ = media_engine_->GetVideoChannel(0);
2022 voice_channel_ = media_engine_->GetVoiceChannel(0);
2023
2024 ASSERT_EQ(1u, video_channel_->recv_streams().size());
2025 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
2026
2027 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
2028 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
2029
2030 ASSERT_EQ(1u, video_channel_->send_streams().size());
2031 EXPECT_TRUE(kVideoTrack1 == video_channel_->send_streams()[0].id);
2032 ASSERT_EQ(1u, voice_channel_->send_streams().size());
2033 EXPECT_TRUE(kAudioTrack1 == voice_channel_->send_streams()[0].id);
2034}
2035
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002036// This test verifies that SetLocalDescription and SetRemoteDescription fails
2037// if BUNDLE is enabled but rtcp-mux is disabled in m-lines.
2038TEST_F(WebRtcSessionTest, TestDisabledRtcpMuxWithBundleEnabled) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002039 WebRtcSessionTest::Init(NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002040 mediastream_signaling_.SendAudioVideoStream1();
2041 EXPECT_TRUE((cricket::PORTALLOCATOR_ENABLE_BUNDLE & allocator_.flags()) ==
2042 cricket::PORTALLOCATOR_ENABLE_BUNDLE);
2043 FakeConstraints constraints;
2044 constraints.SetMandatoryUseRtpMux(true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002045 SessionDescriptionInterface* offer = CreateOffer(&constraints);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002046 std::string offer_str;
2047 offer->ToString(&offer_str);
2048 // Disable rtcp-mux
2049 const std::string rtcp_mux = "rtcp-mux";
2050 const std::string xrtcp_mux = "xrtcp-mux";
2051 talk_base::replace_substrs(rtcp_mux.c_str(), rtcp_mux.length(),
2052 xrtcp_mux.c_str(), xrtcp_mux.length(),
2053 &offer_str);
2054 JsepSessionDescription *local_offer =
2055 new JsepSessionDescription(JsepSessionDescription::kOffer);
2056 EXPECT_TRUE((local_offer)->Initialize(offer_str, NULL));
2057 SetLocalDescriptionExpectError(kBundleWithoutRtcpMux, local_offer);
2058 JsepSessionDescription *remote_offer =
2059 new JsepSessionDescription(JsepSessionDescription::kOffer);
2060 EXPECT_TRUE((remote_offer)->Initialize(offer_str, NULL));
2061 SetRemoteDescriptionExpectError(kBundleWithoutRtcpMux, remote_offer);
2062 // Trying unmodified SDP.
2063 SetLocalDescriptionWithoutError(offer);
2064}
2065
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066TEST_F(WebRtcSessionTest, SetAudioPlayout) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002067 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 mediastream_signaling_.SendAudioVideoStream1();
2069 CreateAndSetRemoteOfferAndLocalAnswer();
2070 cricket::FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
2071 ASSERT_TRUE(channel != NULL);
2072 ASSERT_EQ(1u, channel->recv_streams().size());
2073 uint32 receive_ssrc = channel->recv_streams()[0].first_ssrc();
2074 double left_vol, right_vol;
2075 EXPECT_TRUE(channel->GetOutputScaling(receive_ssrc, &left_vol, &right_vol));
2076 EXPECT_EQ(1, left_vol);
2077 EXPECT_EQ(1, right_vol);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002078 talk_base::scoped_ptr<FakeAudioRenderer> renderer(new FakeAudioRenderer());
2079 session_->SetAudioPlayout(receive_ssrc, false, renderer.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080 EXPECT_TRUE(channel->GetOutputScaling(receive_ssrc, &left_vol, &right_vol));
2081 EXPECT_EQ(0, left_vol);
2082 EXPECT_EQ(0, right_vol);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002083 EXPECT_EQ(0, renderer->channel_id());
2084 session_->SetAudioPlayout(receive_ssrc, true, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002085 EXPECT_TRUE(channel->GetOutputScaling(receive_ssrc, &left_vol, &right_vol));
2086 EXPECT_EQ(1, left_vol);
2087 EXPECT_EQ(1, right_vol);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002088 EXPECT_EQ(-1, renderer->channel_id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089}
2090
2091TEST_F(WebRtcSessionTest, SetAudioSend) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002092 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093 mediastream_signaling_.SendAudioVideoStream1();
2094 CreateAndSetRemoteOfferAndLocalAnswer();
2095 cricket::FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
2096 ASSERT_TRUE(channel != NULL);
2097 ASSERT_EQ(1u, channel->send_streams().size());
2098 uint32 send_ssrc = channel->send_streams()[0].first_ssrc();
2099 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
2100
2101 cricket::AudioOptions options;
2102 options.echo_cancellation.Set(true);
2103
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002104 talk_base::scoped_ptr<FakeAudioRenderer> renderer(new FakeAudioRenderer());
2105 session_->SetAudioSend(send_ssrc, false, options, renderer.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 EXPECT_TRUE(channel->IsStreamMuted(send_ssrc));
2107 EXPECT_FALSE(channel->options().echo_cancellation.IsSet());
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002108 EXPECT_EQ(0, renderer->channel_id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002110 session_->SetAudioSend(send_ssrc, true, options, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
2112 bool value;
2113 EXPECT_TRUE(channel->options().echo_cancellation.Get(&value));
2114 EXPECT_TRUE(value);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002115 EXPECT_EQ(-1, renderer->channel_id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116}
2117
2118TEST_F(WebRtcSessionTest, SetVideoPlayout) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002119 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 mediastream_signaling_.SendAudioVideoStream1();
2121 CreateAndSetRemoteOfferAndLocalAnswer();
2122 cricket::FakeVideoMediaChannel* channel = media_engine_->GetVideoChannel(0);
2123 ASSERT_TRUE(channel != NULL);
2124 ASSERT_LT(0u, channel->renderers().size());
2125 EXPECT_TRUE(channel->renderers().begin()->second == NULL);
2126 ASSERT_EQ(1u, channel->recv_streams().size());
2127 uint32 receive_ssrc = channel->recv_streams()[0].first_ssrc();
2128 cricket::FakeVideoRenderer renderer;
2129 session_->SetVideoPlayout(receive_ssrc, true, &renderer);
2130 EXPECT_TRUE(channel->renderers().begin()->second == &renderer);
2131 session_->SetVideoPlayout(receive_ssrc, false, &renderer);
2132 EXPECT_TRUE(channel->renderers().begin()->second == NULL);
2133}
2134
2135TEST_F(WebRtcSessionTest, SetVideoSend) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002136 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 mediastream_signaling_.SendAudioVideoStream1();
2138 CreateAndSetRemoteOfferAndLocalAnswer();
2139 cricket::FakeVideoMediaChannel* channel = media_engine_->GetVideoChannel(0);
2140 ASSERT_TRUE(channel != NULL);
2141 ASSERT_EQ(1u, channel->send_streams().size());
2142 uint32 send_ssrc = channel->send_streams()[0].first_ssrc();
2143 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
2144 cricket::VideoOptions* options = NULL;
2145 session_->SetVideoSend(send_ssrc, false, options);
2146 EXPECT_TRUE(channel->IsStreamMuted(send_ssrc));
2147 session_->SetVideoSend(send_ssrc, true, options);
2148 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
2149}
2150
2151TEST_F(WebRtcSessionTest, CanNotInsertDtmf) {
2152 TestCanInsertDtmf(false);
2153}
2154
2155TEST_F(WebRtcSessionTest, CanInsertDtmf) {
2156 TestCanInsertDtmf(true);
2157}
2158
2159TEST_F(WebRtcSessionTest, InsertDtmf) {
2160 // Setup
wu@webrtc.org91053e72013-08-10 07:18:04 +00002161 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162 mediastream_signaling_.SendAudioVideoStream1();
2163 CreateAndSetRemoteOfferAndLocalAnswer();
2164 FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
2165 EXPECT_EQ(0U, channel->dtmf_info_queue().size());
2166
2167 // Insert DTMF
2168 const int expected_flags = DF_SEND;
2169 const int expected_duration = 90;
2170 session_->InsertDtmf(kAudioTrack1, 0, expected_duration);
2171 session_->InsertDtmf(kAudioTrack1, 1, expected_duration);
2172 session_->InsertDtmf(kAudioTrack1, 2, expected_duration);
2173
2174 // Verify
2175 ASSERT_EQ(3U, channel->dtmf_info_queue().size());
2176 const uint32 send_ssrc = channel->send_streams()[0].first_ssrc();
2177 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[0], send_ssrc, 0,
2178 expected_duration, expected_flags));
2179 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[1], send_ssrc, 1,
2180 expected_duration, expected_flags));
2181 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[2], send_ssrc, 2,
2182 expected_duration, expected_flags));
2183}
2184
2185// This test verifies the |initiator| flag when session initiates the call.
2186TEST_F(WebRtcSessionTest, TestInitiatorFlagAsOriginator) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002187 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 EXPECT_FALSE(session_->initiator());
wu@webrtc.org91053e72013-08-10 07:18:04 +00002189 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002190 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
2191 SetLocalDescriptionWithoutError(offer);
2192 EXPECT_TRUE(session_->initiator());
2193 SetRemoteDescriptionWithoutError(answer);
2194 EXPECT_TRUE(session_->initiator());
2195}
2196
2197// This test verifies the |initiator| flag when session receives the call.
2198TEST_F(WebRtcSessionTest, TestInitiatorFlagAsReceiver) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002199 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200 EXPECT_FALSE(session_->initiator());
2201 SessionDescriptionInterface* offer = CreateRemoteOffer();
2202 SetRemoteDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002203 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204
2205 EXPECT_FALSE(session_->initiator());
2206 SetLocalDescriptionWithoutError(answer);
2207 EXPECT_FALSE(session_->initiator());
2208}
2209
2210// This test verifies the ice protocol type at initiator of the call
2211// if |a=ice-options:google-ice| is present in answer.
2212TEST_F(WebRtcSessionTest, TestInitiatorGIceInAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002213 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002215 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org723d6832013-07-12 16:04:50 +00002216 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002217 CreateRemoteAnswer(offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 SetLocalDescriptionWithoutError(offer);
2219 std::string sdp;
2220 EXPECT_TRUE(answer->ToString(&sdp));
2221 // Adding ice-options to the session level.
2222 InjectAfter("t=0 0\r\n",
2223 "a=ice-options:google-ice\r\n",
2224 &sdp);
2225 SessionDescriptionInterface* answer_with_gice =
2226 CreateSessionDescription(JsepSessionDescription::kAnswer, sdp, NULL);
2227 SetRemoteDescriptionWithoutError(answer_with_gice);
2228 VerifyTransportType("audio", cricket::ICEPROTO_GOOGLE);
2229 VerifyTransportType("video", cricket::ICEPROTO_GOOGLE);
2230}
2231
2232// This test verifies the ice protocol type at initiator of the call
2233// if ICE RFC5245 is supported in answer.
2234TEST_F(WebRtcSessionTest, TestInitiatorIceInAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002235 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002237 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
2239 SetLocalDescriptionWithoutError(offer);
2240
2241 SetRemoteDescriptionWithoutError(answer);
2242 VerifyTransportType("audio", cricket::ICEPROTO_RFC5245);
2243 VerifyTransportType("video", cricket::ICEPROTO_RFC5245);
2244}
2245
2246// This test verifies the ice protocol type at receiver side of the call if
2247// receiver decides to use google-ice.
2248TEST_F(WebRtcSessionTest, TestReceiverGIceInOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002249 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002251 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252 SetRemoteDescriptionWithoutError(offer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +00002253 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002254 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255 std::string sdp;
2256 EXPECT_TRUE(answer->ToString(&sdp));
2257 // Adding ice-options to the session level.
2258 InjectAfter("t=0 0\r\n",
2259 "a=ice-options:google-ice\r\n",
2260 &sdp);
2261 SessionDescriptionInterface* answer_with_gice =
2262 CreateSessionDescription(JsepSessionDescription::kAnswer, sdp, NULL);
2263 SetLocalDescriptionWithoutError(answer_with_gice);
2264 VerifyTransportType("audio", cricket::ICEPROTO_GOOGLE);
2265 VerifyTransportType("video", cricket::ICEPROTO_GOOGLE);
2266}
2267
2268// This test verifies the ice protocol type at receiver side of the call if
2269// receiver decides to use ice RFC 5245.
2270TEST_F(WebRtcSessionTest, TestReceiverIceInOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002271 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002273 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274 SetRemoteDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002275 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 SetLocalDescriptionWithoutError(answer);
2277 VerifyTransportType("audio", cricket::ICEPROTO_RFC5245);
2278 VerifyTransportType("video", cricket::ICEPROTO_RFC5245);
2279}
2280
2281// This test verifies the session state when ICE RFC5245 in offer and
2282// ICE google-ice in answer.
2283TEST_F(WebRtcSessionTest, TestIceOfferGIceOnlyAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002284 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 mediastream_signaling_.SendAudioVideoStream1();
2286 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002287 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288 std::string offer_str;
2289 offer->ToString(&offer_str);
2290 // Disable google-ice
2291 const std::string gice_option = "google-ice";
2292 const std::string xgoogle_xice = "xgoogle-xice";
2293 talk_base::replace_substrs(gice_option.c_str(), gice_option.length(),
2294 xgoogle_xice.c_str(), xgoogle_xice.length(),
2295 &offer_str);
2296 JsepSessionDescription *ice_only_offer =
2297 new JsepSessionDescription(JsepSessionDescription::kOffer);
2298 EXPECT_TRUE((ice_only_offer)->Initialize(offer_str, NULL));
2299 SetLocalDescriptionWithoutError(ice_only_offer);
2300 std::string original_offer_sdp;
2301 EXPECT_TRUE(offer->ToString(&original_offer_sdp));
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002302 SessionDescriptionInterface* pranswer_with_gice =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303 CreateSessionDescription(JsepSessionDescription::kPrAnswer,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002304 original_offer_sdp, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002305 SetRemoteDescriptionExpectError(kPushDownPranswerTDFailed,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002306 pranswer_with_gice);
2307 SessionDescriptionInterface* answer_with_gice =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 CreateSessionDescription(JsepSessionDescription::kAnswer,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002309 original_offer_sdp, NULL);
henrike@webrtc.org723d6832013-07-12 16:04:50 +00002310 SetRemoteDescriptionExpectError(kPushDownAnswerTDFailed,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002311 answer_with_gice);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312}
2313
2314// Verifing local offer and remote answer have matching m-lines as per RFC 3264.
2315TEST_F(WebRtcSessionTest, TestIncorrectMLinesInRemoteAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002316 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002318 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319 SetLocalDescriptionWithoutError(offer);
2320 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
2321 CreateRemoteAnswer(session_->local_description()));
2322
2323 cricket::SessionDescription* answer_copy = answer->description()->Copy();
2324 answer_copy->RemoveContentByName("video");
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002325 JsepSessionDescription* modified_answer =
2326 new JsepSessionDescription(JsepSessionDescription::kAnswer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327
2328 EXPECT_TRUE(modified_answer->Initialize(answer_copy,
2329 answer->session_id(),
2330 answer->session_version()));
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002331 SetRemoteDescriptionExpectError(kMlineMismatch, modified_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332
2333 // Modifying content names.
2334 std::string sdp;
2335 EXPECT_TRUE(answer->ToString(&sdp));
2336 const std::string kAudioMid = "a=mid:audio";
2337 const std::string kAudioMidReplaceStr = "a=mid:audio_content_name";
2338
2339 // Replacing |audio| with |audio_content_name|.
2340 talk_base::replace_substrs(kAudioMid.c_str(), kAudioMid.length(),
2341 kAudioMidReplaceStr.c_str(),
2342 kAudioMidReplaceStr.length(),
2343 &sdp);
2344
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002345 SessionDescriptionInterface* modified_answer1 =
2346 CreateSessionDescription(JsepSessionDescription::kAnswer, sdp, NULL);
2347 SetRemoteDescriptionExpectError(kMlineMismatch, modified_answer1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348
2349 SetRemoteDescriptionWithoutError(answer.release());
2350}
2351
2352// Verifying remote offer and local answer have matching m-lines as per
2353// RFC 3264.
2354TEST_F(WebRtcSessionTest, TestIncorrectMLinesInLocalAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002355 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 mediastream_signaling_.SendAudioVideoStream1();
2357 SessionDescriptionInterface* offer = CreateRemoteOffer();
2358 SetRemoteDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002359 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360
2361 cricket::SessionDescription* answer_copy = answer->description()->Copy();
2362 answer_copy->RemoveContentByName("video");
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002363 JsepSessionDescription* modified_answer =
2364 new JsepSessionDescription(JsepSessionDescription::kAnswer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002365
2366 EXPECT_TRUE(modified_answer->Initialize(answer_copy,
2367 answer->session_id(),
2368 answer->session_version()));
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002369 SetLocalDescriptionExpectError(kMlineMismatch, modified_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002370 SetLocalDescriptionWithoutError(answer);
2371}
2372
2373// This test verifies that WebRtcSession does not start candidate allocation
2374// before SetLocalDescription is called.
2375TEST_F(WebRtcSessionTest, TestIceStartAfterSetLocalDescriptionOnly) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002376 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002377 mediastream_signaling_.SendAudioVideoStream1();
2378 SessionDescriptionInterface* offer = CreateRemoteOffer();
2379 cricket::Candidate candidate;
2380 candidate.set_component(1);
2381 JsepIceCandidate ice_candidate(kMediaContentName0, kMediaContentIndex0,
2382 candidate);
2383 EXPECT_TRUE(offer->AddCandidate(&ice_candidate));
2384 cricket::Candidate candidate1;
2385 candidate1.set_component(1);
2386 JsepIceCandidate ice_candidate1(kMediaContentName1, kMediaContentIndex1,
2387 candidate1);
2388 EXPECT_TRUE(offer->AddCandidate(&ice_candidate1));
2389 SetRemoteDescriptionWithoutError(offer);
2390 ASSERT_TRUE(session_->GetTransportProxy("audio") != NULL);
2391 ASSERT_TRUE(session_->GetTransportProxy("video") != NULL);
2392
2393 // Pump for 1 second and verify that no candidates are generated.
2394 talk_base::Thread::Current()->ProcessMessages(1000);
2395 EXPECT_TRUE(observer_.mline_0_candidates_.empty());
2396 EXPECT_TRUE(observer_.mline_1_candidates_.empty());
2397
wu@webrtc.org91053e72013-08-10 07:18:04 +00002398 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002399 SetLocalDescriptionWithoutError(answer);
2400 EXPECT_TRUE(session_->GetTransportProxy("audio")->negotiated());
2401 EXPECT_TRUE(session_->GetTransportProxy("video")->negotiated());
2402 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
2403}
2404
2405// This test verifies that crypto parameter is updated in local session
2406// description as per security policy set in MediaSessionDescriptionFactory.
2407TEST_F(WebRtcSessionTest, TestCryptoAfterSetLocalDescription) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002408 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002409 mediastream_signaling_.SendAudioVideoStream1();
2410 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002411 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002412
2413 // Making sure SetLocalDescription correctly sets crypto value in
2414 // SessionDescription object after de-serialization of sdp string. The value
2415 // will be set as per MediaSessionDescriptionFactory.
2416 std::string offer_str;
2417 offer->ToString(&offer_str);
2418 SessionDescriptionInterface* jsep_offer_str =
2419 CreateSessionDescription(JsepSessionDescription::kOffer, offer_str, NULL);
2420 SetLocalDescriptionWithoutError(jsep_offer_str);
2421 EXPECT_TRUE(session_->voice_channel()->secure_required());
2422 EXPECT_TRUE(session_->video_channel()->secure_required());
2423}
2424
2425// This test verifies the crypto parameter when security is disabled.
2426TEST_F(WebRtcSessionTest, TestCryptoAfterSetLocalDescriptionWithDisabled) {
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +00002427 constraints_.reset(new FakeConstraints());
2428 constraints_->AddOptional(
2429 webrtc::MediaConstraintsInterface::kInternalDisableEncryption, true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002430 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002431 mediastream_signaling_.SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002432 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002433 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002434
2435 // Making sure SetLocalDescription correctly sets crypto value in
2436 // SessionDescription object after de-serialization of sdp string. The value
2437 // will be set as per MediaSessionDescriptionFactory.
2438 std::string offer_str;
2439 offer->ToString(&offer_str);
2440 SessionDescriptionInterface *jsep_offer_str =
2441 CreateSessionDescription(JsepSessionDescription::kOffer, offer_str, NULL);
2442 SetLocalDescriptionWithoutError(jsep_offer_str);
2443 EXPECT_FALSE(session_->voice_channel()->secure_required());
2444 EXPECT_FALSE(session_->video_channel()->secure_required());
2445}
2446
2447// This test verifies that an answer contains new ufrag and password if an offer
2448// with new ufrag and password is received.
2449TEST_F(WebRtcSessionTest, TestCreateAnswerWithNewUfragAndPassword) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002450 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002451 cricket::MediaSessionOptions options;
2452 options.has_audio = true;
2453 options.has_video = true;
2454 talk_base::scoped_ptr<JsepSessionDescription> offer(
2455 CreateRemoteOffer(options));
2456 SetRemoteDescriptionWithoutError(offer.release());
2457
2458 mediastream_signaling_.SendAudioVideoStream1();
2459 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002460 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002461 SetLocalDescriptionWithoutError(answer.release());
2462
2463 // Receive an offer with new ufrag and password.
2464 options.transport_options.ice_restart = true;
2465 talk_base::scoped_ptr<JsepSessionDescription> updated_offer1(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002466 CreateRemoteOffer(options, session_->remote_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467 SetRemoteDescriptionWithoutError(updated_offer1.release());
2468
2469 talk_base::scoped_ptr<SessionDescriptionInterface> updated_answer1(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002470 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002471
2472 CompareIceUfragAndPassword(updated_answer1->description(),
2473 session_->local_description()->description(),
2474 false);
2475
2476 SetLocalDescriptionWithoutError(updated_answer1.release());
wu@webrtc.org91053e72013-08-10 07:18:04 +00002477}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002478
wu@webrtc.org91053e72013-08-10 07:18:04 +00002479// This test verifies that an answer contains old ufrag and password if an offer
2480// with old ufrag and password is received.
2481TEST_F(WebRtcSessionTest, TestCreateAnswerWithOldUfragAndPassword) {
2482 Init(NULL);
2483 cricket::MediaSessionOptions options;
2484 options.has_audio = true;
2485 options.has_video = true;
2486 talk_base::scoped_ptr<JsepSessionDescription> offer(
2487 CreateRemoteOffer(options));
2488 SetRemoteDescriptionWithoutError(offer.release());
2489
2490 mediastream_signaling_.SendAudioVideoStream1();
2491 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
2492 CreateAnswer(NULL));
2493 SetLocalDescriptionWithoutError(answer.release());
2494
2495 // Receive an offer without changed ufrag or password.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002496 options.transport_options.ice_restart = false;
2497 talk_base::scoped_ptr<JsepSessionDescription> updated_offer2(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002498 CreateRemoteOffer(options, session_->remote_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499 SetRemoteDescriptionWithoutError(updated_offer2.release());
2500
2501 talk_base::scoped_ptr<SessionDescriptionInterface> updated_answer2(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002502 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002503
2504 CompareIceUfragAndPassword(updated_answer2->description(),
2505 session_->local_description()->description(),
2506 true);
2507
2508 SetLocalDescriptionWithoutError(updated_answer2.release());
2509}
2510
2511TEST_F(WebRtcSessionTest, TestSessionContentError) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002512 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002513 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002514 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002515 const std::string session_id_orig = offer->session_id();
2516 const std::string session_version_orig = offer->session_version();
2517 SetLocalDescriptionWithoutError(offer);
2518
2519 video_channel_ = media_engine_->GetVideoChannel(0);
2520 video_channel_->set_fail_set_send_codecs(true);
2521
2522 mediastream_signaling_.SendAudioVideoStream2();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002523 SessionDescriptionInterface* answer =
2524 CreateRemoteAnswer(session_->local_description());
2525 SetRemoteDescriptionExpectError("ERROR_CONTENT", answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002526}
2527
2528// Runs the loopback call test with BUNDLE and STUN disabled.
2529TEST_F(WebRtcSessionTest, TestIceStatesBasic) {
2530 // Lets try with only UDP ports.
2531 allocator_.set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
2532 cricket::PORTALLOCATOR_DISABLE_TCP |
2533 cricket::PORTALLOCATOR_DISABLE_STUN |
2534 cricket::PORTALLOCATOR_DISABLE_RELAY);
2535 TestLoopbackCall();
2536}
2537
2538// Regression-test for a crash which should have been an error.
2539TEST_F(WebRtcSessionTest, TestNoStateTransitionPendingError) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002540 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002541 cricket::MediaSessionOptions options;
2542 options.has_audio = true;
2543 options.has_video = true;
2544
2545 session_->SetError(cricket::BaseSession::ERROR_CONTENT);
2546 SessionDescriptionInterface* offer = CreateRemoteOffer(options);
2547 SessionDescriptionInterface* answer =
2548 CreateRemoteAnswer(offer, options);
2549 SetRemoteDescriptionExpectError(kSessionError, offer);
2550 SetLocalDescriptionExpectError(kSessionError, answer);
2551 // Not crashing is our success.
2552}
2553
2554TEST_F(WebRtcSessionTest, TestRtpDataChannel) {
2555 constraints_.reset(new FakeConstraints());
2556 constraints_->AddOptional(
2557 webrtc::MediaConstraintsInterface::kEnableRtpDataChannels, true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002558 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002559
2560 SetLocalDescriptionWithDataChannel();
2561 EXPECT_EQ(cricket::DCT_RTP, data_engine_->last_channel_type());
2562}
2563
2564TEST_F(WebRtcSessionTest, TestRtpDataChannelConstraintTakesPrecedence) {
2565 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2566
2567 constraints_.reset(new FakeConstraints());
2568 constraints_->AddOptional(
2569 webrtc::MediaConstraintsInterface::kEnableRtpDataChannels, true);
2570 constraints_->AddOptional(
2571 webrtc::MediaConstraintsInterface::kEnableSctpDataChannels, true);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002572 InitWithDtls(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002573
2574 SetLocalDescriptionWithDataChannel();
2575 EXPECT_EQ(cricket::DCT_RTP, data_engine_->last_channel_type());
2576}
2577
wu@webrtc.org967bfff2013-09-19 05:49:50 +00002578TEST_F(WebRtcSessionTest, TestCreateOfferWithSctpEnabledWithoutStreams) {
2579 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2580
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002581 constraints_.reset(new FakeConstraints());
2582 constraints_->AddOptional(
2583 webrtc::MediaConstraintsInterface::kEnableSctpDataChannels, true);
2584 InitWithDtls(false);
2585
2586 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateOffer(NULL));
2587 EXPECT_TRUE(offer->description()->GetContentByName("data") == NULL);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00002588 EXPECT_TRUE(offer->description()->GetTransportInfoByName("data") == NULL);
2589}
2590
2591TEST_F(WebRtcSessionTest, TestCreateAnswerWithSctpInOfferAndNoStreams) {
2592 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2593 SetFactoryDtlsSrtp();
2594 constraints_.reset(new FakeConstraints());
2595 constraints_->AddOptional(
2596 webrtc::MediaConstraintsInterface::kEnableSctpDataChannels, true);
2597 InitWithDtls(false);
2598
2599 // Create remote offer with SCTP.
2600 cricket::MediaSessionOptions options;
2601 options.data_channel_type = cricket::DCT_SCTP;
2602 JsepSessionDescription* offer =
2603 CreateRemoteOffer(options, cricket::SEC_ENABLED);
2604 SetRemoteDescriptionWithoutError(offer);
2605
2606 // Verifies the answer contains SCTP.
2607 talk_base::scoped_ptr<SessionDescriptionInterface> answer(CreateAnswer(NULL));
2608 EXPECT_TRUE(answer != NULL);
2609 EXPECT_TRUE(answer->description()->GetContentByName("data") != NULL);
2610 EXPECT_TRUE(answer->description()->GetTransportInfoByName("data") != NULL);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002611}
2612
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002613TEST_F(WebRtcSessionTest, TestSctpDataChannelWithoutDtls) {
2614 constraints_.reset(new FakeConstraints());
2615 constraints_->AddOptional(
2616 webrtc::MediaConstraintsInterface::kEnableSctpDataChannels, true);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002617 constraints_->AddOptional(
2618 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
2619 InitWithDtls(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002620
2621 SetLocalDescriptionWithDataChannel();
2622 EXPECT_EQ(cricket::DCT_NONE, data_engine_->last_channel_type());
2623}
2624
2625TEST_F(WebRtcSessionTest, TestSctpDataChannelWithDtls) {
2626 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2627
2628 constraints_.reset(new FakeConstraints());
2629 constraints_->AddOptional(
2630 webrtc::MediaConstraintsInterface::kEnableSctpDataChannels, true);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002631 InitWithDtls(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002632
2633 SetLocalDescriptionWithDataChannel();
2634 EXPECT_EQ(cricket::DCT_SCTP, data_engine_->last_channel_type());
2635}
wu@webrtc.org91053e72013-08-10 07:18:04 +00002636
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002637TEST_F(WebRtcSessionTest, TestSctpDataChannelSendPortParsing) {
2638 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2639 const int new_send_port = 9998;
2640 const int new_recv_port = 7775;
2641
2642 constraints_.reset(new FakeConstraints());
2643 constraints_->AddOptional(
2644 webrtc::MediaConstraintsInterface::kEnableSctpDataChannels, true);
2645
2646 InitWithDtls(false);
2647 SetFactoryDtlsSrtp();
2648
2649 // By default, don't actually add the codecs to desc_factory_; they don't
2650 // actually get serialized for SCTP in BuildMediaDescription(). Instead,
2651 // let the session description get parsed. That'll get the proper codecs
2652 // into the stream.
2653 cricket::MediaSessionOptions options;
2654 JsepSessionDescription* offer = CreateRemoteOfferWithSctpPort(
2655 "stream1", new_send_port, options);
2656
2657 // SetRemoteDescription will take the ownership of the offer.
2658 SetRemoteDescriptionWithoutError(offer);
2659
2660 SessionDescriptionInterface* answer = ChangeSDPSctpPort(
2661 new_recv_port, CreateAnswer(NULL));
2662 ASSERT_TRUE(answer != NULL);
2663
2664 // Now set the local description, which'll take ownership of the answer.
2665 SetLocalDescriptionWithoutError(answer);
2666
2667 // TEST PLAN: Set the port number to something new, set it in the SDP,
2668 // and pass it all the way down.
2669 webrtc::DataChannelInit dci;
2670 dci.reliable = true;
2671 EXPECT_EQ(cricket::DCT_SCTP, data_engine_->last_channel_type());
2672 talk_base::scoped_refptr<webrtc::DataChannel> dc =
2673 session_->CreateDataChannel("datachannel", &dci);
2674
2675 cricket::FakeDataMediaChannel* ch = data_engine_->GetChannel(0);
2676 int portnum = -1;
2677 ASSERT_TRUE(ch != NULL);
2678 ASSERT_EQ(1UL, ch->send_codecs().size());
2679 EXPECT_EQ(cricket::kGoogleSctpDataCodecId, ch->send_codecs()[0].id);
2680 EXPECT_TRUE(!strcmp(cricket::kGoogleSctpDataCodecName,
2681 ch->send_codecs()[0].name.c_str()));
2682 EXPECT_TRUE(ch->send_codecs()[0].GetParam(cricket::kCodecParamPort,
2683 &portnum));
2684 EXPECT_EQ(new_send_port, portnum);
2685
2686 ASSERT_EQ(1UL, ch->recv_codecs().size());
2687 EXPECT_EQ(cricket::kGoogleSctpDataCodecId, ch->recv_codecs()[0].id);
2688 EXPECT_TRUE(!strcmp(cricket::kGoogleSctpDataCodecName,
2689 ch->recv_codecs()[0].name.c_str()));
2690 EXPECT_TRUE(ch->recv_codecs()[0].GetParam(cricket::kCodecParamPort,
2691 &portnum));
2692 EXPECT_EQ(new_recv_port, portnum);
2693}
2694
wu@webrtc.org91053e72013-08-10 07:18:04 +00002695// Verifies that CreateOffer succeeds when CreateOffer is called before async
2696// identity generation is finished.
2697TEST_F(WebRtcSessionTest, TestCreateOfferBeforeIdentityRequestReturnSuccess) {
2698 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org7666db72013-08-22 14:45:42 +00002699 InitWithDtls(false);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002700
2701 EXPECT_TRUE(session_->waiting_for_identity());
2702 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateOffer(NULL));
2703 EXPECT_TRUE(offer != NULL);
2704}
2705
2706// Verifies that CreateAnswer succeeds when CreateOffer is called before async
2707// identity generation is finished.
2708TEST_F(WebRtcSessionTest, TestCreateAnswerBeforeIdentityRequestReturnSuccess) {
2709 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org7666db72013-08-22 14:45:42 +00002710 InitWithDtls(false);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002711
2712 cricket::MediaSessionOptions options;
2713 scoped_ptr<JsepSessionDescription> offer(
2714 CreateRemoteOffer(options, cricket::SEC_REQUIRED));
2715 ASSERT_TRUE(offer.get() != NULL);
2716 SetRemoteDescriptionWithoutError(offer.release());
2717
2718 talk_base::scoped_ptr<SessionDescriptionInterface> answer(CreateAnswer(NULL));
2719 EXPECT_TRUE(answer != NULL);
2720}
2721
2722// Verifies that CreateOffer succeeds when CreateOffer is called after async
2723// identity generation is finished.
2724TEST_F(WebRtcSessionTest, TestCreateOfferAfterIdentityRequestReturnSuccess) {
2725 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org7666db72013-08-22 14:45:42 +00002726 InitWithDtls(false);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002727
2728 EXPECT_TRUE_WAIT(!session_->waiting_for_identity(), 1000);
2729 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateOffer(NULL));
2730 EXPECT_TRUE(offer != NULL);
2731}
2732
2733// Verifies that CreateOffer fails when CreateOffer is called after async
2734// identity generation fails.
2735TEST_F(WebRtcSessionTest, TestCreateOfferAfterIdentityRequestReturnFailure) {
2736 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org7666db72013-08-22 14:45:42 +00002737 InitWithDtls(true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002738
2739 EXPECT_TRUE_WAIT(!session_->waiting_for_identity(), 1000);
2740 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateOffer(NULL));
2741 EXPECT_TRUE(offer == NULL);
2742}
2743
2744// Verifies that CreateOffer succeeds when Multiple CreateOffer calls are made
2745// before async identity generation is finished.
2746TEST_F(WebRtcSessionTest,
2747 TestMultipleCreateOfferBeforeIdentityRequestReturnSuccess) {
2748 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2749 VerifyMultipleAsyncCreateDescription(
2750 true, CreateSessionDescriptionRequest::kOffer);
2751}
2752
2753// Verifies that CreateOffer fails when Multiple CreateOffer calls are made
2754// before async identity generation fails.
2755TEST_F(WebRtcSessionTest,
2756 TestMultipleCreateOfferBeforeIdentityRequestReturnFailure) {
2757 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2758 VerifyMultipleAsyncCreateDescription(
2759 false, CreateSessionDescriptionRequest::kOffer);
2760}
2761
2762// Verifies that CreateAnswer succeeds when Multiple CreateAnswer calls are made
2763// before async identity generation is finished.
2764TEST_F(WebRtcSessionTest,
2765 TestMultipleCreateAnswerBeforeIdentityRequestReturnSuccess) {
2766 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2767 VerifyMultipleAsyncCreateDescription(
2768 true, CreateSessionDescriptionRequest::kAnswer);
2769}
2770
2771// Verifies that CreateAnswer fails when Multiple CreateAnswer calls are made
2772// before async identity generation fails.
2773TEST_F(WebRtcSessionTest,
2774 TestMultipleCreateAnswerBeforeIdentityRequestReturnFailure) {
2775 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2776 VerifyMultipleAsyncCreateDescription(
2777 false, CreateSessionDescriptionRequest::kAnswer);
2778}
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002779
2780// Verifies that setRemoteDescription fails when DTLS is disabled and the remote
2781// offer has no SDES crypto but only DTLS fingerprint.
2782TEST_F(WebRtcSessionTest, TestSetRemoteOfferFailIfDtlsDisabledAndNoCrypto) {
2783 // Init without DTLS.
2784 Init(NULL);
2785 // Create a remote offer with secured transport disabled.
2786 cricket::MediaSessionOptions options;
2787 JsepSessionDescription* offer(CreateRemoteOffer(
2788 options, cricket::SEC_DISABLED));
2789 // Adds a DTLS fingerprint to the remote offer.
2790 cricket::SessionDescription* sdp = offer->description();
2791 TransportInfo* audio = sdp->GetTransportInfoByName("audio");
2792 ASSERT_TRUE(audio != NULL);
2793 ASSERT_TRUE(audio->description.identity_fingerprint.get() == NULL);
2794 audio->description.identity_fingerprint.reset(
2795 talk_base::SSLFingerprint::CreateFromRfc4572(
2796 talk_base::DIGEST_SHA_256, kFakeDtlsFingerprint));
2797 SetRemoteDescriptionExpectError(kSdpWithoutSdesAndDtlsDisabled,
2798 offer);
2799}
2800
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002801// TODO(bemasc): Add a TestIceStatesBundle with BUNDLE enabled. That test
2802// currently fails because upon disconnection and reconnection OnIceComplete is
2803// called more than once without returning to IceGatheringGathering.