From e58f8fa87ffaa348a4adb0b901dd8f8c5a9b2d2c Mon Sep 17 00:00:00 2001 From: Daniel Llewellyn Date: Fri, 22 Sep 2023 08:16:29 +0100 Subject: [PATCH] Revert "Various fixes to tests" This reverts commit 1cd8eb8f85bf0d0812fa7759c879ea0777fd167d. --- app/build.gradle | 2 +- .../java/com/andro/secure/MainActivity.kt | 50 ++++---- .../com/andro/secure/report/ReportFragment.kt | 56 ++++----- build.gradle | 1 - gradle.properties | 1 + gradle/libs.versions.toml | 4 +- safeToRun/build.gradle | 6 +- .../features/oscheck/OSConfigurationChecks.kt | 58 ++++----- .../oscheck/OSInformationQueryAndroid.kt | 48 +------- .../features/oscheck/emulator/emulators.kt | 26 ++-- .../OSInformationQueryAndroidTest.kt | 61 ++++++++++ .../kotlin/com/safetorun/OsInformationMock.kt | 60 +++++++++ .../oscheck/OSConfigurationChecksTest.kt | 65 +++++----- .../oscheck/emulator/EmulatorsKtTest.kt | 114 +++++++++--------- .../AndroidSafeToRunOffDeviceTest.kt | 3 +- .../safetorun/utils/osConfigurationChecks.kt | 14 +++ safeToRunInputValidation/build.gradle | 15 +-- safeToRunInternal/build.gradle | 8 -- safetorunpinscreen/build.gradle | 16 +-- scripts/dependencies/test_dependencies.gradle | 1 + 20 files changed, 327 insertions(+), 282 deletions(-) create mode 100644 safeToRun/src/test/kotlin/com/safetorun/OSInformationQueryAndroidTest.kt create mode 100644 safeToRun/src/test/kotlin/com/safetorun/OsInformationMock.kt create mode 100644 safeToRun/src/test/kotlin/com/safetorun/utils/osConfigurationChecks.kt diff --git a/app/build.gradle b/app/build.gradle index 44c2c290..ad935c56 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -33,7 +33,7 @@ android { } kotlinOptions { - jvmTarget = JavaVersion.VERSION_11.toString() + jvmTarget = "1.8" } buildTypes { diff --git a/app/src/main/java/com/andro/secure/MainActivity.kt b/app/src/main/java/com/andro/secure/MainActivity.kt index 5445d6db..6d889ef1 100644 --- a/app/src/main/java/com/andro/secure/MainActivity.kt +++ b/app/src/main/java/com/andro/secure/MainActivity.kt @@ -41,31 +41,31 @@ class MainActivity : AppCompatActivity() { private inline fun Context.canIRun(checkName: String, actionOnFailure: () -> Unit) { -// if (safeToRunWithLogger( -// logger = logger(checkName), -// { banAvdEmulatorCheck() }, -// { banGenymotionEmulatorCheck() }, -// { banBluestacksEmulatorCheck() }, -// { blacklistedAppCheck("Test app", "Test app 2") }, -// { rootDetectionCheck() }, -// { -// safeToRunCombinedCheck( -// listOf( -// { bannedHardwareCheck("hardware") }, -// { bannedBoardCheck("board") } -// ) -// ) -// }, -// { -// safeToRunCombinedCheck( -// listOf { installOriginCheckWithDefaultsCheck() }, -// listOf { !BuildConfig.DEBUG } -// ) -// }, -// )() -// ) { -// actionOnFailure() -// } + if (safeToRunWithLogger( + logger = logger(checkName), + { banAvdEmulatorCheck() }, + { banGenymotionEmulatorCheck() }, + { banBluestacksEmulatorCheck() }, + { blacklistedAppCheck("Test app", "Test app 2") }, + { rootDetectionCheck() }, + { + safeToRunCombinedCheck( + listOf( + { bannedHardwareCheck("hardware") }, + { bannedBoardCheck("board") } + ) + ) + }, + { + safeToRunCombinedCheck( + listOf { installOriginCheckWithDefaultsCheck() }, + listOf { !BuildConfig.DEBUG } + ) + }, + )() + ) { + actionOnFailure() + } } override fun onCreateOptionsMenu(menu: Menu?): Boolean { diff --git a/app/src/main/java/com/andro/secure/report/ReportFragment.kt b/app/src/main/java/com/andro/secure/report/ReportFragment.kt index 00715455..9902b58f 100644 --- a/app/src/main/java/com/andro/secure/report/ReportFragment.kt +++ b/app/src/main/java/com/andro/secure/report/ReportFragment.kt @@ -68,34 +68,34 @@ class ReportFragment : Fragment() { private inline fun Context.canIRun(checkName: String, actionOnFailure: () -> Unit) { -// if (safeToRunWithLogger( -// logger = loggerForCheck(checkName), -// { verifySignatureCheck() }, -// { banAvdEmulatorCheck() }, -// { banGenymotionEmulatorCheck() }, -// { banBluestacksEmulatorCheck() }, -// { blacklistedAppCheck("Test app", "Test app 2") }, -// { rootDetectionCheck() }, -// { isDebuggableCheck() }, -// { -// safeToRunCombinedCheck( -// listOf( -// { bannedHardwareCheck("hardware") }, -// { bannedBoardCheck("board") } -// ) -// ) -// }, -// { -// safeToRunCombinedCheck( -// listOf { installOriginCheckWithDefaultsCheck() }, -// listOf { !BuildConfig.DEBUG } -// ) -// }, -// { verifySignatureCheck("Abc") } -// )() -// ) { -// actionOnFailure() -// } + if (safeToRunWithLogger( + logger = loggerForCheck(checkName), + { verifySignatureCheck() }, + { banAvdEmulatorCheck() }, + { banGenymotionEmulatorCheck() }, + { banBluestacksEmulatorCheck() }, + { blacklistedAppCheck("Test app", "Test app 2") }, + { rootDetectionCheck() }, + { isDebuggableCheck() }, + { + safeToRunCombinedCheck( + listOf( + { bannedHardwareCheck("hardware") }, + { bannedBoardCheck("board") } + ) + ) + }, + { + safeToRunCombinedCheck( + listOf { installOriginCheckWithDefaultsCheck() }, + listOf { !BuildConfig.DEBUG } + ) + }, + { verifySignatureCheck("Abc") } + )() + ) { + actionOnFailure() + } } companion object { diff --git a/build.gradle b/build.gradle index 90853e4f..cc751221 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,6 @@ buildscript { } - plugins { id "com.diffplug.spotless" version "5.7.0" id("io.github.gradle-nexus.publish-plugin") version "1.1.0" diff --git a/gradle.properties b/gradle.properties index 4788b76a..1be875db 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,6 +11,7 @@ recyclerView=1.2.1 jwtVersion=3.15.0 mockHttpVersion=5.11.2 coreKtxVersion=1.6.0 +robolectricVersion=4.4 jupiterVersion=5.8.1 junitVersion=4.13.2 androidXTest=1.4.0 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0b565a85..40f6f26d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,7 +3,6 @@ kotlin = "1.8.10" mockk = "1.13.5" rootbeer = "0.1.0" googleTruth = "1.1.3" -securityCrypto = "1.0.0" spek_ = "2.0.12" epoxy = "4.6.4" recyclerView = "1.3.0" @@ -12,7 +11,7 @@ jwt = "3.15.0" serialization = "1.5.0" mockHttp = "5.11.2" coreKtx = "1.6.0" -robolectric = "4.10.3" +robolectric = "4.4" jupiter = "5.8.1" junit = "4.13.2" androidXTest = "1.4.0" @@ -26,7 +25,6 @@ safeToRun = "2.2.4" work_version = "2.8.1" [libraries] -androidx-security-crypto = { module = "androidx.security:security-crypto", version.ref = "securityCrypto" } kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "serialization" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialization" } kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } diff --git a/safeToRun/build.gradle b/safeToRun/build.gradle index 8c2dc162..61982128 100644 --- a/safeToRun/build.gradle +++ b/safeToRun/build.gradle @@ -30,12 +30,12 @@ android { } compileOptions { - sourceCompatibility JavaVersion.VERSION_11 - targetCompatibility JavaVersion.VERSION_11 + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { - jvmTarget = JavaVersion.VERSION_11.toString() + jvmTarget = JavaVersion.VERSION_1_8.toString() } buildTypes { diff --git a/safeToRun/src/main/java/com/safetorun/features/oscheck/OSConfigurationChecks.kt b/safeToRun/src/main/java/com/safetorun/features/oscheck/OSConfigurationChecks.kt index 155f1b10..afed66b2 100644 --- a/safeToRun/src/main/java/com/safetorun/features/oscheck/OSConfigurationChecks.kt +++ b/safeToRun/src/main/java/com/safetorun/features/oscheck/OSConfigurationChecks.kt @@ -7,11 +7,8 @@ import android.os.Build * * @param minOSVersion minimum os version */ -inline fun minOsVersionCheck( - minOSVersion: Int, - osCheck: OSInformationQuery = osInformationQuery() -) = - minOSVersion > osCheck.osVersion() +inline fun minOsVersionCheck(minOSVersion: Int) = + minOSVersion > Build.VERSION.SDK_INT /** * Configure to disallow a manufacturer - i.e. fail if it's this @@ -19,11 +16,8 @@ inline fun minOsVersionCheck( * * @param manufacturerName name of the manufacturer */ -inline fun notManufacturerCheck( - manufacturerName: String, - osCheck: OSInformationQuery = osInformationQuery() -) = - osCheck.manufacturer() == manufacturerName +inline fun notManufacturerCheck(manufacturerName: String) = + Build.MANUFACTURER == manufacturerName /** * Ban a model from running - i.e. if you add a model that @@ -31,65 +25,57 @@ inline fun notManufacturerCheck( * * @param bannedModel the model to ban */ -inline fun bannedModelCheck( - bannedModel: String, - osCheck: OSInformationQuery = osInformationQuery() -) = - osCheck.model() == bannedModel +inline fun bannedModelCheck(bannedModel: String) = + Build.MODEL == bannedModel /** * Add a banned board to the list * * @param bannedBoard the model to ban */ -inline fun bannedBoardCheck( - bannedBoard: String, - osCheck: OSInformationQuery = osInformationQuery() -) = - osCheck.board() == bannedBoard +inline fun bannedBoardCheck(bannedBoard: String) = + Build.BOARD == bannedBoard /** * Add a banned cpuAbis * * @param cpuAbi the model to ban */ -inline fun bannedCpusCheck(cpuAbi: String, osCheck: OSInformationQuery = osInformationQuery()) = - osCheck.cpuAbi().contains(cpuAbi) +inline fun bannedCpusCheck(cpuAbi: String) = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + Build.SUPPORTED_ABIS.contains(cpuAbi) + } else { + false + } /** * Add a banned bootloader to the list * * @param bannedBootloader the model to ban */ -inline fun bannedBootloaderCheck( - bannedBootloader: String, - osCheck: OSInformationQuery = osInformationQuery() -) = - osCheck.bootloader() == bannedBootloader +inline fun bannedBootloaderCheck(bannedBootloader: String) = + Build.BOOTLOADER == bannedBootloader /** * Add a banned device * * @param device device to ban */ -inline fun bannedDeviceCheck(device: String, osCheck: OSInformationQuery = osInformationQuery()) = - osCheck.device() == device +inline fun bannedDeviceCheck(device: String) = + Build.DEVICE == device /** * Add a banned host * * @param hardware hardware to ban */ -inline fun bannedHardwareCheck( - hardware: String, - osCheck: OSInformationQuery = osInformationQuery() -) = - osCheck.hardware() == hardware +inline fun bannedHardwareCheck(hardware: String) = + Build.HARDWARE == hardware /** * Add a banned host * * @param host the model to ban */ -inline fun bannedHostCheck(host: String, osCheck: OSInformationQuery = osInformationQuery()) = - osCheck.host() == host +inline fun bannedHostCheck(host: String) = + Build.HOST == host diff --git a/safeToRun/src/main/java/com/safetorun/features/oscheck/OSInformationQueryAndroid.kt b/safeToRun/src/main/java/com/safetorun/features/oscheck/OSInformationQueryAndroid.kt index 8f193e47..5c37de64 100644 --- a/safeToRun/src/main/java/com/safetorun/features/oscheck/OSInformationQueryAndroid.kt +++ b/safeToRun/src/main/java/com/safetorun/features/oscheck/OSInformationQueryAndroid.kt @@ -2,41 +2,12 @@ package com.safetorun.features.oscheck import android.os.Build -/** - * OSInformationQueryAndroid is a concrete implementation of the OSInformationQuery interface. - * It uses Android's Build class to query various details about the device's operating system. - */ -class OSInformationQueryAndroid : OSInformationQuery { - - /** - * Returns the SDK version of the current Android operating system. - */ +internal class OSInformationQueryAndroid : OSInformationQuery { override fun osVersion(): Int = Build.VERSION.SDK_INT - - /** - * Returns the manufacturer of the device. - */ override fun manufacturer(): String = Build.MANUFACTURER - - /** - * Returns the model of the device. - */ override fun model(): String = Build.MODEL - - /** - * Returns the board of the device. - */ override fun board(): String = Build.BOARD - - /** - * Returns the bootloader version of the device. - */ override fun bootloader(): String = Build.BOOTLOADER - - /** - * Returns a list of supported ABI (Application Binary Interface). - * If the SDK version is lower than LOLLIPOP, returns an empty list. - */ override fun cpuAbi(): List = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Build.SUPPORTED_ABIS.toList() @@ -44,24 +15,7 @@ class OSInformationQueryAndroid : OSInformationQuery { emptyList() } - /** - * Returns the hostname of the device. - */ override fun host(): String = Build.HOST - - /** - * Returns the hardware information of the device. - */ override fun hardware(): String = Build.HARDWARE - - /** - * Returns the device identifier. - */ override fun device(): String = Build.DEVICE } - -/** - * Creates and returns an instance of OSInformationQueryAndroid. - * This function serves as a factory for creating OSInformationQueryAndroid instances. - */ -inline fun osInformationQuery(): OSInformationQuery = OSInformationQueryAndroid() diff --git a/safeToRun/src/main/java/com/safetorun/features/oscheck/emulator/emulators.kt b/safeToRun/src/main/java/com/safetorun/features/oscheck/emulator/emulators.kt index b6b8c264..5c2414e3 100644 --- a/safeToRun/src/main/java/com/safetorun/features/oscheck/emulator/emulators.kt +++ b/safeToRun/src/main/java/com/safetorun/features/oscheck/emulator/emulators.kt @@ -1,42 +1,40 @@ package com.safetorun.features.oscheck.emulator -import com.safetorun.features.oscheck.OSInformationQuery +import android.os.Build import com.safetorun.features.oscheck.OsCheckConstants import com.safetorun.features.oscheck.OsCheckConstants.XIAOMI -import com.safetorun.features.oscheck.osInformationQuery /** * Return true if we're running on an emulator * * @return true if we're running on avd */ -inline fun banAvdEmulatorCheck(osInformationQuery: OSInformationQuery = osInformationQuery()) = - isXiaomi(osInformationQuery).not() && (osInformationQuery.bootloader() == OsCheckConstants.UNKNOWN || - osInformationQuery.device() == OsCheckConstants.AVD_DEVICE_TYPE || - osInformationQuery.board() == OsCheckConstants.AVD_EMULATOR_BOARD) +inline fun banAvdEmulatorCheck() = + isXiaomi().not() && (Build.BOOTLOADER == OsCheckConstants.UNKNOWN || + Build.DEVICE == OsCheckConstants.AVD_DEVICE_TYPE || + Build.BOARD == OsCheckConstants.AVD_EMULATOR_BOARD) /** * Ban bluestacks * * @return true if we're running on bluestacks */ -inline fun banBluestacksEmulatorCheck(osInformationQuery: OSInformationQuery = osInformationQuery()) = - isXiaomi(osInformationQuery).not() && osInformationQuery.bootloader() == OsCheckConstants.UNKNOWN +inline fun banBluestacksEmulatorCheck() = + isXiaomi().not() && Build.BOOTLOADER == OsCheckConstants.UNKNOWN /** * Ban the genymotion emulator * * @return true if we're running on the genymotion emulator */ -inline fun banGenymotionEmulatorCheck(osInformationQuery: OSInformationQuery = osInformationQuery()) = - isXiaomi(osInformationQuery).not() && (osInformationQuery.board() == OsCheckConstants.UNKNOWN || - osInformationQuery.manufacturer() == OsCheckConstants.GENYMOTION_MANUFACTURER || - osInformationQuery.bootloader() == OsCheckConstants.UNKNOWN) +inline fun banGenymotionEmulatorCheck() = + isXiaomi().not() && (Build.BOARD == OsCheckConstants.UNKNOWN || + Build.MANUFACTURER == OsCheckConstants.GENYMOTION_MANUFACTURER || + Build.BOOTLOADER == OsCheckConstants.UNKNOWN) /** * Returns true if the device is a Xiaomi device * * @return true if we're on a Xiaomi device */ -inline fun isXiaomi(osInformationQuery: OSInformationQuery = osInformationQuery()) = - osInformationQuery.manufacturer() == XIAOMI +inline fun isXiaomi() = Build.MANUFACTURER == XIAOMI diff --git a/safeToRun/src/test/kotlin/com/safetorun/OSInformationQueryAndroidTest.kt b/safeToRun/src/test/kotlin/com/safetorun/OSInformationQueryAndroidTest.kt new file mode 100644 index 00000000..4289502a --- /dev/null +++ b/safeToRun/src/test/kotlin/com/safetorun/OSInformationQueryAndroidTest.kt @@ -0,0 +1,61 @@ +package com.safetorun + +import android.os.Build +import com.google.common.truth.Truth.assertThat +import com.safetorun.features.oscheck.OSInformationQueryAndroid +import io.mockk.mockkStatic +import junit.framework.TestCase +import java.lang.reflect.Field +import java.lang.reflect.Modifier + +internal class OSInformationQueryAndroidTest : TestCase() { + + fun `test that os information query returns data from build class`() { + // Given + mockkStatic(Build::class) + + val sdkInt = 123 + val manufacturer = "manufacturer" + val model = "brand" + val board = "board" + val bootLoader = "bootloader" + val cpuAbi = arrayOf("cpuAbi", "cpuAbi2") + val host = "host" + val hardware = "hardware" + val device = "devive" + + mockBuildField(sdkInt, "SDK_INT", Build.VERSION::class.java) + mockBuildField(manufacturer, "MANUFACTURER", Build::class.java) + mockBuildField(model, "MODEL", Build::class.java) + mockBuildField(board, "BOARD", Build::class.java) + mockBuildField(bootLoader, "BOOTLOADER", Build::class.java) + mockBuildField(cpuAbi, "SUPPORTED_ABIS", Build::class.java) + mockBuildField(host, "HOST", Build::class.java) + mockBuildField(hardware, "HARDWARE", Build::class.java) + mockBuildField(device, "DEVICE", Build::class.java) + + // When + val osInformationQuery = OSInformationQueryAndroid() + + // Then + assertThat(osInformationQuery.osVersion()).isEqualTo(sdkInt) + assertThat(osInformationQuery.manufacturer()).isEqualTo(manufacturer) + assertThat(osInformationQuery.model()).isEqualTo(model) + assertThat(osInformationQuery.board()).isEqualTo(board) + assertThat(osInformationQuery.bootloader()).isEqualTo(bootLoader) + assertThat(osInformationQuery.cpuAbi()).isEqualTo(cpuAbi.toList()) + assertThat(osInformationQuery.host()).isEqualTo(host) + assertThat(osInformationQuery.hardware()).isEqualTo(hardware) + assertThat(osInformationQuery.device()).isEqualTo(device) + } + + private fun mockBuildField(v: T, fieldName: String, clazz: Class<*>) { + val sdkIntField = clazz.getField(fieldName) + sdkIntField.isAccessible = true + Field::class.java.getDeclaredField("modifiers").also { + it.isAccessible = true + it.set(sdkIntField, sdkIntField.modifiers and Modifier.FINAL.inv()) + } + sdkIntField.set(null, v) + } +} diff --git a/safeToRun/src/test/kotlin/com/safetorun/OsInformationMock.kt b/safeToRun/src/test/kotlin/com/safetorun/OsInformationMock.kt new file mode 100644 index 00000000..0485365c --- /dev/null +++ b/safeToRun/src/test/kotlin/com/safetorun/OsInformationMock.kt @@ -0,0 +1,60 @@ +package com.safetorun + +import android.content.Context +import android.content.pm.InstallSourceInfo +import android.content.pm.PackageManager +import android.os.Build +import com.safetorun.features.installorigin.SharedInstallOrigin +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import java.lang.reflect.Field +import java.lang.reflect.Modifier + + +internal const val SDK_INT = 123 +internal const val MANUFACTURER = "manufacturer" +internal const val MODEL = "brand" +internal const val BOARD = "board" +internal const val BOOTLOADER = "bootloader" +internal val CPU_ABI = arrayOf("cpuAbi", "cpuAbi2") +internal const val HOST = "host" +internal const val HARDWARE = "hardware" +internal const val DEVICE = "devive" + +private val pm = mockk() + + +internal fun Context.setupMocks() { + mockkStatic(Build::class) + + mockBuildField(SDK_INT, "SDK_INT", Build.VERSION::class.java) + mockBuildField(MANUFACTURER, "MANUFACTURER", Build::class.java) + mockBuildField(MODEL, "MODEL", Build::class.java) + mockBuildField(BOARD, "BOARD", Build::class.java) + mockBuildField(BOOTLOADER, "BOOTLOADER", Build::class.java) + mockBuildField(CPU_ABI, "SUPPORTED_ABIS", Build::class.java) + mockBuildField(HOST, "HOST", Build::class.java) + mockBuildField(HARDWARE, "HARDWARE", Build::class.java) + mockBuildField(DEVICE, "DEVICE", Build::class.java) + + every { packageManager } returns pm + every { packageManager.getInstallerPackageName(any()) } returns SharedInstallOrigin.PACKAGE_NAME_RETURNS + every { packageName } returns "com.anything" + every { packageManager.getInstallSourceInfo(any()) } returns mockk().apply { + every { installingPackageName } returns SharedInstallOrigin.PACKAGE_NAME_RETURNS + } + + every { resources } returns SharedInstallOrigin.setupAMockResources() +} + + +private fun mockBuildField(v: T, fieldName: String, clazz: Class<*>) { + val sdkIntField = clazz.getField(fieldName) + sdkIntField.isAccessible = true + Field::class.java.getDeclaredField("modifiers").also { + it.isAccessible = true + it.set(sdkIntField, sdkIntField.modifiers and Modifier.FINAL.inv()) + } + sdkIntField.set(null, v) +} diff --git a/safeToRun/src/test/kotlin/com/safetorun/features/oscheck/OSConfigurationChecksTest.kt b/safeToRun/src/test/kotlin/com/safetorun/features/oscheck/OSConfigurationChecksTest.kt index 027cad34..6d55a76a 100644 --- a/safeToRun/src/test/kotlin/com/safetorun/features/oscheck/OSConfigurationChecksTest.kt +++ b/safeToRun/src/test/kotlin/com/safetorun/features/oscheck/OSConfigurationChecksTest.kt @@ -1,101 +1,100 @@ package com.safetorun.features.oscheck +import android.os.Build import com.google.common.truth.Truth.assertThat -import io.mockk.every -import io.mockk.mockk +import com.safetorun.utils.mockBuildField +import io.mockk.mockkStatic import junit.framework.TestCase internal class OSConfigurationChecksTest : TestCase() { - - private val osCheck by lazy { mockk() } - override fun setUp() { - every { osCheck.osVersion() } returns CURRENT_SDK - every { osCheck.manufacturer() } returns CURRENT_MANUFACTURER - every { osCheck.model() } returns CURRENT_MODEL - every { osCheck.board() } returns CURRENT_BOARD - every { osCheck.bootloader() } returns CURRENT_BOOTLOADER - every { osCheck.cpuAbi() } returns CURRENT_ABIS.toList() - every { osCheck.hardware() } returns CURRENT_HARDWARE - every { osCheck.device() } returns CURRENT_DEVICE - every { osCheck.host() } returns CURRENT_HOST + mockkStatic(Build::class) + mockBuildField(CURRENT_DEVICE, "DEVICE", Build::class.java) + mockBuildField(CURRENT_HARDWARE, "HARDWARE", Build::class.java) + mockBuildField(CURRENT_HOST, "HOST", Build::class.java) + mockBuildField(CURRENT_ABIS, "SUPPORTED_ABIS", Build::class.java) + mockBuildField(CURRENT_BOOTLOADER, "BOOTLOADER", Build::class.java) + mockBuildField(CURRENT_BOARD, "BOARD", Build::class.java) + mockBuildField(CURRENT_MODEL, "MODEL", Build::class.java) + mockBuildField(CURRENT_MANUFACTURER, "MANUFACTURER", Build::class.java) + mockBuildField(CURRENT_SDK, "SDK_INT", Build.VERSION::class.java) } fun `test that min os version check passes if os version is greater than min`() { - assertThat(minOsVersionCheck(CURRENT_SDK - 1, osCheck)).isFalse() + assertThat(minOsVersionCheck(CURRENT_SDK - 1)).isFalse() } fun `test that min os version check passes if os version is equal than min`() { - assertThat(minOsVersionCheck(CURRENT_SDK, osCheck)).isFalse() + assertThat(minOsVersionCheck(CURRENT_SDK)).isFalse() } fun `test that min os version check fails if os version is less than min`() { - assertThat(minOsVersionCheck(CURRENT_SDK + 1, osCheck)).isTrue() + assertThat(minOsVersionCheck(CURRENT_SDK + 1)).isTrue() } fun `test that manufacturer check passes if we are not a banned manufacturer`() { - assertThat(notManufacturerCheck("Not banned", osCheck)).isFalse() + assertThat(notManufacturerCheck("Not banned")).isFalse() } fun `test that manufacturer check fails if we are a banned manufacturer`() { - assertThat(notManufacturerCheck(CURRENT_MANUFACTURER, osCheck)).isTrue() + assertThat(notManufacturerCheck(CURRENT_MANUFACTURER)).isTrue() } fun `test that banned model check if we are a banned`() { - assertThat(bannedModelCheck("", osCheck)).isFalse() + assertThat(bannedModelCheck("")).isFalse() } fun `test that banned model check if we are not banned`() { - assertThat(bannedModelCheck(CURRENT_MODEL, osCheck)).isTrue() + assertThat(bannedModelCheck(CURRENT_MODEL)).isTrue() } fun `test that banned board check doesnt fail if not banned`() { - assertThat(bannedBoardCheck("", osCheck)).isFalse() + assertThat(bannedBoardCheck("")).isFalse() } fun `test that banned board check does fail if banned`() { - assertThat(bannedBoardCheck(CURRENT_BOARD, osCheck)).isTrue() + assertThat(bannedBoardCheck(CURRENT_BOARD)).isTrue() } fun `test that banned bootloader check doesnt fail if not banned`() { - assertThat(bannedBootloaderCheck("", osCheck)).isFalse() + assertThat(bannedBootloaderCheck("")).isFalse() } fun `test that banned bootloader check does fail if banned`() { - assertThat(bannedBootloaderCheck(CURRENT_BOOTLOADER, osCheck)).isTrue() + assertThat(bannedBootloaderCheck(CURRENT_BOOTLOADER)).isTrue() } fun `test that banned cpus check doesnt fail if not banned`() { - assertThat(bannedCpusCheck("", osCheck)).isFalse() + assertThat(bannedCpusCheck("")).isFalse() } fun `test that banned cpus check does fail if banned`() { - assertThat(bannedCpusCheck(CURRENT_ABIS.first(), osCheck)).isTrue() + assertThat(bannedCpusCheck(CURRENT_ABIS.first())).isTrue() } fun `test that banned device check doesnt fail if not banned`() { - assertThat(bannedDeviceCheck("", osCheck)).isFalse() + assertThat(bannedDeviceCheck("")).isFalse() } fun `test that banned device check does fail if banned`() { - assertThat(bannedDeviceCheck(CURRENT_DEVICE, osCheck)).isTrue() + assertThat(bannedDeviceCheck(CURRENT_DEVICE)).isTrue() } fun `test that banned hardware check doesnt fail if not banned`() { - assertThat(bannedHardwareCheck("", osCheck)).isFalse() + assertThat(bannedHardwareCheck("")).isFalse() } fun `test that banned hardware check does fail if banned`() { - assertThat(bannedHardwareCheck(CURRENT_HARDWARE, osCheck)).isTrue() + assertThat(bannedHardwareCheck(CURRENT_HARDWARE)).isTrue() } fun `test that banned host check doesnt fail if not banned`() { - assertThat(bannedHostCheck("", osCheck)).isFalse() + assertThat(bannedHostCheck("")).isFalse() } fun `test that banned host check does fail if banned`() { - assertThat(bannedHostCheck(CURRENT_HOST, osCheck)).isTrue() + assertThat(bannedHostCheck(CURRENT_HOST)).isTrue() } companion object { diff --git a/safeToRun/src/test/kotlin/com/safetorun/features/oscheck/emulator/EmulatorsKtTest.kt b/safeToRun/src/test/kotlin/com/safetorun/features/oscheck/emulator/EmulatorsKtTest.kt index 7dc5c2e3..aa316c84 100644 --- a/safeToRun/src/test/kotlin/com/safetorun/features/oscheck/emulator/EmulatorsKtTest.kt +++ b/safeToRun/src/test/kotlin/com/safetorun/features/oscheck/emulator/EmulatorsKtTest.kt @@ -1,122 +1,116 @@ package com.safetorun.features.oscheck.emulator +import android.os.Build import com.google.common.truth.Truth.assertThat -import com.safetorun.features.oscheck.OSInformationQuery import com.safetorun.features.oscheck.OsCheckConstants -import com.safetorun.features.oscheck.OsCheckConstants.GENYMOTION_MANUFACTURER -import io.mockk.every -import io.mockk.mockk +import com.safetorun.utils.mockBuildField import junit.framework.TestCase internal class EmulatorsKtTest : TestCase() { - private val osCheck by lazy { mockk() } override fun setUp() { super.setUp() - every { osCheck.manufacturer() } returns NOT_BANNED + mockBuildField(NOT_BANNED, "MANUFACTURER", Build::class.java) } fun `test that avd emulator check fails if bootloader is unknown`() { - every { osCheck.bootloader() } returns OsCheckConstants.UNKNOWN - every { osCheck.device() } returns OsCheckConstants.AVD_DEVICE_TYPE - every { osCheck.board() } returns NOT_BANNED - - - assertThat(banAvdEmulatorCheck(osCheck)).isTrue() + mockBuildField(OsCheckConstants.UNKNOWN, "BOOTLOADER", Build::class.java) + mockBuildField(NOT_BANNED, "DEVICE", Build::class.java) + mockBuildField(NOT_BANNED, "BOARD", Build::class.java) + assertThat(banAvdEmulatorCheck()).isTrue() } fun `test that avd emulator check fails if device is banned`() { - every { osCheck.bootloader() } returns NOT_BANNED - every { osCheck.device() } returns OsCheckConstants.AVD_DEVICE_TYPE - every { osCheck.board() } returns NOT_BANNED + mockBuildField(NOT_BANNED, "BOOTLOADER", Build::class.java) + mockBuildField(OsCheckConstants.AVD_DEVICE_TYPE, "DEVICE", Build::class.java) + mockBuildField(NOT_BANNED, "BOARD", Build::class.java) - assertThat(banAvdEmulatorCheck(osCheck)).isTrue() + assertThat(banAvdEmulatorCheck()).isTrue() } fun `test that avd emulator check fails if board is banned`() { - every { osCheck.bootloader() } returns NOT_BANNED - every { osCheck.device() } returns NOT_BANNED - every { osCheck.board() } returns OsCheckConstants.AVD_EMULATOR_BOARD + mockBuildField(NOT_BANNED, "BOOTLOADER", Build::class.java) + mockBuildField(NOT_BANNED, "DEVICE", Build::class.java) + mockBuildField(OsCheckConstants.AVD_EMULATOR_BOARD, "BOARD", Build::class.java) - assertThat(banAvdEmulatorCheck(osCheck)).isTrue() + assertThat(banAvdEmulatorCheck()).isTrue() } fun `test that avd emulator check passes if it passes`() { - every { osCheck.bootloader() } returns NOT_BANNED - every { osCheck.device() } returns NOT_BANNED - every { osCheck.board() } returns NOT_BANNED - - assertThat(banAvdEmulatorCheck(osCheck)).isFalse() + mockBuildField(NOT_BANNED, "BOOTLOADER", Build::class.java) + mockBuildField(NOT_BANNED, "DEVICE", Build::class.java) + mockBuildField(NOT_BANNED, "BOARD", Build::class.java) + assertThat(banAvdEmulatorCheck()).isFalse() } fun `test that avd bluestacks check fails if it fails`() { - every { osCheck.bootloader() } returns OsCheckConstants.UNKNOWN - assertThat(banBluestacksEmulatorCheck(osCheck)).isTrue() + mockBuildField(OsCheckConstants.UNKNOWN, "BOOTLOADER", Build::class.java) + assertThat(banBluestacksEmulatorCheck()).isTrue() } fun `test that bluestacks emulator check passes if it passes`() { - every { osCheck.bootloader() } returns NOT_BANNED - assertThat(banBluestacksEmulatorCheck(osCheck)).isFalse() + mockBuildField(NOT_BANNED, "BOOTLOADER", Build::class.java) + assertThat(banBluestacksEmulatorCheck()).isFalse() } fun `test that genymotion emulator check fails if bootloader check fails`() { - every { osCheck.bootloader() } returns OsCheckConstants.UNKNOWN - every { osCheck.manufacturer() } returns NOT_BANNED - every { osCheck.board() } returns NOT_BANNED - + mockBuildField(OsCheckConstants.UNKNOWN, "BOOTLOADER", Build::class.java) + mockBuildField(NOT_BANNED, "MANUFACTURER", Build::class.java) + mockBuildField(NOT_BANNED, "BOARD", Build::class.java) - assertThat(banGenymotionEmulatorCheck(osCheck)).isTrue() + assertThat(banGenymotionEmulatorCheck()).isTrue() } fun `test that genymotion emulator check fails if manufacturer check fails`() { + mockBuildField(NOT_BANNED, "BOOTLOADER", Build::class.java) + mockBuildField(OsCheckConstants.GENYMOTION_MANUFACTURER, "MANUFACTURER", Build::class.java) + mockBuildField(NOT_BANNED, "BOARD", Build::class.java) - every { osCheck.bootloader() } returns NOT_BANNED - every { osCheck.manufacturer() } returns GENYMOTION_MANUFACTURER - every { osCheck.board() } returns NOT_BANNED - - assertThat(banGenymotionEmulatorCheck(osCheck)).isTrue() + assertThat(banGenymotionEmulatorCheck()).isTrue() } fun `test that genymotion emulator check fails if board check fails`() { - every { osCheck.bootloader() } returns NOT_BANNED - every { osCheck.manufacturer() } returns NOT_BANNED - every { osCheck.board() } returns OsCheckConstants.UNKNOWN + mockBuildField(NOT_BANNED, "BOOTLOADER", Build::class.java) + mockBuildField(NOT_BANNED, "MANUFACTURER", Build::class.java) + mockBuildField(OsCheckConstants.UNKNOWN, "BOARD", Build::class.java) - assertThat(banGenymotionEmulatorCheck(osCheck)).isTrue() + assertThat(banGenymotionEmulatorCheck()).isTrue() } fun `test that genymotion emulator check passes if it passes`() { - every { osCheck.bootloader() } returns NOT_BANNED - every { osCheck.manufacturer() } returns NOT_BANNED - every { osCheck.board() } returns NOT_BANNED + mockBuildField(NOT_BANNED, "BOOTLOADER", Build::class.java) + mockBuildField(NOT_BANNED, "MANUFACTURER", Build::class.java) + mockBuildField(NOT_BANNED, "BOARD", Build::class.java) - assertThat(banGenymotionEmulatorCheck(osCheck)).isFalse() + assertThat(banGenymotionEmulatorCheck()).isFalse() } // Xiaomi checks fun `test that avd emulator check passes if bootloader is unknown but device is Xiaomi`() { - every { osCheck.bootloader() } returns OsCheckConstants.UNKNOWN - every { osCheck.manufacturer() } returns OsCheckConstants.XIAOMI - every { osCheck.board() } returns NOT_BANNED - every { osCheck.device() } returns NOT_BANNED + mockBuildField(OsCheckConstants.UNKNOWN, "BOOTLOADER", Build::class.java) + mockBuildField(NOT_BANNED, "DEVICE", Build::class.java) + mockBuildField(NOT_BANNED, "BOARD", Build::class.java) + mockBuildField(OsCheckConstants.XIAOMI, "MANUFACTURER", Build::class.java) - assertThat(banAvdEmulatorCheck(osCheck)).isFalse() + assertThat(banAvdEmulatorCheck()).isFalse() } fun `test that genymotion emulator check passes if it passes but device is Xiaomi`() { - every { osCheck.bootloader() } returns OsCheckConstants.UNKNOWN - every { osCheck.manufacturer() } returns OsCheckConstants.XIAOMI - every { osCheck.board() } returns NOT_BANNED - every { osCheck.device() } returns NOT_BANNED + mockBuildField(OsCheckConstants.UNKNOWN, "BOOTLOADER", Build::class.java) + mockBuildField(NOT_BANNED, "MANUFACTURER", Build::class.java) + mockBuildField(NOT_BANNED, "BOARD", Build::class.java) + + mockBuildField(OsCheckConstants.XIAOMI, "MANUFACTURER", Build::class.java) - assertThat(banGenymotionEmulatorCheck(osCheck)).isFalse() + assertThat(banGenymotionEmulatorCheck()).isFalse() } fun `test that avd bluestacks check passes if it fails but device is Xiaomi`() { - every { osCheck.bootloader() } returns OsCheckConstants.UNKNOWN - every { osCheck.manufacturer() } returns OsCheckConstants.XIAOMI - assertThat(banBluestacksEmulatorCheck(osCheck)).isFalse() + mockBuildField(OsCheckConstants.UNKNOWN, "BOOTLOADER", Build::class.java) + + mockBuildField(OsCheckConstants.XIAOMI, "MANUFACTURER", Build::class.java) + assertThat(banBluestacksEmulatorCheck()).isFalse() } diff --git a/safeToRun/src/test/kotlin/com/safetorun/offdevice/AndroidSafeToRunOffDeviceTest.kt b/safeToRun/src/test/kotlin/com/safetorun/offdevice/AndroidSafeToRunOffDeviceTest.kt index 8f50cc14..1417f491 100644 --- a/safeToRun/src/test/kotlin/com/safetorun/offdevice/AndroidSafeToRunOffDeviceTest.kt +++ b/safeToRun/src/test/kotlin/com/safetorun/offdevice/AndroidSafeToRunOffDeviceTest.kt @@ -15,6 +15,7 @@ import com.safetorun.models.builders.DeviceInformationDtoBuilder import com.safetorun.models.builders.deviceInformationBuilder import com.safetorun.models.models.DataWrappedLogResponse import com.safetorun.models.models.DeviceSignatureDto +import com.safetorun.setupMocks import io.mockk.every import io.mockk.mockk import io.mockk.mockkStatic @@ -51,7 +52,7 @@ internal class AndroidSafeToRunOffDeviceTest : TestCase() { } returns OffDeviceResultBuilder { it } -// context.setupMocks() + context.setupMocks() every { context.packageManager } returns mockk(relaxed = true).apply { every { getPackageInfo( diff --git a/safeToRun/src/test/kotlin/com/safetorun/utils/osConfigurationChecks.kt b/safeToRun/src/test/kotlin/com/safetorun/utils/osConfigurationChecks.kt new file mode 100644 index 00000000..2c3c81e9 --- /dev/null +++ b/safeToRun/src/test/kotlin/com/safetorun/utils/osConfigurationChecks.kt @@ -0,0 +1,14 @@ +package com.safetorun.utils + +import java.lang.reflect.Field +import java.lang.reflect.Modifier + +internal fun mockBuildField(v: T, fieldName: String, clazz: Class<*>) { + val sdkIntField = clazz.getField(fieldName) + sdkIntField.isAccessible = true + Field::class.java.getDeclaredField("modifiers").also { + it.isAccessible = true + it.set(sdkIntField, sdkIntField.modifiers and Modifier.FINAL.inv()) + } + sdkIntField.set(null, v) +} diff --git a/safeToRunInputValidation/build.gradle b/safeToRunInputValidation/build.gradle index a9d936a2..d9a4771d 100644 --- a/safeToRunInputValidation/build.gradle +++ b/safeToRunInputValidation/build.gradle @@ -3,6 +3,7 @@ plugins { id "com.diffplug.spotless" id "io.gitlab.arturbosch.detekt" id 'org.jetbrains.kotlinx.kover' + } apply plugin: 'com.android.library' @@ -19,7 +20,6 @@ ext { PUBLISH_ARTIFACT_ID = 'inputverification' } - apply from: "${rootProject.projectDir}/scripts/publish-module.gradle" android { @@ -33,19 +33,13 @@ android { consumerProguardFiles "consumer-rules.pro" } - testOptions { - unitTests { - includeAndroidResources = true - } - } - compileOptions { - sourceCompatibility JavaVersion.VERSION_11 - targetCompatibility JavaVersion.VERSION_11 + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { - jvmTarget = JavaVersion.VERSION_11.toString() + jvmTarget = JavaVersion.VERSION_1_8.toString() } buildTypes { @@ -60,5 +54,4 @@ android { dependencies { implementation project(':safeToRunInternal') implementation libs.kotlinx.serialization.json - testImplementation libs.robolectric } diff --git a/safeToRunInternal/build.gradle b/safeToRunInternal/build.gradle index 2a607267..f6dde510 100644 --- a/safeToRunInternal/build.gradle +++ b/safeToRunInternal/build.gradle @@ -8,14 +8,6 @@ plugins { id 'org.jetbrains.kotlinx.kover' } -sourceCompatibility = JavaVersion.VERSION_11.toString() -targetCompatibility = JavaVersion.VERSION_11.toString() - -tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { - kotlinOptions { - jvmTarget = "11" - } -} ext { PUBLISH_GROUP_ID = 'com.safetorun' diff --git a/safetorunpinscreen/build.gradle b/safetorunpinscreen/build.gradle index 79942381..d9c404f2 100644 --- a/safetorunpinscreen/build.gradle +++ b/safetorunpinscreen/build.gradle @@ -3,7 +3,7 @@ plugins { id "com.diffplug.spotless" id "io.gitlab.arturbosch.detekt" id 'org.jetbrains.kotlinx.kover' -} + } apply plugin: 'com.android.library' apply plugin: 'kotlin-android' @@ -33,19 +33,13 @@ android { consumerProguardFiles "consumer-rules.pro" } - testOptions { - unitTests { - includeAndroidResources = true - } - } - compileOptions { - sourceCompatibility JavaVersion.VERSION_11 - targetCompatibility JavaVersion.VERSION_11 + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { - jvmTarget = JavaVersion.VERSION_11.toString() + jvmTarget = JavaVersion.VERSION_1_8.toString() } buildTypes { @@ -58,7 +52,7 @@ android { } dependencies { - implementation libs.androidx.security.crypto + implementation "androidx.security:security-crypto:1.0.0" } diff --git a/scripts/dependencies/test_dependencies.gradle b/scripts/dependencies/test_dependencies.gradle index e94b2657..de93a8dd 100644 --- a/scripts/dependencies/test_dependencies.gradle +++ b/scripts/dependencies/test_dependencies.gradle @@ -7,4 +7,5 @@ dependencies { testImplementation "com.google.truth:truth:$googleTruth" testImplementation "io.mockk:mockk:$mockkVersion" testImplementation "org.junit.jupiter:junit-jupiter:$jupiterVersion" + testImplementation "org.robolectric:robolectric:$robolectricVersion" }