Browse Source

Couple more TLS tests

tags/v1.1.0
Chris Smith 5 years ago
parent
commit
445d987345
1 changed files with 52 additions and 3 deletions
  1. 52
    3
      src/test/kotlin/com/dmdirc/ktirc/io/TlsTest.kt

+ 52
- 3
src/test/kotlin/com/dmdirc/ktirc/io/TlsTest.kt View File

@@ -8,7 +8,6 @@ import kotlinx.coroutines.io.writeFully
8 8
 import kotlinx.coroutines.launch
9 9
 import kotlinx.coroutines.runBlocking
10 10
 import kotlinx.io.core.String
11
-import org.junit.jupiter.api.Assertions
12 11
 import org.junit.jupiter.api.Assertions.*
13 12
 import org.junit.jupiter.api.Test
14 13
 import org.junit.jupiter.api.parallel.Execution
@@ -184,8 +183,58 @@ internal class TlsSocketTest {
184 183
             tlsSocket.write.writeFully("Hello World\r\n".toByteArray())
185 184
 
186 185
             val bytes = clientBytesAsync.await()
187
-            Assertions.assertNotNull(bytes)
188
-            Assertions.assertEquals("Hello World\r\n", String(bytes))
186
+            assertNotNull(bytes)
187
+            assertEquals("Hello World\r\n", String(bytes))
188
+        }
189
+    }
190
+
191
+    @Test
192
+    fun `can read a string from a server over TLS`() = runBlocking<Unit> {
193
+        tlsServerSocket(12321).use { serverSocket ->
194
+            val plainSocket = PlainTextSocket(GlobalScope)
195
+            val tlsSocket = TlsSocket(GlobalScope, plainSocket, getTrustingContext(), "localhost")
196
+            val socket = GlobalScope.async {
197
+                serverSocket.accept().apply {
198
+                    GlobalScope.launch {
199
+                        getInputStream().read()
200
+                    }
201
+                }
202
+            }
203
+
204
+            tlsSocket.connect(InetSocketAddress("localhost", 12321))
205
+
206
+            GlobalScope.launch {
207
+                with (socket.await().getOutputStream()) {
208
+                    write("Hack the planet!".toByteArray())
209
+                    flush()
210
+                }
211
+            }
212
+
213
+            val buffer = tlsSocket.read()
214
+
215
+            assertNotNull(buffer)
216
+            buffer?.let {
217
+                assertEquals("Hack the planet!", String(it.array(), 0, it.limit()))
218
+            }
219
+        }
220
+    }
221
+
222
+    @Test
223
+    fun `read returns null after close`() = runBlocking {
224
+        tlsServerSocket(12321).use { serverSocket ->
225
+            val plainSocket = PlainTextSocket(GlobalScope)
226
+            val tlsSocket = TlsSocket(GlobalScope, plainSocket, getTrustingContext(), "localhost")
227
+            GlobalScope.launch {
228
+                serverSocket.accept().getInputStream().read()
229
+            }
230
+
231
+            tlsSocket.connect(InetSocketAddress("localhost", 12321))
232
+
233
+            tlsSocket.close()
234
+
235
+            val buffer = tlsSocket.read()
236
+
237
+            assertNull(buffer)
189 238
         }
190 239
     }
191 240
 

Loading…
Cancel
Save