From a22a22d8fffb312b0d6dfdf545ff6fe6f05dbcee Mon Sep 17 00:00:00 2001 From: cyBerta Date: Mon, 29 Jan 2018 16:10:15 +0100 Subject: #8818 test NavigationDrawer with espresso test and update sdk therefore --- app/build.gradle | 52 ++- app/src/androidTest/assets/riseup.net.json | 37 ++ app/src/androidTest/assets/riseup.net.pem | 32 ++ .../test/StartActivityDrawerTest.java | 127 ++++++ app/src/main/res/layout-sw600dp-port/f_log.xml | 29 +- app/src/main/res/layout-sw600dp/f_log.xml | 6 +- app/src/main/res/layout-xlarge/about.xml | 2 +- .../res/layout-xlarge/eip_service_fragment.xml | 4 +- app/src/main/res/layout/about.xml | 2 +- app/src/main/res/layout/drawer_main.xml | 5 - app/src/main/res/layout/eip_service_fragment.xml | 5 +- app/src/main/res/layout/f_log.xml | 4 +- .../testutils/TestSetupHelper.java | 43 ++ .../se/leap/bitmaskclient/ConfigHelperTest.java | 4 +- .../bitmaskclient/eip/ProviderApiManagerTest.java | 16 +- .../leap/bitmaskclient/testutils/MockHelper.java | 398 +++++++++++++++++++ .../bitmaskclient/testutils/TestSetupHelper.java | 434 --------------------- docker/android-emulator/Dockerfile | 3 +- docker/android-sdk/Dockerfile | 7 +- 19 files changed, 728 insertions(+), 482 deletions(-) create mode 100644 app/src/androidTest/assets/riseup.net.json create mode 100644 app/src/androidTest/assets/riseup.net.pem create mode 100644 app/src/androidTest/java/se/leap/bitmaskclient/test/StartActivityDrawerTest.java create mode 100644 app/src/sharedTest/java/se.leap.bitmaskclient/testutils/TestSetupHelper.java create mode 100644 app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java delete mode 100644 app/src/test/java/se/leap/bitmaskclient/testutils/TestSetupHelper.java diff --git a/app/build.gradle b/app/build.gradle index 2e6604f2..6e05a6d3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,13 +4,18 @@ import java.util.regex.Pattern apply plugin: 'com.android.application' android { - compileSdkVersion 26 - buildToolsVersion '26.0.0' + compileSdkVersion 27 + buildToolsVersion '27.0.3' defaultConfig { applicationId "se.leap.bitmaskclient" resValue "string", "app_name", "Bitmask" vectorDrawables.useSupportLibrary = true + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + dexOptions { + jumboMode true + multiDexEnabled true + } } signingConfigs { @@ -42,6 +47,9 @@ android { applicationIdSuffix ".beta" resValue "string", "app_name", "Bitmask Beta" } + debug { + testCoverageEnabled = true + } } lintOptions { @@ -57,23 +65,32 @@ android { debug { assets.srcDirs = ['src/debug/assets'] } + test { resources.srcDirs += ['src/test/resources'] + java.srcDirs += ['src/sharedTest/java'] + } + + androidTest { + java.srcDirs += ['src/sharedTest/java'] } } } dependencies { testCompile 'junit:junit:4.12' - testCompile 'org.mockito:mockito-core:2.8.0' - testCompile 'org.powermock:powermock-api-mockito2:1.7.3' + testCompile 'org.mockito:mockito-core:2.8.9' + testCompile('org.powermock:powermock-api-mockito2:1.7.3') + { exclude group: 'junit' exclude group: 'org.mockito' } testCompile 'org.powermock:powermock-module-junit4:1.7.3' testCompile 'org.powermock:powermock-core:1.7.3' testCompile 'org.powermock:powermock-module-junit4-rule:1.7.3' - //testCompile 'com.madgag.spongycastle:bctls-jdk15on:1.58.0.0' - //testCompile 'com.madgag.spongycastle:bcpkix-jdk15on:1.58.0.0' - //testCompile 'com.madgag.spongycastle:bcpg-jdk15on:1.58.0.0' + androidTestCompile 'org.mockito:mockito-core:2.8.9' + androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1' + androidTestCompile 'com.android.support.test.espresso:espresso-contrib:3.0.1' + androidTestCompile 'com.android.support.test.espresso:espresso-intents:3.0.1' + //TODO: remove that library androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.6.3' testCompile 'junit:junit:4.12' testCompile 'org.json:json:20170516' @@ -81,19 +98,22 @@ dependencies { releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' betaCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' compile 'com.jakewharton:butterknife:6.1.0' + //TODO: replace that library provided 'com.squareup.dagger:dagger-compiler:1.2.2' + //TODO: remove that library? compile 'com.github.pedrovgs:renderers:1.5' compile 'com.intellij:annotations:12.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.squareup.okhttp3:okhttp:3.9.0' + //TODO: remove that library compile 'mbanje.kurt:fabbutton:1.1.4' - compile "com.android.support:support-core-utils:26.1.0" + compile "com.android.support:support-core-utils:27.0.2" compile 'com.android.support:support-annotations:27.0.2' - compile 'com.android.support:support-v4:26.1.0' - compile 'com.android.support:appcompat-v7:26.1.0' - compile 'com.android.support:design:26.1.0' - compile 'com.android.support:support-fragment:26.1.0' - compile 'com.android.support.constraint:constraint-layout:1.0.2' + compile 'com.android.support:support-v4:27.0.2' + compile 'com.android.support:appcompat-v7:27.0.2' + compile 'com.android.support:design:27.0.2' + compile 'com.android.support:support-fragment:27.0.2' + compile 'com.android.support.constraint:constraint-layout:1.0.2' } // Ensure the no-op dependency is always used in JVM tests. @@ -105,6 +125,12 @@ configurations.all { config -> } } } + resolutionStrategy.force "com.android.support:support-annotations:27.0.2" + resolutionStrategy.force "com.android.support:support-v4:27.0.2" + resolutionStrategy.force "ccom.android.support:support-core-utils:27.0.2" + resolutionStrategy.force "com.android.support:appcompat-v7:27.0.2" + resolutionStrategy.force "com.android.support:design:27.0.2" + resolutionStrategy.force "com.android.support:support-fragment:27.0.2" } def processFileInplace(file, Closure processText) { diff --git a/app/src/androidTest/assets/riseup.net.json b/app/src/androidTest/assets/riseup.net.json new file mode 100644 index 00000000..9a5ec79e --- /dev/null +++ b/app/src/androidTest/assets/riseup.net.json @@ -0,0 +1,37 @@ +{ + "api_uri": "https://api.black.riseup.net:443", + "api_version": "1", + "ca_cert_fingerprint": "SHA256: a5244308a1374709a9afce95e3ae47c1b44bc2398c0a70ccbf8b3a8a97f29494", + "ca_cert_uri": "https://black.riseup.net/ca.crt", + "default_language": "en", + "description": { + "en": "Riseup is a non-profit collective in Seattle that provides online communication tools for people and groups working toward liberatory social change." + }, + "domain": "riseup.net", + "enrollment_policy": "open", + "languages": [ + "en" + ], + "name": { + "en": "Riseup Networks" + }, + "service": { + "allow_anonymous": false, + "allow_free": true, + "allow_limited_bandwidth": false, + "allow_paid": false, + "allow_registration": true, + "allow_unlimited_bandwidth": true, + "bandwidth_limit": 102400, + "default_service_level": 1, + "levels": { + "1": { + "description": "Please donate.", + "name": "free" + } + } + }, + "services": [ + "openvpn" + ] +} \ No newline at end of file diff --git a/app/src/androidTest/assets/riseup.net.pem b/app/src/androidTest/assets/riseup.net.pem new file mode 100644 index 00000000..c890aff4 --- /dev/null +++ b/app/src/androidTest/assets/riseup.net.pem @@ -0,0 +1,32 @@ +-----BEGIN CERTIFICATE----- +MIIFjTCCA3WgAwIBAgIBATANBgkqhkiG9w0BAQ0FADBZMRgwFgYDVQQKDA9SaXNl +dXAgTmV0d29ya3MxGzAZBgNVBAsMEmh0dHBzOi8vcmlzZXVwLm5ldDEgMB4GA1UE +AwwXUmlzZXVwIE5ldHdvcmtzIFJvb3QgQ0EwHhcNMTQwNDI4MDAwMDAwWhcNMjQw +NDI4MDAwMDAwWjBZMRgwFgYDVQQKDA9SaXNldXAgTmV0d29ya3MxGzAZBgNVBAsM +Emh0dHBzOi8vcmlzZXVwLm5ldDEgMB4GA1UEAwwXUmlzZXVwIE5ldHdvcmtzIFJv +b3QgQ0EwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQC76J4ciMJ8Sg0m +TP7DF2DT9zNe0Csk4myoMFC57rfJeqsAlJCv1XMzBmXrw8wq/9z7XHv6n/0sWU7a +7cF2hLR33ktjwODlx7vorU39/lXLndo492ZBhXQtG1INMShyv+nlmzO6GT7ESfNE +LliFitEzwIegpMqxCIHXFuobGSCWF4N0qLHkq/SYUMoOJ96O3hmPSl1kFDRMtWXY +iw1SEKjUvpyDJpVs3NGxeLCaA7bAWhDY5s5Yb2fA1o8ICAqhowurowJpW7n5ZuLK +5VNTlNy6nZpkjt1QycYvNycffyPOFm/Q/RKDlvnorJIrihPkyniV3YY5cGgP+Qkx +HUOT0uLA6LHtzfiyaOqkXwc4b0ZcQD5Vbf6Prd20Ppt6ei0zazkUPwxld3hgyw58 +m/4UIjG3PInWTNf293GngK2Bnz8Qx9e/6TueMSAn/3JBLem56E0WtmbLVjvko+LF +PM5xA+m0BmuSJtrD1MUCXMhqYTtiOvgLBlUm5zkNxALzG+cXB28k6XikXt6MRG7q +hzIPG38zwkooM55yy5i1YfcIi5NjMH6A+t4IJxxwb67MSb6UFOwg5kFokdONZcwj +shczHdG9gLKSBIvrKa03Nd3W2dF9hMbRu//STcQxOailDBQCnXXfAATj9pYzdY4k +ha8VCAREGAKTDAex9oXf1yRuktES4QIDAQABo2AwXjAdBgNVHQ4EFgQUC4tdmLVu +f9hwfK4AGliaet5KkcgwDgYDVR0PAQH/BAQDAgIEMAwGA1UdEwQFMAMBAf8wHwYD +VR0jBBgwFoAUC4tdmLVuf9hwfK4AGliaet5KkcgwDQYJKoZIhvcNAQENBQADggIB +AGzL+GRnYu99zFoy0bXJKOGCF5XUXP/3gIXPRDqQf5g7Cu/jYMID9dB3No4Zmf7v +qHjiSXiS8jx1j/6/Luk6PpFbT7QYm4QLs1f4BlfZOti2KE8r7KRDPIecUsUXW6P/ +3GJAVYH/+7OjA39za9AieM7+H5BELGccGrM5wfl7JeEz8in+V2ZWDzHQO4hMkiTQ +4ZckuaL201F68YpiItBNnJ9N5nHr1MRiGyApHmLXY/wvlrOpclh95qn+lG6/2jk7 +3AmihLOKYMlPwPakJg4PYczm3icFLgTpjV5sq2md9bRyAg3oPGfAuWHmKj2Ikqch +Td5CHKGxEEWbGUWEMP0s1A/JHWiCbDigc4Cfxhy56CWG4q0tYtnc2GMw8OAUO6Wf +Xu5pYKNkzKSEtT/MrNJt44tTZWbKV/Pi/N2Fx36my7TgTUj7g3xcE9eF4JV2H/sg +tsK3pwE0FEqGnT4qMFbixQmc8bGyuakr23wjMvfO7eZUxBuWYR2SkcP26sozF9PF +tGhbZHQVGZUTVPyvwahMUEhbPGVerOW0IYpxkm0x/eaWdTc4vPpf/rIlgbAjarnJ +UN9SaWRlWKSdP4haujnzCoJbM7dU9bjvlGZNyXEekgeT0W2qFeGGp+yyUWw8tNsp +0BuC1b7uW/bBn/xKm319wXVDvBgZgcktMolak39V7DVO +-----END CERTIFICATE----- \ No newline at end of file diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/StartActivityDrawerTest.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/StartActivityDrawerTest.java new file mode 100644 index 00000000..c7c6a86f --- /dev/null +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/StartActivityDrawerTest.java @@ -0,0 +1,127 @@ +package se.leap.bitmaskclient.test; + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.support.test.InstrumentationRegistry; +import android.support.test.espresso.contrib.DrawerActions; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.IOException; + +import se.leap.bitmaskclient.MainActivity; +import se.leap.bitmaskclient.Provider; +import se.leap.bitmaskclient.testutils.TestSetupHelper; + +import static android.support.test.InstrumentationRegistry.getInstrumentation; +import static android.support.test.espresso.Espresso.onData; +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.contrib.DrawerMatchers.isClosed; +import static android.support.test.espresso.contrib.DrawerMatchers.isOpen; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withId; +import static org.hamcrest.Matchers.anything; +import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; +import static se.leap.bitmaskclient.MainActivity.ACTION_SHOW_VPN_FRAGMENT; +import static se.leap.bitmaskclient.R.id.aboutLayout; +import static se.leap.bitmaskclient.R.id.accountList; +import static se.leap.bitmaskclient.R.id.configuration_wizard_layout; +import static se.leap.bitmaskclient.R.id.drawer_layout; +import static se.leap.bitmaskclient.R.id.eipServiceFragment; +import static se.leap.bitmaskclient.R.id.log_layout; +import static se.leap.bitmaskclient.R.id.settingsList; + +/** + * Created by cyberta on 19.01.18. + */ + +@RunWith(AndroidJUnit4.class) +public class StartActivityDrawerTest { + + Intent intent; + SharedPreferences preferences; + SharedPreferences.Editor preferencesEditor; + + @Rule + public ActivityTestRule mActivityRule = new ActivityTestRule<>( + MainActivity.class, + true, + false); // Activity is not launched immediately + + @Before + public void setUp() throws IOException { + intent = new Intent(ACTION_SHOW_VPN_FRAGMENT); + Context context = getInstrumentation().getTargetContext(); + + preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE); + preferencesEditor = preferences.edit(); + preferencesEditor.putString(Provider.KEY, TestSetupHelper.getInputAsString(InstrumentationRegistry.getContext().getAssets().open("riseup.net.json"))) + .putString(Provider.CA_CERT, TestSetupHelper.getInputAsString(InstrumentationRegistry.getContext().getAssets().open("riseup.net.pem"))) + .commit(); + + } + + + @Test + public void testDisplayDrawer_isOpenUntilUserManuallyOpendDrawerOnce() { + preferencesEditor.putBoolean("navigation_drawer_learned", false).commit(); + mActivityRule.launchActivity(intent); + onView(withId(drawer_layout)).check(matches(isOpen())); + onView(withId(drawer_layout)).perform(DrawerActions.close()); + mActivityRule.finishActivity(); + + mActivityRule.launchActivity(intent); + onView(withId(drawer_layout)).check(matches(isOpen())); + onView(withId(drawer_layout)).perform(DrawerActions.close()); + onView(withId(drawer_layout)).perform(DrawerActions.open()); + mActivityRule.finishActivity(); + + mActivityRule.launchActivity(intent); + onView(withId(drawer_layout)).check(matches(isClosed())); + } + + @Test + public void testClickProviderName_closeDrawerAndShowEipFragment() { + preferencesEditor.putBoolean("navigation_drawer_learned", false).commit(); + mActivityRule.launchActivity(intent); + + onData(anything()).inAdapterView(withId(accountList)).atPosition(0).perform(click()); + onView(withId(drawer_layout)).check(matches(isClosed())); + onView(withId(eipServiceFragment)).check(matches(isDisplayed())); + } + + @Test + public void testClickSwitchProvider_closeDrawerAndShowProviderListView() { + preferencesEditor.putBoolean("navigation_drawer_learned", false).commit(); + mActivityRule.launchActivity(intent); + + onData(anything()).inAdapterView(withId(settingsList)).atPosition(0).perform(click()); + onView(withId(configuration_wizard_layout)).check(matches(isDisplayed())); + } + + @Test + public void testClickLog_closeDrawerAndShowLogFragment() { + preferencesEditor.putBoolean("navigation_drawer_learned", false).commit(); + mActivityRule.launchActivity(intent); + + onData(anything()).inAdapterView(withId(settingsList)).atPosition(1).perform(click()); + onView(withId(log_layout)).check(matches(isDisplayed())); + } + + @Test + public void testClickAbout_closeDrawerAndShowAboutFragment() { + preferencesEditor.putBoolean("navigation_drawer_learned", false).commit(); + mActivityRule.launchActivity(intent); + + onData(anything()).inAdapterView(withId(settingsList)).atPosition(2).perform(click()); + onView(withId(aboutLayout)).check(matches(isDisplayed())); + } +} diff --git a/app/src/main/res/layout-sw600dp-port/f_log.xml b/app/src/main/res/layout-sw600dp-port/f_log.xml index da46450c..4ee64811 100644 --- a/app/src/main/res/layout-sw600dp-port/f_log.xml +++ b/app/src/main/res/layout-sw600dp-port/f_log.xml @@ -5,9 +5,30 @@ --> + android:layout_width="match_parent" + android:layout_height="match_parent" + xmlns:tools="http://schemas.android.com/tools" + android:orientation="vertical" + android:padding="@dimen/activity_margin" + android:id="@+id/log_layout"> + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout-sw600dp/f_log.xml b/app/src/main/res/layout-sw600dp/f_log.xml index 9ad30208..ec66af07 100644 --- a/app/src/main/res/layout-sw600dp/f_log.xml +++ b/app/src/main/res/layout-sw600dp/f_log.xml @@ -5,12 +5,12 @@ --> - + android:padding="20dp" + android:id="@+id/log_layout" + > + android:layout_marginStart="20dp" + android:id="@+id/eipServiceFragment" + > - - + android:layout_height="match_parent" + android:id="@+id/eipServiceFragment" + > + android:padding="16dp" + android:id="@+id/log_layout" + > . + */ + +package se.leap.bitmaskclient.testutils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +/** + * Created by cyberta on 08.10.17. + */ + +public class TestSetupHelper { + + public static String getInputAsString(InputStream fileAsInputStream) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(fileAsInputStream)); + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + while (line != null) { + sb.append(line); + line = br.readLine(); + } + + return sb.toString(); + } + +} diff --git a/app/src/test/java/se/leap/bitmaskclient/ConfigHelperTest.java b/app/src/test/java/se/leap/bitmaskclient/ConfigHelperTest.java index a9a5733d..84f38086 100644 --- a/app/src/test/java/se/leap/bitmaskclient/ConfigHelperTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/ConfigHelperTest.java @@ -6,9 +6,9 @@ import org.junit.Before; import org.junit.Test; import se.leap.bitmaskclient.testutils.MockSharedPreferences; -import se.leap.bitmaskclient.testutils.TestSetupHelper; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static se.leap.bitmaskclient.Constants.PROVIDER_CONFIGURED; import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString; diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java index f04ec0eb..c23e4f49 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java @@ -52,14 +52,14 @@ import static se.leap.bitmaskclient.ProviderAPI.RESULT_KEY; import static se.leap.bitmaskclient.testutils.BackendMockResponses.BackendMockProvider.TestBackendErrorCase.ERROR_CASE_UPDATED_CERTIFICATE; import static se.leap.bitmaskclient.testutils.BackendMockResponses.BackendMockProvider.TestBackendErrorCase.NO_ERROR; import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString; -import static se.leap.bitmaskclient.testutils.TestSetupHelper.mockBundle; -import static se.leap.bitmaskclient.testutils.TestSetupHelper.mockClientGenerator; -import static se.leap.bitmaskclient.testutils.TestSetupHelper.mockFingerprintForCertificate; -import static se.leap.bitmaskclient.testutils.TestSetupHelper.mockIntent; -import static se.leap.bitmaskclient.testutils.TestSetupHelper.mockProviderApiConnector; -import static se.leap.bitmaskclient.testutils.TestSetupHelper.mockResources; -import static se.leap.bitmaskclient.testutils.TestSetupHelper.mockResultReceiver; -import static se.leap.bitmaskclient.testutils.TestSetupHelper.mockTextUtils; +import static se.leap.bitmaskclient.testutils.MockHelper.mockBundle; +import static se.leap.bitmaskclient.testutils.MockHelper.mockClientGenerator; +import static se.leap.bitmaskclient.testutils.MockHelper.mockFingerprintForCertificate; +import static se.leap.bitmaskclient.testutils.MockHelper.mockIntent; +import static se.leap.bitmaskclient.testutils.MockHelper.mockProviderApiConnector; +import static se.leap.bitmaskclient.testutils.MockHelper.mockResources; +import static se.leap.bitmaskclient.testutils.MockHelper.mockResultReceiver; +import static se.leap.bitmaskclient.testutils.MockHelper.mockTextUtils; /** diff --git a/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java new file mode 100644 index 00000000..8372c9bc --- /dev/null +++ b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java @@ -0,0 +1,398 @@ +package se.leap.bitmaskclient.testutils; + +import android.content.Intent; +import android.content.res.Resources; +import android.os.Bundle; +import android.os.Parcelable; +import android.os.ResultReceiver; +import android.support.annotation.NonNull; +import android.text.TextUtils; + +import org.json.JSONException; +import org.json.JSONObject; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.io.IOException; +import java.io.InputStream; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateEncodingException; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import okhttp3.OkHttpClient; +import se.leap.bitmaskclient.ConfigHelper; +import se.leap.bitmaskclient.OkHttpClientGenerator; +import se.leap.bitmaskclient.R; +import se.leap.bitmaskclient.testutils.BackendMockResponses.BackendMockProvider; +import se.leap.bitmaskclient.testutils.matchers.BundleMatcher; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + +/** + * Created by cyberta on 29.01.18. + */ + +public class MockHelper { + @NonNull + public static Bundle mockBundle() { + final Map fakeBooleanBundle = new HashMap<>(); + final Map fakeStringBundle = new HashMap<>(); + final Map fakeIntBundle = new HashMap<>(); + final Map fakeParcelableBundle = new HashMap<>(); + + Bundle bundle = mock(Bundle.class); + + //mock String values in Bundle + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + String value = ((String) arguments[1]); + fakeStringBundle.put(key, value); + return null; + } + }).when(bundle).putString(anyString(), anyString()); + when(bundle.getString(anyString())).thenAnswer(new Answer() { + @Override + public String answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + return fakeStringBundle.get(key); + } + }); + + //mock Boolean values in Bundle + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + Boolean value = ((boolean) arguments[1]); + fakeBooleanBundle.put(key, value); + return null; + } + }).when(bundle).putBoolean(anyString(), anyBoolean()); + when(bundle.getBoolean(anyString())).thenAnswer(new Answer() { + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + return fakeBooleanBundle.get(key); + } + }); + + //mock Integer values in Bundle + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + Integer value = ((int) arguments[1]); + fakeIntBundle.put(key, value); + return null; + } + }).when(bundle).putInt(anyString(), anyInt()); + when(bundle.getInt(anyString())).thenAnswer(new Answer() { + @Override + public Integer answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + return fakeIntBundle.get(key); + } + }); + + //mock Parcelable values in Bundle + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + Parcelable value = ((Parcelable) arguments[1]); + fakeParcelableBundle.put(key, value); + return null; + } + }).when(bundle).putParcelable(anyString(), any(Parcelable.class)); + when(bundle.getParcelable(anyString())).thenAnswer(new Answer() { + @Override + public Parcelable answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + return fakeParcelableBundle.get(key); + } + }); + + //mock get + when(bundle.get(anyString())).thenAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + if (fakeBooleanBundle.containsKey(key)) { + return fakeBooleanBundle.get(key); + } else if (fakeIntBundle.containsKey(key)) { + return fakeIntBundle.get(key); + } else if (fakeStringBundle.containsKey(key)) { + return fakeStringBundle.get(key); + } else { + return fakeParcelableBundle.get(key); + } + } + }); + + //mock getKeySet + when(bundle.keySet()).thenAnswer(new Answer>() { + @Override + public Set answer(InvocationOnMock invocation) throws Throwable { + //this whole approach as a drawback: + //you should not add the same keys for values of different types + HashSet keys = new HashSet(); + keys.addAll(fakeBooleanBundle.keySet()); + keys.addAll(fakeIntBundle.keySet()); + keys.addAll(fakeStringBundle.keySet()); + keys.addAll(fakeParcelableBundle.keySet()); + return keys; + } + }); + + //mock containsKey + when(bundle.containsKey(anyString())).thenAnswer(new Answer() { + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + String key = (String) invocation.getArguments()[0]; + return fakeBooleanBundle.containsKey(key) || + fakeStringBundle.containsKey(key) || + fakeIntBundle.containsKey(key) || + fakeParcelableBundle.containsKey(key); + } + }); + + return bundle; + } + + public static Intent mockIntent() { + Intent intent = mock(Intent.class); + final String[] action = new String[1]; + final Map fakeExtras = new HashMap<>(); + final List categories = new ArrayList<>(); + + + //mock Action in intent + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + action[0] = ((String) arguments[0]); + return null; + } + }).when(intent).setAction(anyString()); + when(intent.getAction()).thenAnswer(new Answer() { + @Override + public String answer(InvocationOnMock invocation) throws Throwable { + return action[0]; + } + }); + + //mock Bundle in intent extras + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + Bundle value = ((Bundle) arguments[1]); + fakeExtras.put(key, value); + return null; + } + }).when(intent).putExtra(anyString(), any(Bundle.class)); + when(intent.getBundleExtra(anyString())).thenAnswer(new Answer() { + @Override + public Bundle answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + return (Bundle) fakeExtras.get(key); + } + }); + + //mock Parcelable in intent extras + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + Parcelable value = ((Parcelable) arguments[1]); + fakeExtras.put(key, value); + return null; + } + }).when(intent).putExtra(anyString(), any(Parcelable.class)); + when(intent.getParcelableExtra(anyString())).thenAnswer(new Answer() { + @Override + public Parcelable answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + String key = ((String) arguments[0]); + return (Parcelable) fakeExtras.get(key); + } + }); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + categories.add(((String) arguments[0])); + return null; + } + }).when(intent).addCategory(anyString()); + + when(intent.getCategories()).thenAnswer(new Answer>() { + @Override + public Set answer(InvocationOnMock invocation) throws Throwable { + return new HashSet<>(categories); + } + }); + + return intent; + } + + public static void mockTextUtils() { + mockStatic(TextUtils.class); + + when(TextUtils.equals(any(CharSequence.class), any(CharSequence.class))).thenAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + CharSequence a = (CharSequence) invocation.getArguments()[0]; + CharSequence b = (CharSequence) invocation.getArguments()[1]; + if (a == b) return true; + int length; + if (a != null && b != null && (length = a.length()) == b.length()) { + if (a instanceof String && b instanceof String) { + return a.equals(b); + } else { + for (int i = 0; i < length; i++) { + if (a.charAt(i) != b.charAt(i)) return false; + } + return true; + } + } + return false; + } + }); + + when(TextUtils.isEmpty(any(CharSequence.class))).thenAnswer(new Answer() { + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + CharSequence param = (CharSequence) invocation.getArguments()[0]; + return param == null || param.length() == 0; + } + }); + } + + public static ResultReceiver mockResultReceiver(final int expectedResultCode, final Bundle expectedBundle) { + ResultReceiver resultReceiver = mock(ResultReceiver.class); + + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] arguments = invocation.getArguments(); + int resultCode = (int) arguments[0]; + Bundle bundle = (Bundle) arguments[1]; + Set keys = expectedBundle.keySet(); + Iterator iterator = keys.iterator(); + HashMap expectedIntegers = new HashMap<>(); + HashMap expectedStrings = new HashMap<>(); + HashMap expectedBooleans = new HashMap<>(); + HashMap expectedParcelables = new HashMap<>(); + while (iterator.hasNext()) { + String key = iterator.next(); + Object value = expectedBundle.get(key); + + if (value instanceof Boolean) { + expectedBooleans.put(key, (Boolean) value); + } else if (value instanceof Integer) { + expectedIntegers.put(key, (Integer) value); + } else if (value instanceof String) { + expectedStrings.put(key, (String) value); + } else if (value instanceof Parcelable) { + expectedParcelables.put(key, (Parcelable) value); + } + } + assertThat("expected bundle: ", bundle, new BundleMatcher(expectedIntegers, expectedStrings, expectedBooleans, expectedParcelables)); + assertEquals("expected resultCode: ", expectedResultCode, resultCode); + return null; + } + }).when(resultReceiver).send(anyInt(), any(Bundle.class)); + return resultReceiver; + } + + public static void mockFingerprintForCertificate(String mockedFingerprint) throws CertificateEncodingException, NoSuchAlgorithmException { + mockStatic(ConfigHelper.class); + when(ConfigHelper.getFingerprintFromCertificate(any(X509Certificate.class), anyString())).thenReturn(mockedFingerprint); + when(ConfigHelper.checkErroneousDownload(anyString())).thenCallRealMethod(); + when(ConfigHelper.parseX509CertificateFromString(anyString())).thenCallRealMethod(); + } + + public static void mockProviderApiConnector(final BackendMockProvider.TestBackendErrorCase errorCase) throws IOException { + BackendMockProvider.provideBackendResponsesFor(errorCase); + } + + public static OkHttpClientGenerator mockClientGenerator() { + OkHttpClientGenerator mockClientGenerator = mock(OkHttpClientGenerator.class); + OkHttpClient mockedOkHttpClient = mock(OkHttpClient.class); + when(mockClientGenerator.initCommercialCAHttpClient(any(JSONObject.class))).thenReturn(mockedOkHttpClient); + when(mockClientGenerator.initSelfSignedCAHttpClient(any(JSONObject.class))).thenReturn(mockedOkHttpClient); + when(mockClientGenerator.initSelfSignedCAHttpClient(any(JSONObject.class), anyString())).thenReturn(mockedOkHttpClient); + return mockClientGenerator; + } + + public static Resources mockResources(InputStream inputStream) throws IOException, JSONException { + Resources mockedResources = mock(Resources.class, RETURNS_DEEP_STUBS); + JSONObject errorMessages = new JSONObject(TestSetupHelper.getInputAsString(inputStream)); + + + when(mockedResources.getString(R.string.warning_corrupted_provider_details)). + thenReturn(errorMessages.getString("warning_corrupted_provider_details")); + when(mockedResources.getString(R.string.server_unreachable_message)). + thenReturn(errorMessages.getString("server_unreachable_message")); + when(mockedResources.getString(R.string.error_security_pinnedcertificate)). + thenReturn(errorMessages.getString("error.security.pinnedcertificate")); + when(mockedResources.getString(R.string.malformed_url)). + thenReturn(errorMessages.getString("malformed_url")); + when(mockedResources.getString(R.string.certificate_error)). + thenReturn(errorMessages.getString("certificate_error")); + when(mockedResources.getString(R.string.error_srp_math_error_user_message)). + thenReturn(errorMessages.getString("error_srp_math_error_user_message")); + when(mockedResources.getString(R.string.error_bad_user_password_user_message)). + thenReturn(errorMessages.getString("error_bad_user_password_user_message")); + when(mockedResources.getString(R.string.error_not_valid_password_user_message)). + thenReturn(errorMessages.getString("error_not_valid_password_user_message")); + when(mockedResources.getString(R.string.error_client_http_user_message)). + thenReturn(errorMessages.getString("error_client_http_user_message")); + when(mockedResources.getString(R.string.error_io_exception_user_message)). + thenReturn(errorMessages.getString("error_io_exception_user_message")); + when(mockedResources.getString(R.string.error_json_exception_user_message)). + thenReturn(errorMessages.getString("error_json_exception_user_message")); + when(mockedResources.getString(R.string.error_no_such_algorithm_exception_user_message)). + thenReturn(errorMessages.getString("error_no_such_algorithm_exception_user_message")); + when(mockedResources.getString(R.string.warning_corrupted_provider_details)). + thenReturn(errorMessages.getString("warning_corrupted_provider_details")); + when(mockedResources.getString(R.string.warning_corrupted_provider_cert)). + thenReturn(errorMessages.getString("warning_corrupted_provider_cert")); + when(mockedResources.getString(R.string.warning_expired_provider_cert)). + thenReturn(errorMessages.getString("warning_expired_provider_cert")); + return mockedResources; + } +} diff --git a/app/src/test/java/se/leap/bitmaskclient/testutils/TestSetupHelper.java b/app/src/test/java/se/leap/bitmaskclient/testutils/TestSetupHelper.java deleted file mode 100644 index f8f70eaf..00000000 --- a/app/src/test/java/se/leap/bitmaskclient/testutils/TestSetupHelper.java +++ /dev/null @@ -1,434 +0,0 @@ -/** - * Copyright (c) 2018 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.testutils; - -import android.content.Intent; -import android.content.res.Resources; -import android.os.Bundle; -import android.os.Parcelable; -import android.os.ResultReceiver; -import android.support.annotation.NonNull; -import android.text.TextUtils; - -import org.json.JSONException; -import org.json.JSONObject; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.security.NoSuchAlgorithmException; -import java.security.cert.CertificateEncodingException; -import java.security.cert.X509Certificate; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import okhttp3.OkHttpClient; -import se.leap.bitmaskclient.ConfigHelper; -import se.leap.bitmaskclient.OkHttpClientGenerator; -import se.leap.bitmaskclient.R; -import se.leap.bitmaskclient.testutils.BackendMockResponses.BackendMockProvider; -import se.leap.bitmaskclient.testutils.matchers.BundleMatcher; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.mockStatic; - -/** - * Created by cyberta on 08.10.17. - */ - -public class TestSetupHelper { - - - - public static String getInputAsString(InputStream fileAsInputStream) throws IOException { - BufferedReader br = new BufferedReader(new InputStreamReader(fileAsInputStream)); - StringBuilder sb = new StringBuilder(); - String line = br.readLine(); - while (line != null) { - sb.append(line); - line = br.readLine(); - } - - return sb.toString(); - } - - @NonNull - public static Bundle mockBundle() { - final Map fakeBooleanBundle = new HashMap<>(); - final Map fakeStringBundle = new HashMap<>(); - final Map fakeIntBundle = new HashMap<>(); - final Map fakeParcelableBundle = new HashMap<>(); - - Bundle bundle = mock(Bundle.class); - - //mock String values in Bundle - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - String value = ((String) arguments[1]); - fakeStringBundle.put(key, value); - return null; - } - }).when(bundle).putString(anyString(), anyString()); - when(bundle.getString(anyString())).thenAnswer(new Answer() { - @Override - public String answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - return fakeStringBundle.get(key); - } - }); - - //mock Boolean values in Bundle - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - Boolean value = ((boolean) arguments[1]); - fakeBooleanBundle.put(key, value); - return null; - } - }).when(bundle).putBoolean(anyString(), anyBoolean()); - when(bundle.getBoolean(anyString())).thenAnswer(new Answer() { - @Override - public Boolean answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - return fakeBooleanBundle.get(key); - } - }); - - //mock Integer values in Bundle - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - Integer value = ((int) arguments[1]); - fakeIntBundle.put(key, value); - return null; - } - }).when(bundle).putInt(anyString(), anyInt()); - when(bundle.getInt(anyString())).thenAnswer(new Answer() { - @Override - public Integer answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - return fakeIntBundle.get(key); - } - }); - - //mock Parcelable values in Bundle - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - Parcelable value = ((Parcelable) arguments[1]); - fakeParcelableBundle.put(key, value); - return null; - } - }).when(bundle).putParcelable(anyString(), any(Parcelable.class)); - when(bundle.getParcelable(anyString())).thenAnswer(new Answer() { - @Override - public Parcelable answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - return fakeParcelableBundle.get(key); - } - }); - - //mock get - when(bundle.get(anyString())).thenAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - if (fakeBooleanBundle.containsKey(key)) { - return fakeBooleanBundle.get(key); - } else if (fakeIntBundle.containsKey(key)) { - return fakeIntBundle.get(key); - } else if (fakeStringBundle.containsKey(key)) { - return fakeStringBundle.get(key); - } else { - return fakeParcelableBundle.get(key); - } - } - }); - - //mock getKeySet - when(bundle.keySet()).thenAnswer(new Answer>() { - @Override - public Set answer(InvocationOnMock invocation) throws Throwable { - //this whole approach as a drawback: - //you should not add the same keys for values of different types - HashSet keys = new HashSet(); - keys.addAll(fakeBooleanBundle.keySet()); - keys.addAll(fakeIntBundle.keySet()); - keys.addAll(fakeStringBundle.keySet()); - keys.addAll(fakeParcelableBundle.keySet()); - return keys; - } - }); - - //mock containsKey - when(bundle.containsKey(anyString())).thenAnswer(new Answer() { - @Override - public Boolean answer(InvocationOnMock invocation) throws Throwable { - String key = (String) invocation.getArguments()[0]; - return fakeBooleanBundle.containsKey(key) || - fakeStringBundle.containsKey(key) || - fakeIntBundle.containsKey(key) || - fakeParcelableBundle.containsKey(key); - } - }); - - return bundle; - } - - public static Intent mockIntent() { - Intent intent = mock(Intent.class); - final String[] action = new String[1]; - final Map fakeExtras = new HashMap<>(); - final List categories = new ArrayList<>(); - - - //mock Action in intent - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - action[0] = ((String) arguments[0]); - return null; - } - }).when(intent).setAction(anyString()); - when(intent.getAction()).thenAnswer(new Answer() { - @Override - public String answer(InvocationOnMock invocation) throws Throwable { - return action[0]; - } - }); - - //mock Bundle in intent extras - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - Bundle value = ((Bundle) arguments[1]); - fakeExtras.put(key, value); - return null; - } - }).when(intent).putExtra(anyString(), any(Bundle.class)); - when(intent.getBundleExtra(anyString())).thenAnswer(new Answer() { - @Override - public Bundle answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - return (Bundle) fakeExtras.get(key); - } - }); - - //mock Parcelable in intent extras - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - Parcelable value = ((Parcelable) arguments[1]); - fakeExtras.put(key, value); - return null; - } - }).when(intent).putExtra(anyString(), any(Parcelable.class)); - when(intent.getParcelableExtra(anyString())).thenAnswer(new Answer() { - @Override - public Parcelable answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - String key = ((String) arguments[0]); - return (Parcelable) fakeExtras.get(key); - } - }); - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - categories.add(((String) arguments[0])); - return null; - } - }).when(intent).addCategory(anyString()); - - when(intent.getCategories()).thenAnswer(new Answer>() { - @Override - public Set answer(InvocationOnMock invocation) throws Throwable { - return new HashSet<>(categories); - } - }); - - return intent; - } - - public static void mockTextUtils() { - mockStatic(TextUtils.class); - - when(TextUtils.equals(any(CharSequence.class), any(CharSequence.class))).thenAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - CharSequence a = (CharSequence) invocation.getArguments()[0]; - CharSequence b = (CharSequence) invocation.getArguments()[1]; - if (a == b) return true; - int length; - if (a != null && b != null && (length = a.length()) == b.length()) { - if (a instanceof String && b instanceof String) { - return a.equals(b); - } else { - for (int i = 0; i < length; i++) { - if (a.charAt(i) != b.charAt(i)) return false; - } - return true; - } - } - return false; - } - }); - - when(TextUtils.isEmpty(any(CharSequence.class))).thenAnswer(new Answer() { - @Override - public Boolean answer(InvocationOnMock invocation) throws Throwable { - CharSequence param = (CharSequence) invocation.getArguments()[0]; - return param == null || param.length() == 0; - } - }); - } - - - public static ResultReceiver mockResultReceiver(final int expectedResultCode, final Bundle expectedBundle) { - ResultReceiver resultReceiver = mock(ResultReceiver.class); - - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Object[] arguments = invocation.getArguments(); - int resultCode = (int) arguments[0]; - Bundle bundle = (Bundle) arguments[1]; - Set keys = expectedBundle.keySet(); - Iterator iterator = keys.iterator(); - HashMap expectedIntegers = new HashMap<>(); - HashMap expectedStrings = new HashMap<>(); - HashMap expectedBooleans = new HashMap<>(); - HashMap expectedParcelables = new HashMap<>(); - while (iterator.hasNext()) { - String key = iterator.next(); - Object value = expectedBundle.get(key); - - if (value instanceof Boolean) { - expectedBooleans.put(key, (Boolean) value); - } else if (value instanceof Integer) { - expectedIntegers.put(key, (Integer) value); - } else if (value instanceof String) { - expectedStrings.put(key, (String) value); - } else if (value instanceof Parcelable) { - expectedParcelables.put(key, (Parcelable) value); - } - } - assertThat("expected bundle: ", bundle, new BundleMatcher(expectedIntegers, expectedStrings, expectedBooleans, expectedParcelables)); - assertEquals("expected resultCode: ", expectedResultCode, resultCode); - return null; - } - }).when(resultReceiver).send(anyInt(), any(Bundle.class)); - return resultReceiver; - } - - public static void mockFingerprintForCertificate(String mockedFingerprint) throws CertificateEncodingException, NoSuchAlgorithmException { - mockStatic(ConfigHelper.class); - when(ConfigHelper.getFingerprintFromCertificate(any(X509Certificate.class), anyString())).thenReturn(mockedFingerprint); - when(ConfigHelper.checkErroneousDownload(anyString())).thenCallRealMethod(); - when(ConfigHelper.parseX509CertificateFromString(anyString())).thenCallRealMethod(); - } - - public static void mockProviderApiConnector(final BackendMockProvider.TestBackendErrorCase errorCase) throws IOException { - BackendMockProvider.provideBackendResponsesFor(errorCase); - } - - public static OkHttpClientGenerator mockClientGenerator() { - OkHttpClientGenerator mockClientGenerator = mock(OkHttpClientGenerator.class); - OkHttpClient mockedOkHttpClient = mock(OkHttpClient.class); - when(mockClientGenerator.initCommercialCAHttpClient(any(JSONObject.class))).thenReturn(mockedOkHttpClient); - when(mockClientGenerator.initSelfSignedCAHttpClient(any(JSONObject.class))).thenReturn(mockedOkHttpClient); - when(mockClientGenerator.initSelfSignedCAHttpClient(any(JSONObject.class), anyString())).thenReturn(mockedOkHttpClient); - return mockClientGenerator; - } - - public static Resources mockResources(InputStream inputStream) throws IOException, JSONException { - Resources mockedResources = mock(Resources.class, RETURNS_DEEP_STUBS); - JSONObject errorMessages = new JSONObject(getInputAsString(inputStream)); - - - when(mockedResources.getString(R.string.warning_corrupted_provider_details)). - thenReturn(errorMessages.getString("warning_corrupted_provider_details")); - when(mockedResources.getString(R.string.server_unreachable_message)). - thenReturn(errorMessages.getString("server_unreachable_message")); - when(mockedResources.getString(R.string.error_security_pinnedcertificate)). - thenReturn(errorMessages.getString("error.security.pinnedcertificate")); - when(mockedResources.getString(R.string.malformed_url)). - thenReturn(errorMessages.getString("malformed_url")); - when(mockedResources.getString(R.string.certificate_error)). - thenReturn(errorMessages.getString("certificate_error")); - when(mockedResources.getString(R.string.error_srp_math_error_user_message)). - thenReturn(errorMessages.getString("error_srp_math_error_user_message")); - when(mockedResources.getString(R.string.error_bad_user_password_user_message)). - thenReturn(errorMessages.getString("error_bad_user_password_user_message")); - when(mockedResources.getString(R.string.error_not_valid_password_user_message)). - thenReturn(errorMessages.getString("error_not_valid_password_user_message")); - when(mockedResources.getString(R.string.error_client_http_user_message)). - thenReturn(errorMessages.getString("error_client_http_user_message")); - when(mockedResources.getString(R.string.error_io_exception_user_message)). - thenReturn(errorMessages.getString("error_io_exception_user_message")); - when(mockedResources.getString(R.string.error_json_exception_user_message)). - thenReturn(errorMessages.getString("error_json_exception_user_message")); - when(mockedResources.getString(R.string.error_no_such_algorithm_exception_user_message)). - thenReturn(errorMessages.getString("error_no_such_algorithm_exception_user_message")); - when(mockedResources.getString(R.string.warning_corrupted_provider_details)). - thenReturn(errorMessages.getString("warning_corrupted_provider_details")); - when(mockedResources.getString(R.string.warning_corrupted_provider_cert)). - thenReturn(errorMessages.getString("warning_corrupted_provider_cert")); - when(mockedResources.getString(R.string.warning_expired_provider_cert)). - thenReturn(errorMessages.getString("warning_expired_provider_cert")); - return mockedResources; - } - -} diff --git a/docker/android-emulator/Dockerfile b/docker/android-emulator/Dockerfile index bececd08..cc9e0750 100644 --- a/docker/android-emulator/Dockerfile +++ b/docker/android-emulator/Dockerfile @@ -31,7 +31,6 @@ RUN apt-get update -qq && \ RUN echo y | sdkmanager "emulator" # Install System Images for emulators -RUN echo y | sdkmanager "system-images;android-26;google_apis;x86" +RUN echo y | sdkmanager "system-images;android-27;google_apis;x86" RUN echo y | sdkmanager "system-images;android-25;google_apis;x86_64" -RUN echo y | sdkmanager "system-images;android-24;google_apis;x86_64" RUN echo y | sdkmanager "system-images;android-23;google_apis;x86_64" diff --git a/docker/android-sdk/Dockerfile b/docker/android-sdk/Dockerfile index fbb63aa8..3218d369 100644 --- a/docker/android-sdk/Dockerfile +++ b/docker/android-sdk/Dockerfile @@ -59,14 +59,11 @@ RUN echo y | sdkmanager "platform-tools" # echo y to accept google licenses RUN sdkmanager "extras;android;m2repository" # Install Build Tools (Please keep in descending order) -RUN sdkmanager "build-tools;26.0.0" +RUN sdkmanager "build-tools;27.0.3" RUN sdkmanager "build-tools;25.0.2" -RUN sdkmanager "build-tools;25.0.0" -RUN sdkmanager "build-tools;24.0.3" RUN sdkmanager "build-tools;23.0.3" # Install Target SDK Packages (Please keep in descending order) -RUN sdkmanager "platforms;android-26" +RUN sdkmanager "platforms;android-27" RUN sdkmanager "platforms;android-25" -RUN sdkmanager "platforms;android-24" RUN sdkmanager "platforms;android-23" -- cgit v1.2.3