Skip to content

Commit a0e5cc5

Browse files
committed
Correct test-only Android verification branches
Previously, the BuildConfig field used was not being evaluated at compile-time like we expected. This resulted in test-only branches not being eliminated by `javac` and remaining in the final artifact. This both makes both auditing for correctness harder and inhibits more future test-only code stripping, so we correct it by providing a more narrowly scoped, and constant, boolean determined at build time based off if any tests are being ran. This allows `javac` to remove test branches entirely like we want.
1 parent 9a7d773 commit a0e5cc5

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

android/rustls-platform-verifier/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ plugins {
33
id 'org.jetbrains.kotlin.android'
44
}
55

6+
def isTest = gradle.startParameter.taskNames.any { it.contains("Test") }
7+
68
static def getOsArch() {
79
final String hostArch = System.getProperty("os.arch")
810

@@ -28,6 +30,8 @@ android {
2830
minSdk 22
2931
targetSdk 33
3032

33+
buildConfigField "boolean", "TEST", "$isTest"
34+
3135
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3236
}
3337

android/rustls-platform-verifier/src/main/java/org/rustls/platformverifier/CertificateVerifier.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ internal object CertificateVerifier {
105105

106106
@JvmStatic
107107
private fun addMockRoot(root: ByteArray) {
108-
if (!BuildConfig.DEBUG) {
108+
if (!BuildConfig.TEST) {
109109
throw Exception("attempted to add a mock root outside a test!")
110110
}
111111

@@ -222,10 +222,10 @@ internal object CertificateVerifier {
222222
// We select them as follows:
223223
// - If built for release, only use the system trust manager. This should let all test-related
224224
// code be optimized out.
225-
// - If built for debug:
225+
// - If built for tests:
226226
// - If the mock CA store has any values, use the mock trust manager.
227227
// - Otherwise, use the system trust manager.
228-
val (trustManager, keystore) = if (!BuildConfig.DEBUG) {
228+
val (trustManager, keystore) = if (!BuildConfig.TEST) {
229229
val trustManager =
230230
systemTrustManager.value ?: return VerificationResult(StatusCode.Unavailable)
231231
Pair(trustManager, systemKeystore)
@@ -256,7 +256,7 @@ internal object CertificateVerifier {
256256
// TEST ONLY: Mock test suite cannot attempt to check revocation status if no OSCP data has been stapled,
257257
// because Android requires certificates to an specify OCSP responder for network fetch in this case.
258258
// If in testing w/o OCSP stapled, short-circuit here - only prior checks apply.
259-
if ((mockKeystore.size() != 0) && (ocspResponse == null)) {
259+
if (BuildConfig.TEST && (mockKeystore.size() != 0) && (ocspResponse == null)) {
260260
return VerificationResult(StatusCode.Ok)
261261
}
262262

0 commit comments

Comments
 (0)