|
@@ -7,18 +7,14 @@ import org.junit.jupiter.api.Test
|
7
|
7
|
import org.junit.jupiter.api.parallel.Execution
|
8
|
8
|
import org.junit.jupiter.api.parallel.ExecutionMode
|
9
|
9
|
import java.net.ServerSocket
|
10
|
|
-import java.security.KeyStore
|
11
|
|
-import java.security.cert.X509Certificate
|
12
|
|
-import javax.net.ssl.KeyManagerFactory
|
13
|
|
-import javax.net.ssl.SSLContext
|
14
|
|
-import javax.net.ssl.X509TrustManager
|
15
|
10
|
|
|
11
|
+@Suppress("BlockingMethodInNonBlockingContext")
|
16
|
12
|
@ExperimentalCoroutinesApi
|
17
|
13
|
@Execution(ExecutionMode.SAME_THREAD)
|
18
|
14
|
internal class LineBufferedSocketImplTest {
|
19
|
15
|
|
20
|
16
|
@Test
|
21
|
|
- fun `KtorLineBufferedSocket can connect to a server`() = runBlocking {
|
|
17
|
+ fun `can connect to a server`() = runBlocking {
|
22
|
18
|
ServerSocket(12321).use { serverSocket ->
|
23
|
19
|
val socket = LineBufferedSocketImpl(GlobalScope, "localhost", "localhost", 12321)
|
24
|
20
|
val clientSocketAsync = GlobalScope.async { serverSocket.accept() }
|
|
@@ -30,7 +26,7 @@ internal class LineBufferedSocketImplTest {
|
30
|
26
|
}
|
31
|
27
|
|
32
|
28
|
@Test
|
33
|
|
- fun `KtorLineBufferedSocket can send a byte array to a server`() = runBlocking {
|
|
29
|
+ fun `can send a byte array to a server`() = runBlocking {
|
34
|
30
|
ServerSocket(12321).use { serverSocket ->
|
35
|
31
|
val socket = LineBufferedSocketImpl(GlobalScope, "localhost", "localhost", 12321)
|
36
|
32
|
val clientBytesAsync = GlobalScope.async {
|
|
@@ -49,7 +45,7 @@ internal class LineBufferedSocketImplTest {
|
49
|
45
|
}
|
50
|
46
|
|
51
|
47
|
@Test
|
52
|
|
- fun `KtorLineBufferedSocket can send a string to a server over TLS`() = runBlocking {
|
|
48
|
+ fun `can send a string to a server over TLS`() = runBlocking {
|
53
|
49
|
tlsServerSocket(12321).use { serverSocket ->
|
54
|
50
|
val socket = LineBufferedSocketImpl(GlobalScope, "localhost", "localhost", 12321, true)
|
55
|
51
|
socket.tlsTrustManager = getTrustingManager()
|
|
@@ -69,7 +65,7 @@ internal class LineBufferedSocketImplTest {
|
69
|
65
|
}
|
70
|
66
|
|
71
|
67
|
@Test
|
72
|
|
- fun `KtorLineBufferedSocket can receive a line of CRLF delimited text`() = runBlocking {
|
|
68
|
+ fun `can receive a line of CRLF delimited text`() = runBlocking {
|
73
|
69
|
ServerSocket(12321).use { serverSocket ->
|
74
|
70
|
val socket = LineBufferedSocketImpl(GlobalScope, "localhost", "localhost", 12321)
|
75
|
71
|
GlobalScope.launch {
|
|
@@ -82,7 +78,7 @@ internal class LineBufferedSocketImplTest {
|
82
|
78
|
}
|
83
|
79
|
|
84
|
80
|
@Test
|
85
|
|
- fun `KtorLineBufferedSocket can receive a line of LF delimited text`() = runBlocking {
|
|
81
|
+ fun `can receive a line of LF delimited text`() = runBlocking {
|
86
|
82
|
ServerSocket(12321).use { serverSocket ->
|
87
|
83
|
val socket = LineBufferedSocketImpl(GlobalScope, "localhost", "localhost", 12321)
|
88
|
84
|
GlobalScope.launch {
|
|
@@ -95,7 +91,7 @@ internal class LineBufferedSocketImplTest {
|
95
|
91
|
}
|
96
|
92
|
|
97
|
93
|
@Test
|
98
|
|
- fun `KtorLineBufferedSocket can receive multiple lines of text in one packet`() = runBlocking {
|
|
94
|
+ fun `can receive multiple lines of text in one packet`() = runBlocking {
|
99
|
95
|
ServerSocket(12321).use { serverSocket ->
|
100
|
96
|
val socket = LineBufferedSocketImpl(GlobalScope, "localhost", "localhost", 12321)
|
101
|
97
|
GlobalScope.launch {
|
|
@@ -110,7 +106,7 @@ internal class LineBufferedSocketImplTest {
|
110
|
106
|
}
|
111
|
107
|
|
112
|
108
|
@Test
|
113
|
|
- fun `KtorLineBufferedSocket can receive multiple long lines of text`() = runBlocking {
|
|
109
|
+ fun `can receive multiple long lines of text`() = runBlocking {
|
114
|
110
|
ServerSocket(12321).use { serverSocket ->
|
115
|
111
|
val socket = LineBufferedSocketImpl(GlobalScope, "localhost", "localhost", 12321)
|
116
|
112
|
val line1 = "abcdefghijklmnopqrstuvwxyz".repeat(500)
|
|
@@ -129,7 +125,7 @@ internal class LineBufferedSocketImplTest {
|
129
|
125
|
}
|
130
|
126
|
|
131
|
127
|
@Test
|
132
|
|
- fun `KtorLineBufferedSocket can receive one line of text over multiple packets`() = runBlocking {
|
|
128
|
+ fun `can receive one line of text over multiple packets`() = runBlocking {
|
133
|
129
|
ServerSocket(12321).use { serverSocket ->
|
134
|
130
|
val socket = LineBufferedSocketImpl(GlobalScope, "localhost", "localhost", 12321)
|
135
|
131
|
GlobalScope.launch {
|
|
@@ -150,7 +146,7 @@ internal class LineBufferedSocketImplTest {
|
150
|
146
|
}
|
151
|
147
|
|
152
|
148
|
@Test
|
153
|
|
- fun `KtorLineBufferedSocket returns from readLines when socket is closed`() = runBlocking {
|
|
149
|
+ fun `returns from readLines when socket is closed`() = runBlocking {
|
154
|
150
|
ServerSocket(12321).use { serverSocket ->
|
155
|
151
|
val socket = LineBufferedSocketImpl(GlobalScope, "localhost", "localhost", 12321)
|
156
|
152
|
GlobalScope.launch {
|
|
@@ -167,7 +163,7 @@ internal class LineBufferedSocketImplTest {
|
167
|
163
|
}
|
168
|
164
|
|
169
|
165
|
@Test
|
170
|
|
- fun `KtorLineBufferedSocket disconnects from server`() = runBlocking {
|
|
166
|
+ fun `disconnects from server`() = runBlocking {
|
171
|
167
|
ServerSocket(12321).use { serverSocket ->
|
172
|
168
|
val socket = LineBufferedSocketImpl(GlobalScope, "localhost", "localhost", 12321)
|
173
|
169
|
val clientSocketAsync = GlobalScope.async { serverSocket.accept() }
|
|
@@ -179,25 +175,4 @@ internal class LineBufferedSocketImplTest {
|
179
|
175
|
}
|
180
|
176
|
}
|
181
|
177
|
|
182
|
|
- private fun tlsServerSocket(port: Int): ServerSocket {
|
183
|
|
- val keyStore = KeyStore.getInstance("PKCS12")
|
184
|
|
- keyStore.load(LineBufferedSocketImplTest::class.java.getResourceAsStream("localhost.p12"), CharArray(0))
|
185
|
|
-
|
186
|
|
- val keyManagerFactory = KeyManagerFactory.getInstance("PKIX")
|
187
|
|
- keyManagerFactory.init(keyStore, CharArray(0))
|
188
|
|
-
|
189
|
|
- val sslContext = SSLContext.getInstance("TLSv1.2")
|
190
|
|
- sslContext.init(keyManagerFactory.keyManagers, null, null)
|
191
|
|
- return sslContext.serverSocketFactory.createServerSocket(port)
|
192
|
|
- }
|
193
|
|
-
|
194
|
|
- private fun getTrustingManager() = object : X509TrustManager {
|
195
|
|
- override fun getAcceptedIssuers(): Array<X509Certificate> = emptyArray()
|
196
|
|
-
|
197
|
|
- override fun checkClientTrusted(certs: Array<X509Certificate>, authType: String) {}
|
198
|
|
-
|
199
|
|
- override fun checkServerTrusted(certs: Array<X509Certificate>, authType: String) {}
|
200
|
|
- }
|
201
|
|
-
|
202
|
|
-
|
203
|
178
|
}
|