-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathReinitUserCryptoTests.swift
More file actions
91 lines (75 loc) · 3.36 KB
/
Copy pathReinitUserCryptoTests.swift
File metadata and controls
91 lines (75 loc) · 3.36 KB
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
import BitwardenSdk
import XCTest
/// Swift integration tests for `CryptoClient.reinitUserCrypto`.
final class ReinitUserCryptoTests: XCTestCase {
var stateBridge: InMemoryStateBridge!
override func setUp() async throws {
try await super.setUp()
stateBridge = InMemoryStateBridge()
}
func testReturnsNotUnlockedWhenLocked() async throws {
// No `initializeUserCrypto` call — the user-key slot is empty.
let client = Client(tokenProvider: MockTokenProvider(), settings: nil)
client.kmStateBridge().registerBridgeImpl(bridgeImpl: stateBridge)
let req = ReinitUserCryptoRequest(
accountCryptographicState: makeV2AccountCryptographicState(),
upgradeToken: makeMockUpgradeToken()
)
do {
try await client.crypto().reinitUserCrypto(req: req)
XCTFail("expected ReinitUserCryptoError.NotUnlocked")
} catch BitwardenError.ReinitUserCrypto(let inner) {
guard case .NotUnlocked = inner else {
XCTFail("expected .NotUnlocked, got \(inner)")
return
}
}
}
func testReturnsAlreadyV2WhenActiveUserIsV2() async throws {
let client = try await makeV2InitializedClient(stateBridge: stateBridge)
let req = ReinitUserCryptoRequest(
accountCryptographicState: makeV2AccountCryptographicState(),
upgradeToken: makeMockUpgradeToken()
)
do {
try await client.crypto().reinitUserCrypto(req: req)
XCTFail("expected ReinitUserCryptoError.AlreadyV2Encryption")
} catch BitwardenError.ReinitUserCrypto(let inner) {
guard case .AlreadyV2Encryption = inner else {
XCTFail("expected .AlreadyV2Encryption, got \(inner)")
return
}
}
}
func testUpgradesV1ToV2WithValidToken() async throws {
let client = try await makeV1InitializedClient(stateBridge: stateBridge)
let upgradeToken = makeValidUpgradeToken()
await stateBridge.setV2UpgradeToken(value: upgradeToken)
let req = ReinitUserCryptoRequest(
accountCryptographicState: makeV2AccountCryptographicState(),
upgradeToken: upgradeToken
)
try await client.crypto().reinitUserCrypto(req: req)
// After a successful V1→V2 reinit, the active user key in the slot
// must be the V2 test-vector key (returned base64-encoded by
// `getUserEncryptionKey` for V2 keys via COSE serialization).
let userKey = try await client.crypto().getUserEncryptionKey()
XCTAssertEqual(userKey, TEST_VECTOR_USER_KEY_V2_B64)
}
func testInvalidUpgradeTokenReturnsError() async throws {
let client = try await makeV1InitializedClient(stateBridge: stateBridge)
let req = ReinitUserCryptoRequest(
accountCryptographicState: makeV2AccountCryptographicState(),
upgradeToken: makeMockUpgradeToken()
)
do {
try await client.crypto().reinitUserCrypto(req: req)
XCTFail("expected ReinitUserCryptoError.InvalidUpgradeToken")
} catch BitwardenError.ReinitUserCrypto(let inner) {
guard case .InvalidUpgradeToken = inner else {
XCTFail("expected .InvalidUpgradeToken, got \(inner)")
return
}
}
}
}