From 0ff2c5b1fecc9fec3a98bf016b9cca01084d381d Mon Sep 17 00:00:00 2001 From: Fabrice Drouin Date: Thu, 7 Nov 2024 11:26:18 +0100 Subject: [PATCH] Tests: use kotlinx-io to read resource files (#114) klio (which we used to read files) is not maintained anymore. --- tests/build.gradle.kts | 4 +-- .../kotlin/fr/acinq/secp256k1/Musig2Test.kt | 30 ++++--------------- .../kotlin/fr/acinq/secp256k1/TestHelpers.kt | 26 ++++++++++++++++ .../fr/acinq/secp256k1/TestHelpers.jvm.kt | 5 ++++ .../fr/acinq/secp256k1/TestHelpers.native.kt | 9 ++++++ 5 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 tests/src/commonTest/kotlin/fr/acinq/secp256k1/TestHelpers.kt create mode 100644 tests/src/jvmTest/kotlin/fr/acinq/secp256k1/TestHelpers.jvm.kt create mode 100644 tests/src/nativeTest/kotlin/fr/acinq/secp256k1/TestHelpers.native.kt diff --git a/tests/build.gradle.kts b/tests/build.gradle.kts index 6df7c90..920c988 100644 --- a/tests/build.gradle.kts +++ b/tests/build.gradle.kts @@ -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") } } diff --git a/tests/src/commonTest/kotlin/fr/acinq/secp256k1/Musig2Test.kt b/tests/src/commonTest/kotlin/fr/acinq/secp256k1/Musig2Test.kt index 7630df4..584476c 100644 --- a/tests/src/commonTest/kotlin/fr/acinq/secp256k1/Musig2Test.kt +++ b/tests/src/commonTest/kotlin/fr/acinq/secp256k1/Musig2Test.kt @@ -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) } @@ -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) } @@ -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 } @@ -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) } @@ -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) } @@ -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) } diff --git a/tests/src/commonTest/kotlin/fr/acinq/secp256k1/TestHelpers.kt b/tests/src/commonTest/kotlin/fr/acinq/secp256k1/TestHelpers.kt new file mode 100644 index 0000000..6cbfb9e --- /dev/null +++ b/tests/src/commonTest/kotlin/fr/acinq/secp256k1/TestHelpers.kt @@ -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? diff --git a/tests/src/jvmTest/kotlin/fr/acinq/secp256k1/TestHelpers.jvm.kt b/tests/src/jvmTest/kotlin/fr/acinq/secp256k1/TestHelpers.jvm.kt new file mode 100644 index 0000000..cdc5453 --- /dev/null +++ b/tests/src/jvmTest/kotlin/fr/acinq/secp256k1/TestHelpers.jvm.kt @@ -0,0 +1,5 @@ +package fr.acinq.secp256k1 + +actual fun readEnvironmentVariable(name: String): String? { + return System.getenv(name) +} \ No newline at end of file diff --git a/tests/src/nativeTest/kotlin/fr/acinq/secp256k1/TestHelpers.native.kt b/tests/src/nativeTest/kotlin/fr/acinq/secp256k1/TestHelpers.native.kt new file mode 100644 index 0000000..9ed544a --- /dev/null +++ b/tests/src/nativeTest/kotlin/fr/acinq/secp256k1/TestHelpers.native.kt @@ -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() +} \ No newline at end of file