summaryrefslogtreecommitdiff
path: root/app/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/test')
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java2
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java146
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java3
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java4
-rw-r--r--app/src/test/resources/v4/riseup_geoip_v4_bad_obfs4_gateway.json41
5 files changed, 179 insertions, 17 deletions
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 5186c462..6f26e494 100644
--- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java
@@ -27,6 +27,7 @@ import se.leap.bitmaskclient.base.utils.ConfigHelper;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.anyInt;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_PRIVATE_KEY;
@@ -75,6 +76,7 @@ public class GatewaySelectorTest {
@Before
public void setup() throws IOException, JSONException, ConfigParser.ConfigParseError {
mockStatic(ConfigHelper.class);
+ when(ConfigHelper.timezoneDistance(anyInt(), anyInt())).thenCallRealMethod();
mockTextUtils();
eipDefinition = new JSONObject(getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-four-gateways.json")));
JSONArray gateways = eipDefinition.getJSONArray("gateways");
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 08591b6d..43a6a496 100644
--- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java
@@ -23,27 +23,28 @@ import de.blinkt.openvpn.core.ConfigParser;
import se.leap.bitmaskclient.base.models.Location;
import se.leap.bitmaskclient.base.models.Provider;
import se.leap.bitmaskclient.base.models.ProviderObservable;
+import se.leap.bitmaskclient.base.utils.ConfigHelper;
+import se.leap.bitmaskclient.base.utils.PreferenceHelper;
import se.leap.bitmaskclient.testutils.MockHelper;
import se.leap.bitmaskclient.testutils.MockSharedPreferences;
import se.leap.bitmaskclient.testutils.TestSetupHelper;
-import se.leap.bitmaskclient.base.utils.ConfigHelper;
-import se.leap.bitmaskclient.base.utils.PreferenceHelper;
import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4;
import static de.blinkt.openvpn.core.connection.Connection.TransportType.OPENVPN;
import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
-import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static se.leap.bitmaskclient.base.models.Constants.GATEWAYS;
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.models.Constants.USE_BRIDGES;
import static se.leap.bitmaskclient.base.models.Provider.CA_CERT;
import static se.leap.bitmaskclient.testutils.MockHelper.mockTextUtils;
import static se.leap.bitmaskclient.testutils.TestSetupHelper.getProvider;
@@ -69,6 +70,9 @@ public class GatewaysManagerTest {
mockTextUtils();
when(ConfigHelper.getCurrentTimezone()).thenReturn(-1);
when(ConfigHelper.stringEqual(anyString(), anyString())).thenCallRealMethod();
+ when(ConfigHelper.getConnectionQualityFromTimezoneDistance(anyInt())).thenCallRealMethod();
+ when(ConfigHelper.isIPv4(anyString())).thenCallRealMethod();
+ when(ConfigHelper.timezoneDistance(anyInt(), anyInt())).thenCallRealMethod();
secrets = new JSONObject(getJsonStringFor("secrets.json"));
sharedPreferences = new MockSharedPreferences();
sharedPreferences.edit().
@@ -357,11 +361,11 @@ public class GatewaysManagerTest {
assertEquals(3, locations.size());
for (Location location : locations) {
- if ("Paris".equals(location.name)) {
- assertEquals(3, location.numberOfGateways);
+ if ("Paris".equals(location.getName())) {
+ assertEquals(3, location.getNumberOfGateways(OPENVPN));
// manually calculate average load of paris gateways in "v4/riseup_geoip_v4.json"
double averageLoad = (0.3 + 0.36 + 0.92) / 3.0;
- assertEquals(averageLoad, location.averageLoad);
+ assertEquals(averageLoad, location.getAverageLoad(OPENVPN));
}
}
}
@@ -373,24 +377,136 @@ public class GatewaysManagerTest {
MockHelper.mockProviderObservable(provider);
mockStatic(PreferenceHelper.class);
when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
+ sharedPreferences.edit().putBoolean(USE_BRIDGES, true).commit();
+ GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
+ List<Location> locations = gatewaysManager.getGatewayLocations();
+
+ assertEquals(3, locations.size());
+ for (Location location : locations) {
+ if ("Montreal".equals(location.getName())) {
+ assertEquals(1, location.getNumberOfGateways(OBFS4));
+ assertEquals(0.59, location.getAverageLoad(OBFS4));
+ assertTrue(location.supportsTransport(OBFS4));
+ }
+ if ("Paris".equals(location.getName())) {
+ // checks that only gateways supporting obfs4 are taken into account
+ assertEquals(1, location.getNumberOfGateways(OBFS4));
+ assertEquals(0.36, location.getAverageLoad(OBFS4));
+ assertTrue(location.supportsTransport(OBFS4));
+ }
+ if ("Amsterdam".equals(location.getName())) {
+ assertFalse(location.supportsTransport(OBFS4));
+ }
+ }
+
+ }
+
+ @Test
+ public void testGetLocations_noMenshen_obfs4_calculateAverageLoadFromTimezoneDistance() {
+ Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v1.json");
+
+ MockHelper.mockProviderObservable(provider);
+ mockStatic(PreferenceHelper.class);
+ when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true);
+ sharedPreferences.edit().putBoolean(USE_BRIDGES, true).commit();
+ GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
+ List<Location> locations = gatewaysManager.getGatewayLocations();
+
+ assertEquals(3, locations.size());
+ for (Location location : locations) {
+ if ("Montreal".equals(location.getName())) {
+ assertEquals(1, location.getNumberOfGateways(OBFS4));
+ assertEquals(1/3.0, location.getAverageLoad(OBFS4));
+ }
+ if ("Paris".equals(location.getName())) {
+ // checks that only gateways supporting obfs4 are taken into account
+ assertEquals(1, location.getNumberOfGateways(OBFS4));
+ assertEquals(0.25, location.getAverageLoad(OBFS4));
+ }
+ }
+ }
+
+ @Test
+ public void testGetLocations_noMenshen_openvpn_calculateAverageLoadFromTimezoneDistance() {
+ Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v1.json");
+
+ MockHelper.mockProviderObservable(provider);
+ mockStatic(PreferenceHelper.class);
+ when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ sharedPreferences.edit().putBoolean(USE_BRIDGES, false).commit();
GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
List<Location> locations = gatewaysManager.getGatewayLocations();
- assertEquals(2, locations.size());
+ assertEquals(3, locations.size());
for (Location location : locations) {
- if ("Montreal".equals(location.name)) {
- assertEquals(1, location.numberOfGateways);
- assertEquals(0.59, location.averageLoad);
+ if ("Montreal".equals(location.getName())) {
+ assertEquals(1, location.getNumberOfGateways(OPENVPN));
+ assertEquals(1/3.0, location.getAverageLoad(OPENVPN));
}
- if ("Paris".equals(location.name)) {
+ if ("Paris".equals(location.getName())) {
// checks that only gateways supporting obfs4 are taken into account
- assertEquals(1, location.numberOfGateways);
- assertEquals(0.36, location.averageLoad);
+ assertEquals(3, location.getNumberOfGateways(OPENVPN));
+ assertEquals(0.25, location.getAverageLoad(OPENVPN));
}
}
+ }
+
+ @Test
+ public void testGetSortedLocations_openvpn() {
+ Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v4_bad_obfs4_gateway.json");
+
+ MockHelper.mockProviderObservable(provider);
+ mockStatic(PreferenceHelper.class);
+ when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
+ List<Location> locations = gatewaysManager.getSortedGatewayLocations(OPENVPN);
+
+ assertEquals(3, locations.size());
+
+ /** -> v4/riseup_geoip_v4_bad_obfs4_gateway.json OPENVPN
+ * Paris = 0.527
+ * 0.36 - zarapito
+ * 0.92 - hoazin
+ * 0.3 - mouette
+ *
+ * Montreal = 0.59
+ * 0.59 - yal
+ *
+ * Amsterdam = 0.8
+ * 0.8 - redshank
+ */
+ assertEquals("Paris", locations.get(0).getName());
+ assertEquals("Montreal", locations.get(1).getName());
+ assertEquals("Amsterdam", locations.get(2).getName());
}
+ @Test
+ public void testGetSortedLocations_obfs4() {
+ Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v4_bad_obfs4_gateway.json");
+
+ MockHelper.mockProviderObservable(provider);
+ mockStatic(PreferenceHelper.class);
+ when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(false);
+ GatewaysManager gatewaysManager = new GatewaysManager(mockContext);
+ List<Location> locations = gatewaysManager.getSortedGatewayLocations(OBFS4);
+
+ assertEquals(3, locations.size());
+
+ /** -> v4/riseup_geoip_v4_bad_obfs4_gateway.json OBFS4
+ * Paris = 0.92
+ * 0.92 - hoazin
+ *
+ * Montreal = 0.59
+ * 0.59 - yal
+ *
+ * Amsterdam = 0.0 - no obfs4
+ * 0.0 - redshank
+ */
+ assertEquals("Montreal", locations.get(0).getName());
+ assertEquals("Paris", locations.get(1).getName());
+ assertEquals("Amsterdam", locations.get(2).getName());
+ }
private String getJsonStringFor(String filename) throws IOException {
return TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream(filename));
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 2b1dc2ef..5bed679b 100644
--- a/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java
@@ -112,10 +112,11 @@ public class ProviderApiManagerTest {
Throwable startTorServiceException;
boolean hasNetworkConnection;
TestProviderApiServiceCallback() {
- new TestProviderApiServiceCallback(null, true);
+ this(null, true);
}
TestProviderApiServiceCallback(@Nullable Throwable startTorServiceException, boolean hasNetworkConnection) {
this.startTorServiceException = startTorServiceException;
+ this.hasNetworkConnection = hasNetworkConnection;
}
@Override
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 0086e4c2..dd3053df 100644
--- a/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java
+++ b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java
@@ -38,6 +38,7 @@ import java.util.Vector;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
+import okhttp3.Connection;
import okhttp3.OkHttpClient;
import se.leap.bitmaskclient.R;
import se.leap.bitmaskclient.base.models.Provider;
@@ -409,12 +410,13 @@ public class MockHelper {
}
public static void mockConfigHelper(String mockedFingerprint) throws CertificateEncodingException, NoSuchAlgorithmException {
- // FIXME use MockSharedPreferences instead of provider
mockStatic(ConfigHelper.class);
when(ConfigHelper.getFingerprintFromCertificate(any(X509Certificate.class), anyString())).thenReturn(mockedFingerprint);
when(ConfigHelper.checkErroneousDownload(anyString())).thenCallRealMethod();
when(ConfigHelper.parseX509CertificateFromString(anyString())).thenCallRealMethod();
when(ConfigHelper.getProviderFormattedString(any(Resources.class), anyInt())).thenCallRealMethod();
+ when(ConfigHelper.timezoneDistance(anyInt(), anyInt())).thenCallRealMethod();
+ when(ConfigHelper.isIPv4(anyString())).thenCallRealMethod();
}
public static void mockPreferenceHelper(final Provider providerFromPrefs) {
diff --git a/app/src/test/resources/v4/riseup_geoip_v4_bad_obfs4_gateway.json b/app/src/test/resources/v4/riseup_geoip_v4_bad_obfs4_gateway.json
new file mode 100644
index 00000000..fcbc0d85
--- /dev/null
+++ b/app/src/test/resources/v4/riseup_geoip_v4_bad_obfs4_gateway.json
@@ -0,0 +1,41 @@
+{
+ "ip":"51.158.144.32",
+ "cc":"FR",
+ "city":"Paris",
+ "lat":48.8628,
+ "lon":2.3292,
+ "gateways":[
+ "mouette.riseup.net",
+ "hoatzin.riseup.net",
+ "yal.riseup.net",
+ "redshank.riseup.net",
+ "zarapito.riseup.net"
+ ],
+ "sortedGateways": [
+ {
+ "host": "mouette.riseup.net",
+ "fullness": 0.3,
+ "overload": false
+ },
+ {
+ "host": "hoatzin.riseup.net",
+ "fullness": 0.92,
+ "overload": false
+ },
+ {
+ "host": "yal.riseup.net",
+ "fullness": 0.59,
+ "overload": false
+ },
+ {
+ "host": "redshank.riseup.net",
+ "fullness": 0.8,
+ "overload": false
+ },
+ {
+ "host": "zarapito.riseup.net",
+ "fullness": 0.36,
+ "overload": true
+ }
+ ]
+} \ No newline at end of file