109 lines
3.2 KiB
Objective-C
109 lines
3.2 KiB
Objective-C
//
|
||
// SdnsScenarioTest.m
|
||
// TrustHttpDNSTests
|
||
//
|
||
// Created by xuyecan on 2024/5/29.
|
||
// Copyright © 2024 trustapp.com. All rights reserved.
|
||
//
|
||
|
||
#import <Foundation/Foundation.h>
|
||
#import "TestBase.h"
|
||
|
||
|
||
@interface SdnsScenarioTest : TestBase <HttpdnsTTLDelegate>
|
||
|
||
@end
|
||
|
||
static int ttlForTest = 3;
|
||
static NSString *sdnsHost = @"sdns1.onlyforhttpdnstest.run.place";
|
||
|
||
@implementation SdnsScenarioTest
|
||
|
||
+ (void)setUp {
|
||
[super setUp];
|
||
}
|
||
|
||
- (void)setUp {
|
||
[super setUp];
|
||
|
||
static dispatch_once_t onceToken;
|
||
dispatch_once(&onceToken, ^{
|
||
self.httpdns = [[HttpDnsService alloc] initWithAccountID:100000];
|
||
});
|
||
|
||
[self.httpdns setLogEnabled:YES];
|
||
|
||
[self.httpdns setReuseExpiredIPEnabled:NO];
|
||
|
||
[self.httpdns setTtlDelegate:self];
|
||
[self.httpdns setLogHandler:self];
|
||
|
||
self.currentTimeStamp = [[NSDate date] timeIntervalSince1970];
|
||
}
|
||
|
||
- (int64_t)httpdnsHost:(NSString *)host ipType:(TrustHttpDNS_IPType)ipType ttl:(int64_t)ttl {
|
||
// 在测试ä¸åŸŸå<C5B8><C3A5>快速过æœ?
|
||
return ttlForTest;
|
||
}
|
||
|
||
- (void)testSimpleSdnsScenario {
|
||
NSDictionary *extras = @{
|
||
@"testKey": @"testValue",
|
||
@"key2": @"value2",
|
||
@"key3": @"value3"
|
||
};
|
||
|
||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||
|
||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||
HttpdnsResult *result = [self.httpdns resolveHostSync:sdnsHost byIpType:HttpdnsQueryIPTypeIpv4 withSdnsParams:extras sdnsCacheKey:nil];
|
||
XCTAssertNotNil(result);
|
||
XCTAssertNotNil(result.ips);
|
||
// 0.0.0.0 是FCå‡½æ•°ä¸Šæ·»åŠ è¿›åŽ»çš„
|
||
XCTAssertTrue([result.ips containsObject:@"0.0.0.0"]);
|
||
|
||
dispatch_semaphore_signal(semaphore);
|
||
});
|
||
|
||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
||
}
|
||
|
||
- (void)testSdnsScenarioUsingCustomCacheKey {
|
||
[self.httpdns.requestManager cleanAllHostMemoryCache];
|
||
|
||
NSDictionary *extras = @{
|
||
@"testKey": @"testValue",
|
||
@"key2": @"value2",
|
||
@"key3": @"value3"
|
||
};
|
||
|
||
NSString *cacheKey = [NSString stringWithFormat:@"abcd_%@", sdnsHost];
|
||
|
||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||
|
||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||
HttpdnsResult *result = [self.httpdns resolveHostSync:sdnsHost byIpType:HttpdnsQueryIPTypeIpv4 withSdnsParams:extras sdnsCacheKey:cacheKey];
|
||
XCTAssertNotNil(result);
|
||
XCTAssertNotNil(result.ips);
|
||
// 0.0.0.0 是FCå‡½æ•°ä¸Šæ·»åŠ è¿›åŽ»çš„
|
||
XCTAssertTrue([result.ips containsObject:@"0.0.0.0"]);
|
||
|
||
dispatch_semaphore_signal(semaphore);
|
||
});
|
||
|
||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
||
|
||
// 按预期,上é<C5A0>¢çš„结果是按cacheKeyæ<79>¥ç¼“å˜çš„,这里ä¸<C3A4>指定cacheKey,应当拿到nil
|
||
HttpdnsResult *result1 = [self.httpdns resolveHostSyncNonBlocking:sdnsHost byIpType:HttpdnsQueryIPTypeIpv4];
|
||
XCTAssertNil(result1);
|
||
|
||
// 使用cachekey,应立å<E280B9>³æ‹¿åˆ°ç¼“å˜é‡Œçš„结果
|
||
HttpdnsResult *result2 = [self.httpdns resolveHostSync:sdnsHost byIpType:HttpdnsQueryIPTypeIpv4 withSdnsParams:@{} sdnsCacheKey:cacheKey];
|
||
XCTAssertNotNil(result2);
|
||
XCTAssertNotNil(result2.ips);
|
||
// 0.0.0.0 是FCå‡½æ•°ä¸Šæ·»åŠ è¿›åŽ»çš„
|
||
XCTAssertTrue([result2.ips containsObject:@"0.0.0.0"]);
|
||
}
|
||
|
||
@end
|