summaryrefslogtreecommitdiff
path: root/app/src/test/java/se/leap
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/test/java/se/leap')
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java7
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java4
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java4
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java66
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java223
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java50
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java38
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java2
8 files changed, 245 insertions, 149 deletions
diff --git a/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java b/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java
index 955ddc44..e03fccff 100644
--- a/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java
@@ -13,6 +13,7 @@ import static org.junit.Assert.assertTrue;
import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_EIP_DEFINITION;
import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_PRIVATE_KEY;
import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_VPN_CERTIFICATE;
+import static se.leap.bitmaskclient.base.utils.PreferenceHelper.preferUDP;
import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString;
import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getSavedProviderFromSharedPreferences;
@@ -22,16 +23,18 @@ import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getSavedProvider
public class PreferenceHelperTest {
private SharedPreferences mockPreferences;
+ PreferenceHelper preferenceHelper;
@Before
public void setup() {
mockPreferences = new MockSharedPreferences();
+ preferenceHelper = new PreferenceHelper(mockPreferences);
}
@Test
public void getSavedProviderFromSharedPreferences_notInPreferences_returnsDefaultProvider() throws Exception {
- Provider provider = getSavedProviderFromSharedPreferences(mockPreferences);
+ Provider provider = getSavedProviderFromSharedPreferences();
assertFalse(provider.isConfigured());
}
@@ -45,7 +48,7 @@ public class PreferenceHelperTest {
.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 = getSavedProviderFromSharedPreferences(mockPreferences);
+ Provider provider = getSavedProviderFromSharedPreferences();
assertTrue(provider.isConfigured());
}
diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java
index 1e15c5ab..cec627ef 100644
--- a/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java
@@ -61,7 +61,7 @@ public class EipStatusTest {
VpnProfile mockVpnProfile = new VpnProfile("mockProfile", OPENVPN);
mockVpnProfile.mPersistTun = true;
doNothing().when(PreferenceHelper.class);
- VpnStatus.setLastConnectedVpnProfile(null, mockVpnProfile);
+ VpnStatus.setLastConnectedVpnProfile(mockVpnProfile);
VpnStatus.updateStateString("SCREENOFF", "", R.string.state_screenoff, LEVEL_VPNPAUSED);
assertTrue("LEVEL_VPN_PAUSED eipLevel", eipStatus.getEipLevel() == CONNECTING);
assertTrue("LEVEL_VPN_PAUSED level", eipStatus.getLevel() == LEVEL_VPNPAUSED);
@@ -74,7 +74,7 @@ public class EipStatusTest {
VpnProfile mockVpnProfile = new VpnProfile("mockProfile", OPENVPN);
mockVpnProfile.mPersistTun = false;
doNothing().when(PreferenceHelper.class);
- VpnStatus.setLastConnectedVpnProfile(null, mockVpnProfile);
+ VpnStatus.setLastConnectedVpnProfile(mockVpnProfile);
VpnStatus.updateStateString("SCREENOFF", "", R.string.state_screenoff, LEVEL_VPNPAUSED);
assertTrue("LEVEL_VPN_PAUSED eipLevel", eipStatus.getEipLevel() == DISCONNECTED);
assertTrue("LEVEL_VPN_PAUSED level", eipStatus.getLevel() == LEVEL_VPNPAUSED);
diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java
index 6f26e494..74762813 100644
--- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java
@@ -24,6 +24,8 @@ import java.util.ArrayList;
import de.blinkt.openvpn.core.ConfigParser;
import se.leap.bitmaskclient.base.models.Provider;
import se.leap.bitmaskclient.base.utils.ConfigHelper;
+import se.leap.bitmaskclient.base.utils.PreferenceHelper;
+import se.leap.bitmaskclient.testutils.MockSharedPreferences;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
@@ -69,6 +71,7 @@ public class GatewaySelectorTest {
*/
+ PreferenceHelper preferenceHelper;
GatewaySelector gatewaySelector;
JSONObject eipDefinition;
ArrayList<Gateway> gatewayList = new ArrayList<>();
@@ -78,6 +81,7 @@ public class GatewaySelectorTest {
mockStatic(ConfigHelper.class);
when(ConfigHelper.timezoneDistance(anyInt(), anyInt())).thenCallRealMethod();
mockTextUtils();
+ preferenceHelper = new PreferenceHelper(new MockSharedPreferences());
eipDefinition = new JSONObject(getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-four-gateways.json")));
JSONArray gateways = eipDefinition.getJSONArray("gateways");
for (int i = 0; i < gateways.length(); i++) {
diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java
index 1f5aa74b..86373ff6 100644
--- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java
@@ -84,7 +84,7 @@ public class GatewaysManagerTest {
putString(PROVIDER_VPN_CERTIFICATE, secrets.getString(PROVIDER_VPN_CERTIFICATE))
.commit();
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getSharedPreferences(any())).thenReturn(sharedPreferences);
+ PreferenceHelper preferenceHelper = new PreferenceHelper(sharedPreferences);
}
@@ -211,7 +211,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals("37.12.247.10", gatewaysManager.select(0).gateway.getRemoteIP());
@@ -223,8 +223,8 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
- when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(true);
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
+ when(PreferenceHelper.allowExperimentalTransports()).thenReturn(true);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
ArrayList<String> hosts = new ArrayList<>();
hosts.add(gatewaysManager.select(0).gateway.getHost());
@@ -241,8 +241,8 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
- when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(true);
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
+ when(PreferenceHelper.allowExperimentalTransports()).thenReturn(true);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
ArrayList<String> hosts = new ArrayList<>();
@@ -258,8 +258,8 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
- when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
+ when(PreferenceHelper.allowExperimentalTransports()).thenReturn(false);
for (int i = 0; i < 1000; i++) {
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
@@ -273,8 +273,8 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
- when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
+ when(PreferenceHelper.allowExperimentalTransports()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals("bridge-nyc1-01.bitmask-dev.leapvpn.net", gatewaysManager.select(0).gateway.getHost());
@@ -287,8 +287,8 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
- when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
+ when(PreferenceHelper.allowExperimentalTransports()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
ArrayList<String> hosts = new ArrayList<>();
@@ -306,7 +306,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
//use openvpn, not pluggable transports
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals("manila.bitmask.net", gatewaysManager.select(0).gateway.getHost());
@@ -321,7 +321,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
//use openvpn, not pluggable transports
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals("moscow.bitmask.net", gatewaysManager.select(0).gateway.getHost());
@@ -337,8 +337,8 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
//use openvpn, not pluggable transports
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
- when(PreferenceHelper.getPreferredCity(any(Context.class))).thenReturn("Paris");
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
+ when(PreferenceHelper.getPreferredCity()).thenReturn("Paris");
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals("mouette.riseup.net", gatewaysManager.select(0).gateway.getHost());
@@ -353,8 +353,8 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
//use openvpn, not pluggable transports
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
- when(PreferenceHelper.getPreferredCity(any(Context.class))).thenReturn("Paris");
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
+ when(PreferenceHelper.getPreferredCity()).thenReturn("Paris");
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals("mouette.riseup.net", gatewaysManager.select(0).gateway.getHost());
@@ -370,8 +370,8 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
//use openvpn, not pluggable transports
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
- when(PreferenceHelper.getPreferredCity(any(Context.class))).thenReturn("Paris");
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
+ when(PreferenceHelper.getPreferredCity()).thenReturn("Paris");
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals("Paris", gatewaysManager.select(0).gateway.getName());
@@ -387,7 +387,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
//use openvpn, not pluggable transports
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals("mouette.riseup.net", gatewaysManager.select(0, "Paris").gateway.getHost());
@@ -402,7 +402,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
//use openvpn, not pluggable transports
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals("mouette.riseup.net", gatewaysManager.select(0, "Paris").gateway.getHost());
@@ -418,7 +418,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
//use openvpn, not pluggable transports
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals("Paris", gatewaysManager.select(0, "Paris").gateway.getName());
@@ -435,7 +435,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
//use openvpn, not pluggable transports
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertNull(gatewaysManager.select(0, "Stockholm"));
}
@@ -446,7 +446,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
List<Location> locations = gatewaysManager.getGatewayLocations();
@@ -467,7 +467,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
sharedPreferences.edit().putBoolean(USE_BRIDGES, true).commit();
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
List<Location> locations = gatewaysManager.getGatewayLocations();
@@ -498,7 +498,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
sharedPreferences.edit().putBoolean(USE_BRIDGES, true).commit();
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
List<Location> locations = gatewaysManager.getGatewayLocations();
@@ -523,7 +523,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
sharedPreferences.edit().putBoolean(USE_BRIDGES, false).commit();
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
List<Location> locations = gatewaysManager.getGatewayLocations();
@@ -549,7 +549,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
List<Location> locations = gatewaysManager.getSortedGatewayLocations(OPENVPN);
@@ -578,7 +578,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
List<Location> locations = gatewaysManager.getSortedGatewayLocations(OBFS4);
@@ -608,7 +608,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
List<Location> locations = gatewaysManager.getSortedGatewayLocations(OBFS4);
@@ -621,7 +621,7 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals(0.3, gatewaysManager.getLocation("Amsterdam").getAverageLoad(OBFS4));
@@ -648,7 +648,7 @@ public class GatewaysManagerTest {
Provider provider = getProvider(null, null, null, null, null, null, "decoupled_pt.eip-service.json", null);
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
- when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(true);
+ when(PreferenceHelper.allowExperimentalTransports()).thenReturn(true);
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
assertEquals(3, gatewaysManager.size());
}
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 feeaf796..a478af63 100644
--- a/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java
@@ -17,50 +17,12 @@
package se.leap.bitmaskclient.eip;
-import android.content.Context;
-import android.content.Intent;
-import android.content.SharedPreferences;
-import android.content.res.Resources;
-import android.os.Bundle;
-import android.text.TextUtils;
-
-import androidx.annotation.Nullable;
-
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Answers;
-import org.mockito.Mock;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-
-import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
-import java.security.cert.CertificateEncodingException;
-import java.util.concurrent.TimeoutException;
-
-import se.leap.bitmaskclient.BuildConfig;
-import se.leap.bitmaskclient.base.models.Provider;
-import se.leap.bitmaskclient.base.utils.ConfigHelper;
-import se.leap.bitmaskclient.base.utils.PreferenceHelper;
-import se.leap.bitmaskclient.providersetup.ProviderAPI;
-import se.leap.bitmaskclient.providersetup.ProviderApiConnector;
-import se.leap.bitmaskclient.providersetup.ProviderApiManager;
-import se.leap.bitmaskclient.providersetup.ProviderApiManagerBase;
-import se.leap.bitmaskclient.testutils.MockSharedPreferences;
-import se.leap.bitmaskclient.tor.TorStatusObservable;
-
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.when;
import static se.leap.bitmaskclient.base.models.Constants.BROADCAST_RESULT_KEY;
import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_START;
import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_KEY;
-import static se.leap.bitmaskclient.base.models.Constants.USE_BRIDGES;
-import static se.leap.bitmaskclient.base.models.Constants.USE_SNOWFLAKE;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.CORRECTLY_DOWNLOADED_GEOIP_JSON;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.CORRECTLY_UPDATED_INVALID_VPN_CERTIFICATE;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORS;
@@ -94,9 +56,42 @@ import static se.leap.bitmaskclient.testutils.MockHelper.mockTextUtils;
import static se.leap.bitmaskclient.testutils.MockHelper.mockTorStatusObservable;
import static se.leap.bitmaskclient.testutils.TestSetupHelper.getConfiguredProvider;
import static se.leap.bitmaskclient.testutils.TestSetupHelper.getConfiguredProviderAPIv4;
-import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString;
import static se.leap.bitmaskclient.testutils.TestSetupHelper.getProvider;
+import android.content.Context;
+import android.content.Intent;
+import android.content.res.Resources;
+import android.os.Bundle;
+import android.text.TextUtils;
+
+import androidx.annotation.Nullable;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateEncodingException;
+import java.util.concurrent.TimeoutException;
+
+import se.leap.bitmaskclient.BuildConfig;
+import se.leap.bitmaskclient.base.models.Provider;
+import se.leap.bitmaskclient.base.utils.ConfigHelper;
+import se.leap.bitmaskclient.base.utils.PreferenceHelper;
+import se.leap.bitmaskclient.providersetup.ProviderAPI;
+import se.leap.bitmaskclient.providersetup.ProviderApiConnector;
+import se.leap.bitmaskclient.providersetup.ProviderApiManager;
+import se.leap.bitmaskclient.providersetup.ProviderApiManagerBase;
+import se.leap.bitmaskclient.tor.TorStatusObservable;
+
/**
* Created by cyberta on 04.01.18.
@@ -106,7 +101,6 @@ import static se.leap.bitmaskclient.testutils.TestSetupHelper.getProvider;
@PrepareForTest({ProviderApiManager.class, TextUtils.class, ConfigHelper.RSAHelper.class, ConfigHelper.class, ProviderApiConnector.class, PreferenceHelper.class, TorStatusObservable.class, android.util.Base64.class})
public class ProviderApiManagerTest {
- private SharedPreferences mockPreferences;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Resources mockResources;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -166,15 +160,14 @@ public class ProviderApiManagerTest {
Intent intent = mockIntent();
PowerMockito.whenNew(Intent.class).withAnyArguments().thenReturn(intent);
mockTextUtils();
- mockPreferences = new MockSharedPreferences();
mockResources = mockResources(getClass().getClassLoader().getResourceAsStream("error_messages.json"));
}
@Test
public void test_handleIntentSetupProvider_noProviderMainURL() throws IOException, JSONException {
Provider provider = new Provider("");
-
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ mockPreferenceHelper(provider);
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -193,10 +186,10 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentSetupProvider_happyPath_preseededProviderAndCA() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException {
Provider provider = getConfiguredProvider();
-
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(NO_ERROR);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, true);
@@ -215,10 +208,10 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentSetupProvider_happyPath_no_preseededProviderAndCA() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException {
Provider provider = getConfiguredProvider();
-
+ mockPreferenceHelper(provider);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(NO_ERROR);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, true);
@@ -240,9 +233,7 @@ public class ProviderApiManagerTest {
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();
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, true);
@@ -260,9 +251,11 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentSetupProvider_preseededProviderAndCA_failedCAPinning() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException {
Provider provider = getConfiguredProvider();
+
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29495");
mockProviderApiConnector(NO_ERROR);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
expectedResult.putString(ERRORS, "{\"errorId\":\"ERROR_CERTIFICATE_PINNING\",\"errors\":\"Stored provider certificate is invalid. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\"}");
@@ -282,9 +275,10 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentSetupProvider_no_preseededProviderAndCA_failedPinning() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException {
Provider provider = new Provider("https://riseup.net");
+ mockPreferenceHelper(provider);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29495");
mockProviderApiConnector(NO_ERROR);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -309,9 +303,7 @@ public class ProviderApiManagerTest {
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29495");
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();
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -332,9 +324,10 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentSetupProvider_preseededProviderAndCA_outdatedCertificate() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException {
Provider provider = getProvider(null ,null, null, null, "outdated_cert.pem", null, null, null);
+ mockPreferenceHelper(provider);
mockProviderApiConnector(NO_ERROR);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -353,12 +346,12 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentSetupProvider_storedProviderAndCAFromPreviousSetup_outdatedCertificate() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException {
- Provider provider = getConfiguredProvider(); //new Provider("https://riseup.net");
+ Provider provider = getProvider(null, null, null, null, "outdated_cert.pem", "riseup.net.json", null, null);
+ mockPreferenceHelper(provider);
+ PreferenceHelper.getEipDefinitionFromPreferences();
mockProviderApiConnector(NO_ERROR);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
- 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("outdated_cert.pem"))).apply();
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -382,7 +375,7 @@ public class ProviderApiManagerTest {
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(ERROR_CASE_UPDATED_CERTIFICATE);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
expectedResult.putString(ERRORS, "{\"errorId\":\"ERROR_INVALID_CERTIFICATE\",\"errors\":\"Stored provider certificate is invalid. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\"}");
@@ -405,9 +398,7 @@ public class ProviderApiManagerTest {
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();
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -429,10 +420,11 @@ public class ProviderApiManagerTest {
public void test_handleIntentSetupProvider_preseededProviderAndCA_failedConfiguration() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException {
Provider provider = getConfiguredProvider();
+ mockPreferenceHelper(provider);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(ERROR_CASE_MICONFIGURED_PROVIDER);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -455,12 +447,13 @@ public class ProviderApiManagerTest {
return;
}
Provider provider = getConfiguredProvider();
+ mockPreferenceHelper(provider);
mockProviderApiConnector(ERROR_CASE_MICONFIGURED_PROVIDER);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
when(ConfigHelper.isDefaultBitmask()).thenReturn(false);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -481,10 +474,10 @@ public class ProviderApiManagerTest {
public void test_handleIntentSetupProvider_outdatedPreseededProviderAndCA_successfulConfiguration() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException {
Provider provider = getProvider(null, null, null, null, null, "riseup_net_outdated_config.json", null, null);
-
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(NO_ERROR);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, true);
@@ -507,10 +500,11 @@ public class ProviderApiManagerTest {
}
Provider provider = new Provider("https://riseup.net");
+ mockPreferenceHelper(provider);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(ERROR_CASE_FETCH_EIP_SERVICE_CERTIFICATE_INVALID);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -534,11 +528,12 @@ public class ProviderApiManagerTest {
}
Provider inputProvider = getConfiguredProvider();
+ mockPreferenceHelper(inputProvider);
inputProvider.setGeoIpJson(new JSONObject());
Provider expectedProvider = getConfiguredProvider();
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(NO_ERROR);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(EIP_ACTION_START, true);
@@ -566,10 +561,14 @@ public class ProviderApiManagerTest {
}
Provider provider = getConfiguredProvider();
+ mockPreferenceHelper(provider);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(ERROR_GEOIP_SERVICE_IS_DOWN);
- mockPreferences.edit().putBoolean(USE_BRIDGES, false).putBoolean(USE_SNOWFLAKE, false).commit();
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
+ when(PreferenceHelper.getUseSnowflake()).thenReturn(false);
+ when(PreferenceHelper.hasSnowflakePrefs()).thenReturn(true);
+
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(EIP_ACTION_START, true);
@@ -597,9 +596,10 @@ public class ProviderApiManagerTest {
mockTorStatusObservable(null);
Provider provider = getConfiguredProvider();
+ mockPreferenceHelper(provider);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(ERROR_GEOIP_SERVICE_IS_DOWN_TOR_FALLBACK);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(EIP_ACTION_START, true);
@@ -629,9 +629,10 @@ public class ProviderApiManagerTest {
Provider provider = getConfiguredProvider();
provider.setLastGeoIpUpdate(System.currentTimeMillis());
+ mockPreferenceHelper(provider);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(NO_ERROR);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(EIP_ACTION_START, true);
@@ -659,9 +660,10 @@ public class ProviderApiManagerTest {
Provider provider = getConfiguredProvider();
provider.setGeoipUrl(null);
provider.setGeoIpJson(new JSONObject());
+ mockPreferenceHelper(provider);
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(NO_ERROR);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(EIP_ACTION_START, true);
@@ -683,10 +685,10 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentSetupProvider_APIv4_happyPath() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException {
Provider provider = getConfiguredProviderAPIv4();
-
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(NO_ERROR_API_V4);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, true);
@@ -703,11 +705,15 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentSetupProvider_TorFallback_SecondTryHappyPath() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException {
+
Provider provider = getConfiguredProviderAPIv4();
+ mockPreferenceHelper(provider);
+ when(PreferenceHelper.hasSnowflakePrefs()).thenReturn(false);
+ when(PreferenceHelper.getUseSnowflake()).thenReturn(true); // getUseSnowflake() defaults to true if not set
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(ERROR_DNS_RESUOLUTION_TOR_FALLBACK);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Intent providerApiCommand = mockIntent();
providerApiCommand.putExtra(PROVIDER_KEY, provider);
@@ -724,10 +730,10 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentSetupProvider_TorFallbackStartServiceException_SecondTryFailed() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException {
Provider provider = getConfiguredProviderAPIv4();
-
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(ERROR_DNS_RESUOLUTION_TOR_FALLBACK);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback(new IllegalStateException("Tor service start not failed."), true));
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback(new IllegalStateException("Tor service start not failed."), true));
Intent providerApiCommand = mockIntent();
providerApiCommand.putExtra(PROVIDER_KEY, provider);
@@ -743,10 +749,10 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentSetupProvider_TorFallbackTimeoutException_SecondTryFailed() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException {
Provider provider = getConfiguredProviderAPIv4();
-
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(ERROR_DNS_RESUOLUTION_TOR_FALLBACK);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Intent providerApiCommand = mockIntent();
providerApiCommand.putExtra(PROVIDER_KEY, provider);
@@ -763,11 +769,15 @@ public class ProviderApiManagerTest {
public void test_handleIntentSetupProvider_TorBridgesPreferenceEnabled_Success() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException {
Provider provider = getConfiguredProviderAPIv4();
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(NO_ERROR_API_V4);
- mockPreferences.edit().putBoolean(USE_BRIDGES, true).putBoolean(USE_SNOWFLAKE, true).commit();
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
+ when(PreferenceHelper.getUseSnowflake()).thenReturn(true);
+ when(PreferenceHelper.hasSnowflakePrefs()).thenReturn(true);
+
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Intent providerApiCommand = mockIntent();
providerApiCommand.putExtra(PROVIDER_KEY, provider);
@@ -784,11 +794,15 @@ public class ProviderApiManagerTest {
public void test_handleIntentSetupProvider_TorBridgesDisabled_TorNotStarted() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException {
Provider provider = getConfiguredProviderAPIv4();
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(NO_ERROR_API_V4);
- mockPreferences.edit().putBoolean(USE_BRIDGES, false).putBoolean(USE_SNOWFLAKE, false).commit();
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
+ when(PreferenceHelper.getUseSnowflake()).thenReturn(false);
+ when(PreferenceHelper.hasSnowflakePrefs()).thenReturn(true);
+
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Intent providerApiCommand = mockIntent();
providerApiCommand.putExtra(PROVIDER_KEY, provider);
@@ -804,13 +818,15 @@ public class ProviderApiManagerTest {
@Test
public void test_handleIntentUpdateVPNCertificate_TorBridgesPreferencesNotConfigured_TorStartedAndSuccess() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException {
Provider provider = getConfiguredProviderAPIv4();
-
+ mockPreferenceHelper(provider);
+ when(PreferenceHelper.hasSnowflakePrefs()).thenReturn(false);
+ when(PreferenceHelper.getUseSnowflake()).thenReturn(true);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockRSAHelper();
mockBase64();
mockProviderApiConnector(ERROR_DNS_RESUOLUTION_TOR_FALLBACK);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Intent providerApiCommand = mockIntent();
providerApiCommand.putExtra(PROVIDER_KEY, provider);
@@ -827,12 +843,15 @@ public class ProviderApiManagerTest {
public void test_handleIntentUpdateVPNCertificate_TorBridgesPreferencesFalse_TorNotStartedAndFailure() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException {
Provider provider = getConfiguredProviderAPIv4();
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockBase64();
mockProviderApiConnector(ERROR_DNS_RESUOLUTION_TOR_FALLBACK);
- mockPreferences.edit().putBoolean(USE_BRIDGES, false).putBoolean(USE_SNOWFLAKE, false).commit();
+ when(PreferenceHelper.getUseBridges()).thenReturn(false);
+ when(PreferenceHelper.getUseSnowflake()).thenReturn(false);
+ when(PreferenceHelper.hasSnowflakePrefs()).thenReturn(true);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Intent providerApiCommand = mockIntent();
providerApiCommand.putExtra(PROVIDER_KEY, provider);
@@ -849,13 +868,16 @@ public class ProviderApiManagerTest {
public void test_handleIntentUpdateVPNCertificate_TorBridgesPreferencesTrue_TorStartedAndSuccess() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException {
Provider provider = getConfiguredProviderAPIv4();
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockBase64();
mockRSAHelper();
mockProviderApiConnector(NO_ERROR_API_V4);
- mockPreferences.edit().putBoolean(USE_BRIDGES, true).putBoolean(USE_SNOWFLAKE, true).commit();
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
+ when(PreferenceHelper.getUseSnowflake()).thenReturn(true);
+ when(PreferenceHelper.hasSnowflakePrefs()).thenReturn(true);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Intent providerApiCommand = mockIntent();
providerApiCommand.putExtra(PROVIDER_KEY, provider);
@@ -872,12 +894,15 @@ public class ProviderApiManagerTest {
public void test_handleIntentUpdateVPNCertificate_TorBridgesPreferencesTrue_TorException_Failure() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException {
Provider provider = getConfiguredProviderAPIv4();
+ mockPreferenceHelper(provider);
mockConfigHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockBase64();
mockProviderApiConnector(NO_ERROR_API_V4);
- mockPreferences.edit().putBoolean(USE_BRIDGES, true).putBoolean(USE_SNOWFLAKE, true).commit();
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
+ when(PreferenceHelper.getUseSnowflake()).thenReturn(true);
+ when(PreferenceHelper.hasSnowflakePrefs()).thenReturn(true);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -899,8 +924,12 @@ public class ProviderApiManagerTest {
public void test_handleIntentSetupProvider_TorBridgesPreferencesEnabledTimeout_TimeoutError() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException {
Provider provider = getConfiguredProviderAPIv4();
- mockPreferences.edit().putBoolean(USE_BRIDGES, true).putBoolean(USE_SNOWFLAKE, true).commit();
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
+ mockPreferenceHelper(provider);
+ when(PreferenceHelper.getUseBridges()).thenReturn(true);
+ when(PreferenceHelper.getUseSnowflake()).thenReturn(true);
+ when(PreferenceHelper.hasSnowflakePrefs()).thenReturn(true);
+
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback());
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
@@ -924,7 +953,7 @@ public class ProviderApiManagerTest {
mockConfigHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494");
mockProviderApiConnector(NO_ERROR);
- providerApiManager = new ProviderApiManager(mockPreferences, mockResources, mockClientGenerator(), new TestProviderApiServiceCallback(null, false));
+ providerApiManager = new ProviderApiManager(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback(null, false));
Bundle expectedResult = mockBundle();
expectedResult.putBoolean(BROADCAST_RESULT_KEY, false);
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 c48f520e..e7295452 100644
--- a/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java
@@ -99,33 +99,59 @@ public class ProviderManagerTest {
}
@Test
- public void testSize_has5ProvidersWithCurrentTestSetup() {
+ public void testSize_dummyEntry_has5ProvidersWithCurrentTestSetup() {
providerManager = ProviderManager.getInstance(assetManager, file);
+ providerManager.setAddDummyEntry(true);
assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.size());
}
@Test
- public void testAdd_newCustomProviderThatIsNotPartOfDefaultNorCustomList_returnTrue() throws Exception {
+ public void testSize_has4ProvidersWithCurrentTestSetup() {
providerManager = ProviderManager.getInstance(assetManager, file);
+ assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.size());
+ }
+
+
+ @Test
+ public void testAdd_dummyEntry_newCustomProviderThatIsNotPartOfDefaultNorCustomList_returnTrue() throws Exception {
+ providerManager = ProviderManager.getInstance(assetManager, file);
+ providerManager.setAddDummyEntry(true);
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 {
+ 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", 5, providerManager.providers().size());
+ }
+
+ @Test
+ public void testAdd_dummyEntry_newCustomProviderThatIsNotPartOfDefaultButOfCustomList_returnFalse() throws Exception {
providerManager = ProviderManager.getInstance(assetManager, file);
+ providerManager.setAddDummyEntry(true);
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_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", 4, 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());
+ assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.providers().size());
}
@Test
@@ -133,7 +159,7 @@ public class ProviderManagerTest {
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());
+ assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.providers().size());
}
@Test
@@ -141,7 +167,7 @@ public class ProviderManagerTest {
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());
+ assertEquals("3 preconfigured, 0 custom providers", 3, providerManager.providers().size());
}
@Test
@@ -149,18 +175,26 @@ public class ProviderManagerTest {
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());
+ assertEquals("3 preconfigured, 1 custom providers", 4, providerManager.providers().size());
}
@Test
- public void testClear_ProvidersListHasOnlyDummyProvider() throws Exception {
+ public void testClear_dummyEntry_ProvidersListHasOnlyDummyProvider() throws Exception {
providerManager = ProviderManager.getInstance(assetManager, file);
+ providerManager.setAddDummyEntry(true);
providerManager.clear();
assertEquals("1 providers", 1, providerManager.providers().size());
assertEquals("provider is dummy element", "https://example.net", providerManager.get(0).getMainUrlString());
}
@Test
+ public void testClear_noEntries() throws Exception {
+ providerManager = ProviderManager.getInstance(assetManager, file);
+ 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);
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 4ef8b9cc..1f8a18f4 100644
--- a/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java
+++ b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java
@@ -26,7 +26,6 @@ import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getStringSetFrom
import android.content.Context;
import android.content.Intent;
-import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
@@ -469,7 +468,7 @@ public class MockHelper {
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>() {
+ when(getFromPersistedProvider(anyString(), anyString())).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
String key = (String) invocation.getArguments()[0];
@@ -495,7 +494,7 @@ public class MockHelper {
return null;
}
});
- when(getLongFromPersistedProvider(anyString(), anyString(), any(SharedPreferences.class))).thenAnswer(new Answer<Long>() {
+ when(getLongFromPersistedProvider(anyString(), anyString())).thenAnswer(new Answer<Long>() {
@Override
public Long answer(InvocationOnMock invocation) throws Throwable {
String key = (String) invocation.getArguments()[0];
@@ -508,7 +507,7 @@ public class MockHelper {
return 0L;
}
});
- when(getStringSetFromPersistedProvider(anyString(), anyString(), any(SharedPreferences.class))).thenAnswer(new Answer<Set<String>>() {
+ when(getStringSetFromPersistedProvider(anyString(), anyString())).thenAnswer(new Answer<Set<String>>() {
@Override
public Set<String> answer(InvocationOnMock invocation) throws Throwable {
String key = (String) invocation.getArguments()[0];
@@ -519,14 +518,43 @@ public class MockHelper {
return null;
}
});
+ when(PreferenceHelper.hasKey(anyString())).then(new Answer<Boolean>() {
+ @Override
+ public Boolean answer(InvocationOnMock invocation) throws Throwable {
+ String key = (String) invocation.getArguments()[0];
+ if (key!= null && key.contains(providerFromPrefs.getDomain()) && !providerFromPrefs.getDomain().isEmpty()) {
+ key = key.substring(0, key.indexOf(providerFromPrefs.getDomain()) - 1 /* -1 -> "." at the end */);
+ }
+ switch (key) {
+ case PROVIDER_PRIVATE_KEY:
+ return providerFromPrefs.getPrivateKey() != null;
+ case PROVIDER_VPN_CERTIFICATE:
+ return providerFromPrefs.getVpnCertificate() != null;
+ case Provider.KEY:
+ return providerFromPrefs.getDefinition().keys().hasNext();
+ case Provider.CA_CERT_FINGERPRINT:
+ return providerFromPrefs.getCaCertFingerprint().length() > 1;
+ case Provider.CA_CERT:
+ return providerFromPrefs.getCaCert() != null;
+ case Provider.GEOIP_URL:
+ return !providerFromPrefs.getGeoipUrl().isDefault();
+ case Provider.MOTD_URL:
+ return !providerFromPrefs.getMotdUrl().isDefault();
+ case PROVIDER_MOTD:
+ return providerFromPrefs.getMotdJson().keys().hasNext();
+ }
+ return false;
+ }
+ });
}
public static void mockPreferenceHelper(MockSharedPreferences preferences) {
mockStatic(PreferenceHelper.class);
- when(getEipDefinitionFromPreferences(any(SharedPreferences.class))).thenAnswer(new Answer<JSONObject>() {
+ when(getEipDefinitionFromPreferences()).thenAnswer(new Answer<JSONObject>() {
@Override
public JSONObject answer(InvocationOnMock invocation) throws Throwable {
+
return getEipDefinitionFromPreferences(preferences);
}
});
diff --git a/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java
index d972923c..a2569282 100644
--- a/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java
@@ -68,8 +68,6 @@ public class TetheringStateManagerTest {
PowerMockito.whenNew(IntentFilter.class).withArguments(anyString()).thenReturn(intentFilter);
PowerMockito.whenNew(IntentFilter.class).withNoArguments().thenReturn(intentFilter);
mockStatic(PreferenceHelper.class);
- SharedPreferences sharedPreferences = new MockSharedPreferences();
- when(PreferenceHelper.getSharedPreferences(any())).thenReturn(sharedPreferences);
observable = TetheringObservable.getInstance();