feat: sync httpdns sdk/platform updates without large binaries

This commit is contained in:
robin
2026-03-04 17:59:14 +08:00
parent 853897a6f8
commit 532891fad0
700 changed files with 6096 additions and 2712 deletions

View File

@@ -0,0 +1,402 @@
//
// IpDetectorTest.m
// TrustHttpDNSTests
//
// Created by xuyecan on 2025/3/14.
// Copyright © 2025 trustapp.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "../Testbase/TestBase.h"
#import "HttpdnsIPQualityDetector.h"
@interface IpDetectorTest : TestBase
@end
@implementation IpDetectorTest
- (void)setUp {
[super setUp];
// 使maxConcurrentDetections
}
- (void)tearDown {
[super tearDown];
}
#pragma mark - <EFBFBD><EFBFBD>?
- (void)testSharedInstance {
//
HttpdnsIPQualityDetector *detector1 = [HttpdnsIPQualityDetector sharedInstance];
HttpdnsIPQualityDetector *detector2 = [HttpdnsIPQualityDetector sharedInstance];
XCTAssertEqual(detector1, detector2, @"<EFBFBD><EFBFBD>?);
XCTAssertNotNil(detector1, @"单例实例不应为nil");
}
#pragma mark - TCP
- (void)testTcpConnectToValidIP {
// IP
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
// 使DNS<EFBFBD><EFBFBD>?
NSInteger costTime = [detector tcpConnectToIP:@"8.8.8.8" port:53];
//
XCTAssertGreaterThan(costTime, 0, @"连接到有效IP应返回正数耗时");
}
- (void)testTcpConnectToInvalidIP {
// IP
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
// 使IP
NSInteger costTime = [detector tcpConnectToIP:@"192.168.255.255" port:12345];
// <EFBFBD><EFBFBD>?1
XCTAssertEqual(costTime, -1, @"连接到无效IP应返<E5BA94><E8BF94>?1");
}
- (void)testTcpConnectWithInvalidParameters {
// 使
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
// IP
NSInteger costTime = [detector tcpConnectToIP:nil port:80];
XCTAssertEqual(costTime, -1, @"使用nil IP应返<E5BA94><E8BF94>?1");
// IP
costTime = [detector tcpConnectToIP:@"not-an-ip" port:80];
XCTAssertEqual(costTime, -1, @"使用无效格式IP应返<E5BA94><E8BF94>?1");
//
costTime = [detector tcpConnectToIP:@"8.8.8.8" port:-1];
XCTAssertEqual(costTime, -1, @"使用无效端口应返<E5BA94><E8BF94>?1");
}
#pragma mark -
- (void)testScheduleIPQualityDetection {
// IP<EFBFBD><EFBFBD>?
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
id detectorMock = OCMPartialMock(detector);
// executeDetection
OCMExpect([detectorMock executeDetection:@"example.com"
ip:@"1.2.3.4"
port:[NSNumber numberWithInt:80]
callback:[OCMArg any]]);
//
[detectorMock scheduleIPQualityDetection:@"example.com"
ip:@"1.2.3.4"
port:[NSNumber numberWithInt:80]
callback:^(NSString *cacheKey, NSString *ip, NSInteger costTime) {
// executeDetection
}];
//
OCMVerifyAll(detectorMock);
//
[detectorMock stopMocking];
}
- (void)testScheduleWithInvalidParameters {
// 使
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
id detectorMock = OCMPartialMock(detector);
// executeDetection<EFBFBD><EFBFBD>?
OCMReject([detectorMock executeDetection:[OCMArg any]
ip:[OCMArg any]
port:[OCMArg any]
callback:[OCMArg any]]);
// nil cacheKey
[detectorMock scheduleIPQualityDetection:nil
ip:@"1.2.3.4"
port:[NSNumber numberWithInt:80]
callback:^(NSString *cacheKey, NSString *ip, NSInteger costTime) {}];
// nil IP
[detectorMock scheduleIPQualityDetection:@"example.com"
ip:nil
port:[NSNumber numberWithInt:80]
callback:^(NSString *cacheKey, NSString *ip, NSInteger costTime) {}];
// nil callback
[detectorMock scheduleIPQualityDetection:@"example.com"
ip:@"1.2.3.4"
port:[NSNumber numberWithInt:80]
callback:nil];
//
OCMVerifyAll(detectorMock);
//
[detectorMock stopMocking];
}
- (void)testConcurrencyLimitReached {
//
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
id detectorMock = OCMPartialMock(detector);
// dispatch_semaphore_wait<EFBFBD><EFBFBD>?
// scheduleIPQualityDetectionaddPendingTask
OCMStub([detectorMock scheduleIPQualityDetection:[OCMArg any]
ip:[OCMArg any]
port:[OCMArg any]
callback:[OCMArg any]]).andDo(^(NSInvocation *invocation) {
//
NSString *cacheKey;
NSString *ip;
NSNumber *port;
HttpdnsIPQualityCallback callback;
[invocation getArgument:&cacheKey atIndex:2];
[invocation getArgument:&ip atIndex:3];
[invocation getArgument:&port atIndex:4];
[invocation getArgument:&callback atIndex:5];
// addPendingTask<EFBFBD><EFBFBD>?
[detector addPendingTask:cacheKey ip:ip port:port callback:callback];
});
// addPendingTaskexecuteDetection
// 使mockmock
OCMExpect([detectorMock addPendingTask:@"example.com"
ip:@"1.2.3.4"
port:[NSNumber numberWithInt:80]
callback:[OCMArg any]]);
OCMReject([detectorMock executeDetection:[OCMArg any]
ip:[OCMArg any]
port:[OCMArg any]
callback:[OCMArg any]]);
//
[detectorMock scheduleIPQualityDetection:@"example.com"
ip:@"1.2.3.4"
port:[NSNumber numberWithInt:80]
callback:^(NSString *cacheKey, NSString *ip, NSInteger costTime) {}];
//
OCMVerifyAll(detectorMock);
//
[detectorMock stopMocking];
}
- (void)testAddPendingTask {
// <EFBFBD><EFBFBD>?
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
id detectorMock = OCMPartialMock(detector);
// processPendingTasksIfNeeded<EFBFBD><EFBFBD>?
OCMStub([detectorMock processPendingTasksIfNeeded]);
// <EFBFBD><EFBFBD>?
NSUInteger initialCount = [detector pendingTasksCount];
//
[detectorMock addPendingTask:@"example.com"
ip:@"1.2.3.4"
port:[NSNumber numberWithInt:80]
callback:^(NSString *cacheKey, NSString *ip, NSInteger costTime) {}];
// <EFBFBD><EFBFBD>?
XCTAssertEqual([detector pendingTasksCount], initialCount + 1, @"添加任务后待处理任务数量应增<E5BA94><E5A29E>?");
//
[detectorMock stopMocking];
}
- (void)testPendingTasksProcessing {
//
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
id detectorMock = OCMPartialMock(detector);
// executeDetection<EFBFBD><EFBFBD>?
OCMStub([detectorMock executeDetection:[OCMArg any]
ip:[OCMArg any]
port:[OCMArg any]
callback:[OCMArg any]]);
//
[detectorMock addPendingTask:@"example.com"
ip:@"1.2.3.4"
port:[NSNumber numberWithInt:80]
callback:^(NSString *cacheKey, NSString *ip, NSInteger costTime) {}];
// <EFBFBD><EFBFBD>?
[detectorMock processPendingTasksIfNeeded];
// <EFBFBD><EFBFBD>?
[NSThread sleepForTimeInterval:0.1];
// <EFBFBD><EFBFBD>?
XCTAssertEqual([detector pendingTasksCount], 0, @"处理后待处理任务数量应为0");
//
[detectorMock stopMocking];
}
#pragma mark -
- (void)testExecuteDetection {
//
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
id detectorMock = OCMPartialMock(detector);
// tcpConnectToIP<EFBFBD><EFBFBD>?
OCMStub([detectorMock tcpConnectToIP:@"1.2.3.4" port:80]).andReturn(100);
//
XCTestExpectation *expectation = [self expectationWithDescription:@"回调应被执行"];
//
[detectorMock executeDetection:@"example.com"
ip:@"1.2.3.4"
port:[NSNumber numberWithInt:80]
callback:^(NSString *cacheKey, NSString *ip, NSInteger costTime) {
//
XCTAssertEqualObjects(cacheKey, @"example.com", @"cacheKey<EFBFBD><EFBFBD>?);
XCTAssertEqualObjects(ip, @"1.2.3.4", @"IP<EFBFBD><EFBFBD>?);
XCTAssertEqual(costTime, 100, @"<EFBFBD><EFBFBD>?);
[expectation fulfill];
}];
//
[self waitForExpectationsWithTimeout:5.0 handler:nil];
//
[detectorMock stopMocking];
}
- (void)testExecuteDetectionWithFailure {
//
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
id detectorMock = OCMPartialMock(detector);
// tcpConnectToIP
OCMStub([detectorMock tcpConnectToIP:@"1.2.3.4" port:80]).andReturn(-1);
//
XCTestExpectation *expectation = [self expectationWithDescription:@"失败回调应被执行"];
//
[detectorMock executeDetection:@"example.com"
ip:@"1.2.3.4"
port:[NSNumber numberWithInt:80]
callback:^(NSString *cacheKey, NSString *ip, NSInteger costTime) {
//
XCTAssertEqualObjects(cacheKey, @"example.com", @"cacheKey<EFBFBD><EFBFBD>?);
XCTAssertEqualObjects(ip, @"1.2.3.4", @"IP<EFBFBD><EFBFBD>?);
XCTAssertEqual(costTime, -1, @"连接失败时回调中的耗时应为-1");
[expectation fulfill];
}];
//
[self waitForExpectationsWithTimeout:5.0 handler:nil];
//
[detectorMock stopMocking];
}
- (void)testExecuteDetectionWithNilPort {
// nil<EFBFBD><EFBFBD>?
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
id detectorMock = OCMPartialMock(detector);
// tcpConnectToIP使<EFBFBD><EFBFBD>?0
OCMExpect([detectorMock tcpConnectToIP:@"1.2.3.4" port:80]).andReturn(100);
//
XCTestExpectation *expectation = [self expectationWithDescription:@"默认端口回调应被执行"];
//
[detectorMock executeDetection:@"example.com"
ip:@"1.2.3.4"
port:nil
callback:^(NSString *cacheKey, NSString *ip, NSInteger costTime) {
[expectation fulfill];
}];
//
[self waitForExpectationsWithTimeout:5.0 handler:nil];
//
OCMVerifyAll(detectorMock);
//
[detectorMock stopMocking];
}
#pragma mark -
- (void)testMemoryManagementInAsyncOperations {
//
HttpdnsIPQualityDetector *detector = [HttpdnsIPQualityDetector sharedInstance];
id detectorMock = OCMPartialMock(detector);
//
__block NSString *tempCacheKey = [@"example.com" copy];
__block NSString *tempIP = [@"1.2.3.4" copy];
//
__weak NSString *weakCacheKey = tempCacheKey;
__weak NSString *weakIP = tempIP;
// tcpConnectToIP
OCMStub([detectorMock tcpConnectToIP:[OCMArg any] port:80]).andDo(^(NSInvocation *invocation) {
// GC<EFBFBD><EFBFBD>?
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// <EFBFBD><EFBFBD>?
NSInteger result = 100;
[invocation setReturnValue:&result];
});
});
//
XCTestExpectation *expectation = [self expectationWithDescription:@"内存管理回调应被执行"];
//
[detectorMock executeDetection:tempCacheKey
ip:tempIP
port:[NSNumber numberWithInt:80]
callback:^(NSString *cacheKey, NSString *ip, NSInteger costTime) {
//
XCTAssertEqualObjects(cacheKey, @"example.com", @"cacheKey<EFBFBD><EFBFBD>?);
XCTAssertEqualObjects(ip, @"1.2.3.4", @"IP<EFBFBD><EFBFBD>?);
[expectation fulfill];
}];
// <EFBFBD><EFBFBD>?
tempCacheKey = nil;
tempIP = nil;
// GCARC<EFBFBD><EFBFBD>?
@autoreleasepool {
// <EFBFBD><EFBFBD>?
}
// executeDetection
XCTAssertNotNil(weakCacheKey, @"cacheKey<EFBFBD><EFBFBD>?);
XCTAssertNotNil(weakIP, @"IP<EFBFBD><EFBFBD>?);
//
[self waitForExpectationsWithTimeout:5.0 handler:nil];
//
[detectorMock stopMocking];
}
@end