Skip to content

Commit

Permalink
Fix TLS test on JS
Browse files Browse the repository at this point in the history
  • Loading branch information
davidepianca98 committed Jan 21, 2024
1 parent 38b1e73 commit a83b1f9
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 31 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ buildscript {
plugins {
kotlin("multiplatform") version "1.9.22" apply false
id("com.louiscad.complete-kotlin") version "1.1.0"
id("com.goncalossilva.resources") version "0.4.0"
}

subprojects {
Expand Down
1 change: 1 addition & 0 deletions kmqtt-broker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ kotlin {
implementation(kotlin("test-annotations-common"))
implementation(project(":kmqtt-client"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutineVersion")
implementation("com.goncalossilva:resources:0.4.0")
}
}
val jvmMain by getting {
Expand Down
30 changes: 16 additions & 14 deletions kmqtt-broker/src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import mqtt.broker.Broker
import socket.tls.TLSSettings

public fun main(args: Array<String>) {
// TODO this is here only because of NodeJS tests, that run main. As in mocha settings it's not possible to set --exit
// we need to avoid starting the broker, otherwise the test task won't ever terminate
if (args.isEmpty()) {
println("At least one argument needed:")
println(" -h x.x.x.x")
println(" -p port")
println(" --max-connections n")
println(" --key-store path")
println(" --key-store-psw password")
println(" --wsp port")
return
}
println("Starting KMQTT")
private fun showHelp() {
println("At least one argument needed:")
println(" -h x.x.x.x")
println(" -p port")
println(" --max-connections n")
println(" --key-store path")
println(" --key-store-psw password")
println(" --wsp port")
}

public fun main(args: Array<String>) {
val argumentsMap = HashMap<String, String>()
var i = 0
while (i < args.size) {
Expand All @@ -26,10 +22,16 @@ public fun main(args: Array<String>) {
"--key-store" -> argumentsMap["keyStore"] = args[++i]
"--key-store-psw" -> argumentsMap["keyStorePassword"] = args[++i]
"--wsp" -> argumentsMap["wsPort"] = args[++i]
"--help" -> {
showHelp()
return
}
}
i++
}

println("Starting KMQTT")

val host = argumentsMap["host"] ?: "0.0.0.0"
val port = argumentsMap["port"]?.toInt() ?: 1883
val backlog = argumentsMap["maxConn"]?.toInt() ?: 128
Expand Down
20 changes: 16 additions & 4 deletions kmqtt-broker/src/commonTest/kotlin/integration/TLSTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import MQTTClient
import TLSClientSettings
import com.goncalossilva.resources.Resource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
Expand All @@ -10,6 +11,7 @@ import mqtt.MQTTVersion
import mqtt.Subscription
import mqtt.broker.Broker
import mqtt.packets.Qos
import mqtt.packets.mqttv5.ReasonCode
import mqtt.packets.mqttv5.SubscriptionOptions
import socket.tls.TLSSettings
import kotlin.test.Test
Expand All @@ -18,15 +20,14 @@ import kotlin.test.assertEquals

class TLSTest {

@Test
fun testPublish() = runTest {
private suspend fun runTest(pathPrefix: String) {
val sendPayload = "Test"
val topic = "test/topic"

var received = false

val broker = Broker(port = 8883, tlsSettings = TLSSettings(keyStoreFilePath = "docker/linux/keyStore.p12", keyStorePassword = "changeit"))
val client = MQTTClient(MQTTVersion.MQTT5, "127.0.0.1", broker.port, TLSClientSettings(serverCertificatePath = "docker/linux/cert.pem")) {
val broker = Broker(port = 8883, tlsSettings = TLSSettings(keyStoreFilePath = pathPrefix + "keyStore.p12", keyStorePassword = "changeit"))
val client = MQTTClient(MQTTVersion.MQTT5, "127.0.0.1", broker.port, TLSClientSettings(serverCertificatePath = pathPrefix + "cert.pem")) {
assertEquals(topic, it.topicName)
assertContentEquals(sendPayload.encodeToByteArray().toUByteArray(), it.payload)
assertEquals(Qos.AT_MOST_ONCE, it.qos)
Expand All @@ -50,10 +51,21 @@ class TLSTest {
}
}

client.disconnect(ReasonCode.SUCCESS)

broker.stop()

if (i >= 1000) {
throw Exception("Test timeout")
}
}

@Test
fun testPublish() = runTest {
if (Resource("src/commonTest/resources/keyStore.p12").exists()) {
runTest("src/commonTest/resources/")
} else {
runTest("kotlin/")
}
}
}
1 change: 0 additions & 1 deletion kmqtt-broker/src/jsMain/kotlin/setShutdownHook.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import node.events.EventType
import node.process.process

internal actual fun setShutdownHook(hook: () -> Unit) {
Expand Down
10 changes: 7 additions & 3 deletions kmqtt-client/src/jsMain/kotlin/ClientSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ public actual class ClientSocket actual constructor(
doLater()
}

private fun doLater() {
protected fun doLater() {
if (open) {
setTimeout({
checkCallback()
doLater()
try {
checkCallback()
doLater()
} catch (e: dynamic) {
close()
}
}, 250)
}
}
Expand Down
25 changes: 17 additions & 8 deletions kmqtt-client/src/jsMain/kotlin/TLSClientSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,27 @@ public actual class TLSClientSocket actual constructor(
public actual val handshakeComplete: Boolean
get() = true

private var open = true

init {
doLater()
}

private fun doLater() {
setTimeout({
try {
checkCallback()
doLater()
} catch (e: dynamic) {
close()
}
}, 250)
if (open) {
setTimeout({
try {
checkCallback()
doLater()
} catch (e: dynamic) {
close()
}
}, 250)
}
}

override fun close() {
open = false
super.close()
}
}
9 changes: 8 additions & 1 deletion kmqtt-common/src/jsMain/kotlin/socket/tcp/Socket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public actual open class Socket(

actual override fun send(data: UByteArray) {
socket.write(data.toBuffer())
selectCallback(attachment, SocketState.WRITE)
try {
selectCallback(attachment, SocketState.WRITE)
} catch (e: dynamic) {
close()
}
}

actual override fun read(): UByteArray? {
Expand All @@ -40,6 +44,9 @@ public actual open class Socket(

actual override fun close() {
socket.end()
socket._destroy(null) {
socket.unref()
}
}

actual override fun sendRemaining() {
Expand Down

0 comments on commit a83b1f9

Please sign in to comment.