From dd6e5a29f40f38481dec5545ff4fe1cabd5d6384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Fri, 12 Dec 2014 10:43:54 +0100 Subject: Beginning the tests for EIP. --- .../java/se/leap/bitmaskclient/test/testEIP.java | 31 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'app/src/androidTest') diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java index 4e1819d0..be774ecb 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java @@ -2,20 +2,25 @@ package se.leap.bitmaskclient.test; import android.content.Context; import android.content.Intent; -import android.test.ActivityUnitTestCase; +import android.content.SharedPreferences; import android.test.ServiceTestCase; +import android.test.suitebuilder.annotation.MediumTest; import se.leap.bitmaskclient.Dashboard; +import se.leap.bitmaskclient.eip.Constants; import se.leap.bitmaskclient.eip.EIP; public class testEIP extends ServiceTestCase { private Context context; private Intent intent; - private EIP activity; + private SharedPreferences preferences; public testEIP(Class activityClass) { super(activityClass); + context = getSystemContext(); + intent = new Intent(context, EIP.class); + preferences = context.getSharedPreferences(Dashboard.SHARED_PREFERENCES, Context.MODE_PRIVATE); } @Override @@ -28,5 +33,27 @@ public class testEIP extends ServiceTestCase { super.tearDown(); } + @MediumTest + private void testCheckCertValidity() { + testEmptyCertificate(); + testExpiredCertificate(); + // Wait for the service to start + // Check result is OK. + } + + private void testEmptyCertificate() { + preferences.edit().putString(Constants.CERTIFICATE, ""); + startService(Constants.ACTION_CHECK_CERT_VALIDITY); + } + private void testExpiredCertificate() { + String expired_certificate = "expired certificate"; + preferences.edit().putString(Constants.CERTIFICATE, expired_certificate); + startService(Constants.ACTION_CHECK_CERT_VALIDITY); + } + + private void startService(String action) { + intent.setAction(action); + startService(intent); + } } -- cgit v1.2.3 From a74d09929575e44f86d09283ae4633b0dcfcb566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 6 Jan 2015 10:11:50 +0100 Subject: Extracted GatewaysManager + coded its tests --- app/src/androidTest/assets/eip-service-test.json | 69 +++++++++++ app/src/androidTest/assets/gateway.json | 1 + app/src/androidTest/assets/secrets.json | 1 + .../java/se/leap/bitmaskclient/test/testEIP.java | 23 +++- .../bitmaskclient/test/testGatewaysManager.java | 133 +++++++++++++++++++++ 5 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 app/src/androidTest/assets/eip-service-test.json create mode 100644 app/src/androidTest/assets/gateway.json create mode 100644 app/src/androidTest/assets/secrets.json create mode 100644 app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java (limited to 'app/src/androidTest') diff --git a/app/src/androidTest/assets/eip-service-test.json b/app/src/androidTest/assets/eip-service-test.json new file mode 100644 index 00000000..78b49bae --- /dev/null +++ b/app/src/androidTest/assets/eip-service-test.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 diff --git a/app/src/androidTest/assets/gateway.json b/app/src/androidTest/assets/gateway.json new file mode 100644 index 00000000..51a19ec9 --- /dev/null +++ b/app/src/androidTest/assets/gateway.json @@ -0,0 +1 @@ +{"location":"seattle__wa","ip_address":"198.252.153.84","capabilities":{"limited":false,"ports":["443"],"adblock":false,"transport":["openvpn"],"filter_dns":false,"protocols":["tcp","udp"],"user_ips":false},"host":"millipede.demo.bitmask.net"} diff --git a/app/src/androidTest/assets/secrets.json b/app/src/androidTest/assets/secrets.json new file mode 100644 index 00000000..36ba5977 --- /dev/null +++ b/app/src/androidTest/assets/secrets.json @@ -0,0 +1 @@ +{"ca_cert":"-----BEGIN CERTIFICATE-----\nMIIFbzCCA1egAwIBAgIBATANBgkqhkiG9w0BAQ0FADBKMRgwFgYDVQQDDA9CaXRt\nYXNrIFJvb3QgQ0ExEDAOBgNVBAoMB0JpdG1hc2sxHDAaBgNVBAsME2h0dHBzOi8v\nYml0bWFzay5uZXQwHhcNMTIxMTA2MDAwMDAwWhcNMjIxMTA2MDAwMDAwWjBKMRgw\nFgYDVQQDDA9CaXRtYXNrIFJvb3QgQ0ExEDAOBgNVBAoMB0JpdG1hc2sxHDAaBgNV\nBAsME2h0dHBzOi8vYml0bWFzay5uZXQwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAw\nggIKAoICAQC1eV4YvayaU+maJbWrD4OHo3d7S1BtDlcvkIRS1Fw3iYDjsyDkZxai\ndHp4EUasfNQ+EVtXUvtk6170EmLco6Elg8SJBQ27trE6nielPRPCfX3fQzETRfvB\n7tNvGw4Jn2YKiYoMD79kkjgyZjkJ2r\/bEHUSevmR09BRp86syHZerdNGpXYhcQ84\nCA1+V+603GFIHnrP+uQDdssW93rgDNYu+exT+Wj6STfnUkugyjmPRPjL7wh0tzy+\nznCeLl4xiV3g9sjPnc7r2EQKd5uaTe3j71sDPF92KRk0SSUndRE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cwK\/ZNW7g\ndQPZR4iJNkC4Xu6cBZ6Cnn92swBheEYvLSoNlX0vDZ7aLE3\/jzQqrjzC8NP8sbH5jtbuvgeDXZX3\nAmGRp5j6C6A61ihAPmEVz3ZfN8SSfJ3vl\/\/PAIg6lyz0J+cy4Q7RkwSeuVQ72Hl4M8TEvmmKC3Af\nispy6Y0CgYEAgl1o2lo+ACyk+oVQPaaPqK3d7WOBFp4eR2nXFor\/vsx9igQOlZUgzRDQsR8jo1o9\nefOSBf87igrZGgssys89pWa2dnXnz5PMmzkKr6bw4D9Ez6u6Puc9UZhGw\/8wDYg6fSosdB9utspm\nM698ycef7jBNMDgmhpSvfw5GctoNQ4s=\n-----END RSA PRIVATE KEY-----"} diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java index be774ecb..d9235085 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java @@ -1,3 +1,19 @@ +/** + * Copyright (c) 2013, 2014, 2015 LEAP Encryption Access Project and contributers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package se.leap.bitmaskclient.test; import android.content.Context; @@ -10,6 +26,9 @@ import se.leap.bitmaskclient.Dashboard; import se.leap.bitmaskclient.eip.Constants; import se.leap.bitmaskclient.eip.EIP; +/** + * @author parmegv + */ public class testEIP extends ServiceTestCase { private Context context; @@ -42,13 +61,13 @@ public class testEIP extends ServiceTestCase { } private void testEmptyCertificate() { - preferences.edit().putString(Constants.CERTIFICATE, ""); + preferences.edit().putString(Constants.CERTIFICATE, "").apply(); startService(Constants.ACTION_CHECK_CERT_VALIDITY); } private void testExpiredCertificate() { String expired_certificate = "expired certificate"; - preferences.edit().putString(Constants.CERTIFICATE, expired_certificate); + preferences.edit().putString(Constants.CERTIFICATE, expired_certificate).apply(); startService(Constants.ACTION_CHECK_CERT_VALIDITY); } diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java new file mode 100644 index 00000000..98d4d6bc --- /dev/null +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java @@ -0,0 +1,133 @@ +/** + * Copyright (c) 2013, 2014, 2015 LEAP Encryption Access Project and contributers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package se.leap.bitmaskclient.test; + +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences; +import android.test.InstrumentationTestCase; +import android.test.suitebuilder.annotation.MediumTest; +import android.test.suitebuilder.annotation.SmallTest; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.io.InputStream; + +import se.leap.bitmaskclient.Dashboard; +import se.leap.bitmaskclient.eip.Gateway; +import se.leap.bitmaskclient.eip.GatewaysManager; + +/** + * @author parmegv + */ +public class testGatewaysManager extends InstrumentationTestCase { + + GatewaysManager gateways_manager; + Gateway gateway; + JSONObject eip_definition; + + Context context; + SharedPreferences preferences; + + @Override + protected void setUp() throws Exception { + mockGatewaysManager(); + mockRealGateway(); + super.setUp(); + } + + @MediumTest + public void testFromEipServiceJson() { + gateways_manager.fromEipServiceJson(eip_definition); + assertEquals(2, gateways_manager.size()); + gateways_manager.addFromString(gateway.toString()); + assertEquals(2, gateways_manager.size()); + } + + @SmallTest + public void testAddFromString() { + gateways_manager.addFromString(""); + gateways_manager.addFromString(gateway.toString()); + } + + @MediumTest + public void testRemoveDuplicate() { + gateways_manager.addFromString(gateway.toString()); + assertEquals(1, gateways_manager.size()); + + mockArtificialGateway(); + gateways_manager.addFromString(gateway.toString()); + assertEquals(1, gateways_manager.size()); + } + + @MediumTest + public void testToString() { + assertEquals("[]", gateways_manager.toString()); + + gateways_manager.addFromString(gateway.toString()); + assertEquals("["+gateway.toString()+"]", gateways_manager.toString()); + } + + @SmallTest + public void testIsEmpty() { + assertTrue(gateways_manager.isEmpty()); + gateways_manager.addFromString(""); + assertTrue(gateways_manager.isEmpty()); + gateways_manager.addFromString(gateway.toString()); + assertFalse(gateways_manager.isEmpty()); + } + + private void mockGatewaysManager() { + context = getInstrumentation().getContext(); + preferences = context.getSharedPreferences(Dashboard.SHARED_PREFERENCES, Activity.MODE_PRIVATE); + gateways_manager = new GatewaysManager(context, preferences); + } + + private void mockRealGateway() { + try { + eip_definition = new JSONObject(fromAssets("eip-service-test.json")); + JSONObject secrets = new JSONObject(fromAssets("secrets.json")); + JSONObject gateway = new JSONObject(fromAssets("gateway.json")); + this.gateway = new Gateway(eip_definition, secrets, gateway); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void mockArtificialGateway() { + try { + eip_definition = new JSONObject(fromAssets("eip-service-test.json")); + JSONObject secrets = new JSONObject(fromAssets("secrets.json").replace("6u6", "7u7")); + JSONObject gateway = new JSONObject(fromAssets("gateway.json")); + this.gateway = new Gateway(eip_definition, secrets, gateway); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private String fromAssets(String filename) throws IOException, JSONException { + String result = ""; + InputStream is = context.getAssets().open(filename); + byte[] bytes = new byte[is.available()]; + if(is.read(bytes) > 0) { + result = new String(bytes); + } + return result; + } +} -- cgit v1.2.3 From e1f028e6a27f209ed1f1773c234faf85085d87ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Wed, 7 Jan 2015 16:44:11 +0100 Subject: Tests for valid certificate. --- .../se/leap/bitmaskclient/test/FromAssets.java | 26 ++++++++ .../se/leap/bitmaskclient/test/TestConstants.java | 26 ++++++++ .../bitmaskclient/test/testGatewaysManager.java | 32 ++++----- .../test/testVpnCertificateValidator.java | 77 ++++++++++++++++++++++ 4 files changed, 141 insertions(+), 20 deletions(-) create mode 100644 app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java create mode 100644 app/src/androidTest/java/se/leap/bitmaskclient/test/TestConstants.java create mode 100644 app/src/androidTest/java/se/leap/bitmaskclient/test/testVpnCertificateValidator.java (limited to 'app/src/androidTest') diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java new file mode 100644 index 00000000..4f771922 --- /dev/null +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java @@ -0,0 +1,26 @@ +package se.leap.bitmaskclient.test; + +import android.content.Context; + +import org.json.JSONException; + +import java.io.IOException; +import java.io.InputStream; + +public class FromAssets { + + Context context; + + public FromAssets(Context context) { + this.context = context; + } + public String toString(String filename) throws IOException, JSONException { + String result = ""; + InputStream is = context.getAssets().open(filename); + byte[] bytes = new byte[is.available()]; + if(is.read(bytes) > 0) { + result = new String(bytes); + } + return result; + } +} diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/TestConstants.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/TestConstants.java new file mode 100644 index 00000000..6b4cdfb1 --- /dev/null +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/TestConstants.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2013, 2014, 2015 LEAP Encryption Access Project and contributers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package se.leap.bitmaskclient.test; + +/** + * @author parmegv + */ +public class TestConstants { + public final static String EIP_DEFINITION_FILE = "eip-service-test.json"; + public final static String SECRETS_FILE = "secrets.json"; + public final static String GATEWAY_FILE = "gateway.json"; +} diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java index 98d4d6bc..c4303251 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java @@ -23,11 +23,9 @@ import android.test.InstrumentationTestCase; import android.test.suitebuilder.annotation.MediumTest; import android.test.suitebuilder.annotation.SmallTest; -import org.json.JSONException; -import org.json.JSONObject; +import junit.framework.Test; -import java.io.IOException; -import java.io.InputStream; +import org.json.JSONObject; import se.leap.bitmaskclient.Dashboard; import se.leap.bitmaskclient.eip.Gateway; @@ -42,11 +40,15 @@ public class testGatewaysManager extends InstrumentationTestCase { Gateway gateway; JSONObject eip_definition; + FromAssets assets; + Context context; SharedPreferences preferences; @Override protected void setUp() throws Exception { + context = getInstrumentation().getContext(); + assets = new FromAssets(context); mockGatewaysManager(); mockRealGateway(); super.setUp(); @@ -101,9 +103,9 @@ public class testGatewaysManager extends InstrumentationTestCase { private void mockRealGateway() { try { - eip_definition = new JSONObject(fromAssets("eip-service-test.json")); - JSONObject secrets = new JSONObject(fromAssets("secrets.json")); - JSONObject gateway = new JSONObject(fromAssets("gateway.json")); + eip_definition = new JSONObject(assets.toString(TestConstants.EIP_DEFINITION_FILE)); + JSONObject secrets = new JSONObject(assets.toString(TestConstants.SECRETS_FILE)); + JSONObject gateway = new JSONObject(assets.toString(TestConstants.GATEWAY_FILE)); this.gateway = new Gateway(eip_definition, secrets, gateway); } catch (Exception e) { e.printStackTrace(); @@ -112,22 +114,12 @@ public class testGatewaysManager extends InstrumentationTestCase { private void mockArtificialGateway() { try { - eip_definition = new JSONObject(fromAssets("eip-service-test.json")); - JSONObject secrets = new JSONObject(fromAssets("secrets.json").replace("6u6", "7u7")); - JSONObject gateway = new JSONObject(fromAssets("gateway.json")); + eip_definition = new JSONObject(assets.toString(TestConstants.EIP_DEFINITION_FILE)); + JSONObject secrets = new JSONObject(assets.toString(TestConstants.SECRETS_FILE).replace("6u6", "7u7")); + JSONObject gateway = new JSONObject(assets.toString(TestConstants.GATEWAY_FILE)); this.gateway = new Gateway(eip_definition, secrets, gateway); } catch (Exception e) { e.printStackTrace(); } } - - private String fromAssets(String filename) throws IOException, JSONException { - String result = ""; - InputStream is = context.getAssets().open(filename); - byte[] bytes = new byte[is.available()]; - if(is.read(bytes) > 0) { - result = new String(bytes); - } - return result; - } } diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testVpnCertificateValidator.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testVpnCertificateValidator.java new file mode 100644 index 00000000..87154956 --- /dev/null +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testVpnCertificateValidator.java @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2013, 2014, 2015 LEAP Encryption Access Project and contributers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package se.leap.bitmaskclient.test; + +import android.content.Context; +import android.os.SystemClock; +import android.test.InstrumentationTestCase; + +import org.json.JSONObject; + +import java.io.IOException; +import java.util.Calendar; + +import se.leap.bitmaskclient.Provider; +import se.leap.bitmaskclient.eip.VpnCertificateValidator; + +/** + * @author parmegv + */ +public class testVpnCertificateValidator extends InstrumentationTestCase { + + String certificate_valid_from_jan2015_to_nov2022 = ""; + + Context context; + FromAssets assets; + + @Override + protected void setUp() throws Exception { + context = getInstrumentation().getContext(); + assets = new FromAssets(context); + JSONObject secrets = new JSONObject(assets.toString(TestConstants.SECRETS_FILE)); + certificate_valid_from_jan2015_to_nov2022 = secrets.getString(Provider.CA_CERT); + super.setUp(); + } + + public void testIsValid() { + VpnCertificateValidator validator = new VpnCertificateValidator(certificate_valid_from_jan2015_to_nov2022); + setTime(2015, 1, 6); + assertTrue(validator.isValid()); + setTime(2020, 1, 6); + assertFalse(validator.isValid()); + } + + private void setTime(int year, int month, int day) { + shellCommand("adb shell chmod 666 /dev/alarm"); + Calendar c = Calendar.getInstance(); + c.set(year, month, day, 12, 00, 00); + SystemClock.setCurrentTimeMillis(c.getTimeInMillis()); + shellCommand("adb shell chmod 664 /dev/alarm"); + } + + private int shellCommand(String command) { + int result = -1; + try { + result = Runtime.getRuntime().exec(command).waitFor(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return result; + } +} -- cgit v1.2.3 From 250b56762857f2e52626236d3716459786ce126d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 15 Jan 2015 15:24:52 +0100 Subject: More tests, sometimes they don't pass sometimes do testIsValid depends on the speed of Runtime's exec: sometimes it's quick and passes (because it changes the date correctly) and sometimes is not. A waitFor doesn't fix the issue, it waits forever. --- .../leap/bitmaskclient/test/ConnectionManager.java | 6 +- .../test/testConfigurationWizard.java | 106 +++++++---- .../test/testDashboardIntegration.java | 194 +++++++++++---------- .../test/testVpnCertificateValidator.java | 22 ++- 4 files changed, 184 insertions(+), 144 deletions(-) (limited to 'app/src/androidTest') diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java index f1cbff19..e67dd820 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java @@ -17,12 +17,10 @@ public class ConnectionManager { method.setAccessible(true); try { method.invoke(conman, enabled); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + } catch (InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } - } + } } } diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java index 78ce1c81..755f83a7 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java @@ -1,14 +1,10 @@ package se.leap.bitmaskclient.test; -import android.test.ActivityInstrumentationTestCase2; -import android.widget.ListView; -import com.robotium.solo.Solo; -import java.io.IOException; -import se.leap.bitmaskclient.AboutActivity; -import se.leap.bitmaskclient.ConfigurationWizard; -import se.leap.bitmaskclient.ProviderDetailFragment; -import se.leap.bitmaskclient.R; -import se.leap.bitmaskclient.test.ConnectionManager; +import android.test.*; +import android.widget.*; +import com.robotium.solo.*; +import java.io.*; +import se.leap.bitmaskclient.*; public class testConfigurationWizard extends ActivityInstrumentationTestCase2 { @@ -19,10 +15,15 @@ public class testConfigurationWizard extends ActivityInstrumentationTestCase2 { private Solo solo; - + private Context context; + public testDashboardIntegration() { super(Dashboard.class); } @@ -30,8 +21,12 @@ public class testDashboardIntegration extends ActivityInstrumentationTestCase2