diff options
author | cyBerta <cyberta@riseup.net> | 2024-11-15 13:11:13 +0100 |
---|---|---|
committer | cyberta <cyberta@riseup.net> | 2024-12-11 00:09:34 +0000 |
commit | 5682720dd0113ae3701df97cb15088ead9b231d8 (patch) | |
tree | bec6857454f5eb3ee93ab7c816b818c645ea2a09 | |
parent | aa780e9698e0aa564ff925d4d17a59e6db67ec6b (diff) |
fix tests
10 files changed, 286 insertions, 140 deletions
diff --git a/app/src/test/java/io/swagger/client/JSONTest.java b/app/src/test/java/io/swagger/client/JSONTest.java new file mode 100644 index 00000000..5baa7e79 --- /dev/null +++ b/app/src/test/java/io/swagger/client/JSONTest.java @@ -0,0 +1,31 @@ +package io.swagger.client; + +import org.junit.Test; + +import java.io.IOException; +import static org.junit.Assert.*; + +import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4; + +import com.google.gson.JsonSyntaxException; + +import de.blinkt.openvpn.core.ConfigParser; +import io.swagger.client.model.ModelsProvider; +import se.leap.bitmaskclient.testutils.TestSetupHelper; + +public class JSONTest { + + @Test + public void testProviderJsonParsing_testBackwardsCompatibility_v4() throws IOException { + String boblove = TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("v4/riseup.net.json")); + ModelsProvider p = JSON.createGson().create().fromJson(boblove, ModelsProvider.class); + assertNotNull(p); + assertEquals("riseup.net", p.getDomain()); + } + + @Test + public void testProvidingNull() throws IOException { + String p = JSON.createGson().create().toJson(null); + assertEquals("null", p); + } +} diff --git a/app/src/test/java/se/leap/bitmaskclient/base/models/ProviderTest.java b/app/src/test/java/se/leap/bitmaskclient/base/models/ProviderTest.java index ee6cd30f..dc35ecb9 100644 --- a/app/src/test/java/se/leap/bitmaskclient/base/models/ProviderTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/base/models/ProviderTest.java @@ -5,8 +5,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; +import android.os.Build; + import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; import se.leap.bitmaskclient.base.utils.BuildConfigHelper; import se.leap.bitmaskclient.testutils.MockHelper; @@ -16,6 +21,8 @@ import se.leap.bitmaskclient.testutils.TestSetupHelper; * Created by cyberta on 12.02.18. */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Build.VERSION_CODES.P}) public class ProviderTest { @Before 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 e03fccff..1b093f62 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 @@ -1,5 +1,13 @@ package se.leap.bitmaskclient.base.utils; +import static org.junit.Assert.assertFalse; +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.getSavedProviderFromSharedPreferences; +import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString; + import android.content.SharedPreferences; import org.junit.Before; @@ -8,15 +16,6 @@ import org.junit.Test; import se.leap.bitmaskclient.base.models.Provider; import se.leap.bitmaskclient.testutils.MockSharedPreferences; -import static org.junit.Assert.assertFalse; -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; - /** * Created by cyberta on 17.01.18. */ diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/GatewayTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/GatewayTest.java index 66c139b2..88e58cd8 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewayTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewayTest.java @@ -10,6 +10,7 @@ import static se.leap.bitmaskclient.testutils.TestSetupHelper.getProvider; import android.content.Context; import android.content.SharedPreferences; +import android.os.Build; import androidx.annotation.Nullable; @@ -17,8 +18,11 @@ 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.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; import java.io.IOException; import java.util.Arrays; @@ -35,6 +39,8 @@ import se.leap.bitmaskclient.base.utils.TimezoneHelper; import se.leap.bitmaskclient.testutils.MockSharedPreferences; import se.leap.bitmaskclient.testutils.TestSetupHelper; +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Build.VERSION_CODES.P}) public class GatewayTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) 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 b79c34ae..a9a73628 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java @@ -20,6 +20,7 @@ import static se.leap.bitmaskclient.testutils.TestSetupHelper.getProvider; import android.content.Context; import android.content.SharedPreferences; +import android.os.Build; import androidx.annotation.Nullable; @@ -27,8 +28,11 @@ 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.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; import java.io.IOException; import java.util.ArrayList; @@ -49,6 +53,8 @@ import se.leap.bitmaskclient.testutils.TestSetupHelper; /** * Created by cyberta on 09.10.17. */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Build.VERSION_CODES.P}) public class GatewaysManagerTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) @@ -114,7 +120,8 @@ public class GatewaysManagerTest { VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; configuration.remoteGatewayIP = "37.218.247.60"; - VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, gateway1, configuration); + configuration.transports = Transport.createTransportsFrom(gateway1, 3); + VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, configuration); VpnProfile profile = createProfile(configGenerator, OBFS4); assertNotNull(profile); assertEquals(0, gatewaysManager.getPosition(profile)); @@ -132,7 +139,8 @@ public class GatewaysManagerTest { VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; configuration.remoteGatewayIP = "37.218.247.60"; - VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, gateway1, configuration); + configuration.transports = Transport.createTransportsFrom(gateway1, 3); + VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, configuration); VpnProfile profile = createProfile(configGenerator, OPENVPN); assertNotNull(profile); assertEquals(0, gatewaysManager.getPosition(profile)); @@ -149,7 +157,8 @@ public class GatewaysManagerTest { VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; configuration.remoteGatewayIP = "37.218.247.60"; - VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, gateway1, configuration); + configuration.transports = Transport.createTransportsFrom(gateway1, 3); + VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, configuration); assertThrows(ConfigParser.ConfigParseError.class, () -> createProfile(configGenerator, OBFS4)); } @@ -164,7 +173,8 @@ public class GatewaysManagerTest { VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; configuration.remoteGatewayIP = "37.218.247.60"; - VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, gateway1, configuration); + configuration.transports = Transport.createTransportsFrom(gateway1, 3); + VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, configuration); VpnProfile profile = createProfile(configGenerator, OBFS4); assertEquals(2, gatewaysManager.getPosition(profile)); @@ -181,7 +191,8 @@ public class GatewaysManagerTest { VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; configuration.remoteGatewayIP = "37.218.247.60"; - VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, gateway1, configuration); + configuration.transports = Transport.createTransportsFrom(gateway1, 3); + VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, configuration); VpnProfile profile = createProfile(configGenerator, OPENVPN); assertEquals(2, gatewaysManager.getPosition(profile)); @@ -198,7 +209,8 @@ public class GatewaysManagerTest { VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; configuration.remoteGatewayIP = "37.218.247.61"; - VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, gateway1, configuration); + configuration.transports = Transport.createTransportsFrom(gateway1, 3); + VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, configuration); VpnProfile profile = createProfile(configGenerator, OBFS4); assertEquals(-1, gatewaysManager.getPosition(profile)); @@ -215,7 +227,8 @@ public class GatewaysManagerTest { VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); configuration.apiVersion = 3; configuration.remoteGatewayIP = "3.21.247.89"; - VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, gateway1, configuration); + configuration.transports = Transport.createTransportsFrom(gateway1, 3); + VpnConfigGenerator configGenerator = new VpnConfigGenerator(provider.getDefinition(), secrets, configuration); VpnProfile profile = createProfile(configGenerator, OBFS4); assertEquals(1, gatewaysManager.getPosition(profile)); 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 5343c466..327e2085 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/VpnConfigGeneratorTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/VpnConfigGeneratorTest.java @@ -8,17 +8,24 @@ import static org.mockito.Mockito.when; import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4; import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4_HOP; import static de.blinkt.openvpn.core.connection.Connection.TransportType.OPENVPN; +import static se.leap.bitmaskclient.base.models.Constants.IP_ADDRESS; +import static se.leap.bitmaskclient.base.models.Constants.IP_ADDRESS6; import static se.leap.bitmaskclient.base.models.Constants.OPENVPN_CONFIGURATION; +import static se.leap.bitmaskclient.base.models.Transport.createTransportsFrom; +import static se.leap.bitmaskclient.eip.VpnConfigGenerator.Configuration.createProfileConfig; import android.content.Context; import android.content.SharedPreferences; +import android.os.Build; import org.json.JSONObject; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; import java.io.File; -import java.util.HashMap; import java.util.Vector; import de.blinkt.openvpn.VpnProfile; @@ -26,6 +33,7 @@ import de.blinkt.openvpn.core.ConfigParser; import de.blinkt.openvpn.core.connection.Connection; import de.blinkt.openvpn.core.connection.Obfs4Connection; import se.leap.bitmaskclient.base.models.ProviderObservable; +import se.leap.bitmaskclient.base.models.Transport; import se.leap.bitmaskclient.base.utils.BuildConfigHelper; import se.leap.bitmaskclient.base.utils.PreferenceHelper; import se.leap.bitmaskclient.base.utils.PrivateKeyHelper; @@ -36,6 +44,8 @@ import se.leap.bitmaskclient.testutils.TestSetupHelper; /** * Created by cyberta on 03.10.17. */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Build.VERSION_CODES.P}) public class VpnConfigGeneratorTest { Context context; @@ -55,7 +65,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -151,7 +161,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -247,7 +257,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -343,7 +353,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -439,7 +449,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -535,7 +545,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -631,7 +641,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -727,7 +737,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -829,7 +839,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -931,7 +941,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -1042,7 +1052,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -1145,7 +1155,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -1246,7 +1256,7 @@ public class VpnConfigGeneratorTest { "management-hold\n" + "\n" + "setenv IV_GUI_VER \"se.leap.bitmaskclient 0.9.10\" \n" + - "setenv IV_PLAT_VER \"0 null JUNIT null null null\"\n" + + "setenv IV_PLAT_VER \"28 9 ROBO Android unknown robolectric\"\n" + "machine-readable-output\n" + "allow-recursive-routing\n" + "ifconfig-nowarn\n" + @@ -1375,9 +1385,8 @@ public class VpnConfigGeneratorTest { @Test public void testGenerateVpnProfile_v1_tcp_udp() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("gateway_tcp_udp.json"))); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 1; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 1), 1, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertFalse(containsKey(vpnProfiles, OBFS4)); assertEquals(expectedVPNConfig_v1_tcp_udp.trim(), getVpnProfile(vpnProfiles, OPENVPN).getConfigFile(context, false).trim()); @@ -1386,9 +1395,8 @@ public class VpnConfigGeneratorTest { @Test public void testGenerateVpnProfile_v1_udp_tcp() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("gateway_udp_tcp.json"))); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 1; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 1), 1, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertFalse(containsKey(vpnProfiles, OBFS4)); assertEquals(expectedVPNConfig_v1_udp_tcp.trim(), getVpnProfile(vpnProfiles, OPENVPN).getConfigFile(context, false).trim()); @@ -1397,9 +1405,9 @@ public class VpnConfigGeneratorTest { @Test public void testGenerateVpnProfile_v2_tcp_udp() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("gateway_tcp_udp.json"))); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 2; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 2), 2, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertFalse(containsKey(vpnProfiles, OBFS4)); assertEquals(expectedVPNConfig_v1_tcp_udp.trim(), getVpnProfile(vpnProfiles, OPENVPN).getConfigFile(context, false).trim()); @@ -1408,9 +1416,8 @@ public class VpnConfigGeneratorTest { @Test public void testGenerateVpnProfile_v2_udp_tcp() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("gateway_udp_tcp.json"))); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 2; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 2), 2, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertFalse(containsKey(vpnProfiles, OBFS4)); assertEquals(expectedVPNConfig_v1_udp_tcp.trim(), getVpnProfile(vpnProfiles, OPENVPN).getConfigFile(context, false).trim()); @@ -1421,9 +1428,8 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_v3_obfs4() throws Exception { BuildConfigHelper buildConfigHelper = MockHelper.mockBuildConfigHelper(false); gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo.bitmask.eip-service-obfsvpn1.0.0.json"))).getJSONArray("gateways").getJSONObject(0); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1435,9 +1441,8 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_v3_obfs4_obfsvpn() throws Exception { BuildConfigHelper buildConfigHelper = MockHelper.mockBuildConfigHelper(true); gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo.bitmask.eip-service-obfsvpn1.0.0.json"))).getJSONArray("gateways").getJSONObject(0); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1448,9 +1453,9 @@ public class VpnConfigGeneratorTest { @Test public void testGenerateVpnProfile_v3_ovpn_tcp_udp() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_pt_tcp_udp.eip-service.json"))).getJSONArray("gateways").getJSONObject(0); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + configuration.preferUDP = false; + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1461,9 +1466,8 @@ public class VpnConfigGeneratorTest { @Test public void testGenerateVpnProfile_v3_ovpn_udp_tcp() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_pt_udp_tcp.eip-service.json"))).getJSONArray("gateways").getJSONObject(0); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1476,9 +1480,8 @@ public class VpnConfigGeneratorTest { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_pt_udp_tcp.eip-service.json"))).getJSONArray("gateways").getJSONObject(0); //delete "data-ciphers" from config to test if the resulting openvpn config file will contain the default value taken from "cipher" flag generalConfig.put("data-ciphers", null); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1490,9 +1493,9 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_v4_ovpn_tcp_udp() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("v4/ptdemo_pt_tcp_udp.eip-service.json"))).getJSONArray("gateways").getJSONObject(0); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("v4/ptdemo_pt_tcp_udp.eip-service.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 4; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 4), 4, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + configuration.preferUDP = false; + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1504,9 +1507,8 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_v4_ovpn_udp_tcp() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("v4/ptdemo_pt_udp_tcp.eip-service.json"))).getJSONArray("gateways").getJSONObject(0); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("v4/ptdemo_pt_udp_tcp.eip-service.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 4; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 4), 4, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1518,9 +1520,8 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_v3_ipv6only_allowOpenvpnIPv6Only() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_ipv6.json"))).getJSONArray("gateways").getJSONObject(0); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_ipv6.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OPENVPN)); } @@ -1529,9 +1530,8 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_v3_obfs4IPv6_skip() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_ipv6.json"))).getJSONArray("gateways").getJSONObject(0); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_ipv6.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertFalse(containsKey(vpnProfiles, OBFS4)); } @@ -1543,9 +1543,8 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_v3_obfs4IPv4AndIPv6_skipIPv6() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_ipv4ipv6.json"))).getJSONArray("gateways").getJSONObject(0); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_ipv4ipv6.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1560,9 +1559,8 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_v3_obfs4udp_skip() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_udp.json"))).getJSONArray("gateways").getJSONObject(0); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_udp.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertFalse(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1575,9 +1573,8 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_v3_obfs4TCP_openvpnTCP_skip() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_tcp2.json"))).getJSONArray("gateways").getJSONObject(0); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_tcp2.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertFalse(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1587,9 +1584,8 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_v3_obfs4UDPAndTCP_skipUDP() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_udptcp.json"))).getJSONArray("gateways").getJSONObject(0); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_udptcp.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1600,10 +1596,9 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_preferUDP_firstRemotesUDP() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("v4/multiport_tcpudp_eip-service.json"))).getJSONArray("gateways").getJSONObject(0); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("v4/multiport_tcpudp_eip-service.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.preferUDP = true; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1615,9 +1610,9 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_testNewCiphers() throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("v4/ptdemo_pt_tcp_udp_new_ciphers.eip-service.json"))).getJSONArray("gateways").getJSONObject(0); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("v4/ptdemo_pt_tcp_udp_new_ciphers.eip-service.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 4; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 4), 4, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); + configuration.preferUDP = false; + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); System.out.println(getVpnProfile(vpnProfiles, OPENVPN).getConfigFile(context, false)); assertEquals(expectedVPNConfig_v4_ovpn_tcp_udp_new_ciphers.trim(), getVpnProfile(vpnProfiles, OPENVPN).getConfigFile(context, false).trim()); @@ -1627,11 +1622,10 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfileExperimentalTransportsEnabled () throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_kcp_gateways.json"))).getJSONArray("gateways").getJSONObject(2); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_kcp_gateways.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.experimentalTransports = true; configuration.preferUDP = true; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4) && ((Obfs4Connection)getVpnProfile(vpnProfiles, OBFS4).mConnections[0]).getObfs4Options().transport.getProtocols()[0].equals("kcp")); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1641,10 +1635,9 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_experimentalTransportsEnabled_KCPMisconfiguredWithUDP_SkippingObfsKCP () throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_kcp_gateways.json"))).getJSONArray("gateways").getJSONObject(2); generalConfig = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_misconfigured_kcp_gateways.json"))).getJSONObject(OPENVPN_CONFIGURATION); - VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); - configuration.apiVersion = 3; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.experimentalTransports = true; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertFalse(containsKey(vpnProfiles, OBFS4)); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1662,7 +1655,8 @@ public class VpnConfigGeneratorTest { configuration.obfuscationProxyCert = "asdfasdf"; configuration.obfuscationProxyKCP = true; configuration.remoteGatewayIP = "1.2.3.4"; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + configuration.transports = createTransportsFrom(gateway, 3); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue("has openvpn profile", containsKey(vpnProfiles, OPENVPN)); assertTrue("has obfs4 profile", containsKey(vpnProfiles, OBFS4)); @@ -1682,7 +1676,8 @@ public class VpnConfigGeneratorTest { configuration.obfuscationProxyIP = "5.6.7.8"; configuration.obfuscationProxyCert = "asdfasdf"; configuration.remoteGatewayIP = "1.2.3.4"; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + configuration.transports = createTransportsFrom(gateway, 3); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertFalse("has openvpn profile", containsKey(vpnProfiles, OPENVPN)); assertTrue("has obfs4 profile", containsKey(vpnProfiles, OBFS4)); @@ -1702,8 +1697,9 @@ public class VpnConfigGeneratorTest { configuration.obfuscationProxyIP = "5.6.7.8"; configuration.obfuscationProxyCert = "asdfasdf"; configuration.remoteGatewayIP = "1.2.3.4"; + configuration.transports = createTransportsFrom(gateway, 3); - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertFalse("has openvpn profile", containsKey(vpnProfiles, OPENVPN)); assertTrue("has no obfs4 profile", containsKey(vpnProfiles, OBFS4)); @@ -1714,10 +1710,9 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_obfs4hop_tcp () throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_obfs4hop_tcp_gateways.json"))).getJSONArray("gateways").getJSONObject(2); 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; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.experimentalTransports = true; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4_HOP) && ((Obfs4Connection)getVpnProfile(vpnProfiles, OBFS4_HOP).mConnections[0]).getObfs4Options().transport.getProtocols()[0].equals("tcp")); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1727,10 +1722,9 @@ public class VpnConfigGeneratorTest { public void testGenerateVpnProfile_obfs4hop_kcp () throws Exception { gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("ptdemo_obfs4hop_kcp_gateways.json"))).getJSONArray("gateways").getJSONObject(2); 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; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.experimentalTransports = true; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4_HOP) && ((Obfs4Connection)getVpnProfile(vpnProfiles, OBFS4_HOP).mConnections[0]).getObfs4Options().transport.getProtocols()[0].equals("kcp")); assertTrue(containsKey(vpnProfiles, OPENVPN)); @@ -1741,10 +1735,9 @@ public class VpnConfigGeneratorTest { BuildConfigHelper buildConfigHelper = MockHelper.mockBuildConfigHelper(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; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.experimentalTransports = true; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); System.out.println(getVpnProfile(vpnProfiles, OBFS4_HOP).getConfigFile(context, false)); assertEquals(expectedVPNConfig_hopping_pt_portHopping.trim(), getVpnProfile(vpnProfiles, OBFS4_HOP).getConfigFile(context, false).trim()); @@ -1755,10 +1748,9 @@ public class VpnConfigGeneratorTest { BuildConfigHelper buildConfigHelper = MockHelper.mockBuildConfigHelper(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; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.experimentalTransports = true; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); System.out.println(getVpnProfile(vpnProfiles, OBFS4_HOP).getConfigFile(context, false)); assertEquals(expectedVPNConfig_hopping_pt_portHopping.trim(), getVpnProfile(vpnProfiles, OBFS4_HOP).getConfigFile(context, false).trim()); @@ -1768,10 +1760,9 @@ public class VpnConfigGeneratorTest { BuildConfigHelper buildConfigHelper = MockHelper.mockBuildConfigHelper(true); gateway = new JSONObject(TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream("decoupled_pt.eip-service.json"))).getJSONArray("gateways").getJSONObject(1); 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; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.experimentalTransports = true; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4)); assertTrue(((Obfs4Connection)getVpnProfile(vpnProfiles, OBFS4).mConnections[0]).getObfs4Options().transport.getProtocols()[0].equals("tcp")); @@ -1783,10 +1774,9 @@ public class VpnConfigGeneratorTest { BuildConfigHelper buildConfigHelper = MockHelper.mockBuildConfigHelper(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; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.experimentalTransports = true; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4_HOP)); assertTrue(((Obfs4Connection)getVpnProfile(vpnProfiles, OBFS4_HOP).mConnections[0]).getObfs4Options().transport.getProtocols()[0].equals("tcp")); @@ -1799,10 +1789,9 @@ public class VpnConfigGeneratorTest { BuildConfigHelper buildConfigHelper = MockHelper.mockBuildConfigHelper(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; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.experimentalTransports = false; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Exception exception = null; try { vpnConfigGenerator.generateVpnProfiles(); @@ -1817,10 +1806,9 @@ public class VpnConfigGeneratorTest { BuildConfigHelper buildConfigHelper = MockHelper.mockBuildConfigHelper(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; + VpnConfigGenerator.Configuration configuration = createProfileConfig(createTransportsFrom(gateway, 3), 3, gateway.optString(IP_ADDRESS), gateway.optString(IP_ADDRESS6), ""); configuration.experimentalTransports = true; - vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, gateway, configuration); + vpnConfigGenerator = new VpnConfigGenerator(generalConfig, secrets, configuration); Vector<VpnProfile> vpnProfiles = vpnConfigGenerator.generateVpnProfiles(); assertTrue(containsKey(vpnProfiles, OBFS4_HOP)); assertTrue(((Obfs4Connection)getVpnProfile(vpnProfiles, OBFS4_HOP).mConnections[0]).getObfs4Options().transport.getProtocols()[0].equals("tcp")); diff --git a/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerTest.java index 86f9dc8e..adbcf8cb 100644 --- a/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerTest.java @@ -33,6 +33,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import androidx.annotation.Nullable; @@ -40,6 +41,9 @@ import androidx.annotation.Nullable; import org.json.JSONException; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; import java.io.IOException; import java.security.NoSuchAlgorithmException; @@ -55,6 +59,8 @@ import se.leap.bitmaskclient.base.utils.PrivateKeyHelper; import se.leap.bitmaskclient.testutils.MockSharedPreferences; import se.leap.bitmaskclient.tor.TorStatusObservable; +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Build.VERSION_CODES.P}) public class ProviderApiManagerTest { private Resources mockResources; @@ -158,7 +164,7 @@ public class ProviderApiManagerTest { Bundle expectedResult = new Bundle(); expectedResult.putBoolean(BROADCAST_RESULT_KEY, false); - expectedResult.putString(ERRORS, "{\"errorId\":\"ERROR_TOR_TIMEOUT\",\"initalAction\":\"setUpProvider\",\"errors\":\"Starting bridges failed. Do you want to retry or continue with an unobfuscated secure connection to configure Bitmask?\"}"); + expectedResult.putString(ERRORS, "{\"errors\":\"Starting bridges failed. Do you want to retry or continue with an unobfuscated secure connection to configure Bitmask?\",\"errorId\":\"ERROR_TOR_TIMEOUT\",\"initalAction\":\"setUpProvider\"}"); expectedResult.putParcelable(PROVIDER_KEY, provider); Intent providerApiCommand = new Intent(); diff --git a/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerV3Test.java b/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerV3Test.java index db43970c..5751d1c5 100644 --- a/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerV3Test.java +++ b/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerV3Test.java @@ -47,8 +47,8 @@ import static se.leap.bitmaskclient.testutils.MockHelper.mockCertificateHelper; import static se.leap.bitmaskclient.testutils.MockHelper.mockClientGenerator; import static se.leap.bitmaskclient.testutils.MockHelper.mockContext; import static se.leap.bitmaskclient.testutils.MockHelper.mockPreferenceHelper; -import static se.leap.bitmaskclient.testutils.MockHelper.mockProviderApiConnector; import static se.leap.bitmaskclient.testutils.MockHelper.mockPrivateKeyHelper; +import static se.leap.bitmaskclient.testutils.MockHelper.mockProviderApiConnector; import static se.leap.bitmaskclient.testutils.MockHelper.mockResources; import static se.leap.bitmaskclient.testutils.MockHelper.mockResultReceiver; import static se.leap.bitmaskclient.testutils.TestSetupHelper.getConfiguredProvider; @@ -60,6 +60,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import androidx.annotation.Nullable; @@ -68,6 +69,9 @@ import org.json.JSONException; import org.json.JSONObject; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; import java.io.IOException; import java.security.NoSuchAlgorithmException; @@ -87,7 +91,8 @@ import se.leap.bitmaskclient.tor.TorStatusObservable; /** * Created by cyberta on 04.01.18. */ - +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Build.VERSION_CODES.P}) public class ProviderApiManagerV3Test { private Resources mockResources; @@ -250,7 +255,7 @@ public class ProviderApiManagerV3Test { providerApiManager = new ProviderApiManagerV3(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback()); Bundle expectedResult = new Bundle(); 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.\"}"); + expectedResult.putString(ERRORS, "{\"errors\":\"Stored provider certificate is invalid. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\",\"errorId\":\"ERROR_CERTIFICATE_PINNING\"}"); expectedResult.putParcelable(PROVIDER_KEY, provider); providerApiManager.handleAction(SET_UP_PROVIDER, provider, new Bundle(), mockResultReceiver(PROVIDER_NOK, expectedResult)); @@ -267,7 +272,7 @@ public class ProviderApiManagerV3Test { Bundle expectedResult = new Bundle(); 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.\"}"); + expectedResult.putString(ERRORS, "{\"errors\":\"Stored provider certificate is invalid. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\",\"errorId\":\"ERROR_CERTIFICATE_PINNING\"}"); expectedResult.putParcelable(PROVIDER_KEY, provider); providerApiManager.handleAction(SET_UP_PROVIDER, provider, new Bundle(), mockResultReceiver(PROVIDER_NOK, expectedResult)); @@ -284,7 +289,7 @@ public class ProviderApiManagerV3Test { Bundle expectedResult = new Bundle(); 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.\"}"); + expectedResult.putString(ERRORS, "{\"errors\":\"Stored provider certificate is invalid. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\",\"errorId\":\"ERROR_CERTIFICATE_PINNING\"}"); expectedResult.putParcelable(PROVIDER_KEY, provider); providerApiManager.handleAction(SET_UP_PROVIDER, provider, new Bundle(), mockResultReceiver(PROVIDER_NOK, expectedResult)); @@ -301,7 +306,7 @@ public class ProviderApiManagerV3Test { Bundle expectedResult = new Bundle(); expectedResult.putBoolean(BROADCAST_RESULT_KEY, false); - expectedResult.putString(ERRORS, "{\"errorId\":\"ERROR_INVALID_CERTIFICATE\",\"errors\":\"Stored provider certificate is expired. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\"}"); + expectedResult.putString(ERRORS, "{\"errors\":\"Stored provider certificate is expired. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\",\"errorId\":\"ERROR_INVALID_CERTIFICATE\"}"); expectedResult.putParcelable(PROVIDER_KEY, provider); providerApiManager.handleAction(SET_UP_PROVIDER, provider, new Bundle(), mockResultReceiver(PROVIDER_NOK, expectedResult)); @@ -318,7 +323,7 @@ public class ProviderApiManagerV3Test { Bundle expectedResult = new Bundle(); expectedResult.putBoolean(BROADCAST_RESULT_KEY, false); - expectedResult.putString(ERRORS, "{\"errorId\":\"ERROR_INVALID_CERTIFICATE\",\"errors\":\"Stored provider certificate is expired. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\"}"); + expectedResult.putString(ERRORS, "{\"errors\":\"Stored provider certificate is expired. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\",\"errorId\":\"ERROR_INVALID_CERTIFICATE\"}"); expectedResult.putParcelable(PROVIDER_KEY, provider); providerApiManager.handleAction(SET_UP_PROVIDER, provider, new Bundle(), mockResultReceiver(PROVIDER_NOK, expectedResult)); @@ -334,7 +339,7 @@ public class ProviderApiManagerV3Test { providerApiManager = new ProviderApiManagerV3(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback()); Bundle expectedResult = new Bundle(); 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.\"}"); + expectedResult.putString(ERRORS, "{\"errors\":\"Stored provider certificate is invalid. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\",\"errorId\":\"ERROR_INVALID_CERTIFICATE\"}"); expectedResult.putParcelable(PROVIDER_KEY, provider); Intent providerApiCommand = new Intent(); @@ -344,15 +349,15 @@ public class ProviderApiManagerV3Test { @Test public void test_handleIntentSetupProvider_storedProviderAndCAFromPreviousSetup_ValidCertificateButUpdatedCertificateOnServerSide() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException { - Provider provider = new Provider("https://riseup.net"); - PreferenceHelper preferenceHelper = mockPreferenceHelper(getConfiguredProvider()); + Provider provider = getConfiguredProvider(); + PreferenceHelper preferenceHelper = mockPreferenceHelper(provider); CertificateHelper certHelper = mockCertificateHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494"); ProviderApiConnector mockedApiConnector = mockProviderApiConnector(ERROR_CASE_UPDATED_CERTIFICATE); providerApiManager = new ProviderApiManagerV3(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback()); Bundle expectedResult = new Bundle(); 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.\"}"); + expectedResult.putString(ERRORS, "{\"errors\":\"Stored provider certificate is invalid. You can either update Bitmask (recommended) or update the provider certificate using a commercial CA certificate.\",\"errorId\":\"ERROR_INVALID_CERTIFICATE\"}"); expectedResult.putParcelable(PROVIDER_KEY, provider); providerApiManager.handleAction(SET_UP_PROVIDER, provider, new Bundle(), mockResultReceiver(PROVIDER_NOK, expectedResult)); @@ -361,11 +366,11 @@ public class ProviderApiManagerV3Test { @Test public void test_handleIntentSetupProvider_preseededProviderAndCA_failedConfiguration() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException { - Provider provider = getConfiguredProvider(); + Provider provider = getProvider(null, null, null, null, null, "riseup_net_invalid_config.json", null, null); PreferenceHelper preferenceHelper = mockPreferenceHelper(provider); CertificateHelper certHelper = mockCertificateHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494"); - ProviderApiConnector mockedApiConnector = mockProviderApiConnector(ERROR_CASE_MICONFIGURED_PROVIDER); + ProviderApiConnector mockedApiConnector = mockProviderApiConnector(NO_ERROR); providerApiManager = new ProviderApiManagerV3(mockResources, mockClientGenerator(), new TestProviderApiServiceCallback()); @@ -380,8 +385,8 @@ public class ProviderApiManagerV3Test { @Test public void test_handleIntentSetupProvider_preseededCustomProviderAndCA_failedConfiguration() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, JSONException { - Provider provider = getConfiguredProvider(); - PreferenceHelper preferenceHelper = mockPreferenceHelper(provider); + Provider provider = new Provider("riseup.net"); + PreferenceHelper preferenceHelper = new PreferenceHelper(new MockSharedPreferences()); ProviderApiConnector mockedApiConnector = mockProviderApiConnector(ERROR_CASE_MICONFIGURED_PROVIDER); CertificateHelper certHelper = mockCertificateHelper("a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494"); @@ -554,7 +559,7 @@ public class ProviderApiManagerV3Test { @Test public void test_handleIntentSetupProvider_TorFallback_SecondTryHappyPath() throws IOException, CertificateEncodingException, NoSuchAlgorithmException, TimeoutException, InterruptedException { - Provider provider = getConfiguredProviderAPIv4(); + Provider provider = new Provider("riseup.net"); PreferenceHelper preferenceHelper = mockPreferenceHelper(provider); CertificateHelper certHelper = mockCertificateHelper(" a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494"); ProviderApiConnector mockedApiConnector = mockProviderApiConnector(ERROR_DNS_RESUOLUTION_TOR_FALLBACK); 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 7cca9c2f..b6a2becd 100644 --- a/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java @@ -9,26 +9,36 @@ import static org.mockito.Mockito.when; import android.content.SharedPreferences; import android.content.res.AssetManager; +import android.os.Build; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; import java.io.InputStream; import java.util.HashSet; import java.util.Set; +import mobile.BitmaskMobile; +import mobilemodels.BitmaskMobileCore; import se.leap.bitmaskclient.base.models.Constants; import se.leap.bitmaskclient.base.models.Provider; +import se.leap.bitmaskclient.base.utils.BitmaskCoreProvider; 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; /** * Created by cyberta on 20.02.18. */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Build.VERSION_CODES.P}) public class ProviderManagerTest { private AssetManager assetManager; @@ -40,6 +50,7 @@ public class ProviderManagerTest { @Before public void setup() throws Exception { assetManager = mock(AssetManager.class); + BitmaskCoreProvider.initBitmaskMobile(TestSetupHelper.getCustomBitmaskCore()); when(assetManager.open(anyString())).thenAnswer(new Answer<InputStream>() { @Override @@ -162,7 +173,7 @@ public class ProviderManagerTest { providerManager.setAddDummyEntry(true); providerManager.clear(); assertEquals("1 providers", 1, providerManager.providers().size()); - assertEquals("provider is dummy element", "https://example.net", providerManager.get(0).getMainUrl()); + assertEquals("provider is dummy element", "", providerManager.get(0).getMainUrl()); } @Test @@ -195,8 +206,8 @@ public class ProviderManagerTest { providerManager.add(secondCustomProvider); providerManager.saveCustomProviders(); Set<String> providerSet = mockSharedPrefs.getStringSet(Constants.CUSTOM_PROVIDER_DOMAINS, new HashSet<>()); - assertEquals("persist was called twice", 2, providerSet.size()); - assertEquals("PreferenceHelper has 2 providers", 2, PreferenceHelper.getCustomProviders().size()); + assertEquals("persist was called twice", 3, providerSet.size()); + assertEquals("PreferenceHelper has 2 providers", 3, PreferenceHelper.getCustomProviders().size()); } diff --git a/app/src/test/java/se/leap/bitmaskclient/testutils/TestSetupHelper.java b/app/src/test/java/se/leap/bitmaskclient/testutils/TestSetupHelper.java index 72791ba6..97420203 100644 --- a/app/src/test/java/se/leap/bitmaskclient/testutils/TestSetupHelper.java +++ b/app/src/test/java/se/leap/bitmaskclient/testutils/TestSetupHelper.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import mobilemodels.BitmaskMobileCore; import se.leap.bitmaskclient.base.models.Provider; /** @@ -105,4 +106,83 @@ public class TestSetupHelper { return null; } + public static BitmaskMobileCore getCustomBitmaskCore() { + return new BitmaskMobileCore() { + @Override + public String getAllBridges(String s, String s1, String s2, String s3) throws Exception { + return null; + } + + @Override + public String getAllGateways(String s, String s1, String s2) throws Exception { + return null; + } + + @Override + public String getBestBridge() throws Exception { + return null; + } + + @Override + public String getBestGateway() throws Exception { + return null; + } + + @Override + public String getGeolocation() throws Exception { + return null; + } + + @Override + public String getIntroducerURLByDomain(String s) throws Exception { + return null; + } + + @Override + public String getOpenVPNCert() throws Exception { + return null; + } + + @Override + public String getProvider() throws Exception { + return null; + } + + @Override + public String getService() throws Exception { + return null; + } + + @Override + public void setCountryCode(String s) { + + } + + @Override + public void setDebug(boolean b) { + + } + + @Override + public void setIntroducer(String s) throws Exception { + + } + + @Override + public void setResolveWithDoH(boolean b) { + + } + + @Override + public void setSocksProxy(String s) { + + } + + @Override + public void setUseTls(boolean b) { + + } + }; + } + } |