@@ -16,13 +16,14 @@ import org.mockito.kotlin.whenever
1616class DistrustedCertificatesTest {
1717
1818 private val connection = mock<HttpsURLConnection >()
19+ private val sut = DistrustedCertificates .getInstance()
1920
2021 @Test
2122 fun `throws and disconnects when the chain contains a distrusted certificate` () {
2223 val certificates = arrayOf(trustedCertificate(), distrustedCertificate())
2324 whenever(connection.serverCertificates).thenReturn(certificates)
2425
25- assertFailsWith<SSLPeerUnverifiedException > { DistrustedCertificates .verify(connection) }
26+ assertFailsWith<SSLPeerUnverifiedException > { sut .verify(connection) }
2627
2728 verify(connection).disconnect()
2829 }
@@ -32,7 +33,7 @@ class DistrustedCertificatesTest {
3233 val certificates = arrayOf(trustedCertificate())
3334 whenever(connection.serverCertificates).thenReturn(certificates)
3435
35- DistrustedCertificates .verify(connection)
36+ sut .verify(connection)
3637
3738 verify(connection, never()).disconnect()
3839 }
@@ -41,7 +42,7 @@ class DistrustedCertificatesTest {
4142 fun `accepts a connection without server certificates` () {
4243 whenever(connection.serverCertificates).thenReturn(null )
4344
44- DistrustedCertificates .verify(connection)
45+ sut .verify(connection)
4546
4647 verify(connection, never()).disconnect()
4748 }
@@ -50,26 +51,22 @@ class DistrustedCertificatesTest {
5051 fun `ignores plain HTTP connections` () {
5152 val connection = mock<HttpURLConnection >()
5253
53- DistrustedCertificates .verify(connection)
54+ sut .verify(connection)
5455
5556 verify(connection, never()).disconnect()
5657 }
5758
58- companion object {
59- /* * EM DV TLS CA - G3B as cross-signed by Sectigo's AAA Certificate Services root. */
60- fun distrustedCertificate (): Certificate =
61- DistrustedCertificatesTest ::class
62- .java
63- .classLoader!!
64- .getResourceAsStream(" emudhra-em-dv-tls-ca-g3b.pem" )!!
65- .use { CertificateFactory .getInstance(" X.509" ).generateCertificate(it) }
66-
67- private fun trustedCertificate (): Certificate {
68- val publicKey = mock<PublicKey >()
69- whenever(publicKey.encoded).thenReturn(byteArrayOf(1 , 2 , 3 ))
70- val certificate = mock<Certificate >()
71- whenever(certificate.publicKey).thenReturn(publicKey)
72- return certificate
59+ /* * EM DV TLS CA - G3B as cross-signed by Sectigo's AAA Certificate Services root. */
60+ private fun distrustedCertificate (): Certificate =
61+ javaClass.classLoader!! .getResourceAsStream(" emudhra-em-dv-tls-ca-g3b.pem" )!! .use {
62+ CertificateFactory .getInstance(" X.509" ).generateCertificate(it)
7363 }
64+
65+ private fun trustedCertificate (): Certificate {
66+ val publicKey = mock<PublicKey >()
67+ whenever(publicKey.encoded).thenReturn(byteArrayOf(1 , 2 , 3 ))
68+ val certificate = mock<Certificate >()
69+ whenever(certificate.publicKey).thenReturn(publicKey)
70+ return certificate
7471 }
7572}
0 commit comments