blob: 19b3c8e69fdbed7f311046b3b4e22c73b6cbc0e2 [file] [log] [blame]
jtteh4eeb5372017-04-03 15:06:37 -07001/*
2 * Copyright 2015 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#import <Foundation/Foundation.h>
12
Jonas Oreland285f83d2020-02-07 10:30:08 +010013#include <memory>
jtteh4eeb5372017-04-03 15:06:37 -070014#include <vector>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/gunit.h"
jtteh4eeb5372017-04-03 15:06:37 -070017
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020018#import "api/peerconnection/RTCConfiguration+Private.h"
19#import "api/peerconnection/RTCConfiguration.h"
Benjamin Wright8c27cca2018-10-25 10:16:44 -070020#import "api/peerconnection/RTCCryptoOptions.h"
Byoungchan Lee8c487572021-08-04 20:55:25 +090021#import "api/peerconnection/RTCIceCandidate.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020022#import "api/peerconnection/RTCIceServer.h"
23#import "api/peerconnection/RTCMediaConstraints.h"
24#import "api/peerconnection/RTCPeerConnection.h"
Jonas Oreland285f83d2020-02-07 10:30:08 +010025#import "api/peerconnection/RTCPeerConnectionFactory+Native.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020026#import "api/peerconnection/RTCPeerConnectionFactory.h"
Byoungchan Lee33728152021-08-04 20:54:45 +090027#import "api/peerconnection/RTCSessionDescription.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020028#import "helpers/NSString+StdString.h"
jtteh4eeb5372017-04-03 15:06:37 -070029
30@interface RTCPeerConnectionTest : NSObject
31- (void)testConfigurationGetter;
Jonas Oreland285f83d2020-02-07 10:30:08 +010032- (void)testWithDependencies;
Byoungchan Lee33728152021-08-04 20:54:45 +090033- (void)testWithInvalidSDP;
Byoungchan Lee8c487572021-08-04 20:55:25 +090034- (void)testWithInvalidIceCandidate;
jtteh4eeb5372017-04-03 15:06:37 -070035@end
36
37@implementation RTCPeerConnectionTest
38
39- (void)testConfigurationGetter {
40 NSArray *urlStrings = @[ @"stun:stun1.example.net" ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020041 RTC_OBJC_TYPE(RTCIceServer) *server =
42 [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings];
jtteh4eeb5372017-04-03 15:06:37 -070043
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020044 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
jtteh4eeb5372017-04-03 15:06:37 -070045 config.iceServers = @[ server ];
46 config.iceTransportPolicy = RTCIceTransportPolicyRelay;
47 config.bundlePolicy = RTCBundlePolicyMaxBundle;
48 config.rtcpMuxPolicy = RTCRtcpMuxPolicyNegotiate;
49 config.tcpCandidatePolicy = RTCTcpCandidatePolicyDisabled;
50 config.candidateNetworkPolicy = RTCCandidateNetworkPolicyLowCost;
51 const int maxPackets = 60;
Qingsi Wangdea68892018-03-27 10:55:21 -070052 const int timeout = 1500;
53 const int interval = 2000;
jtteh4eeb5372017-04-03 15:06:37 -070054 config.audioJitterBufferMaxPackets = maxPackets;
55 config.audioJitterBufferFastAccelerate = YES;
56 config.iceConnectionReceivingTimeout = timeout;
57 config.iceBackupCandidatePairPingInterval = interval;
58 config.continualGatheringPolicy =
59 RTCContinualGatheringPolicyGatherContinually;
60 config.shouldPruneTurnPorts = YES;
Zhi Huangb57e1692018-06-12 11:41:11 -070061 config.activeResetSrtpParams = YES;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020062 config.cryptoOptions =
63 [[RTC_OBJC_TYPE(RTCCryptoOptions) alloc] initWithSrtpEnableGcmCryptoSuites:YES
64 srtpEnableAes128Sha1_32CryptoCipher:YES
65 srtpEnableEncryptedRtpHeaderExtensions:NO
66 sframeRequireFrameEncryption:NO];
jtteh4eeb5372017-04-03 15:06:37 -070067
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020068 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
69 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
70 optionalConstraints:nil];
71 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
72 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
jtteh4eeb5372017-04-03 15:06:37 -070073
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020074 RTC_OBJC_TYPE(RTCConfiguration) * newConfig;
kthelgasonc0977102017-04-24 00:57:16 -070075 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020076 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
kthelgasonc0977102017-04-24 00:57:16 -070077 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
78 newConfig = peerConnection.configuration;
zstein03adb7c2017-08-09 14:29:42 -070079
zstein8b476172017-09-05 14:43:03 -070080 EXPECT_TRUE([peerConnection setBweMinBitrateBps:[NSNumber numberWithInt:100000]
81 currentBitrateBps:[NSNumber numberWithInt:5000000]
82 maxBitrateBps:[NSNumber numberWithInt:500000000]]);
83 EXPECT_FALSE([peerConnection setBweMinBitrateBps:[NSNumber numberWithInt:2]
84 currentBitrateBps:[NSNumber numberWithInt:1]
85 maxBitrateBps:nil]);
kthelgasonc0977102017-04-24 00:57:16 -070086 }
zstein03adb7c2017-08-09 14:29:42 -070087
jtteh4eeb5372017-04-03 15:06:37 -070088 EXPECT_EQ([config.iceServers count], [newConfig.iceServers count]);
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020089 RTC_OBJC_TYPE(RTCIceServer) *newServer = newConfig.iceServers[0];
90 RTC_OBJC_TYPE(RTCIceServer) *origServer = config.iceServers[0];
jtteh4eeb5372017-04-03 15:06:37 -070091 std::string origUrl = origServer.urlStrings.firstObject.UTF8String;
92 std::string url = newServer.urlStrings.firstObject.UTF8String;
93 EXPECT_EQ(origUrl, url);
94
95 EXPECT_EQ(config.iceTransportPolicy, newConfig.iceTransportPolicy);
96 EXPECT_EQ(config.bundlePolicy, newConfig.bundlePolicy);
97 EXPECT_EQ(config.rtcpMuxPolicy, newConfig.rtcpMuxPolicy);
98 EXPECT_EQ(config.tcpCandidatePolicy, newConfig.tcpCandidatePolicy);
99 EXPECT_EQ(config.candidateNetworkPolicy, newConfig.candidateNetworkPolicy);
100 EXPECT_EQ(config.audioJitterBufferMaxPackets, newConfig.audioJitterBufferMaxPackets);
101 EXPECT_EQ(config.audioJitterBufferFastAccelerate, newConfig.audioJitterBufferFastAccelerate);
102 EXPECT_EQ(config.iceConnectionReceivingTimeout, newConfig.iceConnectionReceivingTimeout);
103 EXPECT_EQ(config.iceBackupCandidatePairPingInterval,
104 newConfig.iceBackupCandidatePairPingInterval);
105 EXPECT_EQ(config.continualGatheringPolicy, newConfig.continualGatheringPolicy);
106 EXPECT_EQ(config.shouldPruneTurnPorts, newConfig.shouldPruneTurnPorts);
Zhi Huangb57e1692018-06-12 11:41:11 -0700107 EXPECT_EQ(config.activeResetSrtpParams, newConfig.activeResetSrtpParams);
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700108 EXPECT_EQ(config.cryptoOptions.srtpEnableGcmCryptoSuites,
109 newConfig.cryptoOptions.srtpEnableGcmCryptoSuites);
110 EXPECT_EQ(config.cryptoOptions.srtpEnableAes128Sha1_32CryptoCipher,
111 newConfig.cryptoOptions.srtpEnableAes128Sha1_32CryptoCipher);
112 EXPECT_EQ(config.cryptoOptions.srtpEnableEncryptedRtpHeaderExtensions,
113 newConfig.cryptoOptions.srtpEnableEncryptedRtpHeaderExtensions);
114 EXPECT_EQ(config.cryptoOptions.sframeRequireFrameEncryption,
115 newConfig.cryptoOptions.sframeRequireFrameEncryption);
jtteh4eeb5372017-04-03 15:06:37 -0700116}
117
Jonas Oreland285f83d2020-02-07 10:30:08 +0100118- (void)testWithDependencies {
119 NSArray *urlStrings = @[ @"stun:stun1.example.net" ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200120 RTC_OBJC_TYPE(RTCIceServer) *server =
121 [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100122
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200123 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100124 config.iceServers = @[ server ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200125 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
126 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
127 optionalConstraints:nil];
128 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
129 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100130
Jonas Oreland285f83d2020-02-07 10:30:08 +0100131 std::unique_ptr<webrtc::PeerConnectionDependencies> pc_dependencies =
132 std::make_unique<webrtc::PeerConnectionDependencies>(nullptr);
133 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200134 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
Jonas Oreland285f83d2020-02-07 10:30:08 +0100135 [factory peerConnectionWithDependencies:config
136 constraints:contraints
137 dependencies:std::move(pc_dependencies)
138 delegate:nil];
Byoungchan Lee32c4ecb2021-10-28 15:49:46 +0900139 ASSERT_NE(peerConnection, nil);
Jonas Oreland285f83d2020-02-07 10:30:08 +0100140 }
141}
142
Byoungchan Lee33728152021-08-04 20:54:45 +0900143- (void)testWithInvalidSDP {
144 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
145 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
146
147 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
148 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
149 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
150 optionalConstraints:nil];
151 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
152 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
153
154 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
155 [peerConnection setRemoteDescription:[[RTC_OBJC_TYPE(RTCSessionDescription) alloc]
156 initWithType:RTCSdpTypeOffer
157 sdp:@"invalid"]
158 completionHandler:^(NSError *error) {
159 ASSERT_NE(error, nil);
160 if (error != nil) {
161 dispatch_semaphore_signal(negotiatedSem);
162 }
163 }];
164
165 NSTimeInterval timeout = 5;
166 ASSERT_EQ(
167 0,
168 dispatch_semaphore_wait(negotiatedSem,
169 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))));
170 [peerConnection close];
171}
Byoungchan Lee8c487572021-08-04 20:55:25 +0900172
173- (void)testWithInvalidIceCandidate {
174 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
175 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
176
177 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
178 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
179 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
180 optionalConstraints:nil];
181 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
182 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
183
184 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
185 [peerConnection addIceCandidate:[[RTC_OBJC_TYPE(RTCIceCandidate) alloc] initWithSdp:@"invalid"
186 sdpMLineIndex:-1
187 sdpMid:nil]
188 completionHandler:^(NSError *error) {
189 ASSERT_NE(error, nil);
190 if (error != nil) {
191 dispatch_semaphore_signal(negotiatedSem);
192 }
193 }];
194
195 NSTimeInterval timeout = 5;
196 ASSERT_EQ(
197 0,
198 dispatch_semaphore_wait(negotiatedSem,
199 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))));
200 [peerConnection close];
201}
202
jtteh4eeb5372017-04-03 15:06:37 -0700203@end
204
205TEST(RTCPeerConnectionTest, ConfigurationGetterTest) {
206 @autoreleasepool {
207 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
208 [test testConfigurationGetter];
209 }
210}
Jonas Oreland285f83d2020-02-07 10:30:08 +0100211
212TEST(RTCPeerConnectionTest, TestWithDependencies) {
213 @autoreleasepool {
214 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
215 [test testWithDependencies];
216 }
217}
Byoungchan Lee33728152021-08-04 20:54:45 +0900218
219TEST(RTCPeerConnectionTest, TestWithInvalidSDP) {
220 @autoreleasepool {
221 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
222 [test testWithInvalidSDP];
223 }
224}
Byoungchan Lee8c487572021-08-04 20:55:25 +0900225
226TEST(RTCPeerConnectionTest, TestWithInvalidIceCandidate) {
227 @autoreleasepool {
228 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
229 [test testWithInvalidIceCandidate];
230 }
231}