302 lines
13 KiB
Objective-C
302 lines
13 KiB
Objective-C
//
|
||
// PresetCacheAndRetrieveTest.m
|
||
// TrustHttpDNSTests
|
||
//
|
||
// Created by xuyecan on 2024/5/26.
|
||
// Copyright ツゥ 2024 trustapp.com. All rights reserved.
|
||
//
|
||
|
||
#import <Foundation/Foundation.h>
|
||
#import <OCMock/OCMock.h>
|
||
#import "TestBase.h"
|
||
#import "HttpdnsHostObject.h"
|
||
#import "HttpdnsService.h"
|
||
#import "HttpdnsService_Internal.h"
|
||
|
||
|
||
/**
|
||
* 逕ア莠惹スソ逕ィOCMock蝨ィ霑樒サュ逧<EFBDAD>オ玖ッ慕畑萓倶クュ驥榊、庚ock蟇ケ雎。(蜊ウ菴ソ豈乗ャ。驛ス蟾イ扈峻topMocking)莨壽怏蜀<E6808F>ュ倬漠荵ア逧<EFBDB1>琉鬚假シ<E58187>
|
||
* 逶ョ蜑崎ソ倩ァ」蜀ウ荳堺コ<E5A0BA>シ梧園莉・霑吩クェ邀サ荳ュ逧<EFBDAD>オ玖ッ苗ase<73>碁怙隕∵焔蜉ィ蜊慕峡謇ァ陦?
|
||
*/
|
||
@interface PresetCacheAndRetrieveTest : TestBase
|
||
|
||
@end
|
||
|
||
|
||
@implementation PresetCacheAndRetrieveTest
|
||
|
||
+ (void)setUp {
|
||
[super setUp];
|
||
|
||
HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:100000];
|
||
[httpdns setLogEnabled:YES];
|
||
}
|
||
|
||
+ (void)tearDown {
|
||
[super tearDown];
|
||
}
|
||
|
||
- (void)setUp {
|
||
[super setUp];
|
||
|
||
self.httpdns = [HttpDnsService sharedInstance];
|
||
self.currentTimeStamp = [[NSDate date] timeIntervalSince1970];
|
||
}
|
||
|
||
- (void)tearDown {
|
||
[super tearDown];
|
||
}
|
||
|
||
// 鄂醍サ懈ュ蜀オ荳コipv4荳狗噪郛灘ュ俶オ玖ッ<E78E96>
|
||
- (void)testSimplyRetrieveCachedResultUnderIpv4Only {
|
||
[self presetNetworkEnvAsIpv4];
|
||
[self.httpdns cleanAllHostCache];
|
||
|
||
HttpdnsHostObject *hostObject = [self constructSimpleIpv4AndIpv6HostObject];
|
||
[self.httpdns.requestManager mergeLookupResultToManager:hostObject host:ipv4AndIpv6Host cacheKey:ipv4AndIpv6Host underQueryIpType:HttpdnsQueryIPTypeBoth];
|
||
|
||
// 隸キ豎らアサ蝙倶クコipv4<76>梧響蛻ーipv4扈捺棡
|
||
HttpdnsResult *result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeIpv4];
|
||
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
|
||
// 隸キ豎らアサ蝙倶クコipv6<76>梧響蛻ーipv6扈捺棡
|
||
result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeIpv6];
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ipv6s count] == 2);
|
||
XCTAssertTrue([result.ipv6s[0] isEqualToString:ipv61]);
|
||
|
||
// both
|
||
result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeBoth];
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
XCTAssertTrue([result.ipv6s count] == 2);
|
||
XCTAssertTrue([result.ipv6s[0] isEqualToString:ipv61]);
|
||
|
||
// 隸キ豎らアサ蝙倶クコauto<74>悟宵諡ソ蛻ーipv4扈捺棡
|
||
result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeAuto];
|
||
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
XCTAssertTrue([result.ipv6s count] == 0);
|
||
}
|
||
|
||
// 鄂醍サ懆ッキ豎ゆクコipv6荳狗噪郛灘ュ俶オ玖ッ<E78E96>
|
||
- (void)testSimplyRetrieveCachedResultUnderIpv6Only {
|
||
[self presetNetworkEnvAsIpv6];
|
||
[self.httpdns cleanAllHostCache];
|
||
|
||
HttpdnsHostObject *hostObject = [self constructSimpleIpv4AndIpv6HostObject];
|
||
[self.httpdns.requestManager mergeLookupResultToManager:hostObject host:ipv4AndIpv6Host cacheKey:ipv4AndIpv6Host underQueryIpType:HttpdnsQueryIPTypeBoth];
|
||
|
||
// 隸キ豎らアサ蝙倶クコipv4<76>梧響蛻ーipv4扈捺棡
|
||
HttpdnsResult *result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeIpv4];
|
||
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
|
||
// 隸キ豎らアサ蝙倶クコipv6<76>梧響蛻ーipv6扈捺棡
|
||
result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeIpv6];
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ipv6s count] == 2);
|
||
XCTAssertTrue([result.ipv6s[0] isEqualToString:ipv61]);
|
||
|
||
// both
|
||
result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeBoth];
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
XCTAssertTrue([result.ipv6s count] == 2);
|
||
XCTAssertTrue([result.ipv6s[0] isEqualToString:ipv61]);
|
||
|
||
// 隸キ豎らアサ蝙倶クコauto<74>梧ウィ諢擾シ梧<EFBDBC>莉ャ隶、荳コipv6only蜿ェ蟄伜惠逅<E683A0>ョコ荳奇シ梧ッ泌ヲょョ樣ェ悟ョ、邇ッ蠅<EFBDAF>
|
||
// 蝗<>豁、<E8B181>景pv4逧<34>慍蝮譏ッ荳螳壻シ壼悉隗」譫千噪<E58D83>径uto逧<6F>ス懃畑蝨ィ莠趣シ悟ヲよ棡蜿醍鴫鄂醍サ懆ソ俶髪謖(pv6<76>碁ぅ蟆ア螟夊執蜿吠pv6逧<36>サ捺<EFBDBB>?
|
||
// 蝗<>豁、<E8B181>瑚ソ咎㈹蠕怜芦逧<E88AA6>ケ滓弍ipv4+ipv6
|
||
result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeAuto];
|
||
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
XCTAssertTrue([result.ipv6s count] == 2);
|
||
XCTAssertTrue([result.ipv6s[0] isEqualToString:ipv61]);
|
||
}
|
||
|
||
// 鄂醍サ懈ュ蜀オ荳コipv4蜥景pv6荳狗噪郛灘ュ俶オ玖ッ<E78E96>
|
||
- (void)testSimplyRetrieveCachedResultUnderDualStack {
|
||
[self presetNetworkEnvAsIpv4AndIpv6];
|
||
[self.httpdns cleanAllHostCache];
|
||
|
||
// 蟄伜<E89F84>ipv4蜥景pv6逧<36>慍蝮
|
||
HttpdnsHostObject *hostObject = [self constructSimpleIpv4AndIpv6HostObject];
|
||
[self.httpdns.requestManager mergeLookupResultToManager:hostObject host:ipv4AndIpv6Host cacheKey:ipv4AndIpv6Host underQueryIpType:HttpdnsQueryIPTypeBoth];
|
||
|
||
// 蜿ェ隸キ豎Jpv4
|
||
HttpdnsResult *result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeIpv4];
|
||
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
|
||
// 蜿ェ隸キ豎Jpv6
|
||
result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeIpv6];
|
||
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ipv6s count] == 2);
|
||
XCTAssertTrue([result.ipv6s[0] isEqualToString:ipv61]);
|
||
|
||
// 隸キ豎Jpv4蜥景pv6
|
||
result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeBoth];
|
||
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
XCTAssertTrue([result.ipv6s count] == 2);
|
||
XCTAssertTrue([result.ipv6s[0] isEqualToString:ipv61]);
|
||
|
||
// 閾ェ蜉ィ蛻、譁ュ邀サ蝙<EFBDBB>
|
||
result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeAuto];
|
||
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4AndIpv6Host]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
XCTAssertTrue([result.ipv6s count] == 2);
|
||
XCTAssertTrue([result.ipv6s[0] isEqualToString:ipv61]);
|
||
}
|
||
|
||
// ttl縲〕astLookupTime<6D>景pv4蜥景pv6譏ッ蛻<EFBDAF>シ螟<C280>炊逧?
|
||
- (void)testTTLAndLastLookUpTime {
|
||
[self presetNetworkEnvAsIpv4AndIpv6];
|
||
[self.httpdns cleanAllHostCache];
|
||
|
||
// 蟄伜<E89F84>ipv4蜥景pv6逧<36>慍蝮
|
||
HttpdnsHostObject *hostObject1 = [self constructSimpleIpv4AndIpv6HostObject];
|
||
hostObject1.v4ttl = 200;
|
||
hostObject1.v6ttl = 300;
|
||
|
||
int64_t currentTimestamp = [[NSDate new] timeIntervalSince1970];
|
||
|
||
hostObject1.lastIPv4LookupTime = currentTimestamp - 1;
|
||
hostObject1.lastIPv6LookupTime = currentTimestamp - 2;
|
||
|
||
// 隨ャ荳谺。隶セ鄂ョ郛灘ュ?
|
||
[self.httpdns.requestManager mergeLookupResultToManager:hostObject1 host:ipv4AndIpv6Host cacheKey:ipv4AndIpv6Host underQueryIpType:HttpdnsQueryIPTypeBoth];
|
||
|
||
// auto蝨ィ蠖灘燕邇ッ蠅<EFBDAF>ク句叉隸キ豎Jpv4蜥景pv6
|
||
HttpdnsResult *result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeAuto];
|
||
XCTAssertEqual(result.ttl, hostObject1.v4ttl);
|
||
XCTAssertEqual(result.lastUpdatedTimeInterval, hostObject1.lastIPv4LookupTime);
|
||
XCTAssertEqual(result.v6ttl, hostObject1.v6ttl);
|
||
XCTAssertEqual(result.v6LastUpdatedTimeInterval, hostObject1.lastIPv6LookupTime);
|
||
|
||
HttpdnsHostObject *hostObject2 = [self constructSimpleIpv4HostObject];
|
||
hostObject2.hostName = ipv4AndIpv6Host;
|
||
hostObject2.v4ttl = 600;
|
||
hostObject2.lastIPv4LookupTime = currentTimestamp - 10;
|
||
|
||
// 蜊慕峡蝨ィ郛灘ュ俶峩譁ーipv4蝨ー蝮逧<C280>嶌蜈ウ菫。諱?
|
||
[self.httpdns.requestManager mergeLookupResultToManager:hostObject2 host:ipv4AndIpv6Host cacheKey:ipv4AndIpv6Host underQueryIpType:HttpdnsQueryIPTypeIpv4];
|
||
|
||
// v4逧<34>ソ。諱ッ蜿醍函蜿伜喧<E4BC9C>計6逧<36>ソ。諱ッ菫晄戟荳榊<E88DB3>?
|
||
result = [self.httpdns resolveHostSyncNonBlocking:ipv4AndIpv6Host byIpType:HttpdnsQueryIPTypeAuto];
|
||
XCTAssertEqual(result.ttl, hostObject2.v4ttl);
|
||
XCTAssertEqual(result.lastUpdatedTimeInterval, hostObject2.lastIPv4LookupTime);
|
||
XCTAssertEqual(result.v6ttl, hostObject1.v6ttl);
|
||
XCTAssertEqual(result.v6LastUpdatedTimeInterval, hostObject1.lastIPv6LookupTime);
|
||
}
|
||
|
||
// 蜿ェ郛灘ュ亙pv4蜊墓<E89C8A>育噪蝨ー蝮<E89DAE>梧潔隸キ豎ょ曙譬育アサ蝙句ュ伜<EFBDAD><E4BC9C>梧ュ、譌カ莨壽<E88EA8><E5A3BD>ョー隸・蝓溷錐豐。譛永pv6蝨ー蝮
|
||
// 謖蛾「<E89BBE>悄<EFBFBD>御シ壼愛譁ュ隸・蝓溷錐豐。譛永pv6蝨ー蝮<E89DAE>悟屏豁、荳堺シ夊ソ泌屓ipv6蝨ー蝮<E89DAE>御ケ滉ク堺シ壼書隸キ豎?
|
||
- (void)testMergeNoIpv6ResultAndGetBoth {
|
||
[self presetNetworkEnvAsIpv4AndIpv6];
|
||
|
||
HttpdnsHostObject *hostObject = [self constructSimpleIpv4HostObject];
|
||
|
||
// 蜿梧<E89CBF>井ク玖ァ」譫千サ捺棡莉<E6A3A1>怏ipv4<76>悟粋蟷カ譌カ莨壽<E88EA8><E5A3BD>ョー隸・host譌<74>ipv6
|
||
[self.httpdns.requestManager mergeLookupResultToManager:hostObject host:ipv4OnlyHost cacheKey:ipv4OnlyHost underQueryIpType:HttpdnsQueryIPTypeBoth];
|
||
|
||
[self shouldNotHaveCallNetworkRequestWhenResolving:^{
|
||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||
|
||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||
HttpdnsResult *result = [self.httpdns resolveHostSync:ipv4OnlyHost byIpType:HttpdnsQueryIPTypeBoth];
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4OnlyHost]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
|
||
result = [self.httpdns resolveHostSync:ipv4OnlyHost byIpType:HttpdnsQueryIPTypeAuto];
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:ipv4OnlyHost]);
|
||
XCTAssertTrue([result.ips count] == 2);
|
||
XCTAssertTrue([result.ips[0] isEqualToString:ipv41]);
|
||
|
||
dispatch_semaphore_signal(semaphore);
|
||
});
|
||
|
||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
||
}];
|
||
}
|
||
|
||
// 郛灘ュ亙pv4蜊墓<E89C8A>育噪蝨ー蝮<E89DAE>御ス<E5BEA1>弍隸キ豎Jpv4邀サ蝙句ュ伜<EFBDAD><E4BC9C>梧ュ、譌カ荳堺シ壽遠譬<E981A0>ョー豐。譛永pv6
|
||
// 莠取弍<E58F96>瑚ッサ蜿匁慮<E58C81>御シ壼ー晁ッ募書隸キ豎り執蜿吠pv6蝨ー蝮
|
||
- (void)testMergeOnlyIpv4ResultAndGetBoth {
|
||
[self presetNetworkEnvAsIpv4AndIpv6];
|
||
|
||
HttpdnsHostObject *hostObject = [self constructSimpleIpv4HostObject];
|
||
[self.httpdns.requestManager mergeLookupResultToManager:hostObject host:ipv4OnlyHost cacheKey:ipv4OnlyHost underQueryIpType:HttpdnsQueryIPTypeIpv4];
|
||
|
||
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
|
||
|
||
// 菴ソ逕ィ蜷梧ュ・謗・蜿」<E89CBF>瑚ヲ∝<EFBDA6>謐「蛻ー蠑よュ・郤ソ遞具シ悟凄蛻吝<E89BBB>驛ィ莨夊<E88EA8>蟾ア蛻<EFBDB1>
|
||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||
[self shouldHaveCalledRequestWhenResolving:^{
|
||
HttpdnsResult *result = [self.httpdns resolveHostSync:ipv4OnlyHost byIpType:HttpdnsQueryIPTypeBoth];
|
||
XCTAssertNil(result);
|
||
dispatch_semaphore_signal(sema);
|
||
}];
|
||
});
|
||
|
||
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
|
||
}
|
||
|
||
// 郛灘ュ亙pv6蜊墓<E89C8A>育噪蝨ー蝮<E89DAE>御ス<E5BEA1>弍隸キ豎Jpv6邀サ蝙句ュ伜<EFBDAD><E4BC9C>梧ュ、譌カ荳堺シ壽遠譬<E981A0>ョー豐。譛永pv4
|
||
// 莠取弍<E58F96>瑚ッサ蜿匁慮<E58C81>御シ壼ー晁ッ募書隸キ豎り執蜿吠pv4蝨ー蝮
|
||
- (void)testMergeOnlyIpv6ResultAndGetBoth {
|
||
[self presetNetworkEnvAsIpv4AndIpv6];
|
||
|
||
HttpdnsHostObject *hostObject = [self constructSimpleIpv6HostObject];
|
||
[self.httpdns.requestManager mergeLookupResultToManager:hostObject host:ipv6OnlyHost cacheKey:ipv6OnlyHost underQueryIpType:HttpdnsQueryIPTypeIpv6];
|
||
|
||
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
|
||
|
||
// 菴ソ逕ィ蜷梧ュ・謗・蜿」<E89CBF>瑚ヲ∝<EFBDA6>謐「蛻ー蠑よュ・郤ソ遞具シ悟凄蛻吝<E89BBB>驛ィ莨夊<E88EA8>蟾ア蛻<EFBDB1>
|
||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||
[self shouldHaveCalledRequestWhenResolving:^{
|
||
HttpdnsResult *result = [self.httpdns resolveHostSync:ipv6OnlyHost byIpType:HttpdnsQueryIPTypeBoth];
|
||
XCTAssertNil(result);
|
||
dispatch_semaphore_signal(sema);
|
||
}];
|
||
});
|
||
|
||
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
|
||
}
|
||
|
||
@end
|