diff options
author | lalottacontinua <loddatio@posteo.de> | 2018-06-27 22:44:33 +0200 |
---|---|---|
committer | lalottacontinua <loddatio@posteo.de> | 2018-06-27 22:44:33 +0200 |
commit | d63dd4b36701f2f993e671f8cc3c5424b787e43a (patch) | |
tree | 486a8c3a1a688e76c7da7f5d9f8dfddbcc0b58aa /app/src/test | |
parent | 277f0f12ca505797daec42a5211885ac07b909f9 (diff) | |
parent | 8c9879145e588e985e5305ee2f88dfc1b6bf1d10 (diff) |
Merge branch 'master' into add_provider
Diffstat (limited to 'app/src/test')
-rw-r--r-- | app/src/test/java/se/leap/bitmaskclient/PreferenceHelperTest.java (renamed from app/src/test/java/se/leap/bitmaskclient/ConfigHelperTest.java) | 14 | ||||
-rw-r--r-- | app/src/test/java/se/leap/bitmaskclient/ProviderManagerTest.java | 189 | ||||
-rw-r--r-- | app/src/test/java/se/leap/bitmaskclient/ProviderTest.java | 2 | ||||
-rw-r--r-- | app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java | 22 | ||||
-rw-r--r-- | app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java | 39 |
5 files changed, 239 insertions, 27 deletions
diff --git a/app/src/test/java/se/leap/bitmaskclient/ConfigHelperTest.java b/app/src/test/java/se/leap/bitmaskclient/PreferenceHelperTest.java index 2c7848bc..d49fa08c 100644 --- a/app/src/test/java/se/leap/bitmaskclient/ConfigHelperTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/PreferenceHelperTest.java @@ -6,6 +6,8 @@ import org.junit.Before; import org.junit.Test; import se.leap.bitmaskclient.testutils.MockSharedPreferences; +import se.leap.bitmaskclient.utils.ConfigHelper; +import se.leap.bitmaskclient.utils.PreferenceHelper; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -14,11 +16,13 @@ import static se.leap.bitmaskclient.Constants.PROVIDER_EIP_DEFINITION; import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY; import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString; +import static se.leap.bitmaskclient.utils.PreferenceHelper.getSavedProviderFromSharedPreferences; +import static se.leap.bitmaskclient.utils.PreferenceHelper.providerInSharedPreferences; /** * Created by cyberta on 17.01.18. */ -public class ConfigHelperTest { +public class PreferenceHelperTest { private SharedPreferences mockPreferences; @@ -30,18 +34,18 @@ public class ConfigHelperTest { @Test public void providerInSharedPreferences_notInPreferences_returnsFalse() throws Exception { - assertFalse(ConfigHelper.providerInSharedPreferences(mockPreferences)); + assertFalse(providerInSharedPreferences(mockPreferences)); } @Test public void providerInSharedPreferences_inPreferences_returnsTrue() throws Exception { mockPreferences.edit().putBoolean(PROVIDER_CONFIGURED, true).apply(); - assertTrue(ConfigHelper.providerInSharedPreferences(mockPreferences)); + assertTrue(providerInSharedPreferences(mockPreferences)); } @Test public void getSavedProviderFromSharedPreferences_notInPreferences_returnsDefaultProvider() throws Exception { - Provider provider = ConfigHelper.getSavedProviderFromSharedPreferences(mockPreferences); + Provider provider = getSavedProviderFromSharedPreferences(mockPreferences); assertFalse(provider.isConfigured()); } @@ -55,7 +59,7 @@ public class ConfigHelperTest { .putString(PROVIDER_VPN_CERTIFICATE, getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.vpn_cert.pem"))) .putString(PROVIDER_PRIVATE_KEY, getInputAsString(getClass().getClassLoader().getResourceAsStream("private_rsa_key.pem"))) .apply(); - Provider provider = ConfigHelper.getSavedProviderFromSharedPreferences(mockPreferences); + Provider provider = getSavedProviderFromSharedPreferences(mockPreferences); assertTrue(provider.isConfigured()); } diff --git a/app/src/test/java/se/leap/bitmaskclient/ProviderManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/ProviderManagerTest.java new file mode 100644 index 00000000..1914f989 --- /dev/null +++ b/app/src/test/java/se/leap/bitmaskclient/ProviderManagerTest.java @@ -0,0 +1,189 @@ +package se.leap.bitmaskclient; + +import android.content.res.AssetManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.File; +import java.io.InputStream; +import java.util.ArrayList; + +import se.leap.bitmaskclient.utils.ConfigHelper; +import se.leap.bitmaskclient.utils.FileHelper; +import se.leap.bitmaskclient.utils.InputStreamHelper; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.verifyStatic; +import static se.leap.bitmaskclient.testutils.MockHelper.mockFileHelper; +import static se.leap.bitmaskclient.testutils.MockHelper.mockInputStreamHelper; + +/** + * Created by cyberta on 20.02.18. + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({ConfigHelper.class, FileHelper.class, InputStreamHelper.class}) +public class ProviderManagerTest { + + @Mock + private AssetManager assetManager; + @Mock + private File file; + private ProviderManager providerManager; + + @Before + public void setup() throws Exception { + //mock assetManager methods + //-------------------------- + when(assetManager.open(anyString())).thenAnswer(new Answer<InputStream>() { + @Override + public InputStream answer(InvocationOnMock invocation) throws Throwable { + String filename = "preconfigured/" + invocation.getArguments()[0]; + return getClass().getClassLoader().getResourceAsStream(filename); + } + }); + when(assetManager.list(anyString())).thenAnswer(new Answer<String[]>() { + @Override + public String[] answer(InvocationOnMock invocation) throws Throwable { + String path = (String) invocation.getArguments()[0]; + if ("urls".equals(path)) { + String[] preconfiguredUrls = new String[3]; + preconfiguredUrls[0] = "calyx.net.url"; + preconfiguredUrls[1] = "demo.bitmask.net.url"; + preconfiguredUrls[2] = "riseup.net.url"; + return preconfiguredUrls; + } else + throw new IllegalArgumentException("You need to implement the expected path manually!"); + } + }); + + //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"); + mockFileHelper(file); + + // mock inputStream + //----------------------------------- + mockInputStreamHelper(); + + } + + @After + public void tearDown() { + ProviderManager.reset(); + } + + @Test + public void testSize_has5ProvidersWithCurrentTestSetup() { + providerManager = ProviderManager.getInstance(assetManager, file); + assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.size()); + } + + @Test + public void testAdd_newCustomProviderThatIsNotPartOfDefaultNorCustomList_returnTrue() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://anewprovider.org"); + assertTrue("custom provider added: ", providerManager.add(customProvider)); + assertEquals("3 preconfigured, 2 custom providers, 1 dummy provider", 6, providerManager.providers().size()); + } + + @Test + public void testAdd_newCustomProviderThatIsNotPartOfDefaultButOfCustomList_returnFalse() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://leapcolombia.org"); + assertFalse("custom provider added: ", providerManager.add(customProvider)); + assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.providers().size()); + } + + @Test + public void testAdd_newCustomProviderThatIsPartOfDefaultButNotOfCustomList_returnFalse() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://demo.bitmask.net"); + assertFalse("custom provider added: ", providerManager.add(customProvider)); + assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.providers().size()); + } + + @Test + public void testRemove_ProviderIsPartOfDefaultButNotCustomList_returnsFalse() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://demo.bitmask.net"); + assertFalse("custom provider not removed: ", providerManager.remove(customProvider)); + assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.providers().size()); + } + + @Test + public void testRemove_ProviderIsNotPartOfDefaultButOfCustomList_returnsTrue() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://leapcolombia.org"); + assertTrue("custom provider not removed: ", providerManager.remove(customProvider)); + assertEquals("3 preconfigured, 0 custom providers, 1 dummy provider", 4, providerManager.providers().size()); + } + + @Test + public void testRemove_ProviderIsNotPartOfDefaultNorOfCustomList_returnsFalse() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://anotherprovider.org"); + assertFalse("custom provider not removed: ", providerManager.remove(customProvider)); + assertEquals("3 preconfigured, 1 custom providers, 1 dummy provider", 5, providerManager.providers().size()); + } + + @Test + public void testClear_ProvidersListHasOnlyDummyProvider() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + providerManager.clear(); + assertEquals("1 providers", 1, providerManager.providers().size()); + assertEquals("provider is dummy element", "https://example.net", providerManager.get(0).getMainUrlString()); + } + + @Test + public void testSaveCustomProvidersToFile_CustomProviderDeleted_deletesFromDir() throws Exception { + when(file.exists()).thenReturn(true); + providerManager = ProviderManager.getInstance(assetManager, file); + //leapcolombia is mocked custom provider from setup + Provider customProvider = new Provider("https://leapcolombia.org"); + providerManager.remove(customProvider); + providerManager.saveCustomProvidersToFile(); + verify(file, times(1)).delete(); + } + + + @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); + Provider customProvider = new Provider("https://anotherprovider.org"); + Provider secondCustomProvider = new Provider("https://yetanotherprovider.org"); + providerManager.add(customProvider); + providerManager.add(secondCustomProvider); + providerManager.saveCustomProvidersToFile(); + + verifyStatic(FileHelper.class, times(2)); + FileHelper.persistFile(any(File.class), anyString()); + } + + +}
\ No newline at end of file diff --git a/app/src/test/java/se/leap/bitmaskclient/ProviderTest.java b/app/src/test/java/se/leap/bitmaskclient/ProviderTest.java index 495d5b3f..a141edec 100644 --- a/app/src/test/java/se/leap/bitmaskclient/ProviderTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/ProviderTest.java @@ -1,9 +1,7 @@ package se.leap.bitmaskclient; -import org.json.JSONException; import org.junit.Test; -import java.io.IOException; import java.util.HashSet; import java.util.Set; 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 4842d170..7283968b 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java @@ -38,13 +38,15 @@ import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateEncodingException; -import se.leap.bitmaskclient.ConfigHelper; +import se.leap.bitmaskclient.testutils.MockHelper; +import se.leap.bitmaskclient.utils.ConfigHelper; import se.leap.bitmaskclient.Provider; import se.leap.bitmaskclient.ProviderAPI; import se.leap.bitmaskclient.ProviderApiConnector; import se.leap.bitmaskclient.ProviderApiManager; import se.leap.bitmaskclient.ProviderApiManagerBase; import se.leap.bitmaskclient.testutils.MockSharedPreferences; +import se.leap.bitmaskclient.utils.PreferenceHelper; import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY; import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; @@ -58,6 +60,7 @@ import static se.leap.bitmaskclient.testutils.MockHelper.mockClientGenerator; import static se.leap.bitmaskclient.testutils.MockHelper.mockConfigHelper; import static se.leap.bitmaskclient.testutils.MockHelper.mockFingerprintForCertificate; import static se.leap.bitmaskclient.testutils.MockHelper.mockIntent; +import static se.leap.bitmaskclient.testutils.MockHelper.mockPreferenceHelper; import static se.leap.bitmaskclient.testutils.MockHelper.mockProviderApiConnector; import static se.leap.bitmaskclient.testutils.MockHelper.mockResources; import static se.leap.bitmaskclient.testutils.MockHelper.mockResultReceiver; @@ -72,7 +75,7 @@ import static se.leap.bitmaskclient.testutils.TestSetupHelper.getProvider; */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ProviderApiManager.class, TextUtils.class, ConfigHelper.class, ProviderApiConnector.class}) +@PrepareForTest({ProviderApiManager.class, TextUtils.class, ConfigHelper.class, ProviderApiConnector.class, PreferenceHelper.class}) public class ProviderApiManagerTest { private SharedPreferences mockPreferences; @@ -172,8 +175,8 @@ public class ProviderApiManagerTest { @Test public void test_handleIntentSetupProvider_happyPath_storedProviderAndCAFromPreviousSetup() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException { Provider provider = new Provider("https://riseup.net"); - mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494", getConfiguredProvider()); - + mockPreferenceHelper(getConfiguredProvider()); + mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494"); mockProviderApiConnector(NO_ERROR); mockPreferences.edit().putString(Provider.KEY + ".riseup.net", getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.json"))).apply(); mockPreferences.edit().putString(Provider.CA_CERT + ".riseup.net", getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.pem"))).apply(); @@ -238,7 +241,8 @@ public class ProviderApiManagerTest { @Test public void test_handleIntentSetupProvider_storedProviderAndCAFromPreviousSetup_failedPinning() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException { Provider provider = new Provider("https://riseup.net"); - mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29495", getConfiguredProvider()); + mockPreferenceHelper(getConfiguredProvider()); + mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29495"); mockProviderApiConnector(NO_ERROR); mockPreferences.edit().putString(Provider.KEY + ".riseup.net", getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.json"))).apply(); @@ -308,8 +312,8 @@ public class ProviderApiManagerTest { @Test public void test_handleIntentSetupProvider_preseededProviderAndCA_ValidCertificateButUpdatedCertificateOnServerSide() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException { Provider provider = getConfiguredProvider(); - - mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494", getConfiguredProvider()); + mockPreferenceHelper(provider); + mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494"); mockProviderApiConnector(ERROR_CASE_UPDATED_CERTIFICATE); providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback()); @@ -331,8 +335,8 @@ public class ProviderApiManagerTest { @Test public void test_handleIntentSetupProvider_storedProviderAndCAFromPreviousSetup_ValidCertificateButUpdatedCertificateOnServerSide() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException { Provider provider = new Provider("https://riseup.net"); - - mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494", getConfiguredProvider()); + mockPreferenceHelper(getConfiguredProvider()); + mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494"); mockProviderApiConnector(ERROR_CASE_UPDATED_CERTIFICATE); mockPreferences.edit().putString(Provider.KEY + ".riseup.net", getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.json"))).apply(); mockPreferences.edit().putString(Provider.CA_CERT + ".riseup.net", getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.pem"))).apply(); 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 d85b050f..d68296a8 100644 --- a/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java +++ b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java @@ -14,6 +14,7 @@ import org.json.JSONObject; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -29,12 +30,15 @@ import java.util.Map; import java.util.Set; import okhttp3.OkHttpClient; -import se.leap.bitmaskclient.ConfigHelper; import se.leap.bitmaskclient.OkHttpClientGenerator; import se.leap.bitmaskclient.Provider; import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.testutils.BackendMockResponses.BackendMockProvider; import se.leap.bitmaskclient.testutils.matchers.BundleMatcher; +import se.leap.bitmaskclient.utils.ConfigHelper; +import se.leap.bitmaskclient.utils.FileHelper; +import se.leap.bitmaskclient.utils.InputStreamHelper; +import se.leap.bitmaskclient.utils.PreferenceHelper; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -49,6 +53,8 @@ import static org.mockito.Mockito.when; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY; import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.utils.FileHelper.createFile; +import static se.leap.bitmaskclient.utils.PreferenceHelper.getFromPersistedProvider; /** * Created by cyberta on 29.01.18. @@ -343,22 +349,36 @@ public class MockHelper { return resultReceiver; } - public static void mockConfigHelperForFileInputStream() throws FileNotFoundException { - mockStatic(ConfigHelper.class); - when(ConfigHelper.loadInputStreamAsString(any(InputStream.class))).thenCallRealMethod(); - when(ConfigHelper.getInputStreamFrom(anyString())).thenAnswer(new Answer<InputStream>() { + public static void mockInputStreamHelper() throws FileNotFoundException { + mockStatic(InputStreamHelper.class); + when(InputStreamHelper.loadInputStreamAsString(any(InputStream.class))).thenCallRealMethod(); + when(InputStreamHelper.getInputStreamFrom(anyString())).thenAnswer(new Answer<InputStream>() { @Override public InputStream answer(InvocationOnMock invocation) throws Throwable { String filename = (String) invocation.getArguments()[0]; return getClass().getClassLoader().getResourceAsStream(filename); } }); + + } + + public static void mockFileHelper(final File mockedFile) throws FileNotFoundException { + mockStatic(FileHelper.class); + when(createFile(any(File.class), anyString())).thenReturn(mockedFile); } - public static void mockConfigHelper(String mockedFingerprint, final Provider providerFromPrefs) throws CertificateEncodingException, NoSuchAlgorithmException { + public static void mockConfigHelper(String mockedFingerprint) throws CertificateEncodingException, NoSuchAlgorithmException { // FIXME use MockSharedPreferences instead of provider mockStatic(ConfigHelper.class); - when(ConfigHelper.getFromPersistedProvider(anyString(), anyString(), any(SharedPreferences.class))).thenAnswer(new Answer<String>() { + when(ConfigHelper.getFingerprintFromCertificate(any(X509Certificate.class), anyString())).thenReturn(mockedFingerprint); + when(ConfigHelper.checkErroneousDownload(anyString())).thenCallRealMethod(); + when(ConfigHelper.parseX509CertificateFromString(anyString())).thenCallRealMethod(); + } + + public static void mockPreferenceHelper(final Provider providerFromPrefs) { + // FIXME use MockSharedPreferences instead of provider + mockStatic(PreferenceHelper.class); + when(getFromPersistedProvider(anyString(), anyString(), any(SharedPreferences.class))).thenAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { String key = (String) invocation.getArguments()[0]; @@ -375,11 +395,8 @@ public class MockHelper { return null; } }); - when(ConfigHelper.getFingerprintFromCertificate(any(X509Certificate.class), anyString())).thenReturn(mockedFingerprint); - when(ConfigHelper.checkErroneousDownload(anyString())).thenCallRealMethod(); - when(ConfigHelper.parseX509CertificateFromString(anyString())).thenCallRealMethod(); - when(ConfigHelper.loadInputStreamAsString(any(InputStream.class))).thenCallRealMethod(); } + public static void mockFingerprintForCertificate(String mockedFingerprint) throws CertificateEncodingException, NoSuchAlgorithmException { mockStatic(ConfigHelper.class); when(ConfigHelper.getFingerprintFromCertificate(any(X509Certificate.class), anyString())).thenReturn(mockedFingerprint); |