diff --git a/app/build.gradle b/app/build.gradle index 3ea69de937..6fdc3c7d18 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -196,18 +196,6 @@ android { targetCompatibility JavaVersion.VERSION_1_8 } - testOptions { - unitTests { - returnDefaultValues = true - includeAndroidResources = true - - all { - maxParallelForks = 10 - forkEvery = 20 - } - } - } - useLibrary "org.apache.http.legacy" } @@ -235,27 +223,9 @@ dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - testImplementation "junit:junit:$junit_version" - testImplementation 'org.json:json:20201115' - testImplementation "org.mockito:mockito-core:${mockitoVersion}" - testImplementation "org.powermock:powermock-api-mockito2:${powermockVersion}" - testImplementation "org.powermock:powermock-module-junit4-rule-agent:${powermockVersion}" - testImplementation "org.powermock:powermock-module-junit4-rule:${powermockVersion}" - testImplementation "org.powermock:powermock-module-junit4:${powermockVersion}" - testImplementation "joda-time:joda-time:$jodatime_version" - testImplementation('com.google.truth:truth:1.1.2') { - exclude group: "com.google.guava", module: "guava" - } - testImplementation "org.skyscreamer:jsonassert:1.5.0" - testImplementation "org.hamcrest:hamcrest-all:1.3" - - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0-alpha04' - androidTestImplementation "androidx.test.ext:junit:$androidx_junit" - androidTestImplementation "androidx.test:rules:$androidx_rules" - /* Dagger2 - We are going to use dagger.android which includes - * support for Activity and fragment injection so we need to include - * the following dependencies */ + * support for Activity and fragment injection so we need to include + * the following dependencies */ annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version" @@ -264,10 +234,11 @@ dependencies { /* Dagger2 - default dependency */ kapt "com.google.dagger:dagger-compiler:$dagger_version" - - androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0' } +apply from: "${project.rootDir}/gradle/test_dependencies.gradle" + + /* // Run 'adb' shell command to clear application data of main app for 'debug' variant task clearMainAppData(type: Exec) { diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/EncryptedPrefsFormatTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/EncryptedPrefsFormatTest.kt index 5438f15eac..22674da300 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/EncryptedPrefsFormatTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/EncryptedPrefsFormatTest.kt @@ -4,11 +4,11 @@ import info.nightscout.androidaps.TestBase import info.nightscout.androidaps.plugins.general.maintenance.formats.* import info.nightscout.androidaps.testing.utils.SingleStringStorage import info.nightscout.androidaps.utils.CryptoUtil -import info.nightscout.androidaps.utils.assumeAES256isSupported import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import org.hamcrest.CoreMatchers import org.junit.Assert +import org.junit.Assume import org.junit.Before import org.junit.Test import org.junit.runner.RunWith @@ -20,6 +20,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner import java.io.File +@Suppress("SpellCheckingInspection") @PowerMockIgnore("javax.crypto.*") @RunWith(PowerMockRunner::class) @PrepareForTest(File::class) @@ -32,6 +33,16 @@ class EncryptedPrefsFormatTest : TestBase() { private var cryptoUtil: CryptoUtil = CryptoUtil(aapsLogger) + // https://stackoverflow.com/questions/52344522/joseexception-couldnt-create-aes-gcm-nopadding-cipher-illegal-key-size + // https://stackoverflow.com/questions/47708951/can-aes-256-work-on-android-devices-with-api-level-26 + // Java prior to Oracle Java 8u161 does not have policy for 256 bit AES - but Android support it + // when test is run in Vanilla JVM without policy - Invalid key size exception is thrown + private fun assumeAES256isSupported(cryptoUtil: CryptoUtil) { + cryptoUtil.lastException?.message?.let { exceptionMessage -> + Assume.assumeThat("Upgrade your testing environment Java (OpenJDK or Java 8u161) and JAVA_HOME - AES 256 is supported by Android so this exception should not happen!", exceptionMessage, CoreMatchers.not(CoreMatchers.containsString("key size"))) + } + } + @Before fun mock() { Mockito.`when`(resourceHelper.gs(ArgumentMatchers.anyInt())).thenReturn("mock translation") @@ -57,13 +68,13 @@ class EncryptedPrefsFormatTest : TestBase() { assumeAES256isSupported(cryptoUtil) - Assert.assertThat(prefs.values.size, CoreMatchers.`is`(2)) - Assert.assertThat(prefs.values["key1"], CoreMatchers.`is`("A")) - Assert.assertThat(prefs.values["keyB"], CoreMatchers.`is`("2")) + Assert.assertEquals(prefs.values.size, 2) + Assert.assertEquals(prefs.values["key1"], "A") + Assert.assertEquals(prefs.values["keyB"], "2") - Assert.assertThat(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, CoreMatchers.`is`(PrefsStatus.OK)) - Assert.assertThat(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, CoreMatchers.`is`(EncryptedPrefsFormat.FORMAT_KEY_ENC)) - Assert.assertThat(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, CoreMatchers.`is`(PrefsStatus.OK)) + Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK) + Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, EncryptedPrefsFormat.FORMAT_KEY_ENC) + Assert.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.OK) } @Test @@ -101,13 +112,13 @@ class EncryptedPrefsFormatTest : TestBase() { assumeAES256isSupported(cryptoUtil) - Assert.assertThat(prefsOut.values.size, CoreMatchers.`is`(2)) - Assert.assertThat(prefsOut.values["testpref1"], CoreMatchers.`is`("--1--")) - Assert.assertThat(prefsOut.values["testpref2"], CoreMatchers.`is`("another")) + Assert.assertEquals(prefsOut.values.size, 2) + Assert.assertEquals(prefsOut.values["testpref1"], "--1--") + Assert.assertEquals(prefsOut.values["testpref2"], "another") - Assert.assertThat(prefsOut.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, CoreMatchers.`is`(PrefsStatus.OK)) - Assert.assertThat(prefsOut.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, CoreMatchers.`is`(EncryptedPrefsFormat.FORMAT_KEY_ENC)) - Assert.assertThat(prefsOut.metadata[PrefsMetadataKey.ENCRYPTION]?.status, CoreMatchers.`is`(PrefsStatus.OK)) + Assert.assertEquals(prefsOut.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK) + Assert.assertEquals(prefsOut.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, EncryptedPrefsFormat.FORMAT_KEY_ENC) + Assert.assertEquals(prefsOut.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.OK) } @Test @@ -128,11 +139,11 @@ class EncryptedPrefsFormatTest : TestBase() { val encryptedFormat = EncryptedPrefsFormat(resourceHelper, cryptoUtil, storage) val prefs = encryptedFormat.loadPreferences(getMockedFile(), "it-is-NOT-right-secret") - Assert.assertThat(prefs.values.size, CoreMatchers.`is`(0)) + Assert.assertEquals(prefs.values.size, 0) - Assert.assertThat(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, CoreMatchers.`is`(PrefsStatus.OK)) - Assert.assertThat(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, CoreMatchers.`is`(EncryptedPrefsFormat.FORMAT_KEY_ENC)) - Assert.assertThat(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, CoreMatchers.`is`(PrefsStatus.ERROR)) + Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK) + Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, EncryptedPrefsFormat.FORMAT_KEY_ENC) + Assert.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR) } @Test @@ -158,10 +169,10 @@ class EncryptedPrefsFormatTest : TestBase() { assumeAES256isSupported(cryptoUtil) // contents were not tampered and we can decrypt them - Assert.assertThat(prefs.values.size, CoreMatchers.`is`(2)) + Assert.assertEquals(prefs.values.size, 2) // but checksum fails on metadata, so overall security fails - Assert.assertThat(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, CoreMatchers.`is`(PrefsStatus.ERROR)) + Assert.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR) } @Test @@ -182,8 +193,8 @@ class EncryptedPrefsFormatTest : TestBase() { val encryptedFormat = EncryptedPrefsFormat(resourceHelper, cryptoUtil, storage) val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") - Assert.assertThat(prefs.values.size, CoreMatchers.`is`(0)) - Assert.assertThat(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, CoreMatchers.`is`(PrefsStatus.ERROR)) + Assert.assertEquals(prefs.values.size, 0) + Assert.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR) } @Test @@ -197,8 +208,8 @@ class EncryptedPrefsFormatTest : TestBase() { val encryptedFormat = EncryptedPrefsFormat(resourceHelper, cryptoUtil, storage) val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") - Assert.assertThat(prefs.values.size, CoreMatchers.`is`(0)) - Assert.assertThat(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, CoreMatchers.`is`(PrefsStatus.ERROR)) + Assert.assertEquals(prefs.values.size, 0) + Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.ERROR) } @Test(expected = PrefFormatError::class) diff --git a/core/build.gradle b/core/build.gradle index a539c9528d..20f8c6e5e6 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -46,17 +46,6 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } - testOptions { - unitTests { - returnDefaultValues = true - includeAndroidResources = true - - all { - maxParallelForks = 10 - forkEvery = 20 - } - } - } } @@ -64,4 +53,5 @@ dependencies { implementation project(':database') } -apply from: 'core_dependencies.gradle' \ No newline at end of file +apply from: 'core_dependencies.gradle' +apply from: "${project.rootDir}/gradle/test_dependencies.gradle" \ No newline at end of file diff --git a/core/core_dependencies.gradle b/core/core_dependencies.gradle index dbbb926ca6..cf40da5288 100644 --- a/core/core_dependencies.gradle +++ b/core/core_dependencies.gradle @@ -107,14 +107,4 @@ dependencies { // Phone checker api 'com.scottyab:rootbeer-lib:0.0.8' - - testImplementation "junit:junit:$junit_version" - testImplementation "org.mockito:mockito-core:${mockitoVersion}" - testImplementation "org.powermock:powermock-api-mockito2:${powermockVersion}" - testImplementation "org.powermock:powermock-module-junit4-rule-agent:${powermockVersion}" - testImplementation "org.powermock:powermock-module-junit4-rule:${powermockVersion}" - testImplementation "org.powermock:powermock-module-junit4:${powermockVersion}" - androidTestImplementation "androidx.test.ext:junit:$androidx_junit" - androidTestImplementation "androidx.test:rules:$androidx_rules" - testImplementation 'org.json:json:20201115' // Needed for JsonHelperTest } \ No newline at end of file diff --git a/dana/build.gradle b/dana/build.gradle index 7339a7d833..0c70d57791 100644 --- a/dana/build.gradle +++ b/dana/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' +apply plugin: 'com.hiya.jacoco-android' android { compileSdkVersion 28 @@ -49,8 +50,6 @@ dependencies { annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version" kapt "com.google.dagger:dagger-android-processor:$dagger_version" kapt "com.google.dagger:dagger-compiler:$dagger_version" - - testImplementation "junit:junit:$junit_version" - androidTestImplementation "androidx.test.ext:junit:$androidx_junit" - androidTestImplementation "androidx.test:rules:$androidx_rules" } + +apply from: "${project.rootDir}/gradle/test_dependencies.gradle" diff --git a/danar/build.gradle b/danar/build.gradle index cc8e1bce41..2ef2c3a88c 100644 --- a/danar/build.gradle +++ b/danar/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' +apply plugin: 'com.hiya.jacoco-android' android { compileSdkVersion 28 @@ -51,8 +52,6 @@ dependencies { annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version" kapt "com.google.dagger:dagger-android-processor:$dagger_version" kapt "com.google.dagger:dagger-compiler:$dagger_version" - - testImplementation "junit:junit:$junit_version" - androidTestImplementation "androidx.test.ext:junit:$androidx_junit" - androidTestImplementation "androidx.test:rules:$androidx_rules" } + +apply from: "${project.rootDir}/gradle/test_dependencies.gradle" diff --git a/danars/build.gradle b/danars/build.gradle index 8b407ce390..30ee674f9f 100644 --- a/danars/build.gradle +++ b/danars/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' +apply plugin: 'com.hiya.jacoco-android' android { compileSdkVersion 28 @@ -52,9 +53,6 @@ dependencies { annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version" kapt "com.google.dagger:dagger-android-processor:$dagger_version" kapt "com.google.dagger:dagger-compiler:$dagger_version" - - testImplementation "junit:junit:$junit_version" - androidTestImplementation "androidx.test.ext:junit:$androidx_junit" - androidTestImplementation "androidx.test:rules:$androidx_rules" } +apply from: "${project.rootDir}/gradle/test_dependencies.gradle" diff --git a/gradle/android_dependencies.gradle b/gradle/android_dependencies.gradle new file mode 100644 index 0000000000..e69de29bb2 diff --git a/gradle/test_dependencies.gradle b/gradle/test_dependencies.gradle new file mode 100644 index 0000000000..3a484c419c --- /dev/null +++ b/gradle/test_dependencies.gradle @@ -0,0 +1,35 @@ +dependencies { + testImplementation "junit:junit:$junit_version" + testImplementation 'org.json:json:20201115' + testImplementation "org.mockito:mockito-core:${mockitoVersion}" + testImplementation "org.powermock:powermock-api-mockito2:${powermockVersion}" + testImplementation "org.powermock:powermock-module-junit4-rule-agent:${powermockVersion}" + testImplementation "org.powermock:powermock-module-junit4-rule:${powermockVersion}" + testImplementation "org.powermock:powermock-module-junit4:${powermockVersion}" + testImplementation "joda-time:joda-time:$jodatime_version" + testImplementation('com.google.truth:truth:1.1.2') { + exclude group: "com.google.guava", module: "guava" + } + testImplementation "org.skyscreamer:jsonassert:1.5.0" + testImplementation "org.hamcrest:hamcrest-all:1.3" + + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0-alpha04' + androidTestImplementation "androidx.test.ext:junit:$androidx_junit" + androidTestImplementation "androidx.test:rules:$androidx_rules" + + androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0' +} + +android { + testOptions { + unitTests { + returnDefaultValues = true + includeAndroidResources = true + + all { + maxParallelForks = 10 + forkEvery = 20 + } + } + } +} \ No newline at end of file