diff options
author | cyBerta <cyberta@riseup.net> | 2024-01-24 23:18:34 +0100 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2024-01-24 23:20:02 +0100 |
commit | 827be305805bd98362692f506c46dc7c73c6fc97 (patch) | |
tree | 8261623c065af662fba6d4ae0c85302f8c155741 /app/src/test | |
parent | b73cb05bdcbf9947d0d0ea8c56e894baa395c7f1 (diff) |
fix GatewaysManagerTest
Diffstat (limited to 'app/src/test')
-rw-r--r-- | app/src/test/java/android/util/Log.java | 23 | ||||
-rw-r--r-- | app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java | 237 |
2 files changed, 123 insertions, 137 deletions
diff --git a/app/src/test/java/android/util/Log.java b/app/src/test/java/android/util/Log.java new file mode 100644 index 00000000..b3162d10 --- /dev/null +++ b/app/src/test/java/android/util/Log.java @@ -0,0 +1,23 @@ +package android.util; + +public class Log { + public static int d(String tag, String msg) { + System.out.println("DEBUG: " + tag + ": " + msg); + return 0; + } + + public static int i(String tag, String msg) { + System.out.println("INFO: " + tag + ": " + msg); + return 0; + } + + public static int w(String tag, String msg) { + System.out.println("WARN: " + tag + ": " + msg); + return 0; + } + + public static int e(String tag, String msg) { + System.out.println("ERROR: " + tag + ": " + msg); + return 0; + } +}
\ No newline at end of file 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 86373ff6..d66fedbe 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java @@ -1,19 +1,30 @@ package se.leap.bitmaskclient.eip; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4; +import static de.blinkt.openvpn.core.connection.Connection.TransportType.OPENVPN; +import static se.leap.bitmaskclient.base.models.Constants.ALLOW_EXPERIMENTAL_TRANSPORTS; +import static se.leap.bitmaskclient.base.models.Constants.GATEWAYS; +import static se.leap.bitmaskclient.base.models.Constants.PREFERRED_CITY; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_EIP_DEFINITION; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_PRIVATE_KEY; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.base.models.Constants.USE_BRIDGES; +import static se.leap.bitmaskclient.base.models.Provider.CA_CERT; +import static se.leap.bitmaskclient.testutils.TestSetupHelper.getProvider; + import android.content.Context; import android.content.SharedPreferences; -import android.text.TextUtils; -import android.util.Log; 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.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import java.io.IOException; import java.util.ArrayList; @@ -21,42 +32,17 @@ import java.util.List; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.ConfigParser; -import de.blinkt.openvpn.core.connection.Connection; import se.leap.bitmaskclient.base.models.Location; -import se.leap.bitmaskclient.base.models.Pair; import se.leap.bitmaskclient.base.models.Provider; import se.leap.bitmaskclient.base.models.ProviderObservable; -import se.leap.bitmaskclient.base.utils.ConfigHelper; import se.leap.bitmaskclient.base.utils.PreferenceHelper; -import se.leap.bitmaskclient.testutils.MockHelper; +import se.leap.bitmaskclient.base.utils.TimezoneHelper; import se.leap.bitmaskclient.testutils.MockSharedPreferences; import se.leap.bitmaskclient.testutils.TestSetupHelper; -import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4; -import static de.blinkt.openvpn.core.connection.Connection.TransportType.OPENVPN; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNull; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static se.leap.bitmaskclient.base.models.Constants.GATEWAYS; -import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_EIP_DEFINITION; -import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_PRIVATE_KEY; -import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_VPN_CERTIFICATE; -import static se.leap.bitmaskclient.base.models.Constants.USE_BRIDGES; -import static se.leap.bitmaskclient.base.models.Provider.CA_CERT; -import static se.leap.bitmaskclient.testutils.MockHelper.mockTextUtils; -import static se.leap.bitmaskclient.testutils.TestSetupHelper.getProvider; - /** * Created by cyberta on 09.10.17. */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({ProviderObservable.class, Log.class, PreferenceHelper.class, ConfigHelper.class, TextUtils.class}) public class GatewaysManagerTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) @@ -65,17 +51,15 @@ public class GatewaysManagerTest { private SharedPreferences sharedPreferences; private JSONObject secrets; + private TimezoneHelper timezoneHelper; + ProviderObservable providerObservable; + @Before public void setUp() throws IOException, JSONException { - mockStatic(Log.class); - mockStatic(ConfigHelper.class); - mockTextUtils(); - when(ConfigHelper.getCurrentTimezone()).thenReturn(-1); - when(ConfigHelper.stringEqual(anyString(), anyString())).thenCallRealMethod(); - when(ConfigHelper.getConnectionQualityFromTimezoneDistance(anyInt())).thenCallRealMethod(); - when(ConfigHelper.isIPv4(anyString())).thenCallRealMethod(); - when(ConfigHelper.timezoneDistance(anyInt(), anyInt())).thenCallRealMethod(); + timezoneHelper = new TimezoneHelper(() -> -1); + providerObservable = ProviderObservable.getInstance(); + secrets = new JSONObject(getJsonStringFor("secrets.json")); sharedPreferences = new MockSharedPreferences(); sharedPreferences.edit(). @@ -83,22 +67,20 @@ public class GatewaysManagerTest { putString(CA_CERT, secrets.getString(CA_CERT)). putString(PROVIDER_VPN_CERTIFICATE, secrets.getString(PROVIDER_VPN_CERTIFICATE)) .commit(); - mockStatic(PreferenceHelper.class); PreferenceHelper preferenceHelper = new PreferenceHelper(sharedPreferences); } @Test public void testGatewayManagerFromCurrentProvider_noProvider_noGateways() { - MockHelper.mockProviderObservable(null); + providerObservable.updateProvider(null); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals(0, gatewaysManager.size()); } @Test public void testGatewayManagerFromCurrentProvider_threeGateways() { - Provider provider = getProvider(null, null, null, null,null, null, "ptdemo_three_mixed_gateways.json", null); - MockHelper.mockProviderObservable(provider); + providerObservable.updateProvider(getProvider(null, null, null, null,null, null, "ptdemo_three_mixed_gateways.json", null)); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals(3, gatewaysManager.size()); } @@ -108,7 +90,7 @@ public class GatewaysManagerTest { Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_three_mixed_gateways.json", null); JSONObject eipServiceJson = provider.getEipServiceJson(); JSONObject gateway1 = eipServiceJson.getJSONArray(GATEWAYS).getJSONObject(0); - MockHelper.mockProviderObservable(provider); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); @@ -125,7 +107,7 @@ public class GatewaysManagerTest { Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_three_mixed_gateways.json", null); JSONObject eipServiceJson = provider.getEipServiceJson(); JSONObject gateway1 = eipServiceJson.getJSONArray(GATEWAYS).getJSONObject(0); - MockHelper.mockProviderObservable(provider); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); @@ -142,7 +124,7 @@ public class GatewaysManagerTest { 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.mockProviderObservable(provider); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); @@ -159,7 +141,7 @@ public class GatewaysManagerTest { 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.mockProviderObservable(provider); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); @@ -176,7 +158,7 @@ public class GatewaysManagerTest { Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_three_mixed_gateways.json", null); JSONObject eipServiceJson = provider.getEipServiceJson(); JSONObject gateway1 = eipServiceJson.getJSONArray(GATEWAYS).getJSONObject(0); - MockHelper.mockProviderObservable(provider); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); @@ -193,7 +175,7 @@ public class GatewaysManagerTest { Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_three_mixed_gateways.json", null); JSONObject eipServiceJson = provider.getEipServiceJson(); JSONObject gateway1 = eipServiceJson.getJSONArray(GATEWAYS).getJSONObject(1); - MockHelper.mockProviderObservable(provider); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); VpnConfigGenerator.Configuration configuration = new VpnConfigGenerator.Configuration(); @@ -209,9 +191,8 @@ public class GatewaysManagerTest { public void TestSelectN_selectFirstObfs4Connection_returnThirdGateway() { Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_two_openvpn_one_pt_gateways.json", null); - MockHelper.mockProviderObservable(provider); - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(true); + providerObservable.updateProvider(provider); + sharedPreferences.edit().putBoolean(USE_BRIDGES, true).commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals("37.12.247.10", gatewaysManager.select(0).gateway.getRemoteIP()); @@ -221,10 +202,11 @@ public class GatewaysManagerTest { 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()).thenReturn(true); - when(PreferenceHelper.allowExperimentalTransports()).thenReturn(true); + providerObservable.updateProvider(provider); + sharedPreferences.edit(). + putBoolean(USE_BRIDGES, true). + putBoolean(ALLOW_EXPERIMENTAL_TRANSPORTS, true). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); ArrayList<String> hosts = new ArrayList<>(); hosts.add(gatewaysManager.select(0).gateway.getHost()); @@ -239,10 +221,11 @@ public class GatewaysManagerTest { 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()).thenReturn(true); - when(PreferenceHelper.allowExperimentalTransports()).thenReturn(true); + providerObservable.updateProvider(provider); + sharedPreferences.edit(). + putBoolean(USE_BRIDGES, true). + putBoolean(ALLOW_EXPERIMENTAL_TRANSPORTS, true). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); ArrayList<String> hosts = new ArrayList<>(); @@ -256,10 +239,11 @@ public class GatewaysManagerTest { 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()).thenReturn(true); - when(PreferenceHelper.allowExperimentalTransports()).thenReturn(false); + providerObservable.updateProvider(provider); + sharedPreferences.edit(). + putBoolean(USE_BRIDGES, true). + putBoolean(ALLOW_EXPERIMENTAL_TRANSPORTS, false). + commit(); for (int i = 0; i < 1000; i++) { GatewaysManager gatewaysManager = new GatewaysManager(mockContext); @@ -271,10 +255,11 @@ public class GatewaysManagerTest { 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()).thenReturn(true); - when(PreferenceHelper.allowExperimentalTransports()).thenReturn(false); + providerObservable.updateProvider(provider); + sharedPreferences.edit(). + putBoolean(USE_BRIDGES, true). + putBoolean(ALLOW_EXPERIMENTAL_TRANSPORTS, false). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals("bridge-nyc1-01.bitmask-dev.leapvpn.net", gatewaysManager.select(0).gateway.getHost()); @@ -285,10 +270,11 @@ public class GatewaysManagerTest { 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()).thenReturn(true); - when(PreferenceHelper.allowExperimentalTransports()).thenReturn(false); + providerObservable.updateProvider(provider); + sharedPreferences.edit(). + putBoolean(USE_BRIDGES, true). + putBoolean(ALLOW_EXPERIMENTAL_TRANSPORTS, false). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); ArrayList<String> hosts = new ArrayList<>(); @@ -303,10 +289,7 @@ public class GatewaysManagerTest { 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.mockProviderObservable(provider); - //use openvpn, not pluggable transports - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals("manila.bitmask.net", gatewaysManager.select(0).gateway.getHost()); @@ -318,10 +301,11 @@ public class GatewaysManagerTest { 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.mockProviderObservable(provider); + providerObservable.updateProvider(provider); //use openvpn, not pluggable transports - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(true); + sharedPreferences.edit(). + putBoolean(USE_BRIDGES, true). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals("moscow.bitmask.net", gatewaysManager.select(0).gateway.getHost()); @@ -334,11 +318,11 @@ public class GatewaysManagerTest { public void testSelectN_selectFromCity_returnsGatewaysInPresortedOrder() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v4.json"); - MockHelper.mockProviderObservable(provider); + providerObservable.updateProvider(provider); //use openvpn, not pluggable transports - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); - when(PreferenceHelper.getPreferredCity()).thenReturn("Paris"); + sharedPreferences.edit(). + putString(PREFERRED_CITY, "Paris"). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals("mouette.riseup.net", gatewaysManager.select(0).gateway.getHost()); @@ -350,11 +334,11 @@ public class GatewaysManagerTest { public void testSelectN_selectFromCityWithGeoIpServiceV1_returnsGatewaysInPresortedOrder() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v1.json"); - MockHelper.mockProviderObservable(provider); + providerObservable.updateProvider(provider); //use openvpn, not pluggable transports - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); - when(PreferenceHelper.getPreferredCity()).thenReturn("Paris"); + sharedPreferences.edit(). + putString(PREFERRED_CITY, "Paris"). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals("mouette.riseup.net", gatewaysManager.select(0).gateway.getHost()); @@ -367,11 +351,11 @@ public class GatewaysManagerTest { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", null); provider.setGeoIpJson(new JSONObject()); - MockHelper.mockProviderObservable(provider); + providerObservable.updateProvider(provider); //use openvpn, not pluggable transports - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); - when(PreferenceHelper.getPreferredCity()).thenReturn("Paris"); + sharedPreferences.edit(). + putString(PREFERRED_CITY, "Paris"). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals("Paris", gatewaysManager.select(0).gateway.getName()); @@ -384,10 +368,7 @@ public class GatewaysManagerTest { public void testSelectN_selectNAndCity_returnsGatewaysInPresortedOrder() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v4.json"); - MockHelper.mockProviderObservable(provider); - //use openvpn, not pluggable transports - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals("mouette.riseup.net", gatewaysManager.select(0, "Paris").gateway.getHost()); @@ -399,10 +380,7 @@ public class GatewaysManagerTest { public void testSelectN_selectNAndCityWithGeoIpServiceV1_returnsGatewaysInPresortedOrder() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v1.json"); - MockHelper.mockProviderObservable(provider); - //use openvpn, not pluggable transports - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals("mouette.riseup.net", gatewaysManager.select(0, "Paris").gateway.getHost()); @@ -415,10 +393,7 @@ public class GatewaysManagerTest { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", null); provider.setGeoIpJson(new JSONObject()); - MockHelper.mockProviderObservable(provider); - //use openvpn, not pluggable transports - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals("Paris", gatewaysManager.select(0, "Paris").gateway.getName()); @@ -432,10 +407,7 @@ public class GatewaysManagerTest { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v4.json"); provider.setGeoIpJson(new JSONObject()); - MockHelper.mockProviderObservable(provider); - //use openvpn, not pluggable transports - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertNull(gatewaysManager.select(0, "Stockholm")); } @@ -444,9 +416,10 @@ public class GatewaysManagerTest { public void testGetLocations_openvpn() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v4.json"); - MockHelper.mockProviderObservable(provider); - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); + providerObservable.updateProvider(provider); + sharedPreferences.edit(). + putString(PREFERRED_CITY, "Paris"). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); List<Location> locations = gatewaysManager.getGatewayLocations(); @@ -465,9 +438,7 @@ public class GatewaysManagerTest { public void testGetLocations_obfs4() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v4.json"); - MockHelper.mockProviderObservable(provider); - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(true); + providerObservable.updateProvider(provider); sharedPreferences.edit().putBoolean(USE_BRIDGES, true).commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); List<Location> locations = gatewaysManager.getGatewayLocations(); @@ -496,9 +467,7 @@ public class GatewaysManagerTest { public void testGetLocations_noMenshen_obfs4_calculateAverageLoadFromTimezoneDistance() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v1.json"); - MockHelper.mockProviderObservable(provider); - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(true); + providerObservable.updateProvider(provider); sharedPreferences.edit().putBoolean(USE_BRIDGES, true).commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); List<Location> locations = gatewaysManager.getGatewayLocations(); @@ -521,10 +490,7 @@ public class GatewaysManagerTest { public void testGetLocations_noMenshen_openvpn_calculateAverageLoadFromTimezoneDistance() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v1.json"); - MockHelper.mockProviderObservable(provider); - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); - sharedPreferences.edit().putBoolean(USE_BRIDGES, false).commit(); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); List<Location> locations = gatewaysManager.getGatewayLocations(); @@ -547,9 +513,7 @@ public class GatewaysManagerTest { public void testGetSortedLocations_openvpn() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v4_bad_obfs4_gateway.json"); - MockHelper.mockProviderObservable(provider); - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); List<Location> locations = gatewaysManager.getSortedGatewayLocations(OPENVPN); @@ -576,9 +540,7 @@ public class GatewaysManagerTest { public void testGetSortedLocations_obfs4() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v4_bad_obfs4_gateway.json"); - MockHelper.mockProviderObservable(provider); - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); List<Location> locations = gatewaysManager.getSortedGatewayLocations(OBFS4); @@ -606,9 +568,7 @@ public class GatewaysManagerTest { public void testGetSortedLocations_obfs4kcp_generalizedAsPT() { Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", "v4/riseup_geoip_v4_bad_obfs4_gateway.json"); - MockHelper.mockProviderObservable(provider); - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); List<Location> locations = gatewaysManager.getSortedGatewayLocations(OBFS4); @@ -619,9 +579,10 @@ public class GatewaysManagerTest { public void testgetAverageLoad_isSameForAllTransports() { Provider provider = getProvider(null, null, null, null, null, null, "ptdemo_kcp_gateways.json", "ptdemo_kcp_gateways_geoip.json"); - MockHelper.mockProviderObservable(provider); - mockStatic(PreferenceHelper.class); - when(PreferenceHelper.getUseBridges()).thenReturn(false); + providerObservable.updateProvider(provider); + sharedPreferences.edit(). + putString(PREFERRED_CITY, "Paris"). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals(0.3, gatewaysManager.getLocation("Amsterdam").getAverageLoad(OBFS4)); @@ -630,7 +591,7 @@ public class GatewaysManagerTest { @Test public void testGetLoadForLocation_() { - MockHelper.mockProviderObservable(null); + providerObservable.updateProvider(null); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals(GatewaysManager.Load.UNKNOWN, gatewaysManager.getLoadForLocation("unknown city", OPENVPN)); } @@ -638,7 +599,7 @@ public class GatewaysManagerTest { @Test public void testGatewayManagerFromCurrentProvider_decoupledBridges_twoGateways() throws IOException, NullPointerException { Provider provider = getProvider(null, null, null, null, null, null, "decoupled_pt.eip-service.json", null); - MockHelper.mockProviderObservable(provider); + providerObservable.updateProvider(provider); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals(2, gatewaysManager.size()); } @@ -646,9 +607,11 @@ public class GatewaysManagerTest { @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()).thenReturn(true); + providerObservable.updateProvider(provider); + sharedPreferences.edit(). + putBoolean(USE_BRIDGES, true). + putBoolean(ALLOW_EXPERIMENTAL_TRANSPORTS, true). + commit(); GatewaysManager gatewaysManager = new GatewaysManager(mockContext); assertEquals(3, gatewaysManager.size()); } |