Skip to content

Commit

Permalink
Use DigestInputStream to spy on backup data
Browse files Browse the repository at this point in the history
this is to save memory to prevent OOM errors we saw on CI
  • Loading branch information
grote committed Oct 11, 2024
1 parent 4de39d0 commit 88ac761
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.content.pm.PackageInfo
import android.os.ParcelFileDescriptor
import androidx.test.uiautomator.Until
import com.stevesoltys.seedvault.e2e.io.BackupDataInputIntercept
import com.stevesoltys.seedvault.e2e.io.InputStreamIntercept
import com.stevesoltys.seedvault.e2e.screen.impl.BackupScreen
import com.stevesoltys.seedvault.transport.backup.FullBackup
import com.stevesoltys.seedvault.transport.backup.InputFactory
Expand All @@ -21,8 +20,10 @@ import io.mockk.every
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.calyxos.seedvault.core.toHexString
import org.koin.core.component.get
import java.io.ByteArrayOutputStream
import java.security.DigestInputStream
import java.security.MessageDigest
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.test.fail

Expand Down Expand Up @@ -154,7 +155,8 @@ internal interface LargeBackupTestBase : LargeTestBase {

private fun spyOnFullBackupData(backupResult: SeedvaultLargeTestResult) {
var packageName: String? = null
var dataIntercept = ByteArrayOutputStream()
val messageDigest = MessageDigest.getInstance("SHA-256")
var digestInputStream: DigestInputStream? = null

coEvery {
spyFullBackup.performFullBackup(any(), any(), any())
Expand All @@ -166,20 +168,20 @@ internal interface LargeBackupTestBase : LargeTestBase {
every {
spyInputFactory.getInputStream(any())
} answers {
InputStreamIntercept(
inputStream = callOriginal(),
intercept = dataIntercept
)
digestInputStream = DigestInputStream(callOriginal(), messageDigest)
digestInputStream!!
}

coEvery {
spyFullBackup.finishBackup()
} answers {
val result = callOriginal()
backupResult.full[packageName!!] = dataIntercept.toByteArray().sha256()
val digest = digestInputStream?.messageDigest ?: fail("No digestInputStream")
backupResult.full[packageName!!] = digest.digest().toHexString()
ByteArray(0).sha256()

packageName = null
dataIntercept = ByteArrayOutputStream()
digest.reset()
result
}
}
Expand Down

0 comments on commit 88ac761

Please sign in to comment.