From e1a23e786f22068da004084e363986cd2e377981 Mon Sep 17 00:00:00 2001 From: Mansi-mParticle <159845845+Mansi-mParticle@users.noreply.github.com> Date: Fri, 9 Aug 2024 13:53:26 -0400 Subject: [PATCH] fix: Crash in KitConfiguration due to NumberFormatException (#503) --- .../com/mparticle/kits/KitConfiguration.java | 5 + .../mparticle/kits/KitConfigurationTest.kt | 102 ++++++++++++++++++ .../mparticle/mock/MockKitConfiguration.java | 5 + 3 files changed, 112 insertions(+) diff --git a/android-kit-base/src/main/java/com/mparticle/kits/KitConfiguration.java b/android-kit-base/src/main/java/com/mparticle/kits/KitConfiguration.java index aeef9c910..6c63f8fe9 100644 --- a/android-kit-base/src/main/java/com/mparticle/kits/KitConfiguration.java +++ b/android-kit-base/src/main/java/com/mparticle/kits/KitConfiguration.java @@ -921,12 +921,17 @@ public boolean passesBracketing(int userBucket) { protected SparseBooleanArray convertToSparseArray(JSONObject json) { SparseBooleanArray map = new SparseBooleanArray(); + if (json == null) { + return map; + } for (Iterator iterator = json.keys(); iterator.hasNext(); ) { try { String key = iterator.next(); map.put(Integer.parseInt(key), json.getInt(key) == 1); } catch (JSONException jse) { Logger.error("Issue while parsing kit configuration: " + jse.getMessage()); + } catch (Exception e) { + Logger.error("Exception while parsing kit configuration: " + e.getMessage()); } } return map; diff --git a/android-kit-base/src/test/kotlin/com/mparticle/kits/KitConfigurationTest.kt b/android-kit-base/src/test/kotlin/com/mparticle/kits/KitConfigurationTest.kt index d911874a4..538648014 100644 --- a/android-kit-base/src/test/kotlin/com/mparticle/kits/KitConfigurationTest.kt +++ b/android-kit-base/src/test/kotlin/com/mparticle/kits/KitConfigurationTest.kt @@ -1,5 +1,6 @@ package com.mparticle.kits +import android.util.SparseBooleanArray import com.mparticle.MParticle import com.mparticle.commerce.CommerceEvent import com.mparticle.commerce.Impression @@ -19,6 +20,7 @@ import org.junit.Assert import org.junit.BeforeClass import org.junit.Test import org.mockito.Mockito +import java.lang.reflect.Method class KitConfigurationTest { private val json = @@ -676,6 +678,106 @@ class KitConfigurationTest { ) } + @Test + fun testConvertToSparseArray() { + val kitConfiguration = MockKitConfiguration() + val jsonData = """ + { + "7456529": 0, + "10887805": 0, + "13992010": 0, + "15360852": 0, + "17455322": 0, + "18683141": 0, + "23029959": 0, + "41851400": 0, + "47355425": 0, + "54925556": 0, + "56409892": 0, + "66701264": 0 + } + """.trimIndent() + val jsonConfiguration = JSONObject(jsonData) + val method: Method = MockKitConfiguration::class.java.getDeclaredMethod("convertToSparseArray", JSONObject::class.java) + method.isAccessible = true + val result = method.invoke(kitConfiguration, jsonConfiguration) as SparseBooleanArray + Assert.assertEquals(12, result.size()) + } + + @Test + fun testConvertToSparseArray_When_JSON_OBJECT_IS_NULL() { + val kitConfiguration = MockKitConfiguration() + val jsonData = """ + { + "ec": { + } + } + """ + val method: Method = MockKitConfiguration::class.java.getDeclaredMethod("convertToSparseArray", JSONObject::class.java) + method.isAccessible = true + val jsonObject = JSONObject(jsonData) + val ecData = jsonObject.get("ec") as JSONObject + val result = method.invoke(kitConfiguration, ecData) as SparseBooleanArray + Assert.assertEquals(0, result.size()) + } + + @Test + fun testConvertToSparseArray_When_JSON_IS_NULL() { + val kitConfiguration = MockKitConfiguration() + val method: Method = MockKitConfiguration::class.java.getDeclaredMethod("convertToSparseArray", JSONObject::class.java) + method.isAccessible = true + val result = method.invoke(kitConfiguration, null) as SparseBooleanArray + Assert.assertEquals(0, result.size()) + } + + @Test + fun testConvertToSparseArray_When_JSON_Data_IS_INVALID() { + val kitConfiguration = MockKitConfiguration() + val jsn = """ + { + "7456529": 0, + "10887805": 0, + "-36!037962": 0, + "15360852": 0, + "17455322": 0, + "18683141": 0, + "23029959": 0, + "41851400": 0, + "47355425": 0, + "54925556": 0, + "56409892": 0, + "66701264": 0 + } + """.trimIndent() + val jsonConfiguration = JSONObject(jsn) + val method: Method = MockKitConfiguration::class.java.getDeclaredMethod("convertToSparseArray", JSONObject::class.java) + method.isAccessible = true + + val result = method.invoke(kitConfiguration, jsonConfiguration) as SparseBooleanArray + Assert.assertEquals(11, result.size()) + } + + @Test + fun testConvertToSparseArray_When_JSON_OBJECT_IS_INVALID() { + val kitConfiguration = MockKitConfiguration() + val jsn = """ + { + "name": "John", + "age": "30", + "18683141": 0 + } + """.trimIndent() + val jsonConfiguration = JSONObject(jsn) + val method: Method = MockKitConfiguration::class.java.getDeclaredMethod( + "convertToSparseArray", + JSONObject::class.java + ) + method.isAccessible = true + + val result = method.invoke(kitConfiguration, jsonConfiguration) as SparseBooleanArray + Assert.assertEquals(1, result.size()) + } + @Test fun testExcludeUser() { val kitConfiguration = KitConfiguration() diff --git a/testutils/src/main/java/com/mparticle/mock/MockKitConfiguration.java b/testutils/src/main/java/com/mparticle/mock/MockKitConfiguration.java index 6e9beaf0b..a6677bdab 100644 --- a/testutils/src/main/java/com/mparticle/mock/MockKitConfiguration.java +++ b/testutils/src/main/java/com/mparticle/mock/MockKitConfiguration.java @@ -44,12 +44,17 @@ public static KitConfiguration createKitConfiguration() throws JSONException { @Override protected SparseBooleanArray convertToSparseArray(JSONObject json) { SparseBooleanArray map = new MockSparseBooleanArray(); + if (json == null) { + return map; + } for (Iterator iterator = json.keys(); iterator.hasNext(); ) { try { String key = iterator.next(); map.put(Integer.parseInt(key), json.getInt(key) == 1); } catch (JSONException jse) { Logger.error("Issue while parsing kit configuration: " + jse.getMessage()); + } catch (Exception e) { + Logger.error("Exception while parsing kit configuration: " + e.getMessage()); } } return map;