summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBug Magnet <marco.nikic@mullvad.net>2024-11-25 17:17:43 +0100
committerBug Magnet <marco.nikic@mullvad.net>2024-11-27 10:45:06 +0100
commit12174a710c9f3b103e52384a7fbe50351bf7ad8f (patch)
tree081a2a624d63654821122c0c9c1cf43f29ac00be
parent4ec823039f98697b7ae4dc1debbe02ceff988ce0 (diff)
downloadmullvadvpn-12174a710c9f3b103e52384a7fbe50351bf7ad8f.tar.xz
mullvadvpn-12174a710c9f3b103e52384a7fbe50351bf7ad8f.zip
Use the Firewall API to get the device IP address
-rw-r--r--ios/MullvadVPNUITests/Networking/FirewallAPIClient.swift34
-rw-r--r--ios/MullvadVPNUITests/Networking/FirewallRule.swift6
-rw-r--r--ios/MullvadVPNUITests/Networking/Networking.swift46
3 files changed, 37 insertions, 49 deletions
diff --git a/ios/MullvadVPNUITests/Networking/FirewallAPIClient.swift b/ios/MullvadVPNUITests/Networking/FirewallAPIClient.swift
index b53d0b15e0..da7f2482ac 100644
--- a/ios/MullvadVPNUITests/Networking/FirewallAPIClient.swift
+++ b/ios/MullvadVPNUITests/Networking/FirewallAPIClient.swift
@@ -77,6 +77,40 @@ class FirewallAPIClient {
}
}
+ /// Gets the IP address of the device under test
+ public func getDeviceIPAddress() throws -> String {
+ let deviceIPURL = baseURL.appendingPathComponent("own-ip")
+ let request = URLRequest(url: deviceIPURL)
+ let completionHandlerInvokedExpectation = XCTestExpectation(
+ description: "Completion handler for the request is invoked"
+ )
+ var deviceIPAddress = ""
+ var requestError: Error?
+
+ let dataTask = URLSession.shared.dataTask(with: request) { data, _, _ in
+ defer { completionHandlerInvokedExpectation.fulfill() }
+ guard let data else {
+ requestError = NetworkingError.internalError(reason: "Could not get device IP")
+ return
+ }
+
+ deviceIPAddress = String(data: data, encoding: .utf8)!
+ }
+
+ dataTask.resume()
+
+ let waitResult = XCTWaiter.wait(for: [completionHandlerInvokedExpectation], timeout: 30)
+ if waitResult != .completed {
+ XCTFail("Failed to get device IP address - timeout")
+ }
+
+ if let requestError {
+ throw requestError
+ }
+
+ return deviceIPAddress
+ }
+
/// Remove all firewall rules associated to this device under test
public func removeRules() {
let removeRulesURL = baseURL.appendingPathComponent("remove-rules/\(sessionIdentifier)")
diff --git a/ios/MullvadVPNUITests/Networking/FirewallRule.swift b/ios/MullvadVPNUITests/Networking/FirewallRule.swift
index 84f9a56820..02f1811e87 100644
--- a/ios/MullvadVPNUITests/Networking/FirewallRule.swift
+++ b/ios/MullvadVPNUITests/Networking/FirewallRule.swift
@@ -36,7 +36,7 @@ struct FirewallRule {
/// Make a firewall rule blocking API access for the current device under test
public static func makeBlockAPIAccessFirewallRule() throws -> FirewallRule {
- let deviceIPAddress = try Networking.getIPAddress()
+ let deviceIPAddress = try FirewallAPIClient().getDeviceIPAddress()
let apiIPAddress = try MullvadAPIWrapper.getAPIIPAddress()
return FirewallRule(
fromIPAddress: deviceIPAddress,
@@ -46,7 +46,7 @@ struct FirewallRule {
}
public static func makeBlockAllTrafficRule(toIPAddress: String) throws -> FirewallRule {
- let deviceIPAddress = try Networking.getIPAddress()
+ let deviceIPAddress = try FirewallAPIClient().getDeviceIPAddress()
return FirewallRule(
fromIPAddress: deviceIPAddress,
@@ -56,7 +56,7 @@ struct FirewallRule {
}
public static func makeBlockUDPTrafficRule(toIPAddress: String) throws -> FirewallRule {
- let deviceIPAddress = try Networking.getIPAddress()
+ let deviceIPAddress = try FirewallAPIClient().getDeviceIPAddress()
return FirewallRule(
fromIPAddress: deviceIPAddress,
diff --git a/ios/MullvadVPNUITests/Networking/Networking.swift b/ios/MullvadVPNUITests/Networking/Networking.swift
index 42bac4a243..9e00f48d02 100644
--- a/ios/MullvadVPNUITests/Networking/Networking.swift
+++ b/ios/MullvadVPNUITests/Networking/Networking.swift
@@ -22,52 +22,6 @@ struct DNSServerEntry: Decodable {
/// Class with methods for verifying network connectivity
class Networking {
- /// Get IP address of the iOS device under test
- static func getIPAddress() throws -> String {
- var ipAddress: String
- // Get list of all interfaces on the local machine:
- var interfaceList: UnsafeMutablePointer<ifaddrs>?
- guard getifaddrs(&interfaceList) == 0, let firstInterfaceAddress = interfaceList else {
- throw NetworkingError.internalError(reason: "Failed to locate local networking interface")
- }
-
- // For each interface
- for interfacePointer in sequence(first: firstInterfaceAddress, next: { $0.pointee.ifa_next }) {
- let flags = Int32(interfacePointer.pointee.ifa_flags)
- let interfaceAddress = interfacePointer.pointee.ifa_addr.pointee
-
- // Check for running IPv4 interfaces. Skip the loopback interface.
- if (
- flags &
- (IFF_UP | IFF_RUNNING | IFF_LOOPBACK)
- ) == (IFF_UP | IFF_RUNNING),
- interfaceAddress.sa_family == UInt8(AF_INET) {
- // Check if interface is en0 which is the WiFi connection on the iPhone
- let name = String(cString: interfacePointer.pointee.ifa_name)
- // Convert interface address to a human readable string:
- var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
- if getnameinfo(
- interfacePointer.pointee.ifa_addr,
- socklen_t(interfaceAddress.sa_len),
- &hostname,
- socklen_t(hostname.count),
- nil,
- socklen_t(0),
- NI_NUMERICHOST
- ) == 0 {
- ipAddress = String(cString: hostname)
- if ipAddress.starts(with: "192.168") {
- return ipAddress
- }
- }
- }
- }
-
- freeifaddrs(interfaceList)
-
- throw NetworkingError.internalError(reason: "No local IP found")
- }
-
/// Get configured ad serving domain
private static func getAdServingDomain() throws -> String {
guard let adServingDomain = Bundle(for: Networking.self)