diff --git a/legacy/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.kt b/legacy/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.kt index dfe5c6d6494..ab44cd1040d 100644 --- a/legacy/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.kt +++ b/legacy/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.kt @@ -30,7 +30,7 @@ class SettingsImporter internal constructor( * * @throws SettingsImportExportException In case of an error. */ - @Suppress("TooGenericExceptionCaught") + @Suppress("TooGenericExceptionCaught", "ThrowsCount") @Throws(SettingsImportExportException::class) fun getImportStreamContents(inputStream: InputStream): ImportContents { try { @@ -46,7 +46,11 @@ class SettingsImporter internal constructor( ) } - // TODO: throw exception if neither global settings nor account settings could be found + val hasAccountsWithSettings = imported.accounts.any { it.settings != null } + if (!globalSettings && !hasAccountsWithSettings) { + throw SettingsImportExportException("Neither global settings nor account settings could be found") + } + return ImportContents(globalSettings, accounts) } catch (e: SettingsImportExportException) { throw e diff --git a/legacy/core/src/test/java/com/fsck/k9/preferences/SettingsImporterTest.kt b/legacy/core/src/test/java/com/fsck/k9/preferences/SettingsImporterTest.kt index 292521897f8..d7874881812 100644 --- a/legacy/core/src/test/java/com/fsck/k9/preferences/SettingsImporterTest.kt +++ b/legacy/core/src/test/java/com/fsck/k9/preferences/SettingsImporterTest.kt @@ -269,6 +269,9 @@ class SettingsImporterTest : K9RobolectricTest() { user@gmail.com + + b + @@ -302,6 +305,9 @@ class SettingsImporterTest : K9RobolectricTest() { user@gmail.com + + b + @@ -320,4 +326,28 @@ class SettingsImporterTest : K9RobolectricTest() { } } } + + @Test + fun `getImportStreamContents() should throw when no setting is present in inputStream`() { + val accountUuid = UUID.randomUUID().toString() + val inputStream = + """ + + + + + + + user@gmail.com + + + + + + """.trimIndent().byteInputStream() + + assertFailure { + settingsImporter.getImportStreamContents(inputStream) + }.isInstanceOf() + } }