From 30b6d2346a5ee856a7af49a62c274f7d979d7c2a Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 31 May 2019 12:57:18 +0530 Subject: Fix instrumentation tests Signed-off-by: Harsh Shandilya --- app/build.gradle.kts | 1 + .../java/com/zeapo/pwdstore/DecryptTest.kt | 33 +++++++++++----------- .../java/com/zeapo/pwdstore/EncryptTest.kt | 22 +++++++-------- .../java/com/zeapo/pwdstore/OtpTest.java | 2 +- .../java/com/zeapo/pwdstore/PasswordEntryTest.java | 8 +++--- .../java/com/zeapo/pwdstore/crypto/PgpActivity.kt | 2 +- 6 files changed, 35 insertions(+), 33 deletions(-) (limited to 'app') diff --git a/app/build.gradle.kts b/app/build.gradle.kts index d537dc21..4be810ae 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -91,6 +91,7 @@ dependencies { androidTestImplementation("org.mockito:mockito-core:2.28.2") androidTestImplementation("androidx.test:runner:1.2.0") androidTestImplementation("androidx.test:rules:1.2.0") + androidTestImplementation("androidx.test.ext:junit:1.1.1") androidTestImplementation("androidx.test.espresso:espresso-core:3.2.0") androidTestImplementation("androidx.test.espresso:espresso-intents:3.2.0") } diff --git a/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt b/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt index 9dfd5f04..f0c502d9 100644 --- a/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt +++ b/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt @@ -5,10 +5,10 @@ import android.content.ClipboardManager import android.content.Context import android.content.Intent import android.os.SystemClock -import androidx.test.InstrumentationRegistry +import androidx.test.platform.app.InstrumentationRegistry import androidx.test.filters.LargeTest import androidx.test.rule.ActivityTestRule -import androidx.test.runner.AndroidJUnit4 +import androidx.test.ext.junit.runners.AndroidJUnit4 import android.util.Log import com.zeapo.pwdstore.crypto.PgpActivity import kotlinx.android.synthetic.main.decrypt_layout.* @@ -22,26 +22,27 @@ import org.junit.runner.RunWith import java.io.File import java.io.FileOutputStream import java.io.IOException +import java.nio.charset.StandardCharsets @RunWith(AndroidJUnit4::class) @LargeTest class DecryptTest { - lateinit var targetContext: Context - lateinit var testContext: Context + private lateinit var targetContext: Context + private lateinit var testContext: Context lateinit var activity: PgpActivity - val name = "sub" - val parentPath = "/category/" + private val name = "sub" + private val parentPath = "/category/" lateinit var path: String lateinit var repoPath: String @Rule @JvmField var mActivityRule: ActivityTestRule = ActivityTestRule(PgpActivity::class.java, true, false) - fun init() { + private fun init() { targetContext = InstrumentationRegistry.getInstrumentation().targetContext - testContext = InstrumentationRegistry.getContext() + testContext = InstrumentationRegistry.getInstrumentation().context copyAssets("encrypted-store", File(targetContext.filesDir, "test-store").absolutePath) repoPath = File(targetContext.filesDir, "test-store").absolutePath path = "$repoPath/$parentPath/$name.gpg".replace("//", "/") @@ -61,14 +62,14 @@ class DecryptTest { assertEquals("/cat1/n1.gpg", PgpActivity.getRelativePath(pathOne, "/fake/path")) assertEquals("/cat1/", PgpActivity.getParentPath(pathOne, "/fake/path")) - assertEquals("n1", PgpActivity.getName(pathOne, "/fake/path")) + assertEquals("n1", PgpActivity.getName("$pathOne/fake/path")) // test that even if we append a `/` it still works - assertEquals("n1", PgpActivity.getName(pathOne, "/fake/path/")) + assertEquals("n1", PgpActivity.getName("$pathOne/fake/path/")) assertEquals("/n2.gpg", PgpActivity.getRelativePath(pathTwo, "/fake/path")) assertEquals("/", PgpActivity.getParentPath(pathTwo, "/fake/path")) - assertEquals("n2", PgpActivity.getName(pathTwo, "/fake/path")) - assertEquals("n2", PgpActivity.getName(pathTwo, "/fake/path/")) + assertEquals("n2", PgpActivity.getName("$pathTwo/fake/path")) + assertEquals("n2", PgpActivity.getName("$pathTwo/fake/path/")) } @Test @@ -88,13 +89,13 @@ class DecryptTest { @Test fun shouldDecrypt() { init() - val clearPass = IOUtils.toString(testContext.assets.open("clear-store/category/sub")) + val clearPass = IOUtils.toString(testContext.assets.open("clear-store/category/sub"), StandardCharsets.UTF_8) val passEntry = PasswordEntry(clearPass) // Setup the timer to 1 second // first remember the previous timer to set it back later val showTime = try { - Integer.parseInt(activity.settings.getString("general_show_time", "45")) + Integer.parseInt(activity.settings.getString("general_show_time", "45") ?: "45") } catch (e: NumberFormatException) { 45 } @@ -129,7 +130,7 @@ class DecryptTest { FileUtils.forceMkdir(File(destination)) FileUtils.cleanDirectory(File(destination)) - val testContext = InstrumentationRegistry.getContext() + val testContext = InstrumentationRegistry.getInstrumentation().context val assetManager = testContext.assets val files: Array? = assetManager.list(source) @@ -150,7 +151,7 @@ class DecryptTest { output.flush() output.close() } catch (e: IOException) { - Log.e("tag", "Failed to copy asset file: " + filename, e) + Log.e("tag", "Failed to copy asset file: $filename", e) } } } diff --git a/app/src/androidTest/java/com/zeapo/pwdstore/EncryptTest.kt b/app/src/androidTest/java/com/zeapo/pwdstore/EncryptTest.kt index a86db5ee..64e7e143 100644 --- a/app/src/androidTest/java/com/zeapo/pwdstore/EncryptTest.kt +++ b/app/src/androidTest/java/com/zeapo/pwdstore/EncryptTest.kt @@ -3,15 +3,15 @@ package com.zeapo.pwdstore import android.annotation.SuppressLint import android.content.Context import android.content.Intent -import androidx.test.InstrumentationRegistry import androidx.test.espresso.Espresso.onView import androidx.test.espresso.action.ViewActions.* import androidx.test.espresso.assertion.ViewAssertions import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withText +import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.LargeTest +import androidx.test.platform.app.InstrumentationRegistry import androidx.test.rule.ActivityTestRule -import androidx.test.runner.AndroidJUnit4 import com.zeapo.pwdstore.crypto.PgpActivity import org.apache.commons.io.FileUtils import org.apache.commons.io.IOUtils @@ -24,21 +24,21 @@ import java.io.File @RunWith(AndroidJUnit4::class) @LargeTest class EncryptTest { - lateinit var targetContext: Context - lateinit var testContext: Context - lateinit var activity: PgpActivity + private lateinit var targetContext: Context + private lateinit var testContext: Context + private lateinit var activity: PgpActivity - val name = "sub" - val parentPath = "/category/" - lateinit var path: String - lateinit var repoPath: String + private val name = "sub" + private val parentPath = "/category/" + private lateinit var path: String + private lateinit var repoPath: String @Rule @JvmField var mActivityRule: ActivityTestRule = ActivityTestRule(PgpActivity::class.java, true, false) - fun init() { + private fun init() { targetContext = InstrumentationRegistry.getInstrumentation().targetContext - testContext = InstrumentationRegistry.getContext() + testContext = InstrumentationRegistry.getInstrumentation().context // have an empty store FileUtils.forceMkdir(File(targetContext.filesDir, "test-store")) diff --git a/app/src/androidTest/java/com/zeapo/pwdstore/OtpTest.java b/app/src/androidTest/java/com/zeapo/pwdstore/OtpTest.java index e48f1ab6..2cb04d2e 100644 --- a/app/src/androidTest/java/com/zeapo/pwdstore/OtpTest.java +++ b/app/src/androidTest/java/com/zeapo/pwdstore/OtpTest.java @@ -6,7 +6,7 @@ import junit.framework.TestCase; public class OtpTest extends TestCase { public void testOtp() { - String code = Otp.calculateCode("JBSWY3DPEHPK3PXP", 0L); + String code = Otp.calculateCode("JBSWY3DPEHPK3PXP", 0L, "sha1", "s"); assertEquals("282760", code); } } diff --git a/app/src/androidTest/java/com/zeapo/pwdstore/PasswordEntryTest.java b/app/src/androidTest/java/com/zeapo/pwdstore/PasswordEntryTest.java index 3df296fe..3763d1ab 100644 --- a/app/src/androidTest/java/com/zeapo/pwdstore/PasswordEntryTest.java +++ b/app/src/androidTest/java/com/zeapo/pwdstore/PasswordEntryTest.java @@ -4,7 +4,7 @@ import junit.framework.TestCase; public class PasswordEntryTest extends TestCase { - public void testGetPassword() throws Exception { + public void testGetPassword() { assertEquals("fooooo", new PasswordEntry("fooooo\nbla\n").getPassword()); assertEquals("fooooo", new PasswordEntry("fooooo\nbla").getPassword()); assertEquals("fooooo", new PasswordEntry("fooooo\n").getPassword()); @@ -15,7 +15,7 @@ public class PasswordEntryTest extends TestCase { assertEquals("", new PasswordEntry("").getPassword()); } - public void testGetExtraContent() throws Exception { + public void testGetExtraContent() { assertEquals("bla\n", new PasswordEntry("fooooo\nbla\n").getExtraContent()); assertEquals("bla", new PasswordEntry("fooooo\nbla").getExtraContent()); assertEquals("", new PasswordEntry("fooooo\n").getExtraContent()); @@ -26,7 +26,7 @@ public class PasswordEntryTest extends TestCase { assertEquals("", new PasswordEntry("").getExtraContent()); } - public void testGetUsername() throws Exception { + public void testGetUsername() { assertEquals("username", new PasswordEntry("secret\nextra\nlogin: username\ncontent\n").getUsername()); assertEquals("username", new PasswordEntry("\nextra\nusername: username\ncontent\n").getUsername()); assertEquals("username", new PasswordEntry("\nUSERNaMe: username\ncontent\n").getUsername()); @@ -34,7 +34,7 @@ public class PasswordEntryTest extends TestCase { assertNull(new PasswordEntry("secret\nextra\ncontent\n").getUsername()); } - public void testHasUsername() throws Exception { + public void testHasUsername() { assertTrue(new PasswordEntry("secret\nextra\nlogin: username\ncontent\n").hasUsername()); assertFalse(new PasswordEntry("secret\nextra\ncontent\n").hasUsername()); assertFalse(new PasswordEntry("secret\nlogin failed\n").hasUsername()); 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 7d782a05..d12dee78 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt @@ -83,7 +83,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { } private val relativeParentPath: String by lazy { getParentPath(fullPath, repoPath) } - private val settings: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(this) } + val settings: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(this) } private val keyIDs: MutableSet by lazy { settings.getStringSet("openpgp_key_ids_set", emptySet()) } private var mServiceConnection: OpenPgpServiceConnection? = null -- cgit v1.2.3