Skip to content

Commit 082e511

Browse files
committed
Add connect and read timeouts
1 parent bc66a44 commit 082e511

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lib/src/test/java/at/bitfire/cert4android/CustomCertManagerTest.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@
1010

1111
package at.bitfire.cert4android
1212

13-
import androidx.annotation.VisibleForTesting
14-
import kotlinx.coroutines.flow.StateFlow
1513
import org.junit.Assume.assumeNotNull
1614
import org.junit.Before
1715
import org.junit.Test
1816
import java.io.IOException
17+
import java.net.InetSocketAddress
18+
import java.net.Socket
1919
import java.net.URL
2020
import java.security.SecureRandom
2121
import java.security.cert.CertificateException
2222
import java.security.cert.X509Certificate
23-
import java.util.logging.Level
24-
import java.util.logging.Logger
2523
import javax.net.ssl.SSLContext
2624
import javax.net.ssl.SSLSocket
2725
import javax.net.ssl.TrustManager
@@ -117,9 +115,14 @@ class CustomCertManagerTest {
117115
}
118116

119117
// Create an SSL socket and force a TLS handshake
120-
// (HttpsURLConnection performs the handshake lazily and sometimes the handshake is not
121-
// executed before this method gets called)
122-
sslContext.socketFactory.createSocket(host, port).use { socket ->
118+
val socket = Socket().apply {
119+
soTimeout = 5000 // read timeout
120+
connect(
121+
InetSocketAddress(host, port),
122+
5000 // connect timeout
123+
)
124+
}
125+
sslContext.socketFactory.createSocket(socket, host, port, true).use { socket ->
123126
val sslSocket = socket as SSLSocket
124127
// Explicitly start the handshake (gets certificate)
125128
sslSocket.startHandshake()

0 commit comments

Comments
 (0)