From ce44171a0b0dd66f4e8a0cf4e95e62c337a0c40e Mon Sep 17 00:00:00 2001 From: Mohamed Zenadi Date: Wed, 2 Aug 2017 00:15:45 +0100 Subject: [PATCH] decrypt test finally working --- .../assets/clear-store/category/sub | 3 + app/src/androidTest/assets/clear-store/pass | 3 + .../assets/encrypted-store/category/sub.gpg | Bin 0 -> 362 bytes .../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 .../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 | 87 ++++++++++----- .../com/zeapo/pwdstore/crypto/PgpActivity.kt | 104 +++++++++--------- 14 files changed, 117 insertions(+), 80 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 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 0000000000000000000000000000000000000000..dded01322b92d423490cce7eb767b23db4f1dbc7 GIT binary patch literal 362 zcmV-w0hRuR0Sp7O{_RQ!p)5}U2mB5YbdA{Y2(w>bxC&7Kr742Y8m_0T3PZ1ch`7T=uZVzDWFC?{1qS<{4Vl!Fx0Vc{V2NjSzCz`DB7?;@+7#H$}+xn>gt= zj|&4)P2t*v4661i8qOI#Dp9sB{sOGfjg;%1uo%1soSnuIAK}ex#j?j`BJM;?wL+?V z_ji#iBY?co9|!n~zw>hGapS!^sjP>grHEyjo}J5Y zrFlqPNV#113a879AUlQfpq!QQ6Nk$ucGx1=RMm~8b8N-sRuWk>Ri{b- z?X#_<#|mLAPlYnMXboh0RcM-D`+BlBUa>(|;4b5JFj=H2@`WVsap(0HG>k`3nXxi0 z=@k9GKtn`Bn5*$8EwC8vmx1H z0weXSJ1!Xg<-Z75>m;r9+P&Eiv<{KcXCSDFS=#K5dYahb%SXz_sE+eK}J8J5>`@L51TtBvkNz6~rvV0cyC3P%IX zXd_%SuFe|#m8U-*JVd0W7PZN}VHlS5D=v?)Y*Ut*g|TbTR|@;_*lg$^ zAMi#+vND_(p#%Km-Ahb@QQiLmq=+i#pF{Tm?Smokpuyz% z{je<=n~d;Y*IU8(>dl(1fFj~kSqmREQ}(e-ZfDqp=7{$0zxWDQ@!nfOGmq<)7|Zq? zj~XRJU3Wl;=O4z6!f%96maSlH5~TKj;swX|1K|5sR)%*yrxr4qKTu;aj{EAd{?*S< zse3O`ySj1An8_(=8I;Wmo~QPif!;lzK@X2wAlTY1!W0 zDwS#RM438?(MV+4h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tmi0h&h~(qKOp-DAIAm)Uf}#(qbV4h)M<(I`#x&} zZE-B(sVY`tXh5xGKE*XnQUT)CogB5gzN;Ho$wUs1<`x>CKL^Wde5l#h*^hx)M(*!W zm;dA~ro0ZDXUbZhu}-boW$y8t^1*2KE3G2%u~`M#%?}Y75rqJ+PdI{fu^Fhh(1v!8XA2aUp9*dnA=LDrVg@}ebt*Fkm{=2 z_#S&#LI+|xQ}V$VWXZM|JjSBHOE0Ca^O_d;f~~g37(5RLaTPY5F2NaV%%oquG&9U! z-xdR8i2)x37y$?X2?YXKfdcCS8w>yn2@nP5Z_mOq7t2(#2meA3BeNWX_lR+zKAl`k zMW34OTYDlr#lB9zN>4Odq1Ea8-O7sEl6}5zceyc5J-&=}^n}X`CjRAXKGx3OVKyW8(qsD6d1F^rUUCtpb+0S=+6cFjtnA zqFgfX$_Lm3YiJ lG~Y5+#r_}TUn`Wj-`(_E3Trvw#Fa)hn>(?Xe3c$>VNx = ActivityTestRule(PgpActivity::class.java, true, false) + var mActivityRule: ActivityTestRule = ActivityTestRule(PgpActivity::class.java, true, false) - @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/")) - } + 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("//", "/") - @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" - - - 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) + } - 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 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) diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt index 96cca034..6bfac55f 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt @@ -186,58 +186,58 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { RESULT_CODE_SUCCESS -> { try { -// val showPassword = settings.getBoolean("show_password", true) -// val showExtraContent = settings.getBoolean("show_extra_content", true) -// -// crypto_container_decrypt.visibility = View.VISIBLE -// -// val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf") -// val entry = PasswordEntry(oStream) -// -// passwordEntry = entry -// -// if (operation == "EDIT") { -// editPassword() -// return@executeApiAsync -// } -// -// crypto_password_show.typeface = monoTypeface -// crypto_password_show.text = entry.password -// -// crypto_password_toggle_show.visibility = if (showPassword) View.GONE else View.VISIBLE -// crypto_password_show.transformationMethod = if (showPassword) { -// null -// } else { -// HoldToShowPasswordTransformation( -// crypto_password_toggle_show, -// Runnable { crypto_password_show.text = entry.password } -// ) -// } -// -// if (entry.hasExtraContent()) { -// crypto_extra_show_layout.visibility = if (showExtraContent) View.VISIBLE else View.GONE -// -// crypto_extra_show.typeface = monoTypeface -// crypto_extra_show.text = entry.extraContent -// -// if (entry.hasUsername()) { -// crypto_username_show.visibility = View.VISIBLE -// crypto_username_show_label.visibility = View.VISIBLE -// crypto_copy_username.visibility = View.VISIBLE -// -// crypto_copy_username.setOnClickListener { copyUsernameToClipBoard(entry.username) } -// crypto_username_show.typeface = monoTypeface -// crypto_username_show.text = entry.username -// } else { -// crypto_username_show.visibility = View.GONE -// crypto_username_show_label.visibility = View.GONE -// crypto_copy_username.visibility = View.GONE -// } -// } -// -// if (settings.getBoolean("copy_on_decrypt", true)) { -// copyPasswordToClipBoard() -// } + val showPassword = settings.getBoolean("show_password", true) + val showExtraContent = settings.getBoolean("show_extra_content", true) + + crypto_container_decrypt.visibility = View.VISIBLE + + val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf") + val entry = PasswordEntry(oStream) + + passwordEntry = entry + + if (operation == "EDIT") { + editPassword() + return@executeApiAsync + } + + crypto_password_show.typeface = monoTypeface + crypto_password_show.text = entry.password + + crypto_password_toggle_show.visibility = if (showPassword) View.GONE else View.VISIBLE + crypto_password_show.transformationMethod = if (showPassword) { + null + } else { + HoldToShowPasswordTransformation( + crypto_password_toggle_show, + Runnable { crypto_password_show.text = entry.password } + ) + } + + if (entry.hasExtraContent()) { + crypto_extra_show_layout.visibility = if (showExtraContent) View.VISIBLE else View.GONE + + crypto_extra_show.typeface = monoTypeface + crypto_extra_show.text = entry.extraContent + + if (entry.hasUsername()) { + crypto_username_show.visibility = View.VISIBLE + crypto_username_show_label.visibility = View.VISIBLE + crypto_copy_username.visibility = View.VISIBLE + + crypto_copy_username.setOnClickListener { copyUsernameToClipBoard(entry.username) } + crypto_username_show.typeface = monoTypeface + crypto_username_show.text = entry.username + } else { + crypto_username_show.visibility = View.GONE + crypto_username_show_label.visibility = View.GONE + crypto_copy_username.visibility = View.GONE + } + } + + if (settings.getBoolean("copy_on_decrypt", true)) { + copyPasswordToClipBoard() + } } catch (e: Exception) { Log.e(TAG, "An Exception occurred", e) }