Merge pull request #109 from grote/saf-optimized
Improve likelyhood that a backup with Nextcloud succeeds
This commit is contained in:
commit
93a7ec8143
52 changed files with 1578 additions and 842 deletions
.gitignore
.idea
app
build.gradle
src
androidTest/java/com/stevesoltys/seedvault
main
java/com/stevesoltys/seedvault
NotificationBackupObserver.kt
metadata
plugins/saf
DocumentsProviderBackupPlugin.ktDocumentsProviderFullBackup.ktDocumentsProviderFullRestorePlugin.ktDocumentsProviderKVBackup.ktDocumentsProviderKVRestorePlugin.ktDocumentsProviderModule.ktDocumentsProviderRestorePlugin.ktDocumentsStorage.kt
restore
settings
transport
ConfigurableBackupTransport.kt
backup
ApkBackup.ktBackupCoordinator.ktBackupPlugin.ktFullBackup.ktFullBackupPlugin.ktKVBackup.ktKVBackupPlugin.ktPackageService.kt
restore
ui/storage
res
sharedTest/java/com/stevesoltys/seedvault
test/java/com/stevesoltys/seedvault/transport
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -7,7 +7,8 @@ hs_err_pid*
|
|||
## Intellij
|
||||
out/
|
||||
lib/
|
||||
.idea/
|
||||
.idea/*
|
||||
!.idea/runConfigurations*
|
||||
*.ipr
|
||||
*.iws
|
||||
*.iml
|
||||
|
|
12
.idea/runConfigurations.xml
generated
Normal file
12
.idea/runConfigurations.xml
generated
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RunConfigurationProducerService">
|
||||
<option name="ignoredProducers">
|
||||
<set>
|
||||
<option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" />
|
||||
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" />
|
||||
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" />
|
||||
</set>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
17
.idea/runConfigurations/Unit_Tests.xml
generated
Normal file
17
.idea/runConfigurations/Unit_Tests.xml
generated
Normal file
|
@ -0,0 +1,17 @@
|
|||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Unit Tests" type="AndroidJUnit" factoryName="Android JUnit">
|
||||
<module name="app" />
|
||||
<useClassPathOnly />
|
||||
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
|
||||
<option name="ALTERNATIVE_JRE_PATH" value="/usr/lib/jvm/java-11" />
|
||||
<option name="MAIN_CLASS_NAME" value="" />
|
||||
<option name="METHOD_NAME" value="" />
|
||||
<option name="TEST_OBJECT" value="directory" />
|
||||
<option name="PARAMETERS" value="" />
|
||||
<option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
|
||||
<dir value="$PROJECT_DIR$/app/src/test/java/com/stevesoltys/seedvault" />
|
||||
<method v="2">
|
||||
<option name="Android.Gradle.BeforeRunTask" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
|
@ -7,7 +7,7 @@ apply plugin: 'kotlin-android-extensions'
|
|||
android {
|
||||
|
||||
compileSdkVersion 29
|
||||
buildToolsVersion '29.0.2'
|
||||
buildToolsVersion '29.0.2' // adapt in .travis.yaml if changed here
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 29
|
||||
|
@ -128,9 +128,9 @@ dependencies {
|
|||
|
||||
lintChecks 'com.github.thirdegg:lint-rules:0.0.4-alpha'
|
||||
|
||||
def junit_version = "5.5.2"
|
||||
def junit_version = "5.5.2" // careful, upgrading this can change a Cipher's IV size in tests!?
|
||||
def mockk_version = "1.10.0"
|
||||
testImplementation aospDeps
|
||||
testImplementation aospDeps // anything less fails tests run with gradlew
|
||||
testImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
testImplementation 'org.robolectric:robolectric:4.3.1'
|
||||
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit_version"
|
||||
|
|
|
@ -1,73 +0,0 @@
|
|||
package com.stevesoltys.seedvault
|
||||
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.stevesoltys.seedvault.metadata.MetadataManager
|
||||
import com.stevesoltys.seedvault.plugins.saf.DocumentsStorage
|
||||
import com.stevesoltys.seedvault.plugins.saf.createOrGetFile
|
||||
import com.stevesoltys.seedvault.settings.SettingsManager
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.koin.core.KoinComponent
|
||||
import org.koin.core.inject
|
||||
import kotlin.random.Random
|
||||
|
||||
private const val filename = "test-file"
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class DocumentsStorageTest : KoinComponent {
|
||||
|
||||
private val context = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
private val metadataManager by inject<MetadataManager>()
|
||||
private val settingsManager by inject<SettingsManager>()
|
||||
private val storage = DocumentsStorage(context, metadataManager, settingsManager)
|
||||
|
||||
private lateinit var file: DocumentFile
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
assertNotNull("Select a storage location in the app first!", storage.rootBackupDir)
|
||||
file = storage.rootBackupDir?.createOrGetFile(filename)
|
||||
?: throw RuntimeException("Could not create test file")
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
file.delete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWritingAndReadingFile() {
|
||||
// write to output stream
|
||||
val outputStream = storage.getOutputStream(file)
|
||||
val content = ByteArray(1337).apply { Random.nextBytes(this) }
|
||||
outputStream.write(content)
|
||||
outputStream.flush()
|
||||
outputStream.close()
|
||||
|
||||
// read written data from input stream
|
||||
val inputStream = storage.getInputStream(file)
|
||||
val readContent = inputStream.readBytes()
|
||||
inputStream.close()
|
||||
assertArrayEquals(content, readContent)
|
||||
|
||||
// write smaller content to same file
|
||||
val outputStream2 = storage.getOutputStream(file)
|
||||
val content2 = ByteArray(42).apply { Random.nextBytes(this) }
|
||||
outputStream2.write(content2)
|
||||
outputStream2.flush()
|
||||
outputStream2.close()
|
||||
|
||||
// read written data from input stream
|
||||
val inputStream2 = storage.getInputStream(file)
|
||||
val readContent2 = inputStream2.readBytes()
|
||||
inputStream2.close()
|
||||
assertArrayEquals(content2, readContent2)
|
||||
}
|
||||
|
||||
}
|
|
@ -7,17 +7,19 @@ import com.stevesoltys.seedvault.metadata.MetadataManager
|
|||
import com.stevesoltys.seedvault.plugins.saf.DocumentsProviderBackupPlugin
|
||||
import com.stevesoltys.seedvault.plugins.saf.DocumentsProviderRestorePlugin
|
||||
import com.stevesoltys.seedvault.plugins.saf.DocumentsStorage
|
||||
import com.stevesoltys.seedvault.plugins.saf.MAX_KEY_LENGTH
|
||||
import com.stevesoltys.seedvault.plugins.saf.MAX_KEY_LENGTH_NEXTCLOUD
|
||||
import com.stevesoltys.seedvault.plugins.saf.deleteContents
|
||||
import com.stevesoltys.seedvault.settings.SettingsManager
|
||||
import com.stevesoltys.seedvault.transport.backup.BackupPlugin
|
||||
import com.stevesoltys.seedvault.transport.restore.RestorePlugin
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
|
@ -25,12 +27,11 @@ import org.junit.Test
|
|||
import org.junit.runner.RunWith
|
||||
import org.koin.core.KoinComponent
|
||||
import org.koin.core.inject
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.io.IOException
|
||||
import kotlin.random.Random
|
||||
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
class PluginTest : KoinComponent {
|
||||
|
||||
private val context = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
|
@ -38,7 +39,7 @@ class PluginTest : KoinComponent {
|
|||
private val settingsManager: SettingsManager by inject()
|
||||
private val mockedSettingsManager: SettingsManager = mockk()
|
||||
private val storage = DocumentsStorage(context, metadataManager, mockedSettingsManager)
|
||||
private val backupPlugin: BackupPlugin = DocumentsProviderBackupPlugin(storage, context.packageManager)
|
||||
private val backupPlugin: BackupPlugin = DocumentsProviderBackupPlugin(context, storage)
|
||||
private val restorePlugin: RestorePlugin = DocumentsProviderRestorePlugin(context, storage)
|
||||
|
||||
private val token = Random.nextLong()
|
||||
|
@ -49,7 +50,7 @@ class PluginTest : KoinComponent {
|
|||
fun setup() {
|
||||
every { mockedSettingsManager.getStorage() } returns settingsManager.getStorage()
|
||||
storage.rootBackupDir?.deleteContents()
|
||||
?: error("Select a storage location in the app first!")
|
||||
?: error("Select a storage location in the app first!")
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -70,7 +71,7 @@ class PluginTest : KoinComponent {
|
|||
* that needs to get re-queried to get real results.
|
||||
*/
|
||||
@Test
|
||||
fun testInitializationAndRestoreSets() {
|
||||
fun testInitializationAndRestoreSets() = runBlocking(Dispatchers.IO) {
|
||||
// no backups available initially
|
||||
assertEquals(0, restorePlugin.getAvailableBackups()?.toList()?.size)
|
||||
val uri = settingsManager.getStorage()?.getDocumentFile(context)?.uri ?: error("no storage")
|
||||
|
@ -104,7 +105,7 @@ class PluginTest : KoinComponent {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun testMetadataWriteRead() {
|
||||
fun testMetadataWriteRead() = runBlocking(Dispatchers.IO) {
|
||||
every { mockedSettingsManager.getAndResetIsStorageChanging() } returns true andThen false
|
||||
assertTrue(backupPlugin.initializeDevice(newToken = token))
|
||||
|
||||
|
@ -120,7 +121,7 @@ class PluginTest : KoinComponent {
|
|||
assertFalse(availableBackups[0].error)
|
||||
|
||||
// read metadata matches what was written earlier
|
||||
assertEquals(metadata, availableBackups[0].inputStream)
|
||||
assertReadEquals(metadata, availableBackups[0].inputStream)
|
||||
|
||||
// initializing again (without changing storage) keeps restore set with same token
|
||||
assertFalse(backupPlugin.initializeDevice(newToken = token + 1))
|
||||
|
@ -131,11 +132,11 @@ class PluginTest : KoinComponent {
|
|||
assertFalse(availableBackups[0].error)
|
||||
|
||||
// metadata hasn't changed
|
||||
assertEquals(metadata, availableBackups[0].inputStream)
|
||||
assertReadEquals(metadata, availableBackups[0].inputStream)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testApkWriteRead() {
|
||||
fun testApkWriteRead() = runBlocking {
|
||||
// initialize storage with given token
|
||||
initStorage(token)
|
||||
|
||||
|
@ -144,11 +145,11 @@ class PluginTest : KoinComponent {
|
|||
backupPlugin.getApkOutputStream(packageInfo).writeAndClose(apk)
|
||||
|
||||
// assert that read APK bytes match what was written
|
||||
assertEquals(apk, restorePlugin.getApkInputStream(token, packageInfo.packageName))
|
||||
assertReadEquals(apk, restorePlugin.getApkInputStream(token, packageInfo.packageName))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testKvBackupRestore() {
|
||||
fun testKvBackupRestore() = runBlocking {
|
||||
// define shortcuts
|
||||
val kvBackup = backupPlugin.kvBackupPlugin
|
||||
val kvRestore = restorePlugin.kvRestorePlugin
|
||||
|
@ -163,7 +164,7 @@ class PluginTest : KoinComponent {
|
|||
// define key/value pair records
|
||||
val record1 = Pair(getRandomBase64(23), getRandomByteArray(1337))
|
||||
val record2 = Pair(getRandomBase64(42), getRandomByteArray(42 * 1024))
|
||||
val record3 = Pair(getRandomBase64(255), getRandomByteArray(5 * 1024 * 1024))
|
||||
val record3 = Pair(getRandomBase64(128), getRandomByteArray(5 * 1024 * 1024))
|
||||
|
||||
// write first record
|
||||
kvBackup.ensureRecordStorageForPackage(packageInfo)
|
||||
|
@ -178,7 +179,10 @@ class PluginTest : KoinComponent {
|
|||
var records = kvRestore.listRecords(token, packageInfo)
|
||||
assertEquals(1, records.size)
|
||||
assertEquals(record1.first, records[0])
|
||||
assertEquals(record1.second, kvRestore.getInputStreamForRecord(token, packageInfo, record1.first))
|
||||
assertReadEquals(
|
||||
record1.second,
|
||||
kvRestore.getInputStreamForRecord(token, packageInfo, record1.first)
|
||||
)
|
||||
|
||||
// write second and third record
|
||||
kvBackup.ensureRecordStorageForPackage(packageInfo)
|
||||
|
@ -188,9 +192,18 @@ class PluginTest : KoinComponent {
|
|||
// all records for package are found and returned properly
|
||||
records = kvRestore.listRecords(token, packageInfo)
|
||||
assertEquals(listOf(record1.first, record2.first, record3.first).sorted(), records.sorted())
|
||||
assertEquals(record1.second, kvRestore.getInputStreamForRecord(token, packageInfo, record1.first))
|
||||
assertEquals(record2.second, kvRestore.getInputStreamForRecord(token, packageInfo, record2.first))
|
||||
assertEquals(record3.second, kvRestore.getInputStreamForRecord(token, packageInfo, record3.first))
|
||||
assertReadEquals(
|
||||
record1.second,
|
||||
kvRestore.getInputStreamForRecord(token, packageInfo, record1.first)
|
||||
)
|
||||
assertReadEquals(
|
||||
record2.second,
|
||||
kvRestore.getInputStreamForRecord(token, packageInfo, record2.first)
|
||||
)
|
||||
assertReadEquals(
|
||||
record3.second,
|
||||
kvRestore.getInputStreamForRecord(token, packageInfo, record3.first)
|
||||
)
|
||||
|
||||
// delete record3 and ensure that the other two are still found
|
||||
kvBackup.deleteRecord(packageInfo, record3.first)
|
||||
|
@ -204,7 +217,7 @@ class PluginTest : KoinComponent {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun testMaxKvKeyLength() {
|
||||
fun testMaxKvKeyLength() = runBlocking {
|
||||
// define shortcuts
|
||||
val kvBackup = backupPlugin.kvBackupPlugin
|
||||
val kvRestore = restorePlugin.kvRestorePlugin
|
||||
|
@ -212,31 +225,41 @@ class PluginTest : KoinComponent {
|
|||
// initialize storage with given token
|
||||
initStorage(token)
|
||||
|
||||
// FIXME get Nextcloud to have the same limit
|
||||
val max = if (isNextcloud()) MAX_KEY_LENGTH_NEXTCLOUD else MAX_KEY_LENGTH
|
||||
|
||||
// define record with maximum key length and one above the maximum
|
||||
val recordMax = Pair(getRandomBase64(255), getRandomByteArray(1024))
|
||||
val recordOver = Pair(getRandomBase64(256), getRandomByteArray(1024))
|
||||
val recordMax = Pair(getRandomBase64(max), getRandomByteArray(1024))
|
||||
val recordOver = Pair(getRandomBase64(max + 1), getRandomByteArray(1024))
|
||||
|
||||
// write max record
|
||||
kvBackup.ensureRecordStorageForPackage(packageInfo)
|
||||
kvBackup.getOutputStreamForRecord(packageInfo, recordMax.first).writeAndClose(recordMax.second)
|
||||
kvBackup.getOutputStreamForRecord(packageInfo, recordMax.first)
|
||||
.writeAndClose(recordMax.second)
|
||||
|
||||
// max record is found correctly
|
||||
assertTrue(kvRestore.hasDataForPackage(token, packageInfo))
|
||||
var records = kvRestore.listRecords(token, packageInfo)
|
||||
val records = kvRestore.listRecords(token, packageInfo)
|
||||
assertEquals(listOf(recordMax.first), records)
|
||||
|
||||
// write exceeding key length record
|
||||
kvBackup.ensureRecordStorageForPackage(packageInfo)
|
||||
kvBackup.getOutputStreamForRecord(packageInfo, recordOver.first).writeAndClose(recordOver.second)
|
||||
|
||||
// exceeding record gets truncated
|
||||
assertTrue(kvRestore.hasDataForPackage(token, packageInfo))
|
||||
records = kvRestore.listRecords(token, packageInfo)
|
||||
assertNotEquals(listOf(recordMax.first, recordOver.first).sorted(), records.sorted())
|
||||
if (isNextcloud()) {
|
||||
// Nextcloud simply refuses to write long filenames
|
||||
coAssertThrows(IOException::class.java) {
|
||||
kvBackup.getOutputStreamForRecord(packageInfo, recordOver.first)
|
||||
.writeAndClose(recordOver.second)
|
||||
}
|
||||
} else {
|
||||
coAssertThrows(IllegalStateException::class.java) {
|
||||
kvBackup.getOutputStreamForRecord(packageInfo, recordOver.first)
|
||||
.writeAndClose(recordOver.second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFullBackupRestore() {
|
||||
fun testFullBackupRestore() = runBlocking {
|
||||
// define shortcuts
|
||||
val fullBackup = backupPlugin.fullBackupPlugin
|
||||
val fullRestore = restorePlugin.fullRestorePlugin
|
||||
|
@ -257,13 +280,13 @@ class PluginTest : KoinComponent {
|
|||
assertFalse(fullRestore.hasDataForPackage(token + 1, packageInfo))
|
||||
|
||||
// restore data matches backed up data
|
||||
assertEquals(data, fullRestore.getInputStreamForPackage(token, packageInfo))
|
||||
assertReadEquals(data, fullRestore.getInputStreamForPackage(token, packageInfo))
|
||||
|
||||
// write and check data for second package
|
||||
val data2 = getRandomByteArray(5 * 1024 * 1024)
|
||||
fullBackup.getOutputStream(packageInfo2).writeAndClose(data2)
|
||||
assertTrue(fullRestore.hasDataForPackage(token, packageInfo2))
|
||||
assertEquals(data2, fullRestore.getInputStreamForPackage(token, packageInfo2))
|
||||
assertReadEquals(data2, fullRestore.getInputStreamForPackage(token, packageInfo2))
|
||||
|
||||
// remove data of first package again and ensure that no more data is found
|
||||
fullBackup.removeDataOfPackage(packageInfo)
|
||||
|
@ -277,17 +300,13 @@ class PluginTest : KoinComponent {
|
|||
assertFalse(fullRestore.hasDataForPackage(token, packageInfo2))
|
||||
}
|
||||
|
||||
private fun initStorage(token: Long) {
|
||||
private fun initStorage(token: Long) = runBlocking {
|
||||
every { mockedSettingsManager.getAndResetIsStorageChanging() } returns true
|
||||
assertTrue(backupPlugin.initializeDevice(newToken = token))
|
||||
}
|
||||
|
||||
private fun OutputStream.writeAndClose(data: ByteArray) = use {
|
||||
it.write(data)
|
||||
private fun isNextcloud(): Boolean {
|
||||
return backupPlugin.providerPackageName == "com.nextcloud.client"
|
||||
}
|
||||
|
||||
private fun assertEquals(data: ByteArray, inputStream: InputStream?) = inputStream?.use {
|
||||
assertArrayEquals(data, it.readBytes())
|
||||
} ?: error("no input stream")
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,174 @@
|
|||
package com.stevesoltys.seedvault.plugins.saf
|
||||
|
||||
import android.database.ContentObserver
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.provider.DocumentsContract.EXTRA_LOADING
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import com.stevesoltys.seedvault.assertReadEquals
|
||||
import com.stevesoltys.seedvault.coAssertThrows
|
||||
import com.stevesoltys.seedvault.getRandomBase64
|
||||
import com.stevesoltys.seedvault.getRandomByteArray
|
||||
import com.stevesoltys.seedvault.metadata.MetadataManager
|
||||
import com.stevesoltys.seedvault.settings.SettingsManager
|
||||
import com.stevesoltys.seedvault.writeAndClose
|
||||
import io.mockk.Runs
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.slot
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.TimeoutCancellationException
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.koin.core.KoinComponent
|
||||
import org.koin.core.inject
|
||||
import java.io.IOException
|
||||
import kotlin.random.Random
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
class DocumentsStorageTest : KoinComponent {
|
||||
|
||||
private val context = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
private val metadataManager by inject<MetadataManager>()
|
||||
private val settingsManager by inject<SettingsManager>()
|
||||
private val storage = DocumentsStorage(context, metadataManager, settingsManager)
|
||||
|
||||
private val filename = getRandomBase64()
|
||||
private lateinit var file: DocumentFile
|
||||
|
||||
@Before
|
||||
fun setup() = runBlocking {
|
||||
assertNotNull("Select a storage location in the app first!", storage.rootBackupDir)
|
||||
file = storage.rootBackupDir?.createOrGetFile(context, filename)
|
||||
?: error("Could not create test file")
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
file.delete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWritingAndReadingFile() {
|
||||
// write to output stream
|
||||
val outputStream = storage.getOutputStream(file)
|
||||
val content = ByteArray(1337).apply { Random.nextBytes(this) }
|
||||
outputStream.write(content)
|
||||
outputStream.flush()
|
||||
outputStream.close()
|
||||
|
||||
// read written data from input stream
|
||||
val inputStream = storage.getInputStream(file)
|
||||
val readContent = inputStream.readBytes()
|
||||
inputStream.close()
|
||||
assertArrayEquals(content, readContent)
|
||||
|
||||
// write smaller content to same file
|
||||
val outputStream2 = storage.getOutputStream(file)
|
||||
val content2 = ByteArray(42).apply { Random.nextBytes(this) }
|
||||
outputStream2.write(content2)
|
||||
outputStream2.flush()
|
||||
outputStream2.close()
|
||||
|
||||
// read written data from input stream
|
||||
val inputStream2 = storage.getInputStream(file)
|
||||
val readContent2 = inputStream2.readBytes()
|
||||
inputStream2.close()
|
||||
assertArrayEquals(content2, readContent2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFindFile() = runBlocking(Dispatchers.IO) {
|
||||
val foundFile = storage.rootBackupDir!!.findFileBlocking(context, file.name!!)
|
||||
assertNotNull(foundFile)
|
||||
assertEquals(filename, foundFile!!.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCreateFile() {
|
||||
// create test file
|
||||
val dir = storage.rootBackupDir!!
|
||||
val createdFile = dir.createFile("text", getRandomBase64())
|
||||
assertNotNull(createdFile)
|
||||
assertNotNull(createdFile!!.name)
|
||||
|
||||
// write some data into it
|
||||
val data = getRandomByteArray()
|
||||
context.contentResolver.openOutputStream(createdFile.uri)!!.writeAndClose(data)
|
||||
|
||||
// data should still be there
|
||||
assertReadEquals(data, context.contentResolver.openInputStream(createdFile.uri))
|
||||
|
||||
// delete again
|
||||
createdFile.delete()
|
||||
assertFalse(createdFile.exists())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetLoadedCursor() = runBlocking {
|
||||
// empty cursor extras are like not loading, returns same cursor right away
|
||||
val cursor1: Cursor = mockk()
|
||||
every { cursor1.extras } returns Bundle()
|
||||
assertEquals(cursor1, getLoadedCursor { cursor1 })
|
||||
|
||||
// explicitly not loading, returns same cursor right away
|
||||
val cursor2: Cursor = mockk()
|
||||
every { cursor2.extras } returns Bundle().apply { putBoolean(EXTRA_LOADING, false) }
|
||||
assertEquals(cursor2, getLoadedCursor { cursor2 })
|
||||
|
||||
// loading cursor registers content observer, times out and closes cursor
|
||||
val cursor3: Cursor = mockk()
|
||||
every { cursor3.extras } returns Bundle().apply { putBoolean(EXTRA_LOADING, true) }
|
||||
every { cursor3.registerContentObserver(any()) } just Runs
|
||||
every { cursor3.close() } just Runs
|
||||
coAssertThrows(TimeoutCancellationException::class.java) {
|
||||
getLoadedCursor(1000) { cursor3 }
|
||||
}
|
||||
verify { cursor3.registerContentObserver(any()) }
|
||||
verify { cursor3.close() } // ensure that cursor gets closed
|
||||
|
||||
// loading cursor registers content observer, but re-query fails
|
||||
val cursor4: Cursor = mockk()
|
||||
val observer4 = slot<ContentObserver>()
|
||||
val query: () -> Cursor? = { if (observer4.isCaptured) null else cursor4 }
|
||||
every { cursor4.extras } returns Bundle().apply { putBoolean(EXTRA_LOADING, true) }
|
||||
every { cursor4.registerContentObserver(capture(observer4)) } answers {
|
||||
observer4.captured.onChange(false, Uri.parse("foo://bar"))
|
||||
}
|
||||
every { cursor4.close() } just Runs
|
||||
coAssertThrows(IOException::class.java) {
|
||||
getLoadedCursor(10_000, query)
|
||||
}
|
||||
assertTrue(observer4.isCaptured)
|
||||
verify { cursor4.close() } // ensure that cursor gets closed
|
||||
|
||||
// loading cursor registers content observer, re-queries and returns new result
|
||||
val cursor5: Cursor = mockk()
|
||||
val result5: Cursor = mockk()
|
||||
val observer5 = slot<ContentObserver>()
|
||||
val query5: () -> Cursor? = { if (observer5.isCaptured) result5 else cursor5 }
|
||||
every { cursor5.extras } returns Bundle().apply { putBoolean(EXTRA_LOADING, true) }
|
||||
every { cursor5.registerContentObserver(capture(observer5)) } answers {
|
||||
observer5.captured.onChange(false, null)
|
||||
}
|
||||
every { cursor5.close() } just Runs
|
||||
assertEquals(result5, getLoadedCursor(10_000, query5))
|
||||
assertTrue(observer5.isCaptured)
|
||||
verify { cursor5.close() } // ensure that initial cursor got closed
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.stevesoltys.seedvault.transport.backup
|
||||
|
||||
import android.util.Log
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.koin.core.KoinComponent
|
||||
import org.koin.core.inject
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class PackageServiceTest : KoinComponent {
|
||||
|
||||
private val packageService: PackageService by inject()
|
||||
|
||||
@Test
|
||||
fun testNotAllowedPackages() {
|
||||
val packages = packageService.notAllowedPackages
|
||||
assertTrue(packages.isNotEmpty())
|
||||
Log.e("TEST", "Packages: $packages")
|
||||
}
|
||||
|
||||
}
|
|
@ -14,9 +14,10 @@ import org.koin.core.inject
|
|||
private val TAG = NotificationBackupObserver::class.java.simpleName
|
||||
|
||||
class NotificationBackupObserver(
|
||||
private val context: Context,
|
||||
private val expectedPackages: Int,
|
||||
private val userInitiated: Boolean) : IBackupObserver.Stub(), KoinComponent {
|
||||
private val context: Context,
|
||||
private val expectedPackages: Int,
|
||||
private val userInitiated: Boolean
|
||||
) : IBackupObserver.Stub(), KoinComponent {
|
||||
|
||||
private val nm: BackupNotificationManager by inject()
|
||||
private val metadataManager: MetadataManager by inject()
|
||||
|
@ -25,6 +26,12 @@ class NotificationBackupObserver(
|
|||
|
||||
init {
|
||||
// we need to show this manually as [onUpdate] isn't called for first @pm@ package
|
||||
// TODO consider showing something else for MAGIC_PACKAGE_MANAGER,
|
||||
// because we also back up APKs at the beginning and this can take quite some time.
|
||||
// Therefore, also consider showing a more fine-grained progress bar
|
||||
// by (roughly) doubling the number [expectedPackages] (probably -3)
|
||||
// and calling back here from KvBackup and ApkBackup to update progress.
|
||||
// We will also need to take [PackageService#notAllowedPackages] into account.
|
||||
nm.onBackupUpdate(getAppName(MAGIC_PACKAGE_MANAGER), 0, expectedPackages, userInitiated)
|
||||
}
|
||||
|
||||
|
@ -77,7 +84,9 @@ class NotificationBackupObserver(
|
|||
if (currentPackage == packageName) return
|
||||
|
||||
if (isLoggable(TAG, INFO)) {
|
||||
Log.i(TAG, "Showing progress notification for $currentPackage $numPackages/$expectedPackages")
|
||||
"Showing progress notification for $currentPackage $numPackages/$expectedPackages".let {
|
||||
Log.i(TAG, it)
|
||||
}
|
||||
}
|
||||
currentPackage = packageName
|
||||
val app = getAppName(packageName)
|
||||
|
|
|
@ -2,8 +2,6 @@ package com.stevesoltys.seedvault.metadata
|
|||
|
||||
import android.content.Context
|
||||
import android.content.Context.MODE_PRIVATE
|
||||
import android.content.pm.ApplicationInfo.FLAG_SYSTEM
|
||||
import android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP
|
||||
import android.content.pm.PackageInfo
|
||||
import android.util.Log
|
||||
import androidx.annotation.VisibleForTesting
|
||||
|
@ -12,23 +10,25 @@ import androidx.lifecycle.LiveData
|
|||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.distinctUntilChanged
|
||||
import com.stevesoltys.seedvault.Clock
|
||||
import com.stevesoltys.seedvault.MAGIC_PACKAGE_MANAGER
|
||||
import com.stevesoltys.seedvault.metadata.PackageState.APK_AND_DATA
|
||||
import com.stevesoltys.seedvault.metadata.PackageState.NOT_ALLOWED
|
||||
import com.stevesoltys.seedvault.transport.backup.isSystemApp
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.IOException
|
||||
import java.io.OutputStream
|
||||
|
||||
private val TAG = MetadataManager::class.java.simpleName
|
||||
|
||||
@VisibleForTesting
|
||||
internal const val METADATA_CACHE_FILE = "metadata.cache"
|
||||
|
||||
@WorkerThread
|
||||
class MetadataManager(
|
||||
private val context: Context,
|
||||
private val clock: Clock,
|
||||
private val metadataWriter: MetadataWriter,
|
||||
private val metadataReader: MetadataReader) {
|
||||
private val context: Context,
|
||||
private val clock: Clock,
|
||||
private val metadataWriter: MetadataWriter,
|
||||
private val metadataReader: MetadataReader
|
||||
) {
|
||||
|
||||
private val uninitializedMetadata = BackupMetadata(token = 0L)
|
||||
private var metadata: BackupMetadata = uninitializedMetadata
|
||||
|
@ -67,7 +67,11 @@ class MetadataManager(
|
|||
*/
|
||||
@Synchronized
|
||||
@Throws(IOException::class)
|
||||
fun onApkBackedUp(packageInfo: PackageInfo, packageMetadata: PackageMetadata, metadataOutputStream: OutputStream) {
|
||||
fun onApkBackedUp(
|
||||
packageInfo: PackageInfo,
|
||||
packageMetadata: PackageMetadata,
|
||||
metadataOutputStream: OutputStream
|
||||
) {
|
||||
val packageName = packageInfo.packageName
|
||||
metadata.packageMetadataMap[packageName]?.let {
|
||||
check(packageMetadata.version != null) {
|
||||
|
@ -78,20 +82,21 @@ class MetadataManager(
|
|||
}
|
||||
}
|
||||
val oldPackageMetadata = metadata.packageMetadataMap[packageName]
|
||||
?: PackageMetadata()
|
||||
?: PackageMetadata()
|
||||
// only allow state change if backup of this package is not allowed
|
||||
val newState = if (packageMetadata.state == NOT_ALLOWED)
|
||||
val newState = if (packageMetadata.state == NOT_ALLOWED) {
|
||||
packageMetadata.state
|
||||
else
|
||||
} else {
|
||||
oldPackageMetadata.state
|
||||
}
|
||||
modifyMetadata(metadataOutputStream) {
|
||||
metadata.packageMetadataMap[packageName] = oldPackageMetadata.copy(
|
||||
state = newState,
|
||||
system = packageInfo.isSystemApp(),
|
||||
version = packageMetadata.version,
|
||||
installer = packageMetadata.installer,
|
||||
sha256 = packageMetadata.sha256,
|
||||
signatures = packageMetadata.signatures
|
||||
state = newState,
|
||||
system = packageInfo.isSystemApp(),
|
||||
version = packageMetadata.version,
|
||||
installer = packageMetadata.installer,
|
||||
sha256 = packageMetadata.sha256,
|
||||
signatures = packageMetadata.signatures
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -114,9 +119,9 @@ class MetadataManager(
|
|||
metadata.packageMetadataMap[packageName]!!.state = APK_AND_DATA
|
||||
} else {
|
||||
metadata.packageMetadataMap[packageName] = PackageMetadata(
|
||||
time = now,
|
||||
state = APK_AND_DATA,
|
||||
system = packageInfo.isSystemApp()
|
||||
time = now,
|
||||
state = APK_AND_DATA,
|
||||
system = packageInfo.isSystemApp()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -130,7 +135,11 @@ class MetadataManager(
|
|||
*/
|
||||
@Synchronized
|
||||
@Throws(IOException::class)
|
||||
internal fun onPackageBackupError(packageInfo: PackageInfo, packageState: PackageState, metadataOutputStream: OutputStream) {
|
||||
internal fun onPackageBackupError(
|
||||
packageInfo: PackageInfo,
|
||||
packageState: PackageState,
|
||||
metadataOutputStream: OutputStream
|
||||
) {
|
||||
check(packageState != APK_AND_DATA) { "Backup Error with non-error package state." }
|
||||
val packageName = packageInfo.packageName
|
||||
modifyMetadata(metadataOutputStream) {
|
||||
|
@ -138,9 +147,9 @@ class MetadataManager(
|
|||
metadata.packageMetadataMap[packageName]!!.state = packageState
|
||||
} else {
|
||||
metadata.packageMetadataMap[packageName] = PackageMetadata(
|
||||
time = 0L,
|
||||
state = packageState,
|
||||
system = packageInfo.isSystemApp()
|
||||
time = 0L,
|
||||
state = packageState,
|
||||
system = packageInfo.isSystemApp()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -219,13 +228,3 @@ class MetadataManager(
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
fun PackageInfo.isSystemApp(): Boolean {
|
||||
if (packageName == MAGIC_PACKAGE_MANAGER || applicationInfo == null) return true
|
||||
return applicationInfo.flags and FLAG_SYSTEM != 0
|
||||
}
|
||||
|
||||
fun PackageInfo.isUpdatedSystemApp(): Boolean {
|
||||
if (packageName == MAGIC_PACKAGE_MANAGER || applicationInfo == null) return false
|
||||
return applicationInfo.flags and FLAG_UPDATED_SYSTEM_APP != 0
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.stevesoltys.seedvault.plugins.saf
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
import com.stevesoltys.seedvault.transport.backup.BackupPlugin
|
||||
|
@ -10,23 +11,28 @@ import java.io.OutputStream
|
|||
|
||||
private const val MIME_TYPE_APK = "application/vnd.android.package-archive"
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class DocumentsProviderBackupPlugin(
|
||||
private val storage: DocumentsStorage,
|
||||
packageManager: PackageManager) : BackupPlugin {
|
||||
private val context: Context,
|
||||
private val storage: DocumentsStorage
|
||||
) : BackupPlugin {
|
||||
|
||||
private val packageManager: PackageManager = context.packageManager
|
||||
|
||||
override val kvBackupPlugin: KVBackupPlugin by lazy {
|
||||
DocumentsProviderKVBackup(storage)
|
||||
DocumentsProviderKVBackup(storage, context)
|
||||
}
|
||||
|
||||
override val fullBackupPlugin: FullBackupPlugin by lazy {
|
||||
DocumentsProviderFullBackup(storage)
|
||||
DocumentsProviderFullBackup(storage, context)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun initializeDevice(newToken: Long): Boolean {
|
||||
override suspend fun initializeDevice(newToken: Long): Boolean {
|
||||
// check if storage is already initialized
|
||||
if (storage.isInitialized()) return false
|
||||
|
||||
// TODO consider not creating new RestoreSets, but continue working within the existing one.
|
||||
// reset current storage
|
||||
storage.reset(newToken)
|
||||
|
||||
|
@ -38,7 +44,7 @@ internal class DocumentsProviderBackupPlugin(
|
|||
val fullDir = storage.currentFullBackupDir
|
||||
|
||||
// wipe existing data
|
||||
storage.getSetDir()?.findFile(FILE_BACKUP_METADATA)?.delete()
|
||||
storage.getSetDir()?.findFileBlocking(context, FILE_BACKUP_METADATA)?.delete()
|
||||
kvDir?.deleteContents()
|
||||
fullDir?.deleteContents()
|
||||
|
||||
|
@ -46,16 +52,16 @@ internal class DocumentsProviderBackupPlugin(
|
|||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun getMetadataOutputStream(): OutputStream {
|
||||
override suspend fun getMetadataOutputStream(): OutputStream {
|
||||
val setDir = storage.getSetDir() ?: throw IOException()
|
||||
val metadataFile = setDir.createOrGetFile(FILE_BACKUP_METADATA)
|
||||
val metadataFile = setDir.createOrGetFile(context, FILE_BACKUP_METADATA)
|
||||
return storage.getOutputStream(metadataFile)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun getApkOutputStream(packageInfo: PackageInfo): OutputStream {
|
||||
override suspend fun getApkOutputStream(packageInfo: PackageInfo): OutputStream {
|
||||
val setDir = storage.getSetDir() ?: throw IOException()
|
||||
val file = setDir.createOrGetFile("${packageInfo.packageName}.apk", MIME_TYPE_APK)
|
||||
val file = setDir.createOrGetFile(context, "${packageInfo.packageName}.apk", MIME_TYPE_APK)
|
||||
return storage.getOutputStream(file)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.stevesoltys.seedvault.plugins.saf
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageInfo
|
||||
import android.util.Log
|
||||
import com.stevesoltys.seedvault.transport.backup.DEFAULT_QUOTA_FULL_BACKUP
|
||||
|
@ -9,23 +10,27 @@ import java.io.OutputStream
|
|||
|
||||
private val TAG = DocumentsProviderFullBackup::class.java.simpleName
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class DocumentsProviderFullBackup(
|
||||
private val storage: DocumentsStorage) : FullBackupPlugin {
|
||||
private val storage: DocumentsStorage,
|
||||
private val context: Context
|
||||
) : FullBackupPlugin {
|
||||
|
||||
override fun getQuota() = DEFAULT_QUOTA_FULL_BACKUP
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun getOutputStream(targetPackage: PackageInfo): OutputStream {
|
||||
val file = storage.currentFullBackupDir?.createOrGetFile(targetPackage.packageName)
|
||||
?: throw IOException()
|
||||
override suspend fun getOutputStream(targetPackage: PackageInfo): OutputStream {
|
||||
val file = storage.currentFullBackupDir?.createOrGetFile(context, targetPackage.packageName)
|
||||
?: throw IOException()
|
||||
return storage.getOutputStream(file)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun removeDataOfPackage(packageInfo: PackageInfo) {
|
||||
override suspend fun removeDataOfPackage(packageInfo: PackageInfo) {
|
||||
val packageName = packageInfo.packageName
|
||||
Log.i(TAG, "Deleting $packageName...")
|
||||
val file = storage.currentFullBackupDir?.findFile(packageName) ?: return
|
||||
val file = storage.currentFullBackupDir?.findFileBlocking(context, packageName)
|
||||
?: return
|
||||
if (!file.delete()) throw IOException("Failed to delete $packageName")
|
||||
}
|
||||
|
||||
|
|
|
@ -1,23 +1,31 @@
|
|||
package com.stevesoltys.seedvault.plugins.saf
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageInfo
|
||||
import com.stevesoltys.seedvault.transport.restore.FullRestorePlugin
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class DocumentsProviderFullRestorePlugin(
|
||||
private val documentsStorage: DocumentsStorage) : FullRestorePlugin {
|
||||
private val context: Context,
|
||||
private val documentsStorage: DocumentsStorage
|
||||
) : FullRestorePlugin {
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean {
|
||||
override suspend fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean {
|
||||
val backupDir = documentsStorage.getFullBackupDir(token) ?: return false
|
||||
return backupDir.findFile(packageInfo.packageName) != null
|
||||
return backupDir.findFileBlocking(context, packageInfo.packageName) != null
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun getInputStreamForPackage(token: Long, packageInfo: PackageInfo): InputStream {
|
||||
override suspend fun getInputStreamForPackage(
|
||||
token: Long,
|
||||
packageInfo: PackageInfo
|
||||
): InputStream {
|
||||
val backupDir = documentsStorage.getFullBackupDir(token) ?: throw IOException()
|
||||
val packageFile = backupDir.findFile(packageInfo.packageName) ?: throw IOException()
|
||||
val packageFile =
|
||||
backupDir.findFileBlocking(context, packageInfo.packageName) ?: throw IOException()
|
||||
return documentsStorage.getInputStream(packageFile)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,52 +1,76 @@
|
|||
package com.stevesoltys.seedvault.plugins.saf
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageInfo
|
||||
import android.util.Log
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.stevesoltys.seedvault.transport.backup.DEFAULT_QUOTA_KEY_VALUE_BACKUP
|
||||
import com.stevesoltys.seedvault.transport.backup.KVBackupPlugin
|
||||
import java.io.IOException
|
||||
import java.io.OutputStream
|
||||
|
||||
internal class DocumentsProviderKVBackup(private val storage: DocumentsStorage) : KVBackupPlugin {
|
||||
const val MAX_KEY_LENGTH = 255
|
||||
const val MAX_KEY_LENGTH_NEXTCLOUD = 228
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class DocumentsProviderKVBackup(
|
||||
private val storage: DocumentsStorage,
|
||||
private val context: Context
|
||||
) : KVBackupPlugin {
|
||||
|
||||
private var packageFile: DocumentFile? = null
|
||||
|
||||
override fun getQuota(): Long = DEFAULT_QUOTA_KEY_VALUE_BACKUP
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun hasDataForPackage(packageInfo: PackageInfo): Boolean {
|
||||
val packageFile = storage.currentKvBackupDir?.findFile(packageInfo.packageName)
|
||||
override suspend fun hasDataForPackage(packageInfo: PackageInfo): Boolean {
|
||||
val packageFile =
|
||||
storage.currentKvBackupDir?.findFileBlocking(context, packageInfo.packageName)
|
||||
?: return false
|
||||
return packageFile.listFiles().isNotEmpty()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun ensureRecordStorageForPackage(packageInfo: PackageInfo) {
|
||||
override suspend fun ensureRecordStorageForPackage(packageInfo: PackageInfo) {
|
||||
// remember package file for subsequent operations
|
||||
packageFile = storage.getOrCreateKVBackupDir().createOrGetDirectory(packageInfo.packageName)
|
||||
packageFile =
|
||||
storage.getOrCreateKVBackupDir().createOrGetDirectory(context, packageInfo.packageName)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun removeDataOfPackage(packageInfo: PackageInfo) {
|
||||
override suspend fun removeDataOfPackage(packageInfo: PackageInfo) {
|
||||
// we cannot use the cached this.packageFile here,
|
||||
// because this can be called before [ensureRecordStorageForPackage]
|
||||
val packageFile = storage.currentKvBackupDir?.findFile(packageInfo.packageName) ?: return
|
||||
val packageFile =
|
||||
storage.currentKvBackupDir?.findFileBlocking(context, packageInfo.packageName) ?: return
|
||||
packageFile.delete()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun deleteRecord(packageInfo: PackageInfo, key: String) {
|
||||
override suspend fun deleteRecord(packageInfo: PackageInfo, key: String) {
|
||||
val packageFile = this.packageFile ?: throw AssertionError()
|
||||
packageFile.assertRightFile(packageInfo)
|
||||
val keyFile = packageFile.findFile(key) ?: return
|
||||
val keyFile = packageFile.findFileBlocking(context, key) ?: return
|
||||
keyFile.delete()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun getOutputStreamForRecord(packageInfo: PackageInfo, key: String): OutputStream {
|
||||
override suspend fun getOutputStreamForRecord(
|
||||
packageInfo: PackageInfo,
|
||||
key: String
|
||||
): OutputStream {
|
||||
check(key.length < MAX_KEY_LENGTH) {
|
||||
"Key $key for ${packageInfo.packageName} is too long: ${key.length} chars."
|
||||
}
|
||||
if (key.length > MAX_KEY_LENGTH_NEXTCLOUD) {
|
||||
Log.e(
|
||||
DocumentsProviderKVBackup::class.simpleName,
|
||||
"Key $key for ${packageInfo.packageName} is too long: ${key.length} chars."
|
||||
)
|
||||
}
|
||||
val packageFile = this.packageFile ?: throw AssertionError()
|
||||
packageFile.assertRightFile(packageInfo)
|
||||
val keyFile = packageFile.createOrGetFile(key)
|
||||
val keyFile = packageFile.createOrGetFile(context, key)
|
||||
return storage.getOutputStream(keyFile)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,20 +1,25 @@
|
|||
package com.stevesoltys.seedvault.plugins.saf
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageInfo
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.stevesoltys.seedvault.transport.restore.KVRestorePlugin
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
|
||||
internal class DocumentsProviderKVRestorePlugin(private val storage: DocumentsStorage) : KVRestorePlugin {
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class DocumentsProviderKVRestorePlugin(
|
||||
private val context: Context,
|
||||
private val storage: DocumentsStorage
|
||||
) : KVRestorePlugin {
|
||||
|
||||
private var packageDir: DocumentFile? = null
|
||||
|
||||
override fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean {
|
||||
override suspend fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean {
|
||||
return try {
|
||||
val backupDir = storage.getKVBackupDir(token) ?: return false
|
||||
// remember package file for subsequent operations
|
||||
packageDir = backupDir.findFile(packageInfo.packageName)
|
||||
packageDir = backupDir.findFileBlocking(context, packageInfo.packageName)
|
||||
packageDir != null
|
||||
} catch (e: IOException) {
|
||||
false
|
||||
|
@ -25,15 +30,19 @@ internal class DocumentsProviderKVRestorePlugin(private val storage: DocumentsSt
|
|||
val packageDir = this.packageDir ?: throw AssertionError()
|
||||
packageDir.assertRightFile(packageInfo)
|
||||
return packageDir.listFiles()
|
||||
.filter { file -> file.name != null }
|
||||
.map { file -> file.name!! }
|
||||
.filter { file -> file.name != null }
|
||||
.map { file -> file.name!! }
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun getInputStreamForRecord(token: Long, packageInfo: PackageInfo, key: String): InputStream {
|
||||
override suspend fun getInputStreamForRecord(
|
||||
token: Long,
|
||||
packageInfo: PackageInfo,
|
||||
key: String
|
||||
): InputStream {
|
||||
val packageDir = this.packageDir ?: throw AssertionError()
|
||||
packageDir.assertRightFile(packageInfo)
|
||||
val keyFile = packageDir.findFile(key) ?: throw IOException()
|
||||
val keyFile = packageDir.findFileBlocking(context, key) ?: throw IOException()
|
||||
return storage.getInputStream(keyFile)
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,6 @@ import org.koin.dsl.module
|
|||
|
||||
val documentsProviderModule = module {
|
||||
single { DocumentsStorage(androidContext(), get(), get()) }
|
||||
single<BackupPlugin> { DocumentsProviderBackupPlugin(get(), androidContext().packageManager) }
|
||||
single<BackupPlugin> { DocumentsProviderBackupPlugin(androidContext(), get()) }
|
||||
single<RestorePlugin> { DocumentsProviderRestorePlugin(androidContext(), get()) }
|
||||
}
|
||||
|
|
|
@ -15,32 +15,35 @@ import java.io.InputStream
|
|||
|
||||
private val TAG = DocumentsProviderRestorePlugin::class.java.simpleName
|
||||
|
||||
@WorkerThread
|
||||
@Suppress("BlockingMethodInNonBlockingContext") // all methods do I/O
|
||||
internal class DocumentsProviderRestorePlugin(
|
||||
private val context: Context,
|
||||
private val storage: DocumentsStorage) : RestorePlugin {
|
||||
private val context: Context,
|
||||
private val storage: DocumentsStorage
|
||||
) : RestorePlugin {
|
||||
|
||||
override val kvRestorePlugin: KVRestorePlugin by lazy {
|
||||
DocumentsProviderKVRestorePlugin(storage)
|
||||
DocumentsProviderKVRestorePlugin(context, storage)
|
||||
}
|
||||
|
||||
override val fullRestorePlugin: FullRestorePlugin by lazy {
|
||||
DocumentsProviderFullRestorePlugin(storage)
|
||||
DocumentsProviderFullRestorePlugin(context, storage)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
override fun hasBackup(uri: Uri): Boolean {
|
||||
@Throws(IOException::class)
|
||||
override suspend fun hasBackup(uri: Uri): Boolean {
|
||||
val parent = DocumentFile.fromTreeUri(context, uri) ?: throw AssertionError()
|
||||
val rootDir = parent.findFileBlocking(context, DIRECTORY_ROOT) ?: return false
|
||||
val backupSets = getBackups(context, rootDir)
|
||||
return backupSets.isNotEmpty()
|
||||
}
|
||||
|
||||
override fun getAvailableBackups(): Sequence<EncryptedBackupMetadata>? {
|
||||
override suspend fun getAvailableBackups(): Sequence<EncryptedBackupMetadata>? {
|
||||
val rootDir = storage.rootBackupDir ?: return null
|
||||
val backupSets = getBackups(context, rootDir)
|
||||
val iterator = backupSets.iterator()
|
||||
return generateSequence {
|
||||
if (!iterator.hasNext()) return@generateSequence null // end sequence
|
||||
if (!iterator.hasNext()) return@generateSequence null // end sequence
|
||||
val backupSet = iterator.next()
|
||||
try {
|
||||
val stream = storage.getInputStream(backupSet.metadataFile)
|
||||
|
@ -52,8 +55,7 @@ internal class DocumentsProviderRestorePlugin(
|
|||
}
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
private fun getBackups(context: Context, rootDir: DocumentFile): List<BackupSet> {
|
||||
private suspend fun getBackups(context: Context, rootDir: DocumentFile): List<BackupSet> {
|
||||
val backupSets = ArrayList<BackupSet>()
|
||||
val files = try {
|
||||
// block until the DocumentsProvider has results
|
||||
|
@ -63,20 +65,16 @@ internal class DocumentsProviderRestorePlugin(
|
|||
return backupSets
|
||||
}
|
||||
for (set in files) {
|
||||
if (!set.isDirectory || set.name == null) {
|
||||
if (set.name != FILE_NO_MEDIA) {
|
||||
Log.w(TAG, "Found invalid backup set folder: ${set.name}")
|
||||
}
|
||||
continue
|
||||
}
|
||||
val token = try {
|
||||
set.name!!.toLong()
|
||||
} catch (e: NumberFormatException) {
|
||||
Log.w(TAG, "Found invalid backup set folder: ${set.name}")
|
||||
continue
|
||||
}
|
||||
// get current token from set or continue to next file/set
|
||||
val token = set.getTokenOrNull() ?: continue
|
||||
|
||||
// block until children of set are available
|
||||
val metadata = set.findFileBlocking(context, FILE_BACKUP_METADATA)
|
||||
val metadata = try {
|
||||
set.findFileBlocking(context, FILE_BACKUP_METADATA)
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error reading metadata file in backup set folder: ${set.name}", e)
|
||||
null
|
||||
}
|
||||
if (metadata == null) {
|
||||
Log.w(TAG, "Missing metadata file in backup set folder: ${set.name}")
|
||||
} else {
|
||||
|
@ -86,10 +84,26 @@ internal class DocumentsProviderRestorePlugin(
|
|||
return backupSets
|
||||
}
|
||||
|
||||
private fun DocumentFile.getTokenOrNull(): Long? {
|
||||
if (!isDirectory || name == null) {
|
||||
if (name != FILE_NO_MEDIA) {
|
||||
Log.w(TAG, "Found invalid backup set folder: $name")
|
||||
}
|
||||
return null
|
||||
}
|
||||
return try {
|
||||
name!!.toLong()
|
||||
} catch (e: NumberFormatException) {
|
||||
Log.w(TAG, "Found invalid backup set folder: $name")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun getApkInputStream(token: Long, packageName: String): InputStream {
|
||||
override suspend fun getApkInputStream(token: Long, packageName: String): InputStream {
|
||||
val setDir = storage.getSetDir(token) ?: throw IOException()
|
||||
val file = setDir.findFile("$packageName.apk") ?: throw FileNotFoundException()
|
||||
val file =
|
||||
setDir.findFileBlocking(context, "$packageName.apk") ?: throw FileNotFoundException()
|
||||
return storage.getInputStream(file)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
@file:Suppress("EXPERIMENTAL_API_USAGE", "BlockingMethodInNonBlockingContext")
|
||||
|
||||
package com.stevesoltys.seedvault.plugins.saf
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageInfo
|
||||
import android.database.ContentObserver
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.os.FileUtils.closeQuietly
|
||||
import android.provider.DocumentsContract.Document.COLUMN_DOCUMENT_ID
|
||||
import android.provider.DocumentsContract.Document.COLUMN_MIME_TYPE
|
||||
import android.provider.DocumentsContract.Document.MIME_TYPE_DIR
|
||||
|
@ -14,15 +17,19 @@ import android.provider.DocumentsContract.buildDocumentUriUsingTree
|
|||
import android.provider.DocumentsContract.buildTreeDocumentUri
|
||||
import android.provider.DocumentsContract.getDocumentId
|
||||
import android.util.Log
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.stevesoltys.seedvault.metadata.MetadataManager
|
||||
import com.stevesoltys.seedvault.settings.SettingsManager
|
||||
import com.stevesoltys.seedvault.settings.Storage
|
||||
import libcore.io.IoUtils.closeQuietly
|
||||
import kotlinx.coroutines.TimeoutCancellationException
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.util.concurrent.TimeUnit.MINUTES
|
||||
import kotlin.coroutines.resume
|
||||
|
||||
const val DIRECTORY_ROOT = ".SeedVaultAndroidBackup"
|
||||
const val DIRECTORY_FULL_BACKUP = "full"
|
||||
|
@ -34,9 +41,12 @@ private const val MIME_TYPE = "application/octet-stream"
|
|||
private val TAG = DocumentsStorage::class.java.simpleName
|
||||
|
||||
internal class DocumentsStorage(
|
||||
private val context: Context,
|
||||
private val metadataManager: MetadataManager,
|
||||
private val settingsManager: SettingsManager) {
|
||||
private val context: Context,
|
||||
private val metadataManager: MetadataManager,
|
||||
private val settingsManager: SettingsManager
|
||||
) {
|
||||
|
||||
private val contentResolver = context.contentResolver
|
||||
|
||||
internal var storage: Storage? = null
|
||||
get() {
|
||||
|
@ -45,20 +55,22 @@ internal class DocumentsStorage(
|
|||
}
|
||||
|
||||
internal var rootBackupDir: DocumentFile? = null
|
||||
get() {
|
||||
get() = runBlocking {
|
||||
if (field == null) {
|
||||
val parent = storage?.getDocumentFile(context) ?: return null
|
||||
val parent = storage?.getDocumentFile(context)
|
||||
?: return@runBlocking null
|
||||
field = try {
|
||||
val rootDir = parent.createOrGetDirectory(DIRECTORY_ROOT)
|
||||
// create .nomedia file to prevent Android's MediaScanner from trying to index the backup
|
||||
rootDir.createOrGetFile(FILE_NO_MEDIA)
|
||||
rootDir
|
||||
parent.createOrGetDirectory(context, DIRECTORY_ROOT).apply {
|
||||
// create .nomedia file to prevent Android's MediaScanner
|
||||
// from trying to index the backup
|
||||
createOrGetFile(context, FILE_NO_MEDIA)
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error creating root backup dir.", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
return field
|
||||
field
|
||||
}
|
||||
|
||||
private var currentToken: Long = 0L
|
||||
|
@ -68,47 +80,47 @@ internal class DocumentsStorage(
|
|||
}
|
||||
|
||||
private var currentSetDir: DocumentFile? = null
|
||||
get() {
|
||||
get() = runBlocking {
|
||||
if (field == null) {
|
||||
if (currentToken == 0L) return null
|
||||
if (currentToken == 0L) return@runBlocking null
|
||||
field = try {
|
||||
rootBackupDir?.createOrGetDirectory(currentToken.toString())
|
||||
rootBackupDir?.createOrGetDirectory(context, currentToken.toString())
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error creating current restore set dir.", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
return field
|
||||
field
|
||||
}
|
||||
|
||||
var currentFullBackupDir: DocumentFile? = null
|
||||
get() {
|
||||
get() = runBlocking {
|
||||
if (field == null) {
|
||||
field = try {
|
||||
currentSetDir?.createOrGetDirectory(DIRECTORY_FULL_BACKUP)
|
||||
currentSetDir?.createOrGetDirectory(context, DIRECTORY_FULL_BACKUP)
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error creating full backup dir.", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
return field
|
||||
field
|
||||
}
|
||||
|
||||
var currentKvBackupDir: DocumentFile? = null
|
||||
get() {
|
||||
get() = runBlocking {
|
||||
if (field == null) {
|
||||
field = try {
|
||||
currentSetDir?.createOrGetDirectory(DIRECTORY_KEY_VALUE_BACKUP)
|
||||
currentSetDir?.createOrGetDirectory(context, DIRECTORY_KEY_VALUE_BACKUP)
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error creating K/V backup dir.", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
return field
|
||||
field
|
||||
}
|
||||
|
||||
fun isInitialized(): Boolean {
|
||||
if (settingsManager.getAndResetIsStorageChanging()) return false // storage location has changed
|
||||
if (settingsManager.getAndResetIsStorageChanging()) return false // storage location has changed
|
||||
val kvEmpty = currentKvBackupDir?.listFiles()?.isEmpty() ?: false
|
||||
val fullEmpty = currentFullBackupDir?.listFiles()?.isEmpty() ?: false
|
||||
return kvEmpty && fullEmpty
|
||||
|
@ -125,48 +137,65 @@ internal class DocumentsStorage(
|
|||
|
||||
fun getAuthority(): String? = storage?.uri?.authority
|
||||
|
||||
fun getSetDir(token: Long = currentToken): DocumentFile? {
|
||||
@Throws(IOException::class)
|
||||
suspend fun getSetDir(token: Long = currentToken): DocumentFile? {
|
||||
if (token == currentToken) return currentSetDir
|
||||
return rootBackupDir?.findFile(token.toString())
|
||||
}
|
||||
|
||||
fun getKVBackupDir(token: Long = currentToken): DocumentFile? {
|
||||
if (token == currentToken) return currentKvBackupDir ?: throw IOException()
|
||||
return getSetDir(token)?.findFile(DIRECTORY_KEY_VALUE_BACKUP)
|
||||
return rootBackupDir?.findFileBlocking(context, token.toString())
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun getOrCreateKVBackupDir(token: Long = currentToken): DocumentFile {
|
||||
suspend fun getKVBackupDir(token: Long = currentToken): DocumentFile? {
|
||||
if (token == currentToken) return currentKvBackupDir ?: throw IOException()
|
||||
val setDir = getSetDir(token) ?: throw IOException()
|
||||
return setDir.createOrGetDirectory(DIRECTORY_KEY_VALUE_BACKUP)
|
||||
return getSetDir(token)?.findFileBlocking(context, DIRECTORY_KEY_VALUE_BACKUP)
|
||||
}
|
||||
|
||||
fun getFullBackupDir(token: Long = currentToken): DocumentFile? {
|
||||
@Throws(IOException::class)
|
||||
suspend fun getOrCreateKVBackupDir(token: Long = currentToken): DocumentFile {
|
||||
if (token == currentToken) return currentKvBackupDir ?: throw IOException()
|
||||
val setDir = getSetDir(token) ?: throw IOException()
|
||||
return setDir.createOrGetDirectory(context, DIRECTORY_KEY_VALUE_BACKUP)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
suspend fun getFullBackupDir(token: Long = currentToken): DocumentFile? {
|
||||
if (token == currentToken) return currentFullBackupDir ?: throw IOException()
|
||||
return getSetDir(token)?.findFile(DIRECTORY_FULL_BACKUP)
|
||||
return getSetDir(token)?.findFileBlocking(context, DIRECTORY_FULL_BACKUP)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun getInputStream(file: DocumentFile): InputStream {
|
||||
return context.contentResolver.openInputStream(file.uri) ?: throw IOException()
|
||||
return contentResolver.openInputStream(file.uri) ?: throw IOException()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun getOutputStream(file: DocumentFile): OutputStream {
|
||||
return context.contentResolver.openOutputStream(file.uri, "wt") ?: throw IOException()
|
||||
return contentResolver.openOutputStream(file.uri, "wt") ?: throw IOException()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a file exists and if not, creates it.
|
||||
*
|
||||
* If we were trying to create it right away, some providers create "filename (1)".
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun DocumentFile.createOrGetFile(name: String, mimeType: String = MIME_TYPE): DocumentFile {
|
||||
return findFile(name) ?: createFile(mimeType, name) ?: throw IOException()
|
||||
internal suspend fun DocumentFile.createOrGetFile(
|
||||
context: Context,
|
||||
name: String,
|
||||
mimeType: String = MIME_TYPE
|
||||
): DocumentFile {
|
||||
return findFileBlocking(context, name) ?: createFile(mimeType, name)?.apply {
|
||||
check(this.name == name) { "File named ${this.name}, but should be $name" }
|
||||
} ?: throw IOException()
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a directory already exists and if not, creates it.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun DocumentFile.createOrGetDirectory(name: String): DocumentFile {
|
||||
return findFile(name) ?: createDirectory(name) ?: throw IOException()
|
||||
suspend fun DocumentFile.createOrGetDirectory(context: Context, name: String): DocumentFile {
|
||||
return findFileBlocking(context, name) ?: createDirectory(name) ?: throw IOException()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
|
@ -183,43 +212,22 @@ fun DocumentFile.assertRightFile(packageInfo: PackageInfo) {
|
|||
* This prevents getting an empty list even though there are children to be listed.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun DocumentFile.listFilesBlocking(context: Context): ArrayList<DocumentFile> {
|
||||
suspend fun DocumentFile.listFilesBlocking(context: Context): ArrayList<DocumentFile> {
|
||||
val resolver = context.contentResolver
|
||||
val childrenUri = buildChildDocumentsUriUsingTree(uri, getDocumentId(uri))
|
||||
val projection = arrayOf(COLUMN_DOCUMENT_ID, COLUMN_MIME_TYPE)
|
||||
val result = ArrayList<DocumentFile>()
|
||||
|
||||
@SuppressLint("Recycle") // gets closed in with(), only earlier exit when null
|
||||
var cursor = resolver.query(childrenUri, projection, null, null, null)
|
||||
?: throw IOException()
|
||||
val loading = cursor.extras.getBoolean(EXTRA_LOADING, false)
|
||||
if (loading) {
|
||||
Log.d(TAG, "Wait for children to get loaded...")
|
||||
var loaded = false
|
||||
cursor.registerContentObserver(object : ContentObserver(null) {
|
||||
override fun onChange(selfChange: Boolean, uri: Uri?) {
|
||||
Log.d(TAG, "Children loaded. Continue...")
|
||||
loaded = true
|
||||
}
|
||||
})
|
||||
val timeout = MINUTES.toMillis(2)
|
||||
var time = 0
|
||||
// TODO replace loop with callback flow or something similar
|
||||
while (!loaded && time < timeout) {
|
||||
Thread.sleep(50)
|
||||
time += 50
|
||||
try {
|
||||
getLoadedCursor {
|
||||
resolver.query(childrenUri, projection, null, null, null)
|
||||
}
|
||||
if (time >= timeout) Log.w(TAG, "Timed out while waiting for children to load")
|
||||
closeQuietly(cursor)
|
||||
// do a new query after content was loaded
|
||||
@SuppressLint("Recycle") // gets closed after with block
|
||||
cursor = resolver.query(childrenUri, projection, null, null, null)
|
||||
?: throw IOException()
|
||||
}
|
||||
with(cursor) {
|
||||
while (moveToNext()) {
|
||||
val documentId = getString(0)
|
||||
val isDirectory = getString(1) == MIME_TYPE_DIR
|
||||
} catch (e: TimeoutCancellationException) {
|
||||
throw IOException(e)
|
||||
}.use { cursor ->
|
||||
while (cursor.moveToNext()) {
|
||||
val documentId = cursor.getString(0)
|
||||
val isDirectory = cursor.getString(1) == MIME_TYPE_DIR
|
||||
val file = if (isDirectory) {
|
||||
val treeUri = buildTreeDocumentUri(uri.authority, documentId)
|
||||
DocumentFile.fromTreeUri(context, treeUri)!!
|
||||
|
@ -233,7 +241,14 @@ fun DocumentFile.listFilesBlocking(context: Context): ArrayList<DocumentFile> {
|
|||
return result
|
||||
}
|
||||
|
||||
fun DocumentFile.findFileBlocking(context: Context, displayName: String): DocumentFile? {
|
||||
/**
|
||||
* Same as [DocumentFile.findFile] only that it re-queries when the first result was stale.
|
||||
*
|
||||
* Most documents providers including Nextcloud are listing the full directory content
|
||||
* when querying for a specific file in a directory,
|
||||
* so there is no point in trying to optimize the query by not listing all children.
|
||||
*/
|
||||
suspend fun DocumentFile.findFileBlocking(context: Context, displayName: String): DocumentFile? {
|
||||
val files = try {
|
||||
listFilesBlocking(context)
|
||||
} catch (e: IOException) {
|
||||
|
@ -245,3 +260,46 @@ fun DocumentFile.findFileBlocking(context: Context, displayName: String): Docume
|
|||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a cursor for the given query while ensuring that the cursor was loaded.
|
||||
*
|
||||
* When the SAF backend is a cloud storage provider (e.g. Nextcloud),
|
||||
* it can happen that the query returns an outdated (e.g. empty) cursor
|
||||
* which will only be updated in response to this query.
|
||||
*
|
||||
* See: https://commonsware.com/blog/2019/12/14/scoped-storage-stories-listfiles-woe.html
|
||||
*
|
||||
* This method uses a [suspendCancellableCoroutine] to wait for the result of a [ContentObserver]
|
||||
* registered on the cursor in case the cursor is still loading ([EXTRA_LOADING]).
|
||||
* If the cursor is not loading, it will be returned right away.
|
||||
*
|
||||
* @param timeout an optional time-out in milliseconds
|
||||
* @throws TimeoutCancellationException if there was no result before the time-out
|
||||
* @throws IOException if the query returns null
|
||||
*/
|
||||
@VisibleForTesting
|
||||
@Throws(IOException::class, TimeoutCancellationException::class)
|
||||
internal suspend fun getLoadedCursor(timeout: Long = 15_000, query: () -> Cursor?) =
|
||||
withTimeout(timeout) {
|
||||
suspendCancellableCoroutine<Cursor> { cont ->
|
||||
val cursor = query() ?: throw IOException()
|
||||
cont.invokeOnCancellation { closeQuietly(cursor) }
|
||||
val loading = cursor.extras.getBoolean(EXTRA_LOADING, false)
|
||||
if (loading) {
|
||||
Log.d(TAG, "Wait for children to get loaded...")
|
||||
cursor.registerContentObserver(object : ContentObserver(null) {
|
||||
override fun onChange(selfChange: Boolean, uri: Uri?) {
|
||||
Log.d(TAG, "Children loaded. Continue...")
|
||||
closeQuietly(cursor)
|
||||
val newCursor = query()
|
||||
if (newCursor == null) cont.cancel(IOException("query returned no results"))
|
||||
else cont.resume(newCursor)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// not loading, return cursor right away
|
||||
cont.resume(cursor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -296,7 +296,8 @@ internal class RestoreViewModel(
|
|||
}
|
||||
}
|
||||
}
|
||||
RestoreSetResult(restorableBackups)
|
||||
if (restorableBackups.isEmpty()) RestoreSetResult(app.getString(R.string.restore_set_empty_result))
|
||||
else RestoreSetResult(restorableBackups)
|
||||
}
|
||||
}
|
||||
continuation.resume(result)
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package com.stevesoltys.seedvault.settings
|
||||
|
||||
import android.app.backup.RestoreSet
|
||||
import android.content.Context
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.net.Uri
|
||||
import androidx.annotation.UiThread
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.stevesoltys.seedvault.transport.ConfigurableBackupTransport
|
||||
import java.util.concurrent.ConcurrentSkipListSet
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
internal const val PREF_KEY_BACKUP_APK = "backup_apk"
|
||||
|
@ -27,29 +30,48 @@ class SettingsManager(context: Context) {
|
|||
|
||||
private var isStorageChanging: AtomicBoolean = AtomicBoolean(false)
|
||||
|
||||
private val blacklistedApps: HashSet<String> by lazy {
|
||||
prefs.getStringSet(PREF_KEY_BACKUP_APP_BLACKLIST, emptySet()).toHashSet()
|
||||
/**
|
||||
* This gets accessed by non-UI threads when saving with [PreferenceManager]
|
||||
* and when [isBackupEnabled] is called during a backup run.
|
||||
* Therefore, it is implemented with a thread-safe [ConcurrentSkipListSet].
|
||||
*/
|
||||
private val blacklistedApps: MutableSet<String> by lazy {
|
||||
ConcurrentSkipListSet(prefs.getStringSet(PREF_KEY_BACKUP_APP_BLACKLIST, emptySet()))
|
||||
}
|
||||
|
||||
// FIXME Storage is currently plugin specific and not generic
|
||||
fun setStorage(storage: Storage) {
|
||||
prefs.edit()
|
||||
.putString(PREF_KEY_STORAGE_URI, storage.uri.toString())
|
||||
.putString(PREF_KEY_STORAGE_NAME, storage.name)
|
||||
.putBoolean(PREF_KEY_STORAGE_IS_USB, storage.isUsb)
|
||||
.apply()
|
||||
isStorageChanging.set(true)
|
||||
.putString(PREF_KEY_STORAGE_URI, storage.uri.toString())
|
||||
.putString(PREF_KEY_STORAGE_NAME, storage.name)
|
||||
.putBoolean(PREF_KEY_STORAGE_IS_USB, storage.isUsb)
|
||||
.apply()
|
||||
}
|
||||
|
||||
fun getStorage(): Storage? {
|
||||
val uriStr = prefs.getString(PREF_KEY_STORAGE_URI, null) ?: return null
|
||||
val uri = Uri.parse(uriStr)
|
||||
val name = prefs.getString(PREF_KEY_STORAGE_NAME, null) ?: throw IllegalStateException("no storage name")
|
||||
val name = prefs.getString(PREF_KEY_STORAGE_NAME, null)
|
||||
?: throw IllegalStateException("no storage name")
|
||||
val isUsb = prefs.getBoolean(PREF_KEY_STORAGE_IS_USB, false)
|
||||
return Storage(uri, name, isUsb)
|
||||
}
|
||||
|
||||
// TODO find a better solution for this hack abusing the settings manager
|
||||
/**
|
||||
* When [ConfigurableBackupTransport.initializeDevice] we try to avoid deleting all stored data,
|
||||
* as this gets frequently called after network errors by SAF cloud providers.
|
||||
*
|
||||
* This method allows us to force a re-initialization of the underlying storage root
|
||||
* when we change to a new storage provider.
|
||||
* Currently, this causes us to create a new [RestoreSet].
|
||||
*
|
||||
* As part of the initialization, [getAndResetIsStorageChanging] should get called
|
||||
* to prevent future calls from causing re-initializations.
|
||||
*/
|
||||
fun forceStorageInitialization() {
|
||||
isStorageChanging.set(true)
|
||||
}
|
||||
|
||||
fun getAndResetIsStorageChanging(): Boolean {
|
||||
return isStorageChanging.getAndSet(false)
|
||||
}
|
||||
|
@ -57,18 +79,18 @@ class SettingsManager(context: Context) {
|
|||
fun setFlashDrive(usb: FlashDrive?) {
|
||||
if (usb == null) {
|
||||
prefs.edit()
|
||||
.remove(PREF_KEY_FLASH_DRIVE_NAME)
|
||||
.remove(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER)
|
||||
.remove(PREF_KEY_FLASH_DRIVE_VENDOR_ID)
|
||||
.remove(PREF_KEY_FLASH_DRIVE_PRODUCT_ID)
|
||||
.apply()
|
||||
.remove(PREF_KEY_FLASH_DRIVE_NAME)
|
||||
.remove(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER)
|
||||
.remove(PREF_KEY_FLASH_DRIVE_VENDOR_ID)
|
||||
.remove(PREF_KEY_FLASH_DRIVE_PRODUCT_ID)
|
||||
.apply()
|
||||
} else {
|
||||
prefs.edit()
|
||||
.putString(PREF_KEY_FLASH_DRIVE_NAME, usb.name)
|
||||
.putString(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER, usb.serialNumber)
|
||||
.putInt(PREF_KEY_FLASH_DRIVE_VENDOR_ID, usb.vendorId)
|
||||
.putInt(PREF_KEY_FLASH_DRIVE_PRODUCT_ID, usb.productId)
|
||||
.apply()
|
||||
.putString(PREF_KEY_FLASH_DRIVE_NAME, usb.name)
|
||||
.putString(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER, usb.serialNumber)
|
||||
.putInt(PREF_KEY_FLASH_DRIVE_VENDOR_ID, usb.vendorId)
|
||||
.putInt(PREF_KEY_FLASH_DRIVE_PRODUCT_ID, usb.productId)
|
||||
.apply()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -96,24 +118,26 @@ class SettingsManager(context: Context) {
|
|||
}
|
||||
|
||||
data class Storage(
|
||||
val uri: Uri,
|
||||
val name: String,
|
||||
val isUsb: Boolean) {
|
||||
val uri: Uri,
|
||||
val name: String,
|
||||
val isUsb: Boolean
|
||||
) {
|
||||
fun getDocumentFile(context: Context) = DocumentFile.fromTreeUri(context, uri)
|
||||
?: throw AssertionError("Should only happen on API < 21.")
|
||||
?: throw AssertionError("Should only happen on API < 21.")
|
||||
}
|
||||
|
||||
data class FlashDrive(
|
||||
val name: String,
|
||||
val serialNumber: String?,
|
||||
val vendorId: Int,
|
||||
val productId: Int) {
|
||||
val name: String,
|
||||
val serialNumber: String?,
|
||||
val vendorId: Int,
|
||||
val productId: Int
|
||||
) {
|
||||
companion object {
|
||||
fun from(device: UsbDevice) = FlashDrive(
|
||||
name = "${device.manufacturerName} ${device.productName}",
|
||||
serialNumber = device.serialNumber,
|
||||
vendorId = device.vendorId,
|
||||
productId = device.productId
|
||||
name = "${device.manufacturerName} ${device.productName}",
|
||||
serialNumber = device.serialNumber,
|
||||
vendorId = device.vendorId,
|
||||
productId = device.productId
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,32 +21,35 @@ import com.stevesoltys.seedvault.metadata.PackageState.NOT_ALLOWED
|
|||
import com.stevesoltys.seedvault.metadata.PackageState.NO_DATA
|
||||
import com.stevesoltys.seedvault.metadata.PackageState.QUOTA_EXCEEDED
|
||||
import com.stevesoltys.seedvault.metadata.PackageState.UNKNOWN_ERROR
|
||||
import com.stevesoltys.seedvault.metadata.isSystemApp
|
||||
import com.stevesoltys.seedvault.restore.AppRestoreStatus.FAILED
|
||||
import com.stevesoltys.seedvault.restore.AppRestoreStatus.FAILED_NOT_ALLOWED
|
||||
import com.stevesoltys.seedvault.restore.AppRestoreStatus.FAILED_NO_DATA
|
||||
import com.stevesoltys.seedvault.restore.AppRestoreStatus.FAILED_QUOTA_EXCEEDED
|
||||
import com.stevesoltys.seedvault.restore.AppRestoreStatus.SUCCEEDED
|
||||
import com.stevesoltys.seedvault.transport.backup.isSystemApp
|
||||
import com.stevesoltys.seedvault.transport.requestBackup
|
||||
import com.stevesoltys.seedvault.ui.RequireProvisioningViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.*
|
||||
import java.util.Locale
|
||||
|
||||
private val TAG = SettingsViewModel::class.java.simpleName
|
||||
|
||||
class SettingsViewModel(
|
||||
app: Application,
|
||||
settingsManager: SettingsManager,
|
||||
keyManager: KeyManager,
|
||||
private val metadataManager: MetadataManager
|
||||
app: Application,
|
||||
settingsManager: SettingsManager,
|
||||
keyManager: KeyManager,
|
||||
private val metadataManager: MetadataManager
|
||||
) : RequireProvisioningViewModel(app, settingsManager, keyManager) {
|
||||
|
||||
override val isRestoreOperation = false
|
||||
|
||||
internal val lastBackupTime = metadataManager.lastBackupTime
|
||||
|
||||
private val mAppStatusList = switchMap(lastBackupTime) { getAppStatusResult() }
|
||||
private val mAppStatusList = switchMap(lastBackupTime) {
|
||||
// updates app list when lastBackupTime changes
|
||||
getAppStatusResult()
|
||||
}
|
||||
internal val appStatusList: LiveData<AppStatusResult> = mAppStatusList
|
||||
|
||||
private val mAppEditMode = MutableLiveData<Boolean>()
|
||||
|
@ -63,46 +66,46 @@ class SettingsViewModel(
|
|||
Thread { requestBackup(app) }.start()
|
||||
}
|
||||
|
||||
private fun getAppStatusResult(): LiveData<AppStatusResult> = liveData(Dispatchers.Main) {
|
||||
private fun getAppStatusResult(): LiveData<AppStatusResult> = liveData {
|
||||
val pm = app.packageManager
|
||||
val locale = Locale.getDefault()
|
||||
val list = pm.getInstalledPackages(0)
|
||||
.filter { !it.isSystemApp() }
|
||||
.map {
|
||||
val icon = if (it.packageName == MAGIC_PACKAGE_MANAGER) {
|
||||
.filter { !it.isSystemApp() }
|
||||
.map {
|
||||
val icon = if (it.packageName == MAGIC_PACKAGE_MANAGER) {
|
||||
getDrawable(app, R.drawable.ic_launcher_default)!!
|
||||
} else {
|
||||
try {
|
||||
pm.getApplicationIcon(it.packageName)
|
||||
} catch (e: NameNotFoundException) {
|
||||
getDrawable(app, R.drawable.ic_launcher_default)!!
|
||||
} else {
|
||||
try {
|
||||
pm.getApplicationIcon(it.packageName)
|
||||
} catch (e: NameNotFoundException) {
|
||||
getDrawable(app, R.drawable.ic_launcher_default)!!
|
||||
}
|
||||
}
|
||||
val metadata = metadataManager.getPackageMetadata(it.packageName)
|
||||
val time = metadata?.time ?: 0
|
||||
val status = when (metadata?.state) {
|
||||
null -> {
|
||||
Log.w(TAG, "No metadata available for: ${it.packageName}")
|
||||
FAILED
|
||||
}
|
||||
NO_DATA -> FAILED_NO_DATA
|
||||
NOT_ALLOWED -> FAILED_NOT_ALLOWED
|
||||
QUOTA_EXCEEDED -> FAILED_QUOTA_EXCEEDED
|
||||
UNKNOWN_ERROR -> FAILED
|
||||
APK_AND_DATA -> SUCCEEDED
|
||||
}
|
||||
val metadata = metadataManager.getPackageMetadata(it.packageName)
|
||||
val time = metadata?.time ?: 0
|
||||
val status = when (metadata?.state) {
|
||||
null -> {
|
||||
Log.w(TAG, "No metadata available for: ${it.packageName}")
|
||||
FAILED
|
||||
}
|
||||
if (metadata?.hasApk() == false) {
|
||||
Log.w(TAG, "No APK stored for: ${it.packageName}")
|
||||
}
|
||||
AppStatus(
|
||||
packageName = it.packageName,
|
||||
enabled = settingsManager.isBackupEnabled(it.packageName),
|
||||
icon = icon,
|
||||
name = getAppName(app, it.packageName).toString(),
|
||||
time = time,
|
||||
status = status
|
||||
)
|
||||
}.sortedBy { it.name.toLowerCase(locale) }
|
||||
NO_DATA -> FAILED_NO_DATA
|
||||
NOT_ALLOWED -> FAILED_NOT_ALLOWED
|
||||
QUOTA_EXCEEDED -> FAILED_QUOTA_EXCEEDED
|
||||
UNKNOWN_ERROR -> FAILED
|
||||
APK_AND_DATA -> SUCCEEDED
|
||||
}
|
||||
if (metadata?.hasApk() == false) {
|
||||
Log.w(TAG, "No APK stored for: ${it.packageName}")
|
||||
}
|
||||
AppStatus(
|
||||
packageName = it.packageName,
|
||||
enabled = settingsManager.isBackupEnabled(it.packageName),
|
||||
icon = icon,
|
||||
name = getAppName(app, it.packageName).toString(),
|
||||
time = time,
|
||||
status = status
|
||||
)
|
||||
}.sortedBy { it.name.toLowerCase(locale) }
|
||||
val oldList = mAppStatusList.value?.appStatusList ?: emptyList()
|
||||
val diff = calculateDiff(AppStatusDiff(oldList, list))
|
||||
emit(AppStatusResult(list, diff))
|
||||
|
|
|
@ -12,19 +12,22 @@ import android.util.Log
|
|||
import com.stevesoltys.seedvault.settings.SettingsActivity
|
||||
import com.stevesoltys.seedvault.transport.backup.BackupCoordinator
|
||||
import com.stevesoltys.seedvault.transport.restore.RestoreCoordinator
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.koin.core.KoinComponent
|
||||
import org.koin.core.inject
|
||||
|
||||
val TRANSPORT_ID: String = ConfigurableBackupTransport::class.java.name
|
||||
|
||||
private const val TRANSPORT_DIRECTORY_NAME = "com.stevesoltys.seedvault.transport.ConfigurableBackupTransport"
|
||||
private const val TRANSPORT_DIRECTORY_NAME =
|
||||
"com.stevesoltys.seedvault.transport.ConfigurableBackupTransport"
|
||||
private val TAG = ConfigurableBackupTransport::class.java.simpleName
|
||||
|
||||
/**
|
||||
* @author Steve Soltys
|
||||
* @author Torsten Grote
|
||||
*/
|
||||
class ConfigurableBackupTransport internal constructor(private val context: Context) : BackupTransport(), KoinComponent {
|
||||
class ConfigurableBackupTransport internal constructor(private val context: Context) :
|
||||
BackupTransport(), KoinComponent {
|
||||
|
||||
private val backupCoordinator by inject<BackupCoordinator>()
|
||||
private val restoreCoordinator by inject<RestoreCoordinator>()
|
||||
|
@ -57,24 +60,27 @@ class ConfigurableBackupTransport internal constructor(private val context: Cont
|
|||
// General backup methods
|
||||
//
|
||||
|
||||
override fun initializeDevice(): Int {
|
||||
return backupCoordinator.initializeDevice()
|
||||
override fun initializeDevice(): Int = runBlocking {
|
||||
backupCoordinator.initializeDevice()
|
||||
}
|
||||
|
||||
override fun isAppEligibleForBackup(targetPackage: PackageInfo, isFullBackup: Boolean): Boolean {
|
||||
override fun isAppEligibleForBackup(
|
||||
targetPackage: PackageInfo,
|
||||
isFullBackup: Boolean
|
||||
): Boolean {
|
||||
return backupCoordinator.isAppEligibleForBackup(targetPackage, isFullBackup)
|
||||
}
|
||||
|
||||
override fun getBackupQuota(packageName: String, isFullBackup: Boolean): Long {
|
||||
return backupCoordinator.getBackupQuota(packageName, isFullBackup)
|
||||
override fun getBackupQuota(packageName: String, isFullBackup: Boolean): Long = runBlocking {
|
||||
backupCoordinator.getBackupQuota(packageName, isFullBackup)
|
||||
}
|
||||
|
||||
override fun clearBackupData(packageInfo: PackageInfo): Int {
|
||||
return backupCoordinator.clearBackupData(packageInfo)
|
||||
override fun clearBackupData(packageInfo: PackageInfo): Int = runBlocking {
|
||||
backupCoordinator.clearBackupData(packageInfo)
|
||||
}
|
||||
|
||||
override fun finishBackup(): Int {
|
||||
return backupCoordinator.finishBackup()
|
||||
override fun finishBackup(): Int = runBlocking {
|
||||
backupCoordinator.finishBackup()
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------
|
||||
|
@ -85,11 +91,18 @@ class ConfigurableBackupTransport internal constructor(private val context: Cont
|
|||
return backupCoordinator.requestBackupTime()
|
||||
}
|
||||
|
||||
override fun performBackup(packageInfo: PackageInfo, inFd: ParcelFileDescriptor, flags: Int): Int {
|
||||
return backupCoordinator.performIncrementalBackup(packageInfo, inFd, flags)
|
||||
override fun performBackup(
|
||||
packageInfo: PackageInfo,
|
||||
inFd: ParcelFileDescriptor,
|
||||
flags: Int
|
||||
): Int = runBlocking {
|
||||
backupCoordinator.performIncrementalBackup(packageInfo, inFd, flags)
|
||||
}
|
||||
|
||||
override fun performBackup(targetPackage: PackageInfo, fileDescriptor: ParcelFileDescriptor): Int {
|
||||
override fun performBackup(
|
||||
targetPackage: PackageInfo,
|
||||
fileDescriptor: ParcelFileDescriptor
|
||||
): Int {
|
||||
Log.w(TAG, "Warning: Legacy performBackup() method called.")
|
||||
return performBackup(targetPackage, fileDescriptor, 0)
|
||||
}
|
||||
|
@ -106,20 +119,27 @@ class ConfigurableBackupTransport internal constructor(private val context: Cont
|
|||
return backupCoordinator.checkFullBackupSize(size)
|
||||
}
|
||||
|
||||
override fun performFullBackup(targetPackage: PackageInfo, socket: ParcelFileDescriptor, flags: Int): Int {
|
||||
return backupCoordinator.performFullBackup(targetPackage, socket, flags)
|
||||
override fun performFullBackup(
|
||||
targetPackage: PackageInfo,
|
||||
socket: ParcelFileDescriptor,
|
||||
flags: Int
|
||||
): Int = runBlocking {
|
||||
backupCoordinator.performFullBackup(targetPackage, socket, flags)
|
||||
}
|
||||
|
||||
override fun performFullBackup(targetPackage: PackageInfo, fileDescriptor: ParcelFileDescriptor): Int {
|
||||
override fun performFullBackup(
|
||||
targetPackage: PackageInfo,
|
||||
fileDescriptor: ParcelFileDescriptor
|
||||
): Int = runBlocking {
|
||||
Log.w(TAG, "Warning: Legacy performFullBackup() method called.")
|
||||
return backupCoordinator.performFullBackup(targetPackage, fileDescriptor, 0)
|
||||
backupCoordinator.performFullBackup(targetPackage, fileDescriptor, 0)
|
||||
}
|
||||
|
||||
override fun sendBackupData(numBytes: Int): Int {
|
||||
return backupCoordinator.sendBackupData(numBytes)
|
||||
override fun sendBackupData(numBytes: Int): Int = runBlocking {
|
||||
backupCoordinator.sendBackupData(numBytes)
|
||||
}
|
||||
|
||||
override fun cancelFullBackup() {
|
||||
override fun cancelFullBackup() = runBlocking {
|
||||
backupCoordinator.cancelFullBackup()
|
||||
}
|
||||
|
||||
|
@ -127,8 +147,8 @@ class ConfigurableBackupTransport internal constructor(private val context: Cont
|
|||
// Restore
|
||||
//
|
||||
|
||||
override fun getAvailableRestoreSets(): Array<RestoreSet>? {
|
||||
return restoreCoordinator.getAvailableRestoreSets()
|
||||
override fun getAvailableRestoreSets(): Array<RestoreSet>? = runBlocking {
|
||||
restoreCoordinator.getAvailableRestoreSets()
|
||||
}
|
||||
|
||||
override fun getCurrentRestoreSet(): Long {
|
||||
|
@ -139,16 +159,16 @@ class ConfigurableBackupTransport internal constructor(private val context: Cont
|
|||
return restoreCoordinator.startRestore(token, packages)
|
||||
}
|
||||
|
||||
override fun getNextFullRestoreDataChunk(socket: ParcelFileDescriptor): Int {
|
||||
return restoreCoordinator.getNextFullRestoreDataChunk(socket)
|
||||
override fun getNextFullRestoreDataChunk(socket: ParcelFileDescriptor): Int = runBlocking {
|
||||
restoreCoordinator.getNextFullRestoreDataChunk(socket)
|
||||
}
|
||||
|
||||
override fun nextRestorePackage(): RestoreDescription? {
|
||||
return restoreCoordinator.nextRestorePackage()
|
||||
override fun nextRestorePackage(): RestoreDescription? = runBlocking {
|
||||
restoreCoordinator.nextRestorePackage()
|
||||
}
|
||||
|
||||
override fun getRestoreData(outputFileDescriptor: ParcelFileDescriptor): Int {
|
||||
return restoreCoordinator.getRestoreData(outputFileDescriptor)
|
||||
override fun getRestoreData(outputFileDescriptor: ParcelFileDescriptor): Int = runBlocking {
|
||||
restoreCoordinator.getRestoreData(outputFileDescriptor)
|
||||
}
|
||||
|
||||
override fun abortFullRestore(): Int {
|
||||
|
|
|
@ -11,8 +11,6 @@ import com.stevesoltys.seedvault.encodeBase64
|
|||
import com.stevesoltys.seedvault.metadata.MetadataManager
|
||||
import com.stevesoltys.seedvault.metadata.PackageMetadata
|
||||
import com.stevesoltys.seedvault.metadata.PackageState
|
||||
import com.stevesoltys.seedvault.metadata.isSystemApp
|
||||
import com.stevesoltys.seedvault.metadata.isUpdatedSystemApp
|
||||
import com.stevesoltys.seedvault.settings.SettingsManager
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
|
@ -23,9 +21,10 @@ import java.security.MessageDigest
|
|||
private val TAG = ApkBackup::class.java.simpleName
|
||||
|
||||
class ApkBackup(
|
||||
private val pm: PackageManager,
|
||||
private val settingsManager: SettingsManager,
|
||||
private val metadataManager: MetadataManager) {
|
||||
private val pm: PackageManager,
|
||||
private val settingsManager: SettingsManager,
|
||||
private val metadataManager: MetadataManager
|
||||
) {
|
||||
|
||||
/**
|
||||
* Checks if a new APK needs to get backed up,
|
||||
|
@ -36,7 +35,11 @@ class ApkBackup(
|
|||
* @return new [PackageMetadata] if an APK backup was made or null if no backup was made.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun backupApkIfNecessary(packageInfo: PackageInfo, packageState: PackageState, streamGetter: () -> OutputStream): PackageMetadata? {
|
||||
suspend fun backupApkIfNecessary(
|
||||
packageInfo: PackageInfo,
|
||||
packageState: PackageState,
|
||||
streamGetter: suspend () -> OutputStream
|
||||
): PackageMetadata? {
|
||||
// do not back up @pm@
|
||||
val packageName = packageInfo.packageName
|
||||
if (packageName == MAGIC_PACKAGE_MANAGER) return null
|
||||
|
@ -45,7 +48,7 @@ class ApkBackup(
|
|||
if (!settingsManager.backupApks()) return null
|
||||
|
||||
// do not back up system apps that haven't been updated
|
||||
if (packageInfo.isSystemApp() && !packageInfo.isUpdatedSystemApp()) {
|
||||
if (packageInfo.isNotUpdatedSystemApp()) {
|
||||
Log.d(TAG, "Package $packageName is vanilla system app. Not backing it up.")
|
||||
return null
|
||||
}
|
||||
|
@ -65,15 +68,19 @@ class ApkBackup(
|
|||
|
||||
// get cached metadata about package
|
||||
val packageMetadata = metadataManager.getPackageMetadata(packageName)
|
||||
?: PackageMetadata()
|
||||
?: PackageMetadata()
|
||||
|
||||
// get version codes
|
||||
val version = packageInfo.longVersionCode
|
||||
val backedUpVersion = packageMetadata.version ?: 0L // no version will cause backup
|
||||
val backedUpVersion = packageMetadata.version ?: 0L // no version will cause backup
|
||||
|
||||
// do not backup if we have the version already and signatures did not change
|
||||
if (version <= backedUpVersion && !signaturesChanged(packageMetadata, signatures)) {
|
||||
Log.d(TAG, "Package $packageName with version $version already has a backup ($backedUpVersion) with the same signature. Not backing it up.")
|
||||
Log.d(
|
||||
TAG,
|
||||
"Package $packageName with version $version already has a backup ($backedUpVersion)" +
|
||||
" with the same signature. Not backing it up."
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
|
@ -91,7 +98,7 @@ class ApkBackup(
|
|||
|
||||
// copy the APK to the storage's output and calculate SHA-256 hash while at it
|
||||
val messageDigest = MessageDigest.getInstance("SHA-256")
|
||||
streamGetter.invoke().use { outputStream ->
|
||||
streamGetter().use { outputStream ->
|
||||
inputStream.use { inputStream ->
|
||||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
||||
var bytes = inputStream.read(buffer)
|
||||
|
@ -107,15 +114,18 @@ class ApkBackup(
|
|||
|
||||
// return updated metadata
|
||||
return PackageMetadata(
|
||||
state = packageState,
|
||||
version = version,
|
||||
installer = pm.getInstallerPackageName(packageName),
|
||||
sha256 = sha256,
|
||||
signatures = signatures
|
||||
state = packageState,
|
||||
version = version,
|
||||
installer = pm.getInstallerPackageName(packageName),
|
||||
sha256 = sha256,
|
||||
signatures = signatures
|
||||
)
|
||||
}
|
||||
|
||||
private fun signaturesChanged(packageMetadata: PackageMetadata, signatures: List<String>): Boolean {
|
||||
private fun signaturesChanged(
|
||||
packageMetadata: PackageMetadata,
|
||||
signatures: List<String>
|
||||
): Boolean {
|
||||
// no signatures in package metadata counts as them not having changed
|
||||
if (packageMetadata.signatures == null) return false
|
||||
// TODO to support multiple signers check if lists differ
|
||||
|
|
|
@ -9,6 +9,7 @@ import android.content.pm.PackageInfo
|
|||
import android.content.pm.PackageManager.GET_SIGNING_CERTIFICATES
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.util.Log
|
||||
import androidx.annotation.WorkerThread
|
||||
import com.stevesoltys.seedvault.BackupNotificationManager
|
||||
import com.stevesoltys.seedvault.Clock
|
||||
import com.stevesoltys.seedvault.MAGIC_PACKAGE_MANAGER
|
||||
|
@ -18,7 +19,6 @@ import com.stevesoltys.seedvault.metadata.PackageState.NOT_ALLOWED
|
|||
import com.stevesoltys.seedvault.metadata.PackageState.NO_DATA
|
||||
import com.stevesoltys.seedvault.metadata.PackageState.QUOTA_EXCEEDED
|
||||
import com.stevesoltys.seedvault.metadata.PackageState.UNKNOWN_ERROR
|
||||
import com.stevesoltys.seedvault.metadata.isSystemApp
|
||||
import com.stevesoltys.seedvault.settings.SettingsManager
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.TimeUnit.DAYS
|
||||
|
@ -29,17 +29,20 @@ private val TAG = BackupCoordinator::class.java.simpleName
|
|||
* @author Steve Soltys
|
||||
* @author Torsten Grote
|
||||
*/
|
||||
@WorkerThread // entire class should always be accessed from a worker thread, so blocking is ok
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class BackupCoordinator(
|
||||
private val context: Context,
|
||||
private val plugin: BackupPlugin,
|
||||
private val kv: KVBackup,
|
||||
private val full: FullBackup,
|
||||
private val apkBackup: ApkBackup,
|
||||
private val clock: Clock,
|
||||
private val packageService: PackageService,
|
||||
private val metadataManager: MetadataManager,
|
||||
private val settingsManager: SettingsManager,
|
||||
private val nm: BackupNotificationManager) {
|
||||
private val context: Context,
|
||||
private val plugin: BackupPlugin,
|
||||
private val kv: KVBackup,
|
||||
private val full: FullBackup,
|
||||
private val apkBackup: ApkBackup,
|
||||
private val clock: Clock,
|
||||
private val packageService: PackageService,
|
||||
private val metadataManager: MetadataManager,
|
||||
private val settingsManager: SettingsManager,
|
||||
private val nm: BackupNotificationManager
|
||||
) {
|
||||
|
||||
private var calledInitialize = false
|
||||
private var calledClearBackupData = false
|
||||
|
@ -67,13 +70,15 @@ internal class BackupCoordinator(
|
|||
* @return One of [TRANSPORT_OK] (OK so far) or
|
||||
* [TRANSPORT_ERROR] (to retry following network error or other failure).
|
||||
*/
|
||||
fun initializeDevice(): Int {
|
||||
suspend fun initializeDevice(): Int {
|
||||
Log.i(TAG, "Initialize Device!")
|
||||
return try {
|
||||
val token = clock.time()
|
||||
if (plugin.initializeDevice(token)) {
|
||||
Log.d(TAG, "Resetting backup metadata...")
|
||||
metadataManager.onDeviceInitialization(token, plugin.getMetadataOutputStream())
|
||||
plugin.getMetadataOutputStream().use {
|
||||
metadataManager.onDeviceInitialization(token, it)
|
||||
}
|
||||
} else {
|
||||
Log.d(TAG, "Storage was already initialized, doing no-op")
|
||||
}
|
||||
|
@ -89,7 +94,10 @@ internal class BackupCoordinator(
|
|||
}
|
||||
}
|
||||
|
||||
fun isAppEligibleForBackup(targetPackage: PackageInfo, @Suppress("UNUSED_PARAMETER") isFullBackup: Boolean): Boolean {
|
||||
fun isAppEligibleForBackup(
|
||||
targetPackage: PackageInfo,
|
||||
@Suppress("UNUSED_PARAMETER") isFullBackup: Boolean
|
||||
): Boolean {
|
||||
val packageName = targetPackage.packageName
|
||||
// Check that the app is not blacklisted by the user
|
||||
val enabled = settingsManager.isBackupEnabled(packageName)
|
||||
|
@ -107,7 +115,7 @@ internal class BackupCoordinator(
|
|||
* otherwise for key-value backup.
|
||||
* @return Current limit on backup size in bytes.
|
||||
*/
|
||||
fun getBackupQuota(packageName: String, isFullBackup: Boolean): Long {
|
||||
suspend fun getBackupQuota(packageName: String, isFullBackup: Boolean): Long {
|
||||
if (packageName != MAGIC_PACKAGE_MANAGER) {
|
||||
// try to back up APK here as later methods are sometimes not called called
|
||||
backUpApk(context.packageManager.getPackageInfo(packageName, GET_SIGNING_CERTIFICATES))
|
||||
|
@ -139,7 +147,11 @@ internal class BackupCoordinator(
|
|||
Log.i(TAG, "Request incremental backup time. Returned $this")
|
||||
}
|
||||
|
||||
fun performIncrementalBackup(packageInfo: PackageInfo, data: ParcelFileDescriptor, flags: Int): Int {
|
||||
suspend fun performIncrementalBackup(
|
||||
packageInfo: PackageInfo,
|
||||
data: ParcelFileDescriptor,
|
||||
flags: Int
|
||||
): Int {
|
||||
cancelReason = UNKNOWN_ERROR
|
||||
val packageName = packageInfo.packageName
|
||||
if (packageName == MAGIC_PACKAGE_MANAGER) {
|
||||
|
@ -182,12 +194,16 @@ internal class BackupCoordinator(
|
|||
return result
|
||||
}
|
||||
|
||||
fun performFullBackup(targetPackage: PackageInfo, fileDescriptor: ParcelFileDescriptor, flags: Int): Int {
|
||||
suspend fun performFullBackup(
|
||||
targetPackage: PackageInfo,
|
||||
fileDescriptor: ParcelFileDescriptor,
|
||||
flags: Int
|
||||
): Int {
|
||||
cancelReason = UNKNOWN_ERROR
|
||||
return full.performFullBackup(targetPackage, fileDescriptor, flags)
|
||||
}
|
||||
|
||||
fun sendBackupData(numBytes: Int) = full.sendBackupData(numBytes)
|
||||
suspend fun sendBackupData(numBytes: Int) = full.sendBackupData(numBytes)
|
||||
|
||||
/**
|
||||
* Tells the transport to cancel the currently-ongoing full backup operation.
|
||||
|
@ -202,9 +218,9 @@ internal class BackupCoordinator(
|
|||
* If the transport receives this callback, it will *not* receive a call to [finishBackup].
|
||||
* It needs to tear down any ongoing backup state here.
|
||||
*/
|
||||
fun cancelFullBackup() {
|
||||
suspend fun cancelFullBackup() {
|
||||
val packageInfo = full.getCurrentPackage()
|
||||
?: throw AssertionError("Cancelling full backup, but no current package")
|
||||
?: throw AssertionError("Cancelling full backup, but no current package")
|
||||
Log.i(TAG, "Cancel full backup of ${packageInfo.packageName} because of $cancelReason")
|
||||
onPackageBackupError(packageInfo)
|
||||
full.cancelFullBackup()
|
||||
|
@ -221,7 +237,7 @@ internal class BackupCoordinator(
|
|||
*
|
||||
* @return the same error codes as [performFullBackup].
|
||||
*/
|
||||
fun clearBackupData(packageInfo: PackageInfo): Int {
|
||||
suspend fun clearBackupData(packageInfo: PackageInfo): Int {
|
||||
val packageName = packageInfo.packageName
|
||||
Log.i(TAG, "Clear Backup Data of $packageName.")
|
||||
try {
|
||||
|
@ -248,15 +264,15 @@ internal class BackupCoordinator(
|
|||
*
|
||||
* @return the same error codes as [performIncrementalBackup] or [performFullBackup].
|
||||
*/
|
||||
fun finishBackup(): Int = when {
|
||||
suspend fun finishBackup(): Int = when {
|
||||
kv.hasState() -> {
|
||||
check(!full.hasState()) { "K/V backup has state, but full backup has dangling state as well" }
|
||||
onPackageBackedUp(kv.getCurrentPackage()!!) // not-null because we have state
|
||||
onPackageBackedUp(kv.getCurrentPackage()!!) // not-null because we have state
|
||||
kv.finishBackup()
|
||||
}
|
||||
full.hasState() -> {
|
||||
check(!kv.hasState()) { "Full backup has state, but K/V backup has dangling state as well" }
|
||||
onPackageBackedUp(full.getCurrentPackage()!!) // not-null because we have state
|
||||
onPackageBackedUp(full.getCurrentPackage()!!) // not-null because we have state
|
||||
full.finishBackup()
|
||||
}
|
||||
calledInitialize || calledClearBackupData -> {
|
||||
|
@ -267,7 +283,7 @@ internal class BackupCoordinator(
|
|||
else -> throw IllegalStateException("Unexpected state in finishBackup()")
|
||||
}
|
||||
|
||||
private fun backUpNotAllowedPackages() {
|
||||
private suspend fun backUpNotAllowedPackages() {
|
||||
Log.d(TAG, "Checking if APKs of opt-out apps need backup...")
|
||||
packageService.notAllowedPackages.forEach { optOutPackageInfo ->
|
||||
try {
|
||||
|
@ -278,37 +294,43 @@ internal class BackupCoordinator(
|
|||
}
|
||||
}
|
||||
|
||||
private fun backUpApk(packageInfo: PackageInfo, packageState: PackageState = UNKNOWN_ERROR) {
|
||||
private suspend fun backUpApk(
|
||||
packageInfo: PackageInfo,
|
||||
packageState: PackageState = UNKNOWN_ERROR
|
||||
) {
|
||||
val packageName = packageInfo.packageName
|
||||
try {
|
||||
apkBackup.backupApkIfNecessary(packageInfo, packageState) {
|
||||
plugin.getApkOutputStream(packageInfo)
|
||||
}?.let { packageMetadata ->
|
||||
val outputStream = plugin.getMetadataOutputStream()
|
||||
metadataManager.onApkBackedUp(packageInfo, packageMetadata, outputStream)
|
||||
plugin.getMetadataOutputStream().use {
|
||||
metadataManager.onApkBackedUp(packageInfo, packageMetadata, it)
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error while writing APK or metadata for $packageName", e)
|
||||
}
|
||||
}
|
||||
|
||||
private fun onPackageBackedUp(packageInfo: PackageInfo) {
|
||||
private suspend fun onPackageBackedUp(packageInfo: PackageInfo) {
|
||||
val packageName = packageInfo.packageName
|
||||
try {
|
||||
val outputStream = plugin.getMetadataOutputStream()
|
||||
metadataManager.onPackageBackedUp(packageInfo, outputStream)
|
||||
plugin.getMetadataOutputStream().use {
|
||||
metadataManager.onPackageBackedUp(packageInfo, it)
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error while writing metadata for $packageName", e)
|
||||
}
|
||||
}
|
||||
|
||||
private fun onPackageBackupError(packageInfo: PackageInfo) {
|
||||
private suspend fun onPackageBackupError(packageInfo: PackageInfo) {
|
||||
// don't bother with system apps that have no data
|
||||
if (cancelReason == NO_DATA && packageInfo.isSystemApp()) return
|
||||
val packageName = packageInfo.packageName
|
||||
try {
|
||||
val outputStream = plugin.getMetadataOutputStream()
|
||||
metadataManager.onPackageBackupError(packageInfo, cancelReason, outputStream)
|
||||
plugin.getMetadataOutputStream().use {
|
||||
metadataManager.onPackageBackupError(packageInfo, cancelReason, it)
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error while writing metadata for $packageName", e)
|
||||
}
|
||||
|
|
|
@ -17,19 +17,19 @@ interface BackupPlugin {
|
|||
* false if the device was initialized already and initialization should be a no-op.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun initializeDevice(newToken: Long): Boolean
|
||||
suspend fun initializeDevice(newToken: Long): Boolean
|
||||
|
||||
/**
|
||||
* Returns an [OutputStream] for writing backup metadata.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun getMetadataOutputStream(): OutputStream
|
||||
suspend fun getMetadataOutputStream(): OutputStream
|
||||
|
||||
/**
|
||||
* Returns an [OutputStream] for writing an APK to be backed up.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun getApkOutputStream(packageInfo: PackageInfo): OutputStream
|
||||
suspend fun getApkOutputStream(packageInfo: PackageInfo): OutputStream
|
||||
|
||||
/**
|
||||
* Returns the package name of the app that provides the backend storage
|
||||
|
|
|
@ -18,10 +18,11 @@ import java.io.InputStream
|
|||
import java.io.OutputStream
|
||||
|
||||
private class FullBackupState(
|
||||
internal val packageInfo: PackageInfo,
|
||||
internal val inputFileDescriptor: ParcelFileDescriptor,
|
||||
internal val inputStream: InputStream,
|
||||
internal var outputStreamInit: (() -> OutputStream)?) {
|
||||
internal val packageInfo: PackageInfo,
|
||||
internal val inputFileDescriptor: ParcelFileDescriptor,
|
||||
internal val inputStream: InputStream,
|
||||
internal var outputStreamInit: (suspend () -> OutputStream)?
|
||||
) {
|
||||
internal var outputStream: OutputStream? = null
|
||||
internal val packageName: String = packageInfo.packageName
|
||||
internal var size: Long = 0
|
||||
|
@ -31,11 +32,13 @@ const val DEFAULT_QUOTA_FULL_BACKUP = (2 * (25 * 1024 * 1024)).toLong()
|
|||
|
||||
private val TAG = FullBackup::class.java.simpleName
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class FullBackup(
|
||||
private val plugin: FullBackupPlugin,
|
||||
private val inputFactory: InputFactory,
|
||||
private val headerWriter: HeaderWriter,
|
||||
private val crypto: Crypto) {
|
||||
private val plugin: FullBackupPlugin,
|
||||
private val inputFactory: InputFactory,
|
||||
private val headerWriter: HeaderWriter,
|
||||
private val crypto: Crypto
|
||||
) {
|
||||
|
||||
private var state: FullBackupState? = null
|
||||
|
||||
|
@ -89,7 +92,11 @@ internal class FullBackup(
|
|||
* [TRANSPORT_OK] to indicate that the OS may proceed with delivering backup data;
|
||||
* [TRANSPORT_ERROR] to indicate an error that precludes performing a backup at this time.
|
||||
*/
|
||||
fun performFullBackup(targetPackage: PackageInfo, socket: ParcelFileDescriptor, @Suppress("UNUSED_PARAMETER") flags: Int = 0): Int {
|
||||
suspend fun performFullBackup(
|
||||
targetPackage: PackageInfo,
|
||||
socket: ParcelFileDescriptor,
|
||||
@Suppress("UNUSED_PARAMETER") flags: Int = 0
|
||||
): Int {
|
||||
if (state != null) throw AssertionError()
|
||||
Log.i(TAG, "Perform full backup for ${targetPackage.packageName}.")
|
||||
|
||||
|
@ -101,7 +108,9 @@ internal class FullBackup(
|
|||
val outputStream = try {
|
||||
plugin.getOutputStream(targetPackage)
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error getting OutputStream for full backup of ${targetPackage.packageName}", e)
|
||||
"Error getting OutputStream for full backup of ${targetPackage.packageName}".let {
|
||||
Log.e(TAG, it, e)
|
||||
}
|
||||
throw(e)
|
||||
}
|
||||
// store version header
|
||||
|
@ -115,31 +124,36 @@ internal class FullBackup(
|
|||
throw(e)
|
||||
}
|
||||
outputStream
|
||||
} // this lambda is only called before we actually write backup data the first time
|
||||
} // this lambda is only called before we actually write backup data the first time
|
||||
return TRANSPORT_OK
|
||||
}
|
||||
|
||||
fun sendBackupData(numBytes: Int): Int {
|
||||
suspend fun sendBackupData(numBytes: Int): Int {
|
||||
val state = this.state
|
||||
?: throw AssertionError("Attempted sendBackupData before performFullBackup")
|
||||
?: throw AssertionError("Attempted sendBackupData before performFullBackup")
|
||||
|
||||
// check if size fits quota
|
||||
state.size += numBytes
|
||||
val quota = plugin.getQuota()
|
||||
if (state.size > quota) {
|
||||
Log.w(TAG, "Full backup of additional $numBytes exceeds quota of $quota with ${state.size}.")
|
||||
Log.w(
|
||||
TAG,
|
||||
"Full backup of additional $numBytes exceeds quota of $quota with ${state.size}."
|
||||
)
|
||||
return TRANSPORT_QUOTA_EXCEEDED
|
||||
}
|
||||
|
||||
return try {
|
||||
// get output stream or initialize it, if it does not yet exist
|
||||
check((state.outputStream != null) xor (state.outputStreamInit != null)) { "No OutputStream xor no StreamGetter" }
|
||||
val outputStream = state.outputStream ?: {
|
||||
val stream = state.outputStreamInit!!.invoke() // not-null due to check above
|
||||
check((state.outputStream != null) xor (state.outputStreamInit != null)) {
|
||||
"No OutputStream xor no StreamGetter"
|
||||
}
|
||||
val outputStream = state.outputStream ?: suspend {
|
||||
val stream = state.outputStreamInit!!() // not-null due to check above
|
||||
state.outputStream = stream
|
||||
stream
|
||||
}.invoke()
|
||||
state.outputStreamInit = null // the stream init lambda is not needed beyond that point
|
||||
}()
|
||||
state.outputStreamInit = null // the stream init lambda is not needed beyond that point
|
||||
|
||||
// read backup data, encrypt it and write it to output stream
|
||||
val payload = IOUtils.readFully(state.inputStream, numBytes)
|
||||
|
@ -152,11 +166,11 @@ internal class FullBackup(
|
|||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun clearBackupData(packageInfo: PackageInfo) {
|
||||
suspend fun clearBackupData(packageInfo: PackageInfo) {
|
||||
plugin.removeDataOfPackage(packageInfo)
|
||||
}
|
||||
|
||||
fun cancelFullBackup() {
|
||||
suspend fun cancelFullBackup() {
|
||||
Log.i(TAG, "Cancel full backup")
|
||||
val state = this.state ?: throw AssertionError("No state when canceling")
|
||||
try {
|
||||
|
|
|
@ -10,12 +10,12 @@ interface FullBackupPlugin {
|
|||
|
||||
// TODO consider using a salted hash for the package name to not leak it to the storage server
|
||||
@Throws(IOException::class)
|
||||
fun getOutputStream(targetPackage: PackageInfo): OutputStream
|
||||
suspend fun getOutputStream(targetPackage: PackageInfo): OutputStream
|
||||
|
||||
/**
|
||||
* Remove all data associated with the given package.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun removeDataOfPackage(packageInfo: PackageInfo)
|
||||
suspend fun removeDataOfPackage(packageInfo: PackageInfo)
|
||||
|
||||
}
|
||||
|
|
|
@ -21,11 +21,13 @@ const val DEFAULT_QUOTA_KEY_VALUE_BACKUP = (2 * (5 * 1024 * 1024)).toLong()
|
|||
|
||||
private val TAG = KVBackup::class.java.simpleName
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class KVBackup(
|
||||
private val plugin: KVBackupPlugin,
|
||||
private val inputFactory: InputFactory,
|
||||
private val headerWriter: HeaderWriter,
|
||||
private val crypto: Crypto) {
|
||||
private val plugin: KVBackupPlugin,
|
||||
private val inputFactory: InputFactory,
|
||||
private val headerWriter: HeaderWriter,
|
||||
private val crypto: Crypto
|
||||
) {
|
||||
|
||||
private var state: KVBackupState? = null
|
||||
|
||||
|
@ -35,7 +37,11 @@ internal class KVBackup(
|
|||
|
||||
fun getQuota(): Long = plugin.getQuota()
|
||||
|
||||
fun performBackup(packageInfo: PackageInfo, data: ParcelFileDescriptor, flags: Int): Int {
|
||||
suspend fun performBackup(
|
||||
packageInfo: PackageInfo,
|
||||
data: ParcelFileDescriptor,
|
||||
flags: Int
|
||||
): Int {
|
||||
val isIncremental = flags and FLAG_INCREMENTAL != 0
|
||||
val isNonIncremental = flags and FLAG_NON_INCREMENTAL != 0
|
||||
val packageName = packageInfo.packageName
|
||||
|
@ -64,7 +70,10 @@ internal class KVBackup(
|
|||
return backupError(TRANSPORT_ERROR)
|
||||
}
|
||||
if (isIncremental && !hasDataForPackage) {
|
||||
Log.w(TAG, "Requested incremental, but transport currently stores no data $packageName, requesting non-incremental retry.")
|
||||
Log.w(
|
||||
TAG, "Requested incremental, but transport currently stores no data" +
|
||||
" for $packageName, requesting non-incremental retry."
|
||||
)
|
||||
return backupError(TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED)
|
||||
}
|
||||
|
||||
|
@ -91,7 +100,7 @@ internal class KVBackup(
|
|||
return storeRecords(packageInfo, data)
|
||||
}
|
||||
|
||||
private fun storeRecords(packageInfo: PackageInfo, data: ParcelFileDescriptor): Int {
|
||||
private suspend fun storeRecords(packageInfo: PackageInfo, data: ParcelFileDescriptor): Int {
|
||||
// apply the delta operations
|
||||
for (result in parseBackupStream(data)) {
|
||||
if (result is Result.Error) {
|
||||
|
@ -105,15 +114,26 @@ internal class KVBackup(
|
|||
plugin.deleteRecord(packageInfo, op.base64Key)
|
||||
} else {
|
||||
val outputStream = plugin.getOutputStreamForRecord(packageInfo, op.base64Key)
|
||||
val header = VersionHeader(packageName = packageInfo.packageName, key = op.key)
|
||||
headerWriter.writeVersion(outputStream, header)
|
||||
crypto.encryptHeader(outputStream, header)
|
||||
crypto.encryptMultipleSegments(outputStream, op.value)
|
||||
outputStream.flush()
|
||||
closeQuietly(outputStream)
|
||||
try {
|
||||
val header = VersionHeader(
|
||||
packageName = packageInfo.packageName,
|
||||
key = op.key
|
||||
)
|
||||
headerWriter.writeVersion(outputStream, header)
|
||||
crypto.encryptHeader(outputStream, header)
|
||||
crypto.encryptMultipleSegments(outputStream, op.value)
|
||||
outputStream.flush()
|
||||
} finally {
|
||||
closeQuietly(outputStream)
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Unable to update base64Key file for base64Key ${op.base64Key}", e)
|
||||
// Returning something more forgiving such as TRANSPORT_PACKAGE_REJECTED
|
||||
// will still make the entire backup fail.
|
||||
// TODO However, TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED might buy us a retry,
|
||||
// we would just need to be careful not to create an infinite loop
|
||||
// for permanent errors.
|
||||
return backupError(TRANSPORT_ERROR)
|
||||
}
|
||||
}
|
||||
|
@ -132,7 +152,7 @@ internal class KVBackup(
|
|||
return generateSequence {
|
||||
// read the next header or end the sequence in case of error or no more headers
|
||||
try {
|
||||
if (!changeSet.readNextHeader()) return@generateSequence null // end the sequence
|
||||
if (!changeSet.readNextHeader()) return@generateSequence null // end the sequence
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error reading next header", e)
|
||||
return@generateSequence Result.Error(e)
|
||||
|
@ -163,7 +183,7 @@ internal class KVBackup(
|
|||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun clearBackupData(packageInfo: PackageInfo) {
|
||||
suspend fun clearBackupData(packageInfo: PackageInfo) {
|
||||
plugin.removeDataOfPackage(packageInfo)
|
||||
}
|
||||
|
||||
|
@ -178,18 +198,20 @@ internal class KVBackup(
|
|||
* because [finishBackup] is not called when we don't return [TRANSPORT_OK].
|
||||
*/
|
||||
private fun backupError(result: Int): Int {
|
||||
Log.i(TAG, "Resetting state because of K/V Backup error of ${state!!.packageInfo.packageName}")
|
||||
"Resetting state because of K/V Backup error of ${state!!.packageInfo.packageName}".let {
|
||||
Log.i(TAG, it)
|
||||
}
|
||||
state = null
|
||||
return result
|
||||
}
|
||||
|
||||
private class KVOperation(
|
||||
internal val key: String,
|
||||
internal val base64Key: String,
|
||||
/**
|
||||
* value is null when this is a deletion operation
|
||||
*/
|
||||
internal val value: ByteArray?
|
||||
internal val key: String,
|
||||
internal val base64Key: String,
|
||||
/**
|
||||
* value is null when this is a deletion operation
|
||||
*/
|
||||
internal val value: ByteArray?
|
||||
)
|
||||
|
||||
private sealed class Result<out T> {
|
||||
|
|
|
@ -16,7 +16,7 @@ interface KVBackupPlugin {
|
|||
* Return true if there are records stored for the given package.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun hasDataForPackage(packageInfo: PackageInfo): Boolean
|
||||
suspend fun hasDataForPackage(packageInfo: PackageInfo): Boolean
|
||||
|
||||
/**
|
||||
* This marks the beginning of a backup operation.
|
||||
|
@ -25,25 +25,25 @@ interface KVBackupPlugin {
|
|||
* E.g. file-based plugins should a create a directory for the package, if none exists.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun ensureRecordStorageForPackage(packageInfo: PackageInfo)
|
||||
suspend fun ensureRecordStorageForPackage(packageInfo: PackageInfo)
|
||||
|
||||
/**
|
||||
* Return an [OutputStream] for the given package and key
|
||||
* which will receive the record's encrypted value.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun getOutputStreamForRecord(packageInfo: PackageInfo, key: String): OutputStream
|
||||
suspend fun getOutputStreamForRecord(packageInfo: PackageInfo, key: String): OutputStream
|
||||
|
||||
/**
|
||||
* Delete the record for the given package identified by the given key.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun deleteRecord(packageInfo: PackageInfo, key: String)
|
||||
suspend fun deleteRecord(packageInfo: PackageInfo, key: String)
|
||||
|
||||
/**
|
||||
* Remove all data associated with the given package.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun removeDataOfPackage(packageInfo: PackageInfo)
|
||||
suspend fun removeDataOfPackage(packageInfo: PackageInfo)
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.stevesoltys.seedvault.transport.backup
|
||||
|
||||
import android.app.backup.IBackupManager
|
||||
import android.content.pm.ApplicationInfo.FLAG_ALLOW_BACKUP
|
||||
import android.content.pm.ApplicationInfo.FLAG_SYSTEM
|
||||
import android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.content.pm.PackageManager.GET_SIGNING_CERTIFICATES
|
||||
|
@ -20,8 +23,9 @@ private const val LOG_MAX_PACKAGES = 100
|
|||
* @author Torsten Grote
|
||||
*/
|
||||
internal class PackageService(
|
||||
private val packageManager: PackageManager,
|
||||
private val backupManager: IBackupManager) {
|
||||
private val packageManager: PackageManager,
|
||||
private val backupManager: IBackupManager
|
||||
) {
|
||||
|
||||
private val myUserId = UserHandle.myUserId()
|
||||
|
||||
|
@ -30,8 +34,8 @@ internal class PackageService(
|
|||
@Throws(RemoteException::class)
|
||||
get() {
|
||||
val packages = packageManager.getInstalledPackages(0)
|
||||
.map { packageInfo -> packageInfo.packageName }
|
||||
.sorted()
|
||||
.map { packageInfo -> packageInfo.packageName }
|
||||
.sorted()
|
||||
|
||||
// log packages
|
||||
if (Log.isLoggable(TAG, INFO)) {
|
||||
|
@ -41,14 +45,13 @@ internal class PackageService(
|
|||
}
|
||||
}
|
||||
|
||||
val eligibleApps = backupManager.filterAppsEligibleForBackupForUser(myUserId, packages.toTypedArray())
|
||||
val eligibleApps =
|
||||
backupManager.filterAppsEligibleForBackupForUser(myUserId, packages.toTypedArray())
|
||||
|
||||
// log eligible packages
|
||||
if (Log.isLoggable(TAG, INFO)) {
|
||||
Log.i(TAG, "Filtering left ${eligibleApps.size} eligible packages:")
|
||||
eligibleApps.toList().chunked(LOG_MAX_PACKAGES).forEach {
|
||||
Log.i(TAG, it.toString())
|
||||
}
|
||||
logPackages(eligibleApps.toList())
|
||||
}
|
||||
|
||||
// add magic @pm@ package (PACKAGE_MANAGER_SENTINEL) which holds package manager data
|
||||
|
@ -61,16 +64,48 @@ internal class PackageService(
|
|||
val notAllowedPackages: List<PackageInfo>
|
||||
@WorkerThread
|
||||
get() {
|
||||
val installed = packageManager.getInstalledPackages(GET_SIGNING_CERTIFICATES)
|
||||
val installedArray = installed.map { packageInfo ->
|
||||
packageInfo.packageName
|
||||
}.toTypedArray()
|
||||
|
||||
val eligible = backupManager.filterAppsEligibleForBackupForUser(myUserId, installedArray)
|
||||
|
||||
return installed.filter { packageInfo ->
|
||||
packageInfo.packageName !in eligible
|
||||
}.sortedBy { it.packageName }
|
||||
// We need the GET_SIGNING_CERTIFICATES flag here,
|
||||
// because the package info is used by [ApkBackup] which needs signing info.
|
||||
return packageManager.getInstalledPackages(GET_SIGNING_CERTIFICATES)
|
||||
.filter { packageInfo ->
|
||||
!packageInfo.isBackupAllowed() && // only apps that do not allow backup
|
||||
!packageInfo.isNotUpdatedSystemApp() // and are not vanilla system apps
|
||||
}.sortedBy { packageInfo ->
|
||||
packageInfo.packageName
|
||||
}.also { notAllowed ->
|
||||
// log eligible packages
|
||||
if (Log.isLoggable(TAG, INFO)) {
|
||||
Log.i(TAG, "${notAllowed.size} apps do not allow backup:")
|
||||
logPackages(notAllowed.map { it.packageName })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun logPackages(packages: List<String>) {
|
||||
packages.chunked(LOG_MAX_PACKAGES).forEach {
|
||||
Log.i(TAG, it.toString())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal fun PackageInfo.isSystemApp(): Boolean {
|
||||
if (packageName == MAGIC_PACKAGE_MANAGER || applicationInfo == null) return true
|
||||
return applicationInfo.flags and FLAG_SYSTEM != 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this is a system app that hasn't been updated.
|
||||
* We don't back up those APKs.
|
||||
*/
|
||||
internal fun PackageInfo.isNotUpdatedSystemApp(): Boolean {
|
||||
if (packageName == MAGIC_PACKAGE_MANAGER || applicationInfo == null) return true
|
||||
val isSystemApp = applicationInfo.flags and FLAG_SYSTEM != 0
|
||||
val isUpdatedSystemApp = applicationInfo.flags and FLAG_UPDATED_SYSTEM_APP != 0
|
||||
return isSystemApp && !isUpdatedSystemApp
|
||||
}
|
||||
|
||||
internal fun PackageInfo.isBackupAllowed(): Boolean {
|
||||
if (packageName == MAGIC_PACKAGE_MANAGER || applicationInfo == null) return false
|
||||
return applicationInfo.flags and FLAG_ALLOW_BACKUP != 0
|
||||
}
|
||||
|
|
|
@ -9,8 +9,8 @@ import android.util.Log
|
|||
import com.stevesoltys.seedvault.encodeBase64
|
||||
import com.stevesoltys.seedvault.metadata.PackageMetadata
|
||||
import com.stevesoltys.seedvault.metadata.PackageMetadataMap
|
||||
import com.stevesoltys.seedvault.metadata.isSystemApp
|
||||
import com.stevesoltys.seedvault.transport.backup.getSignatures
|
||||
import com.stevesoltys.seedvault.transport.backup.isSystemApp
|
||||
import com.stevesoltys.seedvault.transport.restore.ApkRestoreStatus.FAILED
|
||||
import com.stevesoltys.seedvault.transport.restore.ApkRestoreStatus.IN_PROGRESS
|
||||
import com.stevesoltys.seedvault.transport.restore.ApkRestoreStatus.QUEUED
|
||||
|
|
|
@ -23,6 +23,7 @@ private class FullRestoreState(
|
|||
|
||||
private val TAG = FullRestore::class.java.simpleName
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class FullRestore(
|
||||
private val plugin: FullRestorePlugin,
|
||||
private val outputFactory: OutputFactory,
|
||||
|
@ -37,7 +38,7 @@ internal class FullRestore(
|
|||
* Return true if there is data stored for the given package.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean {
|
||||
suspend fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean {
|
||||
return plugin.hasDataForPackage(token, packageInfo)
|
||||
}
|
||||
|
||||
|
@ -78,7 +79,7 @@ internal class FullRestore(
|
|||
* Any other negative value such as [TRANSPORT_ERROR] is treated as a fatal error condition
|
||||
* that aborts all further restore operations on the current dataset.
|
||||
*/
|
||||
fun getNextFullRestoreDataChunk(socket: ParcelFileDescriptor): Int {
|
||||
suspend fun getNextFullRestoreDataChunk(socket: ParcelFileDescriptor): Int {
|
||||
val state = this.state ?: throw IllegalStateException("no state")
|
||||
val packageName = state.packageInfo.packageName
|
||||
|
||||
|
@ -113,6 +114,7 @@ internal class FullRestore(
|
|||
try {
|
||||
// read segment from input stream and decrypt it
|
||||
val decrypted = try {
|
||||
// TODO handle IOException
|
||||
crypto.decryptSegment(inputStream)
|
||||
} catch (e: EOFException) {
|
||||
Log.i(TAG, " EOF")
|
||||
|
|
|
@ -10,9 +10,9 @@ interface FullRestorePlugin {
|
|||
* Return true if there is data stored for the given package.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean
|
||||
suspend fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun getInputStreamForPackage(token: Long, packageInfo: PackageInfo): InputStream
|
||||
suspend fun getInputStreamForPackage(token: Long, packageInfo: PackageInfo): InputStream
|
||||
|
||||
}
|
||||
|
|
|
@ -15,24 +15,27 @@ import com.stevesoltys.seedvault.header.HeaderReader
|
|||
import com.stevesoltys.seedvault.header.UnsupportedVersionException
|
||||
import libcore.io.IoUtils.closeQuietly
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
import java.util.ArrayList
|
||||
import javax.crypto.AEADBadTagException
|
||||
|
||||
private class KVRestoreState(
|
||||
internal val token: Long,
|
||||
internal val packageInfo: PackageInfo,
|
||||
/**
|
||||
* Optional [PackageInfo] for single package restore, optimizes restore of @pm@
|
||||
*/
|
||||
internal val pmPackageInfo: PackageInfo?)
|
||||
internal val token: Long,
|
||||
internal val packageInfo: PackageInfo,
|
||||
/**
|
||||
* Optional [PackageInfo] for single package restore, optimizes restore of @pm@
|
||||
*/
|
||||
internal val pmPackageInfo: PackageInfo?
|
||||
)
|
||||
|
||||
private val TAG = KVRestore::class.java.simpleName
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class KVRestore(
|
||||
private val plugin: KVRestorePlugin,
|
||||
private val outputFactory: OutputFactory,
|
||||
private val headerReader: HeaderReader,
|
||||
private val crypto: Crypto) {
|
||||
private val plugin: KVRestorePlugin,
|
||||
private val outputFactory: OutputFactory,
|
||||
private val headerReader: HeaderReader,
|
||||
private val crypto: Crypto
|
||||
) {
|
||||
|
||||
private var state: KVRestoreState? = null
|
||||
|
||||
|
@ -40,7 +43,7 @@ internal class KVRestore(
|
|||
* Return true if there are records stored for the given package.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean {
|
||||
suspend fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean {
|
||||
return plugin.hasDataForPackage(token, packageInfo)
|
||||
}
|
||||
|
||||
|
@ -63,7 +66,7 @@ internal class KVRestore(
|
|||
* @return One of [TRANSPORT_OK]
|
||||
* or [TRANSPORT_ERROR] (an error occurred, the restore should be aborted and rescheduled).
|
||||
*/
|
||||
fun getRestoreData(data: ParcelFileDescriptor): Int {
|
||||
suspend fun getRestoreData(data: ParcelFileDescriptor): Int {
|
||||
val state = this.state ?: throw IllegalStateException("no state")
|
||||
|
||||
// The restore set is the concatenation of the individual record blobs,
|
||||
|
@ -122,11 +125,12 @@ internal class KVRestore(
|
|||
for (recordKey in records) contents.add(DecodedKey(recordKey))
|
||||
// remove keys that are not needed for single package @pm@ restore
|
||||
val pmPackageName = state?.pmPackageInfo?.packageName
|
||||
val sortedKeys = if (packageInfo.packageName == MAGIC_PACKAGE_MANAGER && pmPackageName != null) {
|
||||
val keys = listOf(ANCESTRAL_RECORD_KEY, GLOBAL_METADATA_KEY, pmPackageName)
|
||||
Log.d(TAG, "Single package restore, restrict restore keys to $pmPackageName")
|
||||
contents.filterTo(ArrayList()) { it.key in keys }
|
||||
} else contents
|
||||
val sortedKeys =
|
||||
if (packageInfo.packageName == MAGIC_PACKAGE_MANAGER && pmPackageName != null) {
|
||||
val keys = listOf(ANCESTRAL_RECORD_KEY, GLOBAL_METADATA_KEY, pmPackageName)
|
||||
Log.d(TAG, "Single package restore, restrict restore keys to $pmPackageName")
|
||||
contents.filterTo(ArrayList()) { it.key in keys }
|
||||
} else contents
|
||||
sortedKeys.sort()
|
||||
return sortedKeys
|
||||
}
|
||||
|
@ -135,9 +139,12 @@ internal class KVRestore(
|
|||
* Read the encrypted value for the given key and write it to the given [BackupDataOutput].
|
||||
*/
|
||||
@Throws(IOException::class, UnsupportedVersionException::class, SecurityException::class)
|
||||
private fun readAndWriteValue(state: KVRestoreState, dKey: DecodedKey, out: BackupDataOutput) {
|
||||
val inputStream = plugin.getInputStreamForRecord(state.token, state.packageInfo, dKey.base64Key)
|
||||
try {
|
||||
private suspend fun readAndWriteValue(
|
||||
state: KVRestoreState,
|
||||
dKey: DecodedKey,
|
||||
out: BackupDataOutput
|
||||
) = plugin.getInputStreamForRecord(state.token, state.packageInfo, dKey.base64Key)
|
||||
.use { inputStream ->
|
||||
val version = headerReader.readVersion(inputStream)
|
||||
crypto.decryptHeader(inputStream, version, state.packageInfo.packageName, dKey.key)
|
||||
val value = crypto.decryptMultipleSegments(inputStream)
|
||||
|
@ -146,10 +153,8 @@ internal class KVRestore(
|
|||
|
||||
out.writeEntityHeader(dKey.key, size)
|
||||
out.writeEntityData(value, size)
|
||||
} finally {
|
||||
closeQuietly(inputStream)
|
||||
Unit
|
||||
}
|
||||
}
|
||||
|
||||
private class DecodedKey(internal val base64Key: String) : Comparable<DecodedKey> {
|
||||
internal val key = base64Key.decodeBase64()
|
||||
|
|
|
@ -10,7 +10,7 @@ interface KVRestorePlugin {
|
|||
* Return true if there is data stored for the given package.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean
|
||||
suspend fun hasDataForPackage(token: Long, packageInfo: PackageInfo): Boolean
|
||||
|
||||
/**
|
||||
* Return all record keys for the given token and package.
|
||||
|
@ -29,6 +29,6 @@ interface KVRestorePlugin {
|
|||
* Note: Implementations might expect that you call [hasDataForPackage] before.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun getInputStreamForRecord(token: Long, packageInfo: PackageInfo, key: String): InputStream
|
||||
suspend fun getInputStreamForRecord(token: Long, packageInfo: PackageInfo, key: String): InputStream
|
||||
|
||||
}
|
||||
|
|
|
@ -26,26 +26,29 @@ import libcore.io.IoUtils.closeQuietly
|
|||
import java.io.IOException
|
||||
|
||||
private class RestoreCoordinatorState(
|
||||
internal val token: Long,
|
||||
internal val packages: Iterator<PackageInfo>,
|
||||
/**
|
||||
* Optional [PackageInfo] for single package restore, to reduce data needed to read for @pm@
|
||||
*/
|
||||
internal val pmPackageInfo: PackageInfo?) {
|
||||
internal val token: Long,
|
||||
internal val packages: Iterator<PackageInfo>,
|
||||
/**
|
||||
* Optional [PackageInfo] for single package restore, to reduce data needed to read for @pm@
|
||||
*/
|
||||
internal val pmPackageInfo: PackageInfo?
|
||||
) {
|
||||
internal var currentPackage: String? = null
|
||||
}
|
||||
|
||||
private val TAG = RestoreCoordinator::class.java.simpleName
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class RestoreCoordinator(
|
||||
private val context: Context,
|
||||
private val settingsManager: SettingsManager,
|
||||
private val metadataManager: MetadataManager,
|
||||
private val notificationManager: BackupNotificationManager,
|
||||
private val plugin: RestorePlugin,
|
||||
private val kv: KVRestore,
|
||||
private val full: FullRestore,
|
||||
private val metadataReader: MetadataReader) {
|
||||
private val context: Context,
|
||||
private val settingsManager: SettingsManager,
|
||||
private val metadataManager: MetadataManager,
|
||||
private val notificationManager: BackupNotificationManager,
|
||||
private val plugin: RestorePlugin,
|
||||
private val kv: KVRestore,
|
||||
private val full: FullRestore,
|
||||
private val metadataReader: MetadataReader
|
||||
) {
|
||||
|
||||
private var state: RestoreCoordinatorState? = null
|
||||
private var backupMetadata: LongSparseArray<BackupMetadata>? = null
|
||||
|
@ -57,7 +60,7 @@ internal class RestoreCoordinator(
|
|||
* @return Descriptions of the set of restore images available for this device,
|
||||
* or null if an error occurred (the attempt should be rescheduled).
|
||||
**/
|
||||
fun getAvailableRestoreSets(): Array<RestoreSet>? {
|
||||
suspend fun getAvailableRestoreSets(): Array<RestoreSet>? {
|
||||
val availableBackups = plugin.getAvailableBackups() ?: return null
|
||||
val restoreSets = ArrayList<RestoreSet>()
|
||||
val metadataMap = LongSparseArray<BackupMetadata>()
|
||||
|
@ -67,7 +70,10 @@ internal class RestoreCoordinator(
|
|||
"No error when getting encrypted metadata, but stream is still missing."
|
||||
}
|
||||
try {
|
||||
val metadata = metadataReader.readMetadata(encryptedMetadata.inputStream, encryptedMetadata.token)
|
||||
val metadata = metadataReader.readMetadata(
|
||||
encryptedMetadata.inputStream,
|
||||
encryptedMetadata.token
|
||||
)
|
||||
metadataMap.put(encryptedMetadata.token, metadata)
|
||||
val set = RestoreSet(metadata.deviceName, metadata.deviceName, metadata.token)
|
||||
restoreSets.add(set)
|
||||
|
@ -101,7 +107,7 @@ internal class RestoreCoordinator(
|
|||
*/
|
||||
fun getCurrentRestoreSet(): Long {
|
||||
return metadataManager.getBackupToken()
|
||||
.apply { Log.i(TAG, "Got current restore set token: $this") }
|
||||
.apply { Log.i(TAG, "Got current restore set token: $this") }
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -121,22 +127,26 @@ internal class RestoreCoordinator(
|
|||
Log.i(TAG, "Start restore with ${packages.map { info -> info.packageName }}")
|
||||
|
||||
// If there's only one package to restore (Auto Restore feature), add it to the state
|
||||
val pmPackageInfo = if (packages.size == 2 && packages[0].packageName == MAGIC_PACKAGE_MANAGER) {
|
||||
val pmPackageName = packages[1].packageName
|
||||
Log.d(TAG, "Optimize for single package restore of $pmPackageName")
|
||||
// check if the backup is on removable storage that is not plugged in
|
||||
if (isStorageRemovableAndNotAvailable()) {
|
||||
// check if we even have a backup of that app
|
||||
if (metadataManager.getPackageMetadata(pmPackageName) != null) {
|
||||
// remind user to plug in storage device
|
||||
val storageName = settingsManager.getStorage()?.name
|
||||
val pmPackageInfo =
|
||||
if (packages.size == 2 && packages[0].packageName == MAGIC_PACKAGE_MANAGER) {
|
||||
val pmPackageName = packages[1].packageName
|
||||
Log.d(TAG, "Optimize for single package restore of $pmPackageName")
|
||||
// check if the backup is on removable storage that is not plugged in
|
||||
if (isStorageRemovableAndNotAvailable()) {
|
||||
// check if we even have a backup of that app
|
||||
if (metadataManager.getPackageMetadata(pmPackageName) != null) {
|
||||
// remind user to plug in storage device
|
||||
val storageName = settingsManager.getStorage()?.name
|
||||
?: context.getString(R.string.settings_backup_location_none)
|
||||
notificationManager.onRemovableStorageNotAvailableForRestore(pmPackageName, storageName)
|
||||
notificationManager.onRemovableStorageNotAvailableForRestore(
|
||||
pmPackageName,
|
||||
storageName
|
||||
)
|
||||
}
|
||||
return TRANSPORT_ERROR
|
||||
}
|
||||
return TRANSPORT_ERROR
|
||||
}
|
||||
packages[1]
|
||||
} else null
|
||||
packages[1]
|
||||
} else null
|
||||
|
||||
state = RestoreCoordinatorState(token, packages.iterator(), pmPackageInfo)
|
||||
failedPackages.clear()
|
||||
|
@ -169,7 +179,7 @@ internal class RestoreCoordinator(
|
|||
* or [NO_MORE_PACKAGES] to indicate that no more packages can be restored in this session;
|
||||
* or null to indicate a transport-level error.
|
||||
*/
|
||||
fun nextRestorePackage(): RestoreDescription? {
|
||||
suspend fun nextRestorePackage(): RestoreDescription? {
|
||||
Log.i(TAG, "Next restore package!")
|
||||
val state = this.state ?: throw IllegalStateException("no state")
|
||||
|
||||
|
@ -213,7 +223,7 @@ internal class RestoreCoordinator(
|
|||
* @param data An open, writable file into which the key/value backup data should be stored.
|
||||
* @return the same error codes as [startRestore].
|
||||
*/
|
||||
fun getRestoreData(data: ParcelFileDescriptor): Int {
|
||||
suspend fun getRestoreData(data: ParcelFileDescriptor): Int {
|
||||
return kv.getRestoreData(data).apply {
|
||||
if (this != TRANSPORT_OK) {
|
||||
// add current package to failed ones
|
||||
|
@ -228,7 +238,7 @@ internal class RestoreCoordinator(
|
|||
* After this method returns zero, the system will then call [nextRestorePackage]
|
||||
* to begin the restore process for the next application, and the sequence begins again.
|
||||
*/
|
||||
fun getNextFullRestoreDataChunk(outputFileDescriptor: ParcelFileDescriptor): Int {
|
||||
suspend fun getNextFullRestoreDataChunk(outputFileDescriptor: ParcelFileDescriptor): Int {
|
||||
return full.getNextFullRestoreDataChunk(outputFileDescriptor)
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ interface RestorePlugin {
|
|||
* @return metadata for the set of restore images available,
|
||||
* or null if an error occurred (the attempt should be rescheduled).
|
||||
**/
|
||||
fun getAvailableBackups(): Sequence<EncryptedBackupMetadata>?
|
||||
suspend fun getAvailableBackups(): Sequence<EncryptedBackupMetadata>?
|
||||
|
||||
/**
|
||||
* Searches if there's really a backup available in the given location.
|
||||
|
@ -27,12 +27,13 @@ interface RestorePlugin {
|
|||
* FIXME: Passing a Uri is maybe too plugin-specific?
|
||||
*/
|
||||
@WorkerThread
|
||||
fun hasBackup(uri: Uri): Boolean
|
||||
@Throws(IOException::class)
|
||||
suspend fun hasBackup(uri: Uri): Boolean
|
||||
|
||||
/**
|
||||
* Returns an [InputStream] for the given token, for reading an APK that is to be restored.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun getApkInputStream(token: Long, packageName: String): InputStream
|
||||
suspend fun getApkInputStream(token: Long, packageName: String): InputStream
|
||||
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ internal class BackupStorageViewModel(
|
|||
|
||||
override fun onLocationSet(uri: Uri) {
|
||||
val isUsb = saveStorage(uri)
|
||||
settingsManager.forceStorageInitialization()
|
||||
|
||||
// initialize the new location, will also generate a new backup token
|
||||
val observer = InitializationObserver()
|
||||
|
|
|
@ -3,10 +3,14 @@ package com.stevesoltys.seedvault.ui.storage
|
|||
import android.app.Application
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.stevesoltys.seedvault.R
|
||||
import com.stevesoltys.seedvault.plugins.saf.DIRECTORY_ROOT
|
||||
import com.stevesoltys.seedvault.settings.SettingsManager
|
||||
import com.stevesoltys.seedvault.transport.restore.RestorePlugin
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.IOException
|
||||
|
||||
private val TAG = RestoreStorageViewModel::class.java.simpleName
|
||||
|
||||
|
@ -17,18 +21,26 @@ internal class RestoreStorageViewModel(
|
|||
|
||||
override val isRestoreOperation = true
|
||||
|
||||
override fun onLocationSet(uri: Uri) = Thread {
|
||||
if (restorePlugin.hasBackup(uri)) {
|
||||
saveStorage(uri)
|
||||
override fun onLocationSet(uri: Uri) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val hasBackup = try {
|
||||
restorePlugin.hasBackup(uri)
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error reading URI: $uri", e)
|
||||
false
|
||||
}
|
||||
if (hasBackup) {
|
||||
saveStorage(uri)
|
||||
|
||||
mLocationChecked.postEvent(LocationResult())
|
||||
} else {
|
||||
Log.w(TAG, "Location was rejected: $uri")
|
||||
mLocationChecked.postEvent(LocationResult())
|
||||
} else {
|
||||
Log.w(TAG, "Location was rejected: $uri")
|
||||
|
||||
// notify the UI that the location was invalid
|
||||
val errorMsg = app.getString(R.string.restore_invalid_location_message, DIRECTORY_ROOT)
|
||||
mLocationChecked.postEvent(LocationResult(errorMsg))
|
||||
// notify the UI that the location was invalid
|
||||
val errorMsg = app.getString(R.string.restore_invalid_location_message, DIRECTORY_ROOT)
|
||||
mLocationChecked.postEvent(LocationResult(errorMsg))
|
||||
}
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:tint="?android:attr/textColorSecondary"
|
||||
android:tint="?attr/colorControlNormal"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:fillColor="#FFFFFFFF"
|
||||
android:pathData="M16,1L8,1C6.34,1 5,2.34 5,4v16c0,1.66 1.34,3 3,3h8c1.66,0 3,-1.34 3,-3L19,4c0,-1.66 -1.34,-3 -3,-3zM14,21h-4v-1h4v1zM17.25,18L6.75,18L6.75,4h10.5v14z" />
|
||||
</vector>
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:tint="?android:attr/textColorSecondary"
|
||||
android:tint="?attr/colorControlNormal"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:fillColor="#FFFFFFFF"
|
||||
android:pathData="M15,7v4h1v2h-3V5h2l-3,-4 -3,4h2v8H8v-2.07c0.7,-0.37 1.2,-1.08 1.2,-1.93 0,-1.21 -0.99,-2.2 -2.2,-2.2 -1.21,0 -2.2,0.99 -2.2,2.2 0,0.85 0.5,1.56 1.2,1.93V13c0,1.11 0.89,2 2,2h3v3.05c-0.71,0.37 -1.2,1.1 -1.2,1.95 0,1.22 0.99,2.2 2.2,2.2 1.21,0 2.2,-0.98 2.2,-2.2 0,-0.85 -0.49,-1.58 -1.2,-1.95V15h3c1.11,0 2,-0.89 2,-2v-2h1V7h-4z" />
|
||||
</vector>
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
android:layout_marginStart="16dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:tint="?android:attr/textColorSecondary"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
package com.stevesoltys.seedvault
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.fail
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import kotlin.random.Random
|
||||
|
||||
fun assertContains(stack: String?, needle: String) {
|
||||
|
@ -44,3 +50,24 @@ fun ByteArray.toIntString(): String {
|
|||
}
|
||||
return str
|
||||
}
|
||||
|
||||
fun OutputStream.writeAndClose(data: ByteArray) = use {
|
||||
it.write(data)
|
||||
}
|
||||
|
||||
fun assertReadEquals(data: ByteArray, inputStream: InputStream?) = inputStream?.use {
|
||||
assertArrayEquals(data, it.readBytes())
|
||||
} ?: error("no input stream")
|
||||
|
||||
fun <T : Throwable> coAssertThrows(clazz: Class<T>, block: suspend () -> Unit) {
|
||||
var thrown = false
|
||||
try {
|
||||
runBlocking {
|
||||
block()
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
assertEquals(clazz, e.javaClass)
|
||||
thrown = true
|
||||
}
|
||||
if (!thrown) fail("Exception was not thrown: " + clazz.name)
|
||||
}
|
||||
|
|
|
@ -37,9 +37,11 @@ import com.stevesoltys.seedvault.transport.restore.RestoreCoordinator
|
|||
import com.stevesoltys.seedvault.transport.restore.RestorePlugin
|
||||
import io.mockk.CapturingSlot
|
||||
import io.mockk.Runs
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertArrayEquals
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.fail
|
||||
|
@ -48,6 +50,7 @@ import java.io.ByteArrayInputStream
|
|||
import java.io.ByteArrayOutputStream
|
||||
import kotlin.random.Random
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class CoordinatorIntegrationTest : TransportTest() {
|
||||
|
||||
private val inputFactory = mockk<InputFactory>()
|
||||
|
@ -65,16 +68,37 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
private val fullBackupPlugin = mockk<FullBackupPlugin>()
|
||||
private val fullBackup = FullBackup(fullBackupPlugin, inputFactory, headerWriter, cryptoImpl)
|
||||
private val apkBackup = mockk<ApkBackup>()
|
||||
private val packageService:PackageService = mockk()
|
||||
private val packageService: PackageService = mockk()
|
||||
private val notificationManager = mockk<BackupNotificationManager>()
|
||||
private val backup = BackupCoordinator(context, backupPlugin, kvBackup, fullBackup, apkBackup, clock, packageService, metadataManager, settingsManager, notificationManager)
|
||||
private val backup = BackupCoordinator(
|
||||
context,
|
||||
backupPlugin,
|
||||
kvBackup,
|
||||
fullBackup,
|
||||
apkBackup,
|
||||
clock,
|
||||
packageService,
|
||||
metadataManager,
|
||||
settingsManager,
|
||||
notificationManager
|
||||
)
|
||||
|
||||
private val restorePlugin = mockk<RestorePlugin>()
|
||||
private val kvRestorePlugin = mockk<KVRestorePlugin>()
|
||||
private val kvRestore = KVRestore(kvRestorePlugin, outputFactory, headerReader, cryptoImpl)
|
||||
private val fullRestorePlugin = mockk<FullRestorePlugin>()
|
||||
private val fullRestore = FullRestore(fullRestorePlugin, outputFactory, headerReader, cryptoImpl)
|
||||
private val restore = RestoreCoordinator(context, settingsManager, metadataManager, notificationManager, restorePlugin, kvRestore, fullRestore, metadataReader)
|
||||
private val fullRestore =
|
||||
FullRestore(fullRestorePlugin, outputFactory, headerReader, cryptoImpl)
|
||||
private val restore = RestoreCoordinator(
|
||||
context,
|
||||
settingsManager,
|
||||
metadataManager,
|
||||
notificationManager,
|
||||
restorePlugin,
|
||||
kvRestore,
|
||||
fullRestore,
|
||||
metadataReader
|
||||
)
|
||||
|
||||
private val backupDataInput = mockk<BackupDataInput>()
|
||||
private val fileDescriptor = mockk<ParcelFileDescriptor>(relaxed = true)
|
||||
|
@ -94,15 +118,15 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `test key-value backup and restore with 2 records`() {
|
||||
fun `test key-value backup and restore with 2 records`() = runBlocking {
|
||||
val value = CapturingSlot<ByteArray>()
|
||||
val value2 = CapturingSlot<ByteArray>()
|
||||
val bOutputStream = ByteArrayOutputStream()
|
||||
val bOutputStream2 = ByteArrayOutputStream()
|
||||
|
||||
// read one key/value record and write it to output stream
|
||||
every { kvBackupPlugin.hasDataForPackage(packageInfo) } returns false
|
||||
every { kvBackupPlugin.ensureRecordStorageForPackage(packageInfo) } just Runs
|
||||
coEvery { kvBackupPlugin.hasDataForPackage(packageInfo) } returns false
|
||||
coEvery { kvBackupPlugin.ensureRecordStorageForPackage(packageInfo) } just Runs
|
||||
every { inputFactory.getBackupDataInput(fileDescriptor) } returns backupDataInput
|
||||
every { backupDataInput.readNextHeader() } returns true andThen true andThen false
|
||||
every { backupDataInput.key } returns key andThen key2
|
||||
|
@ -111,15 +135,37 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
appData.copyInto(value.captured) // write the app data into the passed ByteArray
|
||||
appData.size
|
||||
}
|
||||
every { kvBackupPlugin.getOutputStreamForRecord(packageInfo, key64) } returns bOutputStream
|
||||
coEvery {
|
||||
kvBackupPlugin.getOutputStreamForRecord(
|
||||
packageInfo,
|
||||
key64
|
||||
)
|
||||
} returns bOutputStream
|
||||
every { backupDataInput.readEntityData(capture(value2), 0, appData2.size) } answers {
|
||||
appData2.copyInto(value2.captured) // write the app data into the passed ByteArray
|
||||
appData2.size
|
||||
}
|
||||
every { kvBackupPlugin.getOutputStreamForRecord(packageInfo, key264) } returns bOutputStream2
|
||||
every { apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, any()) } returns packageMetadata
|
||||
every { backupPlugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every { metadataManager.onApkBackedUp(packageInfo, packageMetadata, metadataOutputStream) } just Runs
|
||||
coEvery {
|
||||
kvBackupPlugin.getOutputStreamForRecord(
|
||||
packageInfo,
|
||||
key264
|
||||
)
|
||||
} returns bOutputStream2
|
||||
coEvery {
|
||||
apkBackup.backupApkIfNecessary(
|
||||
packageInfo,
|
||||
UNKNOWN_ERROR,
|
||||
any()
|
||||
)
|
||||
} returns packageMetadata
|
||||
coEvery { backupPlugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every {
|
||||
metadataManager.onApkBackedUp(
|
||||
packageInfo,
|
||||
packageMetadata,
|
||||
metadataOutputStream
|
||||
)
|
||||
} just Runs
|
||||
every { metadataManager.onPackageBackedUp(packageInfo, metadataOutputStream) } just Runs
|
||||
|
||||
// start and finish K/V backup
|
||||
|
@ -130,7 +176,7 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
assertEquals(TRANSPORT_OK, restore.startRestore(token, arrayOf(packageInfo)))
|
||||
|
||||
// find data for K/V backup
|
||||
every { kvRestorePlugin.hasDataForPackage(token, packageInfo) } returns true
|
||||
coEvery { kvRestorePlugin.hasDataForPackage(token, packageInfo) } returns true
|
||||
|
||||
val restoreDescription = restore.nextRestorePackage() ?: fail()
|
||||
assertEquals(packageInfo.packageName, restoreDescription.packageName)
|
||||
|
@ -142,10 +188,22 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
val rInputStream2 = ByteArrayInputStream(bOutputStream2.toByteArray())
|
||||
every { kvRestorePlugin.listRecords(token, packageInfo) } returns listOf(key64, key264)
|
||||
every { outputFactory.getBackupDataOutput(fileDescriptor) } returns backupDataOutput
|
||||
every { kvRestorePlugin.getInputStreamForRecord(token, packageInfo, key64) } returns rInputStream
|
||||
coEvery {
|
||||
kvRestorePlugin.getInputStreamForRecord(
|
||||
token,
|
||||
packageInfo,
|
||||
key64
|
||||
)
|
||||
} returns rInputStream
|
||||
every { backupDataOutput.writeEntityHeader(key, appData.size) } returns 1137
|
||||
every { backupDataOutput.writeEntityData(appData, appData.size) } returns appData.size
|
||||
every { kvRestorePlugin.getInputStreamForRecord(token, packageInfo, key264) } returns rInputStream2
|
||||
coEvery {
|
||||
kvRestorePlugin.getInputStreamForRecord(
|
||||
token,
|
||||
packageInfo,
|
||||
key264
|
||||
)
|
||||
} returns rInputStream2
|
||||
every { backupDataOutput.writeEntityHeader(key2, appData2.size) } returns 1137
|
||||
every { backupDataOutput.writeEntityData(appData2, appData2.size) } returns appData2.size
|
||||
|
||||
|
@ -153,15 +211,15 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `test key-value backup with huge value`() {
|
||||
fun `test key-value backup with huge value`() = runBlocking {
|
||||
val value = CapturingSlot<ByteArray>()
|
||||
val size = Random.nextInt(5) * MAX_SEGMENT_CLEARTEXT_LENGTH + Random.nextInt(0, 1337)
|
||||
val appData = ByteArray(size).apply { Random.nextBytes(this) }
|
||||
val bOutputStream = ByteArrayOutputStream()
|
||||
|
||||
// read one key/value record and write it to output stream
|
||||
every { kvBackupPlugin.hasDataForPackage(packageInfo) } returns false
|
||||
every { kvBackupPlugin.ensureRecordStorageForPackage(packageInfo) } just Runs
|
||||
coEvery { kvBackupPlugin.hasDataForPackage(packageInfo) } returns false
|
||||
coEvery { kvBackupPlugin.ensureRecordStorageForPackage(packageInfo) } just Runs
|
||||
every { inputFactory.getBackupDataInput(fileDescriptor) } returns backupDataInput
|
||||
every { backupDataInput.readNextHeader() } returns true andThen false
|
||||
every { backupDataInput.key } returns key
|
||||
|
@ -170,9 +228,14 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
appData.copyInto(value.captured) // write the app data into the passed ByteArray
|
||||
appData.size
|
||||
}
|
||||
every { kvBackupPlugin.getOutputStreamForRecord(packageInfo, key64) } returns bOutputStream
|
||||
every { apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, any()) } returns null
|
||||
every { backupPlugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
coEvery {
|
||||
kvBackupPlugin.getOutputStreamForRecord(
|
||||
packageInfo,
|
||||
key64
|
||||
)
|
||||
} returns bOutputStream
|
||||
coEvery { apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, any()) } returns null
|
||||
coEvery { backupPlugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every { metadataManager.onPackageBackedUp(packageInfo, metadataOutputStream) } just Runs
|
||||
|
||||
// start and finish K/V backup
|
||||
|
@ -183,7 +246,7 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
assertEquals(TRANSPORT_OK, restore.startRestore(token, arrayOf(packageInfo)))
|
||||
|
||||
// find data for K/V backup
|
||||
every { kvRestorePlugin.hasDataForPackage(token, packageInfo) } returns true
|
||||
coEvery { kvRestorePlugin.hasDataForPackage(token, packageInfo) } returns true
|
||||
|
||||
val restoreDescription = restore.nextRestorePackage() ?: fail()
|
||||
assertEquals(packageInfo.packageName, restoreDescription.packageName)
|
||||
|
@ -194,7 +257,13 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
val rInputStream = ByteArrayInputStream(bOutputStream.toByteArray())
|
||||
every { kvRestorePlugin.listRecords(token, packageInfo) } returns listOf(key64)
|
||||
every { outputFactory.getBackupDataOutput(fileDescriptor) } returns backupDataOutput
|
||||
every { kvRestorePlugin.getInputStreamForRecord(token, packageInfo, key64) } returns rInputStream
|
||||
coEvery {
|
||||
kvRestorePlugin.getInputStreamForRecord(
|
||||
token,
|
||||
packageInfo,
|
||||
key64
|
||||
)
|
||||
} returns rInputStream
|
||||
every { backupDataOutput.writeEntityHeader(key, appData.size) } returns 1137
|
||||
every { backupDataOutput.writeEntityData(appData, appData.size) } returns appData.size
|
||||
|
||||
|
@ -202,16 +271,28 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `test full backup and restore with two chunks`() {
|
||||
fun `test full backup and restore with two chunks`() = runBlocking {
|
||||
// return streams from plugin and app data
|
||||
val bOutputStream = ByteArrayOutputStream()
|
||||
val bInputStream = ByteArrayInputStream(appData)
|
||||
every { fullBackupPlugin.getOutputStream(packageInfo) } returns bOutputStream
|
||||
coEvery { fullBackupPlugin.getOutputStream(packageInfo) } returns bOutputStream
|
||||
every { inputFactory.getInputStream(fileDescriptor) } returns bInputStream
|
||||
every { fullBackupPlugin.getQuota() } returns DEFAULT_QUOTA_FULL_BACKUP
|
||||
every { apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, any()) } returns packageMetadata
|
||||
every { backupPlugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every { metadataManager.onApkBackedUp(packageInfo, packageMetadata, metadataOutputStream) } just Runs
|
||||
coEvery {
|
||||
apkBackup.backupApkIfNecessary(
|
||||
packageInfo,
|
||||
UNKNOWN_ERROR,
|
||||
any()
|
||||
)
|
||||
} returns packageMetadata
|
||||
coEvery { backupPlugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every {
|
||||
metadataManager.onApkBackedUp(
|
||||
packageInfo,
|
||||
packageMetadata,
|
||||
metadataOutputStream
|
||||
)
|
||||
} just Runs
|
||||
every { metadataManager.onPackageBackedUp(packageInfo, metadataOutputStream) } just Runs
|
||||
|
||||
// perform backup to output stream
|
||||
|
@ -224,8 +305,8 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
assertEquals(TRANSPORT_OK, restore.startRestore(token, arrayOf(packageInfo)))
|
||||
|
||||
// find data only for full backup
|
||||
every { kvRestorePlugin.hasDataForPackage(token, packageInfo) } returns false
|
||||
every { fullRestorePlugin.hasDataForPackage(token, packageInfo) } returns true
|
||||
coEvery { kvRestorePlugin.hasDataForPackage(token, packageInfo) } returns false
|
||||
coEvery { fullRestorePlugin.hasDataForPackage(token, packageInfo) } returns true
|
||||
|
||||
val restoreDescription = restore.nextRestorePackage() ?: fail()
|
||||
assertEquals(packageInfo.packageName, restoreDescription.packageName)
|
||||
|
@ -234,7 +315,12 @@ internal class CoordinatorIntegrationTest : TransportTest() {
|
|||
// reverse the backup streams into restore input
|
||||
val rInputStream = ByteArrayInputStream(bOutputStream.toByteArray())
|
||||
val rOutputStream = ByteArrayOutputStream()
|
||||
every { fullRestorePlugin.getInputStreamForPackage(token, packageInfo) } returns rInputStream
|
||||
coEvery {
|
||||
fullRestorePlugin.getInputStreamForPackage(
|
||||
token,
|
||||
packageInfo
|
||||
)
|
||||
} returns rInputStream
|
||||
every { outputFactory.getOutputStream(fileDescriptor) } returns rOutputStream
|
||||
|
||||
// restore data
|
||||
|
|
|
@ -11,10 +11,12 @@ import com.stevesoltys.seedvault.getRandomString
|
|||
import com.stevesoltys.seedvault.metadata.PackageMetadata
|
||||
import com.stevesoltys.seedvault.metadata.PackageState.UNKNOWN_ERROR
|
||||
import io.mockk.Runs
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkStatic
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertArrayEquals
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertNull
|
||||
|
@ -30,10 +32,11 @@ import java.nio.file.Path
|
|||
import kotlin.random.Random
|
||||
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class ApkBackupTest : BackupTest() {
|
||||
|
||||
private val pm: PackageManager = mockk()
|
||||
private val streamGetter: () -> OutputStream = mockk()
|
||||
private val streamGetter: suspend () -> OutputStream = mockk()
|
||||
|
||||
private val apkBackup = ApkBackup(pm, settingsManager, metadataManager)
|
||||
|
||||
|
@ -51,20 +54,20 @@ internal class ApkBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `does not back up @pm@`() {
|
||||
fun `does not back up @pm@`() = runBlocking {
|
||||
val packageInfo = PackageInfo().apply { packageName = MAGIC_PACKAGE_MANAGER }
|
||||
assertNull(apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, streamGetter))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `does not back up when setting disabled`() {
|
||||
fun `does not back up when setting disabled`() = runBlocking {
|
||||
every { settingsManager.backupApks() } returns false
|
||||
|
||||
assertNull(apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, streamGetter))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `does not back up system apps`() {
|
||||
fun `does not back up system apps`() = runBlocking {
|
||||
packageInfo.applicationInfo.flags = FLAG_SYSTEM
|
||||
|
||||
every { settingsManager.backupApks() } returns true
|
||||
|
@ -73,7 +76,7 @@ internal class ApkBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `does not back up the same version`() {
|
||||
fun `does not back up the same version`() = runBlocking {
|
||||
packageInfo.applicationInfo.flags = FLAG_UPDATED_SYSTEM_APP
|
||||
val packageMetadata = packageMetadata.copy(
|
||||
version = packageInfo.longVersionCode
|
||||
|
@ -91,12 +94,14 @@ internal class ApkBackupTest : BackupTest() {
|
|||
expectChecks()
|
||||
|
||||
assertThrows(IOException::class.java) {
|
||||
assertNull(apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, streamGetter))
|
||||
runBlocking {
|
||||
assertNull(apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, streamGetter))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `do not accept empty signature`() {
|
||||
fun `do not accept empty signature`() = runBlocking {
|
||||
every { settingsManager.backupApks() } returns true
|
||||
every { metadataManager.getPackageMetadata(packageInfo.packageName) } returns packageMetadata
|
||||
every { sigInfo.hasMultipleSigners() } returns false
|
||||
|
@ -106,7 +111,7 @@ internal class ApkBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `test successful APK backup`(@TempDir tmpDir: Path) {
|
||||
fun `test successful APK backup`(@TempDir tmpDir: Path) = runBlocking {
|
||||
val apkBytes = byteArrayOf(0x04, 0x05, 0x06)
|
||||
val tmpFile = File(tmpDir.toAbsolutePath().toString())
|
||||
packageInfo.applicationInfo.sourceDir = File(tmpFile, "test.apk").apply {
|
||||
|
@ -124,7 +129,7 @@ internal class ApkBackupTest : BackupTest() {
|
|||
)
|
||||
|
||||
expectChecks()
|
||||
every { streamGetter.invoke() } returns apkOutputStream
|
||||
coEvery { streamGetter.invoke() } returns apkOutputStream
|
||||
every { pm.getInstallerPackageName(packageInfo.packageName) } returns updatedMetadata.installer
|
||||
every { metadataManager.onApkBackedUp(packageInfo, updatedMetadata, outputStream) } just Runs
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import android.os.ParcelFileDescriptor
|
|||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.stevesoltys.seedvault.BackupNotificationManager
|
||||
import com.stevesoltys.seedvault.MAGIC_PACKAGE_MANAGER
|
||||
import com.stevesoltys.seedvault.coAssertThrows
|
||||
import com.stevesoltys.seedvault.getRandomString
|
||||
import com.stevesoltys.seedvault.metadata.PackageMetadata
|
||||
import com.stevesoltys.seedvault.metadata.PackageState.NOT_ALLOWED
|
||||
|
@ -18,19 +19,22 @@ import com.stevesoltys.seedvault.metadata.PackageState.QUOTA_EXCEEDED
|
|||
import com.stevesoltys.seedvault.metadata.PackageState.UNKNOWN_ERROR
|
||||
import com.stevesoltys.seedvault.settings.Storage
|
||||
import io.mockk.Runs
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.IOException
|
||||
import java.io.OutputStream
|
||||
import kotlin.random.Random
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class BackupCoordinatorTest : BackupTest() {
|
||||
|
||||
private val plugin = mockk<BackupPlugin>()
|
||||
|
@ -40,7 +44,18 @@ internal class BackupCoordinatorTest : BackupTest() {
|
|||
private val packageService: PackageService = mockk()
|
||||
private val notificationManager = mockk<BackupNotificationManager>()
|
||||
|
||||
private val backup = BackupCoordinator(context, plugin, kv, full, apkBackup, clock, packageService, metadataManager, settingsManager, notificationManager)
|
||||
private val backup = BackupCoordinator(
|
||||
context,
|
||||
plugin,
|
||||
kv,
|
||||
full,
|
||||
apkBackup,
|
||||
clock,
|
||||
packageService,
|
||||
metadataManager,
|
||||
settingsManager,
|
||||
notificationManager
|
||||
)
|
||||
|
||||
private val metadataOutputStream = mockk<OutputStream>()
|
||||
private val fileDescriptor: ParcelFileDescriptor = mockk()
|
||||
|
@ -48,22 +63,25 @@ internal class BackupCoordinatorTest : BackupTest() {
|
|||
private val storage = Storage(Uri.EMPTY, getRandomString(), false)
|
||||
|
||||
@Test
|
||||
fun `device initialization succeeds and delegates to plugin`() {
|
||||
fun `device initialization succeeds and delegates to plugin`() = runBlocking {
|
||||
every { clock.time() } returns token
|
||||
every { plugin.initializeDevice(token) } returns true // TODO test when false
|
||||
every { plugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
coEvery { plugin.initializeDevice(token) } returns true // TODO test when false
|
||||
coEvery { plugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every { metadataManager.onDeviceInitialization(token, metadataOutputStream) } just Runs
|
||||
every { kv.hasState() } returns false
|
||||
every { full.hasState() } returns false
|
||||
every { metadataOutputStream.close() } just Runs
|
||||
|
||||
assertEquals(TRANSPORT_OK, backup.initializeDevice())
|
||||
assertEquals(TRANSPORT_OK, backup.finishBackup())
|
||||
|
||||
verify { metadataOutputStream.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `device initialization does no-op when already initialized`() {
|
||||
fun `device initialization does no-op when already initialized`() = runBlocking {
|
||||
every { clock.time() } returns token
|
||||
every { plugin.initializeDevice(token) } returns false
|
||||
coEvery { plugin.initializeDevice(token) } returns false
|
||||
every { kv.hasState() } returns false
|
||||
every { full.hasState() } returns false
|
||||
|
||||
|
@ -72,9 +90,9 @@ internal class BackupCoordinatorTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `error notification when device initialization fails`() {
|
||||
fun `error notification when device initialization fails`() = runBlocking {
|
||||
every { clock.time() } returns token
|
||||
every { plugin.initializeDevice(token) } throws IOException()
|
||||
coEvery { plugin.initializeDevice(token) } throws IOException()
|
||||
every { settingsManager.getStorage() } returns storage
|
||||
every { notificationManager.onBackupError() } just Runs
|
||||
|
||||
|
@ -83,35 +101,36 @@ internal class BackupCoordinatorTest : BackupTest() {
|
|||
// finish will only be called when TRANSPORT_OK is returned, so it should throw
|
||||
every { kv.hasState() } returns false
|
||||
every { full.hasState() } returns false
|
||||
assertThrows(IllegalStateException::class.java) {
|
||||
coAssertThrows(IllegalStateException::class.java) {
|
||||
backup.finishBackup()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `no error notification when device initialization fails on unplugged USB storage`() {
|
||||
val storage = mockk<Storage>()
|
||||
val documentFile = mockk<DocumentFile>()
|
||||
fun `no error notification when device initialization fails on unplugged USB storage`() =
|
||||
runBlocking {
|
||||
val storage = mockk<Storage>()
|
||||
val documentFile = mockk<DocumentFile>()
|
||||
|
||||
every { clock.time() } returns token
|
||||
every { plugin.initializeDevice(token) } throws IOException()
|
||||
every { settingsManager.getStorage() } returns storage
|
||||
every { storage.isUsb } returns true
|
||||
every { storage.getDocumentFile(context) } returns documentFile
|
||||
every { documentFile.isDirectory } returns false
|
||||
every { clock.time() } returns token
|
||||
coEvery { plugin.initializeDevice(token) } throws IOException()
|
||||
every { settingsManager.getStorage() } returns storage
|
||||
every { storage.isUsb } returns true
|
||||
every { storage.getDocumentFile(context) } returns documentFile
|
||||
every { documentFile.isDirectory } returns false
|
||||
|
||||
assertEquals(TRANSPORT_ERROR, backup.initializeDevice())
|
||||
assertEquals(TRANSPORT_ERROR, backup.initializeDevice())
|
||||
|
||||
// finish will only be called when TRANSPORT_OK is returned, so it should throw
|
||||
every { kv.hasState() } returns false
|
||||
every { full.hasState() } returns false
|
||||
assertThrows(IllegalStateException::class.java) {
|
||||
backup.finishBackup()
|
||||
// finish will only be called when TRANSPORT_OK is returned, so it should throw
|
||||
every { kv.hasState() } returns false
|
||||
every { full.hasState() } returns false
|
||||
coAssertThrows(IllegalStateException::class.java) {
|
||||
backup.finishBackup()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBackupQuota() delegates to right plugin`() {
|
||||
fun `getBackupQuota() delegates to right plugin`() = runBlocking {
|
||||
val isFullBackup = Random.nextBoolean()
|
||||
val quota = Random.nextLong()
|
||||
|
||||
|
@ -121,7 +140,10 @@ internal class BackupCoordinatorTest : BackupTest() {
|
|||
} else {
|
||||
every { kv.getQuota() } returns quota
|
||||
}
|
||||
every { metadataOutputStream.close() } just Runs
|
||||
assertEquals(quota, backup.getBackupQuota(packageInfo.packageName, isFullBackup))
|
||||
|
||||
verify { metadataOutputStream.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -139,24 +161,24 @@ internal class BackupCoordinatorTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `clearing KV backup data throws`() {
|
||||
every { kv.clearBackupData(packageInfo) } throws IOException()
|
||||
fun `clearing KV backup data throws`() = runBlocking {
|
||||
coEvery { kv.clearBackupData(packageInfo) } throws IOException()
|
||||
|
||||
assertEquals(TRANSPORT_ERROR, backup.clearBackupData(packageInfo))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `clearing full backup data throws`() {
|
||||
every { kv.clearBackupData(packageInfo) } just Runs
|
||||
every { full.clearBackupData(packageInfo) } throws IOException()
|
||||
fun `clearing full backup data throws`() = runBlocking {
|
||||
coEvery { kv.clearBackupData(packageInfo) } just Runs
|
||||
coEvery { full.clearBackupData(packageInfo) } throws IOException()
|
||||
|
||||
assertEquals(TRANSPORT_ERROR, backup.clearBackupData(packageInfo))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `clearing backup data succeeds`() {
|
||||
every { kv.clearBackupData(packageInfo) } just Runs
|
||||
every { full.clearBackupData(packageInfo) } just Runs
|
||||
fun `clearing backup data succeeds`() = runBlocking {
|
||||
coEvery { kv.clearBackupData(packageInfo) } just Runs
|
||||
coEvery { full.clearBackupData(packageInfo) } just Runs
|
||||
|
||||
assertEquals(TRANSPORT_OK, backup.clearBackupData(packageInfo))
|
||||
|
||||
|
@ -167,81 +189,112 @@ internal class BackupCoordinatorTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `finish backup delegates to KV plugin if it has state`() {
|
||||
fun `finish backup delegates to KV plugin if it has state`() = runBlocking {
|
||||
val result = Random.nextInt()
|
||||
|
||||
every { kv.hasState() } returns true
|
||||
every { full.hasState() } returns false
|
||||
every { kv.getCurrentPackage() } returns packageInfo
|
||||
every { plugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
coEvery { plugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every { metadataManager.onPackageBackedUp(packageInfo, metadataOutputStream) } just Runs
|
||||
every { kv.finishBackup() } returns result
|
||||
every { metadataOutputStream.close() } just Runs
|
||||
|
||||
assertEquals(result, backup.finishBackup())
|
||||
|
||||
verify { metadataOutputStream.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `finish backup delegates to full plugin if it has state`() {
|
||||
fun `finish backup delegates to full plugin if it has state`() = runBlocking {
|
||||
val result = Random.nextInt()
|
||||
|
||||
every { kv.hasState() } returns false
|
||||
every { full.hasState() } returns true
|
||||
every { full.getCurrentPackage() } returns packageInfo
|
||||
every { plugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
coEvery { plugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every { metadataManager.onPackageBackedUp(packageInfo, metadataOutputStream) } just Runs
|
||||
every { full.finishBackup() } returns result
|
||||
every { metadataOutputStream.close() } just Runs
|
||||
|
||||
assertEquals(result, backup.finishBackup())
|
||||
|
||||
verify { metadataOutputStream.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `metadata does not get updated when no APK was backed up`() {
|
||||
every { full.performFullBackup(packageInfo, fileDescriptor, 0) } returns TRANSPORT_OK
|
||||
every { apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, any()) } returns null
|
||||
fun `metadata does not get updated when no APK was backed up`() = runBlocking {
|
||||
coEvery { full.performFullBackup(packageInfo, fileDescriptor, 0) } returns TRANSPORT_OK
|
||||
coEvery { apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, any()) } returns null
|
||||
|
||||
assertEquals(TRANSPORT_OK, backup.performFullBackup(packageInfo, fileDescriptor, 0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `app exceeding quota gets cancelled and reason written to metadata`() {
|
||||
every { full.performFullBackup(packageInfo, fileDescriptor, 0) } returns TRANSPORT_OK
|
||||
fun `app exceeding quota gets cancelled and reason written to metadata`() = runBlocking {
|
||||
coEvery { full.performFullBackup(packageInfo, fileDescriptor, 0) } returns TRANSPORT_OK
|
||||
expectApkBackupAndMetadataWrite()
|
||||
every { full.getQuota() } returns DEFAULT_QUOTA_FULL_BACKUP
|
||||
every { full.checkFullBackupSize(DEFAULT_QUOTA_FULL_BACKUP + 1) } returns TRANSPORT_QUOTA_EXCEEDED
|
||||
every { full.getCurrentPackage() } returns packageInfo
|
||||
every { metadataManager.onPackageBackupError(packageInfo, QUOTA_EXCEEDED, metadataOutputStream) } just Runs
|
||||
every { full.cancelFullBackup() } just Runs
|
||||
every {
|
||||
metadataManager.onPackageBackupError(
|
||||
packageInfo,
|
||||
QUOTA_EXCEEDED,
|
||||
metadataOutputStream
|
||||
)
|
||||
} just Runs
|
||||
coEvery { full.cancelFullBackup() } just Runs
|
||||
every { settingsManager.getStorage() } returns storage
|
||||
every { metadataOutputStream.close() } just Runs
|
||||
|
||||
assertEquals(TRANSPORT_OK,
|
||||
backup.performFullBackup(packageInfo, fileDescriptor, 0))
|
||||
assertEquals(DEFAULT_QUOTA_FULL_BACKUP,
|
||||
backup.getBackupQuota(packageInfo.packageName, true))
|
||||
assertEquals(TRANSPORT_QUOTA_EXCEEDED,
|
||||
backup.checkFullBackupSize(DEFAULT_QUOTA_FULL_BACKUP + 1))
|
||||
assertEquals(
|
||||
TRANSPORT_OK,
|
||||
backup.performFullBackup(packageInfo, fileDescriptor, 0)
|
||||
)
|
||||
assertEquals(
|
||||
DEFAULT_QUOTA_FULL_BACKUP,
|
||||
backup.getBackupQuota(packageInfo.packageName, true)
|
||||
)
|
||||
assertEquals(
|
||||
TRANSPORT_QUOTA_EXCEEDED,
|
||||
backup.checkFullBackupSize(DEFAULT_QUOTA_FULL_BACKUP + 1)
|
||||
)
|
||||
backup.cancelFullBackup()
|
||||
assertEquals(0L, backup.requestFullBackupTime())
|
||||
|
||||
verify(exactly = 1) {
|
||||
metadataManager.onPackageBackupError(packageInfo, QUOTA_EXCEEDED, metadataOutputStream)
|
||||
}
|
||||
verify { metadataOutputStream.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `app with no data gets cancelled and reason written to metadata`() {
|
||||
every { full.performFullBackup(packageInfo, fileDescriptor, 0) } returns TRANSPORT_OK
|
||||
fun `app with no data gets cancelled and reason written to metadata`() = runBlocking {
|
||||
coEvery { full.performFullBackup(packageInfo, fileDescriptor, 0) } returns TRANSPORT_OK
|
||||
expectApkBackupAndMetadataWrite()
|
||||
every { full.getQuota() } returns DEFAULT_QUOTA_FULL_BACKUP
|
||||
every { full.checkFullBackupSize(0) } returns TRANSPORT_PACKAGE_REJECTED
|
||||
every { full.getCurrentPackage() } returns packageInfo
|
||||
every { metadataManager.onPackageBackupError(packageInfo, NO_DATA, metadataOutputStream) } just Runs
|
||||
every { full.cancelFullBackup() } just Runs
|
||||
every {
|
||||
metadataManager.onPackageBackupError(
|
||||
packageInfo,
|
||||
NO_DATA,
|
||||
metadataOutputStream
|
||||
)
|
||||
} just Runs
|
||||
coEvery { full.cancelFullBackup() } just Runs
|
||||
every { settingsManager.getStorage() } returns storage
|
||||
every { metadataOutputStream.close() } just Runs
|
||||
|
||||
assertEquals(TRANSPORT_OK,
|
||||
backup.performFullBackup(packageInfo, fileDescriptor, 0))
|
||||
assertEquals(DEFAULT_QUOTA_FULL_BACKUP,
|
||||
backup.getBackupQuota(packageInfo.packageName, true))
|
||||
assertEquals(
|
||||
TRANSPORT_OK,
|
||||
backup.performFullBackup(packageInfo, fileDescriptor, 0)
|
||||
)
|
||||
assertEquals(
|
||||
DEFAULT_QUOTA_FULL_BACKUP,
|
||||
backup.getBackupQuota(packageInfo.packageName, true)
|
||||
)
|
||||
assertEquals(TRANSPORT_PACKAGE_REJECTED, backup.checkFullBackupSize(0))
|
||||
backup.cancelFullBackup()
|
||||
assertEquals(0L, backup.requestFullBackupTime())
|
||||
|
@ -249,41 +302,76 @@ internal class BackupCoordinatorTest : BackupTest() {
|
|||
verify(exactly = 1) {
|
||||
metadataManager.onPackageBackupError(packageInfo, NO_DATA, metadataOutputStream)
|
||||
}
|
||||
verify { metadataOutputStream.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `not allowed apps get their APKs backed up during @pm@ backup`() {
|
||||
fun `not allowed apps get their APKs backed up during @pm@ backup`() = runBlocking {
|
||||
val packageInfo = PackageInfo().apply { packageName = MAGIC_PACKAGE_MANAGER }
|
||||
val notAllowedPackages = listOf(
|
||||
PackageInfo().apply { packageName = "org.example.1" },
|
||||
PackageInfo().apply { packageName = "org.example.2" }
|
||||
PackageInfo().apply { packageName = "org.example.1" },
|
||||
PackageInfo().apply { packageName = "org.example.2" }
|
||||
)
|
||||
val packageMetadata: PackageMetadata = mockk()
|
||||
|
||||
every { settingsManager.getStorage() } returns storage // to check for removable storage
|
||||
every { settingsManager.getStorage() } returns storage // to check for removable storage
|
||||
every { packageService.notAllowedPackages } returns notAllowedPackages
|
||||
// no backup needed
|
||||
every { apkBackup.backupApkIfNecessary(notAllowedPackages[0], NOT_ALLOWED, any()) } returns null
|
||||
coEvery {
|
||||
apkBackup.backupApkIfNecessary(
|
||||
notAllowedPackages[0],
|
||||
NOT_ALLOWED,
|
||||
any()
|
||||
)
|
||||
} returns null
|
||||
// was backed up, get new packageMetadata
|
||||
every { apkBackup.backupApkIfNecessary(notAllowedPackages[1], NOT_ALLOWED, any()) } returns packageMetadata
|
||||
every { plugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every { metadataManager.onApkBackedUp(notAllowedPackages[1], packageMetadata, metadataOutputStream) } just Runs
|
||||
coEvery {
|
||||
apkBackup.backupApkIfNecessary(
|
||||
notAllowedPackages[1],
|
||||
NOT_ALLOWED,
|
||||
any()
|
||||
)
|
||||
} returns packageMetadata
|
||||
coEvery { plugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every {
|
||||
metadataManager.onApkBackedUp(
|
||||
notAllowedPackages[1],
|
||||
packageMetadata,
|
||||
metadataOutputStream
|
||||
)
|
||||
} just Runs
|
||||
// do actual @pm@ backup
|
||||
every { kv.performBackup(packageInfo, fileDescriptor, 0) } returns TRANSPORT_OK
|
||||
coEvery { kv.performBackup(packageInfo, fileDescriptor, 0) } returns TRANSPORT_OK
|
||||
every { metadataOutputStream.close() } just Runs
|
||||
|
||||
assertEquals(TRANSPORT_OK,
|
||||
backup.performIncrementalBackup(packageInfo, fileDescriptor, 0))
|
||||
assertEquals(
|
||||
TRANSPORT_OK,
|
||||
backup.performIncrementalBackup(packageInfo, fileDescriptor, 0)
|
||||
)
|
||||
|
||||
verify {
|
||||
coVerify {
|
||||
apkBackup.backupApkIfNecessary(notAllowedPackages[0], NOT_ALLOWED, any())
|
||||
apkBackup.backupApkIfNecessary(notAllowedPackages[1], NOT_ALLOWED, any())
|
||||
metadataOutputStream.close()
|
||||
}
|
||||
}
|
||||
|
||||
private fun expectApkBackupAndMetadataWrite() {
|
||||
every { apkBackup.backupApkIfNecessary(any(), UNKNOWN_ERROR, any()) } returns packageMetadata
|
||||
every { plugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every { metadataManager.onApkBackedUp(any(), packageMetadata, metadataOutputStream) } just Runs
|
||||
coEvery {
|
||||
apkBackup.backupApkIfNecessary(
|
||||
any(),
|
||||
UNKNOWN_ERROR,
|
||||
any()
|
||||
)
|
||||
} returns packageMetadata
|
||||
coEvery { plugin.getMetadataOutputStream() } returns metadataOutputStream
|
||||
every {
|
||||
metadataManager.onApkBackedUp(
|
||||
any(),
|
||||
packageMetadata,
|
||||
metadataOutputStream
|
||||
)
|
||||
} just Runs
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,9 +5,11 @@ import android.app.backup.BackupTransport.TRANSPORT_OK
|
|||
import android.app.backup.BackupTransport.TRANSPORT_PACKAGE_REJECTED
|
||||
import android.app.backup.BackupTransport.TRANSPORT_QUOTA_EXCEEDED
|
||||
import io.mockk.Runs
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
|
@ -16,6 +18,7 @@ import java.io.FileInputStream
|
|||
import java.io.IOException
|
||||
import kotlin.random.Random
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class FullBackupTest : BackupTest() {
|
||||
|
||||
private val plugin = mockk<FullBackupPlugin>()
|
||||
|
@ -62,7 +65,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `performFullBackup runs ok`() {
|
||||
fun `performFullBackup runs ok`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectClearState()
|
||||
|
||||
|
@ -73,7 +76,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `sendBackupData first call over quota`() {
|
||||
fun `sendBackupData first call over quota`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
val numBytes = (quota + 1).toInt()
|
||||
|
@ -89,7 +92,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `sendBackupData second call over quota`() {
|
||||
fun `sendBackupData second call over quota`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
val numBytes1 = quota.toInt()
|
||||
|
@ -109,7 +112,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `sendBackupData throws exception when reading from InputStream`() {
|
||||
fun `sendBackupData throws exception when reading from InputStream`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
every { plugin.getQuota() } returns quota
|
||||
|
@ -125,11 +128,11 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `sendBackupData throws exception when getting outputStream`() {
|
||||
fun `sendBackupData throws exception when getting outputStream`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
|
||||
every { plugin.getQuota() } returns quota
|
||||
every { plugin.getOutputStream(packageInfo) } throws IOException()
|
||||
coEvery { plugin.getOutputStream(packageInfo) } throws IOException()
|
||||
expectClearState()
|
||||
|
||||
assertEquals(TRANSPORT_OK, backup.performFullBackup(packageInfo, data))
|
||||
|
@ -141,11 +144,11 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `sendBackupData throws exception when writing header`() {
|
||||
fun `sendBackupData throws exception when writing header`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
|
||||
every { plugin.getQuota() } returns quota
|
||||
every { plugin.getOutputStream(packageInfo) } returns outputStream
|
||||
coEvery { plugin.getOutputStream(packageInfo) } returns outputStream
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
every { headerWriter.writeVersion(outputStream, header) } throws IOException()
|
||||
expectClearState()
|
||||
|
@ -159,7 +162,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `sendBackupData throws exception when writing encrypted data to OutputStream`() {
|
||||
fun `sendBackupData throws exception when writing encrypted data to OutputStream`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
every { plugin.getQuota() } returns quota
|
||||
|
@ -176,7 +179,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `sendBackupData runs ok`() {
|
||||
fun `sendBackupData runs ok`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
val numBytes1 = (quota / 2).toInt()
|
||||
|
@ -196,18 +199,18 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `clearBackupData delegates to plugin`() {
|
||||
every { plugin.removeDataOfPackage(packageInfo) } just Runs
|
||||
fun `clearBackupData delegates to plugin`() = runBlocking {
|
||||
coEvery { plugin.removeDataOfPackage(packageInfo) } just Runs
|
||||
|
||||
backup.clearBackupData(packageInfo)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `cancel full backup runs ok`() {
|
||||
fun `cancel full backup runs ok`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
expectClearState()
|
||||
every { plugin.removeDataOfPackage(packageInfo) } just Runs
|
||||
coEvery { plugin.removeDataOfPackage(packageInfo) } just Runs
|
||||
|
||||
assertEquals(TRANSPORT_OK, backup.performFullBackup(packageInfo, data))
|
||||
assertTrue(backup.hasState())
|
||||
|
@ -216,11 +219,11 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `cancel full backup ignores exception when calling plugin`() {
|
||||
fun `cancel full backup ignores exception when calling plugin`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
expectClearState()
|
||||
every { plugin.removeDataOfPackage(packageInfo) } throws IOException()
|
||||
coEvery { plugin.removeDataOfPackage(packageInfo) } throws IOException()
|
||||
|
||||
assertEquals(TRANSPORT_OK, backup.performFullBackup(packageInfo, data))
|
||||
assertTrue(backup.hasState())
|
||||
|
@ -229,7 +232,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `clearState throws exception when flushing OutputStream`() {
|
||||
fun `clearState throws exception when flushing OutputStream`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
val numBytes = 42
|
||||
|
@ -245,7 +248,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `clearState ignores exception when closing OutputStream`() {
|
||||
fun `clearState ignores exception when closing OutputStream`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
every { outputStream.flush() } just Runs
|
||||
|
@ -260,7 +263,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `clearState ignores exception when closing InputStream`() {
|
||||
fun `clearState ignores exception when closing InputStream`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
every { outputStream.flush() } just Runs
|
||||
|
@ -275,7 +278,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `clearState ignores exception when closing ParcelFileDescriptor`() {
|
||||
fun `clearState ignores exception when closing ParcelFileDescriptor`() = runBlocking {
|
||||
every { inputFactory.getInputStream(data) } returns inputStream
|
||||
expectInitializeOutputStream()
|
||||
every { outputStream.flush() } just Runs
|
||||
|
@ -290,7 +293,7 @@ internal class FullBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
private fun expectInitializeOutputStream() {
|
||||
every { plugin.getOutputStream(packageInfo) } returns outputStream
|
||||
coEvery { plugin.getOutputStream(packageInfo) } returns outputStream
|
||||
every { headerWriter.writeVersion(outputStream, header) } just Runs
|
||||
every { crypto.encryptHeader(outputStream, header) } just Runs
|
||||
}
|
||||
|
|
|
@ -11,17 +11,21 @@ import com.stevesoltys.seedvault.getRandomString
|
|||
import com.stevesoltys.seedvault.header.MAX_KEY_LENGTH_SIZE
|
||||
import com.stevesoltys.seedvault.header.VersionHeader
|
||||
import io.mockk.Runs
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
import java.util.Base64
|
||||
import kotlin.random.Random
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class KVBackupTest : BackupTest() {
|
||||
|
||||
private val plugin = mockk<KVBackupPlugin>()
|
||||
|
@ -40,7 +44,7 @@ internal class KVBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `simple backup with one record`() {
|
||||
fun `simple backup with one record`() = runBlocking {
|
||||
singleRecordBackup()
|
||||
|
||||
assertEquals(TRANSPORT_OK, backup.performBackup(packageInfo, data, 0))
|
||||
|
@ -50,25 +54,28 @@ internal class KVBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `incremental backup with no data gets rejected`() {
|
||||
every { plugin.hasDataForPackage(packageInfo) } returns false
|
||||
fun `incremental backup with no data gets rejected`() = runBlocking {
|
||||
coEvery { plugin.hasDataForPackage(packageInfo) } returns false
|
||||
|
||||
assertEquals(TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED, backup.performBackup(packageInfo, data, FLAG_INCREMENTAL))
|
||||
assertEquals(
|
||||
TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED,
|
||||
backup.performBackup(packageInfo, data, FLAG_INCREMENTAL)
|
||||
)
|
||||
assertFalse(backup.hasState())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `check for existing data throws exception`() {
|
||||
every { plugin.hasDataForPackage(packageInfo) } throws IOException()
|
||||
fun `check for existing data throws exception`() = runBlocking {
|
||||
coEvery { plugin.hasDataForPackage(packageInfo) } throws IOException()
|
||||
|
||||
assertEquals(TRANSPORT_ERROR, backup.performBackup(packageInfo, data, 0))
|
||||
assertFalse(backup.hasState())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `non-incremental backup with data clears old data first`() {
|
||||
fun `non-incremental backup with data clears old data first`() = runBlocking {
|
||||
singleRecordBackup(true)
|
||||
every { plugin.removeDataOfPackage(packageInfo) } just Runs
|
||||
coEvery { plugin.removeDataOfPackage(packageInfo) } just Runs
|
||||
|
||||
assertEquals(TRANSPORT_OK, backup.performBackup(packageInfo, data, FLAG_NON_INCREMENTAL))
|
||||
assertTrue(backup.hasState())
|
||||
|
@ -77,27 +84,31 @@ internal class KVBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `ignoring exception when clearing data when non-incremental backup has data`() {
|
||||
singleRecordBackup(true)
|
||||
every { plugin.removeDataOfPackage(packageInfo) } throws IOException()
|
||||
fun `ignoring exception when clearing data when non-incremental backup has data`() =
|
||||
runBlocking {
|
||||
singleRecordBackup(true)
|
||||
coEvery { plugin.removeDataOfPackage(packageInfo) } throws IOException()
|
||||
|
||||
assertEquals(TRANSPORT_OK, backup.performBackup(packageInfo, data, FLAG_NON_INCREMENTAL))
|
||||
assertTrue(backup.hasState())
|
||||
assertEquals(TRANSPORT_OK, backup.finishBackup())
|
||||
assertFalse(backup.hasState())
|
||||
}
|
||||
assertEquals(
|
||||
TRANSPORT_OK,
|
||||
backup.performBackup(packageInfo, data, FLAG_NON_INCREMENTAL)
|
||||
)
|
||||
assertTrue(backup.hasState())
|
||||
assertEquals(TRANSPORT_OK, backup.finishBackup())
|
||||
assertFalse(backup.hasState())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ensuring storage throws exception`() {
|
||||
every { plugin.hasDataForPackage(packageInfo) } returns false
|
||||
every { plugin.ensureRecordStorageForPackage(packageInfo) } throws IOException()
|
||||
fun `ensuring storage throws exception`() = runBlocking {
|
||||
coEvery { plugin.hasDataForPackage(packageInfo) } returns false
|
||||
coEvery { plugin.ensureRecordStorageForPackage(packageInfo) } throws IOException()
|
||||
|
||||
assertEquals(TRANSPORT_ERROR, backup.performBackup(packageInfo, data, 0))
|
||||
assertFalse(backup.hasState())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `exception while reading next header`() {
|
||||
fun `exception while reading next header`() = runBlocking {
|
||||
initPlugin(false)
|
||||
createBackupDataInput()
|
||||
every { dataInput.readNextHeader() } throws IOException()
|
||||
|
@ -107,7 +118,7 @@ internal class KVBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `exception while reading value`() {
|
||||
fun `exception while reading value`() = runBlocking {
|
||||
initPlugin(false)
|
||||
createBackupDataInput()
|
||||
every { dataInput.readNextHeader() } returns true
|
||||
|
@ -120,7 +131,7 @@ internal class KVBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `no data records`() {
|
||||
fun `no data records`() = runBlocking {
|
||||
initPlugin(false)
|
||||
getDataInput(listOf(false))
|
||||
|
||||
|
@ -131,43 +142,52 @@ internal class KVBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `exception while writing version header`() {
|
||||
fun `exception while writing version header`() = runBlocking {
|
||||
initPlugin(false)
|
||||
getDataInput(listOf(true))
|
||||
every { plugin.getOutputStreamForRecord(packageInfo, key64) } returns outputStream
|
||||
coEvery { plugin.getOutputStreamForRecord(packageInfo, key64) } returns outputStream
|
||||
every { headerWriter.writeVersion(outputStream, versionHeader) } throws IOException()
|
||||
every { outputStream.close() } just Runs
|
||||
|
||||
assertEquals(TRANSPORT_ERROR, backup.performBackup(packageInfo, data, 0))
|
||||
assertFalse(backup.hasState())
|
||||
|
||||
verify { outputStream.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `exception while writing encrypted value to output stream`() {
|
||||
fun `exception while writing encrypted value to output stream`() = runBlocking {
|
||||
initPlugin(false)
|
||||
getDataInput(listOf(true))
|
||||
writeHeaderAndEncrypt()
|
||||
every { plugin.getOutputStreamForRecord(packageInfo, key64) } returns outputStream
|
||||
coEvery { plugin.getOutputStreamForRecord(packageInfo, key64) } returns outputStream
|
||||
every { headerWriter.writeVersion(outputStream, versionHeader) } just Runs
|
||||
every { crypto.encryptMultipleSegments(outputStream, any()) } throws IOException()
|
||||
every { outputStream.close() } just Runs
|
||||
|
||||
assertEquals(TRANSPORT_ERROR, backup.performBackup(packageInfo, data, 0))
|
||||
assertFalse(backup.hasState())
|
||||
|
||||
verify { outputStream.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `exception while flushing output stream`() {
|
||||
fun `exception while flushing output stream`() = runBlocking {
|
||||
initPlugin(false)
|
||||
getDataInput(listOf(true))
|
||||
writeHeaderAndEncrypt()
|
||||
every { outputStream.write(value) } just Runs
|
||||
every { outputStream.flush() } throws IOException()
|
||||
every { outputStream.close() } just Runs
|
||||
|
||||
assertEquals(TRANSPORT_ERROR, backup.performBackup(packageInfo, data, 0))
|
||||
assertFalse(backup.hasState())
|
||||
|
||||
verify { outputStream.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ignoring exception while closing output stream`() {
|
||||
fun `ignoring exception while closing output stream`() = runBlocking {
|
||||
initPlugin(false)
|
||||
getDataInput(listOf(true, false))
|
||||
writeHeaderAndEncrypt()
|
||||
|
@ -191,8 +211,8 @@ internal class KVBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
private fun initPlugin(hasDataForPackage: Boolean = false) {
|
||||
every { plugin.hasDataForPackage(packageInfo) } returns hasDataForPackage
|
||||
every { plugin.ensureRecordStorageForPackage(packageInfo) } just Runs
|
||||
coEvery { plugin.hasDataForPackage(packageInfo) } returns hasDataForPackage
|
||||
coEvery { plugin.ensureRecordStorageForPackage(packageInfo) } just Runs
|
||||
}
|
||||
|
||||
private fun createBackupDataInput() {
|
||||
|
@ -208,7 +228,7 @@ internal class KVBackupTest : BackupTest() {
|
|||
}
|
||||
|
||||
private fun writeHeaderAndEncrypt() {
|
||||
every { plugin.getOutputStreamForRecord(packageInfo, key64) } returns outputStream
|
||||
coEvery { plugin.getOutputStreamForRecord(packageInfo, key64) } returns outputStream
|
||||
every { headerWriter.writeVersion(outputStream, versionHeader) } just Runs
|
||||
every { crypto.encryptHeader(outputStream, versionHeader) } just Runs
|
||||
every { crypto.encryptMultipleSegments(outputStream, any()) } just Runs
|
||||
|
|
|
@ -15,6 +15,7 @@ import com.stevesoltys.seedvault.transport.restore.ApkRestoreStatus.FAILED
|
|||
import com.stevesoltys.seedvault.transport.restore.ApkRestoreStatus.IN_PROGRESS
|
||||
import com.stevesoltys.seedvault.transport.restore.ApkRestoreStatus.QUEUED
|
||||
import com.stevesoltys.seedvault.transport.restore.ApkRestoreStatus.SUCCEEDED
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
|
@ -31,6 +32,7 @@ import java.io.File
|
|||
import java.nio.file.Path
|
||||
import kotlin.random.Random
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
@ExperimentalCoroutinesApi
|
||||
internal class ApkRestoreTest : RestoreTest() {
|
||||
|
||||
|
@ -71,7 +73,7 @@ internal class ApkRestoreTest : RestoreTest() {
|
|||
val packageMetadataMap: PackageMetadataMap = hashMapOf(packageName to packageMetadata)
|
||||
|
||||
every { strictContext.cacheDir } returns File(tmpDir.toString())
|
||||
every { restorePlugin.getApkInputStream(token, packageName) } returns apkInputStream
|
||||
coEvery { restorePlugin.getApkInputStream(token, packageName) } returns apkInputStream
|
||||
|
||||
apkRestore.restore(token, packageMetadataMap).collectIndexed { index, value ->
|
||||
when (index) {
|
||||
|
@ -96,7 +98,7 @@ internal class ApkRestoreTest : RestoreTest() {
|
|||
packageInfo.packageName = getRandomString()
|
||||
|
||||
every { strictContext.cacheDir } returns File(tmpDir.toString())
|
||||
every { restorePlugin.getApkInputStream(token, packageName) } returns apkInputStream
|
||||
coEvery { restorePlugin.getApkInputStream(token, packageName) } returns apkInputStream
|
||||
every { pm.getPackageArchiveInfo(any(), any()) } returns packageInfo
|
||||
|
||||
apkRestore.restore(token, packageMetadataMap).collectIndexed { index, value ->
|
||||
|
@ -119,7 +121,7 @@ internal class ApkRestoreTest : RestoreTest() {
|
|||
@Test
|
||||
fun `test apkInstaller throws exceptions`(@TempDir tmpDir: Path) = runBlocking {
|
||||
every { strictContext.cacheDir } returns File(tmpDir.toString())
|
||||
every { restorePlugin.getApkInputStream(token, packageName) } returns apkInputStream
|
||||
coEvery { restorePlugin.getApkInputStream(token, packageName) } returns apkInputStream
|
||||
every { pm.getPackageArchiveInfo(any(), any()) } returns packageInfo
|
||||
every { pm.loadItemIcon(packageInfo.applicationInfo, packageInfo.applicationInfo) } returns icon
|
||||
every { pm.getApplicationLabel(packageInfo.applicationInfo) } returns appName
|
||||
|
@ -155,7 +157,7 @@ internal class ApkRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
every { strictContext.cacheDir } returns File(tmpDir.toString())
|
||||
every { restorePlugin.getApkInputStream(token, packageName) } returns apkInputStream
|
||||
coEvery { restorePlugin.getApkInputStream(token, packageName) } returns apkInputStream
|
||||
every { pm.getPackageArchiveInfo(any(), any()) } returns packageInfo
|
||||
every { pm.loadItemIcon(packageInfo.applicationInfo, packageInfo.applicationInfo) } returns icon
|
||||
every { pm.getApplicationLabel(packageInfo.applicationInfo) } returns appName
|
||||
|
@ -199,7 +201,7 @@ internal class ApkRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
every { strictContext.cacheDir } returns File(tmpDir.toString())
|
||||
every { restorePlugin.getApkInputStream(token, packageName) } returns apkInputStream
|
||||
coEvery { restorePlugin.getApkInputStream(token, packageName) } returns apkInputStream
|
||||
every { pm.getPackageArchiveInfo(any(), any()) } returns packageInfo
|
||||
every { pm.loadItemIcon(packageInfo.applicationInfo, packageInfo.applicationInfo) } returns icon
|
||||
every { packageInfo.applicationInfo.loadIcon(pm) } returns icon
|
||||
|
|
|
@ -4,18 +4,20 @@ import android.app.backup.BackupTransport.NO_MORE_DATA
|
|||
import android.app.backup.BackupTransport.TRANSPORT_ERROR
|
||||
import android.app.backup.BackupTransport.TRANSPORT_OK
|
||||
import android.app.backup.BackupTransport.TRANSPORT_PACKAGE_REJECTED
|
||||
import com.stevesoltys.seedvault.coAssertThrows
|
||||
import com.stevesoltys.seedvault.getRandomByteArray
|
||||
import com.stevesoltys.seedvault.header.UnsupportedVersionException
|
||||
import com.stevesoltys.seedvault.header.VERSION
|
||||
import com.stevesoltys.seedvault.header.VersionHeader
|
||||
import io.mockk.Runs
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertArrayEquals
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.ByteArrayOutputStream
|
||||
|
@ -23,6 +25,7 @@ import java.io.EOFException
|
|||
import java.io.IOException
|
||||
import kotlin.random.Random
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class FullRestoreTest : RestoreTest() {
|
||||
|
||||
private val plugin = mockk<FullRestorePlugin>()
|
||||
|
@ -38,9 +41,9 @@ internal class FullRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `hasDataForPackage() delegates to plugin`() {
|
||||
fun `hasDataForPackage() delegates to plugin`() = runBlocking {
|
||||
val result = Random.nextBoolean()
|
||||
every { plugin.hasDataForPackage(token, packageInfo) } returns result
|
||||
coEvery { plugin.hasDataForPackage(token, packageInfo) } returns result
|
||||
assertEquals(result, restore.hasDataForPackage(token, packageInfo))
|
||||
}
|
||||
|
||||
|
@ -54,45 +57,45 @@ internal class FullRestoreTest : RestoreTest() {
|
|||
@Test
|
||||
fun `getting chunks without initializing state throws`() {
|
||||
assertFalse(restore.hasState())
|
||||
assertThrows(IllegalStateException::class.java) {
|
||||
coAssertThrows(IllegalStateException::class.java) {
|
||||
restore.getNextFullRestoreDataChunk(fileDescriptor)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getting InputStream for package when getting first chunk throws`() {
|
||||
fun `getting InputStream for package when getting first chunk throws`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
every { plugin.getInputStreamForPackage(token, packageInfo) } throws IOException()
|
||||
coEvery { plugin.getInputStreamForPackage(token, packageInfo) } throws IOException()
|
||||
|
||||
assertEquals(TRANSPORT_PACKAGE_REJECTED, restore.getNextFullRestoreDataChunk(fileDescriptor))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `reading version header when getting first chunk throws`() {
|
||||
fun `reading version header when getting first chunk throws`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
every { plugin.getInputStreamForPackage(token, packageInfo) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForPackage(token, packageInfo) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } throws IOException()
|
||||
|
||||
assertEquals(TRANSPORT_PACKAGE_REJECTED, restore.getNextFullRestoreDataChunk(fileDescriptor))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `reading unsupported version when getting first chunk`() {
|
||||
fun `reading unsupported version when getting first chunk`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
every { plugin.getInputStreamForPackage(token, packageInfo) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForPackage(token, packageInfo) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } throws UnsupportedVersionException(unsupportedVersion)
|
||||
|
||||
assertEquals(TRANSPORT_PACKAGE_REJECTED, restore.getNextFullRestoreDataChunk(fileDescriptor))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `decrypting version header when getting first chunk throws`() {
|
||||
fun `decrypting version header when getting first chunk throws`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
every { plugin.getInputStreamForPackage(token, packageInfo) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForPackage(token, packageInfo) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream, VERSION, packageInfo.packageName) } throws IOException()
|
||||
|
||||
|
@ -100,10 +103,10 @@ internal class FullRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `decrypting version header when getting first chunk throws security exception`() {
|
||||
fun `decrypting version header when getting first chunk throws security exception`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
every { plugin.getInputStreamForPackage(token, packageInfo) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForPackage(token, packageInfo) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream, VERSION, packageInfo.packageName) } throws SecurityException()
|
||||
|
||||
|
@ -111,7 +114,7 @@ internal class FullRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `decrypting segment throws IOException`() {
|
||||
fun `decrypting segment throws IOException`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
initInputStream()
|
||||
|
@ -124,7 +127,7 @@ internal class FullRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `decrypting segment throws EOFException`() {
|
||||
fun `decrypting segment throws EOFException`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
initInputStream()
|
||||
|
@ -137,7 +140,7 @@ internal class FullRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `full chunk gets encrypted`() {
|
||||
fun `full chunk gets encrypted`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
initInputStream()
|
||||
|
@ -151,7 +154,7 @@ internal class FullRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `aborting full restore closes stream, resets state`() {
|
||||
fun `aborting full restore closes stream, resets state`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
initInputStream()
|
||||
|
@ -166,7 +169,7 @@ internal class FullRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
private fun initInputStream() {
|
||||
every { plugin.getInputStreamForPackage(token, packageInfo) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForPackage(token, packageInfo) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream, VERSION, packageInfo.packageName) } returns versionHeader
|
||||
}
|
||||
|
|
|
@ -3,23 +3,26 @@ package com.stevesoltys.seedvault.transport.restore
|
|||
import android.app.backup.BackupDataOutput
|
||||
import android.app.backup.BackupTransport.TRANSPORT_ERROR
|
||||
import android.app.backup.BackupTransport.TRANSPORT_OK
|
||||
import com.stevesoltys.seedvault.coAssertThrows
|
||||
import com.stevesoltys.seedvault.encodeBase64
|
||||
import com.stevesoltys.seedvault.getRandomByteArray
|
||||
import com.stevesoltys.seedvault.header.UnsupportedVersionException
|
||||
import com.stevesoltys.seedvault.header.VERSION
|
||||
import com.stevesoltys.seedvault.header.VersionHeader
|
||||
import io.mockk.Runs
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verifyAll
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import kotlin.random.Random
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class KVRestoreTest : RestoreTest() {
|
||||
|
||||
private val plugin = mockk<KVRestorePlugin>()
|
||||
|
@ -34,23 +37,23 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
private val versionHeader2 = VersionHeader(VERSION, packageInfo.packageName, key2)
|
||||
|
||||
@Test
|
||||
fun `hasDataForPackage() delegates to plugin`() {
|
||||
fun `hasDataForPackage() delegates to plugin`() = runBlocking {
|
||||
val result = Random.nextBoolean()
|
||||
|
||||
every { plugin.hasDataForPackage(token, packageInfo) } returns result
|
||||
coEvery { plugin.hasDataForPackage(token, packageInfo) } returns result
|
||||
|
||||
assertEquals(result, restore.hasDataForPackage(token, packageInfo))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getRestoreData() throws without initializing state`() {
|
||||
assertThrows(IllegalStateException::class.java) {
|
||||
coAssertThrows(IllegalStateException::class.java) {
|
||||
restore.getRestoreData(fileDescriptor)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `listing records throws`() {
|
||||
fun `listing records throws`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
every { plugin.listRecords(token, packageInfo) } throws IOException()
|
||||
|
@ -59,11 +62,11 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `reading VersionHeader with unsupported version throws`() {
|
||||
fun `reading VersionHeader with unsupported version throws`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
getRecordsAndOutput()
|
||||
every { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } throws UnsupportedVersionException(unsupportedVersion)
|
||||
streamsGetClosed()
|
||||
|
||||
|
@ -72,11 +75,11 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `error reading VersionHeader throws`() {
|
||||
fun `error reading VersionHeader throws`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
getRecordsAndOutput()
|
||||
every { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } throws IOException()
|
||||
streamsGetClosed()
|
||||
|
||||
|
@ -85,11 +88,11 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `decrypting segment throws`() {
|
||||
fun `decrypting segment throws`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
getRecordsAndOutput()
|
||||
every { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream, VERSION, packageInfo.packageName, key) } returns versionHeader
|
||||
every { crypto.decryptMultipleSegments(inputStream) } throws IOException()
|
||||
|
@ -100,11 +103,11 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `decrypting header throws`() {
|
||||
fun `decrypting header throws`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
getRecordsAndOutput()
|
||||
every { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream, VERSION, packageInfo.packageName, key) } throws IOException()
|
||||
streamsGetClosed()
|
||||
|
@ -114,11 +117,11 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `decrypting header throws security exception`() {
|
||||
fun `decrypting header throws security exception`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
getRecordsAndOutput()
|
||||
every { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream, VERSION, packageInfo.packageName, key) } throws SecurityException()
|
||||
streamsGetClosed()
|
||||
|
@ -128,11 +131,11 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `writing header throws`() {
|
||||
fun `writing header throws`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
getRecordsAndOutput()
|
||||
every { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream, VERSION, packageInfo.packageName, key) } returns versionHeader
|
||||
every { crypto.decryptMultipleSegments(inputStream) } returns data
|
||||
|
@ -144,11 +147,11 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `writing value throws`() {
|
||||
fun `writing value throws`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
getRecordsAndOutput()
|
||||
every { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream, VERSION, packageInfo.packageName, key) } returns versionHeader
|
||||
every { crypto.decryptMultipleSegments(inputStream) } returns data
|
||||
|
@ -161,11 +164,11 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `writing value succeeds`() {
|
||||
fun `writing value succeeds`() = runBlocking {
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
getRecordsAndOutput()
|
||||
every { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream, VERSION, packageInfo.packageName, key) } returns versionHeader
|
||||
every { crypto.decryptMultipleSegments(inputStream) } returns data
|
||||
|
@ -178,21 +181,21 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `writing two values succeeds`() {
|
||||
fun `writing two values succeeds`() = runBlocking {
|
||||
val data2 = getRandomByteArray()
|
||||
val inputStream2 = mockk<InputStream>()
|
||||
restore.initializeState(token, packageInfo)
|
||||
|
||||
getRecordsAndOutput(listOf(key64, key264))
|
||||
// first key/value
|
||||
every { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
coEvery { plugin.getInputStreamForRecord(token, packageInfo, key64) } returns inputStream
|
||||
every { headerReader.readVersion(inputStream) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream, VERSION, packageInfo.packageName, key) } returns versionHeader
|
||||
every { crypto.decryptMultipleSegments(inputStream) } returns data
|
||||
every { output.writeEntityHeader(key, data.size) } returns 42
|
||||
every { output.writeEntityData(data, data.size) } returns data.size
|
||||
// second key/value
|
||||
every { plugin.getInputStreamForRecord(token, packageInfo, key264) } returns inputStream2
|
||||
coEvery { plugin.getInputStreamForRecord(token, packageInfo, key264) } returns inputStream2
|
||||
every { headerReader.readVersion(inputStream2) } returns VERSION
|
||||
every { crypto.decryptHeader(inputStream2, VERSION, packageInfo.packageName, key2) } returns versionHeader2
|
||||
every { crypto.decryptMultipleSegments(inputStream2) } returns data2
|
||||
|
|
|
@ -10,6 +10,7 @@ import android.content.pm.PackageInfo
|
|||
import android.os.ParcelFileDescriptor
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.stevesoltys.seedvault.BackupNotificationManager
|
||||
import com.stevesoltys.seedvault.coAssertThrows
|
||||
import com.stevesoltys.seedvault.getRandomString
|
||||
import com.stevesoltys.seedvault.metadata.BackupMetadata
|
||||
import com.stevesoltys.seedvault.metadata.EncryptedBackupMetadata
|
||||
|
@ -18,10 +19,12 @@ import com.stevesoltys.seedvault.metadata.PackageMetadata
|
|||
import com.stevesoltys.seedvault.settings.Storage
|
||||
import com.stevesoltys.seedvault.transport.TransportTest
|
||||
import io.mockk.Runs
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertNotNull
|
||||
import org.junit.jupiter.api.Assertions.assertNull
|
||||
|
@ -32,6 +35,7 @@ import java.io.IOException
|
|||
import java.io.InputStream
|
||||
import kotlin.random.Random
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class RestoreCoordinatorTest : TransportTest() {
|
||||
|
||||
private val notificationManager: BackupNotificationManager = mockk()
|
||||
|
@ -40,7 +44,16 @@ internal class RestoreCoordinatorTest : TransportTest() {
|
|||
private val full = mockk<FullRestore>()
|
||||
private val metadataReader = mockk<MetadataReader>()
|
||||
|
||||
private val restore = RestoreCoordinator(context, settingsManager, metadataManager, notificationManager, plugin, kv, full, metadataReader)
|
||||
private val restore = RestoreCoordinator(
|
||||
context,
|
||||
settingsManager,
|
||||
metadataManager,
|
||||
notificationManager,
|
||||
plugin,
|
||||
kv,
|
||||
full,
|
||||
metadataReader
|
||||
)
|
||||
|
||||
private val token = Random.nextLong()
|
||||
private val inputStream = mockk<InputStream>()
|
||||
|
@ -57,7 +70,7 @@ internal class RestoreCoordinatorTest : TransportTest() {
|
|||
private val storageName = getRandomString()
|
||||
|
||||
@Test
|
||||
fun `getAvailableRestoreSets() builds set from plugin response`() {
|
||||
fun `getAvailableRestoreSets() builds set from plugin response`() = runBlocking {
|
||||
val encryptedMetadata = EncryptedBackupMetadata(token, inputStream)
|
||||
val metadata = BackupMetadata(
|
||||
token = token,
|
||||
|
@ -65,7 +78,7 @@ internal class RestoreCoordinatorTest : TransportTest() {
|
|||
androidIncremental = getRandomString(),
|
||||
deviceName = getRandomString())
|
||||
|
||||
every { plugin.getAvailableBackups() } returns sequenceOf(encryptedMetadata, encryptedMetadata)
|
||||
coEvery { plugin.getAvailableBackups() } returns sequenceOf(encryptedMetadata, encryptedMetadata)
|
||||
every { metadataReader.readMetadata(inputStream, token) } returns metadata
|
||||
every { inputStream.close() } just Runs
|
||||
|
||||
|
@ -137,16 +150,16 @@ internal class RestoreCoordinatorTest : TransportTest() {
|
|||
|
||||
@Test
|
||||
fun `nextRestorePackage() throws without startRestore()`() {
|
||||
assertThrows(IllegalStateException::class.javaObjectType) {
|
||||
coAssertThrows(IllegalStateException::class.javaObjectType) {
|
||||
restore.nextRestorePackage()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nextRestorePackage() returns KV description and takes precedence`() {
|
||||
fun `nextRestorePackage() returns KV description and takes precedence`() = runBlocking {
|
||||
restore.startRestore(token, packageInfoArray)
|
||||
|
||||
every { kv.hasDataForPackage(token, packageInfo) } returns true
|
||||
coEvery { kv.hasDataForPackage(token, packageInfo) } returns true
|
||||
every { kv.initializeState(token, packageInfo) } just Runs
|
||||
|
||||
val expected = RestoreDescription(packageInfo.packageName, TYPE_KEY_VALUE)
|
||||
|
@ -154,11 +167,11 @@ internal class RestoreCoordinatorTest : TransportTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `nextRestorePackage() returns full description if no KV data found`() {
|
||||
fun `nextRestorePackage() returns full description if no KV data found`() = runBlocking {
|
||||
restore.startRestore(token, packageInfoArray)
|
||||
|
||||
every { kv.hasDataForPackage(token, packageInfo) } returns false
|
||||
every { full.hasDataForPackage(token, packageInfo) } returns true
|
||||
coEvery { kv.hasDataForPackage(token, packageInfo) } returns false
|
||||
coEvery { full.hasDataForPackage(token, packageInfo) } returns true
|
||||
every { full.initializeState(token, packageInfo) } just Runs
|
||||
|
||||
val expected = RestoreDescription(packageInfo.packageName, TYPE_FULL_STREAM)
|
||||
|
@ -166,27 +179,27 @@ internal class RestoreCoordinatorTest : TransportTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `nextRestorePackage() returns NO_MORE_PACKAGES if data found`() {
|
||||
fun `nextRestorePackage() returns NO_MORE_PACKAGES if data found`() = runBlocking {
|
||||
restore.startRestore(token, packageInfoArray)
|
||||
|
||||
every { kv.hasDataForPackage(token, packageInfo) } returns false
|
||||
every { full.hasDataForPackage(token, packageInfo) } returns false
|
||||
coEvery { kv.hasDataForPackage(token, packageInfo) } returns false
|
||||
coEvery { full.hasDataForPackage(token, packageInfo) } returns false
|
||||
|
||||
assertEquals(NO_MORE_PACKAGES, restore.nextRestorePackage())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nextRestorePackage() returns all packages from startRestore()`() {
|
||||
fun `nextRestorePackage() returns all packages from startRestore()`() = runBlocking {
|
||||
restore.startRestore(token, packageInfoArray2)
|
||||
|
||||
every { kv.hasDataForPackage(token, packageInfo) } returns true
|
||||
coEvery { kv.hasDataForPackage(token, packageInfo) } returns true
|
||||
every { kv.initializeState(token, packageInfo) } just Runs
|
||||
|
||||
val expected = RestoreDescription(packageInfo.packageName, TYPE_KEY_VALUE)
|
||||
assertEquals(expected, restore.nextRestorePackage())
|
||||
|
||||
every { kv.hasDataForPackage(token, packageInfo2) } returns false
|
||||
every { full.hasDataForPackage(token, packageInfo2) } returns true
|
||||
coEvery { kv.hasDataForPackage(token, packageInfo2) } returns false
|
||||
coEvery { full.hasDataForPackage(token, packageInfo2) } returns true
|
||||
every { full.initializeState(token, packageInfo2) } just Runs
|
||||
|
||||
val expected2 = RestoreDescription(packageInfo2.packageName, TYPE_FULL_STREAM)
|
||||
|
@ -196,40 +209,40 @@ internal class RestoreCoordinatorTest : TransportTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `when kv#hasDataForPackage() throws return null`() {
|
||||
fun `when kv#hasDataForPackage() throws return null`() = runBlocking {
|
||||
restore.startRestore(token, packageInfoArray)
|
||||
|
||||
every { kv.hasDataForPackage(token, packageInfo) } throws IOException()
|
||||
coEvery { kv.hasDataForPackage(token, packageInfo) } throws IOException()
|
||||
|
||||
assertNull(restore.nextRestorePackage())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when full#hasDataForPackage() throws return null`() {
|
||||
fun `when full#hasDataForPackage() throws return null`() = runBlocking {
|
||||
restore.startRestore(token, packageInfoArray)
|
||||
|
||||
every { kv.hasDataForPackage(token, packageInfo) } returns false
|
||||
every { full.hasDataForPackage(token, packageInfo) } throws IOException()
|
||||
coEvery { kv.hasDataForPackage(token, packageInfo) } returns false
|
||||
coEvery { full.hasDataForPackage(token, packageInfo) } throws IOException()
|
||||
|
||||
assertNull(restore.nextRestorePackage())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getRestoreData() delegates to KV`() {
|
||||
fun `getRestoreData() delegates to KV`() = runBlocking {
|
||||
val data = mockk<ParcelFileDescriptor>()
|
||||
val result = Random.nextInt()
|
||||
|
||||
every { kv.getRestoreData(data) } returns result
|
||||
coEvery { kv.getRestoreData(data) } returns result
|
||||
|
||||
assertEquals(result, restore.getRestoreData(data))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getNextFullRestoreDataChunk() delegates to Full`() {
|
||||
fun `getNextFullRestoreDataChunk() delegates to Full`() = runBlocking {
|
||||
val data = mockk<ParcelFileDescriptor>()
|
||||
val result = Random.nextInt()
|
||||
|
||||
every { full.getNextFullRestoreDataChunk(data) } returns result
|
||||
coEvery { full.getNextFullRestoreDataChunk(data) } returns result
|
||||
|
||||
assertEquals(result, restore.getNextFullRestoreDataChunk(data))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue