Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract test resources as files #14

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import app.keemobile.kotpass.database.modifiers.removeEntry
import app.keemobile.kotpass.database.modifiers.removeGroup
import app.keemobile.kotpass.database.modifiers.withHistory
import app.keemobile.kotpass.database.modifiers.withRecycleBin
import app.keemobile.kotpass.io.decodeBase64ToArray
import app.keemobile.kotpass.models.DatabaseElement
import app.keemobile.kotpass.models.DeletedObject
import app.keemobile.kotpass.models.Entry
Expand Down Expand Up @@ -46,28 +45,21 @@ class KeePassDatabaseSpec : DescribeSpec({

describe("Database decoder") {
it("Reads KeePass 3.x file") {
val database = loadDatabase(
rawData = DatabaseRes.DbVer3Aes,
passphrase = "1"
)
val database = loadDatabase("ver3_aes.kdbx", "1")

database.content.group.name shouldBe "New"
}

it("Reads KeePass 4.x file") {
val database = loadDatabase(
rawData = DatabaseRes.DbVer4WithBinaries,
passphrase = "1"
)
val database = loadDatabase("ver4_with_binaries.kdbx", "1")

database.content.group.name shouldBe "New"
}
}

describe("Database encoder") {
it("Writes KeePass 3.x file") {
var database = loadDatabase(
rawData = DatabaseRes.DbVer3Aes,
passphrase = "1"
)
var database = loadDatabase("ver3_aes.kdbx", "1")
val data = ByteArrayOutputStream()
.apply { database.encode(this) }
.toByteArray()
Expand All @@ -79,10 +71,7 @@ class KeePassDatabaseSpec : DescribeSpec({
}

it("Writes KeePass 4.x file") {
var database = loadDatabase(
rawData = DatabaseRes.DbVer4Argon2,
passphrase = "1"
)
var database = loadDatabase("ver4_argon2.kdbx", "1")
val data = ByteArrayOutputStream()
.apply { database.encode(this) }
.toByteArray()
Expand All @@ -96,10 +85,7 @@ class KeePassDatabaseSpec : DescribeSpec({

describe("Database search") {
it("Traverse database") {
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
passphrase = "1"
)
val database = loadDatabase("groups_and_entries.kdbx", "1")
val result = mutableSetOf<DatabaseElement>()
database.traverse { result += it }

Expand Down Expand Up @@ -166,10 +152,7 @@ class KeePassDatabaseSpec : DescribeSpec({
}

it("Finds entries with specific title") {
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
passphrase = "1"
)
val database = loadDatabase("groups_and_entries.kdbx", "1")
val entries = database.findEntries {
it[BasicField.Title]
?.content
Expand All @@ -180,10 +163,7 @@ class KeePassDatabaseSpec : DescribeSpec({
}

it("Finds entry with specific title") {
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
passphrase = "1"
)
val database = loadDatabase("groups_and_entries.kdbx", "1")
val result = database.findEntry {
it[BasicField.Title]
?.content
Expand All @@ -194,10 +174,7 @@ class KeePassDatabaseSpec : DescribeSpec({
}

it("Finds group with specific name") {
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
passphrase = "1"
)
val database = loadDatabase("groups_and_entries.kdbx", "1")
val result = database.getGroup { it.name == "Group 3" }

result shouldNotBe null
Expand All @@ -208,7 +185,7 @@ class KeePassDatabaseSpec : DescribeSpec({
describe("Database modifiers") {
it("Removed Group is moved to recycle bin") {
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
fileName = "groups_and_entries.kdbx",
passphrase = "1"
).withRecycleBin { recycleBinUuid ->
moveGroup(DatabaseRes.GroupsAndEntries.Group2, recycleBinUuid)
Expand All @@ -222,7 +199,7 @@ class KeePassDatabaseSpec : DescribeSpec({

it("Removed Group and it's children UUIDs are added to deleted objects") {
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
fileName = "groups_and_entries.kdbx",
passphrase = "1"
).removeGroup(
DatabaseRes.GroupsAndEntries.Group2
Expand All @@ -242,7 +219,7 @@ class KeePassDatabaseSpec : DescribeSpec({

it("Group modification") {
val (_, group) = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
fileName = "groups_and_entries.kdbx",
passphrase = "1"
).modifyGroup(DatabaseRes.GroupsAndEntries.Group3) {
copy(name = "Hello")
Expand All @@ -256,7 +233,7 @@ class KeePassDatabaseSpec : DescribeSpec({
it("Groups mass modification") {
val label = "Hello"
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
fileName = "groups_and_entries.kdbx",
passphrase = "1"
).modifyGroups {
copy(name = label)
Expand All @@ -272,7 +249,7 @@ class KeePassDatabaseSpec : DescribeSpec({
it("Entries mass modification") {
val label = "Hello"
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
fileName = "groups_and_entries.kdbx",
passphrase = "1"
).modifyEntries {
copy(overrideUrl = label)
Expand All @@ -287,7 +264,7 @@ class KeePassDatabaseSpec : DescribeSpec({

it("Entry modification with history") {
val (_, entry) = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
fileName = "groups_and_entries.kdbx",
passphrase = "1"
).modifyEntry(DatabaseRes.GroupsAndEntries.Entry1) {
withHistory {
Expand All @@ -303,7 +280,7 @@ class KeePassDatabaseSpec : DescribeSpec({

it("Removed Entry is moved to recycle bin") {
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
fileName = "groups_and_entries.kdbx",
passphrase = "1"
).withRecycleBin { recycleBinUuid ->
moveEntry(DatabaseRes.GroupsAndEntries.Entry1, recycleBinUuid)
Expand All @@ -317,7 +294,7 @@ class KeePassDatabaseSpec : DescribeSpec({

it("Removed Entry UUID is added to deleted objects") {
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
fileName = "groups_and_entries.kdbx",
passphrase = "1"
).removeEntry(
DatabaseRes.GroupsAndEntries.Entry1
Expand All @@ -331,10 +308,7 @@ class KeePassDatabaseSpec : DescribeSpec({
}

it("Old entries are removed from history when performing cleanup") {
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
passphrase = "1"
)
val database = loadDatabase("groups_and_entries.kdbx", "1")
val outdated = Instant
.now()
.minus(Period.ofDays(database.content.meta.maintenanceHistoryDays + 1))
Expand All @@ -359,9 +333,9 @@ class KeePassDatabaseSpec : DescribeSpec({
})

private fun loadDatabase(
rawData: String,
fileName: String,
passphrase: String
) = KeePassDatabase.decode(
inputStream = ByteArrayInputStream(rawData.decodeBase64ToArray()),
inputStream = ClassLoader.getSystemResourceAsStream(fileName)!!,
credentials = Credentials.from(EncryptedValue.fromString(passphrase))
)
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
package app.keemobile.kotpass.database.header

import app.keemobile.kotpass.io.decodeBase64ToArray
import app.keemobile.kotpass.resources.DatabaseRes
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
import okio.Buffer
import okio.buffer
import okio.source
import java.io.ByteArrayInputStream

class DatabaseHeaderSpec : DescribeSpec({

describe("Database header") {
it("Properly reads KDF parameters") {
val ver4Argon2 = decodeHeader(DatabaseRes.DbVer4Argon2)
val ver4Argon2 = decodeHeader("ver4_argon2.kdbx")
ver4Argon2.signature.base shouldBe Signature.Base
ver4Argon2.shouldBeInstanceOf<DatabaseHeader.Ver4x>()
with(ver4Argon2) {
kdfParameters.shouldBeInstanceOf<KdfParameters.Argon2>()
}

val ver4Aes = decodeHeader(DatabaseRes.DbVer4Aes)
val ver4Aes = decodeHeader("ver4_aes.kdbx")
ver4Aes.shouldBeInstanceOf<DatabaseHeader.Ver4x>()
with(ver4Aes) {
kdfParameters.shouldBeInstanceOf<KdfParameters.Aes>()
}

val ver3Aes = decodeHeader(DatabaseRes.DbVer3Aes)
val ver3Aes = decodeHeader("ver3_aes.kdbx")
ver3Aes.shouldBeInstanceOf<DatabaseHeader.Ver3x>()
}

it("Try to read/write header") {
val ver4Argon2 = decodeHeader(DatabaseRes.DbVer4Argon2).let { header ->
val ver4Argon2 = decodeHeader("ver4_argon2.kdbx").let { header ->
val buffer = Buffer()
header.writeTo(buffer)
buffer.snapshot()
Expand All @@ -51,7 +48,6 @@ class DatabaseHeaderSpec : DescribeSpec({
}
})

private fun decodeHeader(base64Data: String): DatabaseHeader {
val bytes = base64Data.decodeBase64ToArray()
return DatabaseHeader.readFrom(ByteArrayInputStream(bytes).source().buffer())
}
private fun decodeHeader(fileName: String) = ClassLoader
.getSystemResourceAsStream(fileName)!!
.use { DatabaseHeader.readFrom(it.source().buffer()) }
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package app.keemobile.kotpass.database.header

import app.keemobile.kotpass.constants.CrsAlgorithm
import app.keemobile.kotpass.io.decodeBase64ToArray
import app.keemobile.kotpass.resources.DatabaseRes
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import okio.Buffer
import okio.buffer
import okio.source
import java.io.ByteArrayInputStream

class DatabaseInnerHeaderSpec : DescribeSpec({

describe("Database inner header") {
it("Properly reads and writes data") {
val buffer = Buffer()
val data = DatabaseRes.InnerHeaderWithBinaries.decodeBase64ToArray()
val source = ByteArrayInputStream(data)
.source()
.buffer()
var innerHeader = ClassLoader
.getSystemResourceAsStream("inner_header_with_binaries")!!
.use { DatabaseInnerHeader.readFrom(it.source().buffer()) }

var innerHeader = DatabaseInnerHeader.readFrom(source)
innerHeader.randomStreamId shouldBe CrsAlgorithm.ChaCha20
innerHeader.binaries.size shouldBe 2

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package app.keemobile.kotpass.database.header

import app.keemobile.kotpass.constants.KdfConst
import app.keemobile.kotpass.io.decodeBase64ToArray
import app.keemobile.kotpass.io.decodeHexToArray
import app.keemobile.kotpass.resources.VariantDictionaryRes
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
import okio.ByteString.Companion.toByteString
import java.io.InputStream

class VariantDictionarySpec : DescribeSpec({

describe("Variant dictionary") {
it("Properly reads and writes data") {
val dictionary = VariantDictionaryRes.KdfParams.decodeBase64ToArray()
val dictionary = ClassLoader
.getSystemResourceAsStream("kdf_params")!!
.use(InputStream::readAllBytes)
.toByteString()
.let(VariantDictionary::readFrom)
.let(VariantDictionary::writeToByteString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import app.keemobile.kotpass.database.Credentials
import app.keemobile.kotpass.database.KeePassDatabase
import app.keemobile.kotpass.database.decode
import app.keemobile.kotpass.database.encode
import app.keemobile.kotpass.io.decodeBase64ToArray
import app.keemobile.kotpass.resources.DatabaseRes
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import java.io.ByteArrayInputStream
Expand All @@ -21,7 +19,7 @@ class CredentialsSpec : DescribeSpec({
keyData = byteArrayOf(0x4, 0x7, 0x9)
)
var database = KeePassDatabase.decode(
ByteArrayInputStream(DatabaseRes.DbVer4Argon2.decodeBase64ToArray()),
ClassLoader.getSystemResourceAsStream("ver4_argon2.kdbx")!!,
Credentials.from(EncryptedValue.fromString("1"))
).modifyCredentials {
newCredentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import app.keemobile.kotpass.database.Credentials
import app.keemobile.kotpass.database.KeePassDatabase
import app.keemobile.kotpass.database.decode
import app.keemobile.kotpass.database.traverse
import app.keemobile.kotpass.io.decodeBase64ToArray
import app.keemobile.kotpass.models.CustomIcon
import app.keemobile.kotpass.resources.DatabaseRes
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import java.io.ByteArrayInputStream
import java.util.UUID

class CustomIconsSpec : DescribeSpec({
Expand All @@ -22,7 +19,7 @@ class CustomIconsSpec : DescribeSpec({
uuid to CustomIcon(byteArrayOf(0x1), null, null)
)
val database = KeePassDatabase.decode(
ByteArrayInputStream(DatabaseRes.DbVer4Argon2.decodeBase64ToArray()),
ClassLoader.getSystemResourceAsStream("ver4_argon2.kdbx")!!,
Credentials.from(EncryptedValue.fromString("1"))
).modifyCustomIcons {
customIcons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ import app.keemobile.kotpass.database.modifiers.modifyEntry
import app.keemobile.kotpass.database.modifiers.removeUnusedBinaries
import app.keemobile.kotpass.extensions.getText
import app.keemobile.kotpass.extensions.parseAsXml
import app.keemobile.kotpass.io.decodeBase64ToArray
import app.keemobile.kotpass.io.encodeBase64
import app.keemobile.kotpass.resources.DatabaseRes
import app.keemobile.kotpass.xml.marshal
import app.keemobile.kotpass.xml.unmarshalBinaries
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import okio.buffer
import okio.source
import java.io.ByteArrayInputStream
import java.util.UUID

private const val Contents = "hello kotpass"
Expand Down Expand Up @@ -61,8 +58,8 @@ class BinariesSpec : DescribeSpec({

it("Removes unused binaries") {
val database = KeePassDatabase.decode(
inputStream = ByteArrayInputStream(DatabaseRes.DbVer4WithBinaries.decodeBase64ToArray()),
credentials = Credentials.from(EncryptedValue.fromString("1"))
ClassLoader.getSystemResourceAsStream("ver4_with_binaries.kdbx")!!,
Credentials.from(EncryptedValue.fromString("1"))
).modifyEntry(UUID.fromString("6d9b7812-6d1a-1765-9cd7-c66a93a220e9")) {
copy(binaries = listOf())
}.removeUnusedBinaries()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class EntrySpec : DescribeSpec({

it("Invalid binary references are skipped") {
val database = decodeFromResources(
path = "entry/invalid_references.kdbx",
path = "invalid_references.kdbx",
credentials = Credentials.from(EncryptedValue.fromString("1"))
)

Expand Down

This file was deleted.

Loading