diff options
author | cyBerta <cyberta@riseup.net> | 2020-07-24 17:40:15 +0200 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2020-07-24 17:40:15 +0200 |
commit | d1ddaa7b10fde24b0b5f07369d1a22c221b81287 (patch) | |
tree | 2128ffb7fe977e56664623308bda358d100c87e1 /app | |
parent | e986a294ec3e328bd48bb4c423fe835d1aa2dc69 (diff) |
fix bug in vpn profile based gateway order detection, write additional tests for it
Diffstat (limited to 'app')
3 files changed, 53 insertions, 15 deletions
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 589fa751..f3eea415 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -157,6 +157,10 @@ public class Gateway { return vpnProfiles.get(transportType); } + public boolean suppoortsTransport(Connection.TransportType transportType) { + return vpnProfiles.get(transportType) != null; + } + public int getTimezone() { return timezone; } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java index f3773758..0515a35e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -84,7 +84,7 @@ public class GatewaysManager { int found = 0; int i = 0; while ((gateway = gatewaySelector.select(i)) != null) { - if (gateway.getProfile(transportType) != null) { + if (gateway.suppoortsTransport(transportType)) { if (found == nClosest) { return gateway; } @@ -98,7 +98,7 @@ public class GatewaysManager { private Gateway getGatewayFromPresortedList(int nClosest, Connection.TransportType transportType) { int found = 0; for (Gateway gateway : presortedList) { - if (gateway.getProfile(transportType) != null) { + if (gateway.suppoortsTransport(transportType)) { if (found == nClosest) { return gateway; } @@ -123,14 +123,14 @@ public class GatewaysManager { private int getPositionFromPresortedList(VpnProfile profile) { Connection.TransportType transportType = profile.mUsePluggableTransports ? OBFS4 : OPENVPN; - Gateway gateway; int nClosest = 0; - while ((nClosest < presortedList.size())) { - gateway = presortedList.get(nClosest); - if (profile.equals(gateway.getProfile(transportType))) { - return nClosest; + for (Gateway gateway : presortedList) { + if (gateway.suppoortsTransport(transportType)) { + if (profile.equals(gateway.getProfile(transportType))) { + return nClosest; + } + nClosest++; } - nClosest++; } return -1; } @@ -140,11 +140,15 @@ public class GatewaysManager { GatewaySelector gatewaySelector = new GatewaySelector(new ArrayList<>(gateways.values())); Gateway gateway; int nClosest = 0; - while ((gateway = gatewaySelector.select(nClosest)) != null) { - if (profile.equals(gateway.getProfile(transportType))) { - return nClosest; + int i = 0; + while ((gateway = gatewaySelector.select(i)) != null) { + if (gateway.suppoortsTransport(transportType)) { + if (profile.equals(gateway.getProfile(transportType))) { + return nClosest; + } + nClosest++; } - nClosest++; + i++; } return -1; } 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 5c5ebbfe..bfe96d2c 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java @@ -129,6 +129,36 @@ public class GatewaysManagerTest { } @Test + public void TestGetPosition_VpnProfileExistingObfs4FromPresortedList_returnsPositionOne() throws JSONException, ConfigParser.ConfigParseError, IOException { + Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_three_mixed_gateways.json", "ptdemo_three_mixed_gateways.geoip.json"); + JSONObject eipServiceJson = provider.getEipServiceJson(); + JSONObject gateway1 = eipServiceJson.getJSONArray(GATEWAYS).getJSONObject(0); + MockHelper.mockProviderObserver(provider); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, gateway1, 3); + VpnProfile profile = configGenerator.createProfile(OBFS4); + profile.mGatewayIp = "37.218.247.60"; + + assertEquals(1, gatewaysManager.getPosition(profile)); + } + + @Test + public void TestGetPosition_VpnProfileExistingOpenvpnFromPresortedList_returnsPositionOne() throws JSONException, ConfigParser.ConfigParseError, IOException { + Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_three_mixed_gateways.json", "ptdemo_three_mixed_gateways.geoip.json"); + JSONObject eipServiceJson = provider.getEipServiceJson(); + JSONObject gateway1 = eipServiceJson.getJSONArray(GATEWAYS).getJSONObject(0); + MockHelper.mockProviderObserver(provider); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, gateway1, 3); + VpnProfile profile = configGenerator.createProfile(OPENVPN); + profile.mGatewayIp = "37.218.247.60"; + + assertEquals(2, gatewaysManager.getPosition(profile)); + } + + @Test public void TestGetPosition_VpnProfileDifferentIp_returnMinusOne() throws JSONException, ConfigParser.ConfigParseError, IOException { Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_three_mixed_gateways.json", null); JSONObject eipServiceJson = provider.getEipServiceJson(); @@ -159,7 +189,7 @@ public class GatewaysManagerTest { } @Test - public void TestSelectN_selectFirstObfs4Connection_returnThirdGateway() throws JSONException, ConfigParser.ConfigParseError, IOException { + public void TestSelectN_selectFirstObfs4Connection_returnThirdGateway() { Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_two_openvpn_one_pt_gateways.json", null); MockHelper.mockProviderObserver(provider); @@ -171,7 +201,7 @@ public class GatewaysManagerTest { } @Test - public void testSelectN_selectFromPresortedGateways_returnsGatewaysInPresortedOrder() throws JSONException { + public void testSelectN_selectFromPresortedGateways_returnsGatewaysInPresortedOrder() { Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_three_mixed_gateways.json", "ptdemo_three_mixed_gateways.geoip.json"); MockHelper.mockProviderObserver(provider); @@ -186,7 +216,7 @@ public class GatewaysManagerTest { } @Test - public void testSelectN_selectObfs4FromPresortedGateways_returnsObfs4GatewaysInPresortedOrder() throws JSONException { + public void testSelectN_selectObfs4FromPresortedGateways_returnsObfs4GatewaysInPresortedOrder() { Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_three_mixed_gateways.json", "ptdemo_three_mixed_gateways.geoip.json"); MockHelper.mockProviderObserver(provider); |