diff options
author | Parménides GV <parmegv@sdf.org> | 2015-01-07 16:44:11 +0100 |
---|---|---|
committer | Parménides GV <parmegv@sdf.org> | 2015-01-19 15:57:44 +0100 |
commit | e1f028e6a27f209ed1f1773c234faf85085d87ab (patch) | |
tree | 278cbddf45d6a1da9a875a4580eb02406fac840a /app/src/androidTest/java | |
parent | a74d09929575e44f86d09283ae4633b0dcfcb566 (diff) |
Tests for valid certificate.
Diffstat (limited to 'app/src/androidTest/java')
4 files changed, 141 insertions, 20 deletions
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 <http://www.gnu.org/licenses/>. + */ +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 <http://www.gnu.org/licenses/>. + */ +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; + } +} |