diff options
author | cyBerta <cyberta@riseup.net> | 2024-02-26 21:33:52 +0100 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2024-02-26 21:33:52 +0100 |
commit | 5b07bb3df03cc95d6b50a8eec2bd30563a6d5b43 (patch) | |
tree | 3737978877d4779221680e4591d792fc795796a3 /app/src/test | |
parent | 2522e75f41b2bc2f3d21baee09338527f271ba7c (diff) |
save manually added providers in encrypted shared preferences instead of external files dir
Diffstat (limited to 'app/src/test')
4 files changed, 42 insertions, 57 deletions
diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java index c6e548ce..e78db39c 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java @@ -160,6 +160,11 @@ public class ProviderApiManagerTest { return hasNetworkConnection; } + @Override + public void saveProvider(Provider p) { + + } + } @Before diff --git a/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java index 7584fb3f..925d2464 100644 --- a/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java @@ -5,11 +5,9 @@ import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static se.leap.bitmaskclient.testutils.MockHelper.mockInputStreamHelper; +import android.content.SharedPreferences; import android.content.res.AssetManager; import org.junit.After; @@ -18,14 +16,15 @@ import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import java.io.File; import java.io.InputStream; -import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; +import se.leap.bitmaskclient.base.models.Constants; import se.leap.bitmaskclient.base.models.Provider; -import se.leap.bitmaskclient.base.utils.FileHelper; -import se.leap.bitmaskclient.base.utils.InputStreamHelper; +import se.leap.bitmaskclient.base.utils.PreferenceHelper; import se.leap.bitmaskclient.testutils.MockHelper; +import se.leap.bitmaskclient.testutils.MockSharedPreferences; /** * Created by cyberta on 20.02.18. @@ -33,16 +32,14 @@ import se.leap.bitmaskclient.testutils.MockHelper; public class ProviderManagerTest { private AssetManager assetManager; - private File file; private ProviderManager providerManager; - InputStreamHelper inputStreamHelper; - FileHelper fileHelper; + PreferenceHelper preferenceHelper; + SharedPreferences mockSharedPrefs; MockHelper.MockFileHelper mockedFileHelperInterface; @Before public void setup() throws Exception { assetManager = mock(AssetManager.class); - file = mock(File.class); when(assetManager.open(anyString())).thenAnswer(new Answer<InputStream>() { @Override @@ -66,22 +63,12 @@ public class ProviderManagerTest { } }); - //mock File methods - //------------------ - when(file.isDirectory()).thenReturn(true); - - ArrayList<String> mockedCustomProviderList = new ArrayList<>(); - mockedCustomProviderList.add("leapcolombia.json"); - String[] mockedCustomProviderArray = new String[mockedCustomProviderList.size()]; - mockedCustomProviderArray = mockedCustomProviderList.toArray(mockedCustomProviderArray); - when(file.list()).thenReturn(mockedCustomProviderArray); - - when(file.getAbsolutePath()).thenReturn("externalDir"); - when(file.getPath()).thenReturn("externalDir"); - mockedFileHelperInterface = new MockHelper.MockFileHelper(file); - fileHelper = new FileHelper(mockedFileHelperInterface); - inputStreamHelper = mockInputStreamHelper(); + mockSharedPrefs = new MockSharedPreferences(); + preferenceHelper = new PreferenceHelper(mockSharedPrefs); + HashSet<Provider> customProviders = new HashSet<>(); + customProviders.add(Provider.createCustomProvider("https://leapcolombia.org", "leapcolombia.org")); + PreferenceHelper.setCustomProviders(customProviders); } @After @@ -91,21 +78,21 @@ public class ProviderManagerTest { @Test public void testSize_dummyEntry_has5ProvidersWithCurrentTestSetup() { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); providerManager.setAddDummyEntry(true); assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.size()); } @Test public void testSize_has4ProvidersWithCurrentTestSetup() { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.size()); } @Test public void testAdd_dummyEntry_newCustomProviderThatIsNotPartOfDefaultNorCustomList_returnTrue() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); providerManager.setAddDummyEntry(true); Provider customProvider = new Provider("https://anewprovider.org"); assertTrue("custom provider added: ", providerManager.add(customProvider)); @@ -114,7 +101,7 @@ public class ProviderManagerTest { @Test public void testAdd_newCustomProviderThatIsNotPartOfDefaultNorCustomList_returnTrue() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); Provider customProvider = new Provider("https://anewprovider.org"); assertTrue("custom provider added: ", providerManager.add(customProvider)); assertEquals("3 preconfigured, 2 custom providers", 5, providerManager.providers().size()); @@ -122,7 +109,7 @@ public class ProviderManagerTest { @Test public void testAdd_dummyEntry_newCustomProviderThatIsNotPartOfDefaultButOfCustomList_returnFalse() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); providerManager.setAddDummyEntry(true); Provider customProvider = new Provider("https://leapcolombia.org"); assertFalse("custom provider added: ", providerManager.add(customProvider)); @@ -131,7 +118,7 @@ public class ProviderManagerTest { @Test public void testAdd_newCustomProviderThatIsNotPartOfDefaultButOfCustomList_returnFalse() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); Provider customProvider = new Provider("https://leapcolombia.org"); assertFalse("custom provider added: ", providerManager.add(customProvider)); assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.providers().size()); @@ -139,7 +126,7 @@ public class ProviderManagerTest { @Test public void testAdd_newCustomProviderThatIsPartOfDefaultButNotOfCustomList_returnFalse() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); Provider customProvider = new Provider("https://demo.bitmask.net"); assertFalse("custom provider added: ", providerManager.add(customProvider)); assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.providers().size()); @@ -147,7 +134,7 @@ public class ProviderManagerTest { @Test public void testRemove_ProviderIsPartOfDefaultButNotCustomList_returnsFalse() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); Provider customProvider = new Provider("https://demo.bitmask.net"); assertFalse("custom provider not removed: ", providerManager.remove(customProvider)); assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.providers().size()); @@ -155,7 +142,7 @@ public class ProviderManagerTest { @Test public void testRemove_ProviderIsNotPartOfDefaultButOfCustomList_returnsTrue() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); Provider customProvider = new Provider("https://leapcolombia.org"); assertTrue("custom provider not removed: ", providerManager.remove(customProvider)); assertEquals("3 preconfigured, 0 custom providers", 3, providerManager.providers().size()); @@ -163,7 +150,7 @@ public class ProviderManagerTest { @Test public void testRemove_ProviderIsNotPartOfDefaultNorOfCustomList_returnsFalse() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); Provider customProvider = new Provider("https://anotherprovider.org"); assertFalse("custom provider not removed: ", providerManager.remove(customProvider)); assertEquals("3 preconfigured, 1 custom providers", 4, providerManager.providers().size()); @@ -171,7 +158,7 @@ public class ProviderManagerTest { @Test public void testClear_dummyEntry_ProvidersListHasOnlyDummyProvider() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); providerManager.setAddDummyEntry(true); providerManager.clear(); assertEquals("1 providers", 1, providerManager.providers().size()); @@ -180,35 +167,37 @@ public class ProviderManagerTest { @Test public void testClear_noEntries() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); providerManager.clear(); assertEquals("no providers", 0, providerManager.providers().size()); } @Test public void testSaveCustomProvidersToFile_CustomProviderDeleted_deletesFromDir() throws Exception { - when(file.exists()).thenReturn(true); - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); //leapcolombia is mocked custom provider from setup Provider customProvider = new Provider("https://leapcolombia.org"); providerManager.remove(customProvider); - providerManager.saveCustomProvidersToFile(); - verify(file, times(1)).delete(); + providerManager.saveCustomProviders(); + Set<String> providerSet = mockSharedPrefs.getStringSet(Constants.CUSTOM_PROVIDER_DOMAINS, new HashSet<>()); + assertEquals("provider was removed", 0, providerSet.size()); + assertEquals("preference helper has 0 custom providers", 0, PreferenceHelper.getCustomProviders().size()); + } @Test public void testSaveCustomProvidersToFile_newCustomProviders_persistNew() throws Exception { - when(file.list()).thenReturn(new String[0]); - when(file.exists()).thenReturn(false); - providerManager = ProviderManager.getInstance(assetManager, file); + providerManager = ProviderManager.getInstance(assetManager); Provider customProvider = new Provider("https://anotherprovider.org"); Provider secondCustomProvider = new Provider("https://yetanotherprovider.org"); providerManager.add(customProvider); providerManager.add(secondCustomProvider); - providerManager.saveCustomProvidersToFile(); + providerManager.saveCustomProviders(); + Set<String> providerSet = mockSharedPrefs.getStringSet(Constants.CUSTOM_PROVIDER_DOMAINS, new HashSet<>()); + assertEquals("persist was called twice", 2, providerSet.size()); + assertEquals("PreferenceHelper has 2 providers", 2, PreferenceHelper.getCustomProviders().size()); - assertEquals("persist was called twice", 2, mockedFileHelperInterface.getPersistFileCounter()); } diff --git a/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java index 3175b0ad..a7d3e19c 100644 --- a/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java +++ b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java @@ -133,16 +133,6 @@ public class MockHelper { return resultReceiver; } - - public static InputStreamHelper mockInputStreamHelper() { - return new InputStreamHelper(new InputStreamHelper.InputStreamHelperInterface() { - @Override - public InputStream getInputStreamFrom(String filePath) { - return getClass().getClassLoader().getResourceAsStream(filePath); - } - }); - } - public static class MockFileHelper implements FileHelper.FileHelperInterface { private final File file; private int persistFileCounter = 0; diff --git a/app/src/test/java/se/leap/bitmaskclient/testutils/MockSharedPreferences.java b/app/src/test/java/se/leap/bitmaskclient/testutils/MockSharedPreferences.java index 30ced782..2fd2a2a8 100644 --- a/app/src/test/java/se/leap/bitmaskclient/testutils/MockSharedPreferences.java +++ b/app/src/test/java/se/leap/bitmaskclient/testutils/MockSharedPreferences.java @@ -158,6 +158,7 @@ public class MockSharedPreferences implements SharedPreferences { mockedStringPrefs = new HashMap<>(tempStrings); mockedBooleanPrefs = new HashMap<>(tempBoolean); mockedIntPrefs = new HashMap<>(tempIntegers); + mockedStringSetPrefs = new HashMap<>(tempStringSets); } }; } |