blob: 3452b964d9aa568a3678372f7cadc2c236c14ddf [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"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020021#import "api/peerconnection/RTCIceServer.h"
22#import "api/peerconnection/RTCMediaConstraints.h"
23#import "api/peerconnection/RTCPeerConnection.h"
Jonas Oreland285f83d2020-02-07 10:30:08 +010024#import "api/peerconnection/RTCPeerConnectionFactory+Native.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020025#import "api/peerconnection/RTCPeerConnectionFactory.h"
Byoungchan Lee33728152021-08-04 20:54:45 +090026#import "api/peerconnection/RTCSessionDescription.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020027#import "helpers/NSString+StdString.h"
jtteh4eeb5372017-04-03 15:06:37 -070028
29@interface RTCPeerConnectionTest : NSObject
30- (void)testConfigurationGetter;
Jonas Oreland285f83d2020-02-07 10:30:08 +010031- (void)testWithDependencies;
Byoungchan Lee33728152021-08-04 20:54:45 +090032- (void)testWithInvalidSDP;
jtteh4eeb5372017-04-03 15:06:37 -070033@end
34
35@implementation RTCPeerConnectionTest
36
37- (void)testConfigurationGetter {
38 NSArray *urlStrings = @[ @"stun:stun1.example.net" ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020039 RTC_OBJC_TYPE(RTCIceServer) *server =
40 [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings];
jtteh4eeb5372017-04-03 15:06:37 -070041
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020042 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
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];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100122 config.iceServers = @[ server ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200123 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
124 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
125 optionalConstraints:nil];
126 RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
127 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100128
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200129 RTC_OBJC_TYPE(RTCConfiguration) * newConfig;
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];
138 newConfig = peerConnection.configuration;
139 }
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];
147 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
148 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
149 optionalConstraints:nil];
150 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
151 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
152
153 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
154 [peerConnection setRemoteDescription:[[RTC_OBJC_TYPE(RTCSessionDescription) alloc]
155 initWithType:RTCSdpTypeOffer
156 sdp:@"invalid"]
157 completionHandler:^(NSError *error) {
158 ASSERT_NE(error, nil);
159 if (error != nil) {
160 dispatch_semaphore_signal(negotiatedSem);
161 }
162 }];
163
164 NSTimeInterval timeout = 5;
165 ASSERT_EQ(
166 0,
167 dispatch_semaphore_wait(negotiatedSem,
168 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))));
169 [peerConnection close];
170}
jtteh4eeb5372017-04-03 15:06:37 -0700171@end
172
173TEST(RTCPeerConnectionTest, ConfigurationGetterTest) {
174 @autoreleasepool {
175 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
176 [test testConfigurationGetter];
177 }
178}
Jonas Oreland285f83d2020-02-07 10:30:08 +0100179
180TEST(RTCPeerConnectionTest, TestWithDependencies) {
181 @autoreleasepool {
182 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
183 [test testWithDependencies];
184 }
185}
Byoungchan Lee33728152021-08-04 20:54:45 +0900186
187TEST(RTCPeerConnectionTest, TestWithInvalidSDP) {
188 @autoreleasepool {
189 RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init];
190 [test testWithInvalidSDP];
191 }
192}