diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinator.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinator.kt index f6d68018..95fa79e2 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinator.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinator.kt @@ -26,12 +26,11 @@ import com.stevesoltys.seedvault.metadata.BackupType import com.stevesoltys.seedvault.metadata.DecryptionFailedException import com.stevesoltys.seedvault.metadata.MetadataManager import com.stevesoltys.seedvault.metadata.MetadataReader -import com.stevesoltys.seedvault.proto.Snapshot -import com.stevesoltys.seedvault.repo.Loader +import com.stevesoltys.seedvault.repo.SnapshotManager +import com.stevesoltys.seedvault.repo.getBlobHandles import com.stevesoltys.seedvault.settings.SettingsManager import com.stevesoltys.seedvault.transport.D2D_TRANSPORT_FLAGS import com.stevesoltys.seedvault.transport.DEFAULT_TRANSPORT_FLAGS -import com.stevesoltys.seedvault.repo.getBlobHandles import com.stevesoltys.seedvault.ui.notification.BackupNotificationManager import org.calyxos.seedvault.core.backends.AppBackupFileType import org.calyxos.seedvault.core.backends.Backend @@ -67,7 +66,7 @@ internal class RestoreCoordinator( private val metadataManager: MetadataManager, private val notificationManager: BackupNotificationManager, private val backendManager: BackendManager, - private val loader: Loader, + private val snapshotManager: SnapshotManager, private val kv: KVRestore, private val full: FullRestore, private val metadataReader: MetadataReader, @@ -91,15 +90,10 @@ internal class RestoreCoordinator( for (handle in fileHandles) { try { val backup = when (handle) { - is AppBackupFileType.Snapshot -> { - val snapshot = loader.loadFile(handle).use { inputStream -> - Snapshot.parseFrom(inputStream) - } - RestorableBackup( - repoId = handle.repoId, - snapshot = snapshot, - ) - } + is AppBackupFileType.Snapshot -> RestorableBackup( + repoId = handle.repoId, + snapshot = snapshotManager.loadSnapshot(handle), + ) is LegacyAppBackupFile.Metadata -> { val metadata = backend.load(handle).use { inputStream -> metadataReader.readMetadata(inputStream, handle.token) diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreModule.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreModule.kt index 151a8b58..681eac5f 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreModule.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreModule.kt @@ -20,7 +20,7 @@ val restoreModule = module { metadataManager = get(), notificationManager = get(), backendManager = get(), - loader = get(), + snapshotManager = get(), kv = get(), full = get(), metadataReader = get(), diff --git a/app/src/test/java/com/stevesoltys/seedvault/transport/CoordinatorIntegrationTest.kt b/app/src/test/java/com/stevesoltys/seedvault/transport/CoordinatorIntegrationTest.kt index ffc0d156..bd801efc 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/transport/CoordinatorIntegrationTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/transport/CoordinatorIntegrationTest.kt @@ -26,6 +26,7 @@ import com.stevesoltys.seedvault.repo.AppBackupManager import com.stevesoltys.seedvault.repo.BackupReceiver import com.stevesoltys.seedvault.repo.Loader import com.stevesoltys.seedvault.repo.SnapshotCreator +import com.stevesoltys.seedvault.repo.SnapshotManager import com.stevesoltys.seedvault.transport.backup.BackupCoordinator import com.stevesoltys.seedvault.transport.backup.FullBackup import com.stevesoltys.seedvault.transport.backup.InputFactory @@ -76,6 +77,7 @@ internal class CoordinatorIntegrationTest : TransportTest() { private val legacyPlugin = mockk() private val backend = mockk() private val loader = mockk() + private val snapshotManager = mockk() private val backupReceiver = mockk() private val kvBackup = KVBackup( settingsManager = settingsManager, @@ -121,7 +123,7 @@ internal class CoordinatorIntegrationTest : TransportTest() { metadataManager, notificationManager, backendManager, - loader, + snapshotManager, kvRestore, fullRestore, metadataReader diff --git a/app/src/test/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinatorTest.kt b/app/src/test/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinatorTest.kt index 7bf154a8..76bd9da1 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinatorTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinatorTest.kt @@ -22,7 +22,7 @@ import com.stevesoltys.seedvault.metadata.BackupType import com.stevesoltys.seedvault.metadata.MetadataReader import com.stevesoltys.seedvault.metadata.PackageMetadata import com.stevesoltys.seedvault.proto.copy -import com.stevesoltys.seedvault.repo.Loader +import com.stevesoltys.seedvault.repo.SnapshotManager import com.stevesoltys.seedvault.transport.TransportTest import com.stevesoltys.seedvault.ui.notification.BackupNotificationManager import io.mockk.Runs @@ -45,8 +45,6 @@ import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Assertions.fail import org.junit.jupiter.api.Test -import java.io.ByteArrayInputStream -import java.io.ByteArrayOutputStream import java.io.IOException import java.io.InputStream import kotlin.random.Random @@ -56,7 +54,7 @@ internal class RestoreCoordinatorTest : TransportTest() { private val notificationManager: BackupNotificationManager = mockk() private val backendManager: BackendManager = mockk() private val backend = mockk() - private val loader = mockk() + private val snapshotManager = mockk() private val kv = mockk() private val full = mockk() private val metadataReader = mockk() @@ -68,7 +66,7 @@ internal class RestoreCoordinatorTest : TransportTest() { metadataManager = metadataManager, notificationManager = notificationManager, backendManager = backendManager, - loader = loader, + snapshotManager = snapshotManager, kv = kv, full = full, metadataReader = metadataReader, @@ -261,9 +259,6 @@ internal class RestoreCoordinatorTest : TransportTest() { fun `startRestore() loads snapshots for auto-restore`() = runBlocking { val handle = AppBackupFileType.Snapshot(repoId, getRandomByteArray(32).toHexString()) val info = FileInfo(handle, 1) - val snapshotBytes = ByteArrayOutputStream().apply { - snapshot.writeTo(this) - }.toByteArray() every { backendManager.backendProperties } returns safStorage every { safStorage.isUnavailableUsb(context) } returns false @@ -278,7 +273,7 @@ internal class RestoreCoordinatorTest : TransportTest() { val callback = lambda<(FileInfo) -> Unit>().captured callback(info) } - coEvery { loader.loadFile(handle) } returns ByteArrayInputStream(snapshotBytes) + coEvery { snapshotManager.loadSnapshot(handle) } returns snapshot assertEquals(TRANSPORT_OK, restore.startRestore(token, pmPackageInfoArray)) } @@ -287,9 +282,6 @@ internal class RestoreCoordinatorTest : TransportTest() { fun `startRestore() errors when it can't find snapshots`() = runBlocking { val handle = AppBackupFileType.Snapshot(repoId, getRandomByteArray(32).toHexString()) val info = FileInfo(handle, 1) - val snapshotBytes = ByteArrayOutputStream().apply { // snapshot has different token - snapshot.copy { token = this@RestoreCoordinatorTest.token - 1 }.writeTo(this) - }.toByteArray() every { backendManager.backendProperties } returns safStorage every { safStorage.isUnavailableUsb(context) } returns false @@ -304,12 +296,14 @@ internal class RestoreCoordinatorTest : TransportTest() { val callback = lambda<(FileInfo) -> Unit>().captured callback(info) } - coEvery { loader.loadFile(handle) } returns ByteArrayInputStream(snapshotBytes) + coEvery { snapshotManager.loadSnapshot(handle) } returns snapshot.copy { + token = this@RestoreCoordinatorTest.token - 1 // unexpected token + } assertEquals(TRANSPORT_ERROR, restore.startRestore(token, pmPackageInfoArray)) coVerify { - loader.loadFile(handle) // really loaded snapshot + snapshotManager.loadSnapshot(handle) // really loaded snapshot } } diff --git a/app/src/test/java/com/stevesoltys/seedvault/transport/restore/RestoreV0IntegrationTest.kt b/app/src/test/java/com/stevesoltys/seedvault/transport/restore/RestoreV0IntegrationTest.kt index c6a04fc9..38dbb75f 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/transport/restore/RestoreV0IntegrationTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/transport/restore/RestoreV0IntegrationTest.kt @@ -21,6 +21,7 @@ import com.stevesoltys.seedvault.encodeBase64 import com.stevesoltys.seedvault.header.HeaderReaderImpl import com.stevesoltys.seedvault.metadata.MetadataReaderImpl import com.stevesoltys.seedvault.repo.Loader +import com.stevesoltys.seedvault.repo.SnapshotManager import com.stevesoltys.seedvault.toByteArrayFromHex import com.stevesoltys.seedvault.transport.TransportTest import com.stevesoltys.seedvault.transport.backup.KvDbManager @@ -57,6 +58,7 @@ internal class RestoreV0IntegrationTest : TransportTest() { private val notificationManager = mockk() private val backendManager: BackendManager = mockk() private val loader = mockk() + private val snapshotManager = mockk() @Suppress("Deprecation") private val legacyPlugin = mockk() @@ -79,7 +81,7 @@ internal class RestoreV0IntegrationTest : TransportTest() { metadataManager = metadataManager, notificationManager = notificationManager, backendManager = backendManager, - loader = loader, + snapshotManager = snapshotManager, kv = kvRestore, full = fullRestore, metadataReader = metadataReader,