Skip to content

Commit 91d616e

Browse files
committed
pm-29777 Add tests for new extension
1 parent 0235cfe commit 91d616e

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

app/src/main/kotlin/com/x8bit/bitwarden/data/vault/repository/util/WrappedAccountCryptographicStateExtensions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fun createWrappedAccountCryptographicState(
1919
signingKey: String?,
2020
signedPublicKey: String?,
2121
): WrappedAccountCryptographicState {
22+
// TODO PM-29925: Check if signedPublicKey is not null
2223
return if (signingKey != null && securityState != null) {
2324
WrappedAccountCryptographicState.V2(
2425
privateKey = privateKey,
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.x8bit.bitwarden.data.vault.repository.util
2+
3+
import com.bitwarden.core.WrappedAccountCryptographicState
4+
import org.junit.jupiter.api.Assertions.assertEquals
5+
import org.junit.jupiter.api.Assertions.assertNull
6+
import org.junit.jupiter.api.Test
7+
8+
private const val PRIVATE_KEY = "test-private-key"
9+
private const val SECURITY_STATE = "test-security-state"
10+
private const val SIGNING_KEY = "test-signing-key"
11+
private const val SIGNED_PUBLIC_KEY = "test-signed-public-key"
12+
13+
class WrappedAccountCryptographicStateExtensionsTest {
14+
// TODO PM-29925: Add tests for signedPublicKey
15+
16+
@Test
17+
fun `createWrappedAccountCryptographicState returns V2 when both signingKey and securityState are non-null`() {
18+
val result = createWrappedAccountCryptographicState(
19+
privateKey = PRIVATE_KEY,
20+
securityState = SECURITY_STATE,
21+
signingKey = SIGNING_KEY,
22+
signedPublicKey = SIGNED_PUBLIC_KEY,
23+
)
24+
25+
val v2State = result as WrappedAccountCryptographicState.V2
26+
assertEquals(PRIVATE_KEY, v2State.privateKey)
27+
assertEquals(SECURITY_STATE, v2State.securityState)
28+
assertEquals(SIGNING_KEY, v2State.signingKey)
29+
assertEquals(SIGNED_PUBLIC_KEY, v2State.signedPublicKey)
30+
}
31+
32+
@Test
33+
fun `createWrappedAccountCryptographicState returns V1 when signingKey is null`() {
34+
val result = createWrappedAccountCryptographicState(
35+
privateKey = PRIVATE_KEY,
36+
securityState = SECURITY_STATE,
37+
signingKey = null,
38+
signedPublicKey = SIGNED_PUBLIC_KEY,
39+
)
40+
41+
val v1State = result as WrappedAccountCryptographicState.V1
42+
assertEquals(PRIVATE_KEY, v1State.privateKey)
43+
}
44+
45+
@Test
46+
fun `createWrappedAccountCryptographicState returns V1 when securityState is null`() {
47+
val result = createWrappedAccountCryptographicState(
48+
privateKey = PRIVATE_KEY,
49+
securityState = null,
50+
signingKey = SIGNING_KEY,
51+
signedPublicKey = SIGNED_PUBLIC_KEY,
52+
)
53+
54+
val v1State = result as WrappedAccountCryptographicState.V1
55+
assertEquals(PRIVATE_KEY, v1State.privateKey)
56+
}
57+
58+
@Test
59+
fun `createWrappedAccountCryptographicState returns V1 when both signingKey and securityState are null`() {
60+
val result = createWrappedAccountCryptographicState(
61+
privateKey = PRIVATE_KEY,
62+
securityState = null,
63+
signingKey = null,
64+
signedPublicKey = SIGNED_PUBLIC_KEY,
65+
)
66+
67+
val v1State = result as WrappedAccountCryptographicState.V1
68+
assertEquals(PRIVATE_KEY, v1State.privateKey)
69+
}
70+
71+
@Test
72+
fun `createWrappedAccountCryptographicState returns V2 with null signedPublicKey when signingKey and securityState are present`() {
73+
val result = createWrappedAccountCryptographicState(
74+
privateKey = PRIVATE_KEY,
75+
securityState = SECURITY_STATE,
76+
signingKey = SIGNING_KEY,
77+
signedPublicKey = null,
78+
)
79+
80+
val v2State = result as WrappedAccountCryptographicState.V2
81+
assertEquals(PRIVATE_KEY, v2State.privateKey)
82+
assertEquals(SECURITY_STATE, v2State.securityState)
83+
assertEquals(SIGNING_KEY, v2State.signingKey)
84+
assertNull(v2State.signedPublicKey)
85+
}
86+
}

0 commit comments

Comments
 (0)