blob: 80e679b10e1a7e0185f822f24b51b59dcdcda10f [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
13#include "webrtc/base/fakenetwork.h"
14#include "webrtc/base/gunit.h"
15#include "webrtc/base/physicalsocketserver.h"
16#include "webrtc/base/virtualsocketserver.h"
17#include "webrtc/media/base/fakemediaengine.h"
18#include "webrtc/ortc/ortcfactory.h"
19#include "webrtc/ortc/testrtpparameters.h"
20#include "webrtc/p2p/base/fakepackettransport.h"
21
22namespace webrtc {
23
24// This test uses a virtual network and fake media engine, in order to test the
25// OrtcFactory at only an API level. Any end-to-end test should go in
26// ortcfactory_integrationtest.cc instead.
27class OrtcFactoryTest : public testing::Test {
28 public:
29 OrtcFactoryTest()
30 : virtual_socket_server_(&physical_socket_server_),
31 socket_server_scope_(&virtual_socket_server_),
32 fake_packet_transport_("fake transport") {
33 ortc_factory_ =
34 OrtcFactory::Create(nullptr, nullptr, &fake_network_manager_, nullptr,
35 nullptr,
36 std::unique_ptr<cricket::MediaEngineInterface>(
37 new cricket::FakeMediaEngine()))
38 .MoveValue();
39 }
40
41 protected:
42 // Uses a single pre-made FakePacketTransport, so shouldn't be called twice in
43 // the same test.
44 std::unique_ptr<RtpTransportInterface>
45 CreateRtpTransportWithFakePacketTransport() {
46 return ortc_factory_
47 ->CreateRtpTransport(MakeRtcpMuxParameters(), &fake_packet_transport_,
48 nullptr, nullptr)
49 .MoveValue();
50 }
51
52 rtc::PhysicalSocketServer physical_socket_server_;
53 rtc::VirtualSocketServer virtual_socket_server_;
54 rtc::SocketServerScope socket_server_scope_;
55 rtc::FakeNetworkManager fake_network_manager_;
56 rtc::FakePacketTransport fake_packet_transport_;
57 std::unique_ptr<OrtcFactoryInterface> ortc_factory_;
58};
59
60TEST_F(OrtcFactoryTest, CanCreateMultipleRtpTransportControllers) {
61 auto controller_result1 = ortc_factory_->CreateRtpTransportController();
62 EXPECT_TRUE(controller_result1.ok());
63 auto controller_result2 = ortc_factory_->CreateRtpTransportController();
64 EXPECT_TRUE(controller_result1.ok());
65}
66
67// Simple test for the successful cases of CreateRtpTransport.
68TEST_F(OrtcFactoryTest, CreateRtpTransportWithAndWithoutMux) {
69 rtc::FakePacketTransport rtp("rtp");
70 rtc::FakePacketTransport rtcp("rtcp");
71 // With muxed RTCP.
72 RtcpParameters rtcp_parameters;
73 rtcp_parameters.mux = true;
74 auto result = ortc_factory_->CreateRtpTransport(rtcp_parameters, &rtp,
75 nullptr, nullptr);
76 EXPECT_TRUE(result.ok());
77 result.MoveValue().reset();
78 // With non-muxed RTCP.
79 rtcp_parameters.mux = false;
80 result =
81 ortc_factory_->CreateRtpTransport(rtcp_parameters, &rtp, &rtcp, nullptr);
82 EXPECT_TRUE(result.ok());
83}
84
85// If no CNAME is provided, one should be generated and returned by
86// GetRtpParameters.
87TEST_F(OrtcFactoryTest, CreateRtpTransportGeneratesCname) {
88 rtc::FakePacketTransport rtp("rtp");
89 RtcpParameters rtcp_parameters;
90 rtcp_parameters.mux = true;
91 auto result = ortc_factory_->CreateRtpTransport(rtcp_parameters, &rtp,
92 nullptr, nullptr);
93 ASSERT_TRUE(result.ok());
94 EXPECT_FALSE(result.value()->GetRtcpParameters().cname.empty());
95}
96
97// Extension of the above test; multiple transports created by the same factory
98// should use the same generated CNAME.
99TEST_F(OrtcFactoryTest, MultipleRtpTransportsUseSameGeneratedCname) {
100 rtc::FakePacketTransport packet_transport1("1");
101 rtc::FakePacketTransport packet_transport2("2");
102 RtcpParameters rtcp_parameters;
103 rtcp_parameters.mux = true;
104 // Sanity check.
105 ASSERT_TRUE(rtcp_parameters.cname.empty());
106 auto result = ortc_factory_->CreateRtpTransport(
107 rtcp_parameters, &packet_transport1, nullptr, nullptr);
108 ASSERT_TRUE(result.ok());
109 auto rtp_transport1 = result.MoveValue();
110 result = ortc_factory_->CreateRtpTransport(
111 rtcp_parameters, &packet_transport2, nullptr, nullptr);
112 ASSERT_TRUE(result.ok());
113 auto rtp_transport2 = result.MoveValue();
114 RtcpParameters params1 = rtp_transport1->GetRtcpParameters();
115 RtcpParameters params2 = rtp_transport2->GetRtcpParameters();
116 EXPECT_FALSE(params1.cname.empty());
117 EXPECT_EQ(params1.cname, params2.cname);
118}
119
120TEST_F(OrtcFactoryTest, CreateRtpTransportWithNoPacketTransport) {
121 auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(),
122 nullptr, nullptr, nullptr);
123 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
124}
125
126// If the |mux| member of the RtcpParameters is false, both an RTP and RTCP
127// packet transport are needed.
128TEST_F(OrtcFactoryTest, CreateRtpTransportWithMissingRtcpTransport) {
129 rtc::FakePacketTransport rtp("rtp");
130 RtcpParameters rtcp_parameters;
131 rtcp_parameters.mux = false;
132 auto result = ortc_factory_->CreateRtpTransport(rtcp_parameters, &rtp,
133 nullptr, nullptr);
134 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
135}
136
137// If the |mux| member of the RtcpParameters is true, only an RTP packet
138// transport is necessary. So, passing in an RTCP transport is most likely
139// an accident, and thus should be treated as an error.
140TEST_F(OrtcFactoryTest, CreateRtpTransportWithExtraneousRtcpTransport) {
141 rtc::FakePacketTransport rtp("rtp");
142 rtc::FakePacketTransport rtcp("rtcp");
143 RtcpParameters rtcp_parameters;
144 rtcp_parameters.mux = true;
145 auto result =
146 ortc_factory_->CreateRtpTransport(rtcp_parameters, &rtp, &rtcp, nullptr);
147 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
148}
149
150// Basic test that CreateUdpTransport works with AF_INET and AF_INET6.
151TEST_F(OrtcFactoryTest, CreateUdpTransport) {
152 auto result = ortc_factory_->CreateUdpTransport(AF_INET);
153 EXPECT_TRUE(result.ok());
154 result = ortc_factory_->CreateUdpTransport(AF_INET6);
155 EXPECT_TRUE(result.ok());
156}
157
158// Test CreateUdpPort with the |min_port| and |max_port| arguments.
159TEST_F(OrtcFactoryTest, CreateUdpTransportWithPortRange) {
160 auto socket_result1 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
161 ASSERT_TRUE(socket_result1.ok());
162 EXPECT_EQ(2000, socket_result1.value()->GetLocalAddress().port());
163 auto socket_result2 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
164 ASSERT_TRUE(socket_result2.ok());
165 EXPECT_EQ(2001, socket_result2.value()->GetLocalAddress().port());
166 auto socket_result3 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
167 ASSERT_TRUE(socket_result3.ok());
168 EXPECT_EQ(2002, socket_result3.value()->GetLocalAddress().port());
169
170 // All sockets in the range have been exhausted, so the next call should
171 // fail.
172 auto failed_result = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
173 EXPECT_EQ(RTCErrorType::RESOURCE_EXHAUSTED, failed_result.error().type());
174
175 // If one socket is destroyed, that port should be freed up again.
176 socket_result2.MoveValue().reset();
177 auto socket_result4 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
178 ASSERT_TRUE(socket_result4.ok());
179 EXPECT_EQ(2001, socket_result4.value()->GetLocalAddress().port());
180}
181
182// Basic test that CreateUdpTransport works with AF_INET and AF_INET6.
183TEST_F(OrtcFactoryTest, CreateUdpTransportWithInvalidAddressFamily) {
184 auto result = ortc_factory_->CreateUdpTransport(12345);
185 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
186}
187
188TEST_F(OrtcFactoryTest, CreateUdpTransportWithInvalidPortRange) {
189 auto result = ortc_factory_->CreateUdpTransport(AF_INET, 3000, 2000);
190 EXPECT_EQ(RTCErrorType::INVALID_RANGE, result.error().type());
191}
192
193// Just sanity check that each "GetCapabilities" method returns some codecs.
194TEST_F(OrtcFactoryTest, GetSenderAndReceiverCapabilities) {
195 RtpCapabilities audio_send_caps =
196 ortc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_AUDIO);
197 EXPECT_GT(audio_send_caps.codecs.size(), 0u);
198 RtpCapabilities video_send_caps =
199 ortc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_VIDEO);
200 EXPECT_GT(video_send_caps.codecs.size(), 0u);
201 RtpCapabilities audio_receive_caps =
202 ortc_factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_AUDIO);
203 EXPECT_GT(audio_receive_caps.codecs.size(), 0u);
204 RtpCapabilities video_receive_caps =
205 ortc_factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_VIDEO);
206 EXPECT_GT(video_receive_caps.codecs.size(), 0u);
207}
208
209// Calling CreateRtpSender with a null track should fail, since that makes it
210// impossible to know whether to create an audio or video sender. The
211// application should be using the method that takes a cricket::MediaType
212// instead.
213TEST_F(OrtcFactoryTest, CreateSenderWithNullTrack) {
214 auto rtp_transport = CreateRtpTransportWithFakePacketTransport();
215 auto result = ortc_factory_->CreateRtpSender(nullptr, rtp_transport.get());
216 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
217}
218
219// Calling CreateRtpSender or CreateRtpReceiver with MEDIA_TYPE_DATA should
220// fail.
221TEST_F(OrtcFactoryTest, CreateSenderOrReceieverWithInvalidKind) {
222 auto rtp_transport = CreateRtpTransportWithFakePacketTransport();
223 auto sender_result = ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_DATA,
224 rtp_transport.get());
225 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, sender_result.error().type());
226 auto receiver_result = ortc_factory_->CreateRtpReceiver(
227 cricket::MEDIA_TYPE_DATA, rtp_transport.get());
228 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, receiver_result.error().type());
229}
230
231TEST_F(OrtcFactoryTest, CreateSendersOrReceieversWithNullTransport) {
232 auto sender_result =
233 ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_AUDIO, nullptr);
234 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, sender_result.error().type());
235 auto receiver_result =
236 ortc_factory_->CreateRtpReceiver(cricket::MEDIA_TYPE_AUDIO, nullptr);
237 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, receiver_result.error().type());
238}
239
240} // namespace webrtc