decrypt test finally working

This commit is contained in:
Mohamed Zenadi 2017-08-02 00:15:45 +01:00 committed by Mohamed Zenadi
parent b145dfcf7f
commit ce44171a0b
14 changed files with 117 additions and 80 deletions

View file

@ -0,0 +1,3 @@
sub_pass
login: user
sub_extra

View file

@ -0,0 +1,3 @@
password
username: user
extra

Binary file not shown.

Binary file not shown.

View file

@ -9,6 +9,7 @@ import android.support.test.runner.AndroidJUnit4
import android.util.Log import android.util.Log
import com.zeapo.pwdstore.crypto.PgpActivity import com.zeapo.pwdstore.crypto.PgpActivity
import kotlinx.android.synthetic.main.decrypt_layout.* import kotlinx.android.synthetic.main.decrypt_layout.*
import org.apache.commons.io.FileUtils
import org.apache.commons.io.IOUtils import org.apache.commons.io.IOUtils
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull import org.junit.Assert.assertNotNull
@ -22,37 +23,47 @@ import java.io.IOException
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
@LargeTest @LargeTest
class WelcomeActivityTest { class DecryptTest {
lateinit var targetContext: Context
lateinit var testContext: Context
lateinit var activity: PgpActivity
val name = "sub"
val parentPath = "/category/"
lateinit var path: String
lateinit var repoPath: String
@Rule @JvmField @Rule @JvmField
var mActivityRule: ActivityTestRule<PgpActivity> = ActivityTestRule(PgpActivity::class.java, true, false) var mActivityRule: ActivityTestRule<PgpActivity> = ActivityTestRule<PgpActivity>(PgpActivity::class.java, true, false)
@Test fun init() {
fun pathShouldDecompose() { targetContext = InstrumentationRegistry.getInstrumentation().targetContext
val path = "/data/my.app.com/files/store/cat1/name.gpg" testContext = InstrumentationRegistry.getContext()
val repoPath = "/data/my.app.com/files/store" copyAssets("encrypted-store", File(targetContext.filesDir, "test-store").absolutePath)
assertEquals("/cat1/name.gpg", PgpActivity.getRelativePath(path, repoPath)) repoPath = File(targetContext.filesDir, "test-store").absolutePath
assertEquals("/cat1/", PgpActivity.getParentPath(path, repoPath)) path = "$repoPath/$parentPath/$name.gpg".replace("//", "/")
assertEquals("name", PgpActivity.getName(path, repoPath))
assertEquals("name", PgpActivity.getName(path, "$repoPath/"))
}
@Test val intent = Intent(targetContext, PgpActivity::class.java)
fun activityShouldShowName() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val name = "name"
val parentPath = "/cat1/"
val repoPath = "${context.filesDir}/store/"
val path = "$repoPath/cat1/name.gpg"
val intent = Intent(context, PgpActivity::class.java)
intent.putExtra("OPERATION", "DECRYPT") intent.putExtra("OPERATION", "DECRYPT")
intent.putExtra("FILE_PATH", path) intent.putExtra("FILE_PATH", path)
intent.putExtra("REPO_PATH", repoPath) intent.putExtra("REPO_PATH", repoPath)
copyAssets(context, "store", context.filesDir.absolutePath) activity = mActivityRule.launchActivity(intent)
}
val activity: PgpActivity = mActivityRule.launchActivity(intent) @Test
fun pathShouldDecompose() {
init()
assertEquals("/category/sub.gpg", PgpActivity.getRelativePath(path, repoPath))
assertEquals("/category/", PgpActivity.getParentPath(path, repoPath))
assertEquals("sub", PgpActivity.getName(path, repoPath))
assertEquals("sub", PgpActivity.getName(path, "$repoPath/"))
}
@Test
fun activityShouldShowName() {
init()
val categoryView = activity.crypto_password_category_decrypt val categoryView = activity.crypto_password_category_decrypt
assertNotNull(categoryView) assertNotNull(categoryView)
@ -63,17 +74,37 @@ class WelcomeActivityTest {
assertEquals(name, nameView.text) assertEquals(name, nameView.text)
} }
@Test
fun shouldDecrypt() {
init()
activity.onBound(null)
val clearPass = IOUtils.toString(
IOUtils.toByteArray(testContext.assets.open("clear-store/category/sub")),
Charsets.UTF_8.name()
)
val passEntry = PasswordEntry(clearPass)
assertEquals(passEntry.password, activity.crypto_password_show.text)
assertEquals(passEntry.username, activity.crypto_username_show.text.toString())
assertEquals(passEntry.extraContent, activity.crypto_extra_show.text.toString())
}
companion object { companion object {
fun copyAssets(context: Context, source: String, destination: String) { fun copyAssets(source: String, destination: String) {
val assetManager = context.assets FileUtils.forceMkdir(File(destination))
FileUtils.cleanDirectory(File(destination))
val testContext = InstrumentationRegistry.getContext()
val assetManager = testContext.assets
val files: Array<String>? = assetManager.list(source) val files: Array<String>? = assetManager.list(source)
files?.map { filename -> files?.map { filename ->
val destPath = "$destination/$filename" val destPath = "$destination/$filename"
val sourcePath = "$source/$filename" val sourcePath = "$source/$filename"
if (assetManager.list(filename).isNotEmpty()) {
File(destPath).mkdir() if (assetManager.list(sourcePath).isNotEmpty()) {
copyAssets(context, "$source/$filename", destPath) FileUtils.forceMkdir(File(destination, filename))
copyAssets("$source/$filename", destPath)
} else { } else {
try { try {
val input = assetManager.open(sourcePath) val input = assetManager.open(sourcePath)

View file

@ -186,58 +186,58 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) {
RESULT_CODE_SUCCESS -> { RESULT_CODE_SUCCESS -> {
try { try {
// val showPassword = settings.getBoolean("show_password", true) val showPassword = settings.getBoolean("show_password", true)
// val showExtraContent = settings.getBoolean("show_extra_content", true) val showExtraContent = settings.getBoolean("show_extra_content", true)
//
// crypto_container_decrypt.visibility = View.VISIBLE crypto_container_decrypt.visibility = View.VISIBLE
//
// val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf") val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf")
// val entry = PasswordEntry(oStream) val entry = PasswordEntry(oStream)
//
// passwordEntry = entry passwordEntry = entry
//
// if (operation == "EDIT") { if (operation == "EDIT") {
// editPassword() editPassword()
// return@executeApiAsync return@executeApiAsync
// } }
//
// crypto_password_show.typeface = monoTypeface crypto_password_show.typeface = monoTypeface
// crypto_password_show.text = entry.password crypto_password_show.text = entry.password
//
// crypto_password_toggle_show.visibility = if (showPassword) View.GONE else View.VISIBLE crypto_password_toggle_show.visibility = if (showPassword) View.GONE else View.VISIBLE
// crypto_password_show.transformationMethod = if (showPassword) { crypto_password_show.transformationMethod = if (showPassword) {
// null null
// } else { } else {
// HoldToShowPasswordTransformation( HoldToShowPasswordTransformation(
// crypto_password_toggle_show, crypto_password_toggle_show,
// Runnable { crypto_password_show.text = entry.password } Runnable { crypto_password_show.text = entry.password }
// ) )
// } }
//
// if (entry.hasExtraContent()) { if (entry.hasExtraContent()) {
// crypto_extra_show_layout.visibility = if (showExtraContent) View.VISIBLE else View.GONE crypto_extra_show_layout.visibility = if (showExtraContent) View.VISIBLE else View.GONE
//
// crypto_extra_show.typeface = monoTypeface crypto_extra_show.typeface = monoTypeface
// crypto_extra_show.text = entry.extraContent crypto_extra_show.text = entry.extraContent
//
// if (entry.hasUsername()) { if (entry.hasUsername()) {
// crypto_username_show.visibility = View.VISIBLE crypto_username_show.visibility = View.VISIBLE
// crypto_username_show_label.visibility = View.VISIBLE crypto_username_show_label.visibility = View.VISIBLE
// crypto_copy_username.visibility = View.VISIBLE crypto_copy_username.visibility = View.VISIBLE
//
// crypto_copy_username.setOnClickListener { copyUsernameToClipBoard(entry.username) } crypto_copy_username.setOnClickListener { copyUsernameToClipBoard(entry.username) }
// crypto_username_show.typeface = monoTypeface crypto_username_show.typeface = monoTypeface
// crypto_username_show.text = entry.username crypto_username_show.text = entry.username
// } else { } else {
// crypto_username_show.visibility = View.GONE crypto_username_show.visibility = View.GONE
// crypto_username_show_label.visibility = View.GONE crypto_username_show_label.visibility = View.GONE
// crypto_copy_username.visibility = View.GONE crypto_copy_username.visibility = View.GONE
// } }
// } }
//
// if (settings.getBoolean("copy_on_decrypt", true)) { if (settings.getBoolean("copy_on_decrypt", true)) {
// copyPasswordToClipBoard() copyPasswordToClipBoard()
// } }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "An Exception occurred", e) Log.e(TAG, "An Exception occurred", e)
} }