chore: Improve tests

This commit is contained in:
Minecon724 2025-10-29 13:45:35 +01:00
commit b2710bb18c
Signed by untrusted user who does not match committer: m724
GPG key ID: A02E6E67AB961189
12 changed files with 88 additions and 37 deletions

1
.idea/.name generated Normal file
View file

@ -0,0 +1 @@
auth

View file

@ -50,8 +50,10 @@ kotlin {
}
}
// note the documentation said "temporarily" so idk https://kotest.io/docs/framework/project-setup.html#re-running-tests
tasks.withType<Test>().configureEach {
logger.lifecycle("UP-TO-DATE check for $name is disabled, forcing it to run.")
outputs.upToDateWhen { false }
}
tasks.register<Copy>("copyNativeTestResources") {
from("src/nativeTest/resources")
into("build/bin/native/debugTest/resources")
}
tasks.findByName("nativeTest")!!
.dependsOn("copyNativeTestResources")

View file

@ -0,0 +1,4 @@
package dn42.m724.auth.registry
class RegistryLoader {
}

View file

@ -0,0 +1,30 @@
package dn42.m724.auth
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.matchers.shouldBe
import nl.adaptivity.xmlutil.core.impl.multiplatform.FileInputStream
import nl.adaptivity.xmlutil.core.impl.multiplatform.use
private const val RESOURCES_PATH_PREFIX = "build/bin/native/debugTest/resources/" // ...awe TODO don't
fun getResource(path: String): String {
return FileInputStream(RESOURCES_PATH_PREFIX + path).use { inputStream ->
val buffer = ByteArray(1024)
val result = StringBuilder()
var charsRead: Int
while (inputStream.read(buffer).also { charsRead = it } != -1) {
result.append(buffer.decodeToString(0, charsRead))
}
result.toString()
}
}
class TestUtilsTest: ShouldSpec({
context("getResource") {
should("return the content of test/test_resource.txt") {
getResource("test/test_resource.txt") shouldBe "This is a test resource"
}
}
})

View file

