Skip to content

Commit e7a62c4

Browse files
committed
Move certificate retrieval to @BeforeClass
1 parent 9f59985 commit e7a62c4

File tree

1 file changed

+47
-47
lines changed

1 file changed

+47
-47
lines changed

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

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
package at.bitfire.cert4android
1212

13-
import org.junit.Assume.assumeNotNull
13+
import org.junit.AssumptionViolatedException
1414
import org.junit.Before
15+
import org.junit.BeforeClass
1516
import org.junit.Test
1617
import java.io.IOException
1718
import java.net.URL
@@ -30,16 +31,6 @@ class CustomCertManagerTest {
3031
private lateinit var certManager: CustomCertManager
3132
private lateinit var paranoidCertManager: CustomCertManager
3233

33-
private var siteCerts: List<X509Certificate>? =
34-
try {
35-
getSiteCertificates(URL("https://www.davx5.com"))
36-
} catch(_: IOException) {
37-
null
38-
}
39-
init {
40-
assumeNotNull("Couldn't load certificate from Web", siteCerts)
41-
}
42-
4334
@Before
4435
fun createCertManager() {
4536
certStore = TestCertStore()
@@ -55,18 +46,18 @@ class CustomCertManagerTest {
5546

5647
@Test
5748
fun testTrustedCertificate() {
58-
certManager.checkServerTrusted(siteCerts!!.toTypedArray(), "RSA")
49+
certManager.checkServerTrusted(siteCerts.toTypedArray(), "RSA")
5950
}
6051

6152
@Test(expected = CertificateException::class)
6253
fun testParanoidCertificate() {
63-
paranoidCertManager.checkServerTrusted(siteCerts!!.toTypedArray(), "RSA")
54+
paranoidCertManager.checkServerTrusted(siteCerts.toTypedArray(), "RSA")
6455
}
6556

6657
@Test
6758
fun testAddCustomCertificate() {
6859
addTrustedCertificate()
69-
paranoidCertManager.checkServerTrusted(siteCerts!!.toTypedArray(), "RSA")
60+
paranoidCertManager.checkServerTrusted(siteCerts.toTypedArray(), "RSA")
7061
}
7162

7263
@Test(expected = CertificateException::class)
@@ -77,51 +68,60 @@ class CustomCertManagerTest {
7768
// should now be rejected for the whole session
7869
addUntrustedCertificate()
7970

80-
paranoidCertManager.checkServerTrusted(siteCerts!!.toTypedArray(), "RSA")
71+
paranoidCertManager.checkServerTrusted(siteCerts.toTypedArray(), "RSA")
8172
}
8273

8374

8475
// helpers
8576

8677
private fun addTrustedCertificate() {
87-
certStore.setTrustedByUser(siteCerts!!.first())
78+
certStore.setTrustedByUser(siteCerts.first())
8879
}
8980

9081
private fun addUntrustedCertificate() {
91-
certStore.setUntrustedByUser(siteCerts!!.first())
82+
certStore.setUntrustedByUser(siteCerts.first())
9283
}
9384

94-
/**
95-
* Get the certificates of a site (bypassing all trusted checks).
96-
*
97-
* @param url the URL to get the certificates from
98-
* @return the certificates of the site
99-
*/
100-
fun getSiteCertificates(url: URL): List<X509Certificate> {
101-
val conn = url.openConnection() as HttpsURLConnection
102-
try {
103-
conn.connectTimeout = 5000
104-
conn.readTimeout = 5000
105-
conn.hostnameVerifier = HostnameVerifier { _, _ -> true }
106-
conn.sslSocketFactory = SSLContext.getInstance("TLS").apply {
107-
init(
108-
null,
109-
arrayOf<TrustManager>(object : X509TrustManager {
110-
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
111-
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
112-
override fun getAcceptedIssuers(): Array<X509Certificate> = emptyArray()
113-
}),
114-
SecureRandom()
115-
)
116-
}.socketFactory
117-
conn.inputStream.use {
118-
it.read()
119-
val certs = mutableListOf<X509Certificate>()
120-
conn.serverCertificates.forEach { certs += it as X509Certificate }
121-
return certs
85+
companion object {
86+
private lateinit var siteCerts: List<X509Certificate>
87+
88+
@JvmStatic
89+
@BeforeClass
90+
fun setUp() {
91+
siteCerts = try {
92+
getSiteCertificates(URL("https://www.davx5.com"))
93+
} catch (_: IOException) {
94+
// Skip all tests if the certs can't be fetched
95+
throw AssumptionViolatedException("Couldn't load certificate from Web")
96+
}
97+
}
98+
99+
fun getSiteCertificates(url: URL): List<X509Certificate> {
100+
val conn = url.openConnection() as HttpsURLConnection
101+
try {
102+
conn.connectTimeout = 5000
103+
conn.readTimeout = 5000
104+
conn.hostnameVerifier = HostnameVerifier { _, _ -> true }
105+
conn.sslSocketFactory = SSLContext.getInstance("TLS").apply {
106+
init(
107+
null,
108+
arrayOf<TrustManager>(object : X509TrustManager {
109+
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
110+
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
111+
override fun getAcceptedIssuers(): Array<X509Certificate> = emptyArray()
112+
}),
113+
SecureRandom()
114+
)
115+
}.socketFactory
116+
conn.inputStream.use { stream ->
117+
stream.read()
118+
val certs = mutableListOf<X509Certificate>()
119+
conn.serverCertificates.forEach { certs += it as X509Certificate }
120+
return certs
121+
}
122+
} finally {
123+
conn.disconnect()
122124
}
123-
} finally {
124-
conn.disconnect()
125125
}
126126
}
127127

0 commit comments

Comments
 (0)