Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Crash in KitConfiguration due to NumberFormatException #503

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -676,6 +678,97 @@ 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() {
Mansi-mParticle marked this conversation as resolved.
Show resolved Hide resolved
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_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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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;
Expand Down
Loading