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 --- .../java/se/leap/bitmaskclient/test/testEIP.java | 23 +++- .../bitmaskclient/test/testGatewaysManager.java | 133 +++++++++++++++++++++ 2 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java (limited to 'app/src/androidTest/java/se/leap') 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