diff options
author | Parménides GV <parmegv@sdf.org> | 2015-01-19 16:00:14 +0100 |
---|---|---|
committer | Parménides GV <parmegv@sdf.org> | 2015-01-19 16:00:14 +0100 |
commit | dbc8bc453d71a6c485a40f021fb66436c8e63616 (patch) | |
tree | 12aaee0692ed2ecd0aa213d379617bbe4fa98f48 /app/src/androidTest/java/se | |
parent | 05ad5f226aa7a1504c756b0d77ab03e6add9fea9 (diff) | |
parent | 250b56762857f2e52626236d3716459786ce126d (diff) |
Merge branch 'bug/Add-unit-tests-#6470' into develop
Diffstat (limited to 'app/src/androidTest/java/se')
8 files changed, 474 insertions, 134 deletions
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/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/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<ConfigurationWizard> { @@ -19,10 +15,15 @@ public class testConfigurationWizard extends ActivityInstrumentationTestCase2<Co super(ConfigurationWizard.class); } + public testConfigurationWizard(Solo solo) { + super(ConfigurationWizard.class); + this.solo = solo; + } + @Override protected void setUp() throws Exception { super.setUp(); - solo = new Solo(getInstrumentation(), getActivity()); + solo = new Solo(getInstrumentation(), getActivity()); ConnectionManager.setMobileDataEnabled(true, solo.getCurrentActivity().getApplicationContext()); } @@ -31,35 +32,76 @@ public class testConfigurationWizard extends ActivityInstrumentationTestCase2<Co solo.finishOpenedActivities(); } - public void testListProviders() throws IOException { + public void testListProviders() { assertEquals(solo.getCurrentViews(ListView.class).size(), 1); - - int number_of_available_providers = solo.getCurrentViews(ListView.class).get(0).getCount(); - - assertEquals("Number of available providers differ", solo.getCurrentActivity().getAssets().list("urls").length + added_providers, number_of_available_providers); + + assertEquals("Number of available providers differ", predefinedProviders() + added_providers, shownProviders()); } + + private int shownProviders() { + return solo.getCurrentViews(ListView.class).get(0).getCount(); + } + + private int predefinedProviders() { + int predefined_providers = 0; + try { + predefined_providers = solo.getCurrentActivity().getAssets().list("urls").length; + } catch (IOException e) { + e.printStackTrace(); + return predefined_providers; + } + + return predefined_providers; + } public void testSelectProvider() { - solo.clickOnText("bitmask"); - assertTrue("Provider details dialog did not appear", solo.waitForFragmentByTag(ProviderDetailFragment.TAG, 60*1000)); + selectProvider("demo.bitmask.net"); } - - public void testAddNewProvider() { - solo.clickOnActionBarItem(R.id.new_provider); - solo.enterText(0, "calyx.net"); - solo.clickOnCheckBox(0); - solo.clickOnText(solo.getString(R.string.save)); - //added_providers = added_providers+1; - assertTrue("Provider details dialog did not appear", solo.waitForFragmentByTag(ProviderDetailFragment.TAG, 60*1000)); - solo.goBack(); + + private void selectProvider(String provider) { + solo.clickOnText(provider); + waitForProviderDetails(); + } + + private void waitForProviderDetails() { + String text = solo.getString(R.string.provider_details_fragment_title); + assertTrue("Provider details dialog did not appear", solo.waitForText(text)); + } + + public void testAddNewProvider() { + addProvider("calyx.net"); } + + private void addProvider(String url) { + boolean is_new_provider = !solo.searchText(url); + if(is_new_provider) + added_providers = added_providers+1; + solo.clickOnActionBarItem(R.id.new_provider); + solo.enterText(0, url); + solo.clickOnCheckBox(0); + solo.clickOnText(solo.getString(R.string.save)); + waitForProviderDetails(); + solo.goBack(); + } public void testShowAbout() { - solo.clickOnMenuItem(solo.getString(R.string.about)); - assertTrue("Provider details dialog did not appear", solo.waitForActivity(AboutActivity.class)); - } - - public void testShowSettings() { - //TODO We still don't have the settings button + showAbout(); } + + private void showAbout() { + String text = solo.getString(R.string.about); + solo.clickOnMenuItem(text); + assertTrue("Provider details dialog did not appear", solo.waitForActivity(AboutActivity.class)); + } + + protected void toDashboard(String provider) { + selectProvider(provider); + useAnonymously(); + } + + private void useAnonymously() { + String text = solo.getString(R.string.use_anonymously_button); + solo.clickOnText(text); + solo.waitForText(solo.getString(R.string.title_activity_dashboard)); + } } diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java index 0c7db284..91b93d42 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java @@ -1,28 +1,19 @@ package se.leap.bitmaskclient.test; -import android.content.BroadcastReceiver; import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.provider.Settings; -import android.test.ActivityInstrumentationTestCase2; -import android.util.Log; -import com.robotium.solo.Solo; +import android.test.*; +import com.robotium.solo.*; import java.io.IOException; -import java.io.InputStream; -import java.util.Scanner; -import de.blinkt.openvpn.activities.DisconnectVPN; -import se.leap.bitmaskclient.ConfigurationWizard; -import se.leap.bitmaskclient.Dashboard; -import se.leap.bitmaskclient.R; -import se.leap.bitmaskclient.test.ConnectionManager; +import de.blinkt.openvpn.activities.*; +import se.leap.bitmaskclient.*; public class testDashboardIntegration extends ActivityInstrumentationTestCase2<Dashboard> { private Solo solo; - + private Context context; + public testDashboardIntegration() { super(Dashboard.class); } @@ -30,8 +21,12 @@ public class testDashboardIntegration extends ActivityInstrumentationTestCase2<D @Override protected void setUp() throws Exception { super.setUp(); + context = getInstrumentation().getContext(); solo = new Solo(getInstrumentation(), getActivity()); - ConnectionManager.setMobileDataEnabled(true, solo.getCurrentActivity().getApplicationContext()); + ConnectionManager.setMobileDataEnabled(true, context); + solo.unlockScreen(); + if(solo.searchText(solo.getString(R.string.configuration_wizard_title))) + new testConfigurationWizard(solo).toDashboard("demo.bitmask.net"); } @Override @@ -45,121 +40,128 @@ public class testDashboardIntegration extends ActivityInstrumentationTestCase2<D */ public void testOnOffOpenVpn() { solo.clickOnView(solo.getView(R.id.eipSwitch)); - testEipTurningOn(); + turningEipOn(); solo.clickOnView(solo.getView(R.id.eipSwitch)); - testEipTurningOff(); + turningEipOff(); solo.clickOnView(solo.getView(R.id.eipSwitch)); - testEipTurningOn(); + turningEipOn(); solo.clickOnView(solo.getView(R.id.eipSwitch)); - testEipTurningOff(); - - solo.clickOnView(solo.getView(R.id.eipSwitch)); - testEipTurningOn(); - - solo.clickOnView(solo.getView(R.id.eipSwitch)); - testEipTurningOff(); + turningEipOff(); - solo.clickOnView(solo.getView(R.id.eipSwitch)); - testEipTurningOn(); + /*solo.clickOnView(solo.getView(R.id.eipSwitch)); + turningEipOn(); - testEipIsOnNoNetwork(); + turnNetworkOff(); + restartAdbServer(); // This doesn't work + */ } - private void testEipTurningOn() { - if(!solo.waitForText(getActivity().getString(R.string.state_auth))) - fail(); - if(!solo.waitForText(getActivity().getString(R.string.eip_state_connected), 1, 30*1000)) - fail(); - solo.sleep(2*1000); + private void turningEipOn() { + assertAuthenticating(); + int max_seconds_until_connected = 30; + assertConnected(max_seconds_until_connected); + solo.sleep(2*1000); + } + + private void assertAuthenticating() { + String message = solo.getString(R.string.state_auth); + assertTrue(solo.waitForText(message)); + } + + private void assertConnected(int max_seconds_until_connected) { + String message = solo.getString(R.string.eip_state_connected); + assertTrue(solo.waitForText(message, 1, max_seconds_until_connected * 1000)); } - private void testEipTurningOff() { + private void turningEipOff() { sayOkToDisconnect(); - if(!solo.waitForText(getActivity().getString(R.string.eip_state_not_connected))) - fail(); + assertDisconnected(); solo.sleep(2*1000); } private void sayOkToDisconnect() { - if(!solo.waitForActivity(DisconnectVPN.class)) - fail(); - solo.clickOnText(getActivity().getString(android.R.string.yes)); + assertTrue(solo.waitForActivity(DisconnectVPN.class)); + String yes = solo.getString(android.R.string.yes); + solo.clickOnText(yes); + } + + private void assertDisconnected() { + String message = solo.getString(R.string.eip_state_not_connected); + assertTrue(solo.waitForText(message)); } - private void testEipIsOnNoNetwork() { - ConnectionManager.setMobileDataEnabled(false, solo.getCurrentActivity().getApplicationContext()); + private void turnNetworkOff() { + ConnectionManager.setMobileDataEnabled(false, context); if(!solo.waitForText(getActivity().getString(R.string.eip_state_not_connected), 1, 15*1000)) fail(); } + + private void restartAdbServer() { + runAdbCommand("kill-server"); + runAdbCommand("start-server"); + } public void testLogInAndOut() { - long miliseconds_to_log_in = 40 * 1000; - solo.clickOnActionBarItem(R.id.login_button); - solo.enterText(0, "parmegvtest1"); - solo.enterText(1, " S_Zw3'-"); - solo.clickOnText("Log In"); - solo.waitForDialogToClose(); - solo.waitForDialogToClose(miliseconds_to_log_in); - if(!solo.waitForText(getActivity().getString(R.string.succesful_authentication_message))) - fail(); - - solo.clickOnActionBarItem(R.string.logout_button); - if(!solo.waitForDialogToClose()) - fail(); + long milliseconds_to_log_in = 40 * 1000; + logIn("parmegvtest1", " S_Zw3'-"); + solo.waitForDialogToClose(milliseconds_to_log_in); + assertSuccessfulLogin(); + + logOut(); } - - public void testShowAbout() { - solo.clickOnMenuItem(getActivity().getString(R.string.about)); - solo.waitForText(getActivity().getString(R.string.repository_url_text)); + + private void logIn(String username, String password) { + solo.clickOnActionBarItem(R.id.login_button); + solo.enterText(0, username); + solo.enterText(1, password); + solo.clickOnText("Log In"); + solo.waitForDialogToClose(); + } + + private void assertSuccessfulLogin() { + String message = solo.getString(R.string.succesful_authentication_message); + assertTrue(solo.waitForText(message)); + } + + private void logOut() { + solo.clickOnActionBarItem(R.string.logout_button); + assertTrue(solo.waitForDialogToClose()); + } + + public void testShowAbout() { + showAbout(); solo.goBack(); - - solo.clickOnMenuItem(getActivity().getString(R.string.about)); - solo.waitForText(getActivity().getString(R.string.repository_url_text)); + showAbout(); solo.goBack(); } - - public void testSwitchProvider() { - solo.clickOnMenuItem(getActivity().getString(R.string.switch_provider_menu_option)); + + private void showAbout() { + String menu_item = solo.getString(R.string.about); + solo.clickOnMenuItem(menu_item); + + String text_unique_to_about = solo.getString(R.string.repository_url_text); + solo.waitForText(text_unique_to_about); + } + + public void testSwitchProvider() { + solo.clickOnMenuItem(solo.getString(R.string.switch_provider_menu_option)); solo.waitForActivity(ConfigurationWizard.class); solo.goBack(); } - public void testUpdateExpiredCertificate() { - String certificate = "-----BEGIN CERTIFICATE-----" + - "MIIEnDCCAoSgAwIBAgIRAOBkcbMKR0Jlw+xNalHn7aIwDQYJKoZIhvcNAQELBQAwdTEYMBYGA1UE" + - "CgwPUmlzZXVwIE5ldHdvcmtzMRswGQYDVQQLDBJodHRwczovL3Jpc2V1cC5uZXQxPDA6BgNVBAMM" + - "M1Jpc2V1cCBOZXR3b3JrcyBSb290IENBIChjbGllbnQgY2VydGlmaWNhdGVzIG9ubHkhKTAeFw0x" + - "NDA5MTkwMDAwMDBaFw0xNDExMTkwMDAwMDBaMC0xKzApBgNVBAMMIlVOTElNSVRFRDcwZWhxZG9l" + - "ZXQ2Z243bmc3eWx3ZWNxeGwwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDdaKQHSwg2" + - "Q2Uz9t5mae9BfV9Jkk+WSU6jXixsTbtLAr8gvuNcVuI0lKm2zXVqoS8aRCSsCt12vhjU/WBTSv0t" + - "vwTaT2HQYFQ1GlVUBKssJEUpaVyQKL6LN9BA5ZODBpbhefRIX8z+02afxmNWdnOQfDtLU6nHSQLL" + - "IUBSmgu+Y2Q3SdIBojIl9Kj0Zt6uZkhtOXZqkwLBiMr+/ukSidpcmNgbAN0eXSfVouaduzsDPQ6M" + - "eCJTz2lhUvC0/57h5mlkNLzEjyb/pAVTtnK4zdiH6XAuCxU/AkF0yzhaiQWMG0RQb4vEx/UHjkDU" + - "+K0GDy/qx1BmBB7C4vHLauqSXOs1AgMBAAGjbzBtMB0GA1UdDgQWBBQioBn7DdhjmtBKgQKpx/aW" + - "XHYkGjALBgNVHQ8EBAMCB4AwEwYDVR0lBAwwCgYIKwYBBQUHAwIwCQYDVR0TBAIwADAfBgNVHSME" + - "GDAWgBQX9BvV5SoBAU1rol02CikJlmWARjANBgkqhkiG9w0BAQsFAAOCAgEAV7q102FQ62IOX84o" + - "pPvUL3hJkGtZ5chgQwZhfl2fGtEdeqpU27Hx1jLP9o3n1z9XYaZg/d8xYhpY6Mm4rFl6hA4gk81Z" + - "yg/A3QeUgIjOsA0Xp+RNB5ACaLjCPUtWNk5brfuelDdFHjl1noC2P3vQ9ErhUna6TKVsxxrueimO" + - "nc3sV7YMGiVfPC7wEmhERuyhQxftIUHUy2kDCY5QgXtru6IZmc3SP4FcM8LUSC49kqmU9if2GTLo" + - "wQZmz6T7+N5PIJWIOiDh9PyoojRo7ep9szeIZpzgxcsoE/9ed84tg36JLOWi0GOyrdzVExv0rQQt" + - "q/NpqAe1mX5XQVbY8nwgaJ8eWIWIXIn+5RB7b+fm5ZFeM4eFyWeDk99bvS8jdH6uQP5WusL55+ft" + - "ADtESsmBvzUEGqxk5GL4lmmeqE+vsR5TesqGjZ+yH67rR+1+Uy2mhbqJBP0E0LHwWCCPYEVfngHj" + - "aZkDF1UVQdfc9Amc5u5J5YliWrEG80BNeJF7740Gwx69DHEIhElN+BBeeqLLYIZTKmt28/9iWbKL" + - "vhCrz/29wLYksL1bXmyHzvzyAcDHPpO9sQrKYiP1mGRDmXJmZU3i3cgeqQFZ8+lr55wcYdMGJOcx" + - "bz+jL0VkHdnoZdzGzelrAhZtgMtsJ/kgWYRgtFmhpYF1Xtj2MYrpBDxgQck=" + - "-----END CERTIFICATE-----"; - - } + /*public void testReboot() { + runAdbCommand("shell am broadcast -a android.intent.action.BOOT_COMPLETED"); + }*/ - public void testReboot() { + private void runAdbCommand(String adb_command) { try { - String command = "adb shell am broadcast -a android.intent.action.BOOT_COMPLETED"; - Runtime.getRuntime().exec(command); - } catch (IOException e) { + String command = "adb " + adb_command; + Runtime.getRuntime().exec(command).waitFor(); + } catch (IOException | InterruptedException e) { e.printStackTrace(); } } 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..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,21 +1,45 @@ +/** + * 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.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; +/** + * @author parmegv + */ public class testEIP extends ServiceTestCase<EIP> { private Context context; private Intent intent; - private EIP activity; + private SharedPreferences preferences; public testEIP(Class<EIP> activityClass) { super(activityClass); + context = getSystemContext(); + intent = new Intent(context, EIP.class); + preferences = context.getSharedPreferences(Dashboard.SHARED_PREFERENCES, Context.MODE_PRIVATE); } @Override @@ -28,5 +52,27 @@ public class testEIP extends ServiceTestCase<EIP> { 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, "").apply(); + startService(Constants.ACTION_CHECK_CERT_VALIDITY); + } + private void testExpiredCertificate() { + String expired_certificate = "expired certificate"; + preferences.edit().putString(Constants.CERTIFICATE, expired_certificate).apply(); + startService(Constants.ACTION_CHECK_CERT_VALIDITY); + } + + private void startService(String action) { + intent.setAction(action); + startService(intent); + } } 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..c4303251 --- /dev/null +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java @@ -0,0 +1,125 @@ +/** + * 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.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 junit.framework.Test; + +import org.json.JSONObject; + +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; + + FromAssets assets; + + Context context; + SharedPreferences preferences; + + @Override + protected void setUp() throws Exception { + context = getInstrumentation().getContext(); + assets = new FromAssets(context); + 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(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(); + } + } + + private void mockArtificialGateway() { + try { + 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(); + } + } +} 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..cd1d6c3b --- /dev/null +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testVpnCertificateValidator.java @@ -0,0 +1,75 @@ +/** + * 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.*; +import android.os.*; +import android.test.*; + +import org.json.*; + +import java.io.*; +import java.util.*; + +import se.leap.bitmaskclient.*; +import se.leap.bitmaskclient.eip.*; + +/** + * @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 = 0; + try { + Runtime.getRuntime().exec(command); + } catch (IOException e) { + e.printStackTrace(); + } + return result; + } +} |