Skip to content

Commit 3791e40

Browse files
test(ECDH): fix AndroidTest
1 parent fa3a937 commit 3791e40

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

mirai-core/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ kotlin {
8282
implementation(kotlin("test-junit5", Versions.kotlinCompiler))
8383
implementation(kotlin("test-annotations-common"))
8484
implementation(kotlin("test-common"))
85-
//implementation("org.bouncycastle:bcprov-jdk15on:1.64")
85+
implementation(bouncycastle)
8686
}
8787
}
8888

mirai-core/src/androidMain/kotlin/utils/crypto/ECDHAndroid.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ package net.mamoe.mirai.internal.utils.crypto
1111

1212
import java.security.Security
1313

14-
1514
internal actual fun ECDH.Companion.create(): ECDH<*, *> =
16-
if (android.os.Build.VERSION.SDK_INT >= 23){
15+
if (kotlin.runCatching {
16+
// When running tests on JVM desktop, `ClassNotFoundException` will be got
17+
android.os.Build.VERSION.SDK_INT >= 23
18+
}.getOrDefault(false)) {
1719
// For newer Android, BC is deprecated, but AndroidKeyStore (default) handles ECDH well
1820
// Do not specify a provider as Google recommends
1921
JceECDH()

mirai-core/src/androidTest/kotlin/test/initPlatform.android.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@
1010
package net.mamoe.mirai.internal.test
1111

1212
import net.mamoe.mirai.utils.MiraiLogger
13+
import org.bouncycastle.jce.provider.BouncyCastleProvider
14+
import java.security.Security
1315
import kotlin.test.Test
1416
import kotlin.test.assertTrue
1517

1618
internal actual fun initPlatform() {
19+
init
20+
}
21+
22+
private val init: Unit by lazy {
23+
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) != null) {
24+
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME)
25+
}
26+
Security.addProvider(BouncyCastleProvider())
1727

28+
Unit
1829
}
1930

2031
internal actual class PlatformInitializationTest : AbstractTest() {
@@ -26,4 +37,4 @@ internal actual class PlatformInitializationTest : AbstractTest() {
2637
MiraiLogger.Factory.create(this::class, "1") is net.mamoe.mirai.internal.utils.StdoutLogger
2738
}
2839
}
29-
}
40+
}

0 commit comments

Comments
 (0)