-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKeychainTest.kt
More file actions
120 lines (95 loc) · 2.95 KB
/
KeychainTest.kt
File metadata and controls
120 lines (95 loc) · 2.95 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package to.bitkit.data.keychain
import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import to.bitkit.data.AppDb
import to.bitkit.data.entities.ConfigEntity
import to.bitkit.test.BaseAndroidTest
import to.bitkit.test.annotations.DeviceIntegration
import to.bitkit.test.annotations.DeviceStorageIntegration
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull
import kotlin.test.assertTrue
@RunWith(AndroidJUnit4::class)
@DeviceIntegration
@DeviceStorageIntegration
class KeychainTest : BaseAndroidTest() {
private val appContext by lazy { ApplicationProvider.getApplicationContext<Context>() }
private lateinit var db: AppDb
private lateinit var sut: Keychain
@Before
fun setUp() {
db = Room.inMemoryDatabaseBuilder(appContext, AppDb::class.java).build().also {
// seed db
runBlocking {
it.configDao().upsert(
ConfigEntity(
walletIndex = 0L,
),
)
}
}
sut = Keychain(
db,
appContext,
testDispatcher,
)
}
@Test
fun dbSeed() = test {
val walletIndex = db.configDao().getAll().first().first().walletIndex
assertEquals(0L, walletIndex)
}
@Test
fun saveString_loadString() = test {
val (key, value) = "key" to "value"
sut.saveString(key, value)
assertEquals(value, sut.loadString(key))
}
@Test
fun saveString_existingKey_shouldThrow() = test {
val key = "key"
sut.saveString(key, "value1")
assertFailsWith<KeychainError.FailedToSaveAlreadyExists> { sut.saveString(key, "value2") }
}
@Test
fun delete() = test {
val (key, value) = "keyToDelete" to "value"
sut.saveString(key, value)
sut.delete(key)
assertNull(sut.loadString(key))
}
@Test
fun exists() = test {
val (key, value) = "keyToExist" to "value"
sut.saveString(key, value)
assertTrue { sut.exists(key) }
}
@Test
fun wipe() = test {
List(3) { sut.saveString("keyToWipe$it", "value$it") }
sut.wipe()
assertTrue { sut.snapshot.asMap().isEmpty() }
}
@Test
fun pinAttemptsRemaining_shouldReturnDecryptedValue() = test {
val attemptsRemaining = "3"
sut.saveString(Keychain.Key.PIN_ATTEMPTS_REMAINING.name, attemptsRemaining)
val result = sut.pinAttemptsRemaining().first()
assertEquals(attemptsRemaining, result.toString())
}
@After
fun tearDown() {
db.close()
sut.cancel()
}
}