Skip to content

Commit

Permalink
Added 'modifyCustomIcons' helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Trotsenko committed Jun 19, 2022
1 parent 2d4494d commit 3bb2a1c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.anvell.kotpass.database.modifiers

import io.github.anvell.kotpass.database.KeePassDatabase
import io.github.anvell.kotpass.models.CustomIcon
import java.util.*

/**
* Replaces [Map] of [CustomIcon] while removing invalid references in entries.
*/
inline fun KeePassDatabase.modifyCustomIcons(
crossinline block: (Map<UUID, CustomIcon>) -> Map<UUID, CustomIcon>
): KeePassDatabase {
val newCustomIcons = block(content.meta.customIcons)
val removedUuids = content.meta.customIcons.keys
.filter { it !in newCustomIcons.keys }

return modifyEntries {
if (customIconUuid in removedUuids) {
copy(customIconUuid = null)
} else {
this
}
}.modifyMeta {
copy(customIcons = newCustomIcons)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.anvell.kotpass.database.modifiers

import io.github.anvell.kotpass.cryptography.EncryptedValue
import io.github.anvell.kotpass.database.*
import io.github.anvell.kotpass.io.decodeBase64ToArray
import io.github.anvell.kotpass.models.CustomIcon
import io.github.anvell.kotpass.models.Entry
import io.github.anvell.kotpass.resources.DatabaseRes
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import java.io.ByteArrayInputStream
import java.util.*

class CustomIconsSpec : DescribeSpec({

describe("CustomIcons modifier") {
it("Properly cleans up invalid references to custom icons") {
val uuid = UUID.randomUUID()
val customIcons = mapOf(
uuid to CustomIcon(byteArrayOf(0x1), null, null)
)
val database = KeePassDatabase.decode(
ByteArrayInputStream(DatabaseRes.DbVer4Argon2.decodeBase64ToArray()),
Credentials.from(EncryptedValue.fromString("1"))
).modifyCustomIcons {
customIcons
}.modifyEntries {
copy(customIconUuid = uuid)
}
database.traverse { element ->
if (element is Entry) {
element.customIconUuid shouldBe uuid
}
}
val noCustomIcons = database.modifyCustomIcons { mapOf() }

noCustomIcons.traverse { element ->
if (element is Entry) {
element.customIconUuid shouldBe null
}
}
}
}
})

0 comments on commit 3bb2a1c

Please sign in to comment.