From a2aa6af912a0850b00c8f1f586ea0fda2984f13b Mon Sep 17 00:00:00 2001 From: Carlos Ballesteros Velasco Date: Sat, 10 Dec 2022 03:32:01 +0100 Subject: [PATCH] Fixes wrong wskey=null in RawSocketWebSocketClient + add test verifying wrong length for wskey (#1152) Fixes #1146 --- .../com/soywiz/korio/net/ws/WebSocketsAndroid.kt | 2 +- .../soywiz/korio/net/ws/RawSocketWebSocketClient.kt | 4 ++-- .../com/soywiz/korio/net/ws/WebSocketClient.kt | 12 ++++++------ .../korio/net/ws/RawSocketWebSocketClientTest.kt | 10 ++++++++-- .../kotlin/com/soywiz/korio/net/ws/WebSocketsJs.kt | 2 +- .../kotlin/com/soywiz/korio/net/ws/WebSocketsJvm.kt | 2 +- .../com/soywiz/korio/net/ws/WebSocketsNative.kt | 2 +- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/korio/src/androidMain/kotlin/com/soywiz/korio/net/ws/WebSocketsAndroid.kt b/korio/src/androidMain/kotlin/com/soywiz/korio/net/ws/WebSocketsAndroid.kt index e3754d916a..bec07c4737 100644 --- a/korio/src/androidMain/kotlin/com/soywiz/korio/net/ws/WebSocketsAndroid.kt +++ b/korio/src/androidMain/kotlin/com/soywiz/korio/net/ws/WebSocketsAndroid.kt @@ -6,7 +6,7 @@ actual suspend fun WebSocketClient( url: String, protocols: List?, origin: String?, - wskey: String?, + wskey: String, debug: Boolean, headers: Http.Headers, dummy: Boolean, diff --git a/korio/src/commonMain/kotlin/com/soywiz/korio/net/ws/RawSocketWebSocketClient.kt b/korio/src/commonMain/kotlin/com/soywiz/korio/net/ws/RawSocketWebSocketClient.kt index 686e111e64..35dd27755a 100644 --- a/korio/src/commonMain/kotlin/com/soywiz/korio/net/ws/RawSocketWebSocketClient.kt +++ b/korio/src/commonMain/kotlin/com/soywiz/korio/net/ws/RawSocketWebSocketClient.kt @@ -31,7 +31,7 @@ suspend fun RawSocketWebSocketClient( url: String, protocols: List? = null, origin: String? = null, - wskey: String? = DEFAULT_WSKEY, + wskey: String = DEFAULT_WSKEY, debug: Boolean = false, connect: Boolean = true, headers: Http.Headers = Http.Headers(), @@ -41,7 +41,7 @@ suspend fun RawSocketWebSocketClient( if (OS.isJsBrowserOrWorker) error("RawSocketWebSocketClient is not supported on JS browser. Use WebSocketClient instead") val uri = URL(url) val secure: Boolean = uri.isSecureScheme - return RawSocketWebSocketClient(coroutineContext, AsyncClient.create(secure = secure), uri, protocols, debug, origin, wskey ?: "mykey", headers, masked).also { + return RawSocketWebSocketClient(coroutineContext, AsyncClient.create(secure = secure), uri, protocols, debug, origin, wskey, headers, masked).also { init(it) if (connect) it.internalConnect() } diff --git a/korio/src/commonMain/kotlin/com/soywiz/korio/net/ws/WebSocketClient.kt b/korio/src/commonMain/kotlin/com/soywiz/korio/net/ws/WebSocketClient.kt index 5302fe81ae..d62ace6b49 100644 --- a/korio/src/commonMain/kotlin/com/soywiz/korio/net/ws/WebSocketClient.kt +++ b/korio/src/commonMain/kotlin/com/soywiz/korio/net/ws/WebSocketClient.kt @@ -53,11 +53,11 @@ suspend fun WebSocketClient.readString() = onStringMessage.waitOneBase() suspend fun WebSocketClient.readBinary() = onBinaryMessage.waitOneBase() expect suspend fun WebSocketClient( - url: String, - protocols: List?, - origin: String?, - wskey: String?, - debug: Boolean, + url: String, + protocols: List?, + origin: String?, + wskey: String, + debug: Boolean, headers: Http.Headers, dummy: Boolean, wsInit: WebSocketClient.() -> Unit = {}, @@ -67,7 +67,7 @@ suspend fun WebSocketClient( url: String, protocols: List? = null, origin: String? = null, - wskey: String? = DEFAULT_WSKEY, + wskey: String = DEFAULT_WSKEY, debug: Boolean = false, headers: Http.Headers = Http.Headers(), wsInit: WebSocketClient.() -> Unit = {}, diff --git a/korio/src/commonTest/kotlin/com/soywiz/korio/net/ws/RawSocketWebSocketClientTest.kt b/korio/src/commonTest/kotlin/com/soywiz/korio/net/ws/RawSocketWebSocketClientTest.kt index f1f590fedc..99f0d29221 100644 --- a/korio/src/commonTest/kotlin/com/soywiz/korio/net/ws/RawSocketWebSocketClientTest.kt +++ b/korio/src/commonTest/kotlin/com/soywiz/korio/net/ws/RawSocketWebSocketClientTest.kt @@ -14,8 +14,7 @@ import com.soywiz.korio.stream.write16BE import com.soywiz.korio.stream.writeBytes import com.soywiz.korio.stream.writeString import com.soywiz.krypto.encoding.hex -import kotlin.test.Test -import kotlin.test.assertEquals +import kotlin.test.* class RawRawSocketWebSocketClient { @Test @@ -127,4 +126,11 @@ class RawRawSocketWebSocketClient { ws.close() assertEquals("open,close[1002,testing!]", log.joinToString(",")) } + + @Test + fun testWrongSizeWSKey() = suspendTestNoJs { + assertFailsWith { + RawSocketWebSocketClient("ws://127.0.0.1/test", wskey = "...", connect = false) + } + } } diff --git a/korio/src/jsMain/kotlin/com/soywiz/korio/net/ws/WebSocketsJs.kt b/korio/src/jsMain/kotlin/com/soywiz/korio/net/ws/WebSocketsJs.kt index 78a1438de4..f2e88296b2 100644 --- a/korio/src/jsMain/kotlin/com/soywiz/korio/net/ws/WebSocketsJs.kt +++ b/korio/src/jsMain/kotlin/com/soywiz/korio/net/ws/WebSocketsJs.kt @@ -10,7 +10,7 @@ actual suspend fun WebSocketClient( url: String, protocols: List?, origin: String?, - wskey: String?, + wskey: String, debug: Boolean, headers: Http.Headers, dummy: Boolean, diff --git a/korio/src/jvmMain/kotlin/com/soywiz/korio/net/ws/WebSocketsJvm.kt b/korio/src/jvmMain/kotlin/com/soywiz/korio/net/ws/WebSocketsJvm.kt index e3754d916a..bec07c4737 100644 --- a/korio/src/jvmMain/kotlin/com/soywiz/korio/net/ws/WebSocketsJvm.kt +++ b/korio/src/jvmMain/kotlin/com/soywiz/korio/net/ws/WebSocketsJvm.kt @@ -6,7 +6,7 @@ actual suspend fun WebSocketClient( url: String, protocols: List?, origin: String?, - wskey: String?, + wskey: String, debug: Boolean, headers: Http.Headers, dummy: Boolean, diff --git a/korio/src/nativeMain/kotlin/com/soywiz/korio/net/ws/WebSocketsNative.kt b/korio/src/nativeMain/kotlin/com/soywiz/korio/net/ws/WebSocketsNative.kt index 434cbbb8df..f75b4fe517 100644 --- a/korio/src/nativeMain/kotlin/com/soywiz/korio/net/ws/WebSocketsNative.kt +++ b/korio/src/nativeMain/kotlin/com/soywiz/korio/net/ws/WebSocketsNative.kt @@ -6,7 +6,7 @@ actual suspend fun WebSocketClient( url: String, protocols: List?, origin: String?, - wskey: String?, + wskey: String, debug: Boolean, headers: Http.Headers, dummy: Boolean,