blob: 3cf66d64d8827b3c4e71887306844904409a3cdb [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
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 <stdio.h>
29
30#include <algorithm>
31#include <list>
32#include <map>
33#include <vector>
34
35#include "talk/app/webrtc/dtmfsender.h"
jbauchac8869e2015-07-03 01:36:14 -070036#include "talk/app/webrtc/fakemetricsobserver.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/app/webrtc/fakeportallocatorfactory.h"
38#include "talk/app/webrtc/localaudiosource.h"
39#include "talk/app/webrtc/mediastreaminterface.h"
deadbeef0a6c4ca2015-10-06 11:38:28 -070040#include "talk/app/webrtc/peerconnection.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/app/webrtc/peerconnectionfactory.h"
42#include "talk/app/webrtc/peerconnectioninterface.h"
43#include "talk/app/webrtc/test/fakeaudiocapturemodule.h"
44#include "talk/app/webrtc/test/fakeconstraints.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020045#include "talk/app/webrtc/test/fakedtlsidentitystore.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000047#include "talk/app/webrtc/test/fakevideotrackrenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
49#include "talk/app/webrtc/videosourceinterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000050#include "talk/media/webrtc/fakewebrtcvideoengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000051#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000053#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000054#include "webrtc/base/scoped_ptr.h"
55#include "webrtc/base/ssladapter.h"
56#include "webrtc/base/sslstreamadapter.h"
57#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000058#include "webrtc/base/virtualsocketserver.h"
59#include "webrtc/p2p/base/constants.h"
60#include "webrtc/p2p/base/sessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
62#define MAYBE_SKIP_TEST(feature) \
63 if (!(feature())) { \
64 LOG(LS_INFO) << "Feature disabled... skipping"; \
65 return; \
66 }
67
68using cricket::ContentInfo;
69using cricket::FakeWebRtcVideoDecoder;
70using cricket::FakeWebRtcVideoDecoderFactory;
71using cricket::FakeWebRtcVideoEncoder;
72using cricket::FakeWebRtcVideoEncoderFactory;
73using cricket::MediaContentDescription;
74using webrtc::DataBuffer;
75using webrtc::DataChannelInterface;
76using webrtc::DtmfSender;
77using webrtc::DtmfSenderInterface;
78using webrtc::DtmfSenderObserverInterface;
79using webrtc::FakeConstraints;
80using webrtc::MediaConstraintsInterface;
81using webrtc::MediaStreamTrackInterface;
82using webrtc::MockCreateSessionDescriptionObserver;
83using webrtc::MockDataChannelObserver;
84using webrtc::MockSetSessionDescriptionObserver;
85using webrtc::MockStatsObserver;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000086using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020087using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088using webrtc::SessionDescriptionInterface;
89using webrtc::StreamCollectionInterface;
90
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000091static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000092// Disable for TSan v2, see
93// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
94// This declaration is also #ifdef'd as it causes uninitialized-variable
95// warnings.
96#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000098#endif
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000099static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100static const int kEndAudioFrameCount = 3;
101static const int kEndVideoFrameCount = 3;
102
103static const char kStreamLabelBase[] = "stream_label";
104static const char kVideoTrackLabelBase[] = "video_track";
105static const char kAudioTrackLabelBase[] = "audio_track";
106static const char kDataChannelLabel[] = "data_channel";
107
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000108// Disable for TSan v2, see
109// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
110// This declaration is also #ifdef'd as it causes unused-variable errors.
111#if !defined(THREAD_SANITIZER)
112// SRTP cipher name negotiated by the tests. This must be updated if the
113// default changes.
114static const char kDefaultSrtpCipher[] = "AES_CM_128_HMAC_SHA1_32";
115#endif
116
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117static void RemoveLinesFromSdp(const std::string& line_start,
118 std::string* sdp) {
119 const char kSdpLineEnd[] = "\r\n";
120 size_t ssrc_pos = 0;
121 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
122 std::string::npos) {
123 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
124 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
125 }
126}
127
128class SignalingMessageReceiver {
129 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 virtual void ReceiveSdpMessage(const std::string& type,
131 std::string& msg) = 0;
132 virtual void ReceiveIceMessage(const std::string& sdp_mid,
133 int sdp_mline_index,
134 const std::string& msg) = 0;
135
136 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700137 SignalingMessageReceiver() {}
138 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139};
140
deadbeefaf1b59c2015-10-15 12:08:41 -0700141class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
142 public SignalingMessageReceiver {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 public:
deadbeefaf1b59c2015-10-15 12:08:41 -0700144 static PeerConnectionTestClient* CreateClient(
145 const std::string& id,
146 const MediaConstraintsInterface* constraints,
147 const PeerConnectionFactory::Options* options) {
148 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
149 if (!client->Init(constraints, options)) {
150 delete client;
151 return nullptr;
152 }
153 return client;
154 }
155
156 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 while (!fake_video_renderers_.empty()) {
158 RenderMap::iterator it = fake_video_renderers_.begin();
159 delete it->second;
160 fake_video_renderers_.erase(it);
161 }
162 }
163
deadbeefaf1b59c2015-10-15 12:08:41 -0700164 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
deadbeefaf1b59c2015-10-15 12:08:41 -0700166 void Negotiate(bool audio, bool video) {
167 rtc::scoped_ptr<SessionDescriptionInterface> offer;
168 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169
deadbeefaf1b59c2015-10-15 12:08:41 -0700170 if (offer->description()->GetContentByName("audio")) {
171 offer->description()->GetContentByName("audio")->rejected = !audio;
172 }
173 if (offer->description()->GetContentByName("video")) {
174 offer->description()->GetContentByName("video")->rejected = !video;
175 }
176
177 std::string sdp;
178 EXPECT_TRUE(offer->ToString(&sdp));
179 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
180 signaling_message_receiver_->ReceiveSdpMessage(
181 webrtc::SessionDescriptionInterface::kOffer, sdp);
182 }
183
184 // SignalingMessageReceiver callback.
185 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
186 FilterIncomingSdpMessage(&msg);
187 if (type == webrtc::SessionDescriptionInterface::kOffer) {
188 HandleIncomingOffer(msg);
189 } else {
190 HandleIncomingAnswer(msg);
191 }
192 }
193
194 // SignalingMessageReceiver callback.
195 void ReceiveIceMessage(const std::string& sdp_mid,
196 int sdp_mline_index,
197 const std::string& msg) override {
198 LOG(INFO) << id_ << "ReceiveIceMessage";
199 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
200 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
201 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
202 }
203
204 // PeerConnectionObserver callbacks.
205 void OnSignalingChange(
206 webrtc::PeerConnectionInterface::SignalingState new_state) override {
207 EXPECT_EQ(pc()->signaling_state(), new_state);
208 }
209 void OnAddStream(webrtc::MediaStreamInterface* media_stream) override {
210 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
211 const std::string id = media_stream->GetVideoTracks()[i]->id();
212 ASSERT_TRUE(fake_video_renderers_.find(id) ==
213 fake_video_renderers_.end());
214 fake_video_renderers_[id] =
215 new webrtc::FakeVideoTrackRenderer(media_stream->GetVideoTracks()[i]);
216 }
217 }
218 void OnRemoveStream(webrtc::MediaStreamInterface* media_stream) override {}
219 void OnRenegotiationNeeded() override {}
220 void OnIceConnectionChange(
221 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
222 EXPECT_EQ(pc()->ice_connection_state(), new_state);
223 }
224 void OnIceGatheringChange(
225 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
226 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
227 }
228 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
229 LOG(INFO) << id_ << "OnIceCandidate";
230
231 std::string ice_sdp;
232 EXPECT_TRUE(candidate->ToString(&ice_sdp));
233 if (signaling_message_receiver_ == nullptr) {
234 // Remote party may be deleted.
235 return;
236 }
237 signaling_message_receiver_->ReceiveIceMessage(
238 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
239 }
240
241 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 video_constraints_ = video_constraint;
243 }
244
245 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700246 std::string stream_label =
247 kStreamLabelBase +
248 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000249 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000250 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
252 if (audio && can_receive_audio()) {
deadbeef8f46c632015-10-26 14:11:17 -0700253 FakeConstraints constraints;
254 // Disable highpass filter so that we can get all the test audio frames.
255 constraints.AddMandatory(
256 MediaConstraintsInterface::kHighpassFilter, false);
257 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
258 peer_connection_factory_->CreateAudioSource(&constraints);
259 // TODO(perkj): Test audio source when it is implemented. Currently audio
260 // always use the default input.
261 std::string label = stream_label + kAudioTrackLabelBase;
262 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
263 peer_connection_factory_->CreateAudioTrack(label, source));
264 stream->AddTrack(audio_track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 }
266 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000267 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 }
269
deadbeefaf1b59c2015-10-15 12:08:41 -0700270 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 }
272
deadbeefaf1b59c2015-10-15 12:08:41 -0700273 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274
275 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700276 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 }
278
279 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700280 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 signaling_message_receiver_ = signaling_message_receiver;
282 }
283
284 void EnableVideoDecoderFactory() {
285 video_decoder_factory_enabled_ = true;
286 fake_video_decoder_factory_->AddSupportedVideoCodecType(
287 webrtc::kVideoCodecVP8);
288 }
289
deadbeefaf1b59c2015-10-15 12:08:41 -0700290 void IceRestart() {
291 session_description_constraints_.SetMandatoryIceRestart(true);
292 SetExpectIceRestart(true);
293 }
294
295 void SetExpectIceRestart(bool expect_restart) {
296 expect_ice_restart_ = expect_restart;
297 }
298
299 bool ExpectIceRestart() const { return expect_ice_restart_; }
300
301 void SetReceiveAudioVideo(bool audio, bool video) {
302 SetReceiveAudio(audio);
303 SetReceiveVideo(video);
304 ASSERT_EQ(audio, can_receive_audio());
305 ASSERT_EQ(video, can_receive_video());
306 }
307
308 void SetReceiveAudio(bool audio) {
309 if (audio && can_receive_audio())
310 return;
311 session_description_constraints_.SetMandatoryReceiveAudio(audio);
312 }
313
314 void SetReceiveVideo(bool video) {
315 if (video && can_receive_video())
316 return;
317 session_description_constraints_.SetMandatoryReceiveVideo(video);
318 }
319
320 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
321
322 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
323
324 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
325
326 bool can_receive_audio() {
327 bool value;
328 if (webrtc::FindConstraint(&session_description_constraints_,
329 MediaConstraintsInterface::kOfferToReceiveAudio,
330 &value, nullptr)) {
331 return value;
332 }
333 return true;
334 }
335
336 bool can_receive_video() {
337 bool value;
338 if (webrtc::FindConstraint(&session_description_constraints_,
339 MediaConstraintsInterface::kOfferToReceiveVideo,
340 &value, nullptr)) {
341 return value;
342 }
343 return true;
344 }
345
346 void OnIceComplete() override { LOG(INFO) << id_ << "OnIceComplete"; }
347
348 void OnDataChannel(DataChannelInterface* data_channel) override {
349 LOG(INFO) << id_ << "OnDataChannel";
350 data_channel_ = data_channel;
351 data_observer_.reset(new MockDataChannelObserver(data_channel));
352 }
353
354 void CreateDataChannel() {
355 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
356 ASSERT_TRUE(data_channel_.get() != nullptr);
357 data_observer_.reset(new MockDataChannelObserver(data_channel_));
358 }
359
360 DataChannelInterface* data_channel() { return data_channel_; }
361 const MockDataChannelObserver* data_observer() const {
362 return data_observer_.get();
363 }
364
365 webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
366
367 void StopVideoCapturers() {
368 for (std::vector<cricket::VideoCapturer*>::iterator it =
369 video_capturers_.begin();
370 it != video_capturers_.end(); ++it) {
371 (*it)->Stop();
372 }
373 }
374
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375 bool AudioFramesReceivedCheck(int number_of_frames) const {
376 return number_of_frames <= fake_audio_capture_module_->frames_received();
377 }
378
379 bool VideoFramesReceivedCheck(int number_of_frames) {
380 if (video_decoder_factory_enabled_) {
381 const std::vector<FakeWebRtcVideoDecoder*>& decoders
382 = fake_video_decoder_factory_->decoders();
383 if (decoders.empty()) {
384 return number_of_frames <= 0;
385 }
386
387 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
388 it = decoders.begin(); it != decoders.end(); ++it) {
389 if (number_of_frames > (*it)->GetNumFramesReceived()) {
390 return false;
391 }
392 }
393 return true;
394 } else {
395 if (fake_video_renderers_.empty()) {
396 return number_of_frames <= 0;
397 }
398
399 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
400 it != fake_video_renderers_.end(); ++it) {
401 if (number_of_frames > it->second->num_rendered_frames()) {
402 return false;
403 }
404 }
405 return true;
406 }
407 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700408
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409 // Verify the CreateDtmfSender interface
410 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000411 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
412 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413
414 // We can't create a DTMF sender with an invalid audio track or a non local
415 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700416 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000417 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700418 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
419 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420
421 // We should be able to create a DTMF sender from a local track.
422 webrtc::AudioTrackInterface* localtrack =
423 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
424 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700425 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 dtmf_sender->RegisterObserver(observer.get());
427
428 // Test the DtmfSender object just created.
429 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
430 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
431
432 // We don't need to verify that the DTMF tones are actually sent out because
433 // that is already covered by the tests of the lower level components.
434
435 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
436 std::vector<std::string> tones;
437 tones.push_back("1");
438 tones.push_back("a");
439 tones.push_back("");
440 observer->Verify(tones);
441
442 dtmf_sender->UnregisterObserver();
443 }
444
445 // Verifies that the SessionDescription have rejected the appropriate media
446 // content.
447 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700448 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
449 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 const cricket::SessionDescription* remote_desc =
451 peer_connection_->remote_description()->description();
452 const cricket::SessionDescription* local_desc =
453 peer_connection_->local_description()->description();
454
455 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
456 if (remote_audio_content) {
457 const ContentInfo* audio_content =
458 GetFirstAudioContent(local_desc);
459 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
460 }
461
462 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
463 if (remote_video_content) {
464 const ContentInfo* video_content =
465 GetFirstVideoContent(local_desc);
466 EXPECT_EQ(can_receive_video(), !video_content->rejected);
467 }
468 }
469
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700471 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 const cricket::SessionDescription* desc =
473 peer_connection_->local_description()->description();
474 const cricket::ContentInfos& contents = desc->contents();
475
476 for (size_t index = 0; index < contents.size(); ++index) {
477 if (contents[index].rejected)
478 continue;
479 const cricket::TransportDescription* transport_desc =
480 desc->GetTransportDescriptionByName(contents[index].name);
481
482 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000483 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 if (ufragpair_it == ice_ufrag_pwd_.end()) {
485 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000486 ice_ufrag_pwd_[static_cast<int>(index)] =
487 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 } else if (ExpectIceRestart()) {
489 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
490 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
491 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
492 } else {
493 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
494 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
495 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
496 }
497 }
498 }
499
500 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000501 rtc::scoped_refptr<MockStatsObserver>
502 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000503 EXPECT_TRUE(peer_connection_->GetStats(
504 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700506 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 return observer->AudioOutputLevel();
508 }
509
510 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000511 rtc::scoped_refptr<MockStatsObserver>
512 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000513 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700514 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700516 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 return observer->AudioInputLevel();
518 }
519
520 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000521 rtc::scoped_refptr<MockStatsObserver>
522 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000523 EXPECT_TRUE(peer_connection_->GetStats(
524 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700526 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 return observer->BytesReceived();
528 }
529
530 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000531 rtc::scoped_refptr<MockStatsObserver>
532 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000533 EXPECT_TRUE(peer_connection_->GetStats(
534 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700536 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 return observer->BytesSent();
538 }
539
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000540 int GetAvailableReceivedBandwidthStats() {
541 rtc::scoped_refptr<MockStatsObserver>
542 observer(new rtc::RefCountedObject<MockStatsObserver>());
543 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700544 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000545 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700546 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000547 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000548 return bw;
549 }
550
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000551 std::string GetDtlsCipherStats() {
552 rtc::scoped_refptr<MockStatsObserver>
553 observer(new rtc::RefCountedObject<MockStatsObserver>());
554 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700555 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000556 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700557 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000558 return observer->DtlsCipher();
559 }
560
561 std::string GetSrtpCipherStats() {
562 rtc::scoped_refptr<MockStatsObserver>
563 observer(new rtc::RefCountedObject<MockStatsObserver>());
564 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700565 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000566 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700567 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000568 return observer->SrtpCipher();
569 }
570
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 int rendered_width() {
572 EXPECT_FALSE(fake_video_renderers_.empty());
573 return fake_video_renderers_.empty() ? 1 :
574 fake_video_renderers_.begin()->second->width();
575 }
576
577 int rendered_height() {
578 EXPECT_FALSE(fake_video_renderers_.empty());
579 return fake_video_renderers_.empty() ? 1 :
580 fake_video_renderers_.begin()->second->height();
581 }
582
583 size_t number_of_remote_streams() {
584 if (!pc())
585 return 0;
586 return pc()->remote_streams()->count();
587 }
588
589 StreamCollectionInterface* remote_streams() {
590 if (!pc()) {
591 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700592 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 }
594 return pc()->remote_streams();
595 }
596
597 StreamCollectionInterface* local_streams() {
598 if (!pc()) {
599 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700600 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 }
602 return pc()->local_streams();
603 }
604
605 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
606 return pc()->signaling_state();
607 }
608
609 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
610 return pc()->ice_connection_state();
611 }
612
613 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
614 return pc()->ice_gathering_state();
615 }
616
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 private:
618 class DummyDtmfObserver : public DtmfSenderObserverInterface {
619 public:
620 DummyDtmfObserver() : completed_(false) {}
621
622 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700623 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 tones_.push_back(tone);
625 if (tone.empty()) {
626 completed_ = true;
627 }
628 }
629
630 void Verify(const std::vector<std::string>& tones) const {
631 ASSERT_TRUE(tones_.size() == tones.size());
632 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
633 }
634
635 bool completed() const { return completed_; }
636
637 private:
638 bool completed_;
639 std::vector<std::string> tones_;
640 };
641
deadbeefaf1b59c2015-10-15 12:08:41 -0700642 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
643
644 bool Init(const MediaConstraintsInterface* constraints,
645 const PeerConnectionFactory::Options* options) {
646 EXPECT_TRUE(!peer_connection_);
647 EXPECT_TRUE(!peer_connection_factory_);
648 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
649 if (!allocator_factory_) {
650 return false;
651 }
652 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
653
654 if (fake_audio_capture_module_ == nullptr) {
655 return false;
656 }
657 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
658 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
659 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
660 rtc::Thread::Current(), rtc::Thread::Current(),
661 fake_audio_capture_module_, fake_video_encoder_factory_,
662 fake_video_decoder_factory_);
663 if (!peer_connection_factory_) {
664 return false;
665 }
666 if (options) {
667 peer_connection_factory_->SetOptions(*options);
668 }
669 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
670 constraints);
671 return peer_connection_.get() != nullptr;
672 }
673
deadbeef8f46c632015-10-26 14:11:17 -0700674 rtc::scoped_refptr<webrtc::VideoTrackInterface>
675 CreateLocalVideoTrack(const std::string stream_label) {
676 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
677 FakeConstraints source_constraints = video_constraints_;
678 source_constraints.SetMandatoryMaxFrameRate(10);
679
680 cricket::FakeVideoCapturer* fake_capturer =
681 new webrtc::FakePeriodicVideoCapturer();
682 video_capturers_.push_back(fake_capturer);
683 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
684 peer_connection_factory_->CreateVideoSource(
685 fake_capturer, &source_constraints);
686 std::string label = stream_label + kVideoTrackLabelBase;
687 return peer_connection_factory_->CreateVideoTrack(label, source);
688 }
689
deadbeefaf1b59c2015-10-15 12:08:41 -0700690 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
691 webrtc::PortAllocatorFactoryInterface* factory,
692 const MediaConstraintsInterface* constraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 // CreatePeerConnection with IceServers.
694 webrtc::PeerConnectionInterface::IceServers ice_servers;
695 webrtc::PeerConnectionInterface::IceServer ice_server;
696 ice_server.uri = "stun:stun.l.google.com:19302";
697 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000698
Henrik Boström5e56c592015-08-11 10:33:13 +0200699 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store(
700 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
701 : nullptr);
deadbeefaf1b59c2015-10-15 12:08:41 -0700702 return peer_connection_factory_->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200703 ice_servers, constraints, factory, dtls_identity_store.Pass(), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 }
705
706 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700707 LOG(INFO) << id_ << "HandleIncomingOffer ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 if (NumberOfLocalMediaStreams() == 0) {
709 // If we are not sending any streams ourselves it is time to add some.
710 AddMediaStream(true, true);
711 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000712 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700713 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000715 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 EXPECT_TRUE(DoCreateAnswer(answer.use()));
717 std::string sdp;
718 EXPECT_TRUE(answer->ToString(&sdp));
719 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700720 if (signaling_message_receiver_) {
721 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 webrtc::SessionDescriptionInterface::kAnswer, sdp);
723 }
724 }
725
726 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700727 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000728 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700729 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
731 }
732
733 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
734 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000735 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
736 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 MockCreateSessionDescriptionObserver>());
738 if (offer) {
739 pc()->CreateOffer(observer, &session_description_constraints_);
740 } else {
741 pc()->CreateAnswer(observer, &session_description_constraints_);
742 }
743 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
744 *desc = observer->release_desc();
745 if (observer->result() && ExpectIceRestart()) {
746 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
747 }
748 return observer->result();
749 }
750
751 bool DoCreateOffer(SessionDescriptionInterface** desc) {
752 return DoCreateOfferAnswer(desc, true);
753 }
754
755 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
756 return DoCreateOfferAnswer(desc, false);
757 }
758
759 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000760 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
761 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700763 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 pc()->SetLocalDescription(observer, desc);
765 // Ignore the observer result. If we wait for the result with
766 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
767 // before the offer which is an error.
768 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000769 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 // ProcessMessages waits at least 1ms but processes all messages before
771 // returning. Since this test is synchronous and send messages to the remote
772 // peer whenever a callback is invoked, this can lead to messages being
773 // sent to the remote peer in the wrong order.
774 // TODO(perkj): Find a way to check the result without risking that the
775 // order of sent messages are changed. Ex- by posting all messages that are
776 // sent to the remote peer.
777 return true;
778 }
779
780 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000781 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
782 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700784 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 pc()->SetRemoteDescription(observer, desc);
786 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
787 return observer->result();
788 }
789
790 // This modifies all received SDP messages before they are processed.
791 void FilterIncomingSdpMessage(std::string* sdp) {
792 if (remove_msid_) {
793 const char kSdpSsrcAttribute[] = "a=ssrc:";
794 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
795 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
796 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
797 }
798 if (remove_bundle_) {
799 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
800 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
801 }
802 if (remove_sdes_) {
803 const char kSdpSdesCryptoAttribute[] = "a=crypto";
804 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
805 }
806 }
807
deadbeefaf1b59c2015-10-15 12:08:41 -0700808 std::string id_;
809
810 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory_;
811 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
812 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
813 peer_connection_factory_;
814
815 typedef std::pair<std::string, std::string> IceUfragPwdPair;
816 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
817 bool expect_ice_restart_ = false;
818
819 // Needed to keep track of number of frames send.
820 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
821 // Needed to keep track of number of frames received.
822 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
823 RenderMap fake_video_renderers_;
824 // Needed to keep track of number of frames received when external decoder
825 // used.
826 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
827 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
828 bool video_decoder_factory_enabled_ = false;
829 webrtc::FakeConstraints video_constraints_;
830
831 // For remote peer communication.
832 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
833
834 // Store references to the video capturers we've created, so that we can stop
835 // them, if required.
836 std::vector<cricket::VideoCapturer*> video_capturers_;
837
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700839 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
840 bool remove_bundle_ =
841 false; // True if bundle should be removed in received SDP.
842 bool remove_sdes_ =
843 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000845 rtc::scoped_refptr<DataChannelInterface> data_channel_;
846 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847};
848
deadbeefcbc95072015-10-15 19:31:56 -0700849// TODO(deadbeef): Rename this to P2PTestConductor once the Linux memcheck and
850// Windows DrMemory Full bots' blacklists are updated.
851class JsepPeerConnectionP2PTestClient : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 public:
deadbeefcbc95072015-10-15 19:31:56 -0700853 JsepPeerConnectionP2PTestClient()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000854 : pss_(new rtc::PhysicalSocketServer),
855 ss_(new rtc::VirtualSocketServer(pss_.get())),
856 ss_scope_(ss_.get()) {}
857
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 bool SessionActive() {
859 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000860 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000862
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 // Return true if the number of frames provided have been received or it is
864 // known that that will never occur (e.g. no frames will be sent or
865 // captured).
866 bool FramesNotPending(int audio_frames_to_receive,
867 int video_frames_to_receive) {
868 return VideoFramesReceivedCheck(video_frames_to_receive) &&
869 AudioFramesReceivedCheck(audio_frames_to_receive);
870 }
871 bool AudioFramesReceivedCheck(int frames_received) {
872 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
873 receiving_client_->AudioFramesReceivedCheck(frames_received);
874 }
875 bool VideoFramesReceivedCheck(int frames_received) {
876 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
877 receiving_client_->VideoFramesReceivedCheck(frames_received);
878 }
879 void VerifyDtmf() {
880 initiating_client_->VerifyDtmf();
881 receiving_client_->VerifyDtmf();
882 }
883
884 void TestUpdateOfferWithRejectedContent() {
885 initiating_client_->Negotiate(true, false);
886 EXPECT_TRUE_WAIT(
887 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
888 kMaxWaitForFramesMs);
889 // There shouldn't be any more video frame after the new offer is
890 // negotiated.
891 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
892 }
893
894 void VerifyRenderedSize(int width, int height) {
895 EXPECT_EQ(width, receiving_client()->rendered_width());
896 EXPECT_EQ(height, receiving_client()->rendered_height());
897 EXPECT_EQ(width, initializing_client()->rendered_width());
898 EXPECT_EQ(height, initializing_client()->rendered_height());
899 }
900
901 void VerifySessionDescriptions() {
902 initiating_client_->VerifyRejectedMediaInSessionDescription();
903 receiving_client_->VerifyRejectedMediaInSessionDescription();
904 initiating_client_->VerifyLocalIceUfragAndPassword();
905 receiving_client_->VerifyLocalIceUfragAndPassword();
906 }
907
deadbeefcbc95072015-10-15 19:31:56 -0700908 ~JsepPeerConnectionP2PTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700910 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 }
912 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700913 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 }
916
deadbeefaf1b59c2015-10-15 12:08:41 -0700917 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918
919 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
920 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700921 return CreateTestClients(init_constraints, nullptr, recv_constraints,
922 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +0200923 }
924
925 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
926 PeerConnectionFactory::Options* init_options,
927 MediaConstraintsInterface* recv_constraints,
928 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700929 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
930 "Caller: ", init_constraints, init_options));
931 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
932 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 if (!initiating_client_ || !receiving_client_) {
934 return false;
935 }
936 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
937 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
938 return true;
939 }
940
941 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
942 const webrtc::FakeConstraints& recv_constraints) {
943 initiating_client_->SetVideoConstraints(init_constraints);
944 receiving_client_->SetVideoConstraints(recv_constraints);
945 }
946
947 void EnableVideoDecoderFactory() {
948 initiating_client_->EnableVideoDecoderFactory();
949 receiving_client_->EnableVideoDecoderFactory();
950 }
951
952 // This test sets up a call between two parties. Both parties send static
953 // frames to each other. Once the test is finished the number of sent frames
954 // is compared to the number of received frames.
955 void LocalP2PTest() {
956 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
957 initiating_client_->AddMediaStream(true, true);
958 }
959 initiating_client_->Negotiate();
deadbeef8f46c632015-10-26 14:11:17 -0700960 const int kMaxWaitForActivationMs = 5000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 // Assert true is used here since next tests are guaranteed to fail and
962 // would eat up 5 seconds.
963 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
964 VerifySessionDescriptions();
965
deadbeef8f46c632015-10-26 14:11:17 -0700966
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 int audio_frame_count = kEndAudioFrameCount;
968 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
969 if (!initiating_client_->can_receive_audio() ||
970 !receiving_client_->can_receive_audio()) {
971 audio_frame_count = -1;
972 }
973 int video_frame_count = kEndVideoFrameCount;
974 if (!initiating_client_->can_receive_video() ||
975 !receiving_client_->can_receive_video()) {
976 video_frame_count = -1;
977 }
978
979 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000980 // Audio or video is expected to flow, so both clients should reach the
981 // Connected state, and the offerer (ICE controller) should proceed to
982 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 // Note: These tests have been observed to fail under heavy load at
984 // shorter timeouts, so they may be flaky.
985 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000986 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 initiating_client_->ice_connection_state(),
988 kMaxWaitForFramesMs);
989 EXPECT_EQ_WAIT(
990 webrtc::PeerConnectionInterface::kIceConnectionConnected,
991 receiving_client_->ice_connection_state(),
992 kMaxWaitForFramesMs);
993 }
994
995 if (initiating_client_->can_receive_audio() ||
996 initiating_client_->can_receive_video()) {
997 // The initiating client can receive media, so it must produce candidates
998 // that will serve as destinations for that media.
999 // TODO(bemasc): Understand why the state is not already Complete here, as
1000 // seems to be the case for the receiving client. This may indicate a bug
1001 // in the ICE gathering system.
1002 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1003 initiating_client_->ice_gathering_state());
1004 }
1005 if (receiving_client_->can_receive_audio() ||
1006 receiving_client_->can_receive_video()) {
1007 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1008 receiving_client_->ice_gathering_state(),
1009 kMaxWaitForFramesMs);
1010 }
1011
1012 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1013 kMaxWaitForFramesMs);
1014 }
1015
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001016 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1017 // Messages may get lost on the unreliable DataChannel, so we send multiple
1018 // times to avoid test flakiness.
1019 static const size_t kSendAttempts = 5;
1020
1021 for (size_t i = 0; i < kSendAttempts; ++i) {
1022 dc->Send(DataBuffer(data));
1023 }
1024 }
1025
deadbeefaf1b59c2015-10-15 12:08:41 -07001026 PeerConnectionTestClient* initializing_client() {
1027 return initiating_client_.get();
1028 }
1029 PeerConnectionTestClient* receiving_client() {
1030 return receiving_client_.get();
1031 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032
1033 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001034 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1035 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1036 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001037 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1038 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001041// Disable for TSan v2, see
1042// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1043#if !defined(THREAD_SANITIZER)
1044
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001046// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1047// See issue webrtc/2378.
deadbeefcbc95072015-10-15 19:31:56 -07001048TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 ASSERT_TRUE(CreateTestClients());
1050 LocalP2PTest();
1051 VerifyDtmf();
1052}
1053
1054// This test sets up a Jsep call between two parties and test that we can get a
1055// video aspect ratio of 16:9.
deadbeefcbc95072015-10-15 19:31:56 -07001056TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 ASSERT_TRUE(CreateTestClients());
1058 FakeConstraints constraint;
1059 double requested_ratio = 640.0/360;
1060 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1061 SetVideoConstraints(constraint, constraint);
1062 LocalP2PTest();
1063
1064 ASSERT_LE(0, initializing_client()->rendered_height());
1065 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001066 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 initializing_client()->rendered_height();
1068 EXPECT_LE(requested_ratio, initiating_video_ratio);
1069
1070 ASSERT_LE(0, receiving_client()->rendered_height());
1071 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001072 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 receiving_client()->rendered_height();
1074 EXPECT_LE(requested_ratio, receiving_video_ratio);
1075}
1076
1077// This test sets up a Jsep call between two parties and test that the
1078// received video has a resolution of 1280*720.
1079// TODO(mallinath): Enable when
1080// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeefcbc95072015-10-15 19:31:56 -07001081TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 ASSERT_TRUE(CreateTestClients());
1083 FakeConstraints constraint;
1084 constraint.SetMandatoryMinWidth(1280);
1085 constraint.SetMandatoryMinHeight(720);
1086 SetVideoConstraints(constraint, constraint);
1087 LocalP2PTest();
1088 VerifyRenderedSize(1280, 720);
1089}
1090
1091// This test sets up a call between two endpoints that are configured to use
1092// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeefcbc95072015-10-15 19:31:56 -07001093TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001094 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 FakeConstraints setup_constraints;
1096 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1097 true);
1098 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1099 LocalP2PTest();
1100 VerifyRenderedSize(640, 480);
1101}
1102
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001103// This test sets up a audio call initially and then upgrades to audio/video,
1104// using DTLS.
deadbeefcbc95072015-10-15 19:31:56 -07001105TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001106 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001107 FakeConstraints setup_constraints;
1108 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1109 true);
1110 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1111 receiving_client()->SetReceiveAudioVideo(true, false);
1112 LocalP2PTest();
1113 receiving_client()->SetReceiveAudioVideo(true, true);
1114 receiving_client()->Negotiate();
1115}
1116
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117// This test sets up a call between two endpoints that are configured to use
1118// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1119// negotiated and used for transport.
deadbeefcbc95072015-10-15 19:31:56 -07001120TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001121 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 FakeConstraints setup_constraints;
1123 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1124 true);
1125 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1126 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1127 LocalP2PTest();
1128 VerifyRenderedSize(640, 480);
1129}
1130
1131// This test sets up a Jsep call between two parties, and the callee only
1132// accept to receive video.
deadbeefcbc95072015-10-15 19:31:56 -07001133TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 ASSERT_TRUE(CreateTestClients());
1135 receiving_client()->SetReceiveAudioVideo(false, true);
1136 LocalP2PTest();
1137}
1138
1139// This test sets up a Jsep call between two parties, and the callee only
1140// accept to receive audio.
deadbeefcbc95072015-10-15 19:31:56 -07001141TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 ASSERT_TRUE(CreateTestClients());
1143 receiving_client()->SetReceiveAudioVideo(true, false);
1144 LocalP2PTest();
1145}
1146
1147// This test sets up a Jsep call between two parties, and the callee reject both
1148// audio and video.
deadbeefcbc95072015-10-15 19:31:56 -07001149TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 ASSERT_TRUE(CreateTestClients());
1151 receiving_client()->SetReceiveAudioVideo(false, false);
1152 LocalP2PTest();
1153}
1154
1155// This test sets up an audio and video call between two parties. After the call
1156// runs for a while (10 frames), the caller sends an update offer with video
1157// being rejected. Once the re-negotiation is done, the video flow should stop
1158// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001159// Disabled due to b/14955157.
deadbeefcbc95072015-10-15 19:31:56 -07001160TEST_F(JsepPeerConnectionP2PTestClient,
1161 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001162 ASSERT_TRUE(CreateTestClients());
1163 LocalP2PTest();
1164 TestUpdateOfferWithRejectedContent();
1165}
1166
1167// This test sets up a Jsep call between two parties. The MSID is removed from
1168// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001169// Disabled due to b/14955157.
deadbeefcbc95072015-10-15 19:31:56 -07001170TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 ASSERT_TRUE(CreateTestClients());
1172 receiving_client()->RemoveMsidFromReceivedSdp(true);
1173 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1174 // audio and video is muxed when MSID is disabled. Remove
1175 // SetRemoveBundleFromSdp once
1176 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1177 receiving_client()->RemoveBundleFromReceivedSdp(true);
1178 LocalP2PTest();
1179}
1180
1181// This test sets up a Jsep call between two parties and the initiating peer
1182// sends two steams.
1183// TODO(perkj): Disabled due to
1184// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeefcbc95072015-10-15 19:31:56 -07001185TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 ASSERT_TRUE(CreateTestClients());
1187 // Set optional video constraint to max 320pixels to decrease CPU usage.
1188 FakeConstraints constraint;
1189 constraint.SetOptionalMaxWidth(320);
1190 SetVideoConstraints(constraint, constraint);
1191 initializing_client()->AddMediaStream(true, true);
1192 initializing_client()->AddMediaStream(false, true);
1193 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1194 LocalP2PTest();
1195 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1196}
1197
1198// Test that we can receive the audio output level from a remote audio track.
deadbeefcbc95072015-10-15 19:31:56 -07001199TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 ASSERT_TRUE(CreateTestClients());
1201 LocalP2PTest();
1202
1203 StreamCollectionInterface* remote_streams =
1204 initializing_client()->remote_streams();
1205 ASSERT_GT(remote_streams->count(), 0u);
1206 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1207 MediaStreamTrackInterface* remote_audio_track =
1208 remote_streams->at(0)->GetAudioTracks()[0];
1209
1210 // Get the audio output level stats. Note that the level is not available
1211 // until a RTCP packet has been received.
1212 EXPECT_TRUE_WAIT(
1213 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1214 kMaxWaitForStatsMs);
1215}
1216
1217// Test that an audio input level is reported.
deadbeefcbc95072015-10-15 19:31:56 -07001218TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 ASSERT_TRUE(CreateTestClients());
1220 LocalP2PTest();
1221
1222 // Get the audio input level stats. The level should be available very
1223 // soon after the test starts.
1224 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1225 kMaxWaitForStatsMs);
1226}
1227
1228// Test that we can get incoming byte counts from both audio and video tracks.
deadbeefcbc95072015-10-15 19:31:56 -07001229TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 ASSERT_TRUE(CreateTestClients());
1231 LocalP2PTest();
1232
1233 StreamCollectionInterface* remote_streams =
1234 initializing_client()->remote_streams();
1235 ASSERT_GT(remote_streams->count(), 0u);
1236 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1237 MediaStreamTrackInterface* remote_audio_track =
1238 remote_streams->at(0)->GetAudioTracks()[0];
1239 EXPECT_TRUE_WAIT(
1240 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1241 kMaxWaitForStatsMs);
1242
1243 MediaStreamTrackInterface* remote_video_track =
1244 remote_streams->at(0)->GetVideoTracks()[0];
1245 EXPECT_TRUE_WAIT(
1246 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1247 kMaxWaitForStatsMs);
1248}
1249
1250// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeefcbc95072015-10-15 19:31:56 -07001251TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 ASSERT_TRUE(CreateTestClients());
1253 LocalP2PTest();
1254
1255 StreamCollectionInterface* local_streams =
1256 initializing_client()->local_streams();
1257 ASSERT_GT(local_streams->count(), 0u);
1258 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1259 MediaStreamTrackInterface* local_audio_track =
1260 local_streams->at(0)->GetAudioTracks()[0];
1261 EXPECT_TRUE_WAIT(
1262 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1263 kMaxWaitForStatsMs);
1264
1265 MediaStreamTrackInterface* local_video_track =
1266 local_streams->at(0)->GetVideoTracks()[0];
1267 EXPECT_TRUE_WAIT(
1268 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1269 kMaxWaitForStatsMs);
1270}
1271
Joachim Bauch04e5b492015-05-29 09:40:39 +02001272// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
deadbeefcbc95072015-10-15 19:31:56 -07001273TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001274 PeerConnectionFactory::Options init_options;
1275 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1276 PeerConnectionFactory::Options recv_options;
1277 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001278 ASSERT_TRUE(
1279 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001280 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1281 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1282 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001283 LocalP2PTest();
1284
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001285 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1286 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1287 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1288 initializing_client()->GetDtlsCipherStats(),
1289 kMaxWaitForStatsMs);
1290 EXPECT_EQ(1, init_observer->GetEnumCounter(
1291 webrtc::kEnumCounterAudioSslCipher,
1292 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1293 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001294
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001295 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1296 initializing_client()->GetSrtpCipherStats(),
1297 kMaxWaitForStatsMs);
1298 EXPECT_EQ(1, init_observer->GetEnumCounter(
1299 webrtc::kEnumCounterAudioSrtpCipher,
1300 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001301}
1302
1303// Test that DTLS 1.2 is used if both ends support it.
deadbeefcbc95072015-10-15 19:31:56 -07001304TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001305 PeerConnectionFactory::Options init_options;
1306 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1307 PeerConnectionFactory::Options recv_options;
1308 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001309 ASSERT_TRUE(
1310 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001311 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1312 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1313 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001314 LocalP2PTest();
1315
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001316 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1317 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1318 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1319 initializing_client()->GetDtlsCipherStats(),
1320 kMaxWaitForStatsMs);
1321 EXPECT_EQ(1, init_observer->GetEnumCounter(
1322 webrtc::kEnumCounterAudioSslCipher,
1323 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1324 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001325
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001326 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1327 initializing_client()->GetSrtpCipherStats(),
1328 kMaxWaitForStatsMs);
1329 EXPECT_EQ(1, init_observer->GetEnumCounter(
1330 webrtc::kEnumCounterAudioSrtpCipher,
1331 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001332}
1333
1334// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1335// received supports 1.0.
deadbeefcbc95072015-10-15 19:31:56 -07001336TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001337 PeerConnectionFactory::Options init_options;
1338 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1339 PeerConnectionFactory::Options recv_options;
1340 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001341 ASSERT_TRUE(
1342 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001343 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1344 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1345 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001346 LocalP2PTest();
1347
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001348 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1349 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1350 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1351 initializing_client()->GetDtlsCipherStats(),
1352 kMaxWaitForStatsMs);
1353 EXPECT_EQ(1, init_observer->GetEnumCounter(
1354 webrtc::kEnumCounterAudioSslCipher,
1355 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1356 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001357
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001358 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1359 initializing_client()->GetSrtpCipherStats(),
1360 kMaxWaitForStatsMs);
1361 EXPECT_EQ(1, init_observer->GetEnumCounter(
1362 webrtc::kEnumCounterAudioSrtpCipher,
1363 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001364}
1365
1366// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1367// received supports 1.2.
deadbeefcbc95072015-10-15 19:31:56 -07001368TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001369 PeerConnectionFactory::Options init_options;
1370 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1371 PeerConnectionFactory::Options recv_options;
1372 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001373 ASSERT_TRUE(
1374 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001375 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1376 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1377 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001378 LocalP2PTest();
1379
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001380 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1381 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1382 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1383 initializing_client()->GetDtlsCipherStats(),
1384 kMaxWaitForStatsMs);
1385 EXPECT_EQ(1, init_observer->GetEnumCounter(
1386 webrtc::kEnumCounterAudioSslCipher,
1387 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1388 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001389
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001390 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1391 initializing_client()->GetSrtpCipherStats(),
1392 kMaxWaitForStatsMs);
1393 EXPECT_EQ(1, init_observer->GetEnumCounter(
1394 webrtc::kEnumCounterAudioSrtpCipher,
1395 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001396}
1397
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398// This test sets up a call between two parties with audio, video and data.
deadbeefcbc95072015-10-15 19:31:56 -07001399TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400 FakeConstraints setup_constraints;
1401 setup_constraints.SetAllowRtpDataChannels();
1402 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1403 initializing_client()->CreateDataChannel();
1404 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001405 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1406 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001407 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1408 kMaxWaitMs);
1409 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1410 kMaxWaitMs);
1411
1412 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001413
1414 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1416 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001417
1418 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1420 kMaxWaitMs);
1421
1422 receiving_client()->data_channel()->Close();
1423 // Send new offer and answer.
1424 receiving_client()->Negotiate();
1425 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1426 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1427}
1428
1429// This test sets up a call between two parties and creates a data channel.
1430// The test tests that received data is buffered unless an observer has been
1431// registered.
1432// Rtp data channels can receive data before the underlying
1433// transport has detected that a channel is writable and thus data can be
1434// received before the data channel state changes to open. That is hard to test
1435// but the same buffering is used in that case.
deadbeefcbc95072015-10-15 19:31:56 -07001436TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 FakeConstraints setup_constraints;
1438 setup_constraints.SetAllowRtpDataChannels();
1439 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1440 initializing_client()->CreateDataChannel();
1441 initializing_client()->Negotiate();
1442
deadbeefaf1b59c2015-10-15 12:08:41 -07001443 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1444 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1446 kMaxWaitMs);
1447 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1448 receiving_client()->data_channel()->state(), kMaxWaitMs);
1449
1450 // Unregister the existing observer.
1451 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001452
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001454 SendRtpData(initializing_client()->data_channel(), data);
1455
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 // Wait a while to allow the sent data to arrive before an observer is
1457 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001458 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459
1460 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1461 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1462}
1463
1464// This test sets up a call between two parties with audio, video and but only
1465// the initiating client support data.
deadbeefcbc95072015-10-15 19:31:56 -07001466TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001467 FakeConstraints setup_constraints_1;
1468 setup_constraints_1.SetAllowRtpDataChannels();
1469 // Must disable DTLS to make negotiation succeed.
1470 setup_constraints_1.SetMandatory(
1471 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1472 FakeConstraints setup_constraints_2;
1473 setup_constraints_2.SetMandatory(
1474 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1475 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 initializing_client()->CreateDataChannel();
1477 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001478 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479 EXPECT_FALSE(receiving_client()->data_channel());
1480 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1481}
1482
1483// This test sets up a call between two parties with audio, video. When audio
1484// and video is setup and flowing and data channel is negotiated.
deadbeefcbc95072015-10-15 19:31:56 -07001485TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486 FakeConstraints setup_constraints;
1487 setup_constraints.SetAllowRtpDataChannels();
1488 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1489 LocalP2PTest();
1490 initializing_client()->CreateDataChannel();
1491 // Send new offer and answer.
1492 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001493 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1494 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1496 kMaxWaitMs);
1497 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1498 kMaxWaitMs);
1499}
1500
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001501// This test sets up a Jsep call with SCTP DataChannel and verifies the
1502// negotiation is completed without error.
1503#ifdef HAVE_SCTP
deadbeefcbc95072015-10-15 19:31:56 -07001504TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001505 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001506 FakeConstraints constraints;
1507 constraints.SetMandatory(
1508 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1509 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1510 initializing_client()->CreateDataChannel();
1511 initializing_client()->Negotiate(false, false);
1512}
1513#endif
1514
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515// This test sets up a call between two parties with audio, and video.
1516// During the call, the initializing side restart ice and the test verifies that
1517// new ice candidates are generated and audio and video still can flow.
deadbeefcbc95072015-10-15 19:31:56 -07001518TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 ASSERT_TRUE(CreateTestClients());
1520
1521 // Negotiate and wait for ice completion and make sure audio and video plays.
1522 LocalP2PTest();
1523
1524 // Create a SDP string of the first audio candidate for both clients.
1525 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1526 initializing_client()->pc()->local_description()->candidates(0);
1527 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1528 receiving_client()->pc()->local_description()->candidates(0);
1529 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1530 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1531 std::string initiator_candidate;
1532 EXPECT_TRUE(
1533 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1534 std::string receiver_candidate;
1535 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1536
1537 // Restart ice on the initializing client.
1538 receiving_client()->SetExpectIceRestart(true);
1539 initializing_client()->IceRestart();
1540
1541 // Negotiate and wait for ice completion again and make sure audio and video
1542 // plays.
1543 LocalP2PTest();
1544
1545 // Create a SDP string of the first audio candidate for both clients again.
1546 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1547 initializing_client()->pc()->local_description()->candidates(0);
1548 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1549 receiving_client()->pc()->local_description()->candidates(0);
1550 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1551 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1552 std::string initiator_candidate_restart;
1553 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1554 &initiator_candidate_restart));
1555 std::string receiver_candidate_restart;
1556 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1557 &receiver_candidate_restart));
1558
1559 // Verify that the first candidates in the local session descriptions has
1560 // changed.
1561 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1562 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1563}
1564
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565// This test sets up a Jsep call between two parties with external
1566// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001567// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1568// See issue webrtc/2378.
deadbeefcbc95072015-10-15 19:31:56 -07001569TEST_F(JsepPeerConnectionP2PTestClient,
1570 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 ASSERT_TRUE(CreateTestClients());
1572 EnableVideoDecoderFactory();
1573 LocalP2PTest();
1574}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001575
deadbeef0a6c4ca2015-10-06 11:38:28 -07001576class IceServerParsingTest : public testing::Test {
1577 public:
1578 // Convenience for parsing a single URL.
1579 bool ParseUrl(const std::string& url) {
1580 return ParseUrl(url, std::string(), std::string());
1581 }
1582
1583 bool ParseUrl(const std::string& url,
1584 const std::string& username,
1585 const std::string& password) {
1586 PeerConnectionInterface::IceServers servers;
1587 PeerConnectionInterface::IceServer server;
1588 server.urls.push_back(url);
1589 server.username = username;
1590 server.password = password;
1591 servers.push_back(server);
1592 return webrtc::ParseIceServers(servers, &stun_configurations_,
1593 &turn_configurations_);
1594 }
1595
1596 protected:
1597 webrtc::StunConfigurations stun_configurations_;
1598 webrtc::TurnConfigurations turn_configurations_;
1599};
1600
1601// Make sure all STUN/TURN prefixes are parsed correctly.
1602TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1603 EXPECT_TRUE(ParseUrl("stun:hostname"));
1604 EXPECT_EQ(1U, stun_configurations_.size());
1605 EXPECT_EQ(0U, turn_configurations_.size());
1606 stun_configurations_.clear();
1607
1608 EXPECT_TRUE(ParseUrl("stuns:hostname"));
1609 EXPECT_EQ(1U, stun_configurations_.size());
1610 EXPECT_EQ(0U, turn_configurations_.size());
1611 stun_configurations_.clear();
1612
1613 EXPECT_TRUE(ParseUrl("turn:hostname"));
1614 EXPECT_EQ(0U, stun_configurations_.size());
1615 EXPECT_EQ(1U, turn_configurations_.size());
1616 EXPECT_FALSE(turn_configurations_[0].secure);
1617 turn_configurations_.clear();
1618
1619 EXPECT_TRUE(ParseUrl("turns:hostname"));
1620 EXPECT_EQ(0U, stun_configurations_.size());
1621 EXPECT_EQ(1U, turn_configurations_.size());
1622 EXPECT_TRUE(turn_configurations_[0].secure);
1623 turn_configurations_.clear();
1624
1625 // invalid prefixes
1626 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1627 EXPECT_FALSE(ParseUrl(":hostname"));
1628 EXPECT_FALSE(ParseUrl(":"));
1629 EXPECT_FALSE(ParseUrl(""));
1630}
1631
1632TEST_F(IceServerParsingTest, VerifyDefaults) {
1633 // TURNS defaults
1634 EXPECT_TRUE(ParseUrl("turns:hostname"));
1635 EXPECT_EQ(1U, turn_configurations_.size());
1636 EXPECT_EQ(5349, turn_configurations_[0].server.port());
1637 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1638 turn_configurations_.clear();
1639
1640 // TURN defaults
1641 EXPECT_TRUE(ParseUrl("turn:hostname"));
1642 EXPECT_EQ(1U, turn_configurations_.size());
1643 EXPECT_EQ(3478, turn_configurations_[0].server.port());
1644 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1645 turn_configurations_.clear();
1646
1647 // STUN defaults
1648 EXPECT_TRUE(ParseUrl("stun:hostname"));
1649 EXPECT_EQ(1U, stun_configurations_.size());
1650 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1651 stun_configurations_.clear();
1652}
1653
1654// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1655// can be parsed correctly.
1656TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1657 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
1658 EXPECT_EQ(1U, stun_configurations_.size());
1659 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1660 EXPECT_EQ(1234, stun_configurations_[0].server.port());
1661 stun_configurations_.clear();
1662
1663 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
1664 EXPECT_EQ(1U, stun_configurations_.size());
1665 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1666 EXPECT_EQ(4321, stun_configurations_[0].server.port());
1667 stun_configurations_.clear();
1668
1669 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
1670 EXPECT_EQ(1U, stun_configurations_.size());
1671 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1672 EXPECT_EQ(9999, stun_configurations_[0].server.port());
1673 stun_configurations_.clear();
1674
1675 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
1676 EXPECT_EQ(1U, stun_configurations_.size());
1677 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1678 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1679 stun_configurations_.clear();
1680
1681 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
1682 EXPECT_EQ(1U, stun_configurations_.size());
1683 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1684 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1685 stun_configurations_.clear();
1686
1687 EXPECT_TRUE(ParseUrl("stun:hostname"));
1688 EXPECT_EQ(1U, stun_configurations_.size());
1689 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1690 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1691 stun_configurations_.clear();
1692
1693 // Try some invalid hostname:port strings.
1694 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1695 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
1696 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1697 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1698 EXPECT_FALSE(ParseUrl("stun::5555"));
1699 EXPECT_FALSE(ParseUrl("stun:"));
1700}
1701
1702// Test parsing the "?transport=xxx" part of the URL.
1703TEST_F(IceServerParsingTest, ParseTransport) {
1704 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
1705 EXPECT_EQ(1U, turn_configurations_.size());
1706 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1707 turn_configurations_.clear();
1708
1709 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
1710 EXPECT_EQ(1U, turn_configurations_.size());
1711 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1712 turn_configurations_.clear();
1713
1714 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1715}
1716
1717// Test parsing ICE username contained in URL.
1718TEST_F(IceServerParsingTest, ParseUsername) {
1719 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
1720 EXPECT_EQ(1U, turn_configurations_.size());
1721 EXPECT_EQ("user", turn_configurations_[0].username);
1722 turn_configurations_.clear();
1723
1724 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1725 EXPECT_FALSE(ParseUrl("turn:username@"));
1726 EXPECT_FALSE(ParseUrl("turn:@"));
1727 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1728}
1729
1730// Test that username and password from IceServer is copied into the resulting
1731// TurnConfiguration.
1732TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1733 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
1734 EXPECT_EQ(1U, turn_configurations_.size());
1735 EXPECT_EQ("username", turn_configurations_[0].username);
1736 EXPECT_EQ("password", turn_configurations_[0].password);
1737}
1738
1739// Ensure that if a server has multiple URLs, each one is parsed.
1740TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1741 PeerConnectionInterface::IceServers servers;
1742 PeerConnectionInterface::IceServer server;
1743 server.urls.push_back("stun:hostname");
1744 server.urls.push_back("turn:hostname");
1745 servers.push_back(server);
1746 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_configurations_,
1747 &turn_configurations_));
1748 EXPECT_EQ(1U, stun_configurations_.size());
1749 EXPECT_EQ(1U, turn_configurations_.size());
1750}
1751
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001752#endif // if !defined(THREAD_SANITIZER)