diff options
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/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 |
4 files changed, 50 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/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); |