blob: 1d0ae2679e571f666c498c92178f05c0f1a50f53 [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
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200131 RTC_OBJC_TYPE(RTCConfiguration) * newConfig;
Jonas Oreland285f83d2020-02-07 10:30:08 +0100132 std::unique_ptr<webrtc::PeerConnectionDependencies> pc_dependencies =
133 std::make_unique<webrtc::PeerConnectionDependencies>(nullptr);
134 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200135 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
Jonas Oreland285f83d2020-02-07 10:30:08 +0100136 [factory peerConnectionWithDependencies:config
137 constraints:contraints
138 dependencies:std::move(pc_dependencies)
139 delegate:nil];
140 newConfig = peerConnection.configuration;
141 }
142}
143
Byoungchan Lee33728152021-08-04 20:54:45 +0900144- (void)testWithInvalidSDP {
145 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
146 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
147
148 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
149 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
150 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
151 optionalConstraints:nil];
152 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
153 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
154
155 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
156 [peerConnection setRemoteDescription:[[RTC_OBJC_TYPE(RTCSessionDescription) alloc]
157 initWithType:RTCSdpTypeOffer
158 sdp:@"invalid"]
159 completionHandler:^(NSError *error) {
160 ASSERT_NE(error, nil);
161 if (error != nil) {
162 dispatch_semaphore_signal(negotiatedSem);
163 }
164 }];
165
166 NSTimeInterval timeout = 5;
167 ASSERT_EQ(
168 0,
169 dispatch_semaphore_wait(negotiatedSem,
170 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))));
171 [peerConnection close];
172}
Byoungchan Lee8c487572021-08-04 20:55:25 +0900173
174- (void)testWithInvalidIceCandidate {
175 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
176 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
177
178 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
179 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
180 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
181 optionalConstraints:nil];
182 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
183 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
184
185 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
186 [peerConnection addIceCandidate:[[RTC_OBJC_TYPE(RTCIceCandidate) alloc] initWithSdp:@"invalid"
187 sdpMLineIndex:-1
188 sdpMid:nil]
189 completionHandler:^(NSError *error) {
190 ASSERT_NE(error, nil);
191 if (error != nil) {
192 dispatch_semaphore_signal(negotiatedSem);
193 }
194 }];
195
196 NSTimeInterval timeout = 5;
197 ASSERT_EQ(
198 0,
199 dispatch_semaphore_wait(negotiatedSem,
200 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))));
201 [peerConnection close];
202}
203
jtteh4eeb5372017-04-03 15:06:37 -0700204@end
205
206TEST(RTCPeerConnectionTest, ConfigurationGetterTest) {
207 @autoreleasepool {
208 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
209 [test testConfigurationGetter];
210 }
211}
Jonas Oreland285f83d2020-02-07 10:30:08 +0100212
213TEST(RTCPeerConnectionTest, TestWithDependencies) {
214 @autoreleasepool {
215 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
216 [test testWithDependencies];
217 }
218}
Byoungchan Lee33728152021-08-04 20:54:45 +0900219
220TEST(RTCPeerConnectionTest, TestWithInvalidSDP) {
221 @autoreleasepool {
222 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
223 [test testWithInvalidSDP];
224 }
225}
Byoungchan Lee8c487572021-08-04 20:55:25 +0900226
227TEST(RTCPeerConnectionTest, TestWithInvalidIceCandidate) {
228 @autoreleasepool {
229 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
230 [test testWithInvalidIceCandidate];
231 }
232}