blob: 649dafbdf1a8cef4717eeaa7210822056a531cdb [file] [log] [blame]
deadbeefe814a0d2017-02-25 18:15:09 -08001/*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <memory>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "media/base/fakemediaengine.h"
14#include "ortc/ortcfactory.h"
15#include "ortc/testrtpparameters.h"
16#include "p2p/base/fakepackettransport.h"
17#include "rtc_base/fakenetwork.h"
18#include "rtc_base/gunit.h"
19#include "rtc_base/virtualsocketserver.h"
deadbeefe814a0d2017-02-25 18:15:09 -080020
21namespace webrtc {
22
23// This test uses a virtual network and fake media engine, in order to test the
24// OrtcFactory at only an API level. Any end-to-end test should go in
25// ortcfactory_integrationtest.cc instead.
26class OrtcFactoryTest : public testing::Test {
27 public:
28 OrtcFactoryTest()
deadbeef98e186c2017-05-16 18:00:06 -070029 : thread_(&virtual_socket_server_),
deadbeefe814a0d2017-02-25 18:15:09 -080030 fake_packet_transport_("fake transport") {
31 ortc_factory_ =
deadbeef57ca81a2017-06-29 05:34:45 -070032 OrtcFactory::Create(&thread_, nullptr, &fake_network_manager_, nullptr,
deadbeefe814a0d2017-02-25 18:15:09 -080033 nullptr,
34 std::unique_ptr<cricket::MediaEngineInterface>(
35 new cricket::FakeMediaEngine()))
36 .MoveValue();
37 }
38
39 protected:
40 // Uses a single pre-made FakePacketTransport, so shouldn't be called twice in
41 // the same test.
42 std::unique_ptr<RtpTransportInterface>
43 CreateRtpTransportWithFakePacketTransport() {
44 return ortc_factory_
45 ->CreateRtpTransport(MakeRtcpMuxParameters(), &fake_packet_transport_,
46 nullptr, nullptr)
47 .MoveValue();
48 }
49
deadbeefe814a0d2017-02-25 18:15:09 -080050 rtc::VirtualSocketServer virtual_socket_server_;
nisse7eaa4ea2017-05-08 05:25:41 -070051 rtc::AutoSocketServerThread thread_;
deadbeefe814a0d2017-02-25 18:15:09 -080052 rtc::FakeNetworkManager fake_network_manager_;
53 rtc::FakePacketTransport fake_packet_transport_;
54 std::unique_ptr<OrtcFactoryInterface> ortc_factory_;
55};
56
57TEST_F(OrtcFactoryTest, CanCreateMultipleRtpTransportControllers) {
58 auto controller_result1 = ortc_factory_->CreateRtpTransportController();
59 EXPECT_TRUE(controller_result1.ok());
60 auto controller_result2 = ortc_factory_->CreateRtpTransportController();
61 EXPECT_TRUE(controller_result1.ok());
62}
63
64// Simple test for the successful cases of CreateRtpTransport.
65TEST_F(OrtcFactoryTest, CreateRtpTransportWithAndWithoutMux) {
66 rtc::FakePacketTransport rtp("rtp");
67 rtc::FakePacketTransport rtcp("rtcp");
68 // With muxed RTCP.
sprangdb2a9fc2017-08-09 06:42:32 -070069 RtpTransportParameters parameters = MakeRtcpMuxParameters();
70 auto result =
71 ortc_factory_->CreateRtpTransport(parameters, &rtp, nullptr, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -080072 EXPECT_TRUE(result.ok());
73 result.MoveValue().reset();
74 // With non-muxed RTCP.
sprangdb2a9fc2017-08-09 06:42:32 -070075 parameters.rtcp.mux = false;
76 result = ortc_factory_->CreateRtpTransport(parameters, &rtp, &rtcp, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -080077 EXPECT_TRUE(result.ok());
78}
79
zhihuangd3501ad2017-03-03 14:39:06 -080080// Simple test for the successful cases of CreateSrtpTransport.
81TEST_F(OrtcFactoryTest, CreateSrtpTransport) {
82 rtc::FakePacketTransport rtp("rtp");
83 rtc::FakePacketTransport rtcp("rtcp");
84 // With muxed RTCP.
sprangdb2a9fc2017-08-09 06:42:32 -070085 RtpTransportParameters parameters = MakeRtcpMuxParameters();
86 auto result =
87 ortc_factory_->CreateSrtpTransport(parameters, &rtp, nullptr, nullptr);
zhihuangd3501ad2017-03-03 14:39:06 -080088 EXPECT_TRUE(result.ok());
89 result.MoveValue().reset();
90 // With non-muxed RTCP.
sprangdb2a9fc2017-08-09 06:42:32 -070091 parameters.rtcp.mux = false;
92 result = ortc_factory_->CreateSrtpTransport(parameters, &rtp, &rtcp, nullptr);
zhihuangd3501ad2017-03-03 14:39:06 -080093 EXPECT_TRUE(result.ok());
94}
95
deadbeefe814a0d2017-02-25 18:15:09 -080096// If no CNAME is provided, one should be generated and returned by
97// GetRtpParameters.
98TEST_F(OrtcFactoryTest, CreateRtpTransportGeneratesCname) {
99 rtc::FakePacketTransport rtp("rtp");
sprangdb2a9fc2017-08-09 06:42:32 -0700100 auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(), &rtp,
deadbeefe814a0d2017-02-25 18:15:09 -0800101 nullptr, nullptr);
102 ASSERT_TRUE(result.ok());
sprangdb2a9fc2017-08-09 06:42:32 -0700103 EXPECT_FALSE(result.value()->GetParameters().rtcp.cname.empty());
deadbeefe814a0d2017-02-25 18:15:09 -0800104}
105
106// Extension of the above test; multiple transports created by the same factory
107// should use the same generated CNAME.
108TEST_F(OrtcFactoryTest, MultipleRtpTransportsUseSameGeneratedCname) {
109 rtc::FakePacketTransport packet_transport1("1");
110 rtc::FakePacketTransport packet_transport2("2");
sprangdb2a9fc2017-08-09 06:42:32 -0700111 RtpTransportParameters parameters = MakeRtcpMuxParameters();
deadbeefe814a0d2017-02-25 18:15:09 -0800112 // Sanity check.
sprangdb2a9fc2017-08-09 06:42:32 -0700113 ASSERT_TRUE(parameters.rtcp.cname.empty());
deadbeefe814a0d2017-02-25 18:15:09 -0800114 auto result = ortc_factory_->CreateRtpTransport(
sprangdb2a9fc2017-08-09 06:42:32 -0700115 parameters, &packet_transport1, nullptr, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -0800116 ASSERT_TRUE(result.ok());
117 auto rtp_transport1 = result.MoveValue();
sprangdb2a9fc2017-08-09 06:42:32 -0700118 result = ortc_factory_->CreateRtpTransport(parameters, &packet_transport2,
119 nullptr, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -0800120 ASSERT_TRUE(result.ok());
121 auto rtp_transport2 = result.MoveValue();
sprangdb2a9fc2017-08-09 06:42:32 -0700122 RtcpParameters params1 = rtp_transport1->GetParameters().rtcp;
123 RtcpParameters params2 = rtp_transport2->GetParameters().rtcp;
deadbeefe814a0d2017-02-25 18:15:09 -0800124 EXPECT_FALSE(params1.cname.empty());
125 EXPECT_EQ(params1.cname, params2.cname);
126}
127
128TEST_F(OrtcFactoryTest, CreateRtpTransportWithNoPacketTransport) {
129 auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(),
130 nullptr, nullptr, nullptr);
131 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
132}
133
134// If the |mux| member of the RtcpParameters is false, both an RTP and RTCP
135// packet transport are needed.
136TEST_F(OrtcFactoryTest, CreateRtpTransportWithMissingRtcpTransport) {
137 rtc::FakePacketTransport rtp("rtp");
sprangdb2a9fc2017-08-09 06:42:32 -0700138 RtpTransportParameters parameters;
139 parameters.rtcp.mux = false;
140 auto result =
141 ortc_factory_->CreateRtpTransport(parameters, &rtp, nullptr, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -0800142 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
143}
144
145// If the |mux| member of the RtcpParameters is true, only an RTP packet
146// transport is necessary. So, passing in an RTCP transport is most likely
147// an accident, and thus should be treated as an error.
148TEST_F(OrtcFactoryTest, CreateRtpTransportWithExtraneousRtcpTransport) {
149 rtc::FakePacketTransport rtp("rtp");
150 rtc::FakePacketTransport rtcp("rtcp");
sprangdb2a9fc2017-08-09 06:42:32 -0700151 auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(), &rtp,
152 &rtcp, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -0800153 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
154}
155
156// Basic test that CreateUdpTransport works with AF_INET and AF_INET6.
157TEST_F(OrtcFactoryTest, CreateUdpTransport) {
158 auto result = ortc_factory_->CreateUdpTransport(AF_INET);
159 EXPECT_TRUE(result.ok());
160 result = ortc_factory_->CreateUdpTransport(AF_INET6);
161 EXPECT_TRUE(result.ok());
162}
163
164// Test CreateUdpPort with the |min_port| and |max_port| arguments.
165TEST_F(OrtcFactoryTest, CreateUdpTransportWithPortRange) {
166 auto socket_result1 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
167 ASSERT_TRUE(socket_result1.ok());
168 EXPECT_EQ(2000, socket_result1.value()->GetLocalAddress().port());
169 auto socket_result2 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
170 ASSERT_TRUE(socket_result2.ok());
171 EXPECT_EQ(2001, socket_result2.value()->GetLocalAddress().port());
172 auto socket_result3 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
173 ASSERT_TRUE(socket_result3.ok());
174 EXPECT_EQ(2002, socket_result3.value()->GetLocalAddress().port());
175
176 // All sockets in the range have been exhausted, so the next call should
177 // fail.
178 auto failed_result = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
179 EXPECT_EQ(RTCErrorType::RESOURCE_EXHAUSTED, failed_result.error().type());
180
181 // If one socket is destroyed, that port should be freed up again.
182 socket_result2.MoveValue().reset();
183 auto socket_result4 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
184 ASSERT_TRUE(socket_result4.ok());
185 EXPECT_EQ(2001, socket_result4.value()->GetLocalAddress().port());
186}
187
188// Basic test that CreateUdpTransport works with AF_INET and AF_INET6.
189TEST_F(OrtcFactoryTest, CreateUdpTransportWithInvalidAddressFamily) {
190 auto result = ortc_factory_->CreateUdpTransport(12345);
191 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
192}
193
194TEST_F(OrtcFactoryTest, CreateUdpTransportWithInvalidPortRange) {
195 auto result = ortc_factory_->CreateUdpTransport(AF_INET, 3000, 2000);
196 EXPECT_EQ(RTCErrorType::INVALID_RANGE, result.error().type());
197}
198
199// Just sanity check that each "GetCapabilities" method returns some codecs.
200TEST_F(OrtcFactoryTest, GetSenderAndReceiverCapabilities) {
201 RtpCapabilities audio_send_caps =
202 ortc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_AUDIO);
203 EXPECT_GT(audio_send_caps.codecs.size(), 0u);
204 RtpCapabilities video_send_caps =
205 ortc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_VIDEO);
206 EXPECT_GT(video_send_caps.codecs.size(), 0u);
207 RtpCapabilities audio_receive_caps =
208 ortc_factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_AUDIO);
209 EXPECT_GT(audio_receive_caps.codecs.size(), 0u);
210 RtpCapabilities video_receive_caps =
211 ortc_factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_VIDEO);
212 EXPECT_GT(video_receive_caps.codecs.size(), 0u);
213}
214
215// Calling CreateRtpSender with a null track should fail, since that makes it
216// impossible to know whether to create an audio or video sender. The
217// application should be using the method that takes a cricket::MediaType
218// instead.
219TEST_F(OrtcFactoryTest, CreateSenderWithNullTrack) {
220 auto rtp_transport = CreateRtpTransportWithFakePacketTransport();
221 auto result = ortc_factory_->CreateRtpSender(nullptr, rtp_transport.get());
222 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
223}
224
225// Calling CreateRtpSender or CreateRtpReceiver with MEDIA_TYPE_DATA should
226// fail.
227TEST_F(OrtcFactoryTest, CreateSenderOrReceieverWithInvalidKind) {
228 auto rtp_transport = CreateRtpTransportWithFakePacketTransport();
229 auto sender_result = ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_DATA,
230 rtp_transport.get());
231 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, sender_result.error().type());
232 auto receiver_result = ortc_factory_->CreateRtpReceiver(
233 cricket::MEDIA_TYPE_DATA, rtp_transport.get());
234 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, receiver_result.error().type());
235}
236
237TEST_F(OrtcFactoryTest, CreateSendersOrReceieversWithNullTransport) {
238 auto sender_result =
239 ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_AUDIO, nullptr);
240 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, sender_result.error().type());
241 auto receiver_result =
242 ortc_factory_->CreateRtpReceiver(cricket::MEDIA_TYPE_AUDIO, nullptr);
243 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, receiver_result.error().type());
244}
245
246} // namespace webrtc