diff options
author | cyBerta <cyberta@riseup.net> | 2023-04-16 00:23:02 +0200 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2023-04-16 00:23:19 +0200 |
commit | c2c3e13455ecfa310711e78aa830d14804aaaa5c (patch) | |
tree | b8b01db0046caac9aa31e1537f3151ee17371c57 /app/src/test | |
parent | 601a77ad059b3da754c3baaf10b01dbc19b927be (diff) |
only take experimental transports into consideration in case the user switched the experimental transport toggle
Diffstat (limited to 'app/src/test')
3 files changed, 122 insertions, 1 deletions
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 998ed2fd..ee6ccce5 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java @@ -16,6 +16,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import de.blinkt.openvpn.VpnProfile; @@ -215,6 +216,88 @@ public class GatewaysManagerTest { } @Test + public void TestSelectN_select_includeExperimentalTransport_DecoupledPortHoppingGW() { + Provider provider = getProvider(null, null, null, null, null, null, "decoupled_pt_portHopping.eip-service.json", null); + + MockHelper.mockProviderObservable(provider); + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true); + when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(true); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + ArrayList<String> hosts = new ArrayList<>(); + hosts.add(gatewaysManager.select(0).gateway.getHost()); + hosts.add(gatewaysManager.select(1).gateway.getHost()); + + assertTrue(hosts.contains("bridge-nyc1-02.bitmask-dev.leapvpn.net")); + assertTrue(hosts.contains("bridge-nyc1-01.bitmask-dev.leapvpn.net")); + + } + + @Test + public void TestSelectN_select_includeExperimentalTransport_DecoupledPortAndIPHoppingGW() { + Provider provider = getProvider(null, null, null, null, null, null, "decoupled_pt.eip-service.json", null); + + MockHelper.mockProviderObservable(provider); + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true); + when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(true); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + ArrayList<String> hosts = new ArrayList<>(); + hosts.add(gatewaysManager.select(0).gateway.getHost()); + hosts.add(gatewaysManager.select(1).gateway.getHost()); + assertTrue(hosts.contains("bridge-nyc1-02.bitmask-dev.leapvpn.net")); + assertTrue(hosts.contains("bridge-nyc1-01.bitmask-dev.leapvpn.net")); + } + + @Test + public void TestSelectN_select_excludeExperimentalTransport_DecoupledPortHoppingGW() { + Provider provider = getProvider(null, null, null, null, null, null, "decoupled_pt_portHopping.eip-service.json", null); + + MockHelper.mockProviderObservable(provider); + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true); + when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(false); + + for (int i = 0; i < 1000; i++) { + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + assertEquals("bridge-nyc1-01.bitmask-dev.leapvpn.net", gatewaysManager.select(0).gateway.getHost()); + } + } + + @Test + public void TestSelectN_select_excludeExperimentalTransport_DecoupledPortAndIPHoppingGW() { + Provider provider = getProvider(null, null, null, null, null, null, "decoupled_pt.eip-service.json", null); + + MockHelper.mockProviderObservable(provider); + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true); + when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(false); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + assertEquals("bridge-nyc1-01.bitmask-dev.leapvpn.net", gatewaysManager.select(0).gateway.getHost()); + assertNull(gatewaysManager.select(1)); + } + + @Test + public void TestSelectN_select_excludeExperimentalTransport_InGatewayHoppingPTBridge() { + Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_obfs4hop_tcp_gateways.json", null); + + MockHelper.mockProviderObservable(provider); + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUseBridges(any(Context.class))).thenReturn(true); + when(PreferenceHelper.allowExperimentalTransports(any(Context.class))).thenReturn(false); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + ArrayList<String> hosts = new ArrayList<>(); + hosts.add(gatewaysManager.select(0).gateway.getHost()); + hosts.add(gatewaysManager.select(1).gateway.getHost()); + + assertTrue(hosts.contains("pt.demo.bitmask.net")); + assertTrue(hosts.contains("manila.bitmask.net")); + } + + @Test 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"); @@ -555,6 +638,16 @@ public class GatewaysManagerTest { Provider provider = getProvider(null, null, null, null, null, null, "decoupled_pt.eip-service.json", null); MockHelper.mockProviderObservable(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + assertEquals(2, gatewaysManager.size()); + } + + @Test + public void testGatewayManagerFromCurrentProvider_decoupledBridgesIncludingExperimental_threeGateways() throws IOException, NullPointerException { + 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); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals(3, gatewaysManager.size()); } diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/VpnConfigGeneratorTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/VpnConfigGeneratorTest.java index 565e6396..93231055 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/VpnConfigGeneratorTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/VpnConfigGeneratorTest.java @@ -2,6 +2,7 @@ package se.leap.bitmaskclient.eip; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; @@ -33,6 +34,7 @@ import java.io.File; import java.util.HashMap; import de.blinkt.openvpn.VpnProfile; +import de.blinkt.openvpn.core.ConfigParser; import de.blinkt.openvpn.core.connection.Connection; import de.blinkt.openvpn.core.connection.Obfs4Connection; import de.blinkt.openvpn.core.connection.Obfs4HopConnection; @@ -2041,6 +2043,7 @@ public class VpnConfigGeneratorTest { generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_obfs4hop_tcp_gateways.json"))).getJSONObject(OPENVPN_CONFIGURATION); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; + configuration.experimentalTransports = true; vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); HashMap<Connection.TransportType, VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(vpnProfiles.containsKey(OBFS4_HOP) && ((Obfs4HopConnection)vpnProfiles.get(OBFS4_HOP).mConnections[0]).getObfs4Options().transport.getProtocols()[0].equals("tcp")); @@ -2053,6 +2056,7 @@ public class VpnConfigGeneratorTest { generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_obfs4hop_kcp_gateways.json"))).getJSONObject(OPENVPN_CONFIGURATION); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; + configuration.experimentalTransports = true; vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); HashMap<Connection.TransportType, VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(vpnProfiles.containsKey(OBFS4_HOP) && ((Obfs4HopConnection)vpnProfiles.get(OBFS4_HOP).mConnections[0]).getObfs4Options().transport.getProtocols()[0].equals("kcp")); @@ -2066,6 +2070,7 @@ public class VpnConfigGeneratorTest { generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("decoupled_pt_portHopping.eip-service.json"))).getJSONObject(OPENVPN_CONFIGURATION); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; + configuration.experimentalTransports = true; vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); HashMap<Connection.TransportType, VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); System.out.println(vpnProfiles.get(OBFS4_HOP).getConfigFile(context, false)); @@ -2079,6 +2084,7 @@ public class VpnConfigGeneratorTest { generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("decoupled_pt_portHopping.eip-service.json"))).getJSONObject(OPENVPN_CONFIGURATION); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; + configuration.experimentalTransports = true; vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); HashMap<Connection.TransportType, VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); System.out.println(vpnProfiles.get(OBFS4_HOP).getConfigFile(context, false)); @@ -2091,6 +2097,7 @@ public class VpnConfigGeneratorTest { generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("decoupled_pt.eip-service.json"))).getJSONObject(OPENVPN_CONFIGURATION); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; + configuration.experimentalTransports = true; vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); HashMap<Connection.TransportType, VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(vpnProfiles.containsKey(OBFS4)); @@ -2105,6 +2112,7 @@ public class VpnConfigGeneratorTest { generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("decoupled_pt.eip-service.json"))).getJSONObject(OPENVPN_CONFIGURATION); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; + configuration.experimentalTransports = true; vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); HashMap<Connection.TransportType, VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(vpnProfiles.containsKey(OBFS4_HOP)); @@ -2114,12 +2122,31 @@ public class VpnConfigGeneratorTest { } @Test + public void testGenerateVpnProfile_noExperimental_skipObfs4Hop() throws Exception { + when(ConfigHelper.ObfsVpnHelper.useObfsVpn()).thenReturn(true); + gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("decoupled_pt.eip-service.json"))).getJSONArray("gateways").getJSONObject(2); + generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("decoupled_pt.eip-service.json"))).getJSONObject(OPENVPN_CONFIGURATION); + VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); + configuration.apiVersion = 3; + configuration.experimentalTransports = false; + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + Exception exception = null; + try { + vpnConfigGenerator.generateVpnProfiles(); + } catch (ConfigParser.ConfigParseError e) { + exception = e; + } + assertNotNull(exception); + } + + @Test public void testGenerateVpnProfile_obfs4hop_onlyPortHopping_decoupled() throws Exception { when(ConfigHelper.ObfsVpnHelper.useObfsVpn()).thenReturn(true); gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("decoupled_pt_portHopping.eip-service.json"))).getJSONArray("gateways").getJSONObject(2); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("decoupled_pt_portHopping.eip-service.json"))).getJSONObject(OPENVPN_CONFIGURATION); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; + configuration.experimentalTransports = true; vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); HashMap<Connection.TransportType, VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(vpnProfiles.containsKey(OBFS4_HOP)); diff --git a/app/src/test/resources/ptdemo_obfs4hop_tcp_gateways.json b/app/src/test/resources/ptdemo_obfs4hop_tcp_gateways.json index 6a748a4f..34bcecb3 100644 --- a/app/src/test/resources/ptdemo_obfs4hop_tcp_gateways.json +++ b/app/src/test/resources/ptdemo_obfs4hop_tcp_gateways.json @@ -66,7 +66,8 @@ { "type":"openvpn", "protocols":[ - "tcp" + "tcp", + "udp" ], "ports":[ "1195" |