blob: 38f90e75c646e9c2a40f284ddd56bd8dae42c9a6 [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()) {
253 FakeConstraints constraints;
254 // Disable highpass filter so that we can get all the test audio frames.
255 constraints.AddMandatory(
256 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000257 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org97077a32013-10-25 21:18:33 +0000258 peer_connection_factory_->CreateAudioSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 // TODO(perkj): Test audio source when it is implemented. Currently audio
260 // always use the default input.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000261 std::string label = stream_label + kAudioTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000262 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000263 peer_connection_factory_->CreateAudioTrack(label, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 stream->AddTrack(audio_track);
265 }
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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000674 rtc::scoped_refptr<webrtc::VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 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
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000680 cricket::FakeVideoCapturer* fake_capturer =
681 new webrtc::FakePeriodicVideoCapturer();
682 video_capturers_.push_back(fake_capturer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000683 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 peer_connection_factory_->CreateVideoSource(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000685 fake_capturer, &source_constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849class P2PTestConductor : public testing::Test {
850 public:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000851 P2PTestConductor()
852 : pss_(new rtc::PhysicalSocketServer),
853 ss_(new rtc::VirtualSocketServer(pss_.get())),
854 ss_scope_(ss_.get()) {}
855
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 bool SessionActive() {
857 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000858 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000860
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 // Return true if the number of frames provided have been received or it is
862 // known that that will never occur (e.g. no frames will be sent or
863 // captured).
864 bool FramesNotPending(int audio_frames_to_receive,
865 int video_frames_to_receive) {
866 return VideoFramesReceivedCheck(video_frames_to_receive) &&
867 AudioFramesReceivedCheck(audio_frames_to_receive);
868 }
869 bool AudioFramesReceivedCheck(int frames_received) {
870 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
871 receiving_client_->AudioFramesReceivedCheck(frames_received);
872 }
873 bool VideoFramesReceivedCheck(int frames_received) {
874 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
875 receiving_client_->VideoFramesReceivedCheck(frames_received);
876 }
877 void VerifyDtmf() {
878 initiating_client_->VerifyDtmf();
879 receiving_client_->VerifyDtmf();
880 }
881
882 void TestUpdateOfferWithRejectedContent() {
883 initiating_client_->Negotiate(true, false);
884 EXPECT_TRUE_WAIT(
885 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
886 kMaxWaitForFramesMs);
887 // There shouldn't be any more video frame after the new offer is
888 // negotiated.
889 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
890 }
891
892 void VerifyRenderedSize(int width, int height) {
893 EXPECT_EQ(width, receiving_client()->rendered_width());
894 EXPECT_EQ(height, receiving_client()->rendered_height());
895 EXPECT_EQ(width, initializing_client()->rendered_width());
896 EXPECT_EQ(height, initializing_client()->rendered_height());
897 }
898
899 void VerifySessionDescriptions() {
900 initiating_client_->VerifyRejectedMediaInSessionDescription();
901 receiving_client_->VerifyRejectedMediaInSessionDescription();
902 initiating_client_->VerifyLocalIceUfragAndPassword();
903 receiving_client_->VerifyLocalIceUfragAndPassword();
904 }
905
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 ~P2PTestConductor() {
907 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700908 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 }
910 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700911 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 }
914
deadbeefaf1b59c2015-10-15 12:08:41 -0700915 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916
917 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
918 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700919 return CreateTestClients(init_constraints, nullptr, recv_constraints,
920 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +0200921 }
922
923 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
924 PeerConnectionFactory::Options* init_options,
925 MediaConstraintsInterface* recv_constraints,
926 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700927 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
928 "Caller: ", init_constraints, init_options));
929 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
930 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 if (!initiating_client_ || !receiving_client_) {
932 return false;
933 }
934 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
935 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
936 return true;
937 }
938
939 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
940 const webrtc::FakeConstraints& recv_constraints) {
941 initiating_client_->SetVideoConstraints(init_constraints);
942 receiving_client_->SetVideoConstraints(recv_constraints);
943 }
944
945 void EnableVideoDecoderFactory() {
946 initiating_client_->EnableVideoDecoderFactory();
947 receiving_client_->EnableVideoDecoderFactory();
948 }
949
950 // This test sets up a call between two parties. Both parties send static
951 // frames to each other. Once the test is finished the number of sent frames
952 // is compared to the number of received frames.
953 void LocalP2PTest() {
954 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
955 initiating_client_->AddMediaStream(true, true);
956 }
957 initiating_client_->Negotiate();
958 const int kMaxWaitForActivationMs = 5000;
959 // Assert true is used here since next tests are guaranteed to fail and
960 // would eat up 5 seconds.
961 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
962 VerifySessionDescriptions();
963
964
965 int audio_frame_count = kEndAudioFrameCount;
966 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
967 if (!initiating_client_->can_receive_audio() ||
968 !receiving_client_->can_receive_audio()) {
969 audio_frame_count = -1;
970 }
971 int video_frame_count = kEndVideoFrameCount;
972 if (!initiating_client_->can_receive_video() ||
973 !receiving_client_->can_receive_video()) {
974 video_frame_count = -1;
975 }
976
977 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000978 // Audio or video is expected to flow, so both clients should reach the
979 // Connected state, and the offerer (ICE controller) should proceed to
980 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 // Note: These tests have been observed to fail under heavy load at
982 // shorter timeouts, so they may be flaky.
983 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000984 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 initiating_client_->ice_connection_state(),
986 kMaxWaitForFramesMs);
987 EXPECT_EQ_WAIT(
988 webrtc::PeerConnectionInterface::kIceConnectionConnected,
989 receiving_client_->ice_connection_state(),
990 kMaxWaitForFramesMs);
991 }
992
993 if (initiating_client_->can_receive_audio() ||
994 initiating_client_->can_receive_video()) {
995 // The initiating client can receive media, so it must produce candidates
996 // that will serve as destinations for that media.
997 // TODO(bemasc): Understand why the state is not already Complete here, as
998 // seems to be the case for the receiving client. This may indicate a bug
999 // in the ICE gathering system.
1000 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1001 initiating_client_->ice_gathering_state());
1002 }
1003 if (receiving_client_->can_receive_audio() ||
1004 receiving_client_->can_receive_video()) {
1005 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1006 receiving_client_->ice_gathering_state(),
1007 kMaxWaitForFramesMs);
1008 }
1009
1010 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1011 kMaxWaitForFramesMs);
1012 }
1013
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001014 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1015 // Messages may get lost on the unreliable DataChannel, so we send multiple
1016 // times to avoid test flakiness.
1017 static const size_t kSendAttempts = 5;
1018
1019 for (size_t i = 0; i < kSendAttempts; ++i) {
1020 dc->Send(DataBuffer(data));
1021 }
1022 }
1023
deadbeefaf1b59c2015-10-15 12:08:41 -07001024 PeerConnectionTestClient* initializing_client() {
1025 return initiating_client_.get();
1026 }
1027 PeerConnectionTestClient* receiving_client() {
1028 return receiving_client_.get();
1029 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030
1031 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001032 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1033 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1034 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001035 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1036 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001039// Disable for TSan v2, see
1040// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1041#if !defined(THREAD_SANITIZER)
1042
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001044// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1045// See issue webrtc/2378.
deadbeefaf1b59c2015-10-15 12:08:41 -07001046TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 ASSERT_TRUE(CreateTestClients());
1048 LocalP2PTest();
1049 VerifyDtmf();
1050}
1051
1052// This test sets up a Jsep call between two parties and test that we can get a
1053// video aspect ratio of 16:9.
deadbeefaf1b59c2015-10-15 12:08:41 -07001054TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 ASSERT_TRUE(CreateTestClients());
1056 FakeConstraints constraint;
1057 double requested_ratio = 640.0/360;
1058 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1059 SetVideoConstraints(constraint, constraint);
1060 LocalP2PTest();
1061
1062 ASSERT_LE(0, initializing_client()->rendered_height());
1063 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001064 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 initializing_client()->rendered_height();
1066 EXPECT_LE(requested_ratio, initiating_video_ratio);
1067
1068 ASSERT_LE(0, receiving_client()->rendered_height());
1069 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001070 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 receiving_client()->rendered_height();
1072 EXPECT_LE(requested_ratio, receiving_video_ratio);
1073}
1074
1075// This test sets up a Jsep call between two parties and test that the
1076// received video has a resolution of 1280*720.
1077// TODO(mallinath): Enable when
1078// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeefaf1b59c2015-10-15 12:08:41 -07001079TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 ASSERT_TRUE(CreateTestClients());
1081 FakeConstraints constraint;
1082 constraint.SetMandatoryMinWidth(1280);
1083 constraint.SetMandatoryMinHeight(720);
1084 SetVideoConstraints(constraint, constraint);
1085 LocalP2PTest();
1086 VerifyRenderedSize(1280, 720);
1087}
1088
1089// This test sets up a call between two endpoints that are configured to use
1090// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeefaf1b59c2015-10-15 12:08:41 -07001091TEST_F(P2PTestConductor, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001092 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 FakeConstraints setup_constraints;
1094 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1095 true);
1096 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1097 LocalP2PTest();
1098 VerifyRenderedSize(640, 480);
1099}
1100
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001101// This test sets up a audio call initially and then upgrades to audio/video,
1102// using DTLS.
deadbeefaf1b59c2015-10-15 12:08:41 -07001103TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001104 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001105 FakeConstraints setup_constraints;
1106 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1107 true);
1108 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1109 receiving_client()->SetReceiveAudioVideo(true, false);
1110 LocalP2PTest();
1111 receiving_client()->SetReceiveAudioVideo(true, true);
1112 receiving_client()->Negotiate();
1113}
1114
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115// This test sets up a call between two endpoints that are configured to use
1116// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1117// negotiated and used for transport.
deadbeefaf1b59c2015-10-15 12:08:41 -07001118TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001119 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120 FakeConstraints setup_constraints;
1121 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1122 true);
1123 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1124 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1125 LocalP2PTest();
1126 VerifyRenderedSize(640, 480);
1127}
1128
1129// This test sets up a Jsep call between two parties, and the callee only
1130// accept to receive video.
deadbeefaf1b59c2015-10-15 12:08:41 -07001131TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132 ASSERT_TRUE(CreateTestClients());
1133 receiving_client()->SetReceiveAudioVideo(false, true);
1134 LocalP2PTest();
1135}
1136
1137// This test sets up a Jsep call between two parties, and the callee only
1138// accept to receive audio.
deadbeefaf1b59c2015-10-15 12:08:41 -07001139TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 ASSERT_TRUE(CreateTestClients());
1141 receiving_client()->SetReceiveAudioVideo(true, false);
1142 LocalP2PTest();
1143}
1144
1145// This test sets up a Jsep call between two parties, and the callee reject both
1146// audio and video.
deadbeefaf1b59c2015-10-15 12:08:41 -07001147TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 ASSERT_TRUE(CreateTestClients());
1149 receiving_client()->SetReceiveAudioVideo(false, false);
1150 LocalP2PTest();
1151}
1152
1153// This test sets up an audio and video call between two parties. After the call
1154// runs for a while (10 frames), the caller sends an update offer with video
1155// being rejected. Once the re-negotiation is done, the video flow should stop
1156// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001157// Disabled due to b/14955157.
deadbeefaf1b59c2015-10-15 12:08:41 -07001158TEST_F(P2PTestConductor, DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 ASSERT_TRUE(CreateTestClients());
1160 LocalP2PTest();
1161 TestUpdateOfferWithRejectedContent();
1162}
1163
1164// This test sets up a Jsep call between two parties. The MSID is removed from
1165// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001166// Disabled due to b/14955157.
deadbeefaf1b59c2015-10-15 12:08:41 -07001167TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 ASSERT_TRUE(CreateTestClients());
1169 receiving_client()->RemoveMsidFromReceivedSdp(true);
1170 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1171 // audio and video is muxed when MSID is disabled. Remove
1172 // SetRemoveBundleFromSdp once
1173 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1174 receiving_client()->RemoveBundleFromReceivedSdp(true);
1175 LocalP2PTest();
1176}
1177
1178// This test sets up a Jsep call between two parties and the initiating peer
1179// sends two steams.
1180// TODO(perkj): Disabled due to
1181// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeefaf1b59c2015-10-15 12:08:41 -07001182TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 ASSERT_TRUE(CreateTestClients());
1184 // Set optional video constraint to max 320pixels to decrease CPU usage.
1185 FakeConstraints constraint;
1186 constraint.SetOptionalMaxWidth(320);
1187 SetVideoConstraints(constraint, constraint);
1188 initializing_client()->AddMediaStream(true, true);
1189 initializing_client()->AddMediaStream(false, true);
1190 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1191 LocalP2PTest();
1192 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1193}
1194
1195// Test that we can receive the audio output level from a remote audio track.
deadbeefaf1b59c2015-10-15 12:08:41 -07001196TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 ASSERT_TRUE(CreateTestClients());
1198 LocalP2PTest();
1199
1200 StreamCollectionInterface* remote_streams =
1201 initializing_client()->remote_streams();
1202 ASSERT_GT(remote_streams->count(), 0u);
1203 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1204 MediaStreamTrackInterface* remote_audio_track =
1205 remote_streams->at(0)->GetAudioTracks()[0];
1206
1207 // Get the audio output level stats. Note that the level is not available
1208 // until a RTCP packet has been received.
1209 EXPECT_TRUE_WAIT(
1210 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1211 kMaxWaitForStatsMs);
1212}
1213
1214// Test that an audio input level is reported.
deadbeefaf1b59c2015-10-15 12:08:41 -07001215TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 ASSERT_TRUE(CreateTestClients());
1217 LocalP2PTest();
1218
1219 // Get the audio input level stats. The level should be available very
1220 // soon after the test starts.
1221 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1222 kMaxWaitForStatsMs);
1223}
1224
1225// Test that we can get incoming byte counts from both audio and video tracks.
deadbeefaf1b59c2015-10-15 12:08:41 -07001226TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 ASSERT_TRUE(CreateTestClients());
1228 LocalP2PTest();
1229
1230 StreamCollectionInterface* remote_streams =
1231 initializing_client()->remote_streams();
1232 ASSERT_GT(remote_streams->count(), 0u);
1233 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1234 MediaStreamTrackInterface* remote_audio_track =
1235 remote_streams->at(0)->GetAudioTracks()[0];
1236 EXPECT_TRUE_WAIT(
1237 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1238 kMaxWaitForStatsMs);
1239
1240 MediaStreamTrackInterface* remote_video_track =
1241 remote_streams->at(0)->GetVideoTracks()[0];
1242 EXPECT_TRUE_WAIT(
1243 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1244 kMaxWaitForStatsMs);
1245}
1246
1247// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeefaf1b59c2015-10-15 12:08:41 -07001248TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 ASSERT_TRUE(CreateTestClients());
1250 LocalP2PTest();
1251
1252 StreamCollectionInterface* local_streams =
1253 initializing_client()->local_streams();
1254 ASSERT_GT(local_streams->count(), 0u);
1255 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1256 MediaStreamTrackInterface* local_audio_track =
1257 local_streams->at(0)->GetAudioTracks()[0];
1258 EXPECT_TRUE_WAIT(
1259 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1260 kMaxWaitForStatsMs);
1261
1262 MediaStreamTrackInterface* local_video_track =
1263 local_streams->at(0)->GetVideoTracks()[0];
1264 EXPECT_TRUE_WAIT(
1265 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1266 kMaxWaitForStatsMs);
1267}
1268
Joachim Bauch04e5b492015-05-29 09:40:39 +02001269// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
deadbeefaf1b59c2015-10-15 12:08:41 -07001270TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001271 PeerConnectionFactory::Options init_options;
1272 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1273 PeerConnectionFactory::Options recv_options;
1274 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001275 ASSERT_TRUE(
1276 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001277 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1278 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1279 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001280 LocalP2PTest();
1281
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001282 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1283 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1284 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1285 initializing_client()->GetDtlsCipherStats(),
1286 kMaxWaitForStatsMs);
1287 EXPECT_EQ(1, init_observer->GetEnumCounter(
1288 webrtc::kEnumCounterAudioSslCipher,
1289 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1290 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001291
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001292 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1293 initializing_client()->GetSrtpCipherStats(),
1294 kMaxWaitForStatsMs);
1295 EXPECT_EQ(1, init_observer->GetEnumCounter(
1296 webrtc::kEnumCounterAudioSrtpCipher,
1297 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001298}
1299
1300// Test that DTLS 1.2 is used if both ends support it.
deadbeefaf1b59c2015-10-15 12:08:41 -07001301TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001302 PeerConnectionFactory::Options init_options;
1303 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1304 PeerConnectionFactory::Options recv_options;
1305 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001306 ASSERT_TRUE(
1307 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001308 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1309 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1310 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001311 LocalP2PTest();
1312
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001313 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1314 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1315 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1316 initializing_client()->GetDtlsCipherStats(),
1317 kMaxWaitForStatsMs);
1318 EXPECT_EQ(1, init_observer->GetEnumCounter(
1319 webrtc::kEnumCounterAudioSslCipher,
1320 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1321 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001322
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001323 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1324 initializing_client()->GetSrtpCipherStats(),
1325 kMaxWaitForStatsMs);
1326 EXPECT_EQ(1, init_observer->GetEnumCounter(
1327 webrtc::kEnumCounterAudioSrtpCipher,
1328 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001329}
1330
1331// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1332// received supports 1.0.
deadbeefaf1b59c2015-10-15 12:08:41 -07001333TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001334 PeerConnectionFactory::Options init_options;
1335 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1336 PeerConnectionFactory::Options recv_options;
1337 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001338 ASSERT_TRUE(
1339 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001340 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1341 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1342 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001343 LocalP2PTest();
1344
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001345 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1346 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1347 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1348 initializing_client()->GetDtlsCipherStats(),
1349 kMaxWaitForStatsMs);
1350 EXPECT_EQ(1, init_observer->GetEnumCounter(
1351 webrtc::kEnumCounterAudioSslCipher,
1352 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1353 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001354
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001355 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1356 initializing_client()->GetSrtpCipherStats(),
1357 kMaxWaitForStatsMs);
1358 EXPECT_EQ(1, init_observer->GetEnumCounter(
1359 webrtc::kEnumCounterAudioSrtpCipher,
1360 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001361}
1362
1363// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1364// received supports 1.2.
deadbeefaf1b59c2015-10-15 12:08:41 -07001365TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001366 PeerConnectionFactory::Options init_options;
1367 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1368 PeerConnectionFactory::Options recv_options;
1369 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001370 ASSERT_TRUE(
1371 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001372 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1373 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1374 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001375 LocalP2PTest();
1376
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001377 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1378 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1379 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1380 initializing_client()->GetDtlsCipherStats(),
1381 kMaxWaitForStatsMs);
1382 EXPECT_EQ(1, init_observer->GetEnumCounter(
1383 webrtc::kEnumCounterAudioSslCipher,
1384 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1385 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001386
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001387 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1388 initializing_client()->GetSrtpCipherStats(),
1389 kMaxWaitForStatsMs);
1390 EXPECT_EQ(1, init_observer->GetEnumCounter(
1391 webrtc::kEnumCounterAudioSrtpCipher,
1392 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001393}
1394
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395// This test sets up a call between two parties with audio, video and data.
deadbeefaf1b59c2015-10-15 12:08:41 -07001396TEST_F(P2PTestConductor, LocalP2PTestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 FakeConstraints setup_constraints;
1398 setup_constraints.SetAllowRtpDataChannels();
1399 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1400 initializing_client()->CreateDataChannel();
1401 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001402 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1403 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1405 kMaxWaitMs);
1406 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1407 kMaxWaitMs);
1408
1409 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001410
1411 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001412 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1413 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001414
1415 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1417 kMaxWaitMs);
1418
1419 receiving_client()->data_channel()->Close();
1420 // Send new offer and answer.
1421 receiving_client()->Negotiate();
1422 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1423 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1424}
1425
1426// This test sets up a call between two parties and creates a data channel.
1427// The test tests that received data is buffered unless an observer has been
1428// registered.
1429// Rtp data channels can receive data before the underlying
1430// transport has detected that a channel is writable and thus data can be
1431// received before the data channel state changes to open. That is hard to test
1432// but the same buffering is used in that case.
deadbeefaf1b59c2015-10-15 12:08:41 -07001433TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434 FakeConstraints setup_constraints;
1435 setup_constraints.SetAllowRtpDataChannels();
1436 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1437 initializing_client()->CreateDataChannel();
1438 initializing_client()->Negotiate();
1439
deadbeefaf1b59c2015-10-15 12:08:41 -07001440 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1441 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001442 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1443 kMaxWaitMs);
1444 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1445 receiving_client()->data_channel()->state(), kMaxWaitMs);
1446
1447 // Unregister the existing observer.
1448 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001449
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001451 SendRtpData(initializing_client()->data_channel(), data);
1452
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453 // Wait a while to allow the sent data to arrive before an observer is
1454 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001455 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456
1457 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1458 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1459}
1460
1461// This test sets up a call between two parties with audio, video and but only
1462// the initiating client support data.
deadbeefaf1b59c2015-10-15 12:08:41 -07001463TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001464 FakeConstraints setup_constraints_1;
1465 setup_constraints_1.SetAllowRtpDataChannels();
1466 // Must disable DTLS to make negotiation succeed.
1467 setup_constraints_1.SetMandatory(
1468 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1469 FakeConstraints setup_constraints_2;
1470 setup_constraints_2.SetMandatory(
1471 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1472 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 initializing_client()->CreateDataChannel();
1474 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001475 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 EXPECT_FALSE(receiving_client()->data_channel());
1477 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1478}
1479
1480// This test sets up a call between two parties with audio, video. When audio
1481// and video is setup and flowing and data channel is negotiated.
deadbeefaf1b59c2015-10-15 12:08:41 -07001482TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 FakeConstraints setup_constraints;
1484 setup_constraints.SetAllowRtpDataChannels();
1485 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1486 LocalP2PTest();
1487 initializing_client()->CreateDataChannel();
1488 // Send new offer and answer.
1489 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001490 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1491 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1493 kMaxWaitMs);
1494 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1495 kMaxWaitMs);
1496}
1497
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001498// This test sets up a Jsep call with SCTP DataChannel and verifies the
1499// negotiation is completed without error.
1500#ifdef HAVE_SCTP
deadbeefaf1b59c2015-10-15 12:08:41 -07001501TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001502 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001503 FakeConstraints constraints;
1504 constraints.SetMandatory(
1505 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1506 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1507 initializing_client()->CreateDataChannel();
1508 initializing_client()->Negotiate(false, false);
1509}
1510#endif
1511
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512// This test sets up a call between two parties with audio, and video.
1513// During the call, the initializing side restart ice and the test verifies that
1514// new ice candidates are generated and audio and video still can flow.
deadbeefaf1b59c2015-10-15 12:08:41 -07001515TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516 ASSERT_TRUE(CreateTestClients());
1517
1518 // Negotiate and wait for ice completion and make sure audio and video plays.
1519 LocalP2PTest();
1520
1521 // Create a SDP string of the first audio candidate for both clients.
1522 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1523 initializing_client()->pc()->local_description()->candidates(0);
1524 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1525 receiving_client()->pc()->local_description()->candidates(0);
1526 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1527 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1528 std::string initiator_candidate;
1529 EXPECT_TRUE(
1530 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1531 std::string receiver_candidate;
1532 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1533
1534 // Restart ice on the initializing client.
1535 receiving_client()->SetExpectIceRestart(true);
1536 initializing_client()->IceRestart();
1537
1538 // Negotiate and wait for ice completion again and make sure audio and video
1539 // plays.
1540 LocalP2PTest();
1541
1542 // Create a SDP string of the first audio candidate for both clients again.
1543 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1544 initializing_client()->pc()->local_description()->candidates(0);
1545 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1546 receiving_client()->pc()->local_description()->candidates(0);
1547 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1548 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1549 std::string initiator_candidate_restart;
1550 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1551 &initiator_candidate_restart));
1552 std::string receiver_candidate_restart;
1553 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1554 &receiver_candidate_restart));
1555
1556 // Verify that the first candidates in the local session descriptions has
1557 // changed.
1558 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1559 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1560}
1561
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562// This test sets up a Jsep call between two parties with external
1563// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001564// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1565// See issue webrtc/2378.
deadbeefaf1b59c2015-10-15 12:08:41 -07001566TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 ASSERT_TRUE(CreateTestClients());
1568 EnableVideoDecoderFactory();
1569 LocalP2PTest();
1570}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001571
deadbeef0a6c4ca2015-10-06 11:38:28 -07001572class IceServerParsingTest : public testing::Test {
1573 public:
1574 // Convenience for parsing a single URL.
1575 bool ParseUrl(const std::string& url) {
1576 return ParseUrl(url, std::string(), std::string());
1577 }
1578
1579 bool ParseUrl(const std::string& url,
1580 const std::string& username,
1581 const std::string& password) {
1582 PeerConnectionInterface::IceServers servers;
1583 PeerConnectionInterface::IceServer server;
1584 server.urls.push_back(url);
1585 server.username = username;
1586 server.password = password;
1587 servers.push_back(server);
1588 return webrtc::ParseIceServers(servers, &stun_configurations_,
1589 &turn_configurations_);
1590 }
1591
1592 protected:
1593 webrtc::StunConfigurations stun_configurations_;
1594 webrtc::TurnConfigurations turn_configurations_;
1595};
1596
1597// Make sure all STUN/TURN prefixes are parsed correctly.
1598TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1599 EXPECT_TRUE(ParseUrl("stun:hostname"));
1600 EXPECT_EQ(1U, stun_configurations_.size());
1601 EXPECT_EQ(0U, turn_configurations_.size());
1602 stun_configurations_.clear();
1603
1604 EXPECT_TRUE(ParseUrl("stuns:hostname"));
1605 EXPECT_EQ(1U, stun_configurations_.size());
1606 EXPECT_EQ(0U, turn_configurations_.size());
1607 stun_configurations_.clear();
1608
1609 EXPECT_TRUE(ParseUrl("turn:hostname"));
1610 EXPECT_EQ(0U, stun_configurations_.size());
1611 EXPECT_EQ(1U, turn_configurations_.size());
1612 EXPECT_FALSE(turn_configurations_[0].secure);
1613 turn_configurations_.clear();
1614
1615 EXPECT_TRUE(ParseUrl("turns:hostname"));
1616 EXPECT_EQ(0U, stun_configurations_.size());
1617 EXPECT_EQ(1U, turn_configurations_.size());
1618 EXPECT_TRUE(turn_configurations_[0].secure);
1619 turn_configurations_.clear();
1620
1621 // invalid prefixes
1622 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1623 EXPECT_FALSE(ParseUrl(":hostname"));
1624 EXPECT_FALSE(ParseUrl(":"));
1625 EXPECT_FALSE(ParseUrl(""));
1626}
1627
1628TEST_F(IceServerParsingTest, VerifyDefaults) {
1629 // TURNS defaults
1630 EXPECT_TRUE(ParseUrl("turns:hostname"));
1631 EXPECT_EQ(1U, turn_configurations_.size());
1632 EXPECT_EQ(5349, turn_configurations_[0].server.port());
1633 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1634 turn_configurations_.clear();
1635
1636 // TURN defaults
1637 EXPECT_TRUE(ParseUrl("turn:hostname"));
1638 EXPECT_EQ(1U, turn_configurations_.size());
1639 EXPECT_EQ(3478, turn_configurations_[0].server.port());
1640 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1641 turn_configurations_.clear();
1642
1643 // STUN defaults
1644 EXPECT_TRUE(ParseUrl("stun:hostname"));
1645 EXPECT_EQ(1U, stun_configurations_.size());
1646 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1647 stun_configurations_.clear();
1648}
1649
1650// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1651// can be parsed correctly.
1652TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1653 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
1654 EXPECT_EQ(1U, stun_configurations_.size());
1655 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1656 EXPECT_EQ(1234, stun_configurations_[0].server.port());
1657 stun_configurations_.clear();
1658
1659 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
1660 EXPECT_EQ(1U, stun_configurations_.size());
1661 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1662 EXPECT_EQ(4321, stun_configurations_[0].server.port());
1663 stun_configurations_.clear();
1664
1665 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
1666 EXPECT_EQ(1U, stun_configurations_.size());
1667 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1668 EXPECT_EQ(9999, stun_configurations_[0].server.port());
1669 stun_configurations_.clear();
1670
1671 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
1672 EXPECT_EQ(1U, stun_configurations_.size());
1673 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1674 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1675 stun_configurations_.clear();
1676
1677 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
1678 EXPECT_EQ(1U, stun_configurations_.size());
1679 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1680 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1681 stun_configurations_.clear();
1682
1683 EXPECT_TRUE(ParseUrl("stun:hostname"));
1684 EXPECT_EQ(1U, stun_configurations_.size());
1685 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1686 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1687 stun_configurations_.clear();
1688
1689 // Try some invalid hostname:port strings.
1690 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1691 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
1692 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1693 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1694 EXPECT_FALSE(ParseUrl("stun::5555"));
1695 EXPECT_FALSE(ParseUrl("stun:"));
1696}
1697
1698// Test parsing the "?transport=xxx" part of the URL.
1699TEST_F(IceServerParsingTest, ParseTransport) {
1700 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
1701 EXPECT_EQ(1U, turn_configurations_.size());
1702 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1703 turn_configurations_.clear();
1704
1705 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
1706 EXPECT_EQ(1U, turn_configurations_.size());
1707 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1708 turn_configurations_.clear();
1709
1710 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1711}
1712
1713// Test parsing ICE username contained in URL.
1714TEST_F(IceServerParsingTest, ParseUsername) {
1715 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
1716 EXPECT_EQ(1U, turn_configurations_.size());
1717 EXPECT_EQ("user", turn_configurations_[0].username);
1718 turn_configurations_.clear();
1719
1720 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1721 EXPECT_FALSE(ParseUrl("turn:username@"));
1722 EXPECT_FALSE(ParseUrl("turn:@"));
1723 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1724}
1725
1726// Test that username and password from IceServer is copied into the resulting
1727// TurnConfiguration.
1728TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1729 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
1730 EXPECT_EQ(1U, turn_configurations_.size());
1731 EXPECT_EQ("username", turn_configurations_[0].username);
1732 EXPECT_EQ("password", turn_configurations_[0].password);
1733}
1734
1735// Ensure that if a server has multiple URLs, each one is parsed.
1736TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1737 PeerConnectionInterface::IceServers servers;
1738 PeerConnectionInterface::IceServer server;
1739 server.urls.push_back("stun:hostname");
1740 server.urls.push_back("turn:hostname");
1741 servers.push_back(server);
1742 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_configurations_,
1743 &turn_configurations_));
1744 EXPECT_EQ(1U, stun_configurations_.size());
1745 EXPECT_EQ(1U, turn_configurations_.size());
1746}
1747
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001748#endif // if !defined(THREAD_SANITIZER)