summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadRESTTests/ShadowsocksCacheCleanerTests.swift
blob: abd110759a3cf2f2a65c2abbac6de7769e7c86ca (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
//
//  ShadowsocksCacheCleanerTests.swift
//  MullvadRESTTests
//
//  Created by Marco Nikic on 2025-09-18.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Network
import Testing

@testable import MullvadREST
@testable import MullvadSettings
@testable import MullvadTypes

actor ShadowsocksCacheCleanerTests {
    var cache = ShadowsocksCacheStub(
        configuration:
            ShadowsocksConfiguration(
                address: .ipv4(IPv4Address.loopback),
                port: 1234,
                password: "password",
                cipher: "chacha20"
            )
    )

    deinit {
        cache.onRead = nil
        cache.onWrite = nil
        cache.onClear = nil
    }

    @Test func storesLastAccessMethodUUID() async throws {
        let cacheCleaner = ShadowsocksCacheCleaner(cache: cache)
        let newMethodUUID = UUID()

        cacheCleaner.accessMethodChangedTo(newMethodUUID)
        #expect(newMethodUUID == cacheCleaner.lastChangedUUID)
    }

    @Test func clearsCacheWhenPreviousChangeWasShadowsocksUUID() async throws {
        let bridges = AccessMethodRepository.bridgeId
        let direct = AccessMethodRepository.directId

        await confirmation("Did clear cache") { didClearCache in
            cache.onClear = {
                didClearCache()
            }
            let cacheCleaner = ShadowsocksCacheCleaner(cache: cache)
            cacheCleaner.accessMethodChangedTo(bridges)
            cacheCleaner.accessMethodChangedTo(direct)
        }
    }

    @Test func doesNotClearCacheWhenOtherMethodsChange() async throws {
        let encryptedDNS = AccessMethodRepository.encryptedDNSId
        let direct = AccessMethodRepository.directId

        await confirmation("Did clear cache", expectedCount: 0) { didClearCache in
            let cacheCleaner = ShadowsocksCacheCleaner(cache: cache)
            cache.onClear = {
                didClearCache()
            }
            cacheCleaner.accessMethodChangedTo(encryptedDNS)
            cacheCleaner.accessMethodChangedTo(direct)
        }
    }
}

struct ShadowsocksCacheStub: ShadowsocksConfigurationCacheProtocol {
    let configuration: ShadowsocksConfiguration

    var onRead: (@Sendable () -> Void)?
    var onWrite: (@Sendable () -> Void)?
    var onClear: (@Sendable () -> Void)?

    func read() throws -> ShadowsocksConfiguration {
        onRead?()
        return configuration
    }

    func write(_ configuration: ShadowsocksConfiguration) throws {
        onWrite?()
    }

    func clear() throws {
        onClear?()
    }
}