@ -1,43 +1,20 @@
package dn42.m724.auth.checker.pgp
import dn42.m724.auth.checker.SignatureChecker
import dn42.m724.auth.getResource
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
private const val fingerprint = "2DE81640E41DD84A9ED9B8F8FD4D7C6652E72E7B"
private const val invalidFingerprint = "139F1460BC66A19A2F880D8D47BA020D8EBCC05E"
private val clearTextSignature = """
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
simple message
-----BEGIN PGP SIGNATURE-----
iJEEARYKADkWIQQt6BZA5B3YSp7ZuPj9TXxmUucuewUCaP4wchsUgAAAAAAEAA5t
YW51MiwyLjUrMS4xMSwyLDIACgkQ/U18ZlLnLnuM3AD+L0QBrn2pnGAemcJZDh+p
6oJnuTgeSMiMRkkbMgTOMFMBAPzhLKgyzx4YJcgrIinvZsgPowR9Pf0ryzxwQ5mo
qUwJ
=FYVj
-----END PGP SIGNATURE-----
""".trimIndent()
val detachedSignature = """
-----BEGIN PGP SIGNATURE-----
iJEEABYKADkWIQQt6BZA5B3YSp7ZuPj9TXxmUucuewUCaP4wWxsUgAAAAAAEAA5t
YW51MiwyLjUrMS4xMSwyLDIACgkQ/U18ZlLnLnsaEgEA15uncZNWN6zV952vO6rG
mMIAG9X54X9Cfp5DEfG3hPcBAJypNGnlyHivk+ZKYDlKSZB2S53vKrA3q3J2yDqb
0SUF
=12FC
-----END PGP SIGNATURE-----
""".trimIndent()
class PgpSignatureVerificationTest: FunSpec({
val checker: SignatureChecker = PgpSignatureChecker() // stateless (for now?)
val fingerprint = getResource("pgp/fingerprint.txt")
val invalidFingerprint = getResource("pgp/fingerprint_invalid.txt")
val clearTextSignature = getResource("pgp/clear_text_signature.txt")
val detachedSignature = getResource("pgp/detached_signature.txt")
val successfulCases = listOf(
TestCase(
description = "valid clear-text signature",

View file

@ -1,16 +1,19 @@
package dn42.m724.auth.model
import dn42.m724.auth.getResource
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import kotlin.test.assertEquals
class AuthenticationMethodTest: FunSpec({
val fingerprint = getResource("pgp/fingerprint.txt")
withData(
nameFn = { authenticationMethodString -> "fromString should throw for invalid input: $authenticationMethodString" },
"pgp-fingerprint 2DE81640E41D",
"pgp 2DE81640E41DD84A9ED9B8F8FD4D7C6652E72E7B",
"pgp-fingerprint 2DE81640E41DD84A9ED9B8F8FD4xveTes7C6652E72E7B",
"pgp-fingerprint 2DE81640E41DD84A9ED9B8F8FD4xD7C6652E72E7B",
"pgp-fingerprint"
) { authenticationMethodString ->
shouldThrow<Exception> {
@ -20,8 +23,8 @@ class AuthenticationMethodTest: FunSpec({
withData(
nameFn = { (authenticationMethodString, expected) -> "fromString should return $expected for input: $authenticationMethodString" },
Pair("pgp-fingerprint 2DE81640E41DD84A9ED9B8F8FD4D7C6652E72E7B", AuthenticationMethod.Pgp("2DE81640E41DD84A9ED9B8F8FD4D7C6652E72E7B")),
Pair("pgp-fingerprint 2DE81640E41DD84A9ED9B8F8FD4D7C6652E72E7B this is ignored", AuthenticationMethod.Pgp("2DE81640E41DD84A9ED9B8F8FD4D7C6652E72E7B"))
Pair("pgp-fingerprint $fingerprint", AuthenticationMethod.Pgp(fingerprint)),
Pair("pgp-fingerprint $fingerprint this is ignored", AuthenticationMethod.Pgp(fingerprint))
) { (authenticationMethodString, expected) ->
assertEquals(
expected = expected,

View file

@ -0,0 +1,11 @@
package dn42.m724.auth.registry
import dn42.m724.auth.getResource
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class RegistryLoaderTest: FunSpec({
test("Test Test utils") {
getResource("test/test_resource.txt") shouldBe "This is a test resource"
}
})

View file

@ -0,0 +1,12 @@
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
simple message
-----BEGIN PGP SIGNATURE-----
iJEEARYKADkWIQQt6BZA5B3YSp7ZuPj9TXxmUucuewUCaP4wchsUgAAAAAAEAA5t
YW51MiwyLjUrMS4xMSwyLDIACgkQ/U18ZlLnLnuM3AD+L0QBrn2pnGAemcJZDh+p
6oJnuTgeSMiMRkkbMgTOMFMBAPzhLKgyzx4YJcgrIinvZsgPowR9Pf0ryzxwQ5mo
qUwJ
=FYVj
-----END PGP SIGNATURE-----

View file

@ -0,0 +1,8 @@
-----BEGIN PGP SIGNATURE-----
iJEEABYKADkWIQQt6BZA5B3YSp7ZuPj9TXxmUucuewUCaP4wWxsUgAAAAAAEAA5t
YW51MiwyLjUrMS4xMSwyLDIACgkQ/U18ZlLnLnsaEgEA15uncZNWN6zV952vO6rG
mMIAG9X54X9Cfp5DEfG3hPcBAJypNGnlyHivk+ZKYDlKSZB2S53vKrA3q3J2yDqb
0SUF
=12FC
-----END PGP SIGNATURE-----

View file

@ -0,0 +1 @@
2DE81640E41DD84A9ED9B8F8FD4D7C6652E72E7B

View file

@ -0,0 +1 @@
139F1460BC66A19A2F880D8D47BA020D8EBCC05E

View file

@ -0,0 +1 @@
This is a test resource