File size: 16,807 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | /*
* 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.content.Intent
import android.os.Environment
import androidx.appcompat.widget.AppCompatCheckBox
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.text.HtmlCompat
import androidx.core.text.HtmlCompat.FROM_HTML_MODE_COMPACT
import androidx.preference.PreferenceManager
import com.afollestad.materialdialogs.DialogAction
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.internal.MDButton
import com.amaze.filemanager.R
import com.amaze.filemanager.application.AppConfig
import com.amaze.filemanager.asynchronous.services.EncryptService.TAG_AESCRYPT
import com.amaze.filemanager.asynchronous.services.EncryptService.TAG_ENCRYPT_TARGET
import com.amaze.filemanager.asynchronous.services.EncryptService.TAG_OPEN_MODE
import com.amaze.filemanager.asynchronous.services.EncryptService.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.AESCRYPT_EXTENSION
import com.amaze.filemanager.filesystem.files.CryptUtil.CRYPT_EXTENSION
import com.amaze.filemanager.filesystem.files.EncryptDecryptUtils
import com.amaze.filemanager.test.getString
import com.amaze.filemanager.ui.activities.MainActivity
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants
import com.amaze.filemanager.ui.views.WarnableTextInputLayout
import com.google.android.material.textfield.TextInputEditText
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
import org.robolectric.shadows.ShadowDialog
import java.io.File
import kotlin.random.Random
/**
* Logic test for [EncryptAuthenticateDialog].
*/
class EncryptAuthenticateDialogTest : AbstractEncryptDialogTests() {
private val randomizer = Random(System.currentTimeMillis())
private lateinit var file: File
private lateinit var tilFileSaveAs: WarnableTextInputLayout
private lateinit var tilEncryptPassword: WarnableTextInputLayout
private lateinit var tilEncryptPasswordConfirm: WarnableTextInputLayout
private lateinit var editTextFileSaveAs: TextInputEditText
private lateinit var editTextEncryptPassword: TextInputEditText
private lateinit var editTextEncryptPasswordConfirm: TextInputEditText
private lateinit var checkboxUseAze: AppCompatCheckBox
private lateinit var textViewCryptInfo: AppCompatTextView
private lateinit var okButton: MDButton
/**
* MainActivity setup.
*/
@Before
override fun setUp() {
super.setUp()
file =
File(
Environment.getExternalStorageDirectory(),
RandomPathGenerator.generateRandomPath(
randomizer,
16,
),
)
}
/**
* Test simple workflow. Just enter a password and off it go.
*/
@Test
@Suppress("StringLiteralDuplication")
fun testSimpleWorkflow() {
performTest(
{ _, _, _ ->
assertEquals("${file.name}$AESCRYPT_EXTENSION", editTextFileSaveAs.text.toString())
editTextEncryptPassword.setText("abcdefgh")
editTextEncryptPasswordConfirm.setText("abcdefgh")
assertTrue(okButton.isEnabled)
okButton.performClick()
},
object : EncryptDecryptUtils.EncryptButtonCallbackInterface {
override fun onButtonPressed(
intent: Intent,
password: String,
) {
assertEquals(file.absolutePath, intent.getStringExtra(TAG_SOURCE))
assertTrue(intent.getBooleanExtra(TAG_AESCRYPT, false))
assertEquals(
"${file.name}$AESCRYPT_EXTENSION",
intent.getStringExtra(TAG_ENCRYPT_TARGET),
)
assertEquals("abcdefgh", password)
}
},
)
}
/**
* Test simple workflow with aze encryption checkbox checked. Also renamed target filename
*/
@Test
fun testAzeWorkflow() {
performTest(
{ _, _, _ ->
assertEquals("${file.name}$AESCRYPT_EXTENSION", editTextFileSaveAs.text.toString())
editTextEncryptPassword.setText("abcdefgh")
editTextEncryptPasswordConfirm.setText("abcdefgh")
assertTrue(okButton.isEnabled)
checkboxUseAze.isChecked = true
assertEquals("${file.name}$CRYPT_EXTENSION", editTextFileSaveAs.text.toString())
editTextFileSaveAs.setText("testfile$CRYPT_EXTENSION")
okButton.performClick()
},
object : EncryptDecryptUtils.EncryptButtonCallbackInterface {
override fun onButtonPressed(
intent: Intent,
password: String,
) {
assertEquals(file.absolutePath, intent.getStringExtra(TAG_SOURCE))
assertFalse(intent.getBooleanExtra(TAG_AESCRYPT, true))
assertEquals(
"testfile$CRYPT_EXTENSION",
intent.getStringExtra(TAG_ENCRYPT_TARGET),
)
assertEquals("abcdefgh", password)
}
},
)
}
/**
* Test password field validation.
*/
@Test
fun testPasswordValidations() {
performTest({ _, _, _ ->
editTextEncryptPassword.setText("abc")
editTextEncryptPasswordConfirm.setText("def")
assertFalse(okButton.isEnabled)
assertEquals(
getString(R.string.password_no_match),
tilEncryptPasswordConfirm.error,
)
editTextEncryptPassword.setText("")
editTextEncryptPasswordConfirm.setText("")
assertFalse(okButton.isEnabled)
assertEquals(getString(R.string.field_empty), tilEncryptPassword.error)
editTextEncryptPassword.setText(" ")
editTextEncryptPasswordConfirm.setText(" ")
assertFalse(okButton.isEnabled)
assertEquals(getString(R.string.field_empty), tilEncryptPassword.error)
editTextEncryptPassword.setText("abcdef")
editTextEncryptPasswordConfirm.setText("abcdef")
assertTrue(okButton.isEnabled)
editTextFileSaveAs.setText("")
assertFalse(okButton.isEnabled)
})
}
/**
* Test filename field validations.
*/
@Test
fun testFilenameValidations() {
performTest({ _, _, _ ->
editTextEncryptPassword.setText("abc")
editTextEncryptPasswordConfirm.setText("abc")
editTextFileSaveAs.setText("${file.name}.error")
assertFalse(okButton.isEnabled)
assertEquals(
getString(R.string.encrypt_file_must_end_with_aes),
tilFileSaveAs.error,
)
editTextFileSaveAs.setText("${file.name}.aze")
assertFalse(okButton.isEnabled)
assertEquals(
getString(R.string.encrypt_file_must_end_with_aes),
tilFileSaveAs.error,
)
checkboxUseAze.isChecked = true
assertTrue(okButton.isEnabled)
assertNull(tilFileSaveAs.error)
editTextFileSaveAs.setText("${file.name}.error")
assertFalse(okButton.isEnabled)
assertEquals(
getString(R.string.encrypt_file_must_end_with_aze),
tilFileSaveAs.error,
)
editTextFileSaveAs.setText("${file.name}.aes")
assertFalse(okButton.isEnabled)
assertEquals(
getString(R.string.encrypt_file_must_end_with_aze),
tilFileSaveAs.error,
)
editTextFileSaveAs.setText("")
assertFalse(okButton.isEnabled)
assertEquals(
getString(R.string.field_empty),
tilFileSaveAs.error,
)
editTextFileSaveAs.setText(" ")
assertFalse(okButton.isEnabled)
assertEquals(
getString(R.string.field_empty),
tilFileSaveAs.error,
)
editTextFileSaveAs.setText("${file.name}.aze")
assertTrue(okButton.isEnabled)
assertNull(tilFileSaveAs.error)
})
}
/**
* To test how would Amaze legacy encryption dialog pops up.
*/
@Test
fun testAzeCryptDialog() {
performTest({ _, _, _ ->
checkboxUseAze.isChecked = true
assertEquals(
HtmlCompat.fromHtml(
getString(R.string.encrypt_option_use_azecrypt_desc),
FROM_HTML_MODE_COMPACT,
)
.toString(),
textViewCryptInfo.text.toString(),
)
assertTrue(ShadowDialog.getShownDialogs().size == 2)
assertTrue(ShadowDialog.getLatestDialog() is MaterialDialog)
(ShadowDialog.getLatestDialog() as MaterialDialog).run {
assertEquals(getString(R.string.warning), titleView.text)
assertEquals(getString(R.string.crypt_warning_key), contentView?.text.toString())
assertEquals(
getString(R.string.warning_never_show),
getActionButton(DialogAction.NEGATIVE).text,
)
assertEquals(
getString(R.string.warning_confirm),
getActionButton(DialogAction.POSITIVE).text,
)
assertTrue(getActionButton(DialogAction.POSITIVE).performClick())
}
assertEquals(2, ShadowDialog.getShownDialogs().size)
assertFalse(ShadowDialog.getLatestDialog().isShowing)
assertTrue(true == editTextFileSaveAs.text?.endsWith(CRYPT_EXTENSION))
checkboxUseAze.isChecked = false
assertEquals(
HtmlCompat.fromHtml(
getString(R.string.encrypt_option_use_aescrypt_desc),
FROM_HTML_MODE_COMPACT,
)
.toString(),
textViewCryptInfo.text.toString(),
)
assertEquals(2, ShadowDialog.getShownDialogs().size)
assertFalse(ShadowDialog.getLatestDialog().isShowing)
assertTrue(true == editTextFileSaveAs.text?.endsWith(AESCRYPT_EXTENSION))
checkboxUseAze.isChecked = true
assertEquals(
HtmlCompat.fromHtml(
getString(R.string.encrypt_option_use_azecrypt_desc),
FROM_HTML_MODE_COMPACT,
)
.toString(),
textViewCryptInfo.text.toString(),
)
assertEquals(3, ShadowDialog.getShownDialogs().size)
assertTrue(ShadowDialog.getLatestDialog().isShowing)
assertTrue(true == editTextFileSaveAs.text?.endsWith(CRYPT_EXTENSION))
(ShadowDialog.getLatestDialog() as MaterialDialog)
.getActionButton(DialogAction.NEGATIVE).performClick()
assertEquals(3, ShadowDialog.getShownDialogs().size)
assertFalse(ShadowDialog.getLatestDialog().isShowing)
assertTrue(
PreferenceManager.getDefaultSharedPreferences(AppConfig.getInstance())
.getBoolean(PreferencesConstants.PREFERENCE_CRYPT_WARNING_REMEMBER, false),
)
checkboxUseAze.isChecked = false
assertEquals(3, ShadowDialog.getShownDialogs().size) // no new dialog
checkboxUseAze.isChecked = true
assertEquals(3, ShadowDialog.getShownDialogs().size)
})
}
/**
* Test jump across textfields should keep button status.
*/
@Test
fun testTextfieldFocusChanges() {
performTest({ _, _, _ ->
editTextFileSaveAs.requestFocus()
assertFalse(okButton.isEnabled)
editTextEncryptPasswordConfirm.requestFocus()
assertFalse(okButton.isEnabled)
editTextEncryptPassword.requestFocus()
assertFalse(okButton.isEnabled)
})
}
/**
* prepare MainActivity scenario. Also populate dialog fields to test
*
* @param testContent main test function
* @param callback optional callback for result verification
*/
private fun performTest(
testContent: (
dialog: MaterialDialog,
intent: Intent,
activity: MainActivity,
) -> Unit,
callback: EncryptDecryptUtils.EncryptButtonCallbackInterface =
object : EncryptDecryptUtils.EncryptButtonCallbackInterface {},
) {
scenario.onActivity { activity ->
Intent().putExtra(TAG_SOURCE, HybridFileParcelable(file.absolutePath))
.putExtra(TAG_OPEN_MODE, OpenMode.FILE).let { intent ->
EncryptAuthenticateDialog.show(
activity,
intent,
activity,
activity.appTheme,
callback,
)
ShadowDialog.getLatestDialog()?.run {
assertTrue(this is MaterialDialog)
(this as MaterialDialog).let {
editTextFileSaveAs =
findViewById<TextInputEditText>(
R.id.edit_text_encrypt_save_as,
)
editTextEncryptPassword =
findViewById<TextInputEditText>(
R.id.edit_text_dialog_encrypt_password,
)
editTextEncryptPasswordConfirm =
findViewById<TextInputEditText>(
R.id.edit_text_dialog_encrypt_password_confirm,
)
tilFileSaveAs =
findViewById<WarnableTextInputLayout>(
R.id.til_encrypt_save_as,
)
tilEncryptPassword =
findViewById<WarnableTextInputLayout>(
R.id.til_encrypt_password,
)
tilEncryptPasswordConfirm =
findViewById<WarnableTextInputLayout>(
R.id.til_encrypt_password,
)
checkboxUseAze = findViewById<AppCompatCheckBox>(R.id.checkbox_use_aze)
textViewCryptInfo =
findViewById<AppCompatTextView>(
R.id.text_view_crypt_info,
)
okButton = getActionButton(DialogAction.POSITIVE)
assertFalse(okButton.isEnabled)
assertTrue(true == editTextFileSaveAs.text?.startsWith(file.name))
testContent.invoke(it, intent, activity)
}
} ?: fail("Dialog cannot be seen?")
}
}
}
}
|