diff options
6 files changed, 205 insertions, 3 deletions
diff --git a/app/build.gradle b/app/build.gradle index 5b33dff8..2614c6fb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -50,6 +50,7 @@ android { } dependencies { + testCompile 'org.mockito:mockito-core:2.6.3' androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.5.4' testCompile 'junit:junit:4.12' testCompile 'org.json:json:20170516' diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java index 428ac706..73c7337b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -168,6 +168,7 @@ public final class EIP extends IntentService { } private void updateGateways() { + gateways_manager.clearGatewaysAndProfiles(); gateways_manager.fromEipServiceJson(eip_definition); gatewaysToPreferences(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java index f41049c5..6a7e3d0b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -143,15 +143,20 @@ public class GatewaysManager { return result; } + protected void clearGatewaysAndProfiles() { + gateways.clear(); + ArrayList<VpnProfile> profiles = new ArrayList<>(profile_manager.getProfiles()); + for (VpnProfile profile : profiles) { + profile_manager.removeProfile(context, profile); + } + } + private void addGateway(Gateway gateway) { removeDuplicatedGateway(gateway); - gateways.add(gateway); VpnProfile profile = gateway.getProfile(); profile_manager.addProfile(profile); - //profile_manager.saveProfile(context, profile); - //profile_manager.saveProfileList(context); } private void removeDuplicatedGateway(Gateway gateway) { diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java new file mode 100644 index 00000000..e6a67392 --- /dev/null +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java @@ -0,0 +1,84 @@ +package se.leap.bitmaskclient.eip; + +import android.content.Context; +import android.content.SharedPreferences; + +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.mockito.junit.MockitoJUnitRunner; + +import java.io.IOException; + +import se.leap.bitmaskclient.Provider; +import se.leap.bitmaskclient.TestUtils; + +import static junit.framework.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +/** + * Created by cyberta on 09.10.17. + */ +@RunWith(MockitoJUnitRunner.class) +public class GatewaysManagerTest { + + private GatewaysManager gatewaysManager; + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private Context mockContext; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private SharedPreferences sharedPreferences; + + @Before + public void setUp() throws IOException, JSONException { + + + JSONObject secrets = new JSONObject(getJsonStringFor("secrets.json")); + + when(sharedPreferences.getString(eq(Constants.PRIVATE_KEY), anyString())).thenReturn(secrets.getString(Constants.PRIVATE_KEY)); + when(sharedPreferences.getString(eq(Provider.CA_CERT), anyString())).thenReturn(secrets.getString(Provider.CA_CERT)); + when(sharedPreferences.getString(eq(Constants.CERTIFICATE), anyString())).thenReturn(secrets.getString(Constants.CERTIFICATE)); + when(mockContext.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPreferences); + + + gatewaysManager = new GatewaysManager(mockContext, sharedPreferences); + } + + @Test + public void testFromEipServiceJson_emptyJson() throws Exception { + gatewaysManager.fromEipServiceJson(new JSONObject()); + assertEquals(0, gatewaysManager.size()); + } + + + @Test + public void testFromEipServiceJson_ignoreDuplicateGateways() throws Exception { + String eipServiceJson = TestUtils.getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-two-gateways.json")); + gatewaysManager.fromEipServiceJson(new JSONObject(eipServiceJson)); + assertEquals(2, gatewaysManager.size()); + eipServiceJson = TestUtils.getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-one-gateway.json")); + gatewaysManager.fromEipServiceJson(new JSONObject(eipServiceJson)); + assertEquals(2, gatewaysManager.size()); + } + + @Test + public void testClearGatewaysAndProfiles_resetGateways() throws Exception { + String eipServiceJson = TestUtils.getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-two-gateways.json")); + gatewaysManager.fromEipServiceJson(new JSONObject(eipServiceJson)); + assertEquals(2, gatewaysManager.size()); + gatewaysManager.clearGatewaysAndProfiles(); + assertEquals(0, gatewaysManager.size()); + } + + private String getJsonStringFor(String filename) throws IOException { + return TestUtils.getInputAsString(getClass().getClassLoader().getResourceAsStream(filename)); + } + +}
\ No newline at end of file diff --git a/app/src/test/resources/eip-service-one-gateway.json b/app/src/test/resources/eip-service-one-gateway.json new file mode 100644 index 00000000..adf4ba18 --- /dev/null +++ b/app/src/test/resources/eip-service-one-gateway.json @@ -0,0 +1,42 @@ +{ + "gateways": [ + { + "capabilities": { + "adblock": false, + "filter_dns": false, + "limited": false, + "ports": [ + "443" + ], + "protocols": [ + "tcp", + "udp" + ], + "transport": [ + "openvpn" + ], + "user_ips": false + }, + "host": "millipede.demo.bitmask.net", + "ip_address": "198.252.153.84", + "location": "seattle__wa" + } + ], + "locations": { + "seattle__wa": { + "country_code": "US", + "hemisphere": "N", + "name": "Seattle, WA", + "timezone": "-7" + } + }, + "openvpn_configuration": { + "auth": "SHA1", + "cipher": "AES-128-CBC", + "keepalive": "10 30", + "tls-cipher": "DHE-RSA-AES128-SHA", + "tun-ipv6": true + }, + "serial": 1, + "version": 1 +}
\ No newline at end of file diff --git a/app/src/test/resources/eip-service-two-gateways.json b/app/src/test/resources/eip-service-two-gateways.json new file mode 100644 index 00000000..78b49bae --- /dev/null +++ b/app/src/test/resources/eip-service-two-gateways.json @@ -0,0 +1,69 @@ +{ + "gateways": [ + { + "capabilities": { + "adblock": false, + "filter_dns": false, + "limited": false, + "ports": [ + "443" + ], + "protocols": [ + "tcp", + "udp" + ], + "transport": [ + "openvpn" + ], + "user_ips": false + }, + "host": "millipede.demo.bitmask.net", + "ip_address": "198.252.153.84", + "location": "seattle__wa" + }, + { + "capabilities": { + "adblock": false, + "filter_dns": false, + "limited": false, + "ports": [ + "443" + ], + "protocols": [ + "tcp", + "udp" + ], + "transport": [ + "openvpn" + ], + "user_ips": false + }, + "host": "otter.demo.bitmask.net", + "ip_address": "46.165.242.169", + "location": "frankfurt" + } + ], + "locations": { + "frankfurt": { + "country_code": "DE", + "hemisphere": "N", + "name": "Frankfurt", + "timezone": "+1" + }, + "seattle__wa": { + "country_code": "US", + "hemisphere": "N", + "name": "Seattle, WA", + "timezone": "-7" + } + }, + "openvpn_configuration": { + "auth": "SHA1", + "cipher": "AES-128-CBC", + "keepalive": "10 30", + "tls-cipher": "DHE-RSA-AES128-SHA", + "tun-ipv6": true + }, + "serial": 1, + "version": 1 +}
\ No newline at end of file |