jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Oreland | 285f83d | 2020-02-07 10:30:08 +0100 | [diff] [blame] | 13 | #include <memory> |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/gunit.h" |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 17 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 18 | #import "api/peerconnection/RTCConfiguration+Private.h" |
| 19 | #import "api/peerconnection/RTCConfiguration.h" |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 20 | #import "api/peerconnection/RTCCryptoOptions.h" |
Byoungchan Lee | 8c48757 | 2021-08-04 20:55:25 +0900 | [diff] [blame^] | 21 | #import "api/peerconnection/RTCIceCandidate.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 22 | #import "api/peerconnection/RTCIceServer.h" |
| 23 | #import "api/peerconnection/RTCMediaConstraints.h" |
| 24 | #import "api/peerconnection/RTCPeerConnection.h" |
Jonas Oreland | 285f83d | 2020-02-07 10:30:08 +0100 | [diff] [blame] | 25 | #import "api/peerconnection/RTCPeerConnectionFactory+Native.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 26 | #import "api/peerconnection/RTCPeerConnectionFactory.h" |
Byoungchan Lee | 3372815 | 2021-08-04 20:54:45 +0900 | [diff] [blame] | 27 | #import "api/peerconnection/RTCSessionDescription.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 28 | #import "helpers/NSString+StdString.h" |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 29 | |
| 30 | @interface RTCPeerConnectionTest : NSObject |
| 31 | - (void)testConfigurationGetter; |
Jonas Oreland | 285f83d | 2020-02-07 10:30:08 +0100 | [diff] [blame] | 32 | - (void)testWithDependencies; |
Byoungchan Lee | 3372815 | 2021-08-04 20:54:45 +0900 | [diff] [blame] | 33 | - (void)testWithInvalidSDP; |
Byoungchan Lee | 8c48757 | 2021-08-04 20:55:25 +0900 | [diff] [blame^] | 34 | - (void)testWithInvalidIceCandidate; |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 35 | @end |
| 36 | |
| 37 | @implementation RTCPeerConnectionTest |
| 38 | |
| 39 | - (void)testConfigurationGetter { |
| 40 | NSArray *urlStrings = @[ @"stun:stun1.example.net" ]; |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 41 | RTC_OBJC_TYPE(RTCIceServer) *server = |
| 42 | [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings]; |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 43 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 44 | RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init]; |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 45 | 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 Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 52 | const int timeout = 1500; |
| 53 | const int interval = 2000; |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 54 | 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 Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 61 | config.activeResetSrtpParams = YES; |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 62 | config.cryptoOptions = |
| 63 | [[RTC_OBJC_TYPE(RTCCryptoOptions) alloc] initWithSrtpEnableGcmCryptoSuites:YES |
| 64 | srtpEnableAes128Sha1_32CryptoCipher:YES |
| 65 | srtpEnableEncryptedRtpHeaderExtensions:NO |
| 66 | sframeRequireFrameEncryption:NO]; |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 67 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 68 | 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]; |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 73 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 74 | RTC_OBJC_TYPE(RTCConfiguration) * newConfig; |
kthelgason | c097710 | 2017-04-24 00:57:16 -0700 | [diff] [blame] | 75 | @autoreleasepool { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 76 | RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection = |
kthelgason | c097710 | 2017-04-24 00:57:16 -0700 | [diff] [blame] | 77 | [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil]; |
| 78 | newConfig = peerConnection.configuration; |
zstein | 03adb7c | 2017-08-09 14:29:42 -0700 | [diff] [blame] | 79 | |
zstein | 8b47617 | 2017-09-05 14:43:03 -0700 | [diff] [blame] | 80 | 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]); |
kthelgason | c097710 | 2017-04-24 00:57:16 -0700 | [diff] [blame] | 86 | } |
zstein | 03adb7c | 2017-08-09 14:29:42 -0700 | [diff] [blame] | 87 | |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 88 | EXPECT_EQ([config.iceServers count], [newConfig.iceServers count]); |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 89 | RTC_OBJC_TYPE(RTCIceServer) *newServer = newConfig.iceServers[0]; |
| 90 | RTC_OBJC_TYPE(RTCIceServer) *origServer = config.iceServers[0]; |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 91 | 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 Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 107 | EXPECT_EQ(config.activeResetSrtpParams, newConfig.activeResetSrtpParams); |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 108 | 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); |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jonas Oreland | 285f83d | 2020-02-07 10:30:08 +0100 | [diff] [blame] | 118 | - (void)testWithDependencies { |
| 119 | NSArray *urlStrings = @[ @"stun:stun1.example.net" ]; |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 120 | RTC_OBJC_TYPE(RTCIceServer) *server = |
| 121 | [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings]; |
Jonas Oreland | 285f83d | 2020-02-07 10:30:08 +0100 | [diff] [blame] | 122 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 123 | RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init]; |
Jonas Oreland | 285f83d | 2020-02-07 10:30:08 +0100 | [diff] [blame] | 124 | config.iceServers = @[ server ]; |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 125 | 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 Oreland | 285f83d | 2020-02-07 10:30:08 +0100 | [diff] [blame] | 130 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 131 | RTC_OBJC_TYPE(RTCConfiguration) * newConfig; |
Jonas Oreland | 285f83d | 2020-02-07 10:30:08 +0100 | [diff] [blame] | 132 | std::unique_ptr<webrtc::PeerConnectionDependencies> pc_dependencies = |
| 133 | std::make_unique<webrtc::PeerConnectionDependencies>(nullptr); |
| 134 | @autoreleasepool { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 135 | RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection = |
Jonas Oreland | 285f83d | 2020-02-07 10:30:08 +0100 | [diff] [blame] | 136 | [factory peerConnectionWithDependencies:config |
| 137 | constraints:contraints |
| 138 | dependencies:std::move(pc_dependencies) |
| 139 | delegate:nil]; |
| 140 | newConfig = peerConnection.configuration; |
| 141 | } |
| 142 | } |
| 143 | |
Byoungchan Lee | 3372815 | 2021-08-04 20:54:45 +0900 | [diff] [blame] | 144 | - (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 Lee | 8c48757 | 2021-08-04 20:55:25 +0900 | [diff] [blame^] | 173 | |
| 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 | |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 204 | @end |
| 205 | |
| 206 | TEST(RTCPeerConnectionTest, ConfigurationGetterTest) { |
| 207 | @autoreleasepool { |
| 208 | RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init]; |
| 209 | [test testConfigurationGetter]; |
| 210 | } |
| 211 | } |
Jonas Oreland | 285f83d | 2020-02-07 10:30:08 +0100 | [diff] [blame] | 212 | |
| 213 | TEST(RTCPeerConnectionTest, TestWithDependencies) { |
| 214 | @autoreleasepool { |
| 215 | RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init]; |
| 216 | [test testWithDependencies]; |
| 217 | } |
| 218 | } |
Byoungchan Lee | 3372815 | 2021-08-04 20:54:45 +0900 | [diff] [blame] | 219 | |
| 220 | TEST(RTCPeerConnectionTest, TestWithInvalidSDP) { |
| 221 | @autoreleasepool { |
| 222 | RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init]; |
| 223 | [test testWithInvalidSDP]; |
| 224 | } |
| 225 | } |
Byoungchan Lee | 8c48757 | 2021-08-04 20:55:25 +0900 | [diff] [blame^] | 226 | |
| 227 | TEST(RTCPeerConnectionTest, TestWithInvalidIceCandidate) { |
| 228 | @autoreleasepool { |
| 229 | RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init]; |
| 230 | [test testWithInvalidIceCandidate]; |
| 231 | } |
| 232 | } |