1/* 2 * 3 * Copyright 2022 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19#import <XCTest/XCTest.h> 20 21#import <GRPCClient/GRPCCallOptions.h> 22 23static NSString *const kGRPCCallOptionsTestServerAuthority = @"dummy"; 24static NSTimeInterval kGRPCCallOptionsTestTimeout = 11; 25static BOOL kGRPCCallOptionsTestFlowControl = YES; 26static NSString *const kGRPCCallOptionsTestOAuth2Token = @"token"; 27static NSString *const kGRPCCallOptionsTestUserAgentPrefix = @"test_prefix"; 28static NSString *const kGRPCCallOptionsTestUserAgentSuffix = @"test_suffix"; 29static NSUInteger kGRPCCallOptionsTestResponseSizeLimit = 81; 30static GRPCCompressionAlgorithm kGRPCCallOptionsTestCompressionAlgorithm = GRPCCompressDeflate; 31static BOOL kGRPCCallOptionsTestRetryEnabled = YES; 32static NSTimeInterval kGRPCCallOptionsTestMaxRetryInterval = 101; 33static NSTimeInterval kGRPCCallOptionsTestMinRetryInterval = 23; 34static double kGRPCCallOptionsTestRetryFactor = 2.12; 35static NSTimeInterval kGRPCCallOptionsTestKeepAliveTimeout = 120; 36static NSTimeInterval kGRPCCallOptionsTestKeepAliveInterval = 60; 37static NSTimeInterval kGRPCCallOptionsTestConnectMaxBackoff = 180; 38static NSTimeInterval kGRPCCallOptionsTestConnectMinTimeout = 210; 39static NSTimeInterval kGRPCCallOptionsTestConnectInitialBackoff = 12; 40static NSString *const kGRPCCallOptionsTestPEMPrivateKey = @"dummy_key"; 41static NSString *const kGRPCCallOptionsTestPEMRootCertificates = @"dummy_cert"; 42static NSString *const kGRPCCallOptionsTestPEMCertificateChain = @"dummy_chain"; 43static GRPCTransportType kGRPCCallOptionsTestTransportType = GRPCTransportTypeChttp2BoringSSL; 44static GRPCTransportID kGRPCCallOptionsTestTransportID = "dummy_transport"; 45static NSString *const kGRPCCallOptionsTestHostNameOverride = @"localhost"; 46static NSString *const kGRPCCallOptionsTestChannelPoolDomain = @"dummy_domain"; 47static NSUInteger kGRPCCallOptionsTestChannelID = 111; 48 49@interface GRPCCallOptionsTests : XCTestCase 50 51@end 52 53@implementation GRPCCallOptionsTests 54 55/** Verify a valid immutable copy can be created from GRPCCallOptions. */ 56- (void)testCreateImmutableCopyFromImmutable { 57 GRPCCallOptions *opt = [[GRPCCallOptions alloc] init]; 58 GRPCCallOptions *subject = [opt copy]; 59 XCTAssertTrue([subject isKindOfClass:[GRPCCallOptions class]]); 60} 61 62/** Verify a valid mutable copy can be created from GRPCCallOptions. */ 63- (void)testCreateMutableCopyFromImmutable { 64 GRPCCallOptions *opt = [[GRPCCallOptions alloc] init]; 65 GRPCMutableCallOptions *subject = [opt mutableCopy]; 66 XCTAssertTrue([subject isKindOfClass:[GRPCMutableCallOptions class]]); 67} 68 69/** Verify a valid mutable copy can be created from GRPCMutableCallOptions. */ 70- (void)testCreateMutableCopyFromMutable { 71 GRPCMutableCallOptions *opt = [[GRPCMutableCallOptions alloc] init]; 72 GRPCMutableCallOptions *subject = [opt mutableCopy]; 73 XCTAssertTrue([subject isKindOfClass:[GRPCMutableCallOptions class]]); 74} 75 76/** Verify a valid immutable copy can be created from GRPCMutableCallOptions. */ 77- (void)testCreateImmutableCopyFromMutable { 78 GRPCMutableCallOptions *mutableOpt = [[GRPCMutableCallOptions alloc] init]; 79 GRPCCallOptions *subject = [mutableOpt copy]; 80 XCTAssertTrue([subject isKindOfClass:[GRPCCallOptions class]]); 81} 82 83/** Verify property values are copied when copy from mutable options. */ 84- (void)testCopyFromMutableCallOptions { 85 GRPCMutableCallOptions *mutableOpt = [self testMutableCallOptions]; 86 XCTAssertTrue([self isEqualToTestCallOptions:[mutableOpt copy]]); 87 XCTAssertTrue([self isEqualToTestCallOptions:[mutableOpt mutableCopy]]); 88} 89 90/** Verify property values are copied when copy from immutable options */ 91- (void)testCopyFromImmutableCallOptions { 92 GRPCCallOptions *opt = [[self testMutableCallOptions] copy]; 93 XCTAssertTrue([self isEqualToTestCallOptions:[opt copy]]); 94 XCTAssertTrue([self isEqualToTestCallOptions:[opt mutableCopy]]); 95} 96 97#pragma mark - Private 98 99- (GRPCMutableCallOptions *)testMutableCallOptions { 100 GRPCMutableCallOptions *mutableOpt = [[GRPCMutableCallOptions alloc] init]; 101 mutableOpt.serverAuthority = kGRPCCallOptionsTestServerAuthority; 102 mutableOpt.timeout = kGRPCCallOptionsTestTimeout; 103 mutableOpt.flowControlEnabled = kGRPCCallOptionsTestFlowControl; 104 mutableOpt.oauth2AccessToken = kGRPCCallOptionsTestOAuth2Token; 105 mutableOpt.initialMetadata = @{@"key" : @"value"}; 106 mutableOpt.userAgentPrefix = kGRPCCallOptionsTestUserAgentPrefix; 107 mutableOpt.userAgentSuffix = kGRPCCallOptionsTestUserAgentSuffix; 108 mutableOpt.responseSizeLimit = kGRPCCallOptionsTestResponseSizeLimit; 109 mutableOpt.compressionAlgorithm = kGRPCCallOptionsTestCompressionAlgorithm; 110 mutableOpt.retryEnabled = kGRPCCallOptionsTestRetryEnabled; 111 mutableOpt.maxRetryInterval = kGRPCCallOptionsTestMaxRetryInterval; 112 mutableOpt.minRetryInterval = kGRPCCallOptionsTestMinRetryInterval; 113 mutableOpt.retryFactor = kGRPCCallOptionsTestRetryFactor; 114 mutableOpt.keepaliveTimeout = kGRPCCallOptionsTestKeepAliveTimeout; 115 mutableOpt.keepaliveInterval = kGRPCCallOptionsTestKeepAliveInterval; 116 mutableOpt.connectMaxBackoff = kGRPCCallOptionsTestConnectMaxBackoff; 117 mutableOpt.connectMinTimeout = kGRPCCallOptionsTestConnectMinTimeout; 118 mutableOpt.connectInitialBackoff = kGRPCCallOptionsTestConnectInitialBackoff; 119 mutableOpt.additionalChannelArgs = @{@"extra_key" : @"extra_value"}; 120 mutableOpt.PEMPrivateKey = kGRPCCallOptionsTestPEMPrivateKey; 121 mutableOpt.PEMRootCertificates = kGRPCCallOptionsTestPEMRootCertificates; 122 mutableOpt.PEMCertificateChain = kGRPCCallOptionsTestPEMCertificateChain; 123 mutableOpt.transportType = kGRPCCallOptionsTestTransportType; 124 mutableOpt.transport = kGRPCCallOptionsTestTransportID; 125 mutableOpt.hostNameOverride = kGRPCCallOptionsTestHostNameOverride; 126 mutableOpt.channelPoolDomain = kGRPCCallOptionsTestChannelPoolDomain; 127 mutableOpt.channelID = kGRPCCallOptionsTestChannelID; 128 return mutableOpt; 129} 130 131- (BOOL)isEqualToTestCallOptions:(GRPCCallOptions *)callOpt { 132 return [callOpt.serverAuthority isEqualToString:kGRPCCallOptionsTestServerAuthority] && 133 callOpt.timeout == kGRPCCallOptionsTestTimeout && 134 callOpt.flowControlEnabled == kGRPCCallOptionsTestFlowControl && 135 [callOpt.oauth2AccessToken isEqualToString:kGRPCCallOptionsTestOAuth2Token] && 136 [callOpt.initialMetadata isEqualToDictionary:@{@"key" : @"value"}] && 137 [callOpt.userAgentPrefix isEqualToString:kGRPCCallOptionsTestUserAgentPrefix] && 138 [callOpt.userAgentSuffix isEqualToString:kGRPCCallOptionsTestUserAgentSuffix] && 139 callOpt.responseSizeLimit == kGRPCCallOptionsTestResponseSizeLimit && 140 callOpt.compressionAlgorithm == kGRPCCallOptionsTestCompressionAlgorithm && 141 callOpt.retryEnabled == kGRPCCallOptionsTestRetryEnabled && 142 callOpt.maxRetryInterval == kGRPCCallOptionsTestMaxRetryInterval && 143 callOpt.minRetryInterval == kGRPCCallOptionsTestMinRetryInterval && 144 callOpt.retryFactor == kGRPCCallOptionsTestRetryFactor && 145 callOpt.keepaliveTimeout == kGRPCCallOptionsTestKeepAliveTimeout && 146 callOpt.keepaliveInterval == kGRPCCallOptionsTestKeepAliveInterval && 147 callOpt.connectMaxBackoff == kGRPCCallOptionsTestConnectMaxBackoff && 148 callOpt.connectMinTimeout == kGRPCCallOptionsTestConnectMinTimeout && 149 callOpt.connectInitialBackoff == kGRPCCallOptionsTestConnectInitialBackoff && 150 [callOpt.additionalChannelArgs isEqualToDictionary:@{@"extra_key" : @"extra_value"}] && 151 [callOpt.PEMPrivateKey isEqualToString:kGRPCCallOptionsTestPEMPrivateKey] && 152 [callOpt.PEMCertificateChain isEqualToString:kGRPCCallOptionsTestPEMCertificateChain] && 153 [callOpt.PEMRootCertificates isEqualToString:kGRPCCallOptionsTestPEMRootCertificates] && 154 callOpt.transportType == kGRPCCallOptionsTestTransportType && 155 callOpt.transport == kGRPCCallOptionsTestTransportID && 156 [callOpt.hostNameOverride isEqualToString:kGRPCCallOptionsTestHostNameOverride] && 157 [callOpt.channelPoolDomain isEqualToString:kGRPCCallOptionsTestChannelPoolDomain] && 158 callOpt.channelID == kGRPCCallOptionsTestChannelID; 159} 160 161@end 162