-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Denis Trotsenko
committed
Jun 19, 2022
1 parent
2d4494d
commit 3bb2a1c
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
kotpass/src/main/kotlin/io/github/anvell/kotpass/database/modifiers/CustomIcons.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
kotpass/src/test/kotlin/io/github/anvell/kotpass/database/modifiers/CustomIconsSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} | ||
}) |