blob: c8b0c8cf6a14079e0276e46ce37b577b9cd48998 [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:
130 protected:
131 SignalingMessageReceiver() {}
132 virtual ~SignalingMessageReceiver() {}
133};
134
135class JsepMessageReceiver : public SignalingMessageReceiver {
136 public:
137 virtual void ReceiveSdpMessage(const std::string& type,
138 std::string& msg) = 0;
139 virtual void ReceiveIceMessage(const std::string& sdp_mid,
140 int sdp_mline_index,
141 const std::string& msg) = 0;
142
143 protected:
144 JsepMessageReceiver() {}
145 virtual ~JsepMessageReceiver() {}
146};
147
148template <typename MessageReceiver>
149class PeerConnectionTestClientBase
150 : public webrtc::PeerConnectionObserver,
151 public MessageReceiver {
152 public:
153 ~PeerConnectionTestClientBase() {
154 while (!fake_video_renderers_.empty()) {
155 RenderMap::iterator it = fake_video_renderers_.begin();
156 delete it->second;
157 fake_video_renderers_.erase(it);
158 }
159 }
160
161 virtual void Negotiate() = 0;
162
163 virtual void Negotiate(bool audio, bool video) = 0;
164
165 virtual void SetVideoConstraints(
166 const webrtc::FakeConstraints& video_constraint) {
167 video_constraints_ = video_constraint;
168 }
169
170 void AddMediaStream(bool audio, bool video) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000171 std::string stream_label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000172 rtc::ToString<int>(
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000173 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000175 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
177 if (audio && can_receive_audio()) {
178 FakeConstraints constraints;
179 // Disable highpass filter so that we can get all the test audio frames.
180 constraints.AddMandatory(
181 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000182 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org97077a32013-10-25 21:18:33 +0000183 peer_connection_factory_->CreateAudioSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 // TODO(perkj): Test audio source when it is implemented. Currently audio
185 // always use the default input.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000186 std::string label = stream_label + kAudioTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000187 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000188 peer_connection_factory_->CreateAudioTrack(label, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 stream->AddTrack(audio_track);
190 }
191 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000192 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 }
194
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000195 EXPECT_TRUE(peer_connection_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 }
197
198 size_t NumberOfLocalMediaStreams() {
199 return peer_connection_->local_streams()->count();
200 }
201
202 bool SessionActive() {
203 return peer_connection_->signaling_state() ==
204 webrtc::PeerConnectionInterface::kStable;
205 }
206
207 void set_signaling_message_receiver(
208 MessageReceiver* signaling_message_receiver) {
209 signaling_message_receiver_ = signaling_message_receiver;
210 }
211
212 void EnableVideoDecoderFactory() {
213 video_decoder_factory_enabled_ = true;
214 fake_video_decoder_factory_->AddSupportedVideoCodecType(
215 webrtc::kVideoCodecVP8);
216 }
217
218 bool AudioFramesReceivedCheck(int number_of_frames) const {
219 return number_of_frames <= fake_audio_capture_module_->frames_received();
220 }
221
222 bool VideoFramesReceivedCheck(int number_of_frames) {
223 if (video_decoder_factory_enabled_) {
224 const std::vector<FakeWebRtcVideoDecoder*>& decoders
225 = fake_video_decoder_factory_->decoders();
226 if (decoders.empty()) {
227 return number_of_frames <= 0;
228 }
229
230 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
231 it = decoders.begin(); it != decoders.end(); ++it) {
232 if (number_of_frames > (*it)->GetNumFramesReceived()) {
233 return false;
234 }
235 }
236 return true;
237 } else {
238 if (fake_video_renderers_.empty()) {
239 return number_of_frames <= 0;
240 }
241
242 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
243 it != fake_video_renderers_.end(); ++it) {
244 if (number_of_frames > it->second->num_rendered_frames()) {
245 return false;
246 }
247 }
248 return true;
249 }
250 }
251 // Verify the CreateDtmfSender interface
252 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000253 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
254 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255
256 // We can't create a DTMF sender with an invalid audio track or a non local
257 // track.
258 EXPECT_TRUE(peer_connection_->CreateDtmfSender(NULL) == NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000259 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 peer_connection_factory_->CreateAudioTrack("dummy_track",
261 NULL));
262 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == NULL);
263
264 // We should be able to create a DTMF sender from a local track.
265 webrtc::AudioTrackInterface* localtrack =
266 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
267 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
268 EXPECT_TRUE(dtmf_sender.get() != NULL);
269 dtmf_sender->RegisterObserver(observer.get());
270
271 // Test the DtmfSender object just created.
272 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
273 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
274
275 // We don't need to verify that the DTMF tones are actually sent out because
276 // that is already covered by the tests of the lower level components.
277
278 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
279 std::vector<std::string> tones;
280 tones.push_back("1");
281 tones.push_back("a");
282 tones.push_back("");
283 observer->Verify(tones);
284
285 dtmf_sender->UnregisterObserver();
286 }
287
288 // Verifies that the SessionDescription have rejected the appropriate media
289 // content.
290 void VerifyRejectedMediaInSessionDescription() {
291 ASSERT_TRUE(peer_connection_->remote_description() != NULL);
292 ASSERT_TRUE(peer_connection_->local_description() != NULL);
293 const cricket::SessionDescription* remote_desc =
294 peer_connection_->remote_description()->description();
295 const cricket::SessionDescription* local_desc =
296 peer_connection_->local_description()->description();
297
298 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
299 if (remote_audio_content) {
300 const ContentInfo* audio_content =
301 GetFirstAudioContent(local_desc);
302 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
303 }
304
305 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
306 if (remote_video_content) {
307 const ContentInfo* video_content =
308 GetFirstVideoContent(local_desc);
309 EXPECT_EQ(can_receive_video(), !video_content->rejected);
310 }
311 }
312
313 void SetExpectIceRestart(bool expect_restart) {
314 expect_ice_restart_ = expect_restart;
315 }
316
317 bool ExpectIceRestart() const { return expect_ice_restart_; }
318
319 void VerifyLocalIceUfragAndPassword() {
320 ASSERT_TRUE(peer_connection_->local_description() != NULL);
321 const cricket::SessionDescription* desc =
322 peer_connection_->local_description()->description();
323 const cricket::ContentInfos& contents = desc->contents();
324
325 for (size_t index = 0; index < contents.size(); ++index) {
326 if (contents[index].rejected)
327 continue;
328 const cricket::TransportDescription* transport_desc =
329 desc->GetTransportDescriptionByName(contents[index].name);
330
331 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000332 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 if (ufragpair_it == ice_ufrag_pwd_.end()) {
334 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000335 ice_ufrag_pwd_[static_cast<int>(index)] =
336 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 } else if (ExpectIceRestart()) {
338 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
339 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
340 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
341 } else {
342 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
343 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
344 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
345 }
346 }
347 }
348
349 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000350 rtc::scoped_refptr<MockStatsObserver>
351 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000352 EXPECT_TRUE(peer_connection_->GetStats(
353 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700355 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 return observer->AudioOutputLevel();
357 }
358
359 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000360 rtc::scoped_refptr<MockStatsObserver>
361 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000362 EXPECT_TRUE(peer_connection_->GetStats(
363 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700365 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366 return observer->AudioInputLevel();
367 }
368
369 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000370 rtc::scoped_refptr<MockStatsObserver>
371 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000372 EXPECT_TRUE(peer_connection_->GetStats(
373 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700375 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 return observer->BytesReceived();
377 }
378
379 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000380 rtc::scoped_refptr<MockStatsObserver>
381 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000382 EXPECT_TRUE(peer_connection_->GetStats(
383 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700385 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 return observer->BytesSent();
387 }
388
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000389 int GetAvailableReceivedBandwidthStats() {
390 rtc::scoped_refptr<MockStatsObserver>
391 observer(new rtc::RefCountedObject<MockStatsObserver>());
392 EXPECT_TRUE(peer_connection_->GetStats(
393 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
394 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700395 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000396 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000397 return bw;
398 }
399
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000400 std::string GetDtlsCipherStats() {
401 rtc::scoped_refptr<MockStatsObserver>
402 observer(new rtc::RefCountedObject<MockStatsObserver>());
403 EXPECT_TRUE(peer_connection_->GetStats(
404 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
405 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700406 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000407 return observer->DtlsCipher();
408 }
409
410 std::string GetSrtpCipherStats() {
411 rtc::scoped_refptr<MockStatsObserver>
412 observer(new rtc::RefCountedObject<MockStatsObserver>());
413 EXPECT_TRUE(peer_connection_->GetStats(
414 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
415 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700416 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000417 return observer->SrtpCipher();
418 }
419
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 int rendered_width() {
421 EXPECT_FALSE(fake_video_renderers_.empty());
422 return fake_video_renderers_.empty() ? 1 :
423 fake_video_renderers_.begin()->second->width();
424 }
425
426 int rendered_height() {
427 EXPECT_FALSE(fake_video_renderers_.empty());
428 return fake_video_renderers_.empty() ? 1 :
429 fake_video_renderers_.begin()->second->height();
430 }
431
432 size_t number_of_remote_streams() {
433 if (!pc())
434 return 0;
435 return pc()->remote_streams()->count();
436 }
437
438 StreamCollectionInterface* remote_streams() {
439 if (!pc()) {
440 ADD_FAILURE();
441 return NULL;
442 }
443 return pc()->remote_streams();
444 }
445
446 StreamCollectionInterface* local_streams() {
447 if (!pc()) {
448 ADD_FAILURE();
449 return NULL;
450 }
451 return pc()->local_streams();
452 }
453
454 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
455 return pc()->signaling_state();
456 }
457
458 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
459 return pc()->ice_connection_state();
460 }
461
462 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
463 return pc()->ice_gathering_state();
464 }
465
466 // PeerConnectionObserver callbacks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 virtual void OnMessage(const std::string&) {}
468 virtual void OnSignalingMessage(const std::string& /*msg*/) {}
469 virtual void OnSignalingChange(
470 webrtc::PeerConnectionInterface::SignalingState new_state) {
471 EXPECT_EQ(peer_connection_->signaling_state(), new_state);
472 }
473 virtual void OnAddStream(webrtc::MediaStreamInterface* media_stream) {
474 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
475 const std::string id = media_stream->GetVideoTracks()[i]->id();
476 ASSERT_TRUE(fake_video_renderers_.find(id) ==
477 fake_video_renderers_.end());
478 fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
479 media_stream->GetVideoTracks()[i]);
480 }
481 }
482 virtual void OnRemoveStream(webrtc::MediaStreamInterface* media_stream) {}
483 virtual void OnRenegotiationNeeded() {}
484 virtual void OnIceConnectionChange(
485 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
486 EXPECT_EQ(peer_connection_->ice_connection_state(), new_state);
487 }
488 virtual void OnIceGatheringChange(
489 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
490 EXPECT_EQ(peer_connection_->ice_gathering_state(), new_state);
491 }
492 virtual void OnIceCandidate(
493 const webrtc::IceCandidateInterface* /*candidate*/) {}
494
495 webrtc::PeerConnectionInterface* pc() {
496 return peer_connection_.get();
497 }
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000498 void StopVideoCapturers() {
499 for (std::vector<cricket::VideoCapturer*>::iterator it =
500 video_capturers_.begin(); it != video_capturers_.end(); ++it) {
501 (*it)->Stop();
502 }
503 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504
505 protected:
506 explicit PeerConnectionTestClientBase(const std::string& id)
507 : id_(id),
508 expect_ice_restart_(false),
509 fake_video_decoder_factory_(NULL),
510 fake_video_encoder_factory_(NULL),
511 video_decoder_factory_enabled_(false),
512 signaling_message_receiver_(NULL) {
513 }
Joachim Bauch04e5b492015-05-29 09:40:39 +0200514 bool Init(const MediaConstraintsInterface* constraints,
515 const PeerConnectionFactory::Options* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 EXPECT_TRUE(!peer_connection_);
517 EXPECT_TRUE(!peer_connection_factory_);
518 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
519 if (!allocator_factory_) {
520 return false;
521 }
deadbeefee8c6d32015-08-13 14:27:18 -0700522 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523
524 if (fake_audio_capture_module_ == NULL) {
525 return false;
526 }
527 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
528 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
529 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000530 rtc::Thread::Current(), rtc::Thread::Current(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 fake_audio_capture_module_, fake_video_encoder_factory_,
532 fake_video_decoder_factory_);
533 if (!peer_connection_factory_) {
534 return false;
535 }
Joachim Bauch04e5b492015-05-29 09:40:39 +0200536 if (options) {
537 peer_connection_factory_->SetOptions(*options);
538 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
540 constraints);
541 return peer_connection_.get() != NULL;
542 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000543 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
545 const MediaConstraintsInterface* constraints) = 0;
546 MessageReceiver* signaling_message_receiver() {
547 return signaling_message_receiver_;
548 }
549 webrtc::PeerConnectionFactoryInterface* peer_connection_factory() {
550 return peer_connection_factory_.get();
551 }
552
553 virtual bool can_receive_audio() = 0;
554 virtual bool can_receive_video() = 0;
555 const std::string& id() const { return id_; }
556
557 private:
558 class DummyDtmfObserver : public DtmfSenderObserverInterface {
559 public:
560 DummyDtmfObserver() : completed_(false) {}
561
562 // Implements DtmfSenderObserverInterface.
563 void OnToneChange(const std::string& tone) {
564 tones_.push_back(tone);
565 if (tone.empty()) {
566 completed_ = true;
567 }
568 }
569
570 void Verify(const std::vector<std::string>& tones) const {
571 ASSERT_TRUE(tones_.size() == tones.size());
572 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
573 }
574
575 bool completed() const { return completed_; }
576
577 private:
578 bool completed_;
579 std::vector<std::string> tones_;
580 };
581
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000582 rtc::scoped_refptr<webrtc::VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 CreateLocalVideoTrack(const std::string stream_label) {
584 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
585 FakeConstraints source_constraints = video_constraints_;
586 source_constraints.SetMandatoryMaxFrameRate(10);
587
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000588 cricket::FakeVideoCapturer* fake_capturer =
589 new webrtc::FakePeriodicVideoCapturer();
590 video_capturers_.push_back(fake_capturer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000591 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 peer_connection_factory_->CreateVideoSource(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000593 fake_capturer, &source_constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 std::string label = stream_label + kVideoTrackLabelBase;
595 return peer_connection_factory_->CreateVideoTrack(label, source);
596 }
597
598 std::string id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000600 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 allocator_factory_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000602 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
603 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 peer_connection_factory_;
605
606 typedef std::pair<std::string, std::string> IceUfragPwdPair;
607 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
608 bool expect_ice_restart_;
609
610 // Needed to keep track of number of frames send.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000611 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 // Needed to keep track of number of frames received.
613 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
614 RenderMap fake_video_renderers_;
615 // Needed to keep track of number of frames received when external decoder
616 // used.
617 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_;
618 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_;
619 bool video_decoder_factory_enabled_;
620 webrtc::FakeConstraints video_constraints_;
621
622 // For remote peer communication.
623 MessageReceiver* signaling_message_receiver_;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000624
625 // Store references to the video capturers we've created, so that we can stop
626 // them, if required.
627 std::vector<cricket::VideoCapturer*> video_capturers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628};
629
630class JsepTestClient
631 : public PeerConnectionTestClientBase<JsepMessageReceiver> {
632 public:
633 static JsepTestClient* CreateClient(
634 const std::string& id,
Joachim Bauch04e5b492015-05-29 09:40:39 +0200635 const MediaConstraintsInterface* constraints,
636 const PeerConnectionFactory::Options* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 JsepTestClient* client(new JsepTestClient(id));
Joachim Bauch04e5b492015-05-29 09:40:39 +0200638 if (!client->Init(constraints, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 delete client;
640 return NULL;
641 }
642 return client;
643 }
644 ~JsepTestClient() {}
645
646 virtual void Negotiate() {
647 Negotiate(true, true);
648 }
649 virtual void Negotiate(bool audio, bool video) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000650 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pbos@webrtc.orgceb956b2014-09-04 15:27:49 +0000651 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652
653 if (offer->description()->GetContentByName("audio")) {
654 offer->description()->GetContentByName("audio")->rejected = !audio;
655 }
656 if (offer->description()->GetContentByName("video")) {
657 offer->description()->GetContentByName("video")->rejected = !video;
658 }
659
660 std::string sdp;
661 EXPECT_TRUE(offer->ToString(&sdp));
662 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
663 signaling_message_receiver()->ReceiveSdpMessage(
664 webrtc::SessionDescriptionInterface::kOffer, sdp);
665 }
666 // JsepMessageReceiver callback.
667 virtual void ReceiveSdpMessage(const std::string& type,
668 std::string& msg) {
669 FilterIncomingSdpMessage(&msg);
670 if (type == webrtc::SessionDescriptionInterface::kOffer) {
671 HandleIncomingOffer(msg);
672 } else {
673 HandleIncomingAnswer(msg);
674 }
675 }
676 // JsepMessageReceiver callback.
677 virtual void ReceiveIceMessage(const std::string& sdp_mid,
678 int sdp_mline_index,
679 const std::string& msg) {
680 LOG(INFO) << id() << "ReceiveIceMessage";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000681 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, NULL));
683 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
684 }
685 // Implements PeerConnectionObserver functions needed by Jsep.
686 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
687 LOG(INFO) << id() << "OnIceCandidate";
688
689 std::string ice_sdp;
690 EXPECT_TRUE(candidate->ToString(&ice_sdp));
691 if (signaling_message_receiver() == NULL) {
692 // Remote party may be deleted.
693 return;
694 }
695 signaling_message_receiver()->ReceiveIceMessage(candidate->sdp_mid(),
696 candidate->sdp_mline_index(), ice_sdp);
697 }
698
699 void IceRestart() {
700 session_description_constraints_.SetMandatoryIceRestart(true);
701 SetExpectIceRestart(true);
702 }
703
704 void SetReceiveAudioVideo(bool audio, bool video) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000705 SetReceiveAudio(audio);
706 SetReceiveVideo(video);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 ASSERT_EQ(audio, can_receive_audio());
708 ASSERT_EQ(video, can_receive_video());
709 }
710
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000711 void SetReceiveAudio(bool audio) {
712 if (audio && can_receive_audio())
713 return;
714 session_description_constraints_.SetMandatoryReceiveAudio(audio);
715 }
716
717 void SetReceiveVideo(bool video) {
718 if (video && can_receive_video())
719 return;
720 session_description_constraints_.SetMandatoryReceiveVideo(video);
721 }
722
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 void RemoveMsidFromReceivedSdp(bool remove) {
724 remove_msid_ = remove;
725 }
726
727 void RemoveSdesCryptoFromReceivedSdp(bool remove) {
728 remove_sdes_ = remove;
729 }
730
731 void RemoveBundleFromReceivedSdp(bool remove) {
732 remove_bundle_ = remove;
733 }
734
735 virtual bool can_receive_audio() {
736 bool value;
737 if (webrtc::FindConstraint(&session_description_constraints_,
738 MediaConstraintsInterface::kOfferToReceiveAudio, &value, NULL)) {
739 return value;
740 }
741 return true;
742 }
743
744 virtual bool can_receive_video() {
745 bool value;
746 if (webrtc::FindConstraint(&session_description_constraints_,
747 MediaConstraintsInterface::kOfferToReceiveVideo, &value, NULL)) {
748 return value;
749 }
750 return true;
751 }
752
753 virtual void OnIceComplete() {
754 LOG(INFO) << id() << "OnIceComplete";
755 }
756
757 virtual void OnDataChannel(DataChannelInterface* data_channel) {
758 LOG(INFO) << id() << "OnDataChannel";
759 data_channel_ = data_channel;
760 data_observer_.reset(new MockDataChannelObserver(data_channel));
761 }
762
763 void CreateDataChannel() {
764 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel,
765 NULL);
766 ASSERT_TRUE(data_channel_.get() != NULL);
767 data_observer_.reset(new MockDataChannelObserver(data_channel_));
768 }
769
770 DataChannelInterface* data_channel() { return data_channel_; }
771 const MockDataChannelObserver* data_observer() const {
772 return data_observer_.get();
773 }
774
775 protected:
776 explicit JsepTestClient(const std::string& id)
777 : PeerConnectionTestClientBase<JsepMessageReceiver>(id),
778 remove_msid_(false),
779 remove_bundle_(false),
780 remove_sdes_(false) {
781 }
782
Henrik Boström5e56c592015-08-11 10:33:13 +0200783 rtc::scoped_refptr<webrtc::PeerConnectionInterface>
784 CreatePeerConnection(
785 webrtc::PortAllocatorFactoryInterface* factory,
786 const MediaConstraintsInterface* constraints) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 // CreatePeerConnection with IceServers.
788 webrtc::PeerConnectionInterface::IceServers ice_servers;
789 webrtc::PeerConnectionInterface::IceServer ice_server;
790 ice_server.uri = "stun:stun.l.google.com:19302";
791 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000792
Henrik Boström5e56c592015-08-11 10:33:13 +0200793 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store(
794 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
795 : nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 return peer_connection_factory()->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200797 ice_servers, constraints, factory, dtls_identity_store.Pass(), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 }
799
800 void HandleIncomingOffer(const std::string& msg) {
801 LOG(INFO) << id() << "HandleIncomingOffer ";
802 if (NumberOfLocalMediaStreams() == 0) {
803 // If we are not sending any streams ourselves it is time to add some.
804 AddMediaStream(true, true);
805 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000806 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 webrtc::CreateSessionDescription("offer", msg, NULL));
808 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000809 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 EXPECT_TRUE(DoCreateAnswer(answer.use()));
811 std::string sdp;
812 EXPECT_TRUE(answer->ToString(&sdp));
813 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
814 if (signaling_message_receiver()) {
815 signaling_message_receiver()->ReceiveSdpMessage(
816 webrtc::SessionDescriptionInterface::kAnswer, sdp);
817 }
818 }
819
820 void HandleIncomingAnswer(const std::string& msg) {
821 LOG(INFO) << id() << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000822 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 webrtc::CreateSessionDescription("answer", msg, NULL));
824 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
825 }
826
827 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
828 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000829 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
830 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 MockCreateSessionDescriptionObserver>());
832 if (offer) {
833 pc()->CreateOffer(observer, &session_description_constraints_);
834 } else {
835 pc()->CreateAnswer(observer, &session_description_constraints_);
836 }
837 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
838 *desc = observer->release_desc();
839 if (observer->result() && ExpectIceRestart()) {
840 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
841 }
842 return observer->result();
843 }
844
845 bool DoCreateOffer(SessionDescriptionInterface** desc) {
846 return DoCreateOfferAnswer(desc, true);
847 }
848
849 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
850 return DoCreateOfferAnswer(desc, false);
851 }
852
853 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000854 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
855 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 MockSetSessionDescriptionObserver>());
857 LOG(INFO) << id() << "SetLocalDescription ";
858 pc()->SetLocalDescription(observer, desc);
859 // Ignore the observer result. If we wait for the result with
860 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
861 // before the offer which is an error.
862 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000863 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 // ProcessMessages waits at least 1ms but processes all messages before
865 // returning. Since this test is synchronous and send messages to the remote
866 // peer whenever a callback is invoked, this can lead to messages being
867 // sent to the remote peer in the wrong order.
868 // TODO(perkj): Find a way to check the result without risking that the
869 // order of sent messages are changed. Ex- by posting all messages that are
870 // sent to the remote peer.
871 return true;
872 }
873
874 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000875 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
876 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 MockSetSessionDescriptionObserver>());
878 LOG(INFO) << id() << "SetRemoteDescription ";
879 pc()->SetRemoteDescription(observer, desc);
880 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
881 return observer->result();
882 }
883
884 // This modifies all received SDP messages before they are processed.
885 void FilterIncomingSdpMessage(std::string* sdp) {
886 if (remove_msid_) {
887 const char kSdpSsrcAttribute[] = "a=ssrc:";
888 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
889 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
890 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
891 }
892 if (remove_bundle_) {
893 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
894 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
895 }
896 if (remove_sdes_) {
897 const char kSdpSdesCryptoAttribute[] = "a=crypto";
898 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
899 }
900 }
901
902 private:
903 webrtc::FakeConstraints session_description_constraints_;
904 bool remove_msid_; // True if MSID should be removed in received SDP.
905 bool remove_bundle_; // True if bundle should be removed in received SDP.
906 bool remove_sdes_; // True if a=crypto should be removed in received SDP.
907
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000908 rtc::scoped_refptr<DataChannelInterface> data_channel_;
909 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910};
911
912template <typename SignalingClass>
913class P2PTestConductor : public testing::Test {
914 public:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000915 P2PTestConductor()
916 : pss_(new rtc::PhysicalSocketServer),
917 ss_(new rtc::VirtualSocketServer(pss_.get())),
918 ss_scope_(ss_.get()) {}
919
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 bool SessionActive() {
921 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000922 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000924
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 // Return true if the number of frames provided have been received or it is
926 // known that that will never occur (e.g. no frames will be sent or
927 // captured).
928 bool FramesNotPending(int audio_frames_to_receive,
929 int video_frames_to_receive) {
930 return VideoFramesReceivedCheck(video_frames_to_receive) &&
931 AudioFramesReceivedCheck(audio_frames_to_receive);
932 }
933 bool AudioFramesReceivedCheck(int frames_received) {
934 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
935 receiving_client_->AudioFramesReceivedCheck(frames_received);
936 }
937 bool VideoFramesReceivedCheck(int frames_received) {
938 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
939 receiving_client_->VideoFramesReceivedCheck(frames_received);
940 }
941 void VerifyDtmf() {
942 initiating_client_->VerifyDtmf();
943 receiving_client_->VerifyDtmf();
944 }
945
946 void TestUpdateOfferWithRejectedContent() {
947 initiating_client_->Negotiate(true, false);
948 EXPECT_TRUE_WAIT(
949 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
950 kMaxWaitForFramesMs);
951 // There shouldn't be any more video frame after the new offer is
952 // negotiated.
953 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
954 }
955
956 void VerifyRenderedSize(int width, int height) {
957 EXPECT_EQ(width, receiving_client()->rendered_width());
958 EXPECT_EQ(height, receiving_client()->rendered_height());
959 EXPECT_EQ(width, initializing_client()->rendered_width());
960 EXPECT_EQ(height, initializing_client()->rendered_height());
961 }
962
963 void VerifySessionDescriptions() {
964 initiating_client_->VerifyRejectedMediaInSessionDescription();
965 receiving_client_->VerifyRejectedMediaInSessionDescription();
966 initiating_client_->VerifyLocalIceUfragAndPassword();
967 receiving_client_->VerifyLocalIceUfragAndPassword();
968 }
969
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 ~P2PTestConductor() {
971 if (initiating_client_) {
972 initiating_client_->set_signaling_message_receiver(NULL);
973 }
974 if (receiving_client_) {
975 receiving_client_->set_signaling_message_receiver(NULL);
976 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 }
978
979 bool CreateTestClients() {
980 return CreateTestClients(NULL, NULL);
981 }
982
983 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
984 MediaConstraintsInterface* recv_constraints) {
Joachim Bauch04e5b492015-05-29 09:40:39 +0200985 return CreateTestClients(init_constraints, NULL, recv_constraints, NULL);
986 }
987
988 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
989 PeerConnectionFactory::Options* init_options,
990 MediaConstraintsInterface* recv_constraints,
991 PeerConnectionFactory::Options* recv_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 initiating_client_.reset(SignalingClass::CreateClient("Caller: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200993 init_constraints,
994 init_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 receiving_client_.reset(SignalingClass::CreateClient("Callee: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200996 recv_constraints,
997 recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 if (!initiating_client_ || !receiving_client_) {
999 return false;
1000 }
1001 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1002 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1003 return true;
1004 }
1005
1006 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1007 const webrtc::FakeConstraints& recv_constraints) {
1008 initiating_client_->SetVideoConstraints(init_constraints);
1009 receiving_client_->SetVideoConstraints(recv_constraints);
1010 }
1011
1012 void EnableVideoDecoderFactory() {
1013 initiating_client_->EnableVideoDecoderFactory();
1014 receiving_client_->EnableVideoDecoderFactory();
1015 }
1016
1017 // This test sets up a call between two parties. Both parties send static
1018 // frames to each other. Once the test is finished the number of sent frames
1019 // is compared to the number of received frames.
1020 void LocalP2PTest() {
1021 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1022 initiating_client_->AddMediaStream(true, true);
1023 }
1024 initiating_client_->Negotiate();
1025 const int kMaxWaitForActivationMs = 5000;
1026 // Assert true is used here since next tests are guaranteed to fail and
1027 // would eat up 5 seconds.
1028 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1029 VerifySessionDescriptions();
1030
1031
1032 int audio_frame_count = kEndAudioFrameCount;
1033 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1034 if (!initiating_client_->can_receive_audio() ||
1035 !receiving_client_->can_receive_audio()) {
1036 audio_frame_count = -1;
1037 }
1038 int video_frame_count = kEndVideoFrameCount;
1039 if (!initiating_client_->can_receive_video() ||
1040 !receiving_client_->can_receive_video()) {
1041 video_frame_count = -1;
1042 }
1043
1044 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001045 // Audio or video is expected to flow, so both clients should reach the
1046 // Connected state, and the offerer (ICE controller) should proceed to
1047 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 // Note: These tests have been observed to fail under heavy load at
1049 // shorter timeouts, so they may be flaky.
1050 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001051 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 initiating_client_->ice_connection_state(),
1053 kMaxWaitForFramesMs);
1054 EXPECT_EQ_WAIT(
1055 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1056 receiving_client_->ice_connection_state(),
1057 kMaxWaitForFramesMs);
1058 }
1059
1060 if (initiating_client_->can_receive_audio() ||
1061 initiating_client_->can_receive_video()) {
1062 // The initiating client can receive media, so it must produce candidates
1063 // that will serve as destinations for that media.
1064 // TODO(bemasc): Understand why the state is not already Complete here, as
1065 // seems to be the case for the receiving client. This may indicate a bug
1066 // in the ICE gathering system.
1067 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1068 initiating_client_->ice_gathering_state());
1069 }
1070 if (receiving_client_->can_receive_audio() ||
1071 receiving_client_->can_receive_video()) {
1072 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1073 receiving_client_->ice_gathering_state(),
1074 kMaxWaitForFramesMs);
1075 }
1076
1077 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1078 kMaxWaitForFramesMs);
1079 }
1080
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001081 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1082 // Messages may get lost on the unreliable DataChannel, so we send multiple
1083 // times to avoid test flakiness.
1084 static const size_t kSendAttempts = 5;
1085
1086 for (size_t i = 0; i < kSendAttempts; ++i) {
1087 dc->Send(DataBuffer(data));
1088 }
1089 }
1090
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091 SignalingClass* initializing_client() { return initiating_client_.get(); }
1092 SignalingClass* receiving_client() { return receiving_client_.get(); }
1093
1094 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001095 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1096 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1097 rtc::SocketServerScope ss_scope_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001098 rtc::scoped_ptr<SignalingClass> initiating_client_;
1099 rtc::scoped_ptr<SignalingClass> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100};
1101typedef P2PTestConductor<JsepTestClient> JsepPeerConnectionP2PTestClient;
1102
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001103// Disable for TSan v2, see
1104// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1105#if !defined(THREAD_SANITIZER)
1106
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001107// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001108// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1109// See issue webrtc/2378.
1110TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 ASSERT_TRUE(CreateTestClients());
1112 LocalP2PTest();
1113 VerifyDtmf();
1114}
1115
1116// This test sets up a Jsep call between two parties and test that we can get a
1117// video aspect ratio of 16:9.
1118TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
1119 ASSERT_TRUE(CreateTestClients());
1120 FakeConstraints constraint;
1121 double requested_ratio = 640.0/360;
1122 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1123 SetVideoConstraints(constraint, constraint);
1124 LocalP2PTest();
1125
1126 ASSERT_LE(0, initializing_client()->rendered_height());
1127 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001128 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 initializing_client()->rendered_height();
1130 EXPECT_LE(requested_ratio, initiating_video_ratio);
1131
1132 ASSERT_LE(0, receiving_client()->rendered_height());
1133 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001134 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 receiving_client()->rendered_height();
1136 EXPECT_LE(requested_ratio, receiving_video_ratio);
1137}
1138
1139// This test sets up a Jsep call between two parties and test that the
1140// received video has a resolution of 1280*720.
1141// TODO(mallinath): Enable when
1142// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
1143TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
1144 ASSERT_TRUE(CreateTestClients());
1145 FakeConstraints constraint;
1146 constraint.SetMandatoryMinWidth(1280);
1147 constraint.SetMandatoryMinHeight(720);
1148 SetVideoConstraints(constraint, constraint);
1149 LocalP2PTest();
1150 VerifyRenderedSize(1280, 720);
1151}
1152
1153// This test sets up a call between two endpoints that are configured to use
1154// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
1155TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001156 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 FakeConstraints setup_constraints;
1158 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1159 true);
1160 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1161 LocalP2PTest();
1162 VerifyRenderedSize(640, 480);
1163}
1164
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001165// This test sets up a audio call initially and then upgrades to audio/video,
1166// using DTLS.
mallinath@webrtc.org50bc5532013-10-21 17:58:35 +00001167TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001168 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001169 FakeConstraints setup_constraints;
1170 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1171 true);
1172 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1173 receiving_client()->SetReceiveAudioVideo(true, false);
1174 LocalP2PTest();
1175 receiving_client()->SetReceiveAudioVideo(true, true);
1176 receiving_client()->Negotiate();
1177}
1178
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179// This test sets up a call between two endpoints that are configured to use
1180// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1181// negotiated and used for transport.
1182TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001183 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 FakeConstraints setup_constraints;
1185 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1186 true);
1187 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1188 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1189 LocalP2PTest();
1190 VerifyRenderedSize(640, 480);
1191}
1192
1193// This test sets up a Jsep call between two parties, and the callee only
1194// accept to receive video.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001195TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 ASSERT_TRUE(CreateTestClients());
1197 receiving_client()->SetReceiveAudioVideo(false, true);
1198 LocalP2PTest();
1199}
1200
1201// This test sets up a Jsep call between two parties, and the callee only
1202// accept to receive audio.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001203TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 ASSERT_TRUE(CreateTestClients());
1205 receiving_client()->SetReceiveAudioVideo(true, false);
1206 LocalP2PTest();
1207}
1208
1209// This test sets up a Jsep call between two parties, and the callee reject both
1210// audio and video.
1211TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
1212 ASSERT_TRUE(CreateTestClients());
1213 receiving_client()->SetReceiveAudioVideo(false, false);
1214 LocalP2PTest();
1215}
1216
1217// This test sets up an audio and video call between two parties. After the call
1218// runs for a while (10 frames), the caller sends an update offer with video
1219// being rejected. Once the re-negotiation is done, the video flow should stop
1220// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001221// Disabled due to b/14955157.
1222TEST_F(JsepPeerConnectionP2PTestClient,
1223 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 ASSERT_TRUE(CreateTestClients());
1225 LocalP2PTest();
1226 TestUpdateOfferWithRejectedContent();
1227}
1228
1229// This test sets up a Jsep call between two parties. The MSID is removed from
1230// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001231// Disabled due to b/14955157.
1232TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233 ASSERT_TRUE(CreateTestClients());
1234 receiving_client()->RemoveMsidFromReceivedSdp(true);
1235 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1236 // audio and video is muxed when MSID is disabled. Remove
1237 // SetRemoveBundleFromSdp once
1238 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1239 receiving_client()->RemoveBundleFromReceivedSdp(true);
1240 LocalP2PTest();
1241}
1242
1243// This test sets up a Jsep call between two parties and the initiating peer
1244// sends two steams.
1245// TODO(perkj): Disabled due to
1246// https://code.google.com/p/webrtc/issues/detail?id=1454
1247TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
1248 ASSERT_TRUE(CreateTestClients());
1249 // Set optional video constraint to max 320pixels to decrease CPU usage.
1250 FakeConstraints constraint;
1251 constraint.SetOptionalMaxWidth(320);
1252 SetVideoConstraints(constraint, constraint);
1253 initializing_client()->AddMediaStream(true, true);
1254 initializing_client()->AddMediaStream(false, true);
1255 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1256 LocalP2PTest();
1257 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1258}
1259
1260// Test that we can receive the audio output level from a remote audio track.
1261TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
1262 ASSERT_TRUE(CreateTestClients());
1263 LocalP2PTest();
1264
1265 StreamCollectionInterface* remote_streams =
1266 initializing_client()->remote_streams();
1267 ASSERT_GT(remote_streams->count(), 0u);
1268 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1269 MediaStreamTrackInterface* remote_audio_track =
1270 remote_streams->at(0)->GetAudioTracks()[0];
1271
1272 // Get the audio output level stats. Note that the level is not available
1273 // until a RTCP packet has been received.
1274 EXPECT_TRUE_WAIT(
1275 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1276 kMaxWaitForStatsMs);
1277}
1278
1279// Test that an audio input level is reported.
1280TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
1281 ASSERT_TRUE(CreateTestClients());
1282 LocalP2PTest();
1283
1284 // Get the audio input level stats. The level should be available very
1285 // soon after the test starts.
1286 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1287 kMaxWaitForStatsMs);
1288}
1289
1290// Test that we can get incoming byte counts from both audio and video tracks.
1291TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
1292 ASSERT_TRUE(CreateTestClients());
1293 LocalP2PTest();
1294
1295 StreamCollectionInterface* remote_streams =
1296 initializing_client()->remote_streams();
1297 ASSERT_GT(remote_streams->count(), 0u);
1298 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1299 MediaStreamTrackInterface* remote_audio_track =
1300 remote_streams->at(0)->GetAudioTracks()[0];
1301 EXPECT_TRUE_WAIT(
1302 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1303 kMaxWaitForStatsMs);
1304
1305 MediaStreamTrackInterface* remote_video_track =
1306 remote_streams->at(0)->GetVideoTracks()[0];
1307 EXPECT_TRUE_WAIT(
1308 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1309 kMaxWaitForStatsMs);
1310}
1311
1312// Test that we can get outgoing byte counts from both audio and video tracks.
1313TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
1314 ASSERT_TRUE(CreateTestClients());
1315 LocalP2PTest();
1316
1317 StreamCollectionInterface* local_streams =
1318 initializing_client()->local_streams();
1319 ASSERT_GT(local_streams->count(), 0u);
1320 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1321 MediaStreamTrackInterface* local_audio_track =
1322 local_streams->at(0)->GetAudioTracks()[0];
1323 EXPECT_TRUE_WAIT(
1324 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1325 kMaxWaitForStatsMs);
1326
1327 MediaStreamTrackInterface* local_video_track =
1328 local_streams->at(0)->GetVideoTracks()[0];
1329 EXPECT_TRUE_WAIT(
1330 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1331 kMaxWaitForStatsMs);
1332}
1333
Joachim Bauch04e5b492015-05-29 09:40:39 +02001334// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
1335TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12None) {
1336 PeerConnectionFactory::Options init_options;
1337 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1338 PeerConnectionFactory::Options recv_options;
1339 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1340 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001341 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1342 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1343 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001344 LocalP2PTest();
1345
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001346 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1347 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1348 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1349 initializing_client()->GetDtlsCipherStats(),
1350 kMaxWaitForStatsMs);
1351 EXPECT_EQ(1, init_observer->GetEnumCounter(
1352 webrtc::kEnumCounterAudioSslCipher,
1353 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1354 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001355
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001356 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1357 initializing_client()->GetSrtpCipherStats(),
1358 kMaxWaitForStatsMs);
1359 EXPECT_EQ(1, init_observer->GetEnumCounter(
1360 webrtc::kEnumCounterAudioSrtpCipher,
1361 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001362}
1363
1364// Test that DTLS 1.2 is used if both ends support it.
1365TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Both) {
1366 PeerConnectionFactory::Options init_options;
1367 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1368 PeerConnectionFactory::Options recv_options;
1369 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1370 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001371 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1372 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1373 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001374 LocalP2PTest();
1375
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001376 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1377 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1378 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1379 initializing_client()->GetDtlsCipherStats(),
1380 kMaxWaitForStatsMs);
1381 EXPECT_EQ(1, init_observer->GetEnumCounter(
1382 webrtc::kEnumCounterAudioSslCipher,
1383 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1384 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001385
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001386 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1387 initializing_client()->GetSrtpCipherStats(),
1388 kMaxWaitForStatsMs);
1389 EXPECT_EQ(1, init_observer->GetEnumCounter(
1390 webrtc::kEnumCounterAudioSrtpCipher,
1391 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001392}
1393
1394// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1395// received supports 1.0.
1396TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Init) {
1397 PeerConnectionFactory::Options init_options;
1398 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1399 PeerConnectionFactory::Options recv_options;
1400 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1401 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001402 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1403 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1404 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001405 LocalP2PTest();
1406
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001407 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1408 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1409 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1410 initializing_client()->GetDtlsCipherStats(),
1411 kMaxWaitForStatsMs);
1412 EXPECT_EQ(1, init_observer->GetEnumCounter(
1413 webrtc::kEnumCounterAudioSslCipher,
1414 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1415 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001416
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001417 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1418 initializing_client()->GetSrtpCipherStats(),
1419 kMaxWaitForStatsMs);
1420 EXPECT_EQ(1, init_observer->GetEnumCounter(
1421 webrtc::kEnumCounterAudioSrtpCipher,
1422 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001423}
1424
1425// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1426// received supports 1.2.
1427TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
1428 PeerConnectionFactory::Options init_options;
1429 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1430 PeerConnectionFactory::Options recv_options;
1431 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1432 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001433 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1434 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1435 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001436 LocalP2PTest();
1437
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001438 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1439 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1440 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1441 initializing_client()->GetDtlsCipherStats(),
1442 kMaxWaitForStatsMs);
1443 EXPECT_EQ(1, init_observer->GetEnumCounter(
1444 webrtc::kEnumCounterAudioSslCipher,
1445 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1446 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001447
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001448 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1449 initializing_client()->GetSrtpCipherStats(),
1450 kMaxWaitForStatsMs);
1451 EXPECT_EQ(1, init_observer->GetEnumCounter(
1452 webrtc::kEnumCounterAudioSrtpCipher,
1453 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001454}
1455
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456// This test sets up a call between two parties with audio, video and data.
1457TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
1458 FakeConstraints setup_constraints;
1459 setup_constraints.SetAllowRtpDataChannels();
1460 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1461 initializing_client()->CreateDataChannel();
1462 LocalP2PTest();
1463 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1464 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1465 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1466 kMaxWaitMs);
1467 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1468 kMaxWaitMs);
1469
1470 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001471
1472 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1474 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001475
1476 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1478 kMaxWaitMs);
1479
1480 receiving_client()->data_channel()->Close();
1481 // Send new offer and answer.
1482 receiving_client()->Negotiate();
1483 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1484 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1485}
1486
1487// This test sets up a call between two parties and creates a data channel.
1488// The test tests that received data is buffered unless an observer has been
1489// registered.
1490// Rtp data channels can receive data before the underlying
1491// transport has detected that a channel is writable and thus data can be
1492// received before the data channel state changes to open. That is hard to test
1493// but the same buffering is used in that case.
1494TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
1495 FakeConstraints setup_constraints;
1496 setup_constraints.SetAllowRtpDataChannels();
1497 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1498 initializing_client()->CreateDataChannel();
1499 initializing_client()->Negotiate();
1500
1501 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1502 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1503 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1504 kMaxWaitMs);
1505 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1506 receiving_client()->data_channel()->state(), kMaxWaitMs);
1507
1508 // Unregister the existing observer.
1509 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001510
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001512 SendRtpData(initializing_client()->data_channel(), data);
1513
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 // Wait a while to allow the sent data to arrive before an observer is
1515 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001516 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517
1518 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1519 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1520}
1521
1522// This test sets up a call between two parties with audio, video and but only
1523// the initiating client support data.
1524TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001525 FakeConstraints setup_constraints_1;
1526 setup_constraints_1.SetAllowRtpDataChannels();
1527 // Must disable DTLS to make negotiation succeed.
1528 setup_constraints_1.SetMandatory(
1529 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1530 FakeConstraints setup_constraints_2;
1531 setup_constraints_2.SetMandatory(
1532 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1533 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534 initializing_client()->CreateDataChannel();
1535 LocalP2PTest();
1536 EXPECT_TRUE(initializing_client()->data_channel() != NULL);
1537 EXPECT_FALSE(receiving_client()->data_channel());
1538 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1539}
1540
1541// This test sets up a call between two parties with audio, video. When audio
1542// and video is setup and flowing and data channel is negotiated.
1543TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
1544 FakeConstraints setup_constraints;
1545 setup_constraints.SetAllowRtpDataChannels();
1546 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1547 LocalP2PTest();
1548 initializing_client()->CreateDataChannel();
1549 // Send new offer and answer.
1550 initializing_client()->Negotiate();
1551 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1552 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1553 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1554 kMaxWaitMs);
1555 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1556 kMaxWaitMs);
1557}
1558
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001559// This test sets up a Jsep call with SCTP DataChannel and verifies the
1560// negotiation is completed without error.
1561#ifdef HAVE_SCTP
1562TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001563 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001564 FakeConstraints constraints;
1565 constraints.SetMandatory(
1566 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1567 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1568 initializing_client()->CreateDataChannel();
1569 initializing_client()->Negotiate(false, false);
1570}
1571#endif
1572
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573// This test sets up a call between two parties with audio, and video.
1574// During the call, the initializing side restart ice and the test verifies that
1575// new ice candidates are generated and audio and video still can flow.
1576TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
1577 ASSERT_TRUE(CreateTestClients());
1578
1579 // Negotiate and wait for ice completion and make sure audio and video plays.
1580 LocalP2PTest();
1581
1582 // Create a SDP string of the first audio candidate for both clients.
1583 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1584 initializing_client()->pc()->local_description()->candidates(0);
1585 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1586 receiving_client()->pc()->local_description()->candidates(0);
1587 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1588 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1589 std::string initiator_candidate;
1590 EXPECT_TRUE(
1591 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1592 std::string receiver_candidate;
1593 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1594
1595 // Restart ice on the initializing client.
1596 receiving_client()->SetExpectIceRestart(true);
1597 initializing_client()->IceRestart();
1598
1599 // Negotiate and wait for ice completion again and make sure audio and video
1600 // plays.
1601 LocalP2PTest();
1602
1603 // Create a SDP string of the first audio candidate for both clients again.
1604 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1605 initializing_client()->pc()->local_description()->candidates(0);
1606 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1607 receiving_client()->pc()->local_description()->candidates(0);
1608 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1609 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1610 std::string initiator_candidate_restart;
1611 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1612 &initiator_candidate_restart));
1613 std::string receiver_candidate_restart;
1614 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1615 &receiver_candidate_restart));
1616
1617 // Verify that the first candidates in the local session descriptions has
1618 // changed.
1619 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1620 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1621}
1622
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623// This test sets up a Jsep call between two parties with external
1624// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001625// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1626// See issue webrtc/2378.
1627TEST_F(JsepPeerConnectionP2PTestClient,
1628 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629 ASSERT_TRUE(CreateTestClients());
1630 EnableVideoDecoderFactory();
1631 LocalP2PTest();
1632}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001633
deadbeef0a6c4ca2015-10-06 11:38:28 -07001634class IceServerParsingTest : public testing::Test {
1635 public:
1636 // Convenience for parsing a single URL.
1637 bool ParseUrl(const std::string& url) {
1638 return ParseUrl(url, std::string(), std::string());
1639 }
1640
1641 bool ParseUrl(const std::string& url,
1642 const std::string& username,
1643 const std::string& password) {
1644 PeerConnectionInterface::IceServers servers;
1645 PeerConnectionInterface::IceServer server;
1646 server.urls.push_back(url);
1647 server.username = username;
1648 server.password = password;
1649 servers.push_back(server);
1650 return webrtc::ParseIceServers(servers, &stun_configurations_,
1651 &turn_configurations_);
1652 }
1653
1654 protected:
1655 webrtc::StunConfigurations stun_configurations_;
1656 webrtc::TurnConfigurations turn_configurations_;
1657};
1658
1659// Make sure all STUN/TURN prefixes are parsed correctly.
1660TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1661 EXPECT_TRUE(ParseUrl("stun:hostname"));
1662 EXPECT_EQ(1U, stun_configurations_.size());
1663 EXPECT_EQ(0U, turn_configurations_.size());
1664 stun_configurations_.clear();
1665
1666 EXPECT_TRUE(ParseUrl("stuns:hostname"));
1667 EXPECT_EQ(1U, stun_configurations_.size());
1668 EXPECT_EQ(0U, turn_configurations_.size());
1669 stun_configurations_.clear();
1670
1671 EXPECT_TRUE(ParseUrl("turn:hostname"));
1672 EXPECT_EQ(0U, stun_configurations_.size());
1673 EXPECT_EQ(1U, turn_configurations_.size());
1674 EXPECT_FALSE(turn_configurations_[0].secure);
1675 turn_configurations_.clear();
1676
1677 EXPECT_TRUE(ParseUrl("turns:hostname"));
1678 EXPECT_EQ(0U, stun_configurations_.size());
1679 EXPECT_EQ(1U, turn_configurations_.size());
1680 EXPECT_TRUE(turn_configurations_[0].secure);
1681 turn_configurations_.clear();
1682
1683 // invalid prefixes
1684 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1685 EXPECT_FALSE(ParseUrl(":hostname"));
1686 EXPECT_FALSE(ParseUrl(":"));
1687 EXPECT_FALSE(ParseUrl(""));
1688}
1689
1690TEST_F(IceServerParsingTest, VerifyDefaults) {
1691 // TURNS defaults
1692 EXPECT_TRUE(ParseUrl("turns:hostname"));
1693 EXPECT_EQ(1U, turn_configurations_.size());
1694 EXPECT_EQ(5349, turn_configurations_[0].server.port());
1695 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1696 turn_configurations_.clear();
1697
1698 // TURN defaults
1699 EXPECT_TRUE(ParseUrl("turn:hostname"));
1700 EXPECT_EQ(1U, turn_configurations_.size());
1701 EXPECT_EQ(3478, turn_configurations_[0].server.port());
1702 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1703 turn_configurations_.clear();
1704
1705 // STUN defaults
1706 EXPECT_TRUE(ParseUrl("stun:hostname"));
1707 EXPECT_EQ(1U, stun_configurations_.size());
1708 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1709 stun_configurations_.clear();
1710}
1711
1712// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1713// can be parsed correctly.
1714TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1715 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
1716 EXPECT_EQ(1U, stun_configurations_.size());
1717 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1718 EXPECT_EQ(1234, stun_configurations_[0].server.port());
1719 stun_configurations_.clear();
1720
1721 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
1722 EXPECT_EQ(1U, stun_configurations_.size());
1723 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1724 EXPECT_EQ(4321, stun_configurations_[0].server.port());
1725 stun_configurations_.clear();
1726
1727 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
1728 EXPECT_EQ(1U, stun_configurations_.size());
1729 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1730 EXPECT_EQ(9999, stun_configurations_[0].server.port());
1731 stun_configurations_.clear();
1732
1733 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
1734 EXPECT_EQ(1U, stun_configurations_.size());
1735 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1736 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1737 stun_configurations_.clear();
1738
1739 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
1740 EXPECT_EQ(1U, stun_configurations_.size());
1741 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1742 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1743 stun_configurations_.clear();
1744
1745 EXPECT_TRUE(ParseUrl("stun:hostname"));
1746 EXPECT_EQ(1U, stun_configurations_.size());
1747 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1748 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1749 stun_configurations_.clear();
1750
1751 // Try some invalid hostname:port strings.
1752 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1753 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
1754 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1755 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1756 EXPECT_FALSE(ParseUrl("stun::5555"));
1757 EXPECT_FALSE(ParseUrl("stun:"));
1758}
1759
1760// Test parsing the "?transport=xxx" part of the URL.
1761TEST_F(IceServerParsingTest, ParseTransport) {
1762 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
1763 EXPECT_EQ(1U, turn_configurations_.size());
1764 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1765 turn_configurations_.clear();
1766
1767 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
1768 EXPECT_EQ(1U, turn_configurations_.size());
1769 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1770 turn_configurations_.clear();
1771
1772 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1773}
1774
1775// Test parsing ICE username contained in URL.
1776TEST_F(IceServerParsingTest, ParseUsername) {
1777 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
1778 EXPECT_EQ(1U, turn_configurations_.size());
1779 EXPECT_EQ("user", turn_configurations_[0].username);
1780 turn_configurations_.clear();
1781
1782 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1783 EXPECT_FALSE(ParseUrl("turn:username@"));
1784 EXPECT_FALSE(ParseUrl("turn:@"));
1785 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1786}
1787
1788// Test that username and password from IceServer is copied into the resulting
1789// TurnConfiguration.
1790TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1791 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
1792 EXPECT_EQ(1U, turn_configurations_.size());
1793 EXPECT_EQ("username", turn_configurations_[0].username);
1794 EXPECT_EQ("password", turn_configurations_[0].password);
1795}
1796
1797// Ensure that if a server has multiple URLs, each one is parsed.
1798TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1799 PeerConnectionInterface::IceServers servers;
1800 PeerConnectionInterface::IceServer server;
1801 server.urls.push_back("stun:hostname");
1802 server.urls.push_back("turn:hostname");
1803 servers.push_back(server);
1804 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_configurations_,
1805 &turn_configurations_));
1806 EXPECT_EQ(1U, stun_configurations_.size());
1807 EXPECT_EQ(1U, turn_configurations_.size());
1808}
1809
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001810#endif // if !defined(THREAD_SANITIZER)