Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/bitfireAT/ical4android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRicki Hirner <hirner@bitfire.at>2021-07-22 19:22:44 +0300
committerRicki Hirner <hirner@bitfire.at>2021-07-22 19:25:35 +0300
commit5106b711635c14d23f0f220d4b01949be5c92012 (patch)
treeb39081644cb62b3fb90dccc60d21bee2d5f8294a /src
parent39cd98ca269e4fc46f57f0f35272a0ef92abdbbf (diff)
MiscUtils.reflectionToString: catch OOM on toString(); limit values to 10000 chars
Diffstat (limited to 'src')
-rw-r--r--src/androidTest/java/at/bitfire/ical4android/MiscUtilsAndroidTest.kt (renamed from src/androidTest/java/at/bitfire/ical4android/MiscUtilsTest.kt)44
-rw-r--r--src/main/java/at/bitfire/ical4android/MiscUtils.kt10
-rw-r--r--src/test/java/at/bitfire/ical4android/MiscUtilsTest.kt38
3 files changed, 61 insertions, 31 deletions
diff --git a/src/androidTest/java/at/bitfire/ical4android/MiscUtilsTest.kt b/src/androidTest/java/at/bitfire/ical4android/MiscUtilsAndroidTest.kt
index 51a5720..5c51c94 100644
--- a/src/androidTest/java/at/bitfire/ical4android/MiscUtilsTest.kt
+++ b/src/androidTest/java/at/bitfire/ical4android/MiscUtilsAndroidTest.kt
@@ -12,39 +12,17 @@ import android.content.ContentValues
import android.database.MatrixCursor
import androidx.test.filters.SmallTest
import at.bitfire.ical4android.MiscUtils.CursorHelper.toValues
-import org.junit.Assert.*
+import org.junit.Assert
+import org.junit.Assert.assertEquals
import org.junit.Test
-class MiscUtilsTest {
+class MiscUtilsAndroidTest {
private val tzVienna = DateUtils.ical4jTimeZone("Europe/Vienna")
@Test
@SmallTest
- fun testReflectionToString() {
- val s = MiscUtils.reflectionToString(TestClass())
- assertTrue(s.startsWith("TestClass=["))
- assertTrue(s.contains("s=test"))
- assertTrue(s.contains("i=2"))
- }
-
- @Test
- @SmallTest
- fun testRemoveEmptyStrings() {
- val values = ContentValues(2)
- values.put("key1", "value")
- values.put("key2", 1L)
- values.put("key3", "")
- MiscUtils.removeEmptyStrings(values)
- assertEquals("value", values.getAsString("key1"))
- assertEquals(1L, values.getAsLong("key2").toLong())
- assertNull(values.get("key3"))
- }
-
-
- @Test
- @SmallTest
fun testCursorToValues() {
val columns = arrayOf("col1", "col2")
val c = MatrixCursor(columns)
@@ -55,11 +33,17 @@ class MiscUtilsTest {
assertEquals("row1_val2", values.getAsString("col2"))
}
-
- @Suppress("unused")
- private class TestClass {
- private val s = "test"
- val i = 2
+ @Test
+ @SmallTest
+ fun testRemoveEmptyStrings() {
+ val values = ContentValues(2)
+ values.put("key1", "value")
+ values.put("key2", 1L)
+ values.put("key3", "")
+ MiscUtils.removeEmptyStrings(values)
+ Assert.assertEquals("value", values.getAsString("key1"))
+ Assert.assertEquals(1L, values.getAsLong("key2").toLong())
+ Assert.assertNull(values.get("key3"))
}
}
diff --git a/src/main/java/at/bitfire/ical4android/MiscUtils.kt b/src/main/java/at/bitfire/ical4android/MiscUtils.kt
index e0eb940..f95f66d 100644
--- a/src/main/java/at/bitfire/ical4android/MiscUtils.kt
+++ b/src/main/java/at/bitfire/ical4android/MiscUtils.kt
@@ -13,11 +13,14 @@ import android.content.ContentValues
import android.database.Cursor
import android.database.DatabaseUtils
import android.os.Build
+import org.apache.commons.lang3.StringUtils
import java.lang.reflect.Modifier
import java.util.*
object MiscUtils {
+ const val TOSTRING_MAXCHARS = 10000
+
/**
* Generates useful toString info (fields and values) from [obj] by reflection.
*
@@ -30,7 +33,12 @@ object MiscUtils {
while (clazz != null) {
for (prop in clazz.declaredFields.filterNot { Modifier.isStatic(it.modifiers) }) {
prop.isAccessible = true
- s += "${prop.name}=" + prop.get(obj)?.toString()?.trim()
+ val valueStr = try {
+ StringUtils.abbreviate(prop.get(obj)?.toString(), TOSTRING_MAXCHARS)
+ } catch(e: OutOfMemoryError) {
+ "![" + e.toString() + "]"
+ }
+ s += "${prop.name}=" + valueStr
}
clazz = clazz.superclass
}
diff --git a/src/test/java/at/bitfire/ical4android/MiscUtilsTest.kt b/src/test/java/at/bitfire/ical4android/MiscUtilsTest.kt
new file mode 100644
index 0000000..3ff78d9
--- /dev/null
+++ b/src/test/java/at/bitfire/ical4android/MiscUtilsTest.kt
@@ -0,0 +1,38 @@
+package at.bitfire.ical4android
+
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class MiscUtilsTest {
+
+ @Test
+ fun testReflectionToString() {
+ val s = MiscUtils.reflectionToString(MiscUtilsTest.TestClass())
+ assertTrue(s.startsWith("TestClass=["))
+ assertTrue(s.contains("i=2"))
+ assertTrue(s.contains("large=null"))
+ assertTrue(s.contains("s=test"))
+ }
+
+ @Test
+ fun testReflectionToString_OOM() {
+ val t = MiscUtilsTest.TestClass()
+ t.large = object: Any() {
+ override fun toString(): String {
+ throw OutOfMemoryError("toString() causes OOM")
+ }
+ }
+ val s = MiscUtils.reflectionToString(t)
+ assertTrue(s.startsWith("TestClass=["))
+ assertTrue(s.contains("large=![java.lang.OutOfMemoryError"))
+ }
+
+
+ @Suppress("unused")
+ private class TestClass {
+ val i = 2
+ var large: Any? = null
+ private val s = "test"
+ }
+
+} \ No newline at end of file