1
1
//
2
- // AppAPI .swift
2
+ // MullvadAPIWrapper .swift
3
3
// MullvadVPNUITests
4
4
//
5
5
// Created by Niklas Berglund on 2024-01-18.
@@ -11,18 +11,27 @@ import XCTest
11
11
12
12
enum MullvadAPIError : Error {
13
13
case incorrectConfigurationFormat
14
+ case requestError
14
15
}
15
16
16
17
class MullvadAPIWrapper {
17
18
// swiftlint:disable force_cast
18
19
static let hostName = Bundle ( for: MullvadAPIWrapper . self)
19
20
. infoDictionary ? [ " ApiHostName " ] as! String
20
21
22
+ private var mullvadAPI : MullvadApi
23
+
21
24
/// API endpoint configuration value in the format <IP-address>:<port>
22
25
static let endpoint = Bundle ( for: MullvadAPIWrapper . self)
23
26
. infoDictionary ? [ " ApiEndpoint " ] as! String
24
27
// swiftlint:enable force_cast
25
28
29
+ init ( ) throws {
30
+ let apiAddress = try Self . getAPIIPAddress ( ) + " : " + Self. getAPIPort ( )
31
+ let hostname = Self . getAPIHostname ( )
32
+ mullvadAPI = try MullvadApi ( apiAddress: apiAddress, hostname: hostname)
33
+ }
34
+
26
35
public static func getAPIHostname( ) -> String {
27
36
return hostName
28
37
}
@@ -42,4 +51,52 @@ class MullvadAPIWrapper {
42
51
43
52
return port
44
53
}
54
+
55
+ /// Generate a mock WireGuard key
56
+ private func generateMockWireGuardKey( ) -> Data {
57
+ var bytes = [ UInt8] ( )
58
+
59
+ for _ in 0 ..< 44 {
60
+ bytes. append ( UInt8 . random ( in: 0 ..< 255 ) )
61
+ }
62
+
63
+ return Data ( bytes)
64
+ }
65
+
66
+ func createAccount( ) -> String {
67
+ do {
68
+ let accountNumber = try mullvadAPI. createAccount ( )
69
+ return accountNumber
70
+ } catch {
71
+ XCTFail ( " Failed to create account using app API " )
72
+ return String ( )
73
+ }
74
+ }
75
+
76
+ func deleteAccount( _ accountNumber: String ) {
77
+ do {
78
+ try mullvadAPI. delete ( account: accountNumber)
79
+ } catch {
80
+ XCTFail ( " Failed to delete account using app API " )
81
+ }
82
+ }
83
+
84
+ /// Add another device to specified account. A dummy WireGuard key will be generated.
85
+ func addDevice( _ account: String ) throws {
86
+ let devicePublicKey = generateMockWireGuardKey ( )
87
+
88
+ do {
89
+ try mullvadAPI. addDevice ( forAccount: account, publicKey: devicePublicKey)
90
+ } catch {
91
+ throw MullvadAPIError . requestError
92
+ }
93
+ }
94
+
95
+ func getAccountExpiry( _ account: String ) throws -> UInt64 {
96
+ do {
97
+ return try mullvadAPI. getExpiry ( forAccount: account)
98
+ } catch {
99
+ throw MullvadAPIError . requestError
100
+ }
101
+ }
45
102
}
0 commit comments