blob: 45c9ce59ada52cf6bb4341796584733e438bca96 [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>
Byoungchan Leec8a6fb22022-05-13 19:59:49 +090012#import <XCTest/XCTest.h>
jtteh4eeb5372017-04-03 15:06:37 -070013
Jonas Oreland285f83d2020-02-07 10:30:08 +010014#include <memory>
jtteh4eeb5372017-04-03 15:06:37 -070015#include <vector>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/gunit.h"
jtteh4eeb5372017-04-03 15:06:37 -070018
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020019#import "api/peerconnection/RTCConfiguration+Private.h"
20#import "api/peerconnection/RTCConfiguration.h"
Benjamin Wright8c27cca2018-10-25 10:16:44 -070021#import "api/peerconnection/RTCCryptoOptions.h"
Byoungchan Lee8c487572021-08-04 20:55:25 +090022#import "api/peerconnection/RTCIceCandidate.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020023#import "api/peerconnection/RTCIceServer.h"
24#import "api/peerconnection/RTCMediaConstraints.h"
25#import "api/peerconnection/RTCPeerConnection.h"
Jonas Oreland285f83d2020-02-07 10:30:08 +010026#import "api/peerconnection/RTCPeerConnectionFactory+Native.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020027#import "api/peerconnection/RTCPeerConnectionFactory.h"
Byoungchan Lee33728152021-08-04 20:54:45 +090028#import "api/peerconnection/RTCSessionDescription.h"
Byoungchan Lee181ea6e2022-05-13 19:02:07 +090029#import "helpers/NSString+RTCStdString.h"
jtteh4eeb5372017-04-03 15:06:37 -070030
Byoungchan Leec8a6fb22022-05-13 19:59:49 +090031@interface RTCPeerConnectionTests : XCTestCase
jtteh4eeb5372017-04-03 15:06:37 -070032@end
33
Byoungchan Leec8a6fb22022-05-13 19:59:49 +090034@implementation RTCPeerConnectionTests
jtteh4eeb5372017-04-03 15:06:37 -070035
36- (void)testConfigurationGetter {
37 NSArray *urlStrings = @[ @"stun:stun1.example.net" ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020038 RTC_OBJC_TYPE(RTCIceServer) *server =
39 [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings];
jtteh4eeb5372017-04-03 15:06:37 -070040
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020041 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Henrik Boström766c80b2022-01-12 17:46:03 +010042 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
jtteh4eeb5372017-04-03 15:06:37 -070043 config.iceServers = @[ server ];
44 config.iceTransportPolicy = RTCIceTransportPolicyRelay;
45 config.bundlePolicy = RTCBundlePolicyMaxBundle;
46 config.rtcpMuxPolicy = RTCRtcpMuxPolicyNegotiate;
47 config.tcpCandidatePolicy = RTCTcpCandidatePolicyDisabled;
48 config.candidateNetworkPolicy = RTCCandidateNetworkPolicyLowCost;
49 const int maxPackets = 60;
Qingsi Wangdea68892018-03-27 10:55:21 -070050 const int timeout = 1500;
51 const int interval = 2000;
jtteh4eeb5372017-04-03 15:06:37 -070052 config.audioJitterBufferMaxPackets = maxPackets;
53 config.audioJitterBufferFastAccelerate = YES;
54 config.iceConnectionReceivingTimeout = timeout;
55 config.iceBackupCandidatePairPingInterval = interval;
56 config.continualGatheringPolicy =
57 RTCContinualGatheringPolicyGatherContinually;
58 config.shouldPruneTurnPorts = YES;
Zhi Huangb57e1692018-06-12 11:41:11 -070059 config.activeResetSrtpParams = YES;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020060 config.cryptoOptions =
61 [[RTC_OBJC_TYPE(RTCCryptoOptions) alloc] initWithSrtpEnableGcmCryptoSuites:YES
62 srtpEnableAes128Sha1_32CryptoCipher:YES
63 srtpEnableEncryptedRtpHeaderExtensions:NO
64 sframeRequireFrameEncryption:NO];
jtteh4eeb5372017-04-03 15:06:37 -070065
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020066 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
67 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
68 optionalConstraints:nil];
69 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
70 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
jtteh4eeb5372017-04-03 15:06:37 -070071
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020072 RTC_OBJC_TYPE(RTCConfiguration) * newConfig;
kthelgasonc0977102017-04-24 00:57:16 -070073 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020074 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
kthelgasonc0977102017-04-24 00:57:16 -070075 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
76 newConfig = peerConnection.configuration;
zstein03adb7c2017-08-09 14:29:42 -070077
zstein8b476172017-09-05 14:43:03 -070078 EXPECT_TRUE([peerConnection setBweMinBitrateBps:[NSNumber numberWithInt:100000]
79 currentBitrateBps:[NSNumber numberWithInt:5000000]
80 maxBitrateBps:[NSNumber numberWithInt:500000000]]);
81 EXPECT_FALSE([peerConnection setBweMinBitrateBps:[NSNumber numberWithInt:2]
82 currentBitrateBps:[NSNumber numberWithInt:1]
83 maxBitrateBps:nil]);
kthelgasonc0977102017-04-24 00:57:16 -070084 }
zstein03adb7c2017-08-09 14:29:42 -070085
jtteh4eeb5372017-04-03 15:06:37 -070086 EXPECT_EQ([config.iceServers count], [newConfig.iceServers count]);
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020087 RTC_OBJC_TYPE(RTCIceServer) *newServer = newConfig.iceServers[0];
88 RTC_OBJC_TYPE(RTCIceServer) *origServer = config.iceServers[0];
jtteh4eeb5372017-04-03 15:06:37 -070089 std::string origUrl = origServer.urlStrings.firstObject.UTF8String;
90 std::string url = newServer.urlStrings.firstObject.UTF8String;
91 EXPECT_EQ(origUrl, url);
92
93 EXPECT_EQ(config.iceTransportPolicy, newConfig.iceTransportPolicy);
94 EXPECT_EQ(config.bundlePolicy, newConfig.bundlePolicy);
95 EXPECT_EQ(config.rtcpMuxPolicy, newConfig.rtcpMuxPolicy);
96 EXPECT_EQ(config.tcpCandidatePolicy, newConfig.tcpCandidatePolicy);
97 EXPECT_EQ(config.candidateNetworkPolicy, newConfig.candidateNetworkPolicy);
98 EXPECT_EQ(config.audioJitterBufferMaxPackets, newConfig.audioJitterBufferMaxPackets);
99 EXPECT_EQ(config.audioJitterBufferFastAccelerate, newConfig.audioJitterBufferFastAccelerate);
100 EXPECT_EQ(config.iceConnectionReceivingTimeout, newConfig.iceConnectionReceivingTimeout);
101 EXPECT_EQ(config.iceBackupCandidatePairPingInterval,
102 newConfig.iceBackupCandidatePairPingInterval);
103 EXPECT_EQ(config.continualGatheringPolicy, newConfig.continualGatheringPolicy);
104 EXPECT_EQ(config.shouldPruneTurnPorts, newConfig.shouldPruneTurnPorts);
Zhi Huangb57e1692018-06-12 11:41:11 -0700105 EXPECT_EQ(config.activeResetSrtpParams, newConfig.activeResetSrtpParams);
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700106 EXPECT_EQ(config.cryptoOptions.srtpEnableGcmCryptoSuites,
107 newConfig.cryptoOptions.srtpEnableGcmCryptoSuites);
108 EXPECT_EQ(config.cryptoOptions.srtpEnableAes128Sha1_32CryptoCipher,
109 newConfig.cryptoOptions.srtpEnableAes128Sha1_32CryptoCipher);
110 EXPECT_EQ(config.cryptoOptions.srtpEnableEncryptedRtpHeaderExtensions,
111 newConfig.cryptoOptions.srtpEnableEncryptedRtpHeaderExtensions);
112 EXPECT_EQ(config.cryptoOptions.sframeRequireFrameEncryption,
113 newConfig.cryptoOptions.sframeRequireFrameEncryption);
jtteh4eeb5372017-04-03 15:06:37 -0700114}
115
Jonas Oreland285f83d2020-02-07 10:30:08 +0100116- (void)testWithDependencies {
117 NSArray *urlStrings = @[ @"stun:stun1.example.net" ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200118 RTC_OBJC_TYPE(RTCIceServer) *server =
119 [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100120
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200121 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Henrik Boström766c80b2022-01-12 17:46:03 +0100122 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Jonas Oreland285f83d2020-02-07 10:30:08 +0100123 config.iceServers = @[ server ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200124 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
125 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
126 optionalConstraints:nil];
127 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
128 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100129
Jonas Oreland285f83d2020-02-07 10:30:08 +0100130 std::unique_ptr<webrtc::PeerConnectionDependencies> pc_dependencies =
131 std::make_unique<webrtc::PeerConnectionDependencies>(nullptr);
132 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200133 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
Jonas Oreland285f83d2020-02-07 10:30:08 +0100134 [factory peerConnectionWithDependencies:config
135 constraints:contraints
136 dependencies:std::move(pc_dependencies)
137 delegate:nil];
Byoungchan Lee32c4ecb2021-10-28 15:49:46 +0900138 ASSERT_NE(peerConnection, nil);
Jonas Oreland285f83d2020-02-07 10:30:08 +0100139 }
140}
141
Byoungchan Lee33728152021-08-04 20:54:45 +0900142- (void)testWithInvalidSDP {
143 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
144 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
145
146 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Henrik Boström766c80b2022-01-12 17:46:03 +0100147 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Byoungchan Lee33728152021-08-04 20:54:45 +0900148 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];
Henrik Boström766c80b2022-01-12 17:46:03 +0100178 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Byoungchan Lee8c487572021-08-04 20:55:25 +0900179 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