From 7976140834d1e2289e46c1e1e3dfb8f4d68e5814 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Tue, 4 Aug 2020 14:08:21 +0200 Subject: [PATCH] Test opml version before parsing --- api/src/androidTest/assets/wrong_version.opml | 6 ++++++ .../java/com/readrops/api/OPMLParserTest.kt | 10 ++++++++++ .../java/com/readrops/api/opml/OPMLParser.kt | 20 +++++++++++++++---- .../readrops/api/utils/ParseException.java | 7 +++++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 api/src/androidTest/assets/wrong_version.opml diff --git a/api/src/androidTest/assets/wrong_version.opml b/api/src/androidTest/assets/wrong_version.opml new file mode 100644 index 00000000..da6af325 --- /dev/null +++ b/api/src/androidTest/assets/wrong_version.opml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api/src/androidTest/java/com/readrops/api/OPMLParserTest.kt b/api/src/androidTest/java/com/readrops/api/OPMLParserTest.kt index 27b5a232..03cac14e 100644 --- a/api/src/androidTest/java/com/readrops/api/OPMLParserTest.kt +++ b/api/src/androidTest/java/com/readrops/api/OPMLParserTest.kt @@ -4,6 +4,7 @@ import android.content.Context import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import com.readrops.api.opml.OPMLParser +import com.readrops.api.utils.ParseException import com.readrops.db.entities.Feed import com.readrops.db.entities.Folder import io.reactivex.schedulers.Schedulers @@ -37,6 +38,15 @@ class OPMLParserTest { assertEquals(foldersAndFeeds?.get(null)?.size, 2) } + @Test + fun opmlVersionTest() { + val stream = context.resources.assets.open("wrong_version.opml") + + OPMLParser.read(stream) + .test() + .assertError(ParseException::class.java) + } + @Test fun writeOpmlTest() { diff --git a/api/src/main/java/com/readrops/api/opml/OPMLParser.kt b/api/src/main/java/com/readrops/api/opml/OPMLParser.kt index 5d744c00..1c7b3f2f 100644 --- a/api/src/main/java/com/readrops/api/opml/OPMLParser.kt +++ b/api/src/main/java/com/readrops/api/opml/OPMLParser.kt @@ -2,11 +2,13 @@ package com.readrops.api.opml import android.content.Context import android.net.Uri +import android.util.Log import com.readrops.api.opml.model.Body import com.readrops.api.opml.model.Head import com.readrops.api.opml.model.OPML import com.readrops.api.opml.model.Outline import com.readrops.api.utils.LibUtils +import com.readrops.api.utils.ParseException import com.readrops.db.entities.Feed import com.readrops.db.entities.Folder import io.reactivex.Completable @@ -19,6 +21,8 @@ import java.io.OutputStream object OPMLParser { + val TAG = OPMLParser.javaClass.simpleName + @JvmStatic fun read(uri: Uri, context: Context): Single>> { return Single.create(SingleOnSubscribe { @@ -30,12 +34,17 @@ object OPMLParser { @JvmStatic fun read(stream: InputStream): Single>> { return Single.create { emitter -> - val fileString = LibUtils.inputStreamToString(stream) - val serializer: Serializer = Persister() + try { + val fileString = LibUtils.inputStreamToString(stream) + val serializer: Serializer = Persister() - val opml: OPML = serializer.read(OPML::class.java, fileString) + val opml: OPML = serializer.read(OPML::class.java, fileString) - emitter.onSuccess(opmltoFoldersAndFeeds(opml)) + emitter.onSuccess(opmltoFoldersAndFeeds(opml)) + } catch (e: Exception) { + Log.d(TAG, e.message, e) + emitter.onError(e) + } } } @@ -50,6 +59,9 @@ object OPMLParser { } private fun opmltoFoldersAndFeeds(opml: OPML): Map> { + if (opml.version != "2.0") + throw ParseException("Only 2.0 OPML specification is supported") + val foldersAndFeeds: MutableMap> = HashMap() val body = opml.body!! diff --git a/api/src/main/java/com/readrops/api/utils/ParseException.java b/api/src/main/java/com/readrops/api/utils/ParseException.java index 12db280c..46065da3 100644 --- a/api/src/main/java/com/readrops/api/utils/ParseException.java +++ b/api/src/main/java/com/readrops/api/utils/ParseException.java @@ -2,4 +2,11 @@ package com.readrops.api.utils; public class ParseException extends Exception { + public ParseException() { + super(); + } + + public ParseException(String message) { + super(message); + } }