From fb8a5731127ed5f9366eadda203ff2509ad983bd Mon Sep 17 00:00:00 2001 From: Daniel Llewellyn Date: Sat, 23 Sep 2023 16:24:02 +0100 Subject: [PATCH] Various fixes to tests --- .../features/oscheck/OSConfigurationChecks.kt | 1 + .../features/oscheck/emulator/emulators.kt | 2 +- .../plus/AndroidSafeToRunOffDevice.kt | 6 +++++- .../safetorun/plus/queries/InstallOrigin.kt | 5 +++-- .../plus/queries/OSInformationQueryAndroid.kt | 1 + .../plus/AndroidSafeToRunOffDeviceTest.kt | 7 +++++++ .../com/safetorun/plus/SharedInstallOrigin.kt | 19 +++++++------------ .../builders/AndroidInstalledOriginTest.kt | 14 ++++++++------ .../AndroidInstalledPackagesQueryTest.kt | 6 ++++-- 9 files changed, 37 insertions(+), 24 deletions(-) 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..0c816fc7 100644 --- a/safeToRun/src/main/java/com/safetorun/features/oscheck/OSConfigurationChecks.kt +++ b/safeToRun/src/main/java/com/safetorun/features/oscheck/OSConfigurationChecks.kt @@ -1,6 +1,7 @@ package com.safetorun.features.oscheck import android.os.Build +import com.safetorun.plus.queries.osInformationQuery /** * Configure a min os version to fail if we fall below that version 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..13b03ce7 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 @@ -3,7 +3,7 @@ package com.safetorun.features.oscheck.emulator import com.safetorun.features.oscheck.OSInformationQuery import com.safetorun.features.oscheck.OsCheckConstants import com.safetorun.features.oscheck.OsCheckConstants.XIAOMI -import com.safetorun.features.oscheck.osInformationQuery +import com.safetorun.plus.queries.osInformationQuery /** * Return true if we're running on an emulator diff --git a/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/AndroidSafeToRunOffDevice.kt b/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/AndroidSafeToRunOffDevice.kt index cfebc7fa..33a0e2e0 100644 --- a/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/AndroidSafeToRunOffDevice.kt +++ b/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/AndroidSafeToRunOffDevice.kt @@ -2,6 +2,7 @@ package com.safetorun.plus import android.content.Context import com.safetorun.features.installorigin.InstallOriginQuery +import com.safetorun.features.oscheck.OSInformationQuery import com.safetorun.logger.models.BlacklistedApps import com.safetorun.logger.models.DeviceInformation import com.safetorun.logger.models.DeviceSignature @@ -23,6 +24,7 @@ import com.safetorun.plus.offdevice.builders.RootCheckOffDeviceBuilder import com.safetorun.plus.queries.OSInformationQueryAndroid import com.safetorun.plus.queries.getInstaller import com.safetorun.plus.queries.listInstalledPackages +import com.safetorun.plus.queries.osInformationQuery import com.safetorun.plus.repository.AndroidDeviceIdRepository import java.util.concurrent.Executors @@ -91,6 +93,7 @@ fun safeToRunLogger( installedPackagesQuery: (() -> List)? = null, rootCheck: (() -> Boolean)? = null, url: String = "https://api.safetorun.com", + osInformationQuery: OSInformationQuery = osInformationQuery() ): SafeToRunLogger { if (safeToRunOffDeviceLazy.containsKey(apiKey)) { requireNotNull(safeToRunOffDeviceLazy[apiKey]) @@ -154,10 +157,11 @@ internal fun offDeviceResultBuilder( getInstaller: InstallOriginQuery, rootCheck: (() -> Boolean)? = null, installedPackagesQuery: (() -> List)? = null, + osInformationQuery : OSInformationQuery = osInformationQuery() ): OffDeviceResultBuilder = CompositeBuilder( mutableListOf( - OSCheckOffDeviceBuilder(OSInformationQueryAndroid()), + OSCheckOffDeviceBuilder(osInformationQuery), InstallOriginOffDeviceBuilder(getInstaller), ).apply { if (rootCheck != null) { diff --git a/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/queries/InstallOrigin.kt b/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/queries/InstallOrigin.kt index e72aa77c..4077a06c 100644 --- a/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/queries/InstallOrigin.kt +++ b/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/queries/InstallOrigin.kt @@ -3,13 +3,14 @@ package com.safetorun.plus.queries import android.annotation.SuppressLint import android.content.Context import android.os.Build +import com.safetorun.features.oscheck.OSInformationQuery /** * Get the installer that installed this application */ @SuppressLint("NewApi") -fun Context.getInstaller(): String { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { +fun Context.getInstaller(osInformationQuery: OSInformationQuery = osInformationQuery()): String { + return if (osInformationQuery.osVersion() >= Build.VERSION_CODES.R) { packageManager.getInstallSourceInfo(packageName).installingPackageName } else { packageManager.getInstallerPackageName(packageName) diff --git a/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/queries/OSInformationQueryAndroid.kt b/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/queries/OSInformationQueryAndroid.kt index 216793b9..9d37a154 100644 --- a/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/queries/OSInformationQueryAndroid.kt +++ b/safeToRunPlus/src/androidMain/kotlin/com/safetorun/plus/queries/OSInformationQueryAndroid.kt @@ -1,6 +1,7 @@ package com.safetorun.plus.queries import android.os.Build +import com.safetorun.features.oscheck.OSInformationQuery /** * OSInformationQueryAndroid is a concrete implementation of the OSInformationQuery interface. diff --git a/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/AndroidSafeToRunOffDeviceTest.kt b/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/AndroidSafeToRunOffDeviceTest.kt index 233c26d7..e2be85b7 100644 --- a/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/AndroidSafeToRunOffDeviceTest.kt +++ b/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/AndroidSafeToRunOffDeviceTest.kt @@ -4,6 +4,7 @@ import com.safetorun.plus.models.DataWrappedLogResponse import android.content.Context import android.content.pm.PackageManager import com.google.common.truth.Truth.assertThat +import com.safetorun.features.oscheck.OSInformationQuery import com.safetorun.logger.models.BlacklistedApps import com.safetorun.logger.models.DeviceInformation import com.safetorun.logger.models.DeviceSignature @@ -42,6 +43,8 @@ internal class AndroidSafeToRunOffDeviceTest : TestCase() { deviceId ) + private val osInformationQuery = mockk() + private val port = Random.nextInt(9000, 9999) private val mockServer: ClientAndServer by lazy { ClientAndServer.startClientAndServer(port) } private val url: String = "http://localhost:$port" @@ -53,10 +56,14 @@ internal class AndroidSafeToRunOffDeviceTest : TestCase() { getInstaller = { INSTALLER_PACKAGE }, rootCheck = null, installedPackagesQuery = { emptyList() }, + osInformationQuery = osInformationQuery ) } returns OffDeviceResultBuilder { it } + + every { osInformationQuery.manufacturer() } returns "MANUFACTURER" + every { context.packageManager } returns mockk(relaxed = true).apply { every { getPackageInfo( diff --git a/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/SharedInstallOrigin.kt b/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/SharedInstallOrigin.kt index f4a2bb28..8ba47eca 100644 --- a/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/SharedInstallOrigin.kt +++ b/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/SharedInstallOrigin.kt @@ -1,6 +1,8 @@ package com.safetorun.plus import android.os.Build +import com.safetorun.features.oscheck.OSInformationQuery +import io.mockk.every internal object SharedInstallOrigin { @@ -9,18 +11,11 @@ internal object SharedInstallOrigin { } -internal fun setOlderAndroidVersion() { - mockBuildField( - Build.VERSION_CODES.LOLLIPOP, - "SDK_INT", - Build.VERSION::class.java - ) +internal fun setOlderAndroidVersion(osInformation : OSInformationQuery) { + every { osInformation.osVersion() } returns Build.VERSION_CODES.LOLLIPOP } -internal fun setTiramusu() { - mockBuildField( - Build.VERSION_CODES.TIRAMISU, - "SDK_INT", - Build.VERSION::class.java - ) + +internal fun setTiramusu(osInformation : OSInformationQuery) { + every { osInformation.osVersion() } returns Build.VERSION_CODES.TIRAMISU } diff --git a/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/builders/AndroidInstalledOriginTest.kt b/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/builders/AndroidInstalledOriginTest.kt index 839db9a7..b508e492 100644 --- a/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/builders/AndroidInstalledOriginTest.kt +++ b/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/builders/AndroidInstalledOriginTest.kt @@ -6,6 +6,7 @@ import android.content.pm.PackageManager import android.content.pm.PackageManager.PackageInfoFlags import android.os.Build import com.google.common.truth.Truth.assertThat +import com.safetorun.features.oscheck.OSInformationQuery import com.safetorun.plus.SharedInstallOrigin.INSTALLER_PACKAGE import com.safetorun.plus.mockBuildField import com.safetorun.plus.queries.getInstaller @@ -20,6 +21,7 @@ internal class AndroidInstalledOriginTest : TestCase() { private val context by lazy { mockk() } private val pm by lazy { mockk() } + private val osInformation by lazy { mockk() } @Suppress("DEPRECATION") // Need to do it for older devices override fun setUp() { @@ -45,10 +47,10 @@ internal class AndroidInstalledOriginTest : TestCase() { fun `test that android install origin returns based on the context when null`() { // Given every { context.packageName } returns fakeName - setTiramusu() + setTiramusu(osInformation) // When - val result = context.getInstaller() + val result = context.getInstaller(osInformation) // Then assertThat(result).isEqualTo("Not found") @@ -56,10 +58,10 @@ internal class AndroidInstalledOriginTest : TestCase() { fun `test that android install origin returns based on the context pre TIRAMUSU`() { // Given - setOlderAndroidVersion() + setOlderAndroidVersion(osInformation) // When - val result = context.getInstaller() + val result = context.getInstaller(osInformation) // Then assertThat(result).isEqualTo(INSTALLER_PACKAGE) @@ -68,10 +70,10 @@ internal class AndroidInstalledOriginTest : TestCase() { fun `test that android install origin returns based on the context`() { // Given - setTiramusu() + setTiramusu(osInformation) // When - val result = context.getInstaller() + val result = context.getInstaller(osInformation) // Then assertThat(result).isEqualTo(INSTALLER_PACKAGE) diff --git a/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/builders/AndroidInstalledPackagesQueryTest.kt b/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/builders/AndroidInstalledPackagesQueryTest.kt index c0bda69f..6583758d 100644 --- a/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/builders/AndroidInstalledPackagesQueryTest.kt +++ b/safeToRunPlus/src/androidTest/kotlin/com/safetorun/plus/builders/AndroidInstalledPackagesQueryTest.kt @@ -6,6 +6,7 @@ import android.content.pm.PackageManager import android.content.pm.PackageManager.PackageInfoFlags import android.os.Build import com.google.common.truth.Truth.assertThat +import com.safetorun.features.oscheck.OSInformationQuery import com.safetorun.plus.mockBuildField import com.safetorun.plus.queries.listInstalledPackages import com.safetorun.plus.setOlderAndroidVersion @@ -19,6 +20,7 @@ internal class AndroidInstalledPackagesQueryTest : TestCase() { private val context by lazy { mockk() } private val pm by lazy { mockk() } + private val osInformation by lazy { mockk() } @Suppress("DEPRECATION") // Need to do it for older devices override fun setUp() { @@ -35,7 +37,7 @@ internal class AndroidInstalledPackagesQueryTest : TestCase() { fun `test that android installed packages rturns based on the context pre TIRAMUSU`() { // Given - setOlderAndroidVersion() + setOlderAndroidVersion(osInformation) // When val result = context.listInstalledPackages() @@ -46,7 +48,7 @@ internal class AndroidInstalledPackagesQueryTest : TestCase() { fun `test that android installed packages returns based on the context`() { // Given - setTiramusu() + setTiramusu(osInformation) // When val result = context.listInstalledPackages()