From ce44171a0b0dd66f4e8a0cf4e95e62c337a0c40e Mon Sep 17 00:00:00 2001 From: Mohamed Zenadi Date: Wed, 2 Aug 2017 00:15:45 +0100 Subject: decrypt test finally working --- .../androidTest/assets/clear-store/category/sub | 3 + app/src/androidTest/assets/clear-store/pass | 3 + .../assets/encrypted-store/category/sub.gpg | Bin 0 -> 362 bytes .../androidTest/assets/encrypted-store/pass.gpg | Bin 0 -> 361 bytes app/src/androidTest/assets/private_key | Bin 0 -> 2575 bytes app/src/androidTest/assets/store/dir1/f1.gpg | 0 app/src/androidTest/assets/store/dir1/f11.gpg | 0 app/src/androidTest/assets/store/dir2/f2.gpg | 0 app/src/androidTest/assets/store/dir3/dir4/f4.gpg | 0 app/src/androidTest/assets/store/dir3/f3.gpg | 0 app/src/androidTest/assets/store/name1.gpg | 0 app/src/androidTest/assets/store/name2.gpg | 0 .../java/com/zeapo/pwdstore/DecryptTest.kt | 85 ++++++++++++++------- 13 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 app/src/androidTest/assets/clear-store/category/sub create mode 100644 app/src/androidTest/assets/clear-store/pass create mode 100644 app/src/androidTest/assets/encrypted-store/category/sub.gpg create mode 100644 app/src/androidTest/assets/encrypted-store/pass.gpg create mode 100644 app/src/androidTest/assets/private_key delete mode 100644 app/src/androidTest/assets/store/dir1/f1.gpg delete mode 100644 app/src/androidTest/assets/store/dir1/f11.gpg delete mode 100644 app/src/androidTest/assets/store/dir2/f2.gpg delete mode 100644 app/src/androidTest/assets/store/dir3/dir4/f4.gpg delete mode 100644 app/src/androidTest/assets/store/dir3/f3.gpg delete mode 100644 app/src/androidTest/assets/store/name1.gpg delete mode 100644 app/src/androidTest/assets/store/name2.gpg (limited to 'app/src/androidTest') diff --git a/app/src/androidTest/assets/clear-store/category/sub b/app/src/androidTest/assets/clear-store/category/sub new file mode 100644 index 00000000..eb9106c1 --- /dev/null +++ b/app/src/androidTest/assets/clear-store/category/sub @@ -0,0 +1,3 @@ +sub_pass +login: user +sub_extra diff --git a/app/src/androidTest/assets/clear-store/pass b/app/src/androidTest/assets/clear-store/pass new file mode 100644 index 00000000..8415d1ab --- /dev/null +++ b/app/src/androidTest/assets/clear-store/pass @@ -0,0 +1,3 @@ +password +username: user +extra diff --git a/app/src/androidTest/assets/encrypted-store/category/sub.gpg b/app/src/androidTest/assets/encrypted-store/category/sub.gpg new file mode 100644 index 00000000..dded0132 Binary files /dev/null and b/app/src/androidTest/assets/encrypted-store/category/sub.gpg differ diff --git a/app/src/androidTest/assets/encrypted-store/pass.gpg b/app/src/androidTest/assets/encrypted-store/pass.gpg new file mode 100644 index 00000000..53c614c4 Binary files /dev/null and b/app/src/androidTest/assets/encrypted-store/pass.gpg differ diff --git a/app/src/androidTest/assets/private_key b/app/src/androidTest/assets/private_key new file mode 100644 index 00000000..7141d6f4 Binary files /dev/null and b/app/src/androidTest/assets/private_key differ diff --git a/app/src/androidTest/assets/store/dir1/f1.gpg b/app/src/androidTest/assets/store/dir1/f1.gpg deleted file mode 100644 index e69de29b..00000000 diff --git a/app/src/androidTest/assets/store/dir1/f11.gpg b/app/src/androidTest/assets/store/dir1/f11.gpg deleted file mode 100644 index e69de29b..00000000 diff --git a/app/src/androidTest/assets/store/dir2/f2.gpg b/app/src/androidTest/assets/store/dir2/f2.gpg deleted file mode 100644 index e69de29b..00000000 diff --git a/app/src/androidTest/assets/store/dir3/dir4/f4.gpg b/app/src/androidTest/assets/store/dir3/dir4/f4.gpg deleted file mode 100644 index e69de29b..00000000 diff --git a/app/src/androidTest/assets/store/dir3/f3.gpg b/app/src/androidTest/assets/store/dir3/f3.gpg deleted file mode 100644 index e69de29b..00000000 diff --git a/app/src/androidTest/assets/store/name1.gpg b/app/src/androidTest/assets/store/name1.gpg deleted file mode 100644 index e69de29b..00000000 diff --git a/app/src/androidTest/assets/store/name2.gpg b/app/src/androidTest/assets/store/name2.gpg deleted file mode 100644 index e69de29b..00000000 diff --git a/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt b/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt index 52e1e65d..eba20e1d 100644 --- a/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt +++ b/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt @@ -9,6 +9,7 @@ import android.support.test.runner.AndroidJUnit4 import android.util.Log import com.zeapo.pwdstore.crypto.PgpActivity import kotlinx.android.synthetic.main.decrypt_layout.* +import org.apache.commons.io.FileUtils import org.apache.commons.io.IOUtils import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull @@ -22,37 +23,47 @@ import java.io.IOException @RunWith(AndroidJUnit4::class) @LargeTest -class WelcomeActivityTest { - @Rule @JvmField - var mActivityRule: ActivityTestRule = ActivityTestRule(PgpActivity::class.java, true, false) +class DecryptTest { + lateinit var targetContext: Context + lateinit var testContext: Context + lateinit var activity: PgpActivity - @Test - fun pathShouldDecompose() { - val path = "/data/my.app.com/files/store/cat1/name.gpg" - val repoPath = "/data/my.app.com/files/store" - assertEquals("/cat1/name.gpg", PgpActivity.getRelativePath(path, repoPath)) - assertEquals("/cat1/", PgpActivity.getParentPath(path, repoPath)) - assertEquals("name", PgpActivity.getName(path, repoPath)) - assertEquals("name", PgpActivity.getName(path, "$repoPath/")) - } + val name = "sub" + val parentPath = "/category/" + lateinit var path: String + lateinit var repoPath: String - @Test - fun activityShouldShowName() { - val context = InstrumentationRegistry.getInstrumentation().targetContext - val name = "name" - val parentPath = "/cat1/" - val repoPath = "${context.filesDir}/store/" - val path = "$repoPath/cat1/name.gpg" + @Rule @JvmField + var mActivityRule: ActivityTestRule = ActivityTestRule(PgpActivity::class.java, true, false) + fun init() { + targetContext = InstrumentationRegistry.getInstrumentation().targetContext + testContext = InstrumentationRegistry.getContext() + copyAssets("encrypted-store", File(targetContext.filesDir, "test-store").absolutePath) + repoPath = File(targetContext.filesDir, "test-store").absolutePath + path = "$repoPath/$parentPath/$name.gpg".replace("//", "/") - val intent = Intent(context, PgpActivity::class.java) + val intent = Intent(targetContext, PgpActivity::class.java) intent.putExtra("OPERATION", "DECRYPT") intent.putExtra("FILE_PATH", path) intent.putExtra("REPO_PATH", repoPath) - copyAssets(context, "store", context.filesDir.absolutePath) + activity = 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/")) + } - val activity: PgpActivity = mActivityRule.launchActivity(intent) + @Test + fun activityShouldShowName() { + init() val categoryView = activity.crypto_password_category_decrypt assertNotNull(categoryView) @@ -63,17 +74,37 @@ class WelcomeActivityTest { 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 { - fun copyAssets(context: Context, source: String, destination: String) { - val assetManager = context.assets + fun copyAssets(source: String, destination: String) { + FileUtils.forceMkdir(File(destination)) + FileUtils.cleanDirectory(File(destination)) + + val testContext = InstrumentationRegistry.getContext() + val assetManager = testContext.assets val files: Array? = assetManager.list(source) files?.map { filename -> val destPath = "$destination/$filename" val sourcePath = "$source/$filename" - if (assetManager.list(filename).isNotEmpty()) { - File(destPath).mkdir() - copyAssets(context, "$source/$filename", destPath) + + if (assetManager.list(sourcePath).isNotEmpty()) { + FileUtils.forceMkdir(File(destination, filename)) + copyAssets("$source/$filename", destPath) } else { try { val input = assetManager.open(sourcePath) -- cgit v1.2.3