NetworkReachabilityManagerTests.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. func testThatHostManagerCanBeDeinitialized() {
  107. // Given
  108. let expect = expectation(description: "reachability queue should clear")
  109. var manager: NetworkReachabilityManager? = NetworkReachabilityManager(host: "localhost")
  110. weak var weakManager = manager
  111. // When
  112. manager?.startListening(onUpdatePerforming: { _ in })
  113. manager?.stopListening()
  114. manager?.reachabilityQueue.async { expect.fulfill() }
  115. manager = nil
  116. waitForExpectations(timeout: timeout)
  117. // Then
  118. XCTAssertNil(manager, "strong reference should be nil")
  119. XCTAssertNil(weakManager, "weak reference should be nil")
  120. }
  121. func testThatAddressManagerCanBeDeinitialized() {
  122. // Given
  123. let expect = expectation(description: "reachability queue should clear")
  124. var manager: NetworkReachabilityManager? = NetworkReachabilityManager()
  125. weak var weakManager = manager
  126. // When
  127. manager?.startListening(onUpdatePerforming: { _ in })
  128. manager?.stopListening()
  129. manager?.reachabilityQueue.async { expect.fulfill() }
  130. manager = nil
  131. waitForExpectations(timeout: timeout)
  132. // Then
  133. XCTAssertNil(manager, "strong reference should be nil")
  134. XCTAssertNil(weakManager, "weak reference should be nil")
  135. }
  136. // MARK: - Listener
  137. func testThatHostManagerIsNotifiedWhenStartListeningIsCalled() {
  138. // Given
  139. guard let manager = NetworkReachabilityManager(host: "store.apple.com") else {
  140. XCTFail("manager should NOT be nil")
  141. return
  142. }
  143. let expectation = expectation(description: "listener closure should be executed")
  144. var networkReachabilityStatus: NetworkReachabilityManager.NetworkReachabilityStatus?
  145. // When
  146. manager.startListening { status in
  147. guard networkReachabilityStatus == nil else { return }
  148. networkReachabilityStatus = status
  149. expectation.fulfill()
  150. }
  151. waitForExpectations(timeout: timeout)
  152. // Then
  153. XCTAssertEqual(networkReachabilityStatus, .reachable(.ethernetOrWiFi))
  154. }
  155. func testThatAddressManagerIsNotifiedWhenStartListeningIsCalled() {
  156. // Given
  157. let manager = NetworkReachabilityManager()
  158. let expectation = expectation(description: "listener closure should be executed")
  159. var networkReachabilityStatus: NetworkReachabilityManager.NetworkReachabilityStatus?
  160. // When
  161. manager?.startListening { status in
  162. networkReachabilityStatus = status
  163. expectation.fulfill()
  164. }
  165. waitForExpectations(timeout: timeout)
  166. // Then
  167. XCTAssertEqual(networkReachabilityStatus, .reachable(.ethernetOrWiFi))
  168. }
  169. // MARK: - NetworkReachabilityStatus
  170. func testThatStatusIsNotReachableStatusWhenReachableFlagIsAbsent() {
  171. // Given
  172. let flags: SCNetworkReachabilityFlags = [.connectionOnDemand]
  173. // When
  174. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  175. // Then
  176. XCTAssertEqual(status, .notReachable)
  177. }
  178. func testThatStatusIsNotReachableStatusWhenConnectionIsRequired() {
  179. // Given
  180. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired]
  181. // When
  182. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  183. // Then
  184. XCTAssertEqual(status, .notReachable)
  185. }
  186. func testThatStatusIsNotReachableStatusWhenInterventionIsRequired() {
  187. // Given
  188. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .interventionRequired]
  189. // When
  190. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  191. // Then
  192. XCTAssertEqual(status, .notReachable)
  193. }
  194. func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsNotRequired() {
  195. // Given
  196. let flags: SCNetworkReachabilityFlags = [.reachable]
  197. // When
  198. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  199. // Then
  200. XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
  201. }
  202. func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsOnDemand() {
  203. // Given
  204. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .connectionOnDemand]
  205. // When
  206. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  207. // Then
  208. XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
  209. }
  210. func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsOnTraffic() {
  211. // Given
  212. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .connectionOnTraffic]
  213. // When
  214. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  215. // Then
  216. XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
  217. }
  218. #if os(iOS) || os(tvOS)
  219. func testThatStatusIsReachableOnCellularStatusWhenIsWWAN() {
  220. // Given
  221. let flags: SCNetworkReachabilityFlags = [.reachable, .isWWAN]
  222. // When
  223. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  224. // Then
  225. XCTAssertEqual(status, .reachable(.cellular))
  226. }
  227. func testThatStatusIsNotReachableOnCellularStatusWhenIsWWANAndConnectionIsRequired() {
  228. // Given
  229. let flags: SCNetworkReachabilityFlags = [.reachable, .isWWAN, .connectionRequired]
  230. // When
  231. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  232. // Then
  233. XCTAssertEqual(status, .notReachable)
  234. }
  235. #endif
  236. }
  237. #endif