blob: 8ebe6271ab9ba5249317c3b9b655592d1a53f1da [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];
Henrik Boström766c80b2022-01-12 17:46:03 +010045 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
jtteh4eeb5372017-04-03 15:06:37 -070046 config.iceServers = @[ server ];
47 config.iceTransportPolicy = RTCIceTransportPolicyRelay;
48 config.bundlePolicy = RTCBundlePolicyMaxBundle;
49 config.rtcpMuxPolicy = RTCRtcpMuxPolicyNegotiate;
50 config.tcpCandidatePolicy = RTCTcpCandidatePolicyDisabled;
51 config.candidateNetworkPolicy = RTCCandidateNetworkPolicyLowCost;
52 const int maxPackets = 60;
Qingsi Wangdea68892018-03-27 10:55:21 -070053 const int timeout = 1500;
54 const int interval = 2000;
jtteh4eeb5372017-04-03 15:06:37 -070055 config.audioJitterBufferMaxPackets = maxPackets;
56 config.audioJitterBufferFastAccelerate = YES;
57 config.iceConnectionReceivingTimeout = timeout;
58 config.iceBackupCandidatePairPingInterval = interval;
59 config.continualGatheringPolicy =
60 RTCContinualGatheringPolicyGatherContinually;
61 config.shouldPruneTurnPorts = YES;
Zhi Huangb57e1692018-06-12 11:41:11 -070062 config.activeResetSrtpParams = YES;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020063 config.cryptoOptions =
64 [[RTC_OBJC_TYPE(RTCCryptoOptions) alloc] initWithSrtpEnableGcmCryptoSuites:YES
65 srtpEnableAes128Sha1_32CryptoCipher:YES
66 srtpEnableEncryptedRtpHeaderExtensions:NO
67 sframeRequireFrameEncryption:NO];
jtteh4eeb5372017-04-03 15:06:37 -070068
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020069 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
70 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
71 optionalConstraints:nil];
72 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
73 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
jtteh4eeb5372017-04-03 15:06:37 -070074
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020075 RTC_OBJC_TYPE(RTCConfiguration) * newConfig;
kthelgasonc0977102017-04-24 00:57:16 -070076 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020077 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
kthelgasonc0977102017-04-24 00:57:16 -070078 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
79 newConfig = peerConnection.configuration;
zstein03adb7c2017-08-09 14:29:42 -070080
zstein8b476172017-09-05 14:43:03 -070081 EXPECT_TRUE([peerConnection setBweMinBitrateBps:[NSNumber numberWithInt:100000]
82 currentBitrateBps:[NSNumber numberWithInt:5000000]
83 maxBitrateBps:[NSNumber numberWithInt:500000000]]);
84 EXPECT_FALSE([peerConnection setBweMinBitrateBps:[NSNumber numberWithInt:2]
85 currentBitrateBps:[NSNumber numberWithInt:1]
86 maxBitrateBps:nil]);
kthelgasonc0977102017-04-24 00:57:16 -070087 }
zstein03adb7c2017-08-09 14:29:42 -070088
jtteh4eeb5372017-04-03 15:06:37 -070089 EXPECT_EQ([config.iceServers count], [newConfig.iceServers count]);
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020090 RTC_OBJC_TYPE(RTCIceServer) *newServer = newConfig.iceServers[0];
91 RTC_OBJC_TYPE(RTCIceServer) *origServer = config.iceServers[0];
jtteh4eeb5372017-04-03 15:06:37 -070092 std::string origUrl = origServer.urlStrings.firstObject.UTF8String;
93 std::string url = newServer.urlStrings.firstObject.UTF8String;
94 EXPECT_EQ(origUrl, url);
95
96 EXPECT_EQ(config.iceTransportPolicy, newConfig.iceTransportPolicy);
97 EXPECT_EQ(config.bundlePolicy, newConfig.bundlePolicy);
98 EXPECT_EQ(config.rtcpMuxPolicy, newConfig.rtcpMuxPolicy);
99 EXPECT_EQ(config.tcpCandidatePolicy, newConfig.tcpCandidatePolicy);
100 EXPECT_EQ(config.candidateNetworkPolicy, newConfig.candidateNetworkPolicy);
101 EXPECT_EQ(config.audioJitterBufferMaxPackets, newConfig.audioJitterBufferMaxPackets);
102 EXPECT_EQ(config.audioJitterBufferFastAccelerate, newConfig.audioJitterBufferFastAccelerate);
103 EXPECT_EQ(config.iceConnectionReceivingTimeout, newConfig.iceConnectionReceivingTimeout);
104 EXPECT_EQ(config.iceBackupCandidatePairPingInterval,
105 newConfig.iceBackupCandidatePairPingInterval);
106 EXPECT_EQ(config.continualGatheringPolicy, newConfig.continualGatheringPolicy);
107 EXPECT_EQ(config.shouldPruneTurnPorts, newConfig.shouldPruneTurnPorts);
Zhi Huangb57e1692018-06-12 11:41:11 -0700108 EXPECT_EQ(config.activeResetSrtpParams, newConfig.activeResetSrtpParams);
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700109 EXPECT_EQ(config.cryptoOptions.srtpEnableGcmCryptoSuites,
110 newConfig.cryptoOptions.srtpEnableGcmCryptoSuites);
111 EXPECT_EQ(config.cryptoOptions.srtpEnableAes128Sha1_32CryptoCipher,
112 newConfig.cryptoOptions.srtpEnableAes128Sha1_32CryptoCipher);
113 EXPECT_EQ(config.cryptoOptions.srtpEnableEncryptedRtpHeaderExtensions,
114 newConfig.cryptoOptions.srtpEnableEncryptedRtpHeaderExtensions);
115 EXPECT_EQ(config.cryptoOptions.sframeRequireFrameEncryption,
116 newConfig.cryptoOptions.sframeRequireFrameEncryption);
jtteh4eeb5372017-04-03 15:06:37 -0700117}
118
Jonas Oreland285f83d2020-02-07 10:30:08 +0100119- (void)testWithDependencies {
120 NSArray *urlStrings = @[ @"stun:stun1.example.net" ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200121 RTC_OBJC_TYPE(RTCIceServer) *server =
122 [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100123
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200124 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Henrik Boström766c80b2022-01-12 17:46:03 +0100125 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Jonas Oreland285f83d2020-02-07 10:30:08 +0100126 config.iceServers = @[ server ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200127 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
128 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
129 optionalConstraints:nil];
130 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
131 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100132
Jonas Oreland285f83d2020-02-07 10:30:08 +0100133 std::unique_ptr<webrtc::PeerConnectionDependencies> pc_dependencies =
134 std::make_unique<webrtc::PeerConnectionDependencies>(nullptr);
135 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200136 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
Jonas Oreland285f83d2020-02-07 10:30:08 +0100137 [factory peerConnectionWithDependencies:config
138 constraints:contraints
139 dependencies:std::move(pc_dependencies)
140 delegate:nil];
Byoungchan Lee32c4ecb2021-10-28 15:49:46 +0900141 ASSERT_NE(peerConnection, nil);
Jonas Oreland285f83d2020-02-07 10:30:08 +0100142 }
143}
144
Byoungchan Lee33728152021-08-04 20:54:45 +0900145- (void)testWithInvalidSDP {
146 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
147 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
148
149 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Henrik Boström766c80b2022-01-12 17:46:03 +0100150 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Byoungchan Lee33728152021-08-04 20:54:45 +0900151 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
152 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
153 optionalConstraints:nil];
154 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
155 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
156
157 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
158 [peerConnection setRemoteDescription:[[RTC_OBJC_TYPE(RTCSessionDescription) alloc]
159 initWithType:RTCSdpTypeOffer
160 sdp:@"invalid"]
161 completionHandler:^(NSError *error) {
162 ASSERT_NE(error, nil);
163 if (error != nil) {
164 dispatch_semaphore_signal(negotiatedSem);
165 }
166 }];
167
168 NSTimeInterval timeout = 5;
169 ASSERT_EQ(
170 0,
171 dispatch_semaphore_wait(negotiatedSem,
172 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))));
173 [peerConnection close];
174}
Byoungchan Lee8c487572021-08-04 20:55:25 +0900175
176- (void)testWithInvalidIceCandidate {
177 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
178 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
179
180 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Henrik Boström766c80b2022-01-12 17:46:03 +0100181 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Byoungchan Lee8c487572021-08-04 20:55:25 +0900182 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
183 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
184 optionalConstraints:nil];
185 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
186 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
187
188 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
189 [peerConnection addIceCandidate:[[RTC_OBJC_TYPE(RTCIceCandidate) alloc] initWithSdp:@"invalid"
190 sdpMLineIndex:-1
191 sdpMid:nil]
192 completionHandler:^(NSError *error) {
193 ASSERT_NE(error, nil);
194 if (error != nil) {
195 dispatch_semaphore_signal(negotiatedSem);
196 }
197 }];
198
199 NSTimeInterval timeout = 5;
200 ASSERT_EQ(
201 0,
202 dispatch_semaphore_wait(negotiatedSem,
203 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))));
204 [peerConnection close];
205}
206
jtteh4eeb5372017-04-03 15:06:37 -0700207@end
208
209TEST(RTCPeerConnectionTest, ConfigurationGetterTest) {
210 @autoreleasepool {
211 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
212 [test testConfigurationGetter];
213 }
214}
Jonas Oreland285f83d2020-02-07 10:30:08 +0100215
216TEST(RTCPeerConnectionTest, TestWithDependencies) {
217 @autoreleasepool {
218 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
219 [test testWithDependencies];
220 }
221}
Byoungchan Lee33728152021-08-04 20:54:45 +0900222
223TEST(RTCPeerConnectionTest, TestWithInvalidSDP) {
224 @autoreleasepool {
225 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
226 [test testWithInvalidSDP];
227 }
228}
Byoungchan Lee8c487572021-08-04 20:55:25 +0900229
230TEST(RTCPeerConnectionTest, TestWithInvalidIceCandidate) {
231 @autoreleasepool {
232 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
233 [test testWithInvalidIceCandidate];
234 }
235}