blob: 64acad0e18612c1081621017a4823ece374769f8 [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;
wu@webrtc.org97077a32013-10-25 21:18:33 +000084using webrtc::PeerConnectionFactoryInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085using webrtc::PeerConnectionInterface;
86using webrtc::SessionDescriptionInterface;
87using webrtc::StreamCollection;
wu@webrtc.org91053e72013-08-10 07:18:04 +000088using webrtc::WebRtcSession;
wu@webrtc.org364f2042013-11-20 21:49:41 +000089using webrtc::kBundleWithoutRtcpMux;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000090using webrtc::kCreateChannelFailed;
91using webrtc::kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092using webrtc::kMlineMismatch;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000093using webrtc::kPushDownTDFailed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094using webrtc::kSdpWithoutCrypto;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000095using webrtc::kSdpWithoutIceUfragPwd;
wu@webrtc.org364f2042013-11-20 21:49:41 +000096using webrtc::kSdpWithoutSdesAndDtlsDisabled;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097using webrtc::kSessionError;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000098using webrtc::kSessionErrorDesc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
wu@webrtc.org364f2042013-11-20 21:49:41 +0000100static const int kClientAddrPort = 0;
101static const char kClientAddrHost1[] = "11.11.11.11";
102static const char kClientAddrHost2[] = "22.22.22.22";
103static const char kStunAddrHost[] = "99.99.99.1";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
105static const char kSessionVersion[] = "1";
106
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107// Media index of candidates belonging to the first media content.
108static const int kMediaContentIndex0 = 0;
109static const char kMediaContentName0[] = "audio";
110
111// Media index of candidates belonging to the second media content.
112static const int kMediaContentIndex1 = 1;
113static const char kMediaContentName1[] = "video";
114
115static const int kIceCandidatesTimeout = 10000;
116
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000117static const char kFakeDtlsFingerprint[] =
118 "BB:CD:72:F7:2F:D0:BA:43:F3:68:B1:0C:23:72:B6:4A:"
119 "0F:DE:34:06:BC:E0:FE:01:BC:73:C8:6D:F4:65:D5:24";
120
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121// Add some extra |newlines| to the |message| after |line|.
122static void InjectAfter(const std::string& line,
123 const std::string& newlines,
124 std::string* message) {
125 const std::string tmp = line + newlines;
126 talk_base::replace_substrs(line.c_str(), line.length(),
127 tmp.c_str(), tmp.length(), message);
128}
129
130class MockIceObserver : public webrtc::IceObserver {
131 public:
132 MockIceObserver()
133 : oncandidatesready_(false),
134 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
135 ice_gathering_state_(PeerConnectionInterface::kIceGatheringNew) {
136 }
137
138 virtual void OnIceConnectionChange(
139 PeerConnectionInterface::IceConnectionState new_state) {
140 ice_connection_state_ = new_state;
141 }
142 virtual void OnIceGatheringChange(
143 PeerConnectionInterface::IceGatheringState new_state) {
144 // We can never transition back to "new".
145 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, new_state);
146 ice_gathering_state_ = new_state;
147
148 // oncandidatesready_ really means "ICE gathering is complete".
149 // This if statement ensures that this value remains correct when we
150 // transition from kIceGatheringComplete to kIceGatheringGathering.
151 if (new_state == PeerConnectionInterface::kIceGatheringGathering) {
152 oncandidatesready_ = false;
153 }
154 }
155
156 // Found a new candidate.
157 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000158 switch (candidate->sdp_mline_index()) {
159 case kMediaContentIndex0:
160 mline_0_candidates_.push_back(candidate->candidate());
161 break;
162 case kMediaContentIndex1:
163 mline_1_candidates_.push_back(candidate->candidate());
164 break;
165 default:
166 ASSERT(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 }
wu@webrtc.org364f2042013-11-20 21:49:41 +0000168
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 // The ICE gathering state should always be Gathering when a candidate is
170 // received (or possibly Completed in the case of the final candidate).
171 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, ice_gathering_state_);
172 }
173
174 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
175 virtual void OnIceComplete() {
176 EXPECT_FALSE(oncandidatesready_);
177 oncandidatesready_ = true;
178
179 // OnIceGatheringChange(IceGatheringCompleted) and OnIceComplete() should
180 // be called approximately simultaneously. For ease of testing, this
181 // check additionally requires that they be called in the above order.
182 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
183 ice_gathering_state_);
184 }
185
186 bool oncandidatesready_;
187 std::vector<cricket::Candidate> mline_0_candidates_;
188 std::vector<cricket::Candidate> mline_1_candidates_;
189 PeerConnectionInterface::IceConnectionState ice_connection_state_;
190 PeerConnectionInterface::IceGatheringState ice_gathering_state_;
191};
192
193class WebRtcSessionForTest : public webrtc::WebRtcSession {
194 public:
195 WebRtcSessionForTest(cricket::ChannelManager* cmgr,
196 talk_base::Thread* signaling_thread,
197 talk_base::Thread* worker_thread,
198 cricket::PortAllocator* port_allocator,
199 webrtc::IceObserver* ice_observer,
200 webrtc::MediaStreamSignaling* mediastream_signaling)
201 : WebRtcSession(cmgr, signaling_thread, worker_thread, port_allocator,
202 mediastream_signaling) {
203 RegisterIceObserver(ice_observer);
204 }
205 virtual ~WebRtcSessionForTest() {}
206
207 using cricket::BaseSession::GetTransportProxy;
208 using webrtc::WebRtcSession::SetAudioPlayout;
209 using webrtc::WebRtcSession::SetAudioSend;
210 using webrtc::WebRtcSession::SetCaptureDevice;
211 using webrtc::WebRtcSession::SetVideoPlayout;
212 using webrtc::WebRtcSession::SetVideoSend;
213};
214
wu@webrtc.org91053e72013-08-10 07:18:04 +0000215class WebRtcSessionCreateSDPObserverForTest
216 : public talk_base::RefCountedObject<CreateSessionDescriptionObserver> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 public:
wu@webrtc.org91053e72013-08-10 07:18:04 +0000218 enum State {
219 kInit,
220 kFailed,
221 kSucceeded,
222 };
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000223 WebRtcSessionCreateSDPObserverForTest() : state_(kInit) {}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000224
225 // CreateSessionDescriptionObserver implementation.
226 virtual void OnSuccess(SessionDescriptionInterface* desc) {
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000227 description_.reset(desc);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000228 state_ = kSucceeded;
229 }
230 virtual void OnFailure(const std::string& error) {
231 state_ = kFailed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 }
233
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000234 SessionDescriptionInterface* description() { return description_.get(); }
235
236 SessionDescriptionInterface* ReleaseDescription() {
237 return description_.release();
238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239
wu@webrtc.org91053e72013-08-10 07:18:04 +0000240 State state() const { return state_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241
wu@webrtc.org91053e72013-08-10 07:18:04 +0000242 protected:
243 ~WebRtcSessionCreateSDPObserverForTest() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244
245 private:
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000246 talk_base::scoped_ptr<SessionDescriptionInterface> description_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000247 State state_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248};
249
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000250class FakeAudioRenderer : public cricket::AudioRenderer {
251 public:
henrike@webrtc.org26438052014-02-20 22:32:53 +0000252 FakeAudioRenderer() : channel_id_(-1), sink_(NULL) {}
253 virtual ~FakeAudioRenderer() {
254 if (sink_)
255 sink_->OnClose();
256 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000257
258 virtual void AddChannel(int channel_id) OVERRIDE {
259 ASSERT(channel_id_ == -1);
260 channel_id_ = channel_id;
261 }
262 virtual void RemoveChannel(int channel_id) OVERRIDE {
263 ASSERT(channel_id == channel_id_);
264 channel_id_ = -1;
265 }
henrike@webrtc.org26438052014-02-20 22:32:53 +0000266 virtual void SetSink(Sink* sink) OVERRIDE {
267 sink_ = sink;
268 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000269
270 int channel_id() const { return channel_id_; }
henrike@webrtc.org26438052014-02-20 22:32:53 +0000271 cricket::AudioRenderer::Sink* sink() const { return sink_; }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000272 private:
273 int channel_id_;
henrike@webrtc.org26438052014-02-20 22:32:53 +0000274 cricket::AudioRenderer::Sink* sink_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000275};
276
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277class WebRtcSessionTest : public testing::Test {
278 protected:
279 // TODO Investigate why ChannelManager crashes, if it's created
280 // after stun_server.
281 WebRtcSessionTest()
282 : media_engine_(new cricket::FakeMediaEngine()),
283 data_engine_(new cricket::FakeDataEngine()),
284 device_manager_(new cricket::FakeDeviceManager()),
285 channel_manager_(new cricket::ChannelManager(
286 media_engine_, data_engine_, device_manager_,
287 new cricket::CaptureManager(), talk_base::Thread::Current())),
288 tdesc_factory_(new cricket::TransportDescriptionFactory()),
289 desc_factory_(new cricket::MediaSessionDescriptionFactory(
290 channel_manager_.get(), tdesc_factory_.get())),
291 pss_(new talk_base::PhysicalSocketServer),
292 vss_(new talk_base::VirtualSocketServer(pss_.get())),
293 fss_(new talk_base::FirewallSocketServer(vss_.get())),
294 ss_scope_(fss_.get()),
wu@webrtc.org364f2042013-11-20 21:49:41 +0000295 stun_socket_addr_(talk_base::SocketAddress(kStunAddrHost,
296 cricket::STUN_SERVER_PORT)),
297 stun_server_(talk_base::Thread::Current(), stun_socket_addr_),
298 allocator_(&network_manager_, stun_socket_addr_,
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000299 SocketAddress(), SocketAddress(), SocketAddress()),
300 mediastream_signaling_(channel_manager_.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 tdesc_factory_->set_protocol(cricket::ICEPROTO_HYBRID);
302 allocator_.set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
303 cricket::PORTALLOCATOR_DISABLE_RELAY |
304 cricket::PORTALLOCATOR_ENABLE_BUNDLE);
305 EXPECT_TRUE(channel_manager_->Init());
306 desc_factory_->set_add_legacy_streams(false);
307 }
308
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000309 static void SetUpTestCase() {
310 talk_base::InitializeSSL();
311 }
312
313 static void TearDownTestCase() {
314 talk_base::CleanupSSL();
315 }
316
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 void AddInterface(const SocketAddress& addr) {
318 network_manager_.AddInterface(addr);
319 }
320
wu@webrtc.org91053e72013-08-10 07:18:04 +0000321 void Init(DTLSIdentityServiceInterface* identity_service) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 ASSERT_TRUE(session_.get() == NULL);
323 session_.reset(new WebRtcSessionForTest(
324 channel_manager_.get(), talk_base::Thread::Current(),
325 talk_base::Thread::Current(), &allocator_,
326 &observer_,
327 &mediastream_signaling_));
328
329 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
330 observer_.ice_connection_state_);
331 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
332 observer_.ice_gathering_state_);
333
wu@webrtc.org97077a32013-10-25 21:18:33 +0000334 EXPECT_TRUE(session_->Initialize(options_, constraints_.get(),
335 identity_service));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 }
337
338 void InitWithDtmfCodec() {
339 // Add kTelephoneEventCodec for dtmf test.
wu@webrtc.org364f2042013-11-20 21:49:41 +0000340 const cricket::AudioCodec kTelephoneEventCodec(
341 106, "telephone-event", 8000, 0, 1, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 std::vector<cricket::AudioCodec> codecs;
343 codecs.push_back(kTelephoneEventCodec);
344 media_engine_->SetAudioCodecs(codecs);
345 desc_factory_->set_audio_codecs(codecs);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000346 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 }
348
henrike@webrtc.org7666db72013-08-22 14:45:42 +0000349 void InitWithDtls(bool identity_request_should_fail = false) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000350 FakeIdentityService* identity_service = new FakeIdentityService();
351 identity_service->set_should_fail(identity_request_should_fail);
352 Init(identity_service);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 }
354
355 // Creates a local offer and applies it. Starts ice.
356 // Call mediastream_signaling_.UseOptionsWithStreamX() before this function
357 // to decide which streams to create.
358 void InitiateCall() {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000359 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 SetLocalDescriptionWithoutError(offer);
361 EXPECT_TRUE_WAIT(PeerConnectionInterface::kIceGatheringNew !=
362 observer_.ice_gathering_state_,
363 kIceCandidatesTimeout);
364 }
365
wu@webrtc.org91053e72013-08-10 07:18:04 +0000366 SessionDescriptionInterface* CreateOffer(
367 const webrtc::MediaConstraintsInterface* constraints) {
368 talk_base::scoped_refptr<WebRtcSessionCreateSDPObserverForTest>
369 observer = new WebRtcSessionCreateSDPObserverForTest();
370 session_->CreateOffer(observer, constraints);
371 EXPECT_TRUE_WAIT(
372 observer->state() != WebRtcSessionCreateSDPObserverForTest::kInit,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000373 2000);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000374 return observer->ReleaseDescription();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000375 }
376
377 SessionDescriptionInterface* CreateAnswer(
378 const webrtc::MediaConstraintsInterface* constraints) {
379 talk_base::scoped_refptr<WebRtcSessionCreateSDPObserverForTest> observer
380 = new WebRtcSessionCreateSDPObserverForTest();
381 session_->CreateAnswer(observer, constraints);
382 EXPECT_TRUE_WAIT(
383 observer->state() != WebRtcSessionCreateSDPObserverForTest::kInit,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000384 2000);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000385 return observer->ReleaseDescription();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000386 }
387
wu@webrtc.org364f2042013-11-20 21:49:41 +0000388 bool ChannelsExist() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 return (session_->voice_channel() != NULL &&
390 session_->video_channel() != NULL);
391 }
392
wu@webrtc.org364f2042013-11-20 21:49:41 +0000393 void CheckTransportChannels() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 EXPECT_TRUE(session_->GetChannel(cricket::CN_AUDIO, 1) != NULL);
395 EXPECT_TRUE(session_->GetChannel(cricket::CN_AUDIO, 2) != NULL);
396 EXPECT_TRUE(session_->GetChannel(cricket::CN_VIDEO, 1) != NULL);
397 EXPECT_TRUE(session_->GetChannel(cricket::CN_VIDEO, 2) != NULL);
398 }
399
400 void VerifyCryptoParams(const cricket::SessionDescription* sdp) {
401 ASSERT_TRUE(session_.get() != NULL);
402 const cricket::ContentInfo* content = cricket::GetFirstAudioContent(sdp);
403 ASSERT_TRUE(content != NULL);
404 const cricket::AudioContentDescription* audio_content =
405 static_cast<const cricket::AudioContentDescription*>(
406 content->description);
407 ASSERT_TRUE(audio_content != NULL);
408 ASSERT_EQ(1U, audio_content->cryptos().size());
409 ASSERT_EQ(47U, audio_content->cryptos()[0].key_params.size());
410 ASSERT_EQ("AES_CM_128_HMAC_SHA1_80",
411 audio_content->cryptos()[0].cipher_suite);
412 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
413 audio_content->protocol());
414
415 content = cricket::GetFirstVideoContent(sdp);
416 ASSERT_TRUE(content != NULL);
417 const cricket::VideoContentDescription* video_content =
418 static_cast<const cricket::VideoContentDescription*>(
419 content->description);
420 ASSERT_TRUE(video_content != NULL);
421 ASSERT_EQ(1U, video_content->cryptos().size());
422 ASSERT_EQ("AES_CM_128_HMAC_SHA1_80",
423 video_content->cryptos()[0].cipher_suite);
424 ASSERT_EQ(47U, video_content->cryptos()[0].key_params.size());
425 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
426 video_content->protocol());
427 }
428
429 void VerifyNoCryptoParams(const cricket::SessionDescription* sdp, bool dtls) {
430 const cricket::ContentInfo* content = cricket::GetFirstAudioContent(sdp);
431 ASSERT_TRUE(content != NULL);
432 const cricket::AudioContentDescription* audio_content =
433 static_cast<const cricket::AudioContentDescription*>(
434 content->description);
435 ASSERT_TRUE(audio_content != NULL);
436 ASSERT_EQ(0U, audio_content->cryptos().size());
437
438 content = cricket::GetFirstVideoContent(sdp);
439 ASSERT_TRUE(content != NULL);
440 const cricket::VideoContentDescription* video_content =
441 static_cast<const cricket::VideoContentDescription*>(
442 content->description);
443 ASSERT_TRUE(video_content != NULL);
444 ASSERT_EQ(0U, video_content->cryptos().size());
445
446 if (dtls) {
447 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
448 audio_content->protocol());
449 EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf),
450 video_content->protocol());
451 } else {
452 EXPECT_EQ(std::string(cricket::kMediaProtocolAvpf),
453 audio_content->protocol());
454 EXPECT_EQ(std::string(cricket::kMediaProtocolAvpf),
455 video_content->protocol());
456 }
457 }
458
459 // Set the internal fake description factories to do DTLS-SRTP.
460 void SetFactoryDtlsSrtp() {
461 desc_factory_->set_secure(cricket::SEC_ENABLED);
462 std::string identity_name = "WebRTC" +
463 talk_base::ToString(talk_base::CreateRandomId());
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000464 identity_.reset(talk_base::SSLIdentity::Generate(identity_name));
465 tdesc_factory_->set_identity(identity_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 tdesc_factory_->set_secure(cricket::SEC_REQUIRED);
467 }
468
469 void VerifyFingerprintStatus(const cricket::SessionDescription* sdp,
470 bool expected) {
471 const TransportInfo* audio = sdp->GetTransportInfoByName("audio");
472 ASSERT_TRUE(audio != NULL);
473 ASSERT_EQ(expected, audio->description.identity_fingerprint.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 const TransportInfo* video = sdp->GetTransportInfoByName("video");
475 ASSERT_TRUE(video != NULL);
476 ASSERT_EQ(expected, video->description.identity_fingerprint.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 }
478
479 void VerifyAnswerFromNonCryptoOffer() {
480 // Create a SDP without Crypto.
481 cricket::MediaSessionOptions options;
482 options.has_video = true;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000483 JsepSessionDescription* offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 CreateRemoteOffer(options, cricket::SEC_DISABLED));
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000485 ASSERT_TRUE(offer != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 VerifyNoCryptoParams(offer->description(), false);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000487 SetRemoteDescriptionOfferExpectError(
488 "Called with a SDP without crypto enabled", offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000489 const webrtc::SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 // Answer should be NULL as no crypto params in offer.
491 ASSERT_TRUE(answer == NULL);
492 }
493
494 void VerifyAnswerFromCryptoOffer() {
495 cricket::MediaSessionOptions options;
496 options.has_video = true;
497 options.bundle_enabled = true;
498 scoped_ptr<JsepSessionDescription> offer(
499 CreateRemoteOffer(options, cricket::SEC_REQUIRED));
500 ASSERT_TRUE(offer.get() != NULL);
501 VerifyCryptoParams(offer->description());
502 SetRemoteDescriptionWithoutError(offer.release());
wu@webrtc.org91053e72013-08-10 07:18:04 +0000503 scoped_ptr<SessionDescriptionInterface> answer(CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 ASSERT_TRUE(answer.get() != NULL);
505 VerifyCryptoParams(answer->description());
506 }
507
508 void CompareIceUfragAndPassword(const cricket::SessionDescription* desc1,
509 const cricket::SessionDescription* desc2,
510 bool expect_equal) {
511 if (desc1->contents().size() != desc2->contents().size()) {
512 EXPECT_FALSE(expect_equal);
513 return;
514 }
515
516 const cricket::ContentInfos& contents = desc1->contents();
517 cricket::ContentInfos::const_iterator it = contents.begin();
518
519 for (; it != contents.end(); ++it) {
520 const cricket::TransportDescription* transport_desc1 =
521 desc1->GetTransportDescriptionByName(it->name);
522 const cricket::TransportDescription* transport_desc2 =
523 desc2->GetTransportDescriptionByName(it->name);
524 if (!transport_desc1 || !transport_desc2) {
525 EXPECT_FALSE(expect_equal);
526 return;
527 }
528 if (transport_desc1->ice_pwd != transport_desc2->ice_pwd ||
529 transport_desc1->ice_ufrag != transport_desc2->ice_ufrag) {
530 EXPECT_FALSE(expect_equal);
531 return;
532 }
533 }
534 EXPECT_TRUE(expect_equal);
535 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000536
537 void RemoveIceUfragPwdLines(const SessionDescriptionInterface* current_desc,
538 std::string *sdp) {
539 const cricket::SessionDescription* desc = current_desc->description();
540 EXPECT_TRUE(current_desc->ToString(sdp));
541
542 const cricket::ContentInfos& contents = desc->contents();
543 cricket::ContentInfos::const_iterator it = contents.begin();
544 // Replace ufrag and pwd lines with empty strings.
545 for (; it != contents.end(); ++it) {
546 const cricket::TransportDescription* transport_desc =
547 desc->GetTransportDescriptionByName(it->name);
548 std::string ufrag_line = "a=ice-ufrag:" + transport_desc->ice_ufrag
549 + "\r\n";
550 std::string pwd_line = "a=ice-pwd:" + transport_desc->ice_pwd
551 + "\r\n";
552 talk_base::replace_substrs(ufrag_line.c_str(), ufrag_line.length(),
553 "", 0,
554 sdp);
555 talk_base::replace_substrs(pwd_line.c_str(), pwd_line.length(),
556 "", 0,
557 sdp);
558 }
559 }
560
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 // Creates a remote offer and and applies it as a remote description,
562 // creates a local answer and applies is as a local description.
563 // Call mediastream_signaling_.UseOptionsWithStreamX() before this function
564 // to decide which local and remote streams to create.
565 void CreateAndSetRemoteOfferAndLocalAnswer() {
566 SessionDescriptionInterface* offer = CreateRemoteOffer();
567 SetRemoteDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000568 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 SetLocalDescriptionWithoutError(answer);
570 }
571 void SetLocalDescriptionWithoutError(SessionDescriptionInterface* desc) {
572 EXPECT_TRUE(session_->SetLocalDescription(desc, NULL));
573 }
574 void SetLocalDescriptionExpectState(SessionDescriptionInterface* desc,
575 BaseSession::State expected_state) {
576 SetLocalDescriptionWithoutError(desc);
577 EXPECT_EQ(expected_state, session_->state());
578 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000579 void SetLocalDescriptionExpectError(const std::string& action,
580 const std::string& expected_error,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 SessionDescriptionInterface* desc) {
582 std::string error;
583 EXPECT_FALSE(session_->SetLocalDescription(desc, &error));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000584 std::string sdp_type = "local ";
585 sdp_type.append(action);
586 EXPECT_NE(std::string::npos, error.find(sdp_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 EXPECT_NE(std::string::npos, error.find(expected_error));
588 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000589 void SetLocalDescriptionOfferExpectError(const std::string& expected_error,
590 SessionDescriptionInterface* desc) {
591 SetLocalDescriptionExpectError(SessionDescriptionInterface::kOffer,
592 expected_error, desc);
593 }
594 void SetLocalDescriptionAnswerExpectError(const std::string& expected_error,
595 SessionDescriptionInterface* desc) {
596 SetLocalDescriptionExpectError(SessionDescriptionInterface::kAnswer,
597 expected_error, desc);
598 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 void SetRemoteDescriptionWithoutError(SessionDescriptionInterface* desc) {
600 EXPECT_TRUE(session_->SetRemoteDescription(desc, NULL));
601 }
602 void SetRemoteDescriptionExpectState(SessionDescriptionInterface* desc,
603 BaseSession::State expected_state) {
604 SetRemoteDescriptionWithoutError(desc);
605 EXPECT_EQ(expected_state, session_->state());
606 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000607 void SetRemoteDescriptionExpectError(const std::string& action,
608 const std::string& expected_error,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 SessionDescriptionInterface* desc) {
610 std::string error;
611 EXPECT_FALSE(session_->SetRemoteDescription(desc, &error));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000612 std::string sdp_type = "remote ";
613 sdp_type.append(action);
614 EXPECT_NE(std::string::npos, error.find(sdp_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 EXPECT_NE(std::string::npos, error.find(expected_error));
616 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000617 void SetRemoteDescriptionOfferExpectError(
618 const std::string& expected_error, SessionDescriptionInterface* desc) {
619 SetRemoteDescriptionExpectError(SessionDescriptionInterface::kOffer,
620 expected_error, desc);
621 }
622 void SetRemoteDescriptionPranswerExpectError(
623 const std::string& expected_error, SessionDescriptionInterface* desc) {
624 SetRemoteDescriptionExpectError(SessionDescriptionInterface::kPrAnswer,
625 expected_error, desc);
626 }
627 void SetRemoteDescriptionAnswerExpectError(
628 const std::string& expected_error, SessionDescriptionInterface* desc) {
629 SetRemoteDescriptionExpectError(SessionDescriptionInterface::kAnswer,
630 expected_error, desc);
631 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632
633 void CreateCryptoOfferAndNonCryptoAnswer(SessionDescriptionInterface** offer,
634 SessionDescriptionInterface** nocrypto_answer) {
635 // Create a SDP without Crypto.
636 cricket::MediaSessionOptions options;
637 options.has_video = true;
638 options.bundle_enabled = true;
639 *offer = CreateRemoteOffer(options, cricket::SEC_ENABLED);
640 ASSERT_TRUE(*offer != NULL);
641 VerifyCryptoParams((*offer)->description());
642
643 *nocrypto_answer = CreateRemoteAnswer(*offer, options,
644 cricket::SEC_DISABLED);
645 EXPECT_TRUE(*nocrypto_answer != NULL);
646 }
647
648 JsepSessionDescription* CreateRemoteOfferWithVersion(
649 cricket::MediaSessionOptions options,
650 cricket::SecurePolicy secure_policy,
651 const std::string& session_version,
652 const SessionDescriptionInterface* current_desc) {
653 std::string session_id = talk_base::ToString(talk_base::CreateRandomId64());
654 const cricket::SessionDescription* cricket_desc = NULL;
655 if (current_desc) {
656 cricket_desc = current_desc->description();
657 session_id = current_desc->session_id();
658 }
659
660 desc_factory_->set_secure(secure_policy);
661 JsepSessionDescription* offer(
662 new JsepSessionDescription(JsepSessionDescription::kOffer));
663 if (!offer->Initialize(desc_factory_->CreateOffer(options, cricket_desc),
664 session_id, session_version)) {
665 delete offer;
666 offer = NULL;
667 }
668 return offer;
669 }
670 JsepSessionDescription* CreateRemoteOffer(
671 cricket::MediaSessionOptions options) {
672 return CreateRemoteOfferWithVersion(options, cricket::SEC_ENABLED,
673 kSessionVersion, NULL);
674 }
675 JsepSessionDescription* CreateRemoteOffer(
676 cricket::MediaSessionOptions options, cricket::SecurePolicy policy) {
677 return CreateRemoteOfferWithVersion(options, policy, kSessionVersion, NULL);
678 }
679 JsepSessionDescription* CreateRemoteOffer(
680 cricket::MediaSessionOptions options,
681 const SessionDescriptionInterface* current_desc) {
682 return CreateRemoteOfferWithVersion(options, cricket::SEC_ENABLED,
683 kSessionVersion, current_desc);
684 }
685
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000686 JsepSessionDescription* CreateRemoteOfferWithSctpPort(
687 const char* sctp_stream_name, int new_port,
688 cricket::MediaSessionOptions options) {
689 options.data_channel_type = cricket::DCT_SCTP;
690 options.AddStream(cricket::MEDIA_TYPE_DATA, "datachannel",
691 sctp_stream_name);
692 return ChangeSDPSctpPort(new_port, CreateRemoteOffer(options));
693 }
694
695 // Takes ownership of offer_basis (and deletes it).
696 JsepSessionDescription* ChangeSDPSctpPort(
697 int new_port, webrtc::SessionDescriptionInterface *offer_basis) {
698 // Stringify the input SDP, swap the 5000 for 'new_port' and create a new
699 // SessionDescription from the mutated string.
700 const char* default_port_str = "5000";
701 char new_port_str[16];
702 talk_base::sprintfn(new_port_str, sizeof(new_port_str), "%d", new_port);
703 std::string offer_str;
704 offer_basis->ToString(&offer_str);
705 talk_base::replace_substrs(default_port_str, strlen(default_port_str),
706 new_port_str, strlen(new_port_str),
707 &offer_str);
708 JsepSessionDescription* offer = new JsepSessionDescription(
709 offer_basis->type());
710 delete offer_basis;
711 offer->Initialize(offer_str, NULL);
712 return offer;
713 }
714
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 // Create a remote offer. Call mediastream_signaling_.UseOptionsWithStreamX()
716 // before this function to decide which streams to create.
717 JsepSessionDescription* CreateRemoteOffer() {
718 cricket::MediaSessionOptions options;
719 mediastream_signaling_.GetOptionsForAnswer(NULL, &options);
720 return CreateRemoteOffer(options, session_->remote_description());
721 }
722
723 JsepSessionDescription* CreateRemoteAnswer(
724 const SessionDescriptionInterface* offer,
725 cricket::MediaSessionOptions options,
726 cricket::SecurePolicy policy) {
727 desc_factory_->set_secure(policy);
728 const std::string session_id =
729 talk_base::ToString(talk_base::CreateRandomId64());
730 JsepSessionDescription* answer(
731 new JsepSessionDescription(JsepSessionDescription::kAnswer));
732 if (!answer->Initialize(desc_factory_->CreateAnswer(offer->description(),
733 options, NULL),
734 session_id, kSessionVersion)) {
735 delete answer;
736 answer = NULL;
737 }
738 return answer;
739 }
740
741 JsepSessionDescription* CreateRemoteAnswer(
742 const SessionDescriptionInterface* offer,
743 cricket::MediaSessionOptions options) {
744 return CreateRemoteAnswer(offer, options, cricket::SEC_REQUIRED);
745 }
746
747 // Creates an answer session description with streams based on
748 // |mediastream_signaling_|. Call
749 // mediastream_signaling_.UseOptionsWithStreamX() before this function
750 // to decide which streams to create.
751 JsepSessionDescription* CreateRemoteAnswer(
752 const SessionDescriptionInterface* offer) {
753 cricket::MediaSessionOptions options;
754 mediastream_signaling_.GetOptionsForAnswer(NULL, &options);
755 return CreateRemoteAnswer(offer, options, cricket::SEC_REQUIRED);
756 }
757
758 void TestSessionCandidatesWithBundleRtcpMux(bool bundle, bool rtcp_mux) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000759 AddInterface(talk_base::SocketAddress(kClientAddrHost1, kClientAddrPort));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000760 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 mediastream_signaling_.SendAudioVideoStream1();
762 FakeConstraints constraints;
763 constraints.SetMandatoryUseRtpMux(bundle);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000764 SessionDescriptionInterface* offer = CreateOffer(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
766 // and answer.
767 SetLocalDescriptionWithoutError(offer);
768
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000769 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
770 CreateRemoteAnswer(session_->local_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 std::string sdp;
772 EXPECT_TRUE(answer->ToString(&sdp));
773
774 size_t expected_candidate_num = 2;
775 if (!rtcp_mux) {
776 // If rtcp_mux is enabled we should expect 4 candidates - host and srflex
777 // for rtp and rtcp.
778 expected_candidate_num = 4;
779 // Disable rtcp-mux from the answer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 const std::string kRtcpMux = "a=rtcp-mux";
781 const std::string kXRtcpMux = "a=xrtcp-mux";
782 talk_base::replace_substrs(kRtcpMux.c_str(), kRtcpMux.length(),
783 kXRtcpMux.c_str(), kXRtcpMux.length(),
784 &sdp);
785 }
786
787 SessionDescriptionInterface* new_answer = CreateSessionDescription(
788 JsepSessionDescription::kAnswer, sdp, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789
790 // SetRemoteDescription to enable rtcp mux.
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000791 SetRemoteDescriptionWithoutError(new_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
793 EXPECT_EQ(expected_candidate_num, observer_.mline_0_candidates_.size());
794 EXPECT_EQ(expected_candidate_num, observer_.mline_1_candidates_.size());
795 for (size_t i = 0; i < observer_.mline_0_candidates_.size(); ++i) {
796 cricket::Candidate c0 = observer_.mline_0_candidates_[i];
797 cricket::Candidate c1 = observer_.mline_1_candidates_[i];
798 if (bundle) {
799 EXPECT_TRUE(c0.IsEquivalent(c1));
800 } else {
801 EXPECT_FALSE(c0.IsEquivalent(c1));
802 }
803 }
804 }
805 // Tests that we can only send DTMF when the dtmf codec is supported.
806 void TestCanInsertDtmf(bool can) {
807 if (can) {
808 InitWithDtmfCodec();
809 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000810 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 }
812 mediastream_signaling_.SendAudioVideoStream1();
813 CreateAndSetRemoteOfferAndLocalAnswer();
814 EXPECT_FALSE(session_->CanInsertDtmf(""));
815 EXPECT_EQ(can, session_->CanInsertDtmf(kAudioTrack1));
816 }
817
818 // The method sets up a call from the session to itself, in a loopback
819 // arrangement. It also uses a firewall rule to create a temporary
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000820 // disconnection, and then a permanent disconnection.
821 // This code is placed in a method so that it can be invoked
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 // by multiple tests with different allocators (e.g. with and without BUNDLE).
823 // While running the call, this method also checks if the session goes through
824 // the correct sequence of ICE states when a connection is established,
825 // broken, and re-established.
826 // The Connection state should go:
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000827 // New -> Checking -> (Connected) -> Completed -> Disconnected -> Completed
828 // -> Failed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 // The Gathering state should go: New -> Gathering -> Completed.
830 void TestLoopbackCall() {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000831 AddInterface(talk_base::SocketAddress(kClientAddrHost1, kClientAddrPort));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000832 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000834 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835
836 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
837 observer_.ice_gathering_state_);
838 SetLocalDescriptionWithoutError(offer);
839 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
840 observer_.ice_connection_state_);
841 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringGathering,
842 observer_.ice_gathering_state_,
843 kIceCandidatesTimeout);
844 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
845 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
846 observer_.ice_gathering_state_,
847 kIceCandidatesTimeout);
848
849 std::string sdp;
850 offer->ToString(&sdp);
851 SessionDescriptionInterface* desc =
852 webrtc::CreateSessionDescription(JsepSessionDescription::kAnswer, sdp);
853 ASSERT_TRUE(desc != NULL);
854 SetRemoteDescriptionWithoutError(desc);
855
856 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionChecking,
857 observer_.ice_connection_state_,
858 kIceCandidatesTimeout);
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000859 // The ice connection state is "Connected" too briefly to catch in a test.
860 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 observer_.ice_connection_state_,
862 kIceCandidatesTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863
864 // Adding firewall rule to block ping requests, which should cause
865 // transport channel failure.
wu@webrtc.org364f2042013-11-20 21:49:41 +0000866 fss_->AddRule(false,
867 talk_base::FP_ANY,
868 talk_base::FD_ANY,
869 talk_base::SocketAddress(kClientAddrHost1, kClientAddrPort));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
871 observer_.ice_connection_state_,
872 kIceCandidatesTimeout);
873
874 // Clearing the rules, session should move back to completed state.
875 fss_->ClearRules();
876 // Session is automatically calling OnSignalingReady after creation of
877 // new portallocator session which will allocate new set of candidates.
878
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000879 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 observer_.ice_connection_state_,
881 kIceCandidatesTimeout);
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000882
883 // Now we block ping requests and wait until the ICE connection transitions
884 // to the Failed state. This will take at least 30 seconds because it must
885 // wait for the Port to timeout.
886 int port_timeout = 30000;
887 fss_->AddRule(false,
888 talk_base::FP_ANY,
889 talk_base::FD_ANY,
890 talk_base::SocketAddress(kClientAddrHost1, kClientAddrPort));
891 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
892 observer_.ice_connection_state_,
893 kIceCandidatesTimeout + port_timeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 }
895
896 void VerifyTransportType(const std::string& content_name,
897 cricket::TransportProtocol protocol) {
898 const cricket::Transport* transport = session_->GetTransport(content_name);
899 ASSERT_TRUE(transport != NULL);
900 EXPECT_EQ(protocol, transport->protocol());
901 }
902
903 // Adds CN codecs to FakeMediaEngine and MediaDescriptionFactory.
904 void AddCNCodecs() {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000905 const cricket::AudioCodec kCNCodec1(102, "CN", 8000, 0, 1, 0);
906 const cricket::AudioCodec kCNCodec2(103, "CN", 16000, 0, 1, 0);
907
908 // Add kCNCodec for dtmf test.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 std::vector<cricket::AudioCodec> codecs = media_engine_->audio_codecs();;
910 codecs.push_back(kCNCodec1);
911 codecs.push_back(kCNCodec2);
912 media_engine_->SetAudioCodecs(codecs);
913 desc_factory_->set_audio_codecs(codecs);
914 }
915
916 bool VerifyNoCNCodecs(const cricket::ContentInfo* content) {
917 const cricket::ContentDescription* description = content->description;
918 ASSERT(description != NULL);
919 const cricket::AudioContentDescription* audio_content_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000920 static_cast<const cricket::AudioContentDescription*>(description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 ASSERT(audio_content_desc != NULL);
922 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
923 if (audio_content_desc->codecs()[i].name == "CN")
924 return false;
925 }
926 return true;
927 }
928
929 void SetLocalDescriptionWithDataChannel() {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000930 webrtc::InternalDataChannelInit dci;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 dci.reliable = false;
932 session_->CreateDataChannel("datachannel", &dci);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000933 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 SetLocalDescriptionWithoutError(offer);
935 }
936
wu@webrtc.org91053e72013-08-10 07:18:04 +0000937 void VerifyMultipleAsyncCreateDescription(
938 bool success, CreateSessionDescriptionRequest::Type type) {
henrike@webrtc.org7666db72013-08-22 14:45:42 +0000939 InitWithDtls(!success);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000940
941 if (type == CreateSessionDescriptionRequest::kAnswer) {
942 cricket::MediaSessionOptions options;
943 scoped_ptr<JsepSessionDescription> offer(
944 CreateRemoteOffer(options, cricket::SEC_REQUIRED));
945 ASSERT_TRUE(offer.get() != NULL);
946 SetRemoteDescriptionWithoutError(offer.release());
947 }
948
949 const int kNumber = 3;
950 talk_base::scoped_refptr<WebRtcSessionCreateSDPObserverForTest>
951 observers[kNumber];
952 for (int i = 0; i < kNumber; ++i) {
953 observers[i] = new WebRtcSessionCreateSDPObserverForTest();
954 if (type == CreateSessionDescriptionRequest::kOffer) {
955 session_->CreateOffer(observers[i], NULL);
956 } else {
957 session_->CreateAnswer(observers[i], NULL);
958 }
959 }
960
961 WebRtcSessionCreateSDPObserverForTest::State expected_state =
962 success ? WebRtcSessionCreateSDPObserverForTest::kSucceeded :
963 WebRtcSessionCreateSDPObserverForTest::kFailed;
964
965 for (int i = 0; i < kNumber; ++i) {
966 EXPECT_EQ_WAIT(expected_state, observers[i]->state(), 1000);
967 if (success) {
968 EXPECT_TRUE(observers[i]->description() != NULL);
969 } else {
970 EXPECT_TRUE(observers[i]->description() == NULL);
971 }
972 }
973 }
974
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 cricket::FakeMediaEngine* media_engine_;
976 cricket::FakeDataEngine* data_engine_;
977 cricket::FakeDeviceManager* device_manager_;
978 talk_base::scoped_ptr<cricket::ChannelManager> channel_manager_;
979 talk_base::scoped_ptr<cricket::TransportDescriptionFactory> tdesc_factory_;
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000980 talk_base::scoped_ptr<talk_base::SSLIdentity> identity_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 talk_base::scoped_ptr<cricket::MediaSessionDescriptionFactory> desc_factory_;
982 talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_;
983 talk_base::scoped_ptr<talk_base::VirtualSocketServer> vss_;
984 talk_base::scoped_ptr<talk_base::FirewallSocketServer> fss_;
985 talk_base::SocketServerScope ss_scope_;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000986 talk_base::SocketAddress stun_socket_addr_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 cricket::TestStunServer stun_server_;
988 talk_base::FakeNetworkManager network_manager_;
989 cricket::BasicPortAllocator allocator_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000990 PeerConnectionFactoryInterface::Options options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 talk_base::scoped_ptr<FakeConstraints> constraints_;
992 FakeMediaStreamSignaling mediastream_signaling_;
993 talk_base::scoped_ptr<WebRtcSessionForTest> session_;
994 MockIceObserver observer_;
995 cricket::FakeVideoMediaChannel* video_channel_;
996 cricket::FakeVoiceMediaChannel* voice_channel_;
997};
998
999TEST_F(WebRtcSessionTest, TestInitialize) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001000 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001}
1002
1003TEST_F(WebRtcSessionTest, TestInitializeWithDtls) {
1004 InitWithDtls();
1005}
1006
wu@webrtc.org91053e72013-08-10 07:18:04 +00001007// Verifies that WebRtcSession uses SEC_REQUIRED by default.
1008TEST_F(WebRtcSessionTest, TestDefaultSetSecurePolicy) {
1009 Init(NULL);
wu@webrtc.org364f2042013-11-20 21:49:41 +00001010 EXPECT_EQ(cricket::SEC_REQUIRED, session_->SecurePolicy());
wu@webrtc.org91053e72013-08-10 07:18:04 +00001011}
1012
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013TEST_F(WebRtcSessionTest, TestSessionCandidates) {
1014 TestSessionCandidatesWithBundleRtcpMux(false, false);
1015}
1016
1017// Below test cases (TestSessionCandidatesWith*) verify the candidates gathered
1018// with rtcp-mux and/or bundle.
1019TEST_F(WebRtcSessionTest, TestSessionCandidatesWithRtcpMux) {
1020 TestSessionCandidatesWithBundleRtcpMux(false, true);
1021}
1022
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023TEST_F(WebRtcSessionTest, TestSessionCandidatesWithBundleRtcpMux) {
1024 TestSessionCandidatesWithBundleRtcpMux(true, true);
1025}
1026
1027TEST_F(WebRtcSessionTest, TestMultihomeCandidates) {
wu@webrtc.org364f2042013-11-20 21:49:41 +00001028 AddInterface(talk_base::SocketAddress(kClientAddrHost1, kClientAddrPort));
1029 AddInterface(talk_base::SocketAddress(kClientAddrHost2, kClientAddrPort));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001030 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 mediastream_signaling_.SendAudioVideoStream1();
1032 InitiateCall();
1033 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1034 EXPECT_EQ(8u, observer_.mline_0_candidates_.size());
1035 EXPECT_EQ(8u, observer_.mline_1_candidates_.size());
1036}
1037
1038TEST_F(WebRtcSessionTest, TestStunError) {
wu@webrtc.org364f2042013-11-20 21:49:41 +00001039 AddInterface(talk_base::SocketAddress(kClientAddrHost1, kClientAddrPort));
1040 AddInterface(talk_base::SocketAddress(kClientAddrHost2, kClientAddrPort));
1041 fss_->AddRule(false,
1042 talk_base::FP_UDP,
1043 talk_base::FD_ANY,
1044 talk_base::SocketAddress(kClientAddrHost1, kClientAddrPort));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001045 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 mediastream_signaling_.SendAudioVideoStream1();
1047 InitiateCall();
wu@webrtc.org364f2042013-11-20 21:49:41 +00001048 // Since kClientAddrHost1 is blocked, not expecting stun candidates for it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1050 EXPECT_EQ(6u, observer_.mline_0_candidates_.size());
1051 EXPECT_EQ(6u, observer_.mline_1_candidates_.size());
1052}
1053
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001054TEST_F(WebRtcSessionTest, SetSdpFailedOnInvalidSdp) {
1055 Init(NULL);
1056 SessionDescriptionInterface* offer = NULL;
1057 // Since |offer| is NULL, there's no way to tell if it's an offer or answer.
1058 std::string unknown_action;
1059 SetLocalDescriptionExpectError(unknown_action, kInvalidSdp, offer);
1060 SetRemoteDescriptionExpectError(unknown_action, kInvalidSdp, offer);
1061}
1062
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063// Test creating offers and receive answers and make sure the
1064// media engine creates the expected send and receive streams.
1065TEST_F(WebRtcSessionTest, TestCreateOfferReceiveAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001066 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001068 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 const std::string session_id_orig = offer->session_id();
1070 const std::string session_version_orig = offer->session_version();
1071 SetLocalDescriptionWithoutError(offer);
1072
1073 mediastream_signaling_.SendAudioVideoStream2();
1074 SessionDescriptionInterface* answer =
1075 CreateRemoteAnswer(session_->local_description());
1076 SetRemoteDescriptionWithoutError(answer);
1077
1078 video_channel_ = media_engine_->GetVideoChannel(0);
1079 voice_channel_ = media_engine_->GetVoiceChannel(0);
1080
1081 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1082 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1083
1084 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1085 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1086
1087 ASSERT_EQ(1u, video_channel_->send_streams().size());
1088 EXPECT_TRUE(kVideoTrack1 == video_channel_->send_streams()[0].id);
1089 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1090 EXPECT_TRUE(kAudioTrack1 == voice_channel_->send_streams()[0].id);
1091
1092 // Create new offer without send streams.
1093 mediastream_signaling_.SendNothing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001094 offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095
1096 // Verify the session id is the same and the session version is
1097 // increased.
1098 EXPECT_EQ(session_id_orig, offer->session_id());
1099 EXPECT_LT(talk_base::FromString<uint64>(session_version_orig),
1100 talk_base::FromString<uint64>(offer->session_version()));
1101
1102 SetLocalDescriptionWithoutError(offer);
1103
1104 mediastream_signaling_.SendAudioVideoStream2();
1105 answer = CreateRemoteAnswer(session_->local_description());
1106 SetRemoteDescriptionWithoutError(answer);
1107
1108 EXPECT_EQ(0u, video_channel_->send_streams().size());
1109 EXPECT_EQ(0u, voice_channel_->send_streams().size());
1110
1111 // Make sure the receive streams have not changed.
1112 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1113 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1114 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1115 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1116}
1117
1118// Test receiving offers and creating answers and make sure the
1119// media engine creates the expected send and receive streams.
1120TEST_F(WebRtcSessionTest, TestReceiveOfferCreateAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001121 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 mediastream_signaling_.SendAudioVideoStream2();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001123 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 SetRemoteDescriptionWithoutError(offer);
1125
1126 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001127 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 SetLocalDescriptionWithoutError(answer);
1129
1130 const std::string session_id_orig = answer->session_id();
1131 const std::string session_version_orig = answer->session_version();
1132
1133 video_channel_ = media_engine_->GetVideoChannel(0);
1134 voice_channel_ = media_engine_->GetVoiceChannel(0);
1135
1136 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1137 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
1138
1139 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1140 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
1141
1142 ASSERT_EQ(1u, video_channel_->send_streams().size());
1143 EXPECT_TRUE(kVideoTrack1 == video_channel_->send_streams()[0].id);
1144 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1145 EXPECT_TRUE(kAudioTrack1 == voice_channel_->send_streams()[0].id);
1146
1147 mediastream_signaling_.SendAudioVideoStream1And2();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001148 offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149 SetRemoteDescriptionWithoutError(offer);
1150
1151 // Answer by turning off all send streams.
1152 mediastream_signaling_.SendNothing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001153 answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154
1155 // Verify the session id is the same and the session version is
1156 // increased.
1157 EXPECT_EQ(session_id_orig, answer->session_id());
1158 EXPECT_LT(talk_base::FromString<uint64>(session_version_orig),
1159 talk_base::FromString<uint64>(answer->session_version()));
1160 SetLocalDescriptionWithoutError(answer);
1161
1162 ASSERT_EQ(2u, video_channel_->recv_streams().size());
1163 EXPECT_TRUE(kVideoTrack1 == video_channel_->recv_streams()[0].id);
1164 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[1].id);
1165 ASSERT_EQ(2u, voice_channel_->recv_streams().size());
1166 EXPECT_TRUE(kAudioTrack1 == voice_channel_->recv_streams()[0].id);
1167 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[1].id);
1168
1169 // Make sure we have no send streams.
1170 EXPECT_EQ(0u, video_channel_->send_streams().size());
1171 EXPECT_EQ(0u, voice_channel_->send_streams().size());
1172}
1173
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001174TEST_F(WebRtcSessionTest, SetLocalSdpFailedOnCreateChannel) {
1175 Init(NULL);
1176 media_engine_->set_fail_create_channel(true);
1177
1178 SessionDescriptionInterface* offer = CreateOffer(NULL);
1179 ASSERT_TRUE(offer != NULL);
1180 // SetRemoteDescription and SetLocalDescription will take the ownership of
1181 // the offer.
1182 SetRemoteDescriptionOfferExpectError(kCreateChannelFailed, offer);
1183 offer = CreateOffer(NULL);
1184 ASSERT_TRUE(offer != NULL);
1185 SetLocalDescriptionOfferExpectError(kCreateChannelFailed, offer);
1186}
1187
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188// Test we will return fail when apply an offer that doesn't have
1189// crypto enabled.
1190TEST_F(WebRtcSessionTest, SetNonCryptoOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001191 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 cricket::MediaSessionOptions options;
1193 options.has_video = true;
1194 JsepSessionDescription* offer = CreateRemoteOffer(
1195 options, cricket::SEC_DISABLED);
1196 ASSERT_TRUE(offer != NULL);
1197 VerifyNoCryptoParams(offer->description(), false);
1198 // SetRemoteDescription and SetLocalDescription will take the ownership of
1199 // the offer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 SetRemoteDescriptionOfferExpectError(kSdpWithoutCrypto, offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 offer = CreateRemoteOffer(options, cricket::SEC_DISABLED);
1202 ASSERT_TRUE(offer != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001203 SetLocalDescriptionOfferExpectError(kSdpWithoutCrypto, offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204}
1205
1206// Test we will return fail when apply an answer that doesn't have
1207// crypto enabled.
1208TEST_F(WebRtcSessionTest, SetLocalNonCryptoAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001209 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 SessionDescriptionInterface* offer = NULL;
1211 SessionDescriptionInterface* answer = NULL;
1212 CreateCryptoOfferAndNonCryptoAnswer(&offer, &answer);
1213 // SetRemoteDescription and SetLocalDescription will take the ownership of
1214 // the offer.
1215 SetRemoteDescriptionWithoutError(offer);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001216 SetLocalDescriptionAnswerExpectError(kSdpWithoutCrypto, answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217}
1218
1219// Test we will return fail when apply an answer that doesn't have
1220// crypto enabled.
1221TEST_F(WebRtcSessionTest, SetRemoteNonCryptoAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001222 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 SessionDescriptionInterface* offer = NULL;
1224 SessionDescriptionInterface* answer = NULL;
1225 CreateCryptoOfferAndNonCryptoAnswer(&offer, &answer);
1226 // SetRemoteDescription and SetLocalDescription will take the ownership of
1227 // the offer.
1228 SetLocalDescriptionWithoutError(offer);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001229 SetRemoteDescriptionAnswerExpectError(kSdpWithoutCrypto, answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230}
1231
1232// Test that we can create and set an offer with a DTLS fingerprint.
1233TEST_F(WebRtcSessionTest, CreateSetDtlsOffer) {
1234 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
1235 InitWithDtls();
1236 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001237 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238 ASSERT_TRUE(offer != NULL);
1239 VerifyFingerprintStatus(offer->description(), true);
1240 // SetLocalDescription will take the ownership of the offer.
1241 SetLocalDescriptionWithoutError(offer);
1242}
1243
1244// Test that we can process an offer with a DTLS fingerprint
1245// and that we return an answer with a fingerprint.
1246TEST_F(WebRtcSessionTest, ReceiveDtlsOfferCreateAnswer) {
1247 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
1248 InitWithDtls();
1249 SetFactoryDtlsSrtp();
1250 cricket::MediaSessionOptions options;
1251 options.has_video = true;
1252 JsepSessionDescription* offer = CreateRemoteOffer(options);
1253 ASSERT_TRUE(offer != NULL);
1254 VerifyFingerprintStatus(offer->description(), true);
1255
1256 // SetRemoteDescription will take the ownership of the offer.
1257 SetRemoteDescriptionWithoutError(offer);
1258
1259 // Verify that we get a crypto fingerprint in the answer.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001260 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 ASSERT_TRUE(answer != NULL);
1262 VerifyFingerprintStatus(answer->description(), true);
1263 // Check that we don't have an a=crypto line in the answer.
1264 VerifyNoCryptoParams(answer->description(), true);
1265
1266 // Now set the local description, which should work, even without a=crypto.
1267 SetLocalDescriptionWithoutError(answer);
1268}
1269
1270// Test that even if we support DTLS, if the other side didn't offer a
1271// fingerprint, we don't either.
1272TEST_F(WebRtcSessionTest, ReceiveNoDtlsOfferCreateAnswer) {
1273 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
1274 InitWithDtls();
1275 cricket::MediaSessionOptions options;
1276 options.has_video = true;
1277 JsepSessionDescription* offer = CreateRemoteOffer(
1278 options, cricket::SEC_REQUIRED);
1279 ASSERT_TRUE(offer != NULL);
1280 VerifyFingerprintStatus(offer->description(), false);
1281
1282 // SetRemoteDescription will take the ownership of
1283 // the offer.
1284 SetRemoteDescriptionWithoutError(offer);
1285
1286 // Verify that we don't get a crypto fingerprint in the answer.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001287 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 ASSERT_TRUE(answer != NULL);
1289 VerifyFingerprintStatus(answer->description(), false);
1290
1291 // Now set the local description.
1292 SetLocalDescriptionWithoutError(answer);
1293}
1294
1295TEST_F(WebRtcSessionTest, TestSetLocalOfferTwice) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001296 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 mediastream_signaling_.SendNothing();
1298 // SetLocalDescription take ownership of offer.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001299 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 SetLocalDescriptionWithoutError(offer);
1301
1302 // SetLocalDescription take ownership of offer.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001303 SessionDescriptionInterface* offer2 = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 SetLocalDescriptionWithoutError(offer2);
1305}
1306
1307TEST_F(WebRtcSessionTest, TestSetRemoteOfferTwice) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001308 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 mediastream_signaling_.SendNothing();
1310 // SetLocalDescription take ownership of offer.
wu@webrtc.org91053e72013-08-10 07:18:04 +00001311 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 SetRemoteDescriptionWithoutError(offer);
1313
wu@webrtc.org91053e72013-08-10 07:18:04 +00001314 SessionDescriptionInterface* offer2 = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 SetRemoteDescriptionWithoutError(offer2);
1316}
1317
1318TEST_F(WebRtcSessionTest, TestSetLocalAndRemoteOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001319 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 mediastream_signaling_.SendNothing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001321 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 SetLocalDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001323 offer = CreateOffer(NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001324 SetRemoteDescriptionOfferExpectError(
1325 "Called in wrong state: STATE_SENTINITIATE", offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326}
1327
1328TEST_F(WebRtcSessionTest, TestSetRemoteAndLocalOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001329 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 mediastream_signaling_.SendNothing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001331 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 SetRemoteDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001333 offer = CreateOffer(NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001334 SetLocalDescriptionOfferExpectError(
1335 "Called in wrong state: STATE_RECEIVEDINITIATE", offer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336}
1337
1338TEST_F(WebRtcSessionTest, TestSetLocalPrAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001339 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 mediastream_signaling_.SendNothing();
1341 SessionDescriptionInterface* offer = CreateRemoteOffer();
1342 SetRemoteDescriptionExpectState(offer, BaseSession::STATE_RECEIVEDINITIATE);
1343
1344 JsepSessionDescription* pranswer = static_cast<JsepSessionDescription*>(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001345 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 pranswer->set_type(SessionDescriptionInterface::kPrAnswer);
1347 SetLocalDescriptionExpectState(pranswer, BaseSession::STATE_SENTPRACCEPT);
1348
1349 mediastream_signaling_.SendAudioVideoStream1();
1350 JsepSessionDescription* pranswer2 = static_cast<JsepSessionDescription*>(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001351 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 pranswer2->set_type(SessionDescriptionInterface::kPrAnswer);
1353
1354 SetLocalDescriptionExpectState(pranswer2, BaseSession::STATE_SENTPRACCEPT);
1355
1356 mediastream_signaling_.SendAudioVideoStream2();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001357 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 SetLocalDescriptionExpectState(answer, BaseSession::STATE_SENTACCEPT);
1359}
1360
1361TEST_F(WebRtcSessionTest, TestSetRemotePrAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001362 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363 mediastream_signaling_.SendNothing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001364 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365 SetLocalDescriptionExpectState(offer, BaseSession::STATE_SENTINITIATE);
1366
1367 JsepSessionDescription* pranswer =
1368 CreateRemoteAnswer(session_->local_description());
1369 pranswer->set_type(SessionDescriptionInterface::kPrAnswer);
1370
1371 SetRemoteDescriptionExpectState(pranswer,
1372 BaseSession::STATE_RECEIVEDPRACCEPT);
1373
1374 mediastream_signaling_.SendAudioVideoStream1();
1375 JsepSessionDescription* pranswer2 =
1376 CreateRemoteAnswer(session_->local_description());
1377 pranswer2->set_type(SessionDescriptionInterface::kPrAnswer);
1378
1379 SetRemoteDescriptionExpectState(pranswer2,
1380 BaseSession::STATE_RECEIVEDPRACCEPT);
1381
1382 mediastream_signaling_.SendAudioVideoStream2();
1383 SessionDescriptionInterface* answer =
1384 CreateRemoteAnswer(session_->local_description());
1385 SetRemoteDescriptionExpectState(answer, BaseSession::STATE_RECEIVEDACCEPT);
1386}
1387
1388TEST_F(WebRtcSessionTest, TestSetLocalAnswerWithoutOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001389 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390 mediastream_signaling_.SendNothing();
1391 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001392 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393 SessionDescriptionInterface* answer =
1394 CreateRemoteAnswer(offer.get());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001395 SetLocalDescriptionAnswerExpectError("Called in wrong state: STATE_INIT",
1396 answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397}
1398
1399TEST_F(WebRtcSessionTest, TestSetRemoteAnswerWithoutOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001400 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401 mediastream_signaling_.SendNothing();
1402 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001403 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 SessionDescriptionInterface* answer =
1405 CreateRemoteAnswer(offer.get());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001406 SetRemoteDescriptionAnswerExpectError(
1407 "Called in wrong state: STATE_INIT", answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408}
1409
1410TEST_F(WebRtcSessionTest, TestAddRemoteCandidate) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001411 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001412 mediastream_signaling_.SendAudioVideoStream1();
1413
1414 cricket::Candidate candidate;
1415 candidate.set_component(1);
1416 JsepIceCandidate ice_candidate1(kMediaContentName0, 0, candidate);
1417
1418 // Fail since we have not set a offer description.
1419 EXPECT_FALSE(session_->ProcessIceMessage(&ice_candidate1));
1420
wu@webrtc.org91053e72013-08-10 07:18:04 +00001421 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 SetLocalDescriptionWithoutError(offer);
1423 // Candidate should be allowed to add before remote description.
1424 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate1));
1425 candidate.set_component(2);
1426 JsepIceCandidate ice_candidate2(kMediaContentName0, 0, candidate);
1427 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate2));
1428
1429 SessionDescriptionInterface* answer = CreateRemoteAnswer(
1430 session_->local_description());
1431 SetRemoteDescriptionWithoutError(answer);
1432
1433 // Verifying the candidates are copied properly from internal vector.
1434 const SessionDescriptionInterface* remote_desc =
1435 session_->remote_description();
1436 ASSERT_TRUE(remote_desc != NULL);
1437 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
1438 const IceCandidateCollection* candidates =
1439 remote_desc->candidates(kMediaContentIndex0);
1440 ASSERT_EQ(2u, candidates->count());
1441 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
1442 EXPECT_EQ(kMediaContentName0, candidates->at(0)->sdp_mid());
1443 EXPECT_EQ(1, candidates->at(0)->candidate().component());
1444 EXPECT_EQ(2, candidates->at(1)->candidate().component());
1445
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001446 // |ice_candidate3| is identical to |ice_candidate2|. It can be added
1447 // successfully, but the total count of candidates will not increase.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 candidate.set_component(2);
1449 JsepIceCandidate ice_candidate3(kMediaContentName0, 0, candidate);
1450 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate3));
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001451 ASSERT_EQ(2u, candidates->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452
1453 JsepIceCandidate bad_ice_candidate("bad content name", 99, candidate);
1454 EXPECT_FALSE(session_->ProcessIceMessage(&bad_ice_candidate));
1455}
1456
1457// Test that a remote candidate is added to the remote session description and
1458// that it is retained if the remote session description is changed.
1459TEST_F(WebRtcSessionTest, TestRemoteCandidatesAddedToSessionDescription) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001460 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 cricket::Candidate candidate1;
1462 candidate1.set_component(1);
1463 JsepIceCandidate ice_candidate1(kMediaContentName0, kMediaContentIndex0,
1464 candidate1);
1465 mediastream_signaling_.SendAudioVideoStream1();
1466 CreateAndSetRemoteOfferAndLocalAnswer();
1467
1468 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate1));
1469 const SessionDescriptionInterface* remote_desc =
1470 session_->remote_description();
1471 ASSERT_TRUE(remote_desc != NULL);
1472 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
1473 const IceCandidateCollection* candidates =
1474 remote_desc->candidates(kMediaContentIndex0);
1475 ASSERT_EQ(1u, candidates->count());
1476 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
1477
1478 // Update the RemoteSessionDescription with a new session description and
1479 // a candidate and check that the new remote session description contains both
1480 // candidates.
1481 SessionDescriptionInterface* offer = CreateRemoteOffer();
1482 cricket::Candidate candidate2;
1483 JsepIceCandidate ice_candidate2(kMediaContentName0, kMediaContentIndex0,
1484 candidate2);
1485 EXPECT_TRUE(offer->AddCandidate(&ice_candidate2));
1486 SetRemoteDescriptionWithoutError(offer);
1487
1488 remote_desc = session_->remote_description();
1489 ASSERT_TRUE(remote_desc != NULL);
1490 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
1491 candidates = remote_desc->candidates(kMediaContentIndex0);
1492 ASSERT_EQ(2u, candidates->count());
1493 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
1494 // Username and password have be updated with the TransportInfo of the
1495 // SessionDescription, won't be equal to the original one.
1496 candidate2.set_username(candidates->at(0)->candidate().username());
1497 candidate2.set_password(candidates->at(0)->candidate().password());
1498 EXPECT_TRUE(candidate2.IsEquivalent(candidates->at(0)->candidate()));
1499 EXPECT_EQ(kMediaContentIndex0, candidates->at(1)->sdp_mline_index());
1500 // No need to verify the username and password.
1501 candidate1.set_username(candidates->at(1)->candidate().username());
1502 candidate1.set_password(candidates->at(1)->candidate().password());
1503 EXPECT_TRUE(candidate1.IsEquivalent(candidates->at(1)->candidate()));
1504
1505 // Test that the candidate is ignored if we can add the same candidate again.
1506 EXPECT_TRUE(session_->ProcessIceMessage(&ice_candidate2));
1507}
1508
1509// Test that local candidates are added to the local session description and
1510// that they are retained if the local session description is changed.
1511TEST_F(WebRtcSessionTest, TestLocalCandidatesAddedToSessionDescription) {
wu@webrtc.org364f2042013-11-20 21:49:41 +00001512 AddInterface(talk_base::SocketAddress(kClientAddrHost1, kClientAddrPort));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001513 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 mediastream_signaling_.SendAudioVideoStream1();
1515 CreateAndSetRemoteOfferAndLocalAnswer();
1516
1517 const SessionDescriptionInterface* local_desc = session_->local_description();
1518 const IceCandidateCollection* candidates =
1519 local_desc->candidates(kMediaContentIndex0);
1520 ASSERT_TRUE(candidates != NULL);
1521 EXPECT_EQ(0u, candidates->count());
1522
1523 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
1524
1525 local_desc = session_->local_description();
1526 candidates = local_desc->candidates(kMediaContentIndex0);
1527 ASSERT_TRUE(candidates != NULL);
1528 EXPECT_LT(0u, candidates->count());
1529 candidates = local_desc->candidates(1);
1530 ASSERT_TRUE(candidates != NULL);
1531 EXPECT_LT(0u, candidates->count());
1532
1533 // Update the session descriptions.
1534 mediastream_signaling_.SendAudioVideoStream1();
1535 CreateAndSetRemoteOfferAndLocalAnswer();
1536
1537 local_desc = session_->local_description();
1538 candidates = local_desc->candidates(kMediaContentIndex0);
1539 ASSERT_TRUE(candidates != NULL);
1540 EXPECT_LT(0u, candidates->count());
1541 candidates = local_desc->candidates(1);
1542 ASSERT_TRUE(candidates != NULL);
1543 EXPECT_LT(0u, candidates->count());
1544}
1545
1546// Test that we can set a remote session description with remote candidates.
1547TEST_F(WebRtcSessionTest, TestSetRemoteSessionDescriptionWithCandidates) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001548 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549
1550 cricket::Candidate candidate1;
1551 candidate1.set_component(1);
1552 JsepIceCandidate ice_candidate(kMediaContentName0, kMediaContentIndex0,
1553 candidate1);
1554 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001555 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556
1557 EXPECT_TRUE(offer->AddCandidate(&ice_candidate));
1558 SetRemoteDescriptionWithoutError(offer);
1559
1560 const SessionDescriptionInterface* remote_desc =
1561 session_->remote_description();
1562 ASSERT_TRUE(remote_desc != NULL);
1563 ASSERT_EQ(2u, remote_desc->number_of_mediasections());
1564 const IceCandidateCollection* candidates =
1565 remote_desc->candidates(kMediaContentIndex0);
1566 ASSERT_EQ(1u, candidates->count());
1567 EXPECT_EQ(kMediaContentIndex0, candidates->at(0)->sdp_mline_index());
1568
wu@webrtc.org91053e72013-08-10 07:18:04 +00001569 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 SetLocalDescriptionWithoutError(answer);
1571}
1572
1573// Test that offers and answers contains ice candidates when Ice candidates have
1574// been gathered.
1575TEST_F(WebRtcSessionTest, TestSetLocalAndRemoteDescriptionWithCandidates) {
wu@webrtc.org364f2042013-11-20 21:49:41 +00001576 AddInterface(talk_base::SocketAddress(kClientAddrHost1, kClientAddrPort));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001577 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 mediastream_signaling_.SendAudioVideoStream1();
1579 // Ice is started but candidates are not provided until SetLocalDescription
1580 // is called.
1581 EXPECT_EQ(0u, observer_.mline_0_candidates_.size());
1582 EXPECT_EQ(0u, observer_.mline_1_candidates_.size());
1583 CreateAndSetRemoteOfferAndLocalAnswer();
1584 // Wait until at least one local candidate has been collected.
1585 EXPECT_TRUE_WAIT(0u < observer_.mline_0_candidates_.size(),
1586 kIceCandidatesTimeout);
1587 EXPECT_TRUE_WAIT(0u < observer_.mline_1_candidates_.size(),
1588 kIceCandidatesTimeout);
1589
1590 talk_base::scoped_ptr<SessionDescriptionInterface> local_offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001591 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 ASSERT_TRUE(local_offer->candidates(kMediaContentIndex0) != NULL);
1593 EXPECT_LT(0u, local_offer->candidates(kMediaContentIndex0)->count());
1594 ASSERT_TRUE(local_offer->candidates(kMediaContentIndex1) != NULL);
1595 EXPECT_LT(0u, local_offer->candidates(kMediaContentIndex1)->count());
1596
1597 SessionDescriptionInterface* remote_offer(CreateRemoteOffer());
1598 SetRemoteDescriptionWithoutError(remote_offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001599 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 ASSERT_TRUE(answer->candidates(kMediaContentIndex0) != NULL);
1601 EXPECT_LT(0u, answer->candidates(kMediaContentIndex0)->count());
1602 ASSERT_TRUE(answer->candidates(kMediaContentIndex1) != NULL);
1603 EXPECT_LT(0u, answer->candidates(kMediaContentIndex1)->count());
1604 SetLocalDescriptionWithoutError(answer);
1605}
1606
1607// Verifies TransportProxy and media channels are created with content names
1608// present in the SessionDescription.
1609TEST_F(WebRtcSessionTest, TestChannelCreationsWithContentNames) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001610 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611 mediastream_signaling_.SendAudioVideoStream1();
1612 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001613 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614
1615 // CreateOffer creates session description with the content names "audio" and
1616 // "video". Goal is to modify these content names and verify transport channel
1617 // proxy in the BaseSession, as proxies are created with the content names
1618 // present in SDP.
1619 std::string sdp;
1620 EXPECT_TRUE(offer->ToString(&sdp));
1621 const std::string kAudioMid = "a=mid:audio";
1622 const std::string kAudioMidReplaceStr = "a=mid:audio_content_name";
1623 const std::string kVideoMid = "a=mid:video";
1624 const std::string kVideoMidReplaceStr = "a=mid:video_content_name";
1625
1626 // Replacing |audio| with |audio_content_name|.
1627 talk_base::replace_substrs(kAudioMid.c_str(), kAudioMid.length(),
1628 kAudioMidReplaceStr.c_str(),
1629 kAudioMidReplaceStr.length(),
1630 &sdp);
1631 // Replacing |video| with |video_content_name|.
1632 talk_base::replace_substrs(kVideoMid.c_str(), kVideoMid.length(),
1633 kVideoMidReplaceStr.c_str(),
1634 kVideoMidReplaceStr.length(),
1635 &sdp);
1636
1637 SessionDescriptionInterface* modified_offer =
1638 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
1639
1640 SetRemoteDescriptionWithoutError(modified_offer);
1641
1642 SessionDescriptionInterface* answer =
wu@webrtc.org91053e72013-08-10 07:18:04 +00001643 CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 SetLocalDescriptionWithoutError(answer);
1645
1646 EXPECT_TRUE(session_->GetTransportProxy("audio_content_name") != NULL);
1647 EXPECT_TRUE(session_->GetTransportProxy("video_content_name") != NULL);
1648 EXPECT_TRUE((video_channel_ = media_engine_->GetVideoChannel(0)) != NULL);
1649 EXPECT_TRUE((voice_channel_ = media_engine_->GetVoiceChannel(0)) != NULL);
1650}
1651
1652// Test that an offer contains the correct media content descriptions based on
1653// the send streams when no constraints have been set.
1654TEST_F(WebRtcSessionTest, CreateOfferWithoutConstraintsOrStreams) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001655 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001657 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 ASSERT_TRUE(offer != NULL);
1659 const cricket::ContentInfo* content =
1660 cricket::GetFirstAudioContent(offer->description());
1661 EXPECT_TRUE(content != NULL);
1662 content = cricket::GetFirstVideoContent(offer->description());
1663 EXPECT_TRUE(content == NULL);
1664}
1665
1666// Test that an offer contains the correct media content descriptions based on
1667// the send streams when no constraints have been set.
1668TEST_F(WebRtcSessionTest, CreateOfferWithoutConstraints) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001669 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 // Test Audio only offer.
1671 mediastream_signaling_.UseOptionsAudioOnly();
1672 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001673 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 const cricket::ContentInfo* content =
1675 cricket::GetFirstAudioContent(offer->description());
1676 EXPECT_TRUE(content != NULL);
1677 content = cricket::GetFirstVideoContent(offer->description());
1678 EXPECT_TRUE(content == NULL);
1679
1680 // Test Audio / Video offer.
1681 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001682 offer.reset(CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683 content = cricket::GetFirstAudioContent(offer->description());
1684 EXPECT_TRUE(content != NULL);
1685 content = cricket::GetFirstVideoContent(offer->description());
1686 EXPECT_TRUE(content != NULL);
1687}
1688
1689// Test that an offer contains no media content descriptions if
1690// kOfferToReceiveVideo and kOfferToReceiveAudio constraints are set to false.
1691TEST_F(WebRtcSessionTest, CreateOfferWithConstraintsWithoutStreams) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001692 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 webrtc::FakeConstraints constraints_no_receive;
1694 constraints_no_receive.SetMandatoryReceiveAudio(false);
1695 constraints_no_receive.SetMandatoryReceiveVideo(false);
1696
1697 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001698 CreateOffer(&constraints_no_receive));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 ASSERT_TRUE(offer != NULL);
1700 const cricket::ContentInfo* content =
1701 cricket::GetFirstAudioContent(offer->description());
1702 EXPECT_TRUE(content == NULL);
1703 content = cricket::GetFirstVideoContent(offer->description());
1704 EXPECT_TRUE(content == NULL);
1705}
1706
1707// Test that an offer contains only audio media content descriptions if
1708// kOfferToReceiveAudio constraints are set to true.
1709TEST_F(WebRtcSessionTest, CreateAudioOnlyOfferWithConstraints) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001710 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 webrtc::FakeConstraints constraints_audio_only;
1712 constraints_audio_only.SetMandatoryReceiveAudio(true);
1713 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001714 CreateOffer(&constraints_audio_only));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715
1716 const cricket::ContentInfo* content =
1717 cricket::GetFirstAudioContent(offer->description());
1718 EXPECT_TRUE(content != NULL);
1719 content = cricket::GetFirstVideoContent(offer->description());
1720 EXPECT_TRUE(content == NULL);
1721}
1722
1723// Test that an offer contains audio and video media content descriptions if
1724// kOfferToReceiveAudio and kOfferToReceiveVideo constraints are set to true.
1725TEST_F(WebRtcSessionTest, CreateOfferWithConstraints) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001726 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 // Test Audio / Video offer.
1728 webrtc::FakeConstraints constraints_audio_video;
1729 constraints_audio_video.SetMandatoryReceiveAudio(true);
1730 constraints_audio_video.SetMandatoryReceiveVideo(true);
1731 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001732 CreateOffer(&constraints_audio_video));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 const cricket::ContentInfo* content =
1734 cricket::GetFirstAudioContent(offer->description());
1735
1736 EXPECT_TRUE(content != NULL);
1737 content = cricket::GetFirstVideoContent(offer->description());
1738 EXPECT_TRUE(content != NULL);
1739
1740 // TODO(perkj): Should the direction be set to SEND_ONLY if
1741 // The constraints is set to not receive audio or video but a track is added?
1742}
1743
1744// Test that an answer can not be created if the last remote description is not
1745// an offer.
1746TEST_F(WebRtcSessionTest, CreateAnswerWithoutAnOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001747 Init(NULL);
1748 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 SetLocalDescriptionWithoutError(offer);
1750 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
1751 SetRemoteDescriptionWithoutError(answer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001752 EXPECT_TRUE(CreateAnswer(NULL) == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753}
1754
1755// Test that an answer contains the correct media content descriptions when no
1756// constraints have been set.
1757TEST_F(WebRtcSessionTest, CreateAnswerWithoutConstraintsOrStreams) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001758 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 // Create a remote offer with audio and video content.
1760 talk_base::scoped_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
1761 SetRemoteDescriptionWithoutError(offer.release());
1762 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001763 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 const cricket::ContentInfo* content =
1765 cricket::GetFirstAudioContent(answer->description());
1766 ASSERT_TRUE(content != NULL);
1767 EXPECT_FALSE(content->rejected);
1768
1769 content = cricket::GetFirstVideoContent(answer->description());
1770 ASSERT_TRUE(content != NULL);
1771 EXPECT_FALSE(content->rejected);
1772}
1773
1774// Test that an answer contains the correct media content descriptions when no
1775// constraints have been set and the offer only contain audio.
1776TEST_F(WebRtcSessionTest, CreateAudioAnswerWithoutConstraintsOrStreams) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001777 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 // Create a remote offer with audio only.
1779 cricket::MediaSessionOptions options;
1780 options.has_audio = true;
1781 options.has_video = false;
1782 talk_base::scoped_ptr<JsepSessionDescription> offer(
1783 CreateRemoteOffer(options));
1784 ASSERT_TRUE(cricket::GetFirstVideoContent(offer->description()) == NULL);
1785 ASSERT_TRUE(cricket::GetFirstAudioContent(offer->description()) != NULL);
1786
1787 SetRemoteDescriptionWithoutError(offer.release());
1788 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001789 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 const cricket::ContentInfo* content =
1791 cricket::GetFirstAudioContent(answer->description());
1792 ASSERT_TRUE(content != NULL);
1793 EXPECT_FALSE(content->rejected);
1794
1795 EXPECT_TRUE(cricket::GetFirstVideoContent(answer->description()) == NULL);
1796}
1797
1798// Test that an answer contains the correct media content descriptions when no
1799// constraints have been set.
1800TEST_F(WebRtcSessionTest, CreateAnswerWithoutConstraints) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001801 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 // Create a remote offer with audio and video content.
1803 talk_base::scoped_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
1804 SetRemoteDescriptionWithoutError(offer.release());
1805 // Test with a stream with tracks.
1806 mediastream_signaling_.SendAudioVideoStream1();
1807 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001808 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 const cricket::ContentInfo* content =
1810 cricket::GetFirstAudioContent(answer->description());
1811 ASSERT_TRUE(content != NULL);
1812 EXPECT_FALSE(content->rejected);
1813
1814 content = cricket::GetFirstVideoContent(answer->description());
1815 ASSERT_TRUE(content != NULL);
1816 EXPECT_FALSE(content->rejected);
1817}
1818
1819// Test that an answer contains the correct media content descriptions when
1820// constraints have been set but no stream is sent.
1821TEST_F(WebRtcSessionTest, CreateAnswerWithConstraintsWithoutStreams) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001822 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 // Create a remote offer with audio and video content.
1824 talk_base::scoped_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
1825 SetRemoteDescriptionWithoutError(offer.release());
1826
1827 webrtc::FakeConstraints constraints_no_receive;
1828 constraints_no_receive.SetMandatoryReceiveAudio(false);
1829 constraints_no_receive.SetMandatoryReceiveVideo(false);
1830
1831 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001832 CreateAnswer(&constraints_no_receive));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 const cricket::ContentInfo* content =
1834 cricket::GetFirstAudioContent(answer->description());
1835 ASSERT_TRUE(content != NULL);
1836 EXPECT_TRUE(content->rejected);
1837
1838 content = cricket::GetFirstVideoContent(answer->description());
1839 ASSERT_TRUE(content != NULL);
1840 EXPECT_TRUE(content->rejected);
1841}
1842
1843// Test that an answer contains the correct media content descriptions when
1844// constraints have been set and streams are sent.
1845TEST_F(WebRtcSessionTest, CreateAnswerWithConstraints) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001846 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 // Create a remote offer with audio and video content.
1848 talk_base::scoped_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
1849 SetRemoteDescriptionWithoutError(offer.release());
1850
1851 webrtc::FakeConstraints constraints_no_receive;
1852 constraints_no_receive.SetMandatoryReceiveAudio(false);
1853 constraints_no_receive.SetMandatoryReceiveVideo(false);
1854
1855 // Test with a stream with tracks.
1856 mediastream_signaling_.SendAudioVideoStream1();
1857 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001858 CreateAnswer(&constraints_no_receive));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859
1860 // TODO(perkj): Should the direction be set to SEND_ONLY?
1861 const cricket::ContentInfo* content =
1862 cricket::GetFirstAudioContent(answer->description());
1863 ASSERT_TRUE(content != NULL);
1864 EXPECT_FALSE(content->rejected);
1865
1866 // TODO(perkj): Should the direction be set to SEND_ONLY?
1867 content = cricket::GetFirstVideoContent(answer->description());
1868 ASSERT_TRUE(content != NULL);
1869 EXPECT_FALSE(content->rejected);
1870}
1871
1872TEST_F(WebRtcSessionTest, CreateOfferWithoutCNCodecs) {
1873 AddCNCodecs();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001874 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 webrtc::FakeConstraints constraints;
1876 constraints.SetOptionalVAD(false);
1877 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001878 CreateOffer(&constraints));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 const cricket::ContentInfo* content =
1880 cricket::GetFirstAudioContent(offer->description());
1881 EXPECT_TRUE(content != NULL);
1882 EXPECT_TRUE(VerifyNoCNCodecs(content));
1883}
1884
1885TEST_F(WebRtcSessionTest, CreateAnswerWithoutCNCodecs) {
1886 AddCNCodecs();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001887 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 // Create a remote offer with audio and video content.
1889 talk_base::scoped_ptr<JsepSessionDescription> offer(CreateRemoteOffer());
1890 SetRemoteDescriptionWithoutError(offer.release());
1891
1892 webrtc::FakeConstraints constraints;
1893 constraints.SetOptionalVAD(false);
1894 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00001895 CreateAnswer(&constraints));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896 const cricket::ContentInfo* content =
1897 cricket::GetFirstAudioContent(answer->description());
1898 ASSERT_TRUE(content != NULL);
1899 EXPECT_TRUE(VerifyNoCNCodecs(content));
1900}
1901
1902// This test verifies the call setup when remote answer with audio only and
1903// later updates with video.
1904TEST_F(WebRtcSessionTest, TestAVOfferWithAudioOnlyAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001905 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 EXPECT_TRUE(media_engine_->GetVideoChannel(0) == NULL);
1907 EXPECT_TRUE(media_engine_->GetVoiceChannel(0) == NULL);
1908
1909 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001910 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911
1912 cricket::MediaSessionOptions options;
1913 options.has_video = false;
1914 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer, options);
1915
1916 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
1917 // and answer;
1918 SetLocalDescriptionWithoutError(offer);
1919 SetRemoteDescriptionWithoutError(answer);
1920
1921 video_channel_ = media_engine_->GetVideoChannel(0);
1922 voice_channel_ = media_engine_->GetVoiceChannel(0);
1923
1924 ASSERT_TRUE(video_channel_ == NULL);
1925
1926 ASSERT_EQ(0u, voice_channel_->recv_streams().size());
1927 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1928 EXPECT_EQ(kAudioTrack1, voice_channel_->send_streams()[0].id);
1929
1930 // Let the remote end update the session descriptions, with Audio and Video.
1931 mediastream_signaling_.SendAudioVideoStream2();
1932 CreateAndSetRemoteOfferAndLocalAnswer();
1933
1934 video_channel_ = media_engine_->GetVideoChannel(0);
1935 voice_channel_ = media_engine_->GetVoiceChannel(0);
1936
1937 ASSERT_TRUE(video_channel_ != NULL);
1938 ASSERT_TRUE(voice_channel_ != NULL);
1939
1940 ASSERT_EQ(1u, video_channel_->recv_streams().size());
1941 ASSERT_EQ(1u, video_channel_->send_streams().size());
1942 EXPECT_EQ(kVideoTrack2, video_channel_->recv_streams()[0].id);
1943 EXPECT_EQ(kVideoTrack2, video_channel_->send_streams()[0].id);
1944 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1945 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1946 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
1947 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
1948
1949 // Change session back to audio only.
1950 mediastream_signaling_.UseOptionsAudioOnly();
1951 CreateAndSetRemoteOfferAndLocalAnswer();
1952
1953 EXPECT_EQ(0u, video_channel_->recv_streams().size());
1954 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1955 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
1956 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1957 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
1958}
1959
1960// This test verifies the call setup when remote answer with video only and
1961// later updates with audio.
1962TEST_F(WebRtcSessionTest, TestAVOfferWithVideoOnlyAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001963 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 EXPECT_TRUE(media_engine_->GetVideoChannel(0) == NULL);
1965 EXPECT_TRUE(media_engine_->GetVoiceChannel(0) == NULL);
1966 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001967 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968
1969 cricket::MediaSessionOptions options;
1970 options.has_audio = false;
1971 options.has_video = true;
1972 SessionDescriptionInterface* answer = CreateRemoteAnswer(
1973 offer, options, cricket::SEC_ENABLED);
1974
1975 // SetLocalDescription and SetRemoteDescriptions takes ownership of offer
1976 // and answer.
1977 SetLocalDescriptionWithoutError(offer);
1978 SetRemoteDescriptionWithoutError(answer);
1979
1980 video_channel_ = media_engine_->GetVideoChannel(0);
1981 voice_channel_ = media_engine_->GetVoiceChannel(0);
1982
1983 ASSERT_TRUE(voice_channel_ == NULL);
1984 ASSERT_TRUE(video_channel_ != NULL);
1985
1986 EXPECT_EQ(0u, video_channel_->recv_streams().size());
1987 ASSERT_EQ(1u, video_channel_->send_streams().size());
1988 EXPECT_EQ(kVideoTrack1, video_channel_->send_streams()[0].id);
1989
1990 // Update the session descriptions, with Audio and Video.
1991 mediastream_signaling_.SendAudioVideoStream2();
1992 CreateAndSetRemoteOfferAndLocalAnswer();
1993
1994 voice_channel_ = media_engine_->GetVoiceChannel(0);
1995 ASSERT_TRUE(voice_channel_ != NULL);
1996
1997 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
1998 ASSERT_EQ(1u, voice_channel_->send_streams().size());
1999 EXPECT_EQ(kAudioTrack2, voice_channel_->recv_streams()[0].id);
2000 EXPECT_EQ(kAudioTrack2, voice_channel_->send_streams()[0].id);
2001
2002 // Change session back to video only.
2003 mediastream_signaling_.UseOptionsVideoOnly();
2004 CreateAndSetRemoteOfferAndLocalAnswer();
2005
2006 video_channel_ = media_engine_->GetVideoChannel(0);
2007 voice_channel_ = media_engine_->GetVoiceChannel(0);
2008
2009 ASSERT_EQ(1u, video_channel_->recv_streams().size());
2010 EXPECT_EQ(kVideoTrack2, video_channel_->recv_streams()[0].id);
2011 ASSERT_EQ(1u, video_channel_->send_streams().size());
2012 EXPECT_EQ(kVideoTrack2, video_channel_->send_streams()[0].id);
2013}
2014
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015TEST_F(WebRtcSessionTest, VerifyCryptoParamsInSDP) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002016 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 mediastream_signaling_.SendAudioVideoStream1();
2018 scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002019 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020 VerifyCryptoParams(offer->description());
2021 SetRemoteDescriptionWithoutError(offer.release());
wu@webrtc.org91053e72013-08-10 07:18:04 +00002022 scoped_ptr<SessionDescriptionInterface> answer(CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 VerifyCryptoParams(answer->description());
2024}
2025
2026TEST_F(WebRtcSessionTest, VerifyNoCryptoParamsInSDP) {
wu@webrtc.org97077a32013-10-25 21:18:33 +00002027 options_.disable_encryption = true;
wu@webrtc.org91053e72013-08-10 07:18:04 +00002028 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 mediastream_signaling_.SendAudioVideoStream1();
2030 scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002031 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 VerifyNoCryptoParams(offer->description(), false);
2033}
2034
2035TEST_F(WebRtcSessionTest, VerifyAnswerFromNonCryptoOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002036 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037 VerifyAnswerFromNonCryptoOffer();
2038}
2039
2040TEST_F(WebRtcSessionTest, VerifyAnswerFromCryptoOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002041 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 VerifyAnswerFromCryptoOffer();
2043}
2044
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002045// This test verifies that setLocalDescription fails if
2046// no a=ice-ufrag and a=ice-pwd lines are present in the SDP.
2047TEST_F(WebRtcSessionTest, TestSetLocalDescriptionWithoutIce) {
2048 Init(NULL);
2049 mediastream_signaling_.SendAudioVideoStream1();
2050 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateOffer(NULL));
2051 std::string sdp;
2052 RemoveIceUfragPwdLines(offer.get(), &sdp);
2053 SessionDescriptionInterface* modified_offer =
2054 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002055 SetLocalDescriptionOfferExpectError(kSdpWithoutIceUfragPwd, modified_offer);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002056}
2057
2058// This test verifies that setRemoteDescription fails if
2059// no a=ice-ufrag and a=ice-pwd lines are present in the SDP.
2060TEST_F(WebRtcSessionTest, TestSetRemoteDescriptionWithoutIce) {
2061 Init(NULL);
2062 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateRemoteOffer());
2063 std::string sdp;
2064 RemoveIceUfragPwdLines(offer.get(), &sdp);
2065 SessionDescriptionInterface* modified_offer =
2066 CreateSessionDescription(JsepSessionDescription::kOffer, sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002067 SetRemoteDescriptionOfferExpectError(kSdpWithoutIceUfragPwd, modified_offer);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002068}
2069
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070TEST_F(WebRtcSessionTest, VerifyBundleFlagInPA) {
2071 // This test verifies BUNDLE flag in PortAllocator, if BUNDLE information in
2072 // local description is removed by the application, BUNDLE flag should be
2073 // disabled in PortAllocator. By default BUNDLE is enabled in the WebRtc.
wu@webrtc.org91053e72013-08-10 07:18:04 +00002074 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075 EXPECT_TRUE((cricket::PORTALLOCATOR_ENABLE_BUNDLE & allocator_.flags()) ==
2076 cricket::PORTALLOCATOR_ENABLE_BUNDLE);
2077 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002078 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 cricket::SessionDescription* offer_copy =
2080 offer->description()->Copy();
2081 offer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
2082 JsepSessionDescription* modified_offer =
2083 new JsepSessionDescription(JsepSessionDescription::kOffer);
2084 modified_offer->Initialize(offer_copy, "1", "1");
2085
2086 SetLocalDescriptionWithoutError(modified_offer);
2087 EXPECT_FALSE(allocator_.flags() & cricket::PORTALLOCATOR_ENABLE_BUNDLE);
2088}
2089
2090TEST_F(WebRtcSessionTest, TestDisabledBundleInAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002091 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 mediastream_signaling_.SendAudioVideoStream1();
2093 EXPECT_TRUE((cricket::PORTALLOCATOR_ENABLE_BUNDLE & allocator_.flags()) ==
2094 cricket::PORTALLOCATOR_ENABLE_BUNDLE);
2095 FakeConstraints constraints;
2096 constraints.SetMandatoryUseRtpMux(true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002097 SessionDescriptionInterface* offer = CreateOffer(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 SetLocalDescriptionWithoutError(offer);
2099 mediastream_signaling_.SendAudioVideoStream2();
2100 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
2101 CreateRemoteAnswer(session_->local_description()));
2102 cricket::SessionDescription* answer_copy = answer->description()->Copy();
2103 answer_copy->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
2104 JsepSessionDescription* modified_answer =
2105 new JsepSessionDescription(JsepSessionDescription::kAnswer);
2106 modified_answer->Initialize(answer_copy, "1", "1");
2107 SetRemoteDescriptionWithoutError(modified_answer);
2108 EXPECT_TRUE((cricket::PORTALLOCATOR_ENABLE_BUNDLE & allocator_.flags()) ==
2109 cricket::PORTALLOCATOR_ENABLE_BUNDLE);
2110
2111 video_channel_ = media_engine_->GetVideoChannel(0);
2112 voice_channel_ = media_engine_->GetVoiceChannel(0);
2113
2114 ASSERT_EQ(1u, video_channel_->recv_streams().size());
2115 EXPECT_TRUE(kVideoTrack2 == video_channel_->recv_streams()[0].id);
2116
2117 ASSERT_EQ(1u, voice_channel_->recv_streams().size());
2118 EXPECT_TRUE(kAudioTrack2 == voice_channel_->recv_streams()[0].id);
2119
2120 ASSERT_EQ(1u, video_channel_->send_streams().size());
2121 EXPECT_TRUE(kVideoTrack1 == video_channel_->send_streams()[0].id);
2122 ASSERT_EQ(1u, voice_channel_->send_streams().size());
2123 EXPECT_TRUE(kAudioTrack1 == voice_channel_->send_streams()[0].id);
2124}
2125
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002126// This test verifies that SetLocalDescription and SetRemoteDescription fails
2127// if BUNDLE is enabled but rtcp-mux is disabled in m-lines.
2128TEST_F(WebRtcSessionTest, TestDisabledRtcpMuxWithBundleEnabled) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002129 WebRtcSessionTest::Init(NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002130 mediastream_signaling_.SendAudioVideoStream1();
2131 EXPECT_TRUE((cricket::PORTALLOCATOR_ENABLE_BUNDLE & allocator_.flags()) ==
2132 cricket::PORTALLOCATOR_ENABLE_BUNDLE);
2133 FakeConstraints constraints;
2134 constraints.SetMandatoryUseRtpMux(true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002135 SessionDescriptionInterface* offer = CreateOffer(&constraints);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002136 std::string offer_str;
2137 offer->ToString(&offer_str);
2138 // Disable rtcp-mux
2139 const std::string rtcp_mux = "rtcp-mux";
2140 const std::string xrtcp_mux = "xrtcp-mux";
2141 talk_base::replace_substrs(rtcp_mux.c_str(), rtcp_mux.length(),
2142 xrtcp_mux.c_str(), xrtcp_mux.length(),
2143 &offer_str);
2144 JsepSessionDescription *local_offer =
2145 new JsepSessionDescription(JsepSessionDescription::kOffer);
2146 EXPECT_TRUE((local_offer)->Initialize(offer_str, NULL));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002147 SetLocalDescriptionOfferExpectError(kBundleWithoutRtcpMux, local_offer);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002148 JsepSessionDescription *remote_offer =
2149 new JsepSessionDescription(JsepSessionDescription::kOffer);
2150 EXPECT_TRUE((remote_offer)->Initialize(offer_str, NULL));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002151 SetRemoteDescriptionOfferExpectError(kBundleWithoutRtcpMux, remote_offer);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002152 // Trying unmodified SDP.
2153 SetLocalDescriptionWithoutError(offer);
2154}
2155
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156TEST_F(WebRtcSessionTest, SetAudioPlayout) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002157 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002158 mediastream_signaling_.SendAudioVideoStream1();
2159 CreateAndSetRemoteOfferAndLocalAnswer();
2160 cricket::FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
2161 ASSERT_TRUE(channel != NULL);
2162 ASSERT_EQ(1u, channel->recv_streams().size());
2163 uint32 receive_ssrc = channel->recv_streams()[0].first_ssrc();
2164 double left_vol, right_vol;
2165 EXPECT_TRUE(channel->GetOutputScaling(receive_ssrc, &left_vol, &right_vol));
2166 EXPECT_EQ(1, left_vol);
2167 EXPECT_EQ(1, right_vol);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002168 talk_base::scoped_ptr<FakeAudioRenderer> renderer(new FakeAudioRenderer());
2169 session_->SetAudioPlayout(receive_ssrc, false, renderer.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170 EXPECT_TRUE(channel->GetOutputScaling(receive_ssrc, &left_vol, &right_vol));
2171 EXPECT_EQ(0, left_vol);
2172 EXPECT_EQ(0, right_vol);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002173 EXPECT_EQ(0, renderer->channel_id());
2174 session_->SetAudioPlayout(receive_ssrc, true, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175 EXPECT_TRUE(channel->GetOutputScaling(receive_ssrc, &left_vol, &right_vol));
2176 EXPECT_EQ(1, left_vol);
2177 EXPECT_EQ(1, right_vol);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002178 EXPECT_EQ(-1, renderer->channel_id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179}
2180
2181TEST_F(WebRtcSessionTest, SetAudioSend) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002182 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 mediastream_signaling_.SendAudioVideoStream1();
2184 CreateAndSetRemoteOfferAndLocalAnswer();
2185 cricket::FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
2186 ASSERT_TRUE(channel != NULL);
2187 ASSERT_EQ(1u, channel->send_streams().size());
2188 uint32 send_ssrc = channel->send_streams()[0].first_ssrc();
2189 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
2190
2191 cricket::AudioOptions options;
2192 options.echo_cancellation.Set(true);
2193
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002194 talk_base::scoped_ptr<FakeAudioRenderer> renderer(new FakeAudioRenderer());
2195 session_->SetAudioSend(send_ssrc, false, options, renderer.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196 EXPECT_TRUE(channel->IsStreamMuted(send_ssrc));
2197 EXPECT_FALSE(channel->options().echo_cancellation.IsSet());
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002198 EXPECT_EQ(0, renderer->channel_id());
henrike@webrtc.org26438052014-02-20 22:32:53 +00002199 EXPECT_TRUE(renderer->sink() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200
henrike@webrtc.org26438052014-02-20 22:32:53 +00002201 // This will trigger SetSink(NULL) to the |renderer|.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002202 session_->SetAudioSend(send_ssrc, true, options, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
2204 bool value;
2205 EXPECT_TRUE(channel->options().echo_cancellation.Get(&value));
2206 EXPECT_TRUE(value);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002207 EXPECT_EQ(-1, renderer->channel_id());
henrike@webrtc.org26438052014-02-20 22:32:53 +00002208 EXPECT_TRUE(renderer->sink() == NULL);
2209}
2210
2211TEST_F(WebRtcSessionTest, AudioRendererForLocalStream) {
2212 Init(NULL);
2213 mediastream_signaling_.SendAudioVideoStream1();
2214 CreateAndSetRemoteOfferAndLocalAnswer();
2215 cricket::FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
2216 ASSERT_TRUE(channel != NULL);
2217 ASSERT_EQ(1u, channel->send_streams().size());
2218 uint32 send_ssrc = channel->send_streams()[0].first_ssrc();
2219
2220 talk_base::scoped_ptr<FakeAudioRenderer> renderer(new FakeAudioRenderer());
2221 cricket::AudioOptions options;
2222 session_->SetAudioSend(send_ssrc, true, options, renderer.get());
2223 EXPECT_TRUE(renderer->sink() != NULL);
2224
2225 // Delete the |renderer| and it will trigger OnClose() to the sink, and this
2226 // will invalidate the |renderer_| pointer in the sink and prevent getting a
2227 // SetSink(NULL) callback afterwards.
2228 renderer.reset();
2229
2230 // This will trigger SetSink(NULL) if no OnClose() callback.
2231 session_->SetAudioSend(send_ssrc, true, options, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232}
2233
2234TEST_F(WebRtcSessionTest, SetVideoPlayout) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002235 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 mediastream_signaling_.SendAudioVideoStream1();
2237 CreateAndSetRemoteOfferAndLocalAnswer();
2238 cricket::FakeVideoMediaChannel* channel = media_engine_->GetVideoChannel(0);
2239 ASSERT_TRUE(channel != NULL);
2240 ASSERT_LT(0u, channel->renderers().size());
2241 EXPECT_TRUE(channel->renderers().begin()->second == NULL);
2242 ASSERT_EQ(1u, channel->recv_streams().size());
2243 uint32 receive_ssrc = channel->recv_streams()[0].first_ssrc();
2244 cricket::FakeVideoRenderer renderer;
2245 session_->SetVideoPlayout(receive_ssrc, true, &renderer);
2246 EXPECT_TRUE(channel->renderers().begin()->second == &renderer);
2247 session_->SetVideoPlayout(receive_ssrc, false, &renderer);
2248 EXPECT_TRUE(channel->renderers().begin()->second == NULL);
2249}
2250
2251TEST_F(WebRtcSessionTest, SetVideoSend) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002252 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253 mediastream_signaling_.SendAudioVideoStream1();
2254 CreateAndSetRemoteOfferAndLocalAnswer();
2255 cricket::FakeVideoMediaChannel* channel = media_engine_->GetVideoChannel(0);
2256 ASSERT_TRUE(channel != NULL);
2257 ASSERT_EQ(1u, channel->send_streams().size());
2258 uint32 send_ssrc = channel->send_streams()[0].first_ssrc();
2259 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
2260 cricket::VideoOptions* options = NULL;
2261 session_->SetVideoSend(send_ssrc, false, options);
2262 EXPECT_TRUE(channel->IsStreamMuted(send_ssrc));
2263 session_->SetVideoSend(send_ssrc, true, options);
2264 EXPECT_FALSE(channel->IsStreamMuted(send_ssrc));
2265}
2266
2267TEST_F(WebRtcSessionTest, CanNotInsertDtmf) {
2268 TestCanInsertDtmf(false);
2269}
2270
2271TEST_F(WebRtcSessionTest, CanInsertDtmf) {
2272 TestCanInsertDtmf(true);
2273}
2274
2275TEST_F(WebRtcSessionTest, InsertDtmf) {
2276 // Setup
wu@webrtc.org91053e72013-08-10 07:18:04 +00002277 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002278 mediastream_signaling_.SendAudioVideoStream1();
2279 CreateAndSetRemoteOfferAndLocalAnswer();
2280 FakeVoiceMediaChannel* channel = media_engine_->GetVoiceChannel(0);
2281 EXPECT_EQ(0U, channel->dtmf_info_queue().size());
2282
2283 // Insert DTMF
2284 const int expected_flags = DF_SEND;
2285 const int expected_duration = 90;
2286 session_->InsertDtmf(kAudioTrack1, 0, expected_duration);
2287 session_->InsertDtmf(kAudioTrack1, 1, expected_duration);
2288 session_->InsertDtmf(kAudioTrack1, 2, expected_duration);
2289
2290 // Verify
2291 ASSERT_EQ(3U, channel->dtmf_info_queue().size());
2292 const uint32 send_ssrc = channel->send_streams()[0].first_ssrc();
2293 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[0], send_ssrc, 0,
2294 expected_duration, expected_flags));
2295 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[1], send_ssrc, 1,
2296 expected_duration, expected_flags));
2297 EXPECT_TRUE(CompareDtmfInfo(channel->dtmf_info_queue()[2], send_ssrc, 2,
2298 expected_duration, expected_flags));
2299}
2300
2301// This test verifies the |initiator| flag when session initiates the call.
2302TEST_F(WebRtcSessionTest, TestInitiatorFlagAsOriginator) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002303 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002304 EXPECT_FALSE(session_->initiator());
wu@webrtc.org91053e72013-08-10 07:18:04 +00002305 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
2307 SetLocalDescriptionWithoutError(offer);
2308 EXPECT_TRUE(session_->initiator());
2309 SetRemoteDescriptionWithoutError(answer);
2310 EXPECT_TRUE(session_->initiator());
2311}
2312
2313// This test verifies the |initiator| flag when session receives the call.
2314TEST_F(WebRtcSessionTest, TestInitiatorFlagAsReceiver) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002315 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002316 EXPECT_FALSE(session_->initiator());
2317 SessionDescriptionInterface* offer = CreateRemoteOffer();
2318 SetRemoteDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002319 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320
2321 EXPECT_FALSE(session_->initiator());
2322 SetLocalDescriptionWithoutError(answer);
2323 EXPECT_FALSE(session_->initiator());
2324}
2325
2326// This test verifies the ice protocol type at initiator of the call
2327// if |a=ice-options:google-ice| is present in answer.
2328TEST_F(WebRtcSessionTest, TestInitiatorGIceInAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002329 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002331 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org723d6832013-07-12 16:04:50 +00002332 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002333 CreateRemoteAnswer(offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334 SetLocalDescriptionWithoutError(offer);
2335 std::string sdp;
2336 EXPECT_TRUE(answer->ToString(&sdp));
2337 // Adding ice-options to the session level.
2338 InjectAfter("t=0 0\r\n",
2339 "a=ice-options:google-ice\r\n",
2340 &sdp);
2341 SessionDescriptionInterface* answer_with_gice =
2342 CreateSessionDescription(JsepSessionDescription::kAnswer, sdp, NULL);
2343 SetRemoteDescriptionWithoutError(answer_with_gice);
2344 VerifyTransportType("audio", cricket::ICEPROTO_GOOGLE);
2345 VerifyTransportType("video", cricket::ICEPROTO_GOOGLE);
2346}
2347
2348// This test verifies the ice protocol type at initiator of the call
2349// if ICE RFC5245 is supported in answer.
2350TEST_F(WebRtcSessionTest, TestInitiatorIceInAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002351 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002353 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354 SessionDescriptionInterface* answer = CreateRemoteAnswer(offer);
2355 SetLocalDescriptionWithoutError(offer);
2356
2357 SetRemoteDescriptionWithoutError(answer);
2358 VerifyTransportType("audio", cricket::ICEPROTO_RFC5245);
2359 VerifyTransportType("video", cricket::ICEPROTO_RFC5245);
2360}
2361
2362// This test verifies the ice protocol type at receiver side of the call if
2363// receiver decides to use google-ice.
2364TEST_F(WebRtcSessionTest, TestReceiverGIceInOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002365 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002367 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002368 SetRemoteDescriptionWithoutError(offer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +00002369 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002370 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002371 std::string sdp;
2372 EXPECT_TRUE(answer->ToString(&sdp));
2373 // Adding ice-options to the session level.
2374 InjectAfter("t=0 0\r\n",
2375 "a=ice-options:google-ice\r\n",
2376 &sdp);
2377 SessionDescriptionInterface* answer_with_gice =
2378 CreateSessionDescription(JsepSessionDescription::kAnswer, sdp, NULL);
2379 SetLocalDescriptionWithoutError(answer_with_gice);
2380 VerifyTransportType("audio", cricket::ICEPROTO_GOOGLE);
2381 VerifyTransportType("video", cricket::ICEPROTO_GOOGLE);
2382}
2383
2384// This test verifies the ice protocol type at receiver side of the call if
2385// receiver decides to use ice RFC 5245.
2386TEST_F(WebRtcSessionTest, TestReceiverIceInOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002387 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002388 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002389 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002390 SetRemoteDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002391 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002392 SetLocalDescriptionWithoutError(answer);
2393 VerifyTransportType("audio", cricket::ICEPROTO_RFC5245);
2394 VerifyTransportType("video", cricket::ICEPROTO_RFC5245);
2395}
2396
2397// This test verifies the session state when ICE RFC5245 in offer and
2398// ICE google-ice in answer.
2399TEST_F(WebRtcSessionTest, TestIceOfferGIceOnlyAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002400 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401 mediastream_signaling_.SendAudioVideoStream1();
2402 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002403 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002404 std::string offer_str;
2405 offer->ToString(&offer_str);
2406 // Disable google-ice
2407 const std::string gice_option = "google-ice";
2408 const std::string xgoogle_xice = "xgoogle-xice";
2409 talk_base::replace_substrs(gice_option.c_str(), gice_option.length(),
2410 xgoogle_xice.c_str(), xgoogle_xice.length(),
2411 &offer_str);
2412 JsepSessionDescription *ice_only_offer =
2413 new JsepSessionDescription(JsepSessionDescription::kOffer);
2414 EXPECT_TRUE((ice_only_offer)->Initialize(offer_str, NULL));
2415 SetLocalDescriptionWithoutError(ice_only_offer);
2416 std::string original_offer_sdp;
2417 EXPECT_TRUE(offer->ToString(&original_offer_sdp));
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002418 SessionDescriptionInterface* pranswer_with_gice =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419 CreateSessionDescription(JsepSessionDescription::kPrAnswer,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002420 original_offer_sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002421 SetRemoteDescriptionPranswerExpectError(kPushDownTDFailed,
2422 pranswer_with_gice);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002423 SessionDescriptionInterface* answer_with_gice =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002424 CreateSessionDescription(JsepSessionDescription::kAnswer,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002425 original_offer_sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002426 SetRemoteDescriptionAnswerExpectError(kPushDownTDFailed,
2427 answer_with_gice);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002428}
2429
2430// Verifing local offer and remote answer have matching m-lines as per RFC 3264.
2431TEST_F(WebRtcSessionTest, TestIncorrectMLinesInRemoteAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002432 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002433 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002434 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002435 SetLocalDescriptionWithoutError(offer);
2436 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
2437 CreateRemoteAnswer(session_->local_description()));
2438
2439 cricket::SessionDescription* answer_copy = answer->description()->Copy();
2440 answer_copy->RemoveContentByName("video");
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002441 JsepSessionDescription* modified_answer =
2442 new JsepSessionDescription(JsepSessionDescription::kAnswer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002443
2444 EXPECT_TRUE(modified_answer->Initialize(answer_copy,
2445 answer->session_id(),
2446 answer->session_version()));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002447 SetRemoteDescriptionAnswerExpectError(kMlineMismatch, modified_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002448
2449 // Modifying content names.
2450 std::string sdp;
2451 EXPECT_TRUE(answer->ToString(&sdp));
2452 const std::string kAudioMid = "a=mid:audio";
2453 const std::string kAudioMidReplaceStr = "a=mid:audio_content_name";
2454
2455 // Replacing |audio| with |audio_content_name|.
2456 talk_base::replace_substrs(kAudioMid.c_str(), kAudioMid.length(),
2457 kAudioMidReplaceStr.c_str(),
2458 kAudioMidReplaceStr.length(),
2459 &sdp);
2460
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002461 SessionDescriptionInterface* modified_answer1 =
2462 CreateSessionDescription(JsepSessionDescription::kAnswer, sdp, NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002463 SetRemoteDescriptionAnswerExpectError(kMlineMismatch, modified_answer1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002464
2465 SetRemoteDescriptionWithoutError(answer.release());
2466}
2467
2468// Verifying remote offer and local answer have matching m-lines as per
2469// RFC 3264.
2470TEST_F(WebRtcSessionTest, TestIncorrectMLinesInLocalAnswer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002471 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472 mediastream_signaling_.SendAudioVideoStream1();
2473 SessionDescriptionInterface* offer = CreateRemoteOffer();
2474 SetRemoteDescriptionWithoutError(offer);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002475 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476
2477 cricket::SessionDescription* answer_copy = answer->description()->Copy();
2478 answer_copy->RemoveContentByName("video");
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002479 JsepSessionDescription* modified_answer =
2480 new JsepSessionDescription(JsepSessionDescription::kAnswer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002481
2482 EXPECT_TRUE(modified_answer->Initialize(answer_copy,
2483 answer->session_id(),
2484 answer->session_version()));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002485 SetLocalDescriptionAnswerExpectError(kMlineMismatch, modified_answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002486 SetLocalDescriptionWithoutError(answer);
2487}
2488
2489// This test verifies that WebRtcSession does not start candidate allocation
2490// before SetLocalDescription is called.
2491TEST_F(WebRtcSessionTest, TestIceStartAfterSetLocalDescriptionOnly) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002492 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002493 mediastream_signaling_.SendAudioVideoStream1();
2494 SessionDescriptionInterface* offer = CreateRemoteOffer();
2495 cricket::Candidate candidate;
2496 candidate.set_component(1);
2497 JsepIceCandidate ice_candidate(kMediaContentName0, kMediaContentIndex0,
2498 candidate);
2499 EXPECT_TRUE(offer->AddCandidate(&ice_candidate));
2500 cricket::Candidate candidate1;
2501 candidate1.set_component(1);
2502 JsepIceCandidate ice_candidate1(kMediaContentName1, kMediaContentIndex1,
2503 candidate1);
2504 EXPECT_TRUE(offer->AddCandidate(&ice_candidate1));
2505 SetRemoteDescriptionWithoutError(offer);
2506 ASSERT_TRUE(session_->GetTransportProxy("audio") != NULL);
2507 ASSERT_TRUE(session_->GetTransportProxy("video") != NULL);
2508
2509 // Pump for 1 second and verify that no candidates are generated.
2510 talk_base::Thread::Current()->ProcessMessages(1000);
2511 EXPECT_TRUE(observer_.mline_0_candidates_.empty());
2512 EXPECT_TRUE(observer_.mline_1_candidates_.empty());
2513
wu@webrtc.org91053e72013-08-10 07:18:04 +00002514 SessionDescriptionInterface* answer = CreateAnswer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002515 SetLocalDescriptionWithoutError(answer);
2516 EXPECT_TRUE(session_->GetTransportProxy("audio")->negotiated());
2517 EXPECT_TRUE(session_->GetTransportProxy("video")->negotiated());
2518 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
2519}
2520
2521// This test verifies that crypto parameter is updated in local session
2522// description as per security policy set in MediaSessionDescriptionFactory.
2523TEST_F(WebRtcSessionTest, TestCryptoAfterSetLocalDescription) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002524 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002525 mediastream_signaling_.SendAudioVideoStream1();
2526 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002527 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002528
2529 // Making sure SetLocalDescription correctly sets crypto value in
2530 // SessionDescription object after de-serialization of sdp string. The value
2531 // will be set as per MediaSessionDescriptionFactory.
2532 std::string offer_str;
2533 offer->ToString(&offer_str);
2534 SessionDescriptionInterface* jsep_offer_str =
2535 CreateSessionDescription(JsepSessionDescription::kOffer, offer_str, NULL);
2536 SetLocalDescriptionWithoutError(jsep_offer_str);
2537 EXPECT_TRUE(session_->voice_channel()->secure_required());
2538 EXPECT_TRUE(session_->video_channel()->secure_required());
2539}
2540
2541// This test verifies the crypto parameter when security is disabled.
2542TEST_F(WebRtcSessionTest, TestCryptoAfterSetLocalDescriptionWithDisabled) {
wu@webrtc.org97077a32013-10-25 21:18:33 +00002543 options_.disable_encryption = true;
wu@webrtc.org91053e72013-08-10 07:18:04 +00002544 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002545 mediastream_signaling_.SendAudioVideoStream1();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002546 talk_base::scoped_ptr<SessionDescriptionInterface> offer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002547 CreateOffer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002548
2549 // Making sure SetLocalDescription correctly sets crypto value in
2550 // SessionDescription object after de-serialization of sdp string. The value
2551 // will be set as per MediaSessionDescriptionFactory.
2552 std::string offer_str;
2553 offer->ToString(&offer_str);
2554 SessionDescriptionInterface *jsep_offer_str =
2555 CreateSessionDescription(JsepSessionDescription::kOffer, offer_str, NULL);
2556 SetLocalDescriptionWithoutError(jsep_offer_str);
2557 EXPECT_FALSE(session_->voice_channel()->secure_required());
2558 EXPECT_FALSE(session_->video_channel()->secure_required());
2559}
2560
2561// This test verifies that an answer contains new ufrag and password if an offer
2562// with new ufrag and password is received.
2563TEST_F(WebRtcSessionTest, TestCreateAnswerWithNewUfragAndPassword) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002564 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002565 cricket::MediaSessionOptions options;
2566 options.has_audio = true;
2567 options.has_video = true;
2568 talk_base::scoped_ptr<JsepSessionDescription> offer(
2569 CreateRemoteOffer(options));
2570 SetRemoteDescriptionWithoutError(offer.release());
2571
2572 mediastream_signaling_.SendAudioVideoStream1();
2573 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002574 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002575 SetLocalDescriptionWithoutError(answer.release());
2576
2577 // Receive an offer with new ufrag and password.
2578 options.transport_options.ice_restart = true;
2579 talk_base::scoped_ptr<JsepSessionDescription> updated_offer1(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002580 CreateRemoteOffer(options, session_->remote_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002581 SetRemoteDescriptionWithoutError(updated_offer1.release());
2582
2583 talk_base::scoped_ptr<SessionDescriptionInterface> updated_answer1(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002584 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002585
2586 CompareIceUfragAndPassword(updated_answer1->description(),
2587 session_->local_description()->description(),
2588 false);
2589
2590 SetLocalDescriptionWithoutError(updated_answer1.release());
wu@webrtc.org91053e72013-08-10 07:18:04 +00002591}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002592
wu@webrtc.org91053e72013-08-10 07:18:04 +00002593// This test verifies that an answer contains old ufrag and password if an offer
2594// with old ufrag and password is received.
2595TEST_F(WebRtcSessionTest, TestCreateAnswerWithOldUfragAndPassword) {
2596 Init(NULL);
2597 cricket::MediaSessionOptions options;
2598 options.has_audio = true;
2599 options.has_video = true;
2600 talk_base::scoped_ptr<JsepSessionDescription> offer(
2601 CreateRemoteOffer(options));
2602 SetRemoteDescriptionWithoutError(offer.release());
2603
2604 mediastream_signaling_.SendAudioVideoStream1();
2605 talk_base::scoped_ptr<SessionDescriptionInterface> answer(
2606 CreateAnswer(NULL));
2607 SetLocalDescriptionWithoutError(answer.release());
2608
2609 // Receive an offer without changed ufrag or password.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002610 options.transport_options.ice_restart = false;
2611 talk_base::scoped_ptr<JsepSessionDescription> updated_offer2(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002612 CreateRemoteOffer(options, session_->remote_description()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002613 SetRemoteDescriptionWithoutError(updated_offer2.release());
2614
2615 talk_base::scoped_ptr<SessionDescriptionInterface> updated_answer2(
wu@webrtc.org91053e72013-08-10 07:18:04 +00002616 CreateAnswer(NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002617
2618 CompareIceUfragAndPassword(updated_answer2->description(),
2619 session_->local_description()->description(),
2620 true);
2621
2622 SetLocalDescriptionWithoutError(updated_answer2.release());
2623}
2624
2625TEST_F(WebRtcSessionTest, TestSessionContentError) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002626 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002627 mediastream_signaling_.SendAudioVideoStream1();
wu@webrtc.org91053e72013-08-10 07:18:04 +00002628 SessionDescriptionInterface* offer = CreateOffer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002629 const std::string session_id_orig = offer->session_id();
2630 const std::string session_version_orig = offer->session_version();
2631 SetLocalDescriptionWithoutError(offer);
2632
2633 video_channel_ = media_engine_->GetVideoChannel(0);
2634 video_channel_->set_fail_set_send_codecs(true);
2635
2636 mediastream_signaling_.SendAudioVideoStream2();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002637 SessionDescriptionInterface* answer =
2638 CreateRemoteAnswer(session_->local_description());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002639 SetRemoteDescriptionAnswerExpectError("ERROR_CONTENT", answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002640}
2641
2642// Runs the loopback call test with BUNDLE and STUN disabled.
2643TEST_F(WebRtcSessionTest, TestIceStatesBasic) {
2644 // Lets try with only UDP ports.
2645 allocator_.set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
2646 cricket::PORTALLOCATOR_DISABLE_TCP |
2647 cricket::PORTALLOCATOR_DISABLE_STUN |
2648 cricket::PORTALLOCATOR_DISABLE_RELAY);
2649 TestLoopbackCall();
2650}
2651
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00002652// Runs the loopback call test with BUNDLE, STUN, and TCP enabled.
2653TEST_F(WebRtcSessionTest, TestIceStatesBundle) {
2654 allocator_.set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
2655 cricket::PORTALLOCATOR_ENABLE_BUNDLE |
2656 cricket::PORTALLOCATOR_DISABLE_RELAY);
2657 TestLoopbackCall();
2658}
2659
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002660TEST_F(WebRtcSessionTest, SetSdpFailedOnSessionError) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00002661 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002662 cricket::MediaSessionOptions options;
2663 options.has_audio = true;
2664 options.has_video = true;
2665
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002666 cricket::BaseSession::Error error_code = cricket::BaseSession::ERROR_CONTENT;
2667 std::string error_code_str = "ERROR_CONTENT";
2668 std::string error_desc = "Fake session error description.";
2669 session_->SetError(error_code, error_desc);
2670
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002671 SessionDescriptionInterface* offer = CreateRemoteOffer(options);
2672 SessionDescriptionInterface* answer =
2673 CreateRemoteAnswer(offer, options);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002674
2675 std::string action;
2676 std::ostringstream session_error_msg;
2677 session_error_msg << kSessionError << error_code_str << ". ";
2678 session_error_msg << kSessionErrorDesc << error_desc << ".";
2679 SetRemoteDescriptionExpectError(action, session_error_msg.str(), offer);
2680 SetLocalDescriptionExpectError(action, session_error_msg.str(), answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002681}
2682
2683TEST_F(WebRtcSessionTest, TestRtpDataChannel) {
2684 constraints_.reset(new FakeConstraints());
2685 constraints_->AddOptional(
2686 webrtc::MediaConstraintsInterface::kEnableRtpDataChannels, true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002687 Init(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002688
2689 SetLocalDescriptionWithDataChannel();
2690 EXPECT_EQ(cricket::DCT_RTP, data_engine_->last_channel_type());
2691}
2692
2693TEST_F(WebRtcSessionTest, TestRtpDataChannelConstraintTakesPrecedence) {
2694 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2695
2696 constraints_.reset(new FakeConstraints());
2697 constraints_->AddOptional(
2698 webrtc::MediaConstraintsInterface::kEnableRtpDataChannels, true);
wu@webrtc.org97077a32013-10-25 21:18:33 +00002699 options_.disable_sctp_data_channels = false;
2700
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002701 InitWithDtls(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002702
2703 SetLocalDescriptionWithDataChannel();
2704 EXPECT_EQ(cricket::DCT_RTP, data_engine_->last_channel_type());
2705}
2706
wu@webrtc.org967bfff2013-09-19 05:49:50 +00002707TEST_F(WebRtcSessionTest, TestCreateOfferWithSctpEnabledWithoutStreams) {
2708 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2709
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002710 InitWithDtls(false);
2711
2712 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateOffer(NULL));
2713 EXPECT_TRUE(offer->description()->GetContentByName("data") == NULL);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00002714 EXPECT_TRUE(offer->description()->GetTransportInfoByName("data") == NULL);
2715}
2716
2717TEST_F(WebRtcSessionTest, TestCreateAnswerWithSctpInOfferAndNoStreams) {
2718 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2719 SetFactoryDtlsSrtp();
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00002720 InitWithDtls(false);
2721
2722 // Create remote offer with SCTP.
2723 cricket::MediaSessionOptions options;
2724 options.data_channel_type = cricket::DCT_SCTP;
2725 JsepSessionDescription* offer =
2726 CreateRemoteOffer(options, cricket::SEC_ENABLED);
2727 SetRemoteDescriptionWithoutError(offer);
2728
2729 // Verifies the answer contains SCTP.
2730 talk_base::scoped_ptr<SessionDescriptionInterface> answer(CreateAnswer(NULL));
2731 EXPECT_TRUE(answer != NULL);
2732 EXPECT_TRUE(answer->description()->GetContentByName("data") != NULL);
2733 EXPECT_TRUE(answer->description()->GetTransportInfoByName("data") != NULL);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002734}
2735
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002736TEST_F(WebRtcSessionTest, TestSctpDataChannelWithoutDtls) {
2737 constraints_.reset(new FakeConstraints());
2738 constraints_->AddOptional(
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002739 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
2740 InitWithDtls(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002741
2742 SetLocalDescriptionWithDataChannel();
2743 EXPECT_EQ(cricket::DCT_NONE, data_engine_->last_channel_type());
2744}
2745
2746TEST_F(WebRtcSessionTest, TestSctpDataChannelWithDtls) {
2747 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2748
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002749 InitWithDtls(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002750
2751 SetLocalDescriptionWithDataChannel();
2752 EXPECT_EQ(cricket::DCT_SCTP, data_engine_->last_channel_type());
2753}
wu@webrtc.org91053e72013-08-10 07:18:04 +00002754
wu@webrtc.org97077a32013-10-25 21:18:33 +00002755TEST_F(WebRtcSessionTest, TestDisableSctpDataChannels) {
2756 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2757 options_.disable_sctp_data_channels = true;
2758 InitWithDtls(false);
2759
2760 SetLocalDescriptionWithDataChannel();
2761 EXPECT_EQ(cricket::DCT_NONE, data_engine_->last_channel_type());
2762}
2763
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002764TEST_F(WebRtcSessionTest, TestSctpDataChannelSendPortParsing) {
2765 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2766 const int new_send_port = 9998;
2767 const int new_recv_port = 7775;
2768
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002769 InitWithDtls(false);
2770 SetFactoryDtlsSrtp();
2771
2772 // By default, don't actually add the codecs to desc_factory_; they don't
2773 // actually get serialized for SCTP in BuildMediaDescription(). Instead,
2774 // let the session description get parsed. That'll get the proper codecs
2775 // into the stream.
2776 cricket::MediaSessionOptions options;
2777 JsepSessionDescription* offer = CreateRemoteOfferWithSctpPort(
2778 "stream1", new_send_port, options);
2779
2780 // SetRemoteDescription will take the ownership of the offer.
2781 SetRemoteDescriptionWithoutError(offer);
2782
2783 SessionDescriptionInterface* answer = ChangeSDPSctpPort(
2784 new_recv_port, CreateAnswer(NULL));
2785 ASSERT_TRUE(answer != NULL);
2786
2787 // Now set the local description, which'll take ownership of the answer.
2788 SetLocalDescriptionWithoutError(answer);
2789
2790 // TEST PLAN: Set the port number to something new, set it in the SDP,
2791 // and pass it all the way down.
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002792 webrtc::InternalDataChannelInit dci;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002793 dci.reliable = true;
2794 EXPECT_EQ(cricket::DCT_SCTP, data_engine_->last_channel_type());
2795 talk_base::scoped_refptr<webrtc::DataChannel> dc =
2796 session_->CreateDataChannel("datachannel", &dci);
2797
2798 cricket::FakeDataMediaChannel* ch = data_engine_->GetChannel(0);
2799 int portnum = -1;
2800 ASSERT_TRUE(ch != NULL);
2801 ASSERT_EQ(1UL, ch->send_codecs().size());
2802 EXPECT_EQ(cricket::kGoogleSctpDataCodecId, ch->send_codecs()[0].id);
2803 EXPECT_TRUE(!strcmp(cricket::kGoogleSctpDataCodecName,
2804 ch->send_codecs()[0].name.c_str()));
2805 EXPECT_TRUE(ch->send_codecs()[0].GetParam(cricket::kCodecParamPort,
2806 &portnum));
2807 EXPECT_EQ(new_send_port, portnum);
2808
2809 ASSERT_EQ(1UL, ch->recv_codecs().size());
2810 EXPECT_EQ(cricket::kGoogleSctpDataCodecId, ch->recv_codecs()[0].id);
2811 EXPECT_TRUE(!strcmp(cricket::kGoogleSctpDataCodecName,
2812 ch->recv_codecs()[0].name.c_str()));
2813 EXPECT_TRUE(ch->recv_codecs()[0].GetParam(cricket::kCodecParamPort,
2814 &portnum));
2815 EXPECT_EQ(new_recv_port, portnum);
2816}
2817
wu@webrtc.org91053e72013-08-10 07:18:04 +00002818// Verifies that CreateOffer succeeds when CreateOffer is called before async
2819// identity generation is finished.
2820TEST_F(WebRtcSessionTest, TestCreateOfferBeforeIdentityRequestReturnSuccess) {
2821 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org7666db72013-08-22 14:45:42 +00002822 InitWithDtls(false);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002823
2824 EXPECT_TRUE(session_->waiting_for_identity());
2825 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateOffer(NULL));
2826 EXPECT_TRUE(offer != NULL);
2827}
2828
2829// Verifies that CreateAnswer succeeds when CreateOffer is called before async
2830// identity generation is finished.
2831TEST_F(WebRtcSessionTest, TestCreateAnswerBeforeIdentityRequestReturnSuccess) {
2832 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org7666db72013-08-22 14:45:42 +00002833 InitWithDtls(false);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002834
2835 cricket::MediaSessionOptions options;
2836 scoped_ptr<JsepSessionDescription> offer(
2837 CreateRemoteOffer(options, cricket::SEC_REQUIRED));
2838 ASSERT_TRUE(offer.get() != NULL);
2839 SetRemoteDescriptionWithoutError(offer.release());
2840
2841 talk_base::scoped_ptr<SessionDescriptionInterface> answer(CreateAnswer(NULL));
2842 EXPECT_TRUE(answer != NULL);
2843}
2844
2845// Verifies that CreateOffer succeeds when CreateOffer is called after async
2846// identity generation is finished.
2847TEST_F(WebRtcSessionTest, TestCreateOfferAfterIdentityRequestReturnSuccess) {
2848 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org7666db72013-08-22 14:45:42 +00002849 InitWithDtls(false);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002850
2851 EXPECT_TRUE_WAIT(!session_->waiting_for_identity(), 1000);
2852 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateOffer(NULL));
2853 EXPECT_TRUE(offer != NULL);
2854}
2855
2856// Verifies that CreateOffer fails when CreateOffer is called after async
2857// identity generation fails.
2858TEST_F(WebRtcSessionTest, TestCreateOfferAfterIdentityRequestReturnFailure) {
2859 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org7666db72013-08-22 14:45:42 +00002860 InitWithDtls(true);
wu@webrtc.org91053e72013-08-10 07:18:04 +00002861
2862 EXPECT_TRUE_WAIT(!session_->waiting_for_identity(), 1000);
2863 talk_base::scoped_ptr<SessionDescriptionInterface> offer(CreateOffer(NULL));
2864 EXPECT_TRUE(offer == NULL);
2865}
2866
2867// Verifies that CreateOffer succeeds when Multiple CreateOffer calls are made
2868// before async identity generation is finished.
2869TEST_F(WebRtcSessionTest,
2870 TestMultipleCreateOfferBeforeIdentityRequestReturnSuccess) {
2871 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2872 VerifyMultipleAsyncCreateDescription(
2873 true, CreateSessionDescriptionRequest::kOffer);
2874}
2875
2876// Verifies that CreateOffer fails when Multiple CreateOffer calls are made
2877// before async identity generation fails.
2878TEST_F(WebRtcSessionTest,
2879 TestMultipleCreateOfferBeforeIdentityRequestReturnFailure) {
2880 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2881 VerifyMultipleAsyncCreateDescription(
2882 false, CreateSessionDescriptionRequest::kOffer);
2883}
2884
2885// Verifies that CreateAnswer succeeds when Multiple CreateAnswer calls are made
2886// before async identity generation is finished.
2887TEST_F(WebRtcSessionTest,
2888 TestMultipleCreateAnswerBeforeIdentityRequestReturnSuccess) {
2889 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2890 VerifyMultipleAsyncCreateDescription(
2891 true, CreateSessionDescriptionRequest::kAnswer);
2892}
2893
2894// Verifies that CreateAnswer fails when Multiple CreateAnswer calls are made
2895// before async identity generation fails.
2896TEST_F(WebRtcSessionTest,
2897 TestMultipleCreateAnswerBeforeIdentityRequestReturnFailure) {
2898 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
2899 VerifyMultipleAsyncCreateDescription(
2900 false, CreateSessionDescriptionRequest::kAnswer);
2901}
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002902
2903// Verifies that setRemoteDescription fails when DTLS is disabled and the remote
2904// offer has no SDES crypto but only DTLS fingerprint.
2905TEST_F(WebRtcSessionTest, TestSetRemoteOfferFailIfDtlsDisabledAndNoCrypto) {
2906 // Init without DTLS.
2907 Init(NULL);
2908 // Create a remote offer with secured transport disabled.
2909 cricket::MediaSessionOptions options;
2910 JsepSessionDescription* offer(CreateRemoteOffer(
2911 options, cricket::SEC_DISABLED));
2912 // Adds a DTLS fingerprint to the remote offer.
2913 cricket::SessionDescription* sdp = offer->description();
2914 TransportInfo* audio = sdp->GetTransportInfoByName("audio");
2915 ASSERT_TRUE(audio != NULL);
2916 ASSERT_TRUE(audio->description.identity_fingerprint.get() == NULL);
2917 audio->description.identity_fingerprint.reset(
2918 talk_base::SSLFingerprint::CreateFromRfc4572(
2919 talk_base::DIGEST_SHA_256, kFakeDtlsFingerprint));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002920 SetRemoteDescriptionOfferExpectError(kSdpWithoutSdesAndDtlsDisabled,
2921 offer);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002922}
2923
wu@webrtc.orgde305012013-10-31 15:40:38 +00002924// This test verifies DSCP is properly applied on the media channels.
2925TEST_F(WebRtcSessionTest, TestDscpConstraint) {
2926 constraints_.reset(new FakeConstraints());
2927 constraints_->AddOptional(
2928 webrtc::MediaConstraintsInterface::kEnableDscp, true);
2929 Init(NULL);
2930 mediastream_signaling_.SendAudioVideoStream1();
2931 SessionDescriptionInterface* offer = CreateOffer(NULL);
2932
2933 SetLocalDescriptionWithoutError(offer);
2934
2935 video_channel_ = media_engine_->GetVideoChannel(0);
2936 voice_channel_ = media_engine_->GetVoiceChannel(0);
2937
2938 ASSERT_TRUE(video_channel_ != NULL);
2939 ASSERT_TRUE(voice_channel_ != NULL);
2940 cricket::AudioOptions audio_options;
2941 EXPECT_TRUE(voice_channel_->GetOptions(&audio_options));
2942 cricket::VideoOptions video_options;
2943 EXPECT_TRUE(video_channel_->GetOptions(&video_options));
2944 EXPECT_TRUE(audio_options.dscp.IsSet());
2945 EXPECT_TRUE(audio_options.dscp.GetWithDefaultIfUnset(false));
2946 EXPECT_TRUE(video_options.dscp.IsSet());
2947 EXPECT_TRUE(video_options.dscp.GetWithDefaultIfUnset(false));
2948}
2949
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002950// TODO(bemasc): Add a TestIceStatesBundle with BUNDLE enabled. That test
2951// currently fails because upon disconnection and reconnection OnIceComplete is
2952// called more than once without returning to IceGatheringGathering.