2
0

NetworkReachabilityManagerTests.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. @testable import Alamofire
  25. import Foundation
  26. import SystemConfiguration
  27. import XCTest
  28. final class NetworkReachabilityManagerTestCase: BaseTestCase {
  29. // MARK: - Tests - Initialization
  30. func testThatManagerCanBeInitializedFromHost() {
  31. // Given, When
  32. let manager = NetworkReachabilityManager(host: "localhost")
  33. // Then
  34. XCTAssertNotNil(manager)
  35. }
  36. func testThatManagerCanBeInitializedFromAddress() {
  37. // Given, When
  38. let manager = NetworkReachabilityManager()
  39. // Then
  40. XCTAssertNotNil(manager)
  41. }
  42. func testThatHostManagerIsReachableOnWiFi() {
  43. // Given, When
  44. let manager = NetworkReachabilityManager(host: "localhost")
  45. // Then
  46. XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
  47. XCTAssertEqual(manager?.isReachable, true)
  48. XCTAssertEqual(manager?.isReachableOnCellular, false)
  49. XCTAssertEqual(manager?.isReachableOnEthernetOrWiFi, true)
  50. }
  51. func testThatHostManagerStartsWithReachableStatus() {
  52. // Given, When
  53. let manager = NetworkReachabilityManager(host: "localhost")
  54. // Then
  55. XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
  56. XCTAssertEqual(manager?.isReachable, true)
  57. XCTAssertEqual(manager?.isReachableOnCellular, false)
  58. XCTAssertEqual(manager?.isReachableOnEthernetOrWiFi, true)
  59. }
  60. func testThatAddressManagerStartsWithReachableStatus() {
  61. // Given, When
  62. let manager = NetworkReachabilityManager()
  63. // Then
  64. XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
  65. XCTAssertEqual(manager?.isReachable, true)
  66. XCTAssertEqual(manager?.isReachableOnCellular, false)
  67. XCTAssertEqual(manager?.isReachableOnEthernetOrWiFi, true)
  68. }
  69. func testThatZeroManagerCanBeProperlyRestarted() {
  70. // Given
  71. let manager = NetworkReachabilityManager()
  72. let first = expectation(description: "first listener notified")
  73. let second = expectation(description: "second listener notified")
  74. // When
  75. manager?.startListening { _ in
  76. first.fulfill()
  77. }
  78. wait(for: [first], timeout: timeout)
  79. manager?.stopListening()
  80. manager?.startListening { _ in
  81. second.fulfill()
  82. }
  83. wait(for: [second], timeout: timeout)
  84. // Then
  85. XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
  86. }
  87. func testThatHostManagerCanBeProperlyRestarted() {
  88. // Given
  89. let manager = NetworkReachabilityManager(host: "localhost")
  90. let first = expectation(description: "first listener notified")
  91. let second = expectation(description: "second listener notified")
  92. // When
  93. manager?.startListening { _ in
  94. first.fulfill()
  95. }
  96. wait(for: [first], timeout: timeout)
  97. manager?.stopListening()
  98. manager?.startListening { _ in
  99. second.fulfill()
  100. }
  101. wait(for: [second], timeout: timeout)
  102. // Then
  103. XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
  104. }
  105. func testThatHostManagerCanBeDeinitialized() {
  106. // Given
  107. let expect = expectation(description: "reachability queue should clear")
  108. var manager: NetworkReachabilityManager? = NetworkReachabilityManager(host: "localhost")
  109. weak var weakManager = manager
  110. // When
  111. manager?.startListening(onUpdatePerforming: { _ in })
  112. manager?.stopListening()
  113. manager?.reachabilityQueue.async { expect.fulfill() }
  114. manager = nil
  115. waitForExpectations(timeout: timeout)
  116. // Then
  117. XCTAssertNil(manager, "strong reference should be nil")
  118. XCTAssertNil(weakManager, "weak reference should be nil")
  119. }
  120. func testThatAddressManagerCanBeDeinitialized() {
  121. // Given
  122. let expect = expectation(description: "reachability queue should clear")
  123. var manager: NetworkReachabilityManager? = NetworkReachabilityManager()
  124. weak var weakManager = manager
  125. // When
  126. manager?.startListening(onUpdatePerforming: { _ in })
  127. manager?.stopListening()
  128. manager?.reachabilityQueue.async { expect.fulfill() }
  129. manager = nil
  130. waitForExpectations(timeout: timeout)
  131. // Then
  132. XCTAssertNil(manager, "strong reference should be nil")
  133. XCTAssertNil(weakManager, "weak reference should be nil")
  134. }
  135. // MARK: - Listener
  136. func testThatHostManagerIsNotifiedWhenStartListeningIsCalled() {
  137. // Given
  138. guard let manager = NetworkReachabilityManager(host: "store.apple.com") else {
  139. XCTFail("manager should NOT be nil")
  140. return
  141. }
  142. let expectation = self.expectation(description: "listener closure should be executed")
  143. var networkReachabilityStatus: NetworkReachabilityManager.NetworkReachabilityStatus?
  144. // When
  145. manager.startListening { status in
  146. guard networkReachabilityStatus == nil else { return }
  147. networkReachabilityStatus = status
  148. expectation.fulfill()
  149. }
  150. waitForExpectations(timeout: timeout)
  151. // Then
  152. XCTAssertEqual(networkReachabilityStatus, .reachable(.ethernetOrWiFi))
  153. }
  154. func testThatAddressManagerIsNotifiedWhenStartListeningIsCalled() {
  155. // Given
  156. let manager = NetworkReachabilityManager()
  157. let expectation = self.expectation(description: "listener closure should be executed")
  158. var networkReachabilityStatus: NetworkReachabilityManager.NetworkReachabilityStatus?
  159. // When
  160. manager?.startListening { status in
  161. networkReachabilityStatus = status
  162. expectation.fulfill()
  163. }
  164. waitForExpectations(timeout: timeout)
  165. // Then
  166. XCTAssertEqual(networkReachabilityStatus, .reachable(.ethernetOrWiFi))
  167. }
  168. // MARK: - NetworkReachabilityStatus
  169. func testThatStatusIsNotReachableStatusWhenReachableFlagIsAbsent() {
  170. // Given
  171. let flags: SCNetworkReachabilityFlags = [.connectionOnDemand]
  172. // When
  173. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  174. // Then
  175. XCTAssertEqual(status, .notReachable)
  176. }
  177. func testThatStatusIsNotReachableStatusWhenConnectionIsRequired() {
  178. // Given
  179. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired]
  180. // When
  181. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  182. // Then
  183. XCTAssertEqual(status, .notReachable)
  184. }
  185. func testThatStatusIsNotReachableStatusWhenInterventionIsRequired() {
  186. // Given
  187. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .interventionRequired]
  188. // When
  189. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  190. // Then
  191. XCTAssertEqual(status, .notReachable)
  192. }
  193. func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsNotRequired() {
  194. // Given
  195. let flags: SCNetworkReachabilityFlags = [.reachable]
  196. // When
  197. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  198. // Then
  199. XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
  200. }
  201. func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsOnDemand() {
  202. // Given
  203. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .connectionOnDemand]
  204. // When
  205. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  206. // Then
  207. XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
  208. }
  209. func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsOnTraffic() {
  210. // Given
  211. let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .connectionOnTraffic]
  212. // When
  213. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  214. // Then
  215. XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
  216. }
  217. #if os(iOS) || os(tvOS)
  218. func testThatStatusIsReachableOnCellularStatusWhenIsWWAN() {
  219. // Given
  220. let flags: SCNetworkReachabilityFlags = [.reachable, .isWWAN]
  221. // When
  222. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  223. // Then
  224. XCTAssertEqual(status, .reachable(.cellular))
  225. }
  226. func testThatStatusIsNotReachableOnCellularStatusWhenIsWWANAndConnectionIsRequired() {
  227. // Given
  228. let flags: SCNetworkReachabilityFlags = [.reachable, .isWWAN, .connectionRequired]
  229. // When
  230. let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
  231. // Then
  232. XCTAssertEqual(status, .notReachable)
  233. }
  234. #endif
  235. }