Skip to content

Commit db0b8f6

Browse files
committed
Reachability: use NSURLSession for MacOS 10.14 or higher
1 parent 69d0078 commit db0b8f6

File tree

4 files changed

+397
-55
lines changed

4 files changed

+397
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// ODWReachabilityTests.mm
6+
// Tests
7+
//
8+
// Created by Abu Sayem on 13/12/2024.
9+
//
10+
11+
#import <XCTest/XCTest.h>
12+
#import "ODWReachability.h"
13+
14+
#import <sys/socket.h>
15+
#import <netinet/in.h>
16+
#import <netinet6/in6.h>
17+
#import <arpa/inet.h>
18+
#import <ifaddrs.h>
19+
#import <netdb.h>
20+
#import <Foundation/Foundation.h>
21+
22+
@interface ODWReachabilityTests : XCTestCase
23+
@end
24+
25+
@implementation ODWReachabilityTests
26+
27+
- (void)testReachabilityWithHostname
28+
{
29+
NSString *hostname = @"www.microsoft.com";
30+
ODWReachability *reachability = [ODWReachability reachabilityWithHostname:hostname];
31+
32+
XCTestExpectation *expectation = [self expectationWithDescription:@"Reachability check"];
33+
NSString *hostUrl = [NSString stringWithFormat:@"https://%@", hostname];
34+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
35+
XCTAssertNotNil(reachability);
36+
XCTAssertEqualObjects(reachability.url.absoluteString, hostUrl);
37+
[expectation fulfill];
38+
});
39+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
40+
}
41+
42+
- (void)testReachabilityWithInvalidHostname
43+
{
44+
NSString *hostname = @"invalid.hostname";
45+
ODWReachability *reachability = [ODWReachability reachabilityWithHostname:hostname];
46+
47+
XCTestExpectation *expectation = [self expectationWithDescription:@"Reachability check"];
48+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
49+
XCTAssertNil(reachability);
50+
[expectation fulfill];
51+
});
52+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
53+
}
54+
55+
- (void)testReachabilityWithAddress
56+
{
57+
struct sockaddr_in address;
58+
address.sin_family = AF_INET;
59+
address.sin_addr.s_addr = inet_addr("8.8.8.8");
60+
ODWReachability *reachability = [ODWReachability reachabilityWithAddress:&address];
61+
62+
XCTestExpectation *expectation = [self expectationWithDescription:@"Reachability check"];
63+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
64+
XCTAssertNotNil(reachability);
65+
XCTAssertEqualObjects(reachability.url.absoluteString, @"https://8.8.8.8");
66+
[expectation fulfill];
67+
});
68+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
69+
}
70+
71+
- (void)testReachabilityWithInvalidAddress
72+
{
73+
struct sockaddr_in address;
74+
address.sin_family = AF_INET;
75+
address.sin_addr.s_addr = inet_addr("0.0.0.0");
76+
ODWReachability *reachability = [ODWReachability reachabilityWithAddress:&address];
77+
78+
XCTestExpectation *expectation = [self expectationWithDescription:@"Reachability check"];
79+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
80+
XCTAssertNil(reachability);
81+
[expectation fulfill];
82+
});
83+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
84+
}
85+
86+
- (void)testReachabilityForInternetConnection
87+
{
88+
ODWReachability *reachability = [ODWReachability reachabilityForInternetConnection];
89+
90+
XCTestExpectation *expectation = [self expectationWithDescription:@"Reachability check"];
91+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
92+
XCTAssertNotNil(reachability);
93+
XCTAssertEqual(reachability.currentReachabilityStatus, ReachableViaWiFi);
94+
[expectation fulfill];
95+
});
96+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
97+
}
98+
99+
- (void)testReachabilityForLocalWiFi
100+
{
101+
ODWReachability *reachability = [ODWReachability reachabilityForLocalWiFi];
102+
103+
XCTestExpectation *expectation = [self expectationWithDescription:@"Reachability check"];
104+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
105+
XCTAssertNotNil(reachability);
106+
XCTAssertEqual(reachability.currentReachabilityStatus, ReachableViaWiFi);
107+
[expectation fulfill];
108+
});
109+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
110+
}
111+
112+
@end
113+

tests/unittests/unittests-ios.xcodeproj/project.pbxproj

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
16CC10702D4D58DA00C03295 /* ODWReachabilityTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 16CC106F2D4D58DA00C03295 /* ODWReachabilityTests.mm */; };
1011
430102E5235E660F00836D50 /* iOSWrapper.mm in Sources */ = {isa = PBXBuildFile; fileRef = 430102E4235E660F00836D50 /* iOSWrapper.mm */; };
1112
431EFE50233EBE54002FCC18 /* HttpClientTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 431EFE2A233EBE53002FCC18 /* HttpClientTests.cpp */; };
1213
431EFE51233EBE54002FCC18 /* LoggerTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 431EFE2B233EBE53002FCC18 /* LoggerTests.cpp */; };
@@ -116,6 +117,7 @@
116117
/* End PBXCopyFilesBuildPhase section */
117118

118119
/* Begin PBXFileReference section */
120+
16CC106F2D4D58DA00C03295 /* ODWReachabilityTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = ODWReachabilityTests.mm; path = "obj-c/ODWReachabilityTests.mm"; sourceTree = "<group>"; };
119121
430102E4235E660F00836D50 /* iOSWrapper.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = iOSWrapper.mm; path = ../../common/iOSWrapper.mm; sourceTree = "<group>"; };
120122
431EFE1E233EBDF2002FCC18 /* libunittests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libunittests.a; sourceTree = BUILT_PRODUCTS_DIR; };
121123
431EFE2A233EBE53002FCC18 /* HttpClientTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HttpClientTests.cpp; sourceTree = "<group>"; };
@@ -201,6 +203,7 @@
201203
431EFE15233EBDF2002FCC18 = {
202204
isa = PBXGroup;
203205
children = (
206+
16CC106F2D4D58DA00C03295 /* ODWReachabilityTests.mm */,
204207
43BD496F26B8B0C300EC3C0C /* EventPropertiesDecoratorTests.cpp */,
205208
437C72F024D2286F0046F545 /* ODWLogConfigurationTests.mm */,
206209
437C72EE24D21ACE0046F545 /* ODWSemanticContextTests.mm */,
@@ -399,6 +402,7 @@
399402
431EFE60233EBE54002FCC18 /* ControlPlaneProviderTests.cpp in Sources */,
400403
431EFE5D233EBE54002FCC18 /* ClockSkewManagerTests.cpp in Sources */,
401404
431EFE58233EBE54002FCC18 /* PalTests.cpp in Sources */,
405+
16CC10702D4D58DA00C03295 /* ODWReachabilityTests.mm in Sources */,
402406
431EFED423425535002FCC18 /* DataViewerCollectionTests.cpp in Sources */,
403407
431EFE70233EBE54002FCC18 /* OfflineStorageTests.cpp in Sources */,
404408
431EFE6B233EBE54002FCC18 /* OacrTests.cpp in Sources */,
@@ -433,6 +437,7 @@
433437
431EFEA7233EC590002FCC18 /* HttpDeflateCompressionTests.cpp in Sources */,
434438
431EFEB3233EC590002FCC18 /* OfflineStorageTests.cpp in Sources */,
435439
431EFEB8233EC590002FCC18 /* TaskDispatcherCAPITests.cpp in Sources */,
440+
16CC10712D4D58DA00C03295 /* ODWReachabilityTests.mm in Sources */,
436441
437C72ED24D0C5D70046F545 /* ODWEventPropertiesTests.mm in Sources */,
437442
431EFEB6233EC590002FCC18 /* RouteTests.cpp in Sources */,
438443
431EFEA8233EC590002FCC18 /* HttpRequestEncoderTests.cpp in Sources */,

third_party/Reachability/ODWReachability.h

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ typedef void (^NetworkUnreachable)(ODWReachability * reachability);
6060

6161
@property (nonatomic, assign) BOOL reachableOnWWAN;
6262

63+
@property (nonatomic, strong) NSURL *url;
6364

6465
+(ODWReachability*)reachabilityWithHostname:(NSString*)hostname;
6566
// This is identical to the function above, but is here to maintain
@@ -68,6 +69,7 @@ typedef void (^NetworkUnreachable)(ODWReachability * reachability);
6869
+(ODWReachability*)reachabilityForInternetConnection;
6970
+(ODWReachability*)reachabilityWithAddress:(void *)hostAddress;
7071
+(ODWReachability*)reachabilityForLocalWiFi;
72+
+(void)setTimeoutDurationInSeconds:(int)timeoutDuration;
7173

7274
-(ODWReachability *)initWithReachabilityRef:(SCNetworkReachabilityRef)ref;
7375

0 commit comments

Comments
 (0)