File size: 6,387 Bytes
5bbfb59 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | /*
* Copyright (C) 2014-2022 Arpit Khurana <arpitkh96@gmail.com>, Vishal Nehra <vishalmeham2@gmail.com>,
* Emmanuel Messulam<emmanuelbendavid@gmail.com>, Raymond Lai <airwave209gt at gmail.com> and Contributors.
*
* This file is part of Amaze File Manager.
*
* Amaze File Manager 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 com.amaze.filemanager.ui.dialogs
import android.Manifest.permission.USE_FINGERPRINT
import android.content.Intent
import android.hardware.fingerprint.FingerprintManager
import android.os.Build.VERSION_CODES.M
import android.os.Build.VERSION_CODES.P
import android.os.Environment
import androidx.test.filters.SdkSuppress
import com.afollestad.materialdialogs.MaterialDialog
import com.amaze.filemanager.application.AppConfig
import com.amaze.filemanager.asynchronous.services.DecryptService.TAG_DECRYPT_PATH
import com.amaze.filemanager.asynchronous.services.DecryptService.TAG_OPEN_MODE
import com.amaze.filemanager.asynchronous.services.DecryptService.TAG_SOURCE
import com.amaze.filemanager.fileoperations.filesystem.OpenMode
import com.amaze.filemanager.filesystem.HybridFileParcelable
import com.amaze.filemanager.filesystem.RandomPathGenerator
import com.amaze.filemanager.filesystem.files.CryptUtil.CRYPT_EXTENSION
import com.amaze.filemanager.filesystem.files.EncryptDecryptUtils
import com.amaze.filemanager.shadows.ShadowFileUtils
import com.amaze.filemanager.shadows.ShadowMultiDex
import com.amaze.filemanager.test.ShadowTabHandler
import com.amaze.filemanager.utils.security.SecretKeygen
import io.mockk.every
import io.mockk.mockkObject
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowDialog
import org.robolectric.shadows.ShadowFingerprintManager
import java.io.File
import javax.crypto.spec.SecretKeySpec
import kotlin.random.Random
/**
* Unit test for [DecryptFingerprintDialog].
*/
@Config(
shadows = [
ShadowMultiDex::class,
ShadowTabHandler::class,
ShadowFileUtils::class,
ShadowFingerprintManager::class,
],
sdk = [P],
)
class DecryptFingerprintDialogTest : AbstractEncryptDialogTests() {
private lateinit var file: File
/**
* MainActivity setup.
*/
@Before
override fun setUp() {
super.setUp()
file =
File(
Environment.getExternalStorageDirectory(),
RandomPathGenerator.generateRandomPath(
randomizer,
16,
) + CRYPT_EXTENSION,
)
initMockSecretKeygen()
shadowOf(AppConfig.getInstance()).grantPermissions(USE_FINGERPRINT)
}
/**
* Test fingerprint authentication success scenario.
*/
@Test
@SdkSuppress(minSdkVersion = M)
fun testDecryptFingerprintDialogSuccess() {
performTest(
testContent = {
shadowOf(
AppConfig.getInstance().getSystemService(FingerprintManager::class.java),
).run {
setDefaultFingerprints(1)
setIsHardwareDetected(true)
authenticationSucceeds()
}
},
callback =
object : EncryptDecryptUtils.DecryptButtonCallbackInterface {
override fun confirm(intent: Intent) = assertTrue(true)
override fun failed() = fail("Should never called")
},
)
}
/**
* Test fingerprint authentication failure scenario.
*/
@Test
@SdkSuppress(minSdkVersion = M)
fun testDecryptFingerprintDialogFailed() {
performTest(
testContent = {
shadowOf(
AppConfig.getInstance().getSystemService(FingerprintManager::class.java),
).run {
setDefaultFingerprints(1)
setIsHardwareDetected(true)
authenticationFails()
}
},
callback =
object : EncryptDecryptUtils.DecryptButtonCallbackInterface {
override fun confirm(intent: Intent) = fail("Should never called")
override fun failed() = assertTrue(true)
},
)
}
private fun performTest(
testContent: () -> Unit,
callback: EncryptDecryptUtils.DecryptButtonCallbackInterface =
object : EncryptDecryptUtils.DecryptButtonCallbackInterface {},
) {
scenario.onActivity { activity ->
DecryptFingerprintDialog.show(
activity,
activity,
Intent().putExtra(TAG_SOURCE, HybridFileParcelable(file.absolutePath))
.putExtra(TAG_OPEN_MODE, OpenMode.FILE)
.putExtra(
TAG_DECRYPT_PATH,
Environment.getExternalStorageDirectory().absolutePath,
),
callback,
)
ShadowDialog.getLatestDialog()?.run {
assertTrue(this is MaterialDialog)
(this as MaterialDialog).let {
testContent.invoke()
}
}
}
}
companion object {
private val randomizer = Random(System.currentTimeMillis())
private val key = SecretKeySpec(randomizer.nextBytes(16), "AES")
/**
* Mock [SecretKeygen] since Robolectric does not have AndroidKeyStore.
*/
fun initMockSecretKeygen() {
mockkObject(SecretKeygen)
every { SecretKeygen.getSecretKey() } returns key
}
}
}
|