1
1
//
2
- // AppAPI .swift
2
+ // MullvadAPIWrapper .swift
3
3
// MullvadVPNUITests
4
4
//
5
5
// Created by Niklas Berglund on 2024-01-18.
@@ -10,36 +10,89 @@ import Foundation
10
10
import XCTest
11
11
12
12
enum MullvadAPIError : Error {
13
- case incorrectConfigurationFormat
13
+ case invalidEndpointFormatError
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
26
- public static func getAPIHostname( ) -> String {
27
- return hostName
29
+ init ( ) throws {
30
+ let apiAddress = try Self . getAPIIPAddress ( ) + " : " + Self. getAPIPort ( )
31
+ let hostname = Self . hostName
32
+ mullvadAPI = try MullvadApi ( apiAddress: apiAddress, hostname: hostname)
28
33
}
29
34
30
35
public static func getAPIIPAddress( ) throws -> String {
31
36
guard let ipAddress = endpoint. components ( separatedBy: " : " ) . first else {
32
- throw MullvadAPIError . incorrectConfigurationFormat
37
+ throw MullvadAPIError . invalidEndpointFormatError
33
38
}
34
39
35
40
return ipAddress
36
41
}
37
42
38
43
public static func getAPIPort( ) throws -> String {
39
44
guard let port = endpoint. components ( separatedBy: " : " ) . last else {
40
- throw MullvadAPIError . incorrectConfigurationFormat
45
+ throw MullvadAPIError . invalidEndpointFormatError
41
46
}
42
47
43
48
return port
44
49
}
50
+
51
+ /// Generate a mock WireGuard key
52
+ private func generateMockWireGuardKey( ) -> Data {
53
+ var bytes = [ UInt8] ( )
54
+
55
+ for _ in 0 ..< 44 {
56
+ bytes. append ( UInt8 . random ( in: 0 ..< 255 ) )
57
+ }
58
+
59
+ return Data ( bytes)
60
+ }
61
+
62
+ func createAccount( ) -> String {
63
+ do {
64
+ let accountNumber = try mullvadAPI. createAccount ( )
65
+ return accountNumber
66
+ } catch {
67
+ XCTFail ( " Failed to create account using app API " )
68
+ return String ( )
69
+ }
70
+ }
71
+
72
+ func deleteAccount( _ accountNumber: String ) {
73
+ do {
74
+ try mullvadAPI. delete ( account: accountNumber)
75
+ } catch {
76
+ XCTFail ( " Failed to delete account using app API " )
77
+ }
78
+ }
79
+
80
+ /// Add another device to specified account. A dummy WireGuard key will be generated.
81
+ func addDevice( _ account: String ) throws {
82
+ let devicePublicKey = generateMockWireGuardKey ( )
83
+
84
+ do {
85
+ try mullvadAPI. addDevice ( forAccount: account, publicKey: devicePublicKey)
86
+ } catch {
87
+ throw MullvadAPIError . requestError
88
+ }
89
+ }
90
+
91
+ func getAccountExpiry( _ account: String ) throws -> UInt64 {
92
+ do {
93
+ return try mullvadAPI. getExpiry ( forAccount: account)
94
+ } catch {
95
+ throw MullvadAPIError . requestError
96
+ }
97
+ }
45
98
}
0 commit comments