NetworkReachabilityManagerTests.swift 9.1 KB

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