Skip to content

Commit

Permalink
Various fixes to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dllewellyn committed Sep 23, 2023
1 parent b0b5abb commit fb8a573
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -91,6 +93,7 @@ fun safeToRunLogger(
installedPackagesQuery: (() -> List<String>)? = null,
rootCheck: (() -> Boolean)? = null,
url: String = "https://api.safetorun.com",
osInformationQuery: OSInformationQuery = osInformationQuery()
): SafeToRunLogger {
if (safeToRunOffDeviceLazy.containsKey(apiKey)) {
requireNotNull(safeToRunOffDeviceLazy[apiKey])
Expand Down Expand Up @@ -154,10 +157,11 @@ internal fun offDeviceResultBuilder(
getInstaller: InstallOriginQuery,
rootCheck: (() -> Boolean)? = null,
installedPackagesQuery: (() -> List<String>)? = null,
osInformationQuery : OSInformationQuery = osInformationQuery()
): OffDeviceResultBuilder =
CompositeBuilder(
mutableListOf(
OSCheckOffDeviceBuilder(OSInformationQueryAndroid()),
OSCheckOffDeviceBuilder(osInformationQuery),
InstallOriginOffDeviceBuilder(getInstaller),
).apply {
if (rootCheck != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,6 +43,8 @@ internal class AndroidSafeToRunOffDeviceTest : TestCase() {
deviceId
)

private val osInformationQuery = mockk<OSInformationQuery>()

private val port = Random.nextInt(9000, 9999)
private val mockServer: ClientAndServer by lazy { ClientAndServer.startClientAndServer(port) }
private val url: String = "http://localhost:$port"
Expand All @@ -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<PackageManager>(relaxed = true).apply {
every {
getPackageInfo(
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +21,7 @@ internal class AndroidInstalledOriginTest : TestCase() {

private val context by lazy { mockk<Context>() }
private val pm by lazy { mockk<PackageManager>() }
private val osInformation by lazy { mockk<OSInformationQuery>() }

@Suppress("DEPRECATION") // Need to do it for older devices
override fun setUp() {
Expand All @@ -45,21 +47,21 @@ 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")
}

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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +20,7 @@ internal class AndroidInstalledPackagesQueryTest : TestCase() {

private val context by lazy { mockk<Context>() }
private val pm by lazy { mockk<PackageManager>() }
private val osInformation by lazy { mockk<OSInformationQuery>() }

@Suppress("DEPRECATION") // Need to do it for older devices
override fun setUp() {
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit fb8a573

Please sign in to comment.