summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPNUITests/LeakTests.swift
blob: 804901d8d56656b7a6b2c3a99ce14410f089f280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
//  LeakTests.swift
//  MullvadVPNUITests
//
//  Created by Niklas Berglund on 2024-05-31.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import XCTest

class LeakTests: LoggedInWithTimeUITestCase {
    static let capturedStreamStartTimestamp: Double = 8
    static let capturedStreamEndTimestamp: Double = 3

    override func tearDown() {
        FirewallClient().removeRules()
        super.tearDown()
    }

    /// Send UDP traffic to a host, connect to relay and make sure - while connected to relay -
    /// that no leaked traffic went directly to the host
    func testConnectionStartedBeforeTunnelShouldNotLeakOutside() throws {
        let skipReason = """
            Connections started before the packet tunnel will leak as long as
            includeAllNetworks is not set to true when starting the tunnel.
            """
        try XCTSkipIf(true, skipReason)
        let targetIPAddress = Networking.getAlwaysReachableIPAddress()
        startPacketCapture()
        let trafficGenerator = TrafficGenerator(destinationHost: targetIPAddress, port: 80)
        trafficGenerator.startGeneratingUDPTraffic(interval: 1.0)

        TunnelControlPage(app)
            .tapConnectButton()

        allowAddVPNConfigurationsIfAsked()

        TunnelControlPage(app)
            .waitForConnectedLabel()

        // Keep the tunnel connection for a while
        RunLoop.current.run(until: .now + 30)

        TunnelControlPage(app)
            .tapDisconnectButton()

        trafficGenerator.stopGeneratingUDPTraffic()

        var capturedStreams = stopPacketCapture()
        // For now cut the beginning and and end of the stream to trim out the part where the tunnel connection was not up
        capturedStreams = PacketCaptureClient.trimPackets(
            streams: capturedStreams,
            secondsStart: Self.capturedStreamStartTimestamp,
            secondsEnd: Self.capturedStreamEndTimestamp
        )
        LeakCheck.assertNoLeaks(streams: capturedStreams, rules: [NoTrafficToHostLeakRule(host: targetIPAddress)])
    }

    /// 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
    func testTrafficCapturedOutsideOfTunnelShouldLeak() throws {
        let targetIPAddress = Networking.getAlwaysReachableIPAddress()
        startPacketCapture()
        let trafficGenerator = TrafficGenerator(destinationHost: targetIPAddress, port: 80)
        trafficGenerator.startGeneratingUDPTraffic(interval: 1.0)

        TunnelControlPage(app)
            .tapConnectButton()

        allowAddVPNConfigurationsIfAsked()

        TunnelControlPage(app)
            .waitForConnectedLabel()

        RunLoop.current.run(until: .now + 2)

        TunnelControlPage(app)
            .tapDisconnectButton()

        // Give it some time to generate traffic outside of tunnel
        RunLoop.current.run(until: .now + 5)

        TunnelControlPage(app)
            .tapConnectButton()

        // Keep the tunnel connection for a while
        RunLoop.current.run(until: .now + 5)

        TunnelControlPage(app)
            .tapDisconnectButton()

        // Keep the capture open for a while
        RunLoop.current.run(until: .now + 15)
        trafficGenerator.stopGeneratingUDPTraffic()

        var capturedStreams = stopPacketCapture()
        // For now cut the beginning and and end of the stream to trim out the part where the tunnel connection was not up
        capturedStreams = PacketCaptureClient.trimPackets(
            streams: capturedStreams,
            secondsStart: Self.capturedStreamStartTimestamp,
            secondsEnd: Self.capturedStreamEndTimestamp
        )
        LeakCheck.assertLeaks(streams: capturedStreams, rules: [NoTrafficToHostLeakRule(host: targetIPAddress)])
    }
}