diff options
Diffstat (limited to 'app/src/test')
4 files changed, 383 insertions, 0 deletions
diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java index 6056f764..81fcd7d5 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java @@ -16,9 +16,11 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import java.io.IOException; +import java.util.List; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.ConfigParser; +import se.leap.bitmaskclient.base.models.Location; import se.leap.bitmaskclient.base.models.Provider; import se.leap.bitmaskclient.base.models.ProviderObservable; import se.leap.bitmaskclient.testutils.MockHelper; @@ -233,6 +235,163 @@ public class GatewaysManagerTest { } + @Test + 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.mockProviderObserver(provider); + //use openvpn, not pluggable transports + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUsePluggableTransports(any(Context.class))).thenReturn(false); + when(PreferenceHelper.getPreferredCity(any(Context.class))).thenReturn("Paris"); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + assertEquals("mouette.riseup.net", gatewaysManager.select(0).getHost()); + assertEquals("hoatzin.riseup.net", gatewaysManager.select(1).getHost()); + assertEquals("zarapito.riseup.net", gatewaysManager.select(2).getHost()); + } + + @Test + 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.mockProviderObserver(provider); + //use openvpn, not pluggable transports + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUsePluggableTransports(any(Context.class))).thenReturn(false); + when(PreferenceHelper.getPreferredCity(any(Context.class))).thenReturn("Paris"); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + assertEquals("mouette.riseup.net", gatewaysManager.select(0).getHost()); + assertEquals("hoatzin.riseup.net", gatewaysManager.select(1).getHost()); + assertEquals("zarapito.riseup.net", gatewaysManager.select(2).getHost()); + } + + @Test + public void testSelectN_selectFromCityWithTimezoneCalculation_returnsRandomizedGatewaysOfSelectedCity() { + Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", null); + + provider.setGeoIpJson(new JSONObject()); + MockHelper.mockProviderObserver(provider); + //use openvpn, not pluggable transports + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUsePluggableTransports(any(Context.class))).thenReturn(false); + when(PreferenceHelper.getPreferredCity(any(Context.class))).thenReturn("Paris"); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + assertEquals("Paris", gatewaysManager.select(0).getName()); + assertEquals("Paris", gatewaysManager.select(1).getName()); + assertEquals("Paris", gatewaysManager.select(2).getName()); + assertEquals(null, gatewaysManager.select(3)); + } + + @Test + 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.mockProviderObserver(provider); + //use openvpn, not pluggable transports + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUsePluggableTransports(any(Context.class))).thenReturn(false); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + assertEquals("mouette.riseup.net", gatewaysManager.select(0, "Paris").getHost()); + assertEquals("hoatzin.riseup.net", gatewaysManager.select(1, "Paris").getHost()); + assertEquals("zarapito.riseup.net", gatewaysManager.select(2, "Paris").getHost()); + } + + @Test + 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.mockProviderObserver(provider); + //use openvpn, not pluggable transports + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUsePluggableTransports(any(Context.class))).thenReturn(false); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + assertEquals("mouette.riseup.net", gatewaysManager.select(0, "Paris").getHost()); + assertEquals("hoatzin.riseup.net", gatewaysManager.select(1, "Paris").getHost()); + assertEquals("zarapito.riseup.net", gatewaysManager.select(2, "Paris").getHost()); + } + + @Test + public void testSelectN_selectNAndCityWithTimezoneCalculation_returnsRandomizedGatewaysOfSelectedCity() { + Provider provider = getProvider(null, null, null, null, null, null, "v4/riseup_eipservice_for_geoip_v4.json", null); + + provider.setGeoIpJson(new JSONObject()); + MockHelper.mockProviderObserver(provider); + //use openvpn, not pluggable transports + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUsePluggableTransports(any(Context.class))).thenReturn(false); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + + assertEquals("Paris", gatewaysManager.select(0, "Paris").getName()); + assertEquals("Paris", gatewaysManager.select(1, "Paris").getName()); + assertEquals("Paris", gatewaysManager.select(2, "Paris").getName()); + assertEquals(null, gatewaysManager.select(3, "Paris")); + } + + @Test + public void testSelectN_selectFromCityWithTimezoneCalculationCityNotExisting_returnsNull() { + 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.mockProviderObserver(provider); + //use openvpn, not pluggable transports + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUsePluggableTransports(any(Context.class))).thenReturn(false); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + assertNull(gatewaysManager.select(0, "Stockholm")); + } + + @Test + 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.mockProviderObserver(provider); + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUsePluggableTransports(any(Context.class))).thenReturn(false); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + List<Location> locations = gatewaysManager.getGatewayLocations(); + + assertEquals(3, locations.size()); + for (Location location : locations) { + if ("Paris".equals(location.name)) { + assertEquals(3, location.numberOfGateways); + // manually calculate average load of paris gateways in "v4/riseup_geoip_v4.json" + double averageLoad = (0.3 + 0.36 + 0.92) / 3.0; + assertEquals(averageLoad, location.averageLoad); + } + } + } + + @Test + 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.mockProviderObserver(provider); + mockStatic(PreferenceHelper.class); + when(PreferenceHelper.getUsePluggableTransports(any(Context.class))).thenReturn(true); + GatewaysManager gatewaysManager = new GatewaysManager(mockContext); + List<Location> locations = gatewaysManager.getGatewayLocations(); + + assertEquals(2, locations.size()); + for (Location location : locations) { + if ("Montreal".equals(location.name)) { + assertEquals(1, location.numberOfGateways); + assertEquals(0.59, location.averageLoad); + } + if ("Paris".equals(location.name)) { + // checks that only gateways supporting obfs4 are taken into account + assertEquals(1, location.numberOfGateways); + assertEquals(0.36, location.averageLoad); + } + } + + } + + private String getJsonStringFor(String filename) throws IOException { return TestSetupHelper.getInputAsString(getClass().getClassLoader().getResourceAsStream(filename)); } diff --git a/app/src/test/resources/v4/riseup_eipservice_for_geoip_v4.json b/app/src/test/resources/v4/riseup_eipservice_for_geoip_v4.json new file mode 100644 index 00000000..76fbea52 --- /dev/null +++ b/app/src/test/resources/v4/riseup_eipservice_for_geoip_v4.json @@ -0,0 +1,169 @@ +{ + "gateways": [ + { + "capabilities": { + "adblock": false, + "filter_dns": false, + "limited": false, + "transport":[ + { + "type":"openvpn", + "protocols":[ + "tcp" + ], + "ports":[ + "443" + ] + } + ], + "user_ips": false + }, + "host": "zarapito.riseup.net", + "ip_address": "212.129.62.247", + "location": "paris" + }, + { + "capabilities": { + "adblock": false, + "filter_dns": false, + "limited": false, + "transport":[ + { + "type":"obfs4", + "protocols":[ + "tcp" + ], + "ports":[ + "23042" + ], + "options": { + "cert": "48F/JNm/LKYt7zkDmPHXcw5f3Jqgwg/3OBRrqW14Yj87ATZ4KyAZRV7np4RhCXGSJHgoCQ", + "iatMode": "0" + } + }, + { + "type":"openvpn", + "protocols":[ + "tcp" + ], + "ports":[ + "443" + ] + } + ], + "user_ips": false + }, + "host": "hoatzin.riseup.net", + "ip_address": "212.83.143.67", + "location": "paris" + }, + { + "capabilities": { + "adblock": false, + "filter_dns": false, + "limited": false, + "transport":[ + { + "type":"openvpn", + "protocols":[ + "tcp" + ], + "ports":[ + "443" + ] + } + ], + "user_ips": false + }, + "host": "mouette.riseup.net", + "ip_address": "163.172.126.44", + "location": "paris" + }, + { + "capabilities": { + "adblock": false, + "filter_dns": false, + "limited": false, + "transport":[ + { + "type":"openvpn", + "protocols":[ + "tcp" + ], + "ports":[ + "443" + ] + } + ], + "user_ips": false + }, + "host": "redshank.riseup.net", + "ip_address": "212.83.165.160", + "location": "amsterdam" + }, + { + "capabilities": { + "adblock": false, + "filter_dns": false, + "limited": false, + "transport":[ + { + "type":"obfs4", + "protocols":[ + "tcp" + ], + "ports":[ + "23042" + ], + "options": { + "cert": "48F/JNm/LKYt7zkDmPHXcw5f3Jqgwg/3OBRrqW14Yj87ATZ4KyAZRV7np4RhCXGSJHgoCQ", + "iatMode": "0" + } + }, + { + "type":"openvpn", + "protocols":[ + "tcp" + ], + "ports":[ + "443" + ] + } + ], + "user_ips": false + }, + "host": "yal.riseup.net", + "ip_address": "199.58.83.10", + "location": "montreal" + } + ], + "locations": { + "amsterdam": { + "country_code": "NL", + "hemisphere": "N", + "name": "Amsterdam", + "timezone": "+2" + }, + "montreal": { + "country_code": "CA", + "hemisphere": "N", + "name": "Montreal", + "timezone": "-5" + }, + "paris": { + "country_code": "FR", + "hemisphere": "N", + "name": "Paris", + "timezone": "+2" + } + }, + "openvpn_configuration": { + "auth": "SHA1", + "cipher": "AES-128-CBC", + "keepalive": "10 30", + "tls-cipher": "DHE-RSA-AES128-SHA", + "tun-ipv6": true + }, + "serial": 3, + "version": 3 +} diff --git a/app/src/test/resources/v4/riseup_geoip_v1.json b/app/src/test/resources/v4/riseup_geoip_v1.json new file mode 100644 index 00000000..4e3bda2a --- /dev/null +++ b/app/src/test/resources/v4/riseup_geoip_v1.json @@ -0,0 +1,14 @@ +{ + "ip":"51.158.144.32", + "cc":"FR", + "city":"Paris", + "lat":48.8628, + "lon":2.3292, + "gateways":[ + "mouette.riseup.net", + "hoatzin.riseup.net", + "yal.riseup.net", + "redshank.riseup.net", + "zarapito.riseup.net" + ] +}
\ No newline at end of file diff --git a/app/src/test/resources/v4/riseup_geoip_v4.json b/app/src/test/resources/v4/riseup_geoip_v4.json new file mode 100644 index 00000000..c95a9e6d --- /dev/null +++ b/app/src/test/resources/v4/riseup_geoip_v4.json @@ -0,0 +1,41 @@ +{ + "ip":"51.158.144.32", + "cc":"FR", + "city":"Paris", + "lat":48.8628, + "lon":2.3292, + "gateways":[ + "mouette.riseup.net", + "hoatzin.riseup.net", + "yal.riseup.net", + "redshank.riseup.net", + "zarapito.riseup.net" + ], + "sortedGateways": [ + { + "host": "mouette.riseup.net", + "fullness": 0.3, + "overload": false + }, + { + "host": "hoatzin.riseup.net", + "fullness": 0.36, + "overload": false + }, + { + "host": "yal.riseup.net", + "fullness": 0.59, + "overload": false + }, + { + "host": "redshank.riseup.net", + "fullness": 0.8, + "overload": false + }, + { + "host": "zarapito.riseup.net", + "fullness": 0.92, + "overload": true + } + ] +}
\ No newline at end of file |