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. --- README.md | 2 +- .../se/leap/bitmaskclient/test/FromAssets.java | 26 ++++++++ .../se/leap/bitmaskclient/test/TestConstants.java | 26 ++++++++ .../bitmaskclient/test/testGatewaysManager.java | 32 ++++----- .../test/testVpnCertificateValidator.java | 77 ++++++++++++++++++++++ 5 files changed, 142 insertions(+), 21 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 diff --git a/README.md b/README.md index 37ef3bde..f1921e10 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ The resulting apk(s) will be in `app/build/apk`. ## Running tests To run the automated tests: - 1. Run an emulator + 1. Run an emulator (device doesn't necesarily has root, so testVpnCertificateValidator.testIsValid may fail). 2. Unlock Android 3. Issue the command ./gradlew connectedCheck 4. Pay attention and check the "Trust this app" checkbox, if you don't do so tests won't run. 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