updating the keys exporter to validate the generated file size in an attempt to warn the user of malformed outputs

- injects the io dispatcher to allow the testing
- adds unit tests around the different error flows
This commit is contained in:
Adam Brown 2021-09-27 17:18:13 +01:00
parent 789cc6b597
commit ac0c7067e0
2 changed files with 160 additions and 3 deletions

View File

@ -18,6 +18,7 @@ package im.vector.app.features.crypto.keys
import android.content.Context
import android.net.Uri
import im.vector.app.core.dispatchers.CoroutineDispatchers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.session.Session
@ -25,17 +26,36 @@ import javax.inject.Inject
class KeysExporter @Inject constructor(
private val session: Session,
private val context: Context
private val context: Context,
private val dispatchers: CoroutineDispatchers
) {
/**
* Export keys and write them to the provided uri
*/
suspend fun export(password: String, uri: Uri) {
return withContext(Dispatchers.IO) {
withContext(dispatchers.io) {
val data = session.cryptoService().exportRoomKeys(password)
context.contentResolver.openOutputStream(uri)
?.use { it.write(data) }
?: throw IllegalStateException("Unable to open file for writting")
?: throw IllegalStateException("Unable to open file for writing")
verifyExportedKeysOutputFileSize(uri, expectedSize = data.size.toLong())
}
}
private fun verifyExportedKeysOutputFileSize(uri: Uri, expectedSize: Long) {
val output = context.contentResolver.openFileDescriptor(uri, "r", null)
when {
output == null -> throw IllegalStateException("Exported file not found")
output.statSize != expectedSize -> {
throw UnexpectedExportKeysFileSizeException(
expectedFileSize = output.statSize,
actualFileSize = expectedSize,
)
}
}
}
}
class UnexpectedExportKeysFileSizeException(expectedFileSize: Long, actualFileSize: Long) : IllegalStateException(
"Exported Keys file has unexpected file size, got: $actualFileSize but expected: $expectedFileSize"
)

View File

@ -0,0 +1,137 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.crypto.keys
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.os.ParcelFileDescriptor
import im.vector.app.core.dispatchers.CoroutineDispatchers
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.amshove.kluent.internal.assertFailsWith
import org.junit.Before
import org.junit.Test
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.crypto.CryptoService
import java.io.OutputStream
private val A_URI = mockk<Uri>()
private val A_ROOM_KEYS_EXPORT = ByteArray(size = 111)
private const val A_PASSWORD = "a password"
class KeysExporterTest {
private val cryptoService = FakeCryptoService()
private val context = FakeContext()
private val keysExporter = KeysExporter(
session = FakeSession(cryptoService = cryptoService),
context = context.instance,
dispatchers = CoroutineDispatchers(Dispatchers.Unconfined),
)
@Before
fun setUp() {
cryptoService.roomKeysExport = A_ROOM_KEYS_EXPORT
}
@Test
fun `when exporting then writes exported keys to context output stream`() {
givenFileDescriptorWithSize(size = A_ROOM_KEYS_EXPORT.size.toLong())
val outputStream = context.givenOutputStreamFor(A_URI)
runBlocking { keysExporter.export(A_PASSWORD, A_URI) }
verify { outputStream.write(A_ROOM_KEYS_EXPORT) }
}
@Test
fun `given different file size returned for export when exporting then throws UnexpectedExportKeysFileSizeException`() {
givenFileDescriptorWithSize(size = 110)
context.givenOutputStreamFor(A_URI)
assertFailsWith<UnexpectedExportKeysFileSizeException> {
runBlocking { keysExporter.export(A_PASSWORD, A_URI) }
}
}
@Test
fun `given output stream is unavailable for exporting to when exporting then throws IllegalStateException`() {
context.givenMissingOutputStreamFor(A_URI)
assertFailsWith<IllegalStateException>(message = "Unable to open file for writing") {
runBlocking { keysExporter.export(A_PASSWORD, A_URI) }
}
}
@Test
fun `given exported file is missing after export when exporting then throws IllegalStateException`() {
context.givenFileDescriptor(A_URI, mode = "r") { null }
context.givenOutputStreamFor(A_URI)
assertFailsWith<IllegalStateException>(message = "Exported file not found") {
runBlocking { keysExporter.export(A_PASSWORD, A_URI) }
}
}
private fun givenFileDescriptorWithSize(size: Long) {
context.givenFileDescriptor(A_URI, mode = "r") {
mockk<ParcelFileDescriptor>().also { every { it.statSize } returns size }
}
}
}
class FakeContext {
private val contentResolver = mockk<ContentResolver>()
val instance = mockk<Context>()
init {
every { instance.contentResolver } returns contentResolver
}
fun givenFileDescriptor(uri: Uri, mode: String, factory: () -> ParcelFileDescriptor?) {
val fileDescriptor = factory()
every { contentResolver.openFileDescriptor(uri, mode, null) } returns fileDescriptor
}
fun givenOutputStreamFor(uri: Uri): OutputStream {
val outputStream = mockk<OutputStream>(relaxed = true)
every { contentResolver.openOutputStream(uri) } returns outputStream
return outputStream
}
fun givenMissingOutputStreamFor(uri: Uri) {
every { contentResolver.openOutputStream(uri) } returns null
}
}
class FakeSession(
private val cryptoService: CryptoService = FakeCryptoService()
) : Session by mockk(relaxed = true) {
override fun cryptoService() = cryptoService
}
class FakeCryptoService : CryptoService by mockk() {
var roomKeysExport = ByteArray(size = 1)
override suspend fun exportRoomKeys(password: String) = roomKeysExport
}