summaryrefslogtreecommitdiff
path: root/app/src/test
diff options
context:
space:
mode:
authorcyberta <cyberta@riseup.net>2017-10-13 11:34:42 +0000
committercyberta <cyberta@riseup.net>2017-10-13 11:34:42 +0000
commitef91d9235cccbd4dcd9e120c8fff910e0d448022 (patch)
tree88b00bcb87c116190e45f1045c22952fb0ee4991 /app/src/test
parentef98b18734c25da77abe4f0067635cf795218c65 (diff)
parent97ff21c74619269a8c7dc9d8ff1485200db58d8c (diff)
Merge branch '8758_switch_providers' into 'master'
#8758 fix switching providers See merge request leap/bitmask_android!11
Diffstat (limited to 'app/src/test')
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java84
-rw-r--r--app/src/test/resources/eip-service-one-gateway.json42
-rw-r--r--app/src/test/resources/eip-service-two-gateways.json69
3 files changed, 195 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
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