diff options
3 files changed, 63 insertions, 1 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java index 92010992..6c242e5a 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java @@ -206,6 +206,17 @@ public class ConfigHelper { return dist; } + /** + * + * @param remoteTimezone + * @return a value between 0.1 and 1.0 + */ + public static double getConnectionQualityFromTimezoneDistance(int remoteTimezone) { + int localTimeZone = ConfigHelper.getCurrentTimezone(); + int distance = ConfigHelper.timezoneDistance(localTimeZone, remoteTimezone); + return Math.max(distance / 12.0, 0.1); + } + public static String getProviderFormattedString(Resources resources, @StringRes int resourceId) { String appName = resources.getString(R.string.app_name); return resources.getString(resourceId, appName); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java index 8a48684f..a3d3abbc 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -33,6 +33,7 @@ import java.util.Set; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.ConfigParser; import de.blinkt.openvpn.core.connection.Connection; +import se.leap.bitmaskclient.base.utils.ConfigHelper; import se.leap.bitmaskclient.base.utils.PreferenceHelper; import static se.leap.bitmaskclient.base.models.Constants.FULLNESS; @@ -156,7 +157,7 @@ public class Gateway { try { return load.getDouble(FULLNESS); } catch (JSONException | NullPointerException e) { - return 0; + return ConfigHelper.getConnectionQualityFromTimezoneDistance(timezone); } } 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 28311e18..33abfbed 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java @@ -70,6 +70,7 @@ 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")); @@ -400,6 +401,55 @@ public class GatewaysManagerTest { } + @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(3, locations.size()); + for (Location location : locations) { + if ("Montreal".equals(location.getName())) { + assertEquals(1, location.getNumberOfGateways(OPENVPN)); + assertEquals(1/3.0, location.getAverageLoad(OPENVPN)); + } + if ("Paris".equals(location.getName())) { + // checks that only gateways supporting obfs4 are taken into account + assertEquals(3, location.getNumberOfGateways(OPENVPN)); + assertEquals(0.25, location.getAverageLoad(OPENVPN)); + } + } + } private String getJsonStringFor(String filename) throws IOException { return TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream(filename)); |