mirror of
https://github.com/f-droid/fdroidclient.git
synced 2026-06-17 12:19:52 -04:00
Auto-format all files with ktfmt
This commit is contained in:
@@ -1,31 +1,31 @@
|
||||
package org.fdroid.database
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import kotlin.reflect.full.primaryConstructor
|
||||
import kotlin.test.assertNotNull
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
internal class PrimaryConstructorTest {
|
||||
|
||||
private val classes = listOf(
|
||||
AntiFeature::class,
|
||||
Category::class,
|
||||
ReleaseChannel::class,
|
||||
// recent minification removes the primary constructor of CoreRepository
|
||||
// so we need to ensure it is still there for our reflection diffing
|
||||
Class.forName("org.fdroid.database.CoreRepository").kotlin,
|
||||
private val classes =
|
||||
listOf(
|
||||
AntiFeature::class,
|
||||
Category::class,
|
||||
ReleaseChannel::class,
|
||||
// recent minification removes the primary constructor of CoreRepository
|
||||
// so we need to ensure it is still there for our reflection diffing
|
||||
Class.forName("org.fdroid.database.CoreRepository").kotlin,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun testPrimaryConstructor() {
|
||||
classes.forEach {
|
||||
assertNotNull(
|
||||
actual = it.primaryConstructor,
|
||||
message = "${it.simpleName} has no primary constructor",
|
||||
)
|
||||
}
|
||||
@Test
|
||||
fun testPrimaryConstructor() {
|
||||
classes.forEach {
|
||||
assertNotNull(
|
||||
actual = it.primaryConstructor,
|
||||
message = "${it.simpleName} has no primary constructor",
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,101 +3,101 @@ package org.fdroid.download
|
||||
import android.content.Context
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import org.fdroid.settings.SettingsManager
|
||||
import org.junit.runner.RunWith
|
||||
import java.net.InetAddress
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
import org.fdroid.settings.SettingsManager
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class DnsCacheTest {
|
||||
|
||||
private val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
private val settings = SettingsManager(context)
|
||||
private val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
private val settings = SettingsManager(context)
|
||||
|
||||
private val url1 = "locaihost"
|
||||
private val url2 = "fdroid.org"
|
||||
private val url3 = "fdroid.net"
|
||||
private val url1 = "locaihost"
|
||||
private val url2 = "fdroid.org"
|
||||
private val url3 = "fdroid.net"
|
||||
|
||||
private val ip1 = InetAddress.getByName("127.0.0.1")
|
||||
private val ip2 = InetAddress.getByName("127.0.0.2")
|
||||
private val ip3 = InetAddress.getByName("127.0.0.3")
|
||||
private val ip1 = InetAddress.getByName("127.0.0.1")
|
||||
private val ip2 = InetAddress.getByName("127.0.0.2")
|
||||
private val ip3 = InetAddress.getByName("127.0.0.3")
|
||||
|
||||
private val list1 = listOf(ip1, ip2, ip3)
|
||||
private val list2 = listOf(ip2)
|
||||
private val list3 = listOf(ip3)
|
||||
private val list1 = listOf(ip1, ip2, ip3)
|
||||
private val list2 = listOf(ip2)
|
||||
private val list3 = listOf(ip3)
|
||||
|
||||
@Test
|
||||
fun basicCacheTest() {
|
||||
// test setup
|
||||
settings.useDnsCache = true
|
||||
val testObject = DnsCache(settings)
|
||||
@Test
|
||||
fun basicCacheTest() {
|
||||
// test setup
|
||||
settings.useDnsCache = true
|
||||
val testObject = DnsCache(settings)
|
||||
|
||||
// populate cache
|
||||
testObject.insert(url1, list1)
|
||||
testObject.insert(url2, list2)
|
||||
testObject.insert(url3, list3)
|
||||
// populate cache
|
||||
testObject.insert(url1, list1)
|
||||
testObject.insert(url2, list2)
|
||||
testObject.insert(url3, list3)
|
||||
|
||||
// check for cached lookup results
|
||||
val testList1 = testObject.lookup(url1)
|
||||
assertNotNull(testList1)
|
||||
assertEquals(3, testList1.size.toLong())
|
||||
assertEquals(ip1.hostAddress, testList1[0].hostAddress)
|
||||
assertEquals(ip2.hostAddress, testList1[1].hostAddress)
|
||||
assertEquals(ip3.hostAddress, testList1[2].hostAddress)
|
||||
// check for cached lookup results
|
||||
val testList1 = testObject.lookup(url1)
|
||||
assertNotNull(testList1)
|
||||
assertEquals(3, testList1.size.toLong())
|
||||
assertEquals(ip1.hostAddress, testList1[0].hostAddress)
|
||||
assertEquals(ip2.hostAddress, testList1[1].hostAddress)
|
||||
assertEquals(ip3.hostAddress, testList1[2].hostAddress)
|
||||
|
||||
// toggle preference (false)
|
||||
settings.useDnsCache = false
|
||||
// toggle preference (false)
|
||||
settings.useDnsCache = false
|
||||
|
||||
// attempt non-cached lookup
|
||||
val testList2 = testObject.lookup(url1)
|
||||
assertNull(testList2)
|
||||
// attempt non-cached lookup
|
||||
val testList2 = testObject.lookup(url1)
|
||||
assertNull(testList2)
|
||||
|
||||
// toggle preference (true)
|
||||
settings.useDnsCache = true
|
||||
// toggle preference (true)
|
||||
settings.useDnsCache = true
|
||||
|
||||
// confirm lookup results remain in cache
|
||||
val testList3 = testObject.lookup(url2)
|
||||
assertNotNull(testList3)
|
||||
assertEquals(1, testList3.size.toLong())
|
||||
assertEquals(ip2.hostAddress, testList3[0].hostAddress)
|
||||
// confirm lookup results remain in cache
|
||||
val testList3 = testObject.lookup(url2)
|
||||
assertNotNull(testList3)
|
||||
assertEquals(1, testList3.size.toLong())
|
||||
assertEquals(ip2.hostAddress, testList3[0].hostAddress)
|
||||
|
||||
// test removal
|
||||
testObject.remove(url2)
|
||||
// test removal
|
||||
testObject.remove(url2)
|
||||
|
||||
// confirm result was removed from cache
|
||||
val testList4 = testObject.lookup(url2)
|
||||
assertNull(testList4)
|
||||
}
|
||||
// confirm result was removed from cache
|
||||
val testList4 = testObject.lookup(url2)
|
||||
assertNull(testList4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun dnsRetryTest() {
|
||||
// test setup
|
||||
settings.useDnsCache = true
|
||||
val testCache = DnsCache(settings)
|
||||
val testObject = DnsWithCache(settings, testCache)
|
||||
@Test
|
||||
fun dnsRetryTest() {
|
||||
// test setup
|
||||
settings.useDnsCache = true
|
||||
val testCache = DnsCache(settings)
|
||||
val testObject = DnsWithCache(settings, testCache)
|
||||
|
||||
// insert dummy value into cache
|
||||
testCache.insert(url2, list2)
|
||||
// insert dummy value into cache
|
||||
testCache.insert(url2, list2)
|
||||
|
||||
// check initial status
|
||||
val testList1 = testObject.lookup(url2)
|
||||
assertEquals(1, testList1.size.toLong())
|
||||
assertEquals(ip2.hostAddress, testList1[0].hostAddress)
|
||||
// check initial status
|
||||
val testList1 = testObject.lookup(url2)
|
||||
assertEquals(1, testList1.size.toLong())
|
||||
assertEquals(ip2.hostAddress, testList1[0].hostAddress)
|
||||
|
||||
// mismatch with dummy value should require retry and clear cache
|
||||
val testFlag = testObject.shouldRetryRequest(url2)
|
||||
assertTrue(testFlag)
|
||||
val testList2 = testCache.lookup(url2)
|
||||
assertNull(testList2)
|
||||
// mismatch with dummy value should require retry and clear cache
|
||||
val testFlag = testObject.shouldRetryRequest(url2)
|
||||
assertTrue(testFlag)
|
||||
val testList2 = testCache.lookup(url2)
|
||||
assertNull(testList2)
|
||||
|
||||
// subsequent lookup should cache actual dns result (not testing actual values)
|
||||
val testList3 = testObject.lookup(url2)
|
||||
assertNotNull(testList3)
|
||||
val testList4 = testCache.lookup(url2)
|
||||
assertNotNull(testList4)
|
||||
}
|
||||
// subsequent lookup should cache actual dns result (not testing actual values)
|
||||
val testList3 = testObject.lookup(url2)
|
||||
assertNotNull(testList3)
|
||||
val testList4 = testCache.lookup(url2)
|
||||
assertNotNull(testList4)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,68 +4,72 @@ import android.content.pm.ApplicationInfo.FLAG_SYSTEM
|
||||
import android.provider.MediaStore.MediaColumns.DISPLAY_NAME
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import mu.KotlinLogging
|
||||
import org.fdroid.install.ApkFileProvider.Companion.getIntent
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.fail
|
||||
import mu.KotlinLogging
|
||||
import org.fdroid.install.ApkFileProvider.Companion.getIntent
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ApkFileProviderTest {
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
private val context = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
private val pm = context.packageManager
|
||||
private val log = KotlinLogging.logger {}
|
||||
private val context = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
private val pm = context.packageManager
|
||||
|
||||
private val packageInfoList
|
||||
get() = pm.getInstalledPackages(0).filter {
|
||||
val info = it.applicationInfo ?: return@filter false
|
||||
(info.flags and FLAG_SYSTEM) == 0
|
||||
}.sortedBy {
|
||||
val path = it.applicationInfo?.publicSourceDir ?: return@sortedBy Long.MAX_VALUE
|
||||
File(path).length()
|
||||
}.subList(0, 3) // just test with the three smallest apps
|
||||
|
||||
/**
|
||||
* Test whether reading installed APKs via our custom [android.content.ContentProvider] works.
|
||||
* It also only copies max 3 apps so it doesn't take a long time to run.
|
||||
*/
|
||||
@Test
|
||||
fun testCopyFromGetUri() {
|
||||
for (packageInfo in packageInfoList) {
|
||||
val applicationInfo = packageInfo.applicationInfo ?: fail()
|
||||
val apk = File(applicationInfo.publicSourceDir)
|
||||
val uri = getIntent(packageInfo.packageName).data ?: fail()
|
||||
val test = FileOutputStream("/dev/null")
|
||||
val numBytesCopied = context.contentResolver.openInputStream(uri)?.use { inputStream ->
|
||||
inputStream.copyTo(test)
|
||||
} ?: fail()
|
||||
assertEquals(apk.length(), numBytesCopied)
|
||||
log.info {
|
||||
"${packageInfo.packageName} read $numBytesCopied bytes from ${apk.absolutePath}"
|
||||
}
|
||||
private val packageInfoList
|
||||
get() =
|
||||
pm
|
||||
.getInstalledPackages(0)
|
||||
.filter {
|
||||
val info = it.applicationInfo ?: return@filter false
|
||||
(info.flags and FLAG_SYSTEM) == 0
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether querying the custom [android.content.ContentProvider] for installed APKs
|
||||
* returns the right kind of data.
|
||||
*/
|
||||
@Test
|
||||
@Throws(IOException::class)
|
||||
fun testQuery() {
|
||||
for (packageInfo in packageInfoList) {
|
||||
val uri = getIntent(packageInfo.packageName).data ?: fail()
|
||||
context.contentResolver.query(uri, null, null, null, null)?.use { cursor ->
|
||||
assertEquals(1, cursor.count)
|
||||
cursor.moveToFirst()
|
||||
val name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME))
|
||||
assertEquals("${packageInfo.packageName}.apk", name)
|
||||
} ?: fail()
|
||||
.sortedBy {
|
||||
val path = it.applicationInfo?.publicSourceDir ?: return@sortedBy Long.MAX_VALUE
|
||||
File(path).length()
|
||||
}
|
||||
.subList(0, 3) // just test with the three smallest apps
|
||||
|
||||
/**
|
||||
* Test whether reading installed APKs via our custom [android.content.ContentProvider] works. It
|
||||
* also only copies max 3 apps so it doesn't take a long time to run.
|
||||
*/
|
||||
@Test
|
||||
fun testCopyFromGetUri() {
|
||||
for (packageInfo in packageInfoList) {
|
||||
val applicationInfo = packageInfo.applicationInfo ?: fail()
|
||||
val apk = File(applicationInfo.publicSourceDir)
|
||||
val uri = getIntent(packageInfo.packageName).data ?: fail()
|
||||
val test = FileOutputStream("/dev/null")
|
||||
val numBytesCopied =
|
||||
context.contentResolver.openInputStream(uri)?.use { inputStream ->
|
||||
inputStream.copyTo(test)
|
||||
} ?: fail()
|
||||
assertEquals(apk.length(), numBytesCopied)
|
||||
log.info { "${packageInfo.packageName} read $numBytesCopied bytes from ${apk.absolutePath}" }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether querying the custom [android.content.ContentProvider] for installed APKs returns
|
||||
* the right kind of data.
|
||||
*/
|
||||
@Test
|
||||
@Throws(IOException::class)
|
||||
fun testQuery() {
|
||||
for (packageInfo in packageInfoList) {
|
||||
val uri = getIntent(packageInfo.packageName).data ?: fail()
|
||||
context.contentResolver.query(uri, null, null, null, null)?.use { cursor ->
|
||||
assertEquals(1, cursor.count)
|
||||
cursor.moveToFirst()
|
||||
val name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME))
|
||||
assertEquals("${packageInfo.packageName}.apk", name)
|
||||
} ?: fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user