Fix instrumentation tests

Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
This commit is contained in:
Harsh Shandilya 2019-05-31 12:57:18 +05:30
parent 8c0bce3e98
commit 30b6d2346a
No known key found for this signature in database
GPG key ID: C2E74282C2133D62
6 changed files with 35 additions and 33 deletions

View file

@ -91,6 +91,7 @@ dependencies {
androidTestImplementation("org.mockito:mockito-core:2.28.2") androidTestImplementation("org.mockito:mockito-core:2.28.2")
androidTestImplementation("androidx.test:runner:1.2.0") androidTestImplementation("androidx.test:runner:1.2.0")
androidTestImplementation("androidx.test:rules: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-core:3.2.0")
androidTestImplementation("androidx.test.espresso:espresso-intents:3.2.0") androidTestImplementation("androidx.test.espresso:espresso-intents:3.2.0")
} }

View file

@ -5,10 +5,10 @@ import android.content.ClipboardManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.SystemClock import android.os.SystemClock
import androidx.test.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.filters.LargeTest import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule import androidx.test.rule.ActivityTestRule
import androidx.test.runner.AndroidJUnit4 import androidx.test.ext.junit.runners.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.*
@ -22,26 +22,27 @@ import org.junit.runner.RunWith
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.nio.charset.StandardCharsets
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
@LargeTest @LargeTest
class DecryptTest { class DecryptTest {
lateinit var targetContext: Context private lateinit var targetContext: Context
lateinit var testContext: Context private lateinit var testContext: Context
lateinit var activity: PgpActivity lateinit var activity: PgpActivity
val name = "sub" private val name = "sub"
val parentPath = "/category/" private val parentPath = "/category/"
lateinit var path: String lateinit var path: String
lateinit var repoPath: String lateinit var repoPath: String
@Rule @JvmField @Rule @JvmField
var mActivityRule: ActivityTestRule<PgpActivity> = ActivityTestRule<PgpActivity>(PgpActivity::class.java, true, false) var mActivityRule: ActivityTestRule<PgpActivity> = ActivityTestRule<PgpActivity>(PgpActivity::class.java, true, false)
fun init() { private fun init() {
targetContext = InstrumentationRegistry.getInstrumentation().targetContext targetContext = InstrumentationRegistry.getInstrumentation().targetContext
testContext = InstrumentationRegistry.getContext() testContext = InstrumentationRegistry.getInstrumentation().context
copyAssets("encrypted-store", File(targetContext.filesDir, "test-store").absolutePath) copyAssets("encrypted-store", File(targetContext.filesDir, "test-store").absolutePath)
repoPath = File(targetContext.filesDir, "test-store").absolutePath repoPath = File(targetContext.filesDir, "test-store").absolutePath
path = "$repoPath/$parentPath/$name.gpg".replace("//", "/") path = "$repoPath/$parentPath/$name.gpg".replace("//", "/")
@ -61,14 +62,14 @@ class DecryptTest {
assertEquals("/cat1/n1.gpg", PgpActivity.getRelativePath(pathOne, "/fake/path")) assertEquals("/cat1/n1.gpg", PgpActivity.getRelativePath(pathOne, "/fake/path"))
assertEquals("/cat1/", PgpActivity.getParentPath(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 // 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("/n2.gpg", PgpActivity.getRelativePath(pathTwo, "/fake/path"))
assertEquals("/", PgpActivity.getParentPath(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 @Test
@ -88,13 +89,13 @@ class DecryptTest {
@Test @Test
fun shouldDecrypt() { fun shouldDecrypt() {
init() 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) val passEntry = PasswordEntry(clearPass)
// Setup the timer to 1 second // Setup the timer to 1 second
// first remember the previous timer to set it back later // first remember the previous timer to set it back later
val showTime = try { 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) { } catch (e: NumberFormatException) {
45 45
} }
@ -129,7 +130,7 @@ class DecryptTest {
FileUtils.forceMkdir(File(destination)) FileUtils.forceMkdir(File(destination))
FileUtils.cleanDirectory(File(destination)) FileUtils.cleanDirectory(File(destination))
val testContext = InstrumentationRegistry.getContext() val testContext = InstrumentationRegistry.getInstrumentation().context
val assetManager = testContext.assets val assetManager = testContext.assets
val files: Array<String>? = assetManager.list(source) val files: Array<String>? = assetManager.list(source)
@ -150,7 +151,7 @@ class DecryptTest {
output.flush() output.flush()
output.close() output.close()
} catch (e: IOException) { } catch (e: IOException) {
Log.e("tag", "Failed to copy asset file: " + filename, e) Log.e("tag", "Failed to copy asset file: $filename", e)
} }
} }
} }

View file

@ -3,15 +3,15 @@ package com.zeapo.pwdstore
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.test.InstrumentationRegistry
import androidx.test.espresso.Espresso.onView import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.* import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule import androidx.test.rule.ActivityTestRule
import androidx.test.runner.AndroidJUnit4
import com.zeapo.pwdstore.crypto.PgpActivity import com.zeapo.pwdstore.crypto.PgpActivity
import org.apache.commons.io.FileUtils import org.apache.commons.io.FileUtils
import org.apache.commons.io.IOUtils import org.apache.commons.io.IOUtils
@ -24,21 +24,21 @@ import java.io.File
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
@LargeTest @LargeTest
class EncryptTest { class EncryptTest {
lateinit var targetContext: Context private lateinit var targetContext: Context
lateinit var testContext: Context private lateinit var testContext: Context
lateinit var activity: PgpActivity private lateinit var activity: PgpActivity
val name = "sub" private val name = "sub"
val parentPath = "/category/" private val parentPath = "/category/"
lateinit var path: String private lateinit var path: String
lateinit var repoPath: String private lateinit var repoPath: String
@Rule @JvmField @Rule @JvmField
var mActivityRule: ActivityTestRule<PgpActivity> = ActivityTestRule<PgpActivity>(PgpActivity::class.java, true, false) var mActivityRule: ActivityTestRule<PgpActivity> = ActivityTestRule<PgpActivity>(PgpActivity::class.java, true, false)
fun init() { private fun init() {
targetContext = InstrumentationRegistry.getInstrumentation().targetContext targetContext = InstrumentationRegistry.getInstrumentation().targetContext
testContext = InstrumentationRegistry.getContext() testContext = InstrumentationRegistry.getInstrumentation().context
// have an empty store // have an empty store
FileUtils.forceMkdir(File(targetContext.filesDir, "test-store")) FileUtils.forceMkdir(File(targetContext.filesDir, "test-store"))

View file

@ -6,7 +6,7 @@ import junit.framework.TestCase;
public class OtpTest extends TestCase { public class OtpTest extends TestCase {
public void testOtp() { public void testOtp() {
String code = Otp.calculateCode("JBSWY3DPEHPK3PXP", 0L); String code = Otp.calculateCode("JBSWY3DPEHPK3PXP", 0L, "sha1", "s");
assertEquals("282760", code); assertEquals("282760", code);
} }
} }

View file

@ -4,7 +4,7 @@ import junit.framework.TestCase;
public class PasswordEntryTest extends 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\n").getPassword());
assertEquals("fooooo", new PasswordEntry("fooooo\nbla").getPassword()); assertEquals("fooooo", new PasswordEntry("fooooo\nbla").getPassword());
assertEquals("fooooo", new PasswordEntry("fooooo\n").getPassword()); assertEquals("fooooo", new PasswordEntry("fooooo\n").getPassword());
@ -15,7 +15,7 @@ public class PasswordEntryTest extends TestCase {
assertEquals("", new PasswordEntry("").getPassword()); assertEquals("", new PasswordEntry("").getPassword());
} }
public void testGetExtraContent() throws Exception { public void testGetExtraContent() {
assertEquals("bla\n", new PasswordEntry("fooooo\nbla\n").getExtraContent()); assertEquals("bla\n", new PasswordEntry("fooooo\nbla\n").getExtraContent());
assertEquals("bla", new PasswordEntry("fooooo\nbla").getExtraContent()); assertEquals("bla", new PasswordEntry("fooooo\nbla").getExtraContent());
assertEquals("", new PasswordEntry("fooooo\n").getExtraContent()); assertEquals("", new PasswordEntry("fooooo\n").getExtraContent());
@ -26,7 +26,7 @@ public class PasswordEntryTest extends TestCase {
assertEquals("", new PasswordEntry("").getExtraContent()); 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("secret\nextra\nlogin: username\ncontent\n").getUsername());
assertEquals("username", new PasswordEntry("\nextra\nusername: username\ncontent\n").getUsername()); assertEquals("username", new PasswordEntry("\nextra\nusername: username\ncontent\n").getUsername());
assertEquals("username", new PasswordEntry("\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()); 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()); assertTrue(new PasswordEntry("secret\nextra\nlogin: username\ncontent\n").hasUsername());
assertFalse(new PasswordEntry("secret\nextra\ncontent\n").hasUsername()); assertFalse(new PasswordEntry("secret\nextra\ncontent\n").hasUsername());
assertFalse(new PasswordEntry("secret\nlogin failed\n").hasUsername()); assertFalse(new PasswordEntry("secret\nlogin failed\n").hasUsername());

View file

@ -83,7 +83,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
} }
private val relativeParentPath: String by lazy { getParentPath(fullPath, repoPath) } 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<String> by lazy { settings.getStringSet("openpgp_key_ids_set", emptySet()) } private val keyIDs: MutableSet<String> by lazy { settings.getStringSet("openpgp_key_ids_set", emptySet()) }
private var mServiceConnection: OpenPgpServiceConnection? = null private var mServiceConnection: OpenPgpServiceConnection? = null