Skip to content

Commit

Permalink
Fix FullRestore when Loader#loadFiles() throws an error for a later b…
Browse files Browse the repository at this point in the history
…lob stream.
  • Loading branch information
grote committed Nov 14, 2024
1 parent 47c3e65 commit 32aff32
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal class FullRestore(
return outputFactory.getOutputStream(pfd).use { outputStream ->
try {
copyInputStream(outputStream)
} catch (e: IOException) {
} catch (e: Exception) {
Log.w(TAG, "Error copying stream for package $packageName.", e)
return TRANSPORT_PACKAGE_REJECTED
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.stevesoltys.seedvault.coAssertThrows
import com.stevesoltys.seedvault.getRandomByteArray
import com.stevesoltys.seedvault.header.MAX_SEGMENT_LENGTH
import com.stevesoltys.seedvault.header.VERSION
import com.stevesoltys.seedvault.repo.HashMismatchException
import com.stevesoltys.seedvault.repo.Loader
import io.mockk.CapturingSlot
import io.mockk.Runs
Expand All @@ -32,7 +33,9 @@ import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.FilterInputStream
import java.io.IOException
import java.io.SequenceInputStream
import java.security.GeneralSecurityException
import kotlin.random.Random

Expand Down Expand Up @@ -114,6 +117,36 @@ internal class FullRestoreTest : RestoreTest() {
verify { fileDescriptor.close() }
}

@Test
fun `reading from stream throws HashMismatchException in SequenceInputStream`() = runBlocking {
restore.initializeState(VERSION, packageInfo, blobHandles)
val bytes = getRandomByteArray()

val inputStream = SequenceInputStream(
ByteArrayInputStream(bytes),
object : FilterInputStream(ByteArrayInputStream(bytes)) {
override fun read(): Int {
throw HashMismatchException()
}

override fun read(b: ByteArray, off: Int, len: Int): Int {
throw HashMismatchException()
}
},
)
coEvery { loader.loadFiles(blobHandles) } returns inputStream
every { outputFactory.getOutputStream(fileDescriptor) } returns outputStream
every { fileDescriptor.close() } just Runs

assertEquals(bytes.size, restore.getNextFullRestoreDataChunk(fileDescriptor))
assertEquals(
TRANSPORT_PACKAGE_REJECTED,
restore.getNextFullRestoreDataChunk(fileDescriptor),
)

verify { fileDescriptor.close() }
}

@Test
fun `full chunk gets decrypted`() = runBlocking {
restore.initializeState(VERSION, packageInfo, blobHandles)
Expand Down

0 comments on commit 32aff32

Please sign in to comment.