Update tests for API changes

This commit is contained in:
Samuel Hammersberg authored and Emīls committed 2026-09-28 22:54:23 +02:00
1 parent cc2de98c7c
commit 5babed8c67
6 files changed
+84 -68

No files matched your search

@@ -8,34 +8,60 @@
//
// SPDX-License-Identifier: GPL-3.0-only
import Network
import XCTest
@testable import MullvadMockData
@testable import MullvadREST
@testable import MullvadRustRuntime
@testable import MullvadTypes
class DefaultLocationServiceTests: XCTestCase {
private let encoder = JSONEncoder()
func testFetchCurrentLocationIdentifier() async throws {
let mockData = try encoder.encode(
REST.ServerLocation(
country: "USA",
city: "Dallas, TX",
latitude: 32.89748,
longitude: -97.040443
)
let mockData = AmIMullvadResponse(
ip: .ipv4(.loopback),
country: "USA",
city: "Dallas, TX",
latitude: 32.89748,
longitude: -97.040443,
mullvadExitIp: false,
)
let locationService = DefaultLocationService(
urlSession: URLSessionStub(
response: (mockData, URLResponse())
),
relayCache: try MockRelayCache().read()
relayCache: try MockRelayCache().read(),
apiContext: try makeApiContext(),
)
let identifier = try await locationService.fetchCurrentLocationIdentifier()
let identifier = locationService.getLocation(mockData)
XCTAssertEqual(identifier?.country, "us")
XCTAssertEqual(identifier?.city, "dal")
}
private func makeApiContext() throws -> ApiContext {
let shadowsocksLoader = ShadowsocksLoaderStub(
configuration: ShadowsocksConfiguration(
address: .ipv4(.loopback),
port: 1080,
password: "123",
cipher: "aes-128-cfb"
))
let accessMethodsRepository = AccessMethodRepositoryStub.stub
return ApiContext(
host: "localhost",
address: REST.defaultAPIEndpoint.description,
amIMullvadHostIpv4: "ipv4.\(REST.amIMullvadHostname)",
amIMullvadHostIpv6: "ipv6.\(REST.amIMullvadHostname)",
domain: REST.encryptedDNSHostname,
domainFronting: DomainFrontingConfig(front: "", proxyHost: ""),
disableTls: true,
shadowsocksProvider: shadowsocksLoader,
accessMethodWrapper: initAccessMethodSettingsWrapper(methods: accessMethodsRepository.fetchAll()),
accessMethodChangeListeners: []
)
}
}
@@ -46,6 +46,8 @@ class MullvadApiTests: XCTestCase {
let context = ApiContext(
host: "localhost",
address: "\(IPv4Address.loopback.debugDescription):\(port)",
amIMullvadHostIpv4: "ipv4.\(REST.amIMullvadHostname)",
amIMullvadHostIpv6: "ipv6.\(REST.amIMullvadHostname)",
domain: REST.encryptedDNSHostname,
domainFronting: DomainFrontingConfig(front: "", proxyHost: ""),
disableTls: true,
@@ -148,6 +148,8 @@ class RelayListCacheTests: XCTestCase {
let context = ApiContext(
host: "localhost",
address: "\(IPv4Address.loopback.debugDescription):\(port)",
amIMullvadHostIpv4: "ipv4.\(REST.amIMullvadHostname)",
amIMullvadHostIpv6: "ipv6.\(REST.amIMullvadHostname)",
domain: REST.encryptedDNSHostname,
domainFronting: DomainFrontingConfig(front: "", proxyHost: ""),
disableTls: true,
@@ -8,50 +8,29 @@
//
// SPDX-License-Identifier: GPL-3.0-only
import Network
import XCTest
@testable import MullvadMockData
@testable import MullvadREST
@testable import MullvadRustRuntime
@testable import MullvadSettings
@testable import MullvadTypes
final class OutgoingConnectionProxyTests: XCTestCase {
private var mockIPV6ConnectionData: Data!
private var mockIPV4ConnectionData: Data!
private let hostname = "mullvad.net"
private let encoder = JSONEncoder()
override func setUpWithError() throws {
mockIPV4ConnectionData = try encoder.encode(IPV4ConnectionData.mock)
mockIPV6ConnectionData = try encoder.encode(IPV6ConnectionData.mock)
}
override func tearDownWithError() throws {
mockIPV4ConnectionData.removeAll()
mockIPV6ConnectionData.removeAll()
}
func testSuccessGettingIPV4() async throws {
let iPv4Expectation = expectation(description: "Did receive IPv4")
let outgoingConnectionProxy = OutgoingConnectionProxy(
urlSession: URLSessionStub(
response: (mockIPV4ConnectionData, createHTTPURLResponse(ip: .v4, statusCode: 200))
), hostname: hostname)
apiContext: try makeApiContext(disableTls: false)
)
let result = try await outgoingConnectionProxy.getIPV4(retryStrategy: .noRetry)
if result.ip == IPV4ConnectionData.mock.ip {
iPv4Expectation.fulfill()
}
await fulfillment(of: [iPv4Expectation], timeout: .UnitTest.timeout)
_ = try await outgoingConnectionProxy.getIPV4(retryStrategy: .noRetry)
}
func testFailureGettingIPV4() async throws {
let noIPv4Expectation = expectation(description: "Did not receive IPv4")
let outgoingConnectionProxy = OutgoingConnectionProxy(
urlSession: URLSessionStub(
response: (Data(), createHTTPURLResponse(ip: .v4, statusCode: 503))
), hostname: hostname)
apiContext: try makeApiContext(disableTls: true))
await XCTAssertThrowsErrorAsync(try await outgoingConnectionProxy.getIPV4(retryStrategy: .noRetry)) { _ in
noIPv4Expectation.fulfill()
@@ -59,29 +38,19 @@ final class OutgoingConnectionProxyTests: XCTestCase {
await fulfillment(of: [noIPv4Expectation], timeout: .UnitTest.timeout)
}
func testSuccessGettingIPV6() async throws {
let ipv6Expectation = expectation(description: "Did receive IPv6")
// func testSuccessGettingIPV6() async throws {
// let outgoingConnectionProxy = OutgoingConnectionProxy(
// apiContext: try makeApiContext(disableTls: false)
// )
let outgoingConnectionProxy = OutgoingConnectionProxy(
urlSession: URLSessionStub(
response: (mockIPV6ConnectionData, createHTTPURLResponse(ip: .v6, statusCode: 200))
), hostname: hostname)
let result = try await outgoingConnectionProxy.getIPV6(retryStrategy: .noRetry)
if result.ip == IPV6ConnectionData.mock.ip {
ipv6Expectation.fulfill()
}
await fulfillment(of: [ipv6Expectation], timeout: .UnitTest.timeout)
}
// _ = try await outgoingConnectionProxy.getIPV6(retryStrategy: .noRetry)
// }
func testFailureGettingIPV6() async throws {
let noIPv6Expectation = expectation(description: "Did not receive IPv6")
let outgoingConnectionProxy = OutgoingConnectionProxy(
urlSession: URLSessionStub(
response: (mockIPV6ConnectionData, createHTTPURLResponse(ip: .v6, statusCode: 404))
), hostname: hostname)
apiContext: try makeApiContext(disableTls: true))
await XCTAssertThrowsErrorAsync(try await outgoingConnectionProxy.getIPV6(retryStrategy: .noRetry)) { _ in
noIPv6Expectation.fulfill()
@@ -90,13 +59,26 @@ final class OutgoingConnectionProxyTests: XCTestCase {
}
}
extension OutgoingConnectionProxyTests {
private func createHTTPURLResponse(ip: OutgoingConnectionProxy.ExitIPVersion, statusCode: Int) -> HTTPURLResponse {
return HTTPURLResponse(
url: URL(string: "https://\(ip.host(hostname: hostname))/json")!,
statusCode: statusCode,
httpVersion: nil,
headerFields: ["Content-Type": "application/json"]
)!
}
private func makeApiContext(disableTls: Bool) throws -> ApiContext {
let shadowsocksLoader = ShadowsocksLoader(
cache: ShadowsocksConfigurationCacheStub(),
relaySelector: ShadowsocksRelaySelectorStub(relays: .mock()),
tunnelSettings: LatestTunnelSettings(),
settingsUpdater: SettingsUpdater(listener: TunnelSettingsListener())
)
let accessMethodsRepository = AccessMethodRepositoryStub.stub
return ApiContext(
host: "localhost",
address: REST.defaultAPIEndpoint.description,
amIMullvadHostIpv4: "ipv4.\(REST.amIMullvadHostname)",
amIMullvadHostIpv6: "ipv6.\(REST.amIMullvadHostname)",
domain: REST.encryptedDNSHostname,
domainFronting: DomainFrontingConfig(front: "", proxyHost: ""),
disableTls: disableTls,
shadowsocksProvider: shadowsocksLoader,
accessMethodWrapper: initAccessMethodSettingsWrapper(methods: accessMethodsRepository.fetchAll()),
accessMethodChangeListeners: []
)
}
@@ -46,6 +46,8 @@ class TunnelManagerTests: XCTestCase {
apiContext = ApiContext(
host: REST.defaultAPIHostname,
address: REST.defaultAPIEndpoint.description,
amIMullvadHostIpv4: "ipv4.\(REST.amIMullvadHostname)",
amIMullvadHostIpv6: "ipv6.\(REST.amIMullvadHostname)",
domain: REST.encryptedDNSHostname,
domainFronting: REST.domainFronting,
disableTls: false,
+2
View File
@@ -76,6 +76,8 @@ class MullvadApi {
context = ApiContext(
host: hostname,
address: apiAddress,
amIMullvadHostIpv4: "ipv4.\(REST.amIMullvadHostname)",
amIMullvadHostIpv6: "ipv4.\(REST.amIMullvadHostname)",
domain: hostname,
domainFronting: DomainFrontingConfig(front: "", proxyHost: ""),
disableTls: false,