Skip to content

Commit

Permalink
Add isEmpty method to EntryValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Trotsenko committed Jun 28, 2023
1 parent c9edc58 commit 46bcfe9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ sealed class EntryValue {
*/
abstract val content: String

/**
* Checks whether [content] is empty without exposing [EncryptedValue].
*/
abstract fun isEmpty(): Boolean

/**
* Should be used for non-sensitive values.
*/
data class Plain(
override val content: String
) : EntryValue()
) : EntryValue() {
override fun isEmpty() = content.isEmpty()
}

/**
* Should be used for secrets.
Expand All @@ -25,6 +32,8 @@ sealed class EntryValue {
private val value: EncryptedValue
) : EntryValue() {
override val content: String get() = value.text

override fun isEmpty() = value.byteLength == 0
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package app.keemobile.kotpass.models

import app.keemobile.kotpass.cryptography.EncryptedValue
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe

class EntryValueSpec : DescribeSpec({

describe("EntryValue") {
it("isEmpty check properly evaluates empty string") {
val emptyPlain = EntryValue.Plain("")
val emptyEncrypted = EntryValue.Encrypted(EncryptedValue.fromString(""))
val somePlain = EntryValue.Encrypted(EncryptedValue.fromString("123"))
val someEncrypted = EntryValue.Encrypted(EncryptedValue.fromString("123"))

emptyPlain.isEmpty() shouldBe true
emptyEncrypted.isEmpty() shouldBe true

somePlain.isEmpty() shouldBe false
someEncrypted.isEmpty() shouldBe false
}
}
})

0 comments on commit 46bcfe9

Please sign in to comment.