EncryptedKVStorage: can destroy exisint creating one with a new key

This commit is contained in:
sergeych committed 2025-03-16 16:52:26 +03:00
1 parent 875c0f7a50
commit fbbe4d3a34
2 files changed
+42 -7

No files matched your search

+18 -2
View File
@@ -1,11 +1,13 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.bintools.*
import net.sergeych.bipack.decodeFromBipack
import net.sergeych.crypto2.DecryptionFailedException
import net.sergeych.crypto2.EncryptedKVStorage
import net.sergeych.crypto2.SymmetricKey
import net.sergeych.crypto2.initCrypto
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull
class StorageTest {
@@ -15,7 +17,7 @@ class StorageTest {
initCrypto()
val plain = MemoryKVStorage()
val key = SymmetricKey.new()
val storage = EncryptedKVStorage(plain, key)
val storage = EncryptedKVStorage(plain, key, removeExisting = false)
var hello by storage.optStored<String>()
assertNull(hello)
@@ -42,8 +44,9 @@ class StorageTest {
assertEquals("bar", bar)
assertEquals("bazz", bazz)
}
fun setup(s: KVStorage, k: SymmetricKey): EncryptedKVStorage {
val x = EncryptedKVStorage(s, k)
val x = EncryptedKVStorage(s, k, removeExisting = false)
var foo by x.stored("1")
var bar by x.stored("2")
var bazz by x.stored("3")
@@ -52,6 +55,7 @@ class StorageTest {
bazz = "bazz"
return x
}
val k1 = SymmetricKey.new()
val k2 = SymmetricKey.new()
val plain = MemoryKVStorage()
@@ -62,6 +66,18 @@ class StorageTest {
// val s2 = EncryptedKVStorage(plain, k2)
// test(s2)
}
@Test
fun testDeleteExisting() = runTest {
initCrypto()
val plain = MemoryKVStorage()
val c1 = EncryptedKVStorage(plain, SymmetricKey.new(), removeExisting = false) // 1
c1.write("hello", "world")
assertFailsWith<DecryptionFailedException> {
val c2 = EncryptedKVStorage(plain, SymmetricKey.new(), removeExisting = false) // 2
}
val c2 = EncryptedKVStorage(plain, SymmetricKey.new(), removeExisting = true) // 2
}
}
fun KVStorage.dump() {