NetworkReachabilityManagerTests.swift 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // NetworkReachabilityManagerTests.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. #if canImport(SystemConfiguration)
  25. @testable import Alamofire
  26. import Foundation
  27. import SystemConfiguration
  28. import XCTest
  29. final class NetworkReachabilityManagerTestCase: BaseTestCase {
  30. // MARK: - Tests - Initialization
  31. func testThatManagerCanBeInitializedFromHost() {
  32. // Given, When
  33. let manager = NetworkReachabilityManager(host: "localhost")
  34. // Then
  35. XCTAssertNotNil(manager)
  36. }
  37. func testThatManagerCanBeInitializedFromAddress() {
  38. // Given, When
  39. let manager = NetworkReachabilityManager()
  40. // Then
  41. XCTAssertNotNil(manager)
  42. }
  43. func testThatHostManagerIsReachableOnWiFi() {
  44. // Given, When
  45. let manager = NetworkReachabilityManager(host: "localhost")
  46. // Then
  47. XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
  48. XCTAssertEqual(manager?.isReachable, true)
  49. XCTAssertEqual(manager?.isReachableOnCellular, false)
  50. XCTAssertEqual(manager?.isReachableOnEthernetOrWiFi, true)
  51. }
  52. func testThatHostManagerStartsWithReachableStatus() {
  53. // Given, When
  54. let manager = NetworkReachabilityManager(host: "localhost")
  55. // Then
  56. XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
  57. XCTAssertEqual(manager?.isReachable, true)
  58. XCTAssertEqual(manager?.isReachableOnCellular, false)
  59. XCTAssertEqual(manager?.isReachableOnEthernetOrWiFi, true)
  60. }
  61. func testThatAddressManagerStartsWithReachableStatus() {
  62. // Given, When
  63. let manager = NetworkReachabilityManager()
  64. // Then
  65. XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
  66. XCTAssertEqual(manager?.isReachable, true)
  67. XCTAssertEqual(manager?.isReachableOnCellular, false)
  68. XCTAssertEqual(manager?.isReachableOnEthernetOrWiFi, true)
  69. }
  70. func testThatZeroManagerCanBeProperlyRestarted() {
  71. // Given
  72. let manager = NetworkReachabilityManager()
  73. let first = expectation(description: "first listener notified")
  74. let second = expectation(description: "second listener notified")
  75. // When
  76. manager?.startListening { _ in
  77. first.fulfill()
  78. }
  79. wait(for: [first], timeout: timeout)
  80. manager?.stopListening()
  81. manager?.startListening { _ in
  82. second.fulfill()
  83. }
  84. wait(for: [second], timeout: timeout)
  85. // Then
  86. XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
  87. }
  88. func testThatHostManagerCanBeProperlyRestarted() {
  89. // Given
  90. let manager = NetworkReachabilityManager(host: "localhost")
  91. let first = expectation(description: "first listener notified")
  92. let second = expectation(description: "second listener notified")
  93. // When
  94. manager?.startListening { _ in
  95. first.fulfill()
  96. }
  97. wait(for: [first], timeout: timeout)
  98. manager?.stopListening()
  99. manager?.startListening { _ in
  100. second.fulfill()
  101. }
  102. wait(for: [second], timeout: timeout)
  103. // Then
  104. XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
  105. }
  106. @MainActor
  107. func testThatHostManagerCanBeDeinitialized() {
  108. // Given
  109. let expect = expectation(description: "reachability queue should clear")
  110. var manager: NetworkReachabilityManager? = NetworkReachabilityManager(host: "localhost")
  111. weak var weakManager = manager
  112. // When
  113. manager?.startListening(onUpdatePerforming: { _ in })
  114. manager?.stopListening()
  115. manager?.reachabilityQueue.async { expect.fulfill() }
  116. manager = nil
  117. waitForExpectations(timeout: timeout)
  118. // Then
  119. XCTAssertNil(manager, "strong reference should be nil")
  120. XCTAssertNil(weakManager, "weak reference should be nil")
  121. }
  122. @MainActor
  123. func testThatAddressManagerCanBeDeinitialized() {
  124. // Given
  125. let expect = expectation(description: "reachability queue should clear")
  126. var manager: NetworkReachabilityManager? = NetworkReachabilityManager()
  127. weak var weakManager = manager
  128. // When
  129. manager?.startListening(onUpdatePerforming: { _ in })
  130. manager?.stopListening()
  131. manager?.reachabilityQueue.async { expect.fulfill() }
  132. manager = nil
  133. waitForExpectations(timeout: timeout)
  134. // Then
  135. XCTAssertNil(manager, "strong reference should be nil")
  136. XCTAssertNil(weakManager, "weak reference should be nil")
  137. }
  138. // MARK: - Listener
  139. @MainActor
  140. func testThatHostManagerIsNotifiedWhenStartListeningIsCalled() {
  141. // Given
  142. guard let manager = NetworkReachabilityManager(host: "store.apple.com") else {
  143. XCTFail("manager should NOT be nil")
  144. return
  145. }
  146. let expectation = expectation(description: "listener closure should be executed")
  147. var networkReachabilityStatus: NetworkReachabilityManager.NetworkReachabilityStatus?
  148. // When
  149. manager.startListening { status in
  150. guard networkReachabilityStatus == nil else { return }
  151. networkReachabilityStatus = status
  152. expectation.fulfill()
  153. }
  154. waitForExpectations(timeout: timeout)
  155. // Then
  156. XCTAssertEqual(networkReachabilityStatus, .reachable(.ethernetOrWiFi))
  157. }
  158. @MainActor
  159. func testThatAddressManagerIsNotifiedWhenStartListeningIsCalled() {
  160. // Given
  161. let manager = NetworkReachabilityManager()
  162. let expectation = expectation(description: "listener closure should be executed")
  163. var networkReachabilityStatus: NetworkReachabilityManager.NetworkReachabilityStatus?
  164. // When
  165. manager?.startListening { status in
  166. networkReachabilityStatus = status
  167. expectation.fulfill()
  168. }
  169. waitForExpectations(timeout: timeout)
  170. // Then
  171. XCTAssertEqual(networkReachabilityStatus, .reachable(.ethernetOrWiFi))
  172. }
  173. // MARK: - NetworkReachabilityStatus
  174. func testThatStatusIsNotReachableStatusWhenReachableFlagIsAbsent() {
  175. // Given
  176. let flags: SCNetworkReachabilityFlags = [.connectionOnDemand]
  177. // When
  178. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  179. // Then
  180. XCTAssertEqual(status, .notReachable)
  181. }
  182. func testThatStatusIsNotReachableStatusWhenConnectionIsRequired() {
  183. // Given
  184. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired]
  185. // When
  186. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  187. // Then
  188. XCTAssertEqual(status, .notReachable)
  189. }
  190. func testThatStatusIsNotReachableStatusWhenInterventionIsRequired() {
  191. // Given
  192. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .interventionRequired]
  193. // When
  194. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  195. // Then
  196. XCTAssertEqual(status, .notReachable)
  197. }
  198. func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsNotRequired() {
  199. // Given
  200. let flags: SCNetworkReachabilityFlags = [.reachable]
  201. // When
  202. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  203. // Then
  204. XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
  205. }
  206. func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsOnDemand() {
  207. // Given
  208. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .connectionOnDemand]
  209. // When
  210. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  211. // Then
  212. XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
  213. }
  214. func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsOnTraffic() {
  215. // Given
  216. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .connectionOnTraffic]
  217. // When
  218. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  219. // Then
  220. XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
  221. }
  222. #if os(iOS) || os(tvOS)
  223. func testThatStatusIsReachableOnCellularStatusWhenIsWWAN() {
  224. // Given
  225. let flags: SCNetworkReachabilityFlags = [.reachable, .isWWAN]
  226. // When
  227. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  228. // Then
  229. XCTAssertEqual(status, .reachable(.cellular))
  230. }
  231. func testThatStatusIsNotReachableOnCellularStatusWhenIsWWANAndConnectionIsRequired() {
  232. // Given
  233. let flags: SCNetworkReachabilityFlags = [.reachable, .isWWAN, .connectionRequired]
  234. // When
  235. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  236. // Then
  237. XCTAssertEqual(status, .notReachable)
  238. }
  239. #endif
  240. }
  241. #endif