Skip to content

Commit

Permalink
Tests: use kotlinx-io to read resource files
Browse files Browse the repository at this point in the history
klio (which we used to read files) is not maintained anymore.
  • Loading branch information
sstone committed Nov 7, 2024
1 parent eeac6a9 commit b51b2b2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
4 changes: 2 additions & 2 deletions tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ kotlin {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation("org.kodein.memory:klio-files:0.12.0")
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.5.4")
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
}
}

Expand Down
30 changes: 6 additions & 24 deletions tests/src/commonTest/kotlin/fr/acinq/secp256k1/Musig2Test.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
package fr.acinq.secp256k1

import kotlinx.serialization.json.*
import org.kodein.memory.file.FileSystem
import org.kodein.memory.file.Path
import org.kodein.memory.file.openReadableFile
import org.kodein.memory.file.resolve
import org.kodein.memory.system.Environment
import org.kodein.memory.text.readString
import org.kodein.memory.use
import kotlin.test.*

class Musig2Test {
fun resourcesDir() =
Environment.findVariable("TEST_RESOURCES_PATH")?.let { Path(it) }
?: FileSystem.workingDir().resolve("src/commonTest/resources")

fun readData(filename: String): JsonElement {
val file = resourcesDir().resolve(filename)
val raw = file.openReadableFile().use { it.readString() }
val format = Json { ignoreUnknownKeys = true }
return format.parseToJsonElement(raw)
}

@Test
fun `aggregate public keys`() {
val tests = readData("musig2/key_agg_vectors.json")
val tests = TestHelpers.readResourceAsJson("musig2/key_agg_vectors.json")
val pubkeys = tests.jsonObject["pubkeys"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
val tweaks = tests.jsonObject["tweaks"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }

Expand Down Expand Up @@ -81,7 +63,7 @@ class Musig2Test {

@Test
fun `generate secret nonce`() {
val tests = readData("musig2/nonce_gen_vectors.json")
val tests = TestHelpers.readResourceAsJson("musig2/nonce_gen_vectors.json")
tests.jsonObject["test_cases"]!!.jsonArray.forEach {
val randprime = Hex.decode(it.jsonObject["rand_"]!!.jsonPrimitive.content)
val sk = it.jsonObject["sk"]?.jsonPrimitive?.contentOrNull?.let { Hex.decode(it) }
Expand Down Expand Up @@ -110,7 +92,7 @@ class Musig2Test {

@Test
fun `aggregate nonces`() {
val tests = readData("musig2/nonce_agg_vectors.json")
val tests = TestHelpers.readResourceAsJson("musig2/nonce_agg_vectors.json")
val nonces = tests.jsonObject["pnonces"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
tests.jsonObject["valid_test_cases"]!!.jsonArray.forEach {
val nonceIndices = it.jsonObject["pnonce_indices"]!!.jsonArray.map { it.jsonPrimitive.int }
Expand All @@ -129,7 +111,7 @@ class Musig2Test {

@Test
fun sign() {
val tests = readData("musig2/sign_verify_vectors.json")
val tests = TestHelpers.readResourceAsJson("musig2/sign_verify_vectors.json")
val sk = Hex.decode(tests.jsonObject["sk"]!!.jsonPrimitive.content)
val pubkeys = tests.jsonObject["pubkeys"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
val secnonces = tests.jsonObject["secnonces"]!!.jsonArray.map { deserializeSecretNonce(it.jsonPrimitive.content) }
Expand Down Expand Up @@ -179,7 +161,7 @@ class Musig2Test {

@Test
fun `aggregate signatures`() {
val tests = readData("musig2/sig_agg_vectors.json")
val tests = TestHelpers.readResourceAsJson("musig2/sig_agg_vectors.json")
val pubkeys = tests.jsonObject["pubkeys"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
val pnonces = tests.jsonObject["pnonces"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
val tweaks = tests.jsonObject["tweaks"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
Expand Down Expand Up @@ -240,7 +222,7 @@ class Musig2Test {

@Test
fun `tweak tests`() {
val tests = readData("musig2/tweak_vectors.json")
val tests = TestHelpers.readResourceAsJson("musig2/tweak_vectors.json")
val sk = Hex.decode(tests.jsonObject["sk"]!!.jsonPrimitive.content)
val pubkeys = tests.jsonObject["pubkeys"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
val pnonces = tests.jsonObject["pnonces"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
Expand Down
26 changes: 26 additions & 0 deletions tests/src/commonTest/kotlin/fr/acinq/secp256k1/TestHelpers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.acinq.secp256k1

import kotlinx.io.buffered
import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem
import kotlinx.io.readByteArray
import kotlinx.io.readString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement

object TestHelpers {
val resourcesPath = Path(readEnvironmentVariable("TEST_RESOURCES_PATH")?: "src/commonTest/resources")

fun readResourceAsJson(filename: String): JsonElement {
val raw = SystemFileSystem.source(Path(resourcesPath, filename)).buffered().readString()
val format = Json { ignoreUnknownKeys = true }
return format.parseToJsonElement(raw)
}


fun readResourceAsByteArray(filename: String): ByteArray {
return SystemFileSystem.source(Path(resourcesPath, filename)).buffered().readByteArray()
}
}

expect fun readEnvironmentVariable(name: String): String?
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fr.acinq.secp256k1

actual fun readEnvironmentVariable(name: String): String? {
return System.getenv(name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.acinq.secp256k1

import platform.posix.*
import kotlinx.cinterop.*

@OptIn(ExperimentalForeignApi::class)
actual fun readEnvironmentVariable(name: String): String? {
return getenv(name)?.toKString()
}

0 comments on commit b51b2b2

Please sign in to comment.