Skip to content

Commit 3ca2589

Browse files
committed
Remove LeakRule protocol for now, improve leak test names
1 parent cf93a46 commit 3ca2589

File tree

7 files changed

+38
-32
lines changed

7 files changed

+38
-32
lines changed

ios/MullvadVPN.xcodeproj/project.pbxproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -4276,14 +4276,14 @@
42764276
85557B0C2B591B0F00795FE1 /* Networking */ = {
42774277
isa = PBXGroup;
42784278
children = (
4279-
85607C882D131CCD00037E34 /* TestRouterAPIClient.swift */,
4280-
8555C65F2D102FFE0092DAD0 /* LeakCheck.swift */,
42814279
85557B0D2B591B2600795FE1 /* FirewallClient.swift */,
42824280
85557B0F2B59215F00795FE1 /* FirewallRule.swift */,
4281+
8555C65F2D102FFE0092DAD0 /* LeakCheck.swift */,
42834282
85557B132B5983CF00795FE1 /* MullvadAPIWrapper.swift */,
42844283
85E3BDE42B70E18C00FA71FD /* Networking.swift */,
4285-
856952DB2BD2922A008C1F84 /* PartnerAPIClient.swift */,
42864284
85978A532BE0F10E00F999A7 /* PacketCapture.swift */,
4285+
856952DB2BD2922A008C1F84 /* PartnerAPIClient.swift */,
4286+
85607C882D131CCD00037E34 /* TestRouterAPIClient.swift */,
42874287
8590A5432C2AF43400B9BF7B /* TrafficGenerator.swift */,
42884288
);
42894289
path = Networking;

ios/MullvadVPNUITests/Base/BaseUITestCase.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ class BaseUITestCase: XCTestCase {
227227
packetCaptureClient.stopCapture(session: packetCaptureSession)
228228
}
229229

230-
let pcap = packetCaptureClient.getPCAP(session: packetCaptureSession)
230+
let pcapFileContents = packetCaptureClient.getPCAP(session: packetCaptureSession)
231231
let parsedCapture = packetCaptureClient.getParsedCapture(session: packetCaptureSession)
232232
self.packetCaptureSession = nil
233233

234-
let pcapAttachment = XCTAttachment(data: pcap)
234+
let pcapAttachment = XCTAttachment(data: pcapFileContents)
235235
pcapAttachment.name = self.name + ".pcap"
236236
pcapAttachment.lifetime = .keepAlways
237237
self.add(pcapAttachment)

ios/MullvadVPNUITests/LeakTests.swift

+22-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@
99
import XCTest
1010

1111
class LeakTests: LoggedInWithTimeUITestCase {
12+
static let capturedStreamStartTimestamp: Double = 8
13+
static let capturedStreamEndTimestamp: Double = 3
14+
1215
override func tearDown() {
1316
FirewallClient().removeRules()
1417
super.tearDown()
1518
}
1619

17-
/// Send UDP traffic to a host, connect to relay and make sure while connected to relay no traffic leaked went directly to the host
18-
func testNoLeak() throws {
20+
/// Send UDP traffic to a host, connect to relay and make sure - while connected to relay -
21+
/// that no leaked traffic went directly to the host
22+
func testConnectionStartedBeforeTunnelShouldNotLeakOutside() throws {
23+
let skipReason = """
24+
Connections started before the packet tunnel will leak as long as
25+
includeAllNetworks is not set to true when starting the tunnel.
26+
"""
27+
try XCTSkipIf(true, skipReason)
1928
let targetIPAddress = Networking.getAlwaysReachableIPAddress()
2029
startPacketCapture()
2130
let trafficGenerator = TrafficGenerator(destinationHost: targetIPAddress, port: 80)
@@ -39,12 +48,16 @@ class LeakTests: LoggedInWithTimeUITestCase {
3948

4049
var capturedStreams = stopPacketCapture()
4150
// For now cut the beginning and and end of the stream to trim out the part where the tunnel connection was not up
42-
capturedStreams = PacketCaptureClient.trimPackets(streams: capturedStreams, secondsStart: 8, secondsEnd: 3)
51+
capturedStreams = PacketCaptureClient.trimPackets(
52+
streams: capturedStreams,
53+
secondsStart: Self.capturedStreamStartTimestamp,
54+
secondsEnd: Self.capturedStreamEndTimestamp
55+
)
4356
LeakCheck.assertNoLeaks(streams: capturedStreams, rules: [NoTrafficToHostLeakRule(host: targetIPAddress)])
4457
}
4558

4659
/// Send UDP traffic to a host, connect to relay and then disconnect to intentionally leak traffic and make sure that the test catches the leak
47-
func testShouldLeak() throws {
60+
func testTrafficCapturedOutsideOfTunnelShouldLeak() throws {
4861
let targetIPAddress = Networking.getAlwaysReachableIPAddress()
4962
startPacketCapture()
5063
let trafficGenerator = TrafficGenerator(destinationHost: targetIPAddress, port: 80)
@@ -72,7 +85,6 @@ class LeakTests: LoggedInWithTimeUITestCase {
7285
// Keep the tunnel connection for a while
7386
RunLoop.current.run(until: .now + 5)
7487

75-
app.launch()
7688
TunnelControlPage(app)
7789
.tapDisconnectButton()
7890

@@ -82,7 +94,11 @@ class LeakTests: LoggedInWithTimeUITestCase {
8294

8395
var capturedStreams = stopPacketCapture()
8496
// For now cut the beginning and and end of the stream to trim out the part where the tunnel connection was not up
85-
capturedStreams = PacketCaptureClient.trimPackets(streams: capturedStreams, secondsStart: 8, secondsEnd: 3)
97+
capturedStreams = PacketCaptureClient.trimPackets(
98+
streams: capturedStreams,
99+
secondsStart: Self.capturedStreamStartTimestamp,
100+
secondsEnd: Self.capturedStreamEndTimestamp
101+
)
86102
LeakCheck.assertLeaks(streams: capturedStreams, rules: [NoTrafficToHostLeakRule(host: targetIPAddress)])
87103
}
88104
}

ios/MullvadVPNUITests/Networking/LeakCheck.swift

+5-9
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,26 @@
99
import XCTest
1010

1111
class LeakCheck {
12-
static func assertNoLeaks(streams: [Stream], rules: [LeakRule]) {
12+
static func assertNoLeaks(streams: [Stream], rules: [NoTrafficToHostLeakRule]) {
1313
XCTAssertFalse(streams.isEmpty, "No streams to leak check")
1414
XCTAssertFalse(rules.isEmpty, "No leak rules to check")
1515

1616
for rule in rules where rule.isViolated(streams: streams) {
17-
XCTFail("Leak rule violated")
17+
XCTFail("Leaked traffic destined to \(rule.host) outside of the tunnel connection")
1818
}
1919
}
2020

21-
static func assertLeaks(streams: [Stream], rules: [LeakRule]) {
21+
static func assertLeaks(streams: [Stream], rules: [NoTrafficToHostLeakRule]) {
2222
XCTAssertFalse(streams.isEmpty, "No streams to leak check")
2323
XCTAssertFalse(rules.isEmpty, "No leak rules to check")
2424

2525
for rule in rules where rule.isViolated(streams: streams) == false {
26-
XCTFail("Leak rule unexpectedly not violated when asserting leak")
26+
XCTFail("Expected to leak traffic to \(rule.host) outside of tunnel")
2727
}
2828
}
2929
}
3030

31-
protocol LeakRule {
32-
func isViolated(streams: [Stream]) -> Bool
33-
}
34-
35-
class NoTrafficToHostLeakRule: LeakRule {
31+
class NoTrafficToHostLeakRule {
3632
let host: String
3733

3834
init(host: String) {

ios/MullvadVPNUITests/Networking/PacketCapture.swift

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ class PacketCaptureClient: TestRouterAPIClient {
164164
var collectionStartDate: Date?
165165
var collectionEndDate: Date?
166166

167+
XCTAssertTrue(streams.count >= 1, "Captured streams are empty, expected at least 1")
168+
167169
for stream in streams {
168170
if collectionStartDate != nil {
169171
collectionStartDate = min(collectionStartDate!, stream.dateInterval.start)

ios/MullvadVPNUITests/Networking/TestRouterAPIClient.swift

+2-10
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@
99
import XCTest
1010

1111
class TestRouterAPIClient {
12-
// swiftlint:disable force_cast
13-
static let baseURL = URL(
14-
string:
15-
Bundle(for: FirewallClient.self).infoDictionary?["FirewallApiBaseURL"] as! String
16-
)!
17-
// swiftlint:enable force_cast
18-
19-
static func getIPAddress() throws -> String {
20-
return ""
21-
}
12+
// swiftlint:disable:next force_cast
13+
static let baseURL = URL(string: Bundle(for: FirewallClient.self).infoDictionary?["FirewallApiBaseURL"] as! String)!
2214

2315
/// Gets the IP address of the device under test
2416
public func getDeviceIPAddress() throws -> String {

ios/MullvadVPNUITests/Networking/TrafficGenerator.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ class TrafficGenerator {
3535
print("Attempting to reconnect")
3636
connection.forceCancel()
3737

38-
connection = recreateConnection()
38+
connection = createConnection()
3939
setupConnection()
4040
setupOtherHandlers()
4141
}
4242

43-
func recreateConnection() -> NWConnection {
43+
func createConnection() -> NWConnection {
4444
let params = NWParameters.udp
4545
return NWConnection(
4646
host: NWEndpoint.Host(destinationHost),

0 commit comments

Comments
 (0)