Cleanup and fix CI tests (#1464)

This commit is contained in:
Harsh Shandilya
2021-07-19 16:31:37 +05:30
committed by GitHub
parent 921e9f96b9
commit 392ad847bf
10 changed files with 162 additions and 53 deletions

View File

@@ -54,7 +54,7 @@ jobs:
if: ${{ steps.service-changed.outputs.result == 'true' }} if: ${{ steps.service-changed.outputs.result == 'true' }}
uses: burrunan/gradle-cache-action@03c71a8ba93d670980695505f48f49daf43704a6 uses: burrunan/gradle-cache-action@03c71a8ba93d670980695505f48f49daf43704a6
with: with:
arguments: apiCheck testFreeDebug lintFreeDebug spotlessCheck arguments: apiCheck test lintFreeDebug spotlessCheck -PslimTests
- name: (Fail-only) upload test report - name: (Fail-only) upload test report
if: failure() if: failure()

View File

@@ -60,17 +60,10 @@ android {
disable("CoroutineCreationDuringComposition") disable("CoroutineCreationDuringComposition")
} }
flavorDimensions("free") // composeOptions {
productFlavors { // kotlinCompilerVersion = libs.versions.kotlin.get()
create("free") {} // kotlinCompilerExtensionVersion = libs.versions.compose.get()
create("nonFree") {} // }
}
testOptions { unitTests.isReturnDefaultValues = true }
composeOptions {
kotlinCompilerVersion = libs.versions.kotlin.get()
kotlinCompilerExtensionVersion = libs.versions.compose.get()
}
} }
dependencies { dependencies {

View File

@@ -13,10 +13,10 @@ class UriTotpFinder @Inject constructor() : TotpFinder {
override fun findSecret(content: String): String? { override fun findSecret(content: String): String? {
content.split("\n".toRegex()).forEach { line -> content.split("\n".toRegex()).forEach { line ->
if (line.startsWith(TOTP_FIELDS[0])) { if (line.startsWith(TotpFinder.TOTP_FIELDS[0])) {
return Uri.parse(line).getQueryParameter("secret") return Uri.parse(line).getQueryParameter("secret")
} }
if (line.startsWith(TOTP_FIELDS[1], ignoreCase = true)) { if (line.startsWith(TotpFinder.TOTP_FIELDS[1], ignoreCase = true)) {
return line.split(": *".toRegex(), 2).toTypedArray()[1] return line.split(": *".toRegex(), 2).toTypedArray()[1]
} }
} }
@@ -42,15 +42,11 @@ class UriTotpFinder @Inject constructor() : TotpFinder {
private fun getQueryParameter(content: String, parameterName: String): String? { private fun getQueryParameter(content: String, parameterName: String): String? {
content.split("\n".toRegex()).forEach { line -> content.split("\n".toRegex()).forEach { line ->
val uri = Uri.parse(line) val uri = Uri.parse(line)
if (line.startsWith(TOTP_FIELDS[0]) && uri.getQueryParameter(parameterName) != null) { if (line.startsWith(TotpFinder.TOTP_FIELDS[0]) && uri.getQueryParameter(parameterName) != null
) {
return uri.getQueryParameter(parameterName) return uri.getQueryParameter(parameterName)
} }
} }
return null return null
} }
companion object {
val TOTP_FIELDS = arrayOf("otpauth://totp", "totp:")
}
} }

View File

@@ -13,6 +13,7 @@ import org.gradle.api.tasks.wrapper.Wrapper
import org.gradle.kotlin.dsl.maven import org.gradle.kotlin.dsl.maven
import org.gradle.kotlin.dsl.repositories import org.gradle.kotlin.dsl.repositories
import org.gradle.kotlin.dsl.withType import org.gradle.kotlin.dsl.withType
import org.gradle.language.nativeplatform.internal.BuildType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
/** /**
@@ -50,9 +51,10 @@ internal fun Project.configureForAllProjects() {
languageVersion = "1.5" languageVersion = "1.5"
} }
} }
tasks.withType<Test> { tasks.withType<Test>().configureEach {
maxParallelForks = Runtime.getRuntime().availableProcessors() * 2 maxParallelForks = Runtime.getRuntime().availableProcessors() * 2
testLogging { events(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED) } testLogging { events(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED) }
outputs.upToDateWhen { false }
} }
} }
@@ -79,13 +81,19 @@ internal fun BaseAppModuleExtension.configureAndroidApplicationOptions(project:
buildConfig = true buildConfig = true
} }
flavorDimensions(FlavorDimensions.FREE)
productFlavors {
create(ProductFlavors.FREE) {}
create(ProductFlavors.NON_FREE) {}
}
buildTypes { buildTypes {
named("release") { named(BuildType.RELEASE.name) {
isMinifyEnabled = !minifySwitch.isPresent isMinifyEnabled = !minifySwitch.isPresent
setProguardFiles(listOf("proguard-android-optimize.txt", "proguard-rules.pro")) setProguardFiles(listOf("proguard-android-optimize.txt", "proguard-rules.pro"))
buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "${project.isSnapshot()}") buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "${project.isSnapshot()}")
} }
named("debug") { named(BuildType.DEBUG.name) {
applicationIdSuffix = ".debug" applicationIdSuffix = ".debug"
versionNameSuffix = "-debug" versionNameSuffix = "-debug"
isMinifyEnabled = false isMinifyEnabled = false
@@ -121,5 +129,8 @@ internal fun TestedExtension.configureCommonAndroidOptions() {
targetCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8
} }
testOptions.animationsDisabled = true testOptions {
animationsDisabled = true
unitTests.isReturnDefaultValues = true
}
} }

View File

@@ -43,6 +43,7 @@ class PasswordStorePlugin : Plugin<Project> {
is LibraryPlugin -> { is LibraryPlugin -> {
project.extensions.getByType<TestedExtension>().configureCommonAndroidOptions() project.extensions.getByType<TestedExtension>().configureCommonAndroidOptions()
project.configureExplicitApi() project.configureExplicitApi()
project.configureSlimTests()
} }
is AppPlugin -> { is AppPlugin -> {
project project
@@ -51,6 +52,7 @@ class PasswordStorePlugin : Plugin<Project> {
.configureAndroidApplicationOptions(project) .configureAndroidApplicationOptions(project)
project.extensions.getByType<BaseAppModuleExtension>().configureBuildSigning(project) project.extensions.getByType<BaseAppModuleExtension>().configureBuildSigning(project)
project.extensions.getByType<TestedExtension>().configureCommonAndroidOptions() project.extensions.getByType<TestedExtension>().configureCommonAndroidOptions()
project.configureSlimTests()
} }
is SigningPlugin -> { is SigningPlugin -> {
project.extensions.getByType<SigningExtension>().configureBuildSigning() project.extensions.getByType<SigningExtension>().configureBuildSigning()

View File

@@ -0,0 +1,13 @@
/*
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
object FlavorDimensions {
const val FREE = "free"
}
object ProductFlavors {
const val FREE = "free"
const val NON_FREE = "nonFree"
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
import com.android.build.api.extension.ApplicationAndroidComponentsExtension
import com.android.build.api.extension.LibraryAndroidComponentsExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.findByType
import org.gradle.language.nativeplatform.internal.BuildType
/**
* When the "slimTests" project property is provided, disable the unit test tasks on `release` build
* type and `nonFree` product flavor to avoid running the same tests repeatedly in different build
* variants.
*
* Examples: `./gradlew test -PslimTests` will run unit tests for `nonFreeDebug` and `debug` build
* variants in Android App and Library projects, and all tests in JVM projects.
*/
internal fun Project.configureSlimTests() {
if (providers.gradleProperty(SLIM_TESTS_PROPERTY).forUseAtConfigurationTime().isPresent) {
// disable unit test tasks on the release build type for Android Library projects
extensions.findByType<LibraryAndroidComponentsExtension>()?.run {
beforeUnitTests(selector().withBuildType(BuildType.RELEASE.name)) { it.enabled = false }
}
// disable unit test tasks on the release build type and free flavor for Android Application
// projects.
extensions.findByType<ApplicationAndroidComponentsExtension>()?.run {
beforeUnitTests(selector().withBuildType(BuildType.RELEASE.name)) { it.enabled = false }
beforeUnitTests(selector().withFlavor(FlavorDimensions.FREE to ProductFlavors.NON_FREE)) {
it.enabled = false
}
}
}
}
private const val SLIM_TESTS_PROPERTY = "slimTests"

View File

@@ -122,8 +122,7 @@ constructor(
foundUsername = true foundUsername = true
false false
} }
line.startsWith("otpauth://", ignoreCase = true) || TotpFinder.TOTP_FIELDS.any { prefix -> line.startsWith(prefix, ignoreCase = true) } -> {
line.startsWith("totp:", ignoreCase = true) -> {
false false
} }
else -> { else -> {

View File

@@ -186,6 +186,9 @@ internal class PasswordEntryTest {
override fun findAlgorithm(content: String): String { override fun findAlgorithm(content: String): String {
return "SHA1" return "SHA1"
} }
override fun findIssuer(content: String): String {
return "ACME Co"
}
} }
} }
} }

View File

@@ -13,19 +13,35 @@ import org.junit.Test
internal class OtpTest { internal class OtpTest {
private fun generateOtp(
counter: Long,
secret: String = "JBSWY3DPEHPK3PXP",
algorithm: String = "SHA1",
digits: String = "6",
issuer: String? = null,
): String? {
return Otp.calculateCode(secret, counter, algorithm, digits, issuer).get()
}
@Test @Test
fun testOtpGeneration6Digits() { fun testOtpGeneration6Digits() {
assertEquals( assertEquals(
"953550", "953550",
Otp.calculateCode("JBSWY3DPEHPK3PXP", 1593333298159 / (1000 * 30), "SHA1", "6").get() generateOtp(
counter = 1593333298159 / (1000 * 30),
)
) )
assertEquals( assertEquals(
"275379", "275379",
Otp.calculateCode("JBSWY3DPEHPK3PXP", 1593333571918 / (1000 * 30), "SHA1", "6").get() generateOtp(
counter = 1593333571918 / (1000 * 30),
)
) )
assertEquals( assertEquals(
"867507", "867507",
Otp.calculateCode("JBSWY3DPEHPK3PXP", 1593333600517 / (1000 * 57), "SHA1", "6").get() generateOtp(
counter = 1593333600517 / (1000 * 57),
)
) )
} }
@@ -33,36 +49,79 @@ internal class OtpTest {
fun testOtpGeneration10Digits() { fun testOtpGeneration10Digits() {
assertEquals( assertEquals(
"0740900914", "0740900914",
Otp.calculateCode("JBSWY3DPEHPK3PXP", 1593333655044 / (1000 * 30), "SHA1", "10").get() generateOtp(
counter = 1593333655044 / (1000 * 30),
digits = "10",
)
) )
assertEquals( assertEquals(
"0070632029", "0070632029",
Otp.calculateCode("JBSWY3DPEHPK3PXP", 1593333691405 / (1000 * 30), "SHA1", "10").get() generateOtp(
counter = 1593333691405 / (1000 * 30),
digits = "10",
)
) )
assertEquals( assertEquals(
"1017265882", "1017265882",
Otp.calculateCode("JBSWY3DPEHPK3PXP", 1593333728893 / (1000 * 83), "SHA1", "10").get() generateOtp(
counter = 1593333728893 / (1000 * 83),
digits = "10",
)
) )
} }
@Test @Test
fun testOtpGenerationIllegalInput() { fun testOtpGenerationIllegalInput() {
assertNull(Otp.calculateCode("JBSWY3DPEHPK3PXP", 10000, "SHA0", "10").get()) assertNull(
assertNull(Otp.calculateCode("JBSWY3DPEHPK3PXP", 10000, "SHA1", "a").get()) generateOtp(
assertNull(Otp.calculateCode("JBSWY3DPEHPK3PXP", 10000, "SHA1", "5").get()) counter = 10000,
assertNull(Otp.calculateCode("JBSWY3DPEHPK3PXP", 10000, "SHA1", "11").get()) algorithm = "SHA0",
assertNull(Otp.calculateCode("JBSWY3DPEHPK3PXPAAAAB", 10000, "SHA1", "6").get()) digits = "10",
)
)
assertNull(
generateOtp(
counter = 10000,
digits = "a",
)
)
assertNull(
generateOtp(
counter = 10000,
algorithm = "SHA1",
digits = "5",
)
)
assertNull(
generateOtp(
counter = 10000,
digits = "11",
)
)
assertNull(
generateOtp(
counter = 10000,
secret = "JBSWY3DPEHPK3PXPAAAAB",
digits = "6",
)
)
} }
@Test @Test
fun testOtpGenerationUnusualSecrets() { fun testOtpGenerationUnusualSecrets() {
assertEquals( assertEquals(
"127764", "127764",
Otp.calculateCode("JBSWY3DPEHPK3PXPAAAAAAAA", 1593367111963 / (1000 * 30), "SHA1", "6").get() generateOtp(
counter = 1593367111963 / (1000 * 30),
secret = "JBSWY3DPEHPK3PXPAAAAAAAA",
)
) )
assertEquals( assertEquals(
"047515", "047515",
Otp.calculateCode("JBSWY3DPEHPK3PXPAAAAA", 1593367171420 / (1000 * 30), "SHA1", "6").get() generateOtp(
counter = 1593367171420 / (1000 * 30),
secret = "JBSWY3DPEHPK3PXPAAAAA",
)
) )
} }
@@ -72,21 +131,16 @@ internal class OtpTest {
// We don't care for the resultant OTP's actual value, we just want both the padded and // We don't care for the resultant OTP's actual value, we just want both the padded and
// unpadded variant to generate the same one. // unpadded variant to generate the same one.
val unpaddedOtp = val unpaddedOtp =
Otp.calculateCode( generateOtp(
"ON2HE2LOM4QHO2LUNAQHG33NMUQHAYLEMRUW4ZZANZSWKZDFMQFA", counter = 1593367171420 / (1000 * 30),
1593367171420 / (1000 * 30), secret = "ON2HE2LOM4QHO2LUNAQHG33NMUQHAYLEMRUW4ZZANZSWKZDFMQFA",
"SHA1", )
"6"
)
.get()
val paddedOtp = val paddedOtp =
Otp.calculateCode( generateOtp(
"ON2HE2LOM4QHO2LUNAQHG33NMUQHAYLEMRUW4ZZANZSWKZDFMQFA====", 1593367171420 / (1000 * 30),
1593367171420 / (1000 * 30), secret = "ON2HE2LOM4QHO2LUNAQHG33NMUQHAYLEMRUW4ZZANZSWKZDFMQFA====",
"SHA1", )
"6"
)
.get()
assertNotNull(unpaddedOtp) assertNotNull(unpaddedOtp)
assertNotNull(paddedOtp) assertNotNull(paddedOtp)
assertEquals(unpaddedOtp, paddedOtp) assertEquals(unpaddedOtp, paddedOtp)