|
| 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 | + |
0 commit comments