Skip to content

Commit

Permalink
Add custom DNS test
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasberglund authored and buggmagnet committed Apr 8, 2024
1 parent 4c8730b commit daa31da
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
61 changes: 61 additions & 0 deletions ios/MullvadVPNUITests/Networking/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ enum NetworkingError: Error {
case internalError(reason: String)
}

struct DNSServerEntry: Decodable {
let organization: String
let mullvad_dns: Bool
}

/// Class with methods for verifying network connectivity
class Networking {
/// Get IP address of the iOS device under test
Expand Down Expand Up @@ -165,4 +170,60 @@ class Networking {
public static func verifyCannotReachAdServingDomain() throws {
XCTAssertFalse(try Self.canConnectSocket(host: try Self.getAdServingDomain(), port: "80"))
}

/// Verify that the expected DNS server is used by verifying provider name and whether it is a Mullvad DNS server or not
public static func verifyDNSServerProvider(_ providerName: String, isMullvad: Bool) throws {
guard let mullvadDNSLeakURL = URL(string: "https://am.i.mullvad.net/dnsleak") else {
throw NetworkingError.internalError(reason: "Failed to create URL object")
}

var request = URLRequest(url: mullvadDNSLeakURL)
request.setValue("application/json", forHTTPHeaderField: "accept")

var requestData: Data?
var requestResponse: URLResponse?
var requestError: Error?
let completionHandlerInvokedExpectation = XCTestExpectation(
description: "Completion handler for the request is invoked"
)

do {
let dataTask = URLSession.shared.dataTask(with: request) { data, response, error in
requestData = data
requestResponse = response
requestError = error
completionHandlerInvokedExpectation.fulfill()
}

dataTask.resume()

let waitResult = XCTWaiter.wait(for: [completionHandlerInvokedExpectation], timeout: 30)

if waitResult != .completed {
XCTFail("Failed to verify DNS server provider - timeout")
} else {
if let response = requestResponse as? HTTPURLResponse {
if response.statusCode != 200 {
XCTFail("Failed to verify DNS server provider - unexpected server response")
}
}

if let error = requestError {
XCTFail("Failed to verify DNS server provider - encountered error \(error.localizedDescription)")
}

if let requestData = requestData {
let dnsServerEntries = try JSONDecoder().decode([DNSServerEntry].self, from: requestData)
XCTAssertGreaterThanOrEqual(dnsServerEntries.count, 1)

for dnsServerEntry in dnsServerEntries {
XCTAssertEqual(dnsServerEntry.organization, providerName)
XCTAssertEqual(dnsServerEntry.mullvad_dns, isMullvad)
}
}
}
} catch {
XCTFail("Failed to verify DNS server provider - couldn't serialize JSON")
}
}
}
30 changes: 30 additions & 0 deletions ios/MullvadVPNUITests/RelayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,34 @@ class RelayTests: LoggedInWithTimeUITestCase {

return relayIPAddress
}

func testCustomDNS() throws {
let dnsServerIPAddress = "8.8.8.8"
let dnsServerProviderName = "GOOGLE"

TunnelControlPage(app)
.tapSecureConnectionButton()

allowAddVPNConfigurations()

HeaderBar(app)
.tapSettingsButton()

SettingsPage(app)
.tapVPNSettingsCell()

VPNSettingsPage(app)
.tapDNSSettingsCell()

DNSSettingsPage(app)
.tapEditButton()
.tapAddAServer()
.tapEnterIPAddressTextField()
.enterText(dnsServerIPAddress)
.dismissKeyboard()
.tapUseCustomDNSSwitch()
.tapDoneButton()

try Networking.verifyDNSServerProvider(dnsServerProviderName, isMullvad: false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class BaseUITestCase: XCTestCase {

func logoutIfLoggedIn() {
if isLoggedIn() {
// First dismiss settings modal if presented
if isPresentingSettings() {
SettingsPage(app)
.swipeDownToDismissModal()
Expand Down

0 comments on commit daa31da

Please sign in to comment.