diff --git a/app/src/main/java/org/fdroid/fdroid/Preferences.java b/app/src/main/java/org/fdroid/fdroid/Preferences.java index 955b431e5..0e9e4f2b6 100644 --- a/app/src/main/java/org/fdroid/fdroid/Preferences.java +++ b/app/src/main/java/org/fdroid/fdroid/Preferences.java @@ -597,10 +597,17 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh private String intMapToString(HashMap intMap) { String output = ""; for (String key : intMap.keySet()) { - if (!output.isEmpty()) { - output = output + "\n"; + Integer value = intMap.get(key); + if (key == null || key.isEmpty()) { + Utils.debugLog(TAG, "Don't serialize record with null key"); + } else if (value == null) { + Utils.debugLog(TAG, "Don't serialize null value for: " + key); + } else { + if (!output.isEmpty()) { + output = output + "\n"; + } + output = output + key + " " + value; } - output = output + key + " " + intMap.get(key); } return output; } @@ -612,17 +619,21 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh // values may be missing or unparseable String key = pair[0]; Integer value = 0; - if (pair.length > 1) { - try { - value = Integer.valueOf(pair[1]); - } catch (NumberFormatException e) { - // use default value if stored value can't be parsed - Utils.debugLog(TAG, "Serialized map entry value can't be parsed: " + line); + if (key != null && !key.isEmpty()) { + if (pair.length > 1) { + try { + value = Integer.valueOf(pair[1]); + } catch (NumberFormatException e) { + // use default value if stored value can't be parsed + Utils.debugLog(TAG, "Serialized map entry value can't be parsed: " + line); + } + } else { + Utils.debugLog(TAG, "Serialized map entry value is missing: " + line); } + output.put(key, value); } else { - Utils.debugLog(TAG, "Serialized map entry value is missing: " + line); + Utils.debugLog(TAG, "Serialized map entry key is missing: " + line); } - output.put(key, value); } return output; } diff --git a/app/src/test/java/org/fdroid/fdroid/PreferencesTest.java b/app/src/test/java/org/fdroid/fdroid/PreferencesTest.java index 31266cdaf..2eaf48e4a 100644 --- a/app/src/test/java/org/fdroid/fdroid/PreferencesTest.java +++ b/app/src/test/java/org/fdroid/fdroid/PreferencesTest.java @@ -20,6 +20,7 @@ package org.fdroid.fdroid; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import android.content.Context; @@ -35,6 +36,7 @@ import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.shadows.ShadowLog; +import java.util.HashMap; import java.util.Map; @RunWith(RobolectricTestRunner.class) @@ -157,4 +159,16 @@ public class PreferencesTest { assertNotEquals(Long.parseLong(defaults.getString(Preferences.PREF_KEEP_CACHE_TIME, null)), Preferences.get().getKeepCacheTime()); } + + @Test + public void testMirrorErrorMethods() { + Preferences.setupForTests(CONTEXT); + Preferences preferences = Preferences.get(); + // serialize an empty map + preferences.setMirrorErrorData(new HashMap<>(0)); + HashMap result = preferences.getMirrorErrorData(); + // deserializing should return an empty map without throwing any exceptions + assertNotNull(result); + assertEquals(result.size(), 0); + } }