Skip to content

Commit b51b2b2

Browse files
committed
Tests: use kotlinx-io to read resource files
klio (which we used to read files) is not maintained anymore.
1 parent eeac6a9 commit b51b2b2

File tree

5 files changed

+48
-26
lines changed

5 files changed

+48
-26
lines changed

tests/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ kotlin {
2323
dependencies {
2424
implementation(kotlin("test-common"))
2525
implementation(kotlin("test-annotations-common"))
26-
implementation("org.kodein.memory:klio-files:0.12.0")
27-
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
26+
implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.5.4")
27+
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
2828
}
2929
}
3030

tests/src/commonTest/kotlin/fr/acinq/secp256k1/Musig2Test.kt

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
package fr.acinq.secp256k1
22

33
import kotlinx.serialization.json.*
4-
import org.kodein.memory.file.FileSystem
5-
import org.kodein.memory.file.Path
6-
import org.kodein.memory.file.openReadableFile
7-
import org.kodein.memory.file.resolve
8-
import org.kodein.memory.system.Environment
9-
import org.kodein.memory.text.readString
10-
import org.kodein.memory.use
114
import kotlin.test.*
125

136
class Musig2Test {
14-
fun resourcesDir() =
15-
Environment.findVariable("TEST_RESOURCES_PATH")?.let { Path(it) }
16-
?: FileSystem.workingDir().resolve("src/commonTest/resources")
17-
18-
fun readData(filename: String): JsonElement {
19-
val file = resourcesDir().resolve(filename)
20-
val raw = file.openReadableFile().use { it.readString() }
21-
val format = Json { ignoreUnknownKeys = true }
22-
return format.parseToJsonElement(raw)
23-
}
24-
257
@Test
268
fun `aggregate public keys`() {
27-
val tests = readData("musig2/key_agg_vectors.json")
9+
val tests = TestHelpers.readResourceAsJson("musig2/key_agg_vectors.json")
2810
val pubkeys = tests.jsonObject["pubkeys"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
2911
val tweaks = tests.jsonObject["tweaks"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
3012

@@ -81,7 +63,7 @@ class Musig2Test {
8163

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

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

130112
@Test
131113
fun sign() {
132-
val tests = readData("musig2/sign_verify_vectors.json")
114+
val tests = TestHelpers.readResourceAsJson("musig2/sign_verify_vectors.json")
133115
val sk = Hex.decode(tests.jsonObject["sk"]!!.jsonPrimitive.content)
134116
val pubkeys = tests.jsonObject["pubkeys"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
135117
val secnonces = tests.jsonObject["secnonces"]!!.jsonArray.map { deserializeSecretNonce(it.jsonPrimitive.content) }
@@ -179,7 +161,7 @@ class Musig2Test {
179161

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

241223
@Test
242224
fun `tweak tests`() {
243-
val tests = readData("musig2/tweak_vectors.json")
225+
val tests = TestHelpers.readResourceAsJson("musig2/tweak_vectors.json")
244226
val sk = Hex.decode(tests.jsonObject["sk"]!!.jsonPrimitive.content)
245227
val pubkeys = tests.jsonObject["pubkeys"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
246228
val pnonces = tests.jsonObject["pnonces"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package fr.acinq.secp256k1
2+
3+
import kotlinx.io.buffered
4+
import kotlinx.io.files.Path
5+
import kotlinx.io.files.SystemFileSystem
6+
import kotlinx.io.readByteArray
7+
import kotlinx.io.readString
8+
import kotlinx.serialization.json.Json
9+
import kotlinx.serialization.json.JsonElement
10+
11+
object TestHelpers {
12+
val resourcesPath = Path(readEnvironmentVariable("TEST_RESOURCES_PATH")?: "src/commonTest/resources")
13+
14+
fun readResourceAsJson(filename: String): JsonElement {
15+
val raw = SystemFileSystem.source(Path(resourcesPath, filename)).buffered().readString()
16+
val format = Json { ignoreUnknownKeys = true }
17+
return format.parseToJsonElement(raw)
18+
}
19+
20+
21+
fun readResourceAsByteArray(filename: String): ByteArray {
22+
return SystemFileSystem.source(Path(resourcesPath, filename)).buffered().readByteArray()
23+
}
24+
}
25+
26+
expect fun readEnvironmentVariable(name: String): String?
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package fr.acinq.secp256k1
2+
3+
actual fun readEnvironmentVariable(name: String): String? {
4+
return System.getenv(name)
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package fr.acinq.secp256k1
2+
3+
import platform.posix.*
4+
import kotlinx.cinterop.*
5+
6+
@OptIn(ExperimentalForeignApi::class)
7+
actual fun readEnvironmentVariable(name: String): String? {
8+
return getenv(name)?.toKString()
9+
}

0 commit comments

Comments
 (0)