From 5e66d99c852ea67a88b650c03b0e8d55e83eccde Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 5 Dec 2020 06:07:18 +0530 Subject: Refactor package structure (#1233) * idea: default test runner to Gradle * Kick off package structure revamp * Reparent all classes under dev.msfjarvis.aps Signed-off-by: Harsh Shandilya --- .../java/com/zeapo/pwdstore/MigrationsTest.kt | 109 ------------------- .../com/zeapo/pwdstore/StrictDomainRegexTest.kt | 49 --------- .../pwdstore/model/PasswordEntryAndroidTest.kt | 115 --------------------- .../com/zeapo/pwdstore/utils/UriTotpFinderTest.kt | 45 -------- .../aps/data/password/PasswordEntryAndroidTest.kt | 115 +++++++++++++++++++++ .../msfjarvis/aps/util/settings/MigrationsTest.kt | 107 +++++++++++++++++++ .../msfjarvis/aps/util/totp/UriTotpFinderTest.kt | 45 ++++++++ .../aps/util/viewmodel/StrictDomainRegexTest.kt | 49 +++++++++ 8 files changed, 316 insertions(+), 318 deletions(-) delete mode 100644 app/src/androidTest/java/com/zeapo/pwdstore/MigrationsTest.kt delete mode 100644 app/src/androidTest/java/com/zeapo/pwdstore/StrictDomainRegexTest.kt delete mode 100644 app/src/androidTest/java/com/zeapo/pwdstore/model/PasswordEntryAndroidTest.kt delete mode 100644 app/src/androidTest/java/com/zeapo/pwdstore/utils/UriTotpFinderTest.kt create mode 100644 app/src/androidTest/java/dev/msfjarvis/aps/data/password/PasswordEntryAndroidTest.kt create mode 100644 app/src/androidTest/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt create mode 100644 app/src/androidTest/java/dev/msfjarvis/aps/util/totp/UriTotpFinderTest.kt create mode 100644 app/src/androidTest/java/dev/msfjarvis/aps/util/viewmodel/StrictDomainRegexTest.kt (limited to 'app/src/androidTest') diff --git a/app/src/androidTest/java/com/zeapo/pwdstore/MigrationsTest.kt b/app/src/androidTest/java/com/zeapo/pwdstore/MigrationsTest.kt deleted file mode 100644 index 5ba1b307..00000000 --- a/app/src/androidTest/java/com/zeapo/pwdstore/MigrationsTest.kt +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -@file:Suppress("DEPRECATION") - -package com.zeapo.pwdstore - -import android.content.Context -import androidx.core.content.edit -import com.zeapo.pwdstore.git.config.AuthMode -import com.zeapo.pwdstore.git.config.Protocol -import com.zeapo.pwdstore.utils.PreferenceKeys -import com.zeapo.pwdstore.utils.getString -import com.zeapo.pwdstore.utils.sharedPrefs -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNull -import org.junit.Test - -class MigrationsTest { - - private fun checkOldKeysAreRemoved(context: Context) = with(context.sharedPrefs) { - assertNull(getString(PreferenceKeys.GIT_REMOTE_PORT)) - assertNull(getString(PreferenceKeys.GIT_REMOTE_USERNAME)) - assertNull(getString(PreferenceKeys.GIT_REMOTE_SERVER)) - assertNull(getString(PreferenceKeys.GIT_REMOTE_LOCATION)) - assertNull(getString(PreferenceKeys.GIT_REMOTE_PROTOCOL)) - } - - @Test - fun verifySshWithCustomPortMigration() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { - clear() - putString(PreferenceKeys.GIT_REMOTE_PORT, "2200") - putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") - putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo") - putString(PreferenceKeys.GIT_REMOTE_SERVER, "192.168.0.102") - putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref) - putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.Password.pref) - } - runMigrations(context) - checkOldKeysAreRemoved(context) - assertEquals( - context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), - "ssh://msfjarvis@192.168.0.102:2200/mnt/disk3/pass-repo" - ) - } - - @Test - fun verifySshWithDefaultPortMigration() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { - clear() - putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") - putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo") - putString(PreferenceKeys.GIT_REMOTE_SERVER, "192.168.0.102") - putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref) - putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.SshKey.pref) - } - runMigrations(context) - checkOldKeysAreRemoved(context) - assertEquals( - context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), - "msfjarvis@192.168.0.102:/mnt/disk3/pass-repo" - ) - } - - @Test - fun verifyHttpsWithGitHubMigration() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { - clear() - putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") - putString(PreferenceKeys.GIT_REMOTE_LOCATION, "Android-Password-Store/pass-test") - putString(PreferenceKeys.GIT_REMOTE_SERVER, "github.com") - putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Https.pref) - putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.None.pref) - } - runMigrations(context) - checkOldKeysAreRemoved(context) - assertEquals( - context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), - "https://github.com/Android-Password-Store/pass-test" - ) - } - - @Test - fun verifyHiddenFoldersMigrationIfDisabled() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { clear() } - runMigrations(context) - assertEquals(true, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true)) - assertEquals(false, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false)) - } - - @Test - fun verifyHiddenFoldersMigrationIfEnabled() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { - clear() - putBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true) - } - runMigrations(context) - assertEquals(false, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false)) - assertEquals(true, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false)) - } -} diff --git a/app/src/androidTest/java/com/zeapo/pwdstore/StrictDomainRegexTest.kt b/app/src/androidTest/java/com/zeapo/pwdstore/StrictDomainRegexTest.kt deleted file mode 100644 index 2631cb41..00000000 --- a/app/src/androidTest/java/com/zeapo/pwdstore/StrictDomainRegexTest.kt +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ -package com.zeapo.pwdstore - -import kotlin.test.assertFalse -import kotlin.test.assertNull -import kotlin.test.assertTrue -import org.junit.Test - -private infix fun String.matchedForDomain(domain: String) = - SearchableRepositoryViewModel.generateStrictDomainRegex(domain)?.containsMatchIn(this) == true - -class StrictDomainRegexTest { - - @Test fun acceptsLiteralDomain() { - assertTrue("work/example.org/john.doe@example.org.gpg" matchedForDomain "example.org") - assertTrue("example.org/john.doe@example.org.gpg" matchedForDomain "example.org") - assertTrue("example.org.gpg" matchedForDomain "example.org") - } - - @Test fun acceptsSubdomains() { - assertTrue("work/www.example.org/john.doe@example.org.gpg" matchedForDomain "example.org") - assertTrue("www2.example.org/john.doe@example.org.gpg" matchedForDomain "example.org") - assertTrue("www.login.example.org.gpg" matchedForDomain "example.org") - } - - @Test fun rejectsPhishingAttempts() { - assertFalse("example.org.gpg" matchedForDomain "xample.org") - assertFalse("login.example.org.gpg" matchedForDomain "xample.org") - assertFalse("example.org/john.doe@exmple.org.gpg" matchedForDomain "xample.org") - assertFalse("example.org.gpg" matchedForDomain "e/xample.org") - } - - @Test fun rejectNonGpgComponentMatches() { - assertFalse("work/example.org" matchedForDomain "example.org") - } - - @Test fun rejectsEmailAddresses() { - assertFalse("work/notexample.org/john.doe@example.org.gpg" matchedForDomain "example.org") - assertFalse("work/notexample.org/john.doe@www.example.org.gpg" matchedForDomain "example.org") - assertFalse("work/john.doe@www.example.org/foo.org" matchedForDomain "example.org") - } - - @Test fun rejectsPathSeparators() { - assertNull(SearchableRepositoryViewModel.generateStrictDomainRegex("ex/ample.org")) - } -} diff --git a/app/src/androidTest/java/com/zeapo/pwdstore/model/PasswordEntryAndroidTest.kt b/app/src/androidTest/java/com/zeapo/pwdstore/model/PasswordEntryAndroidTest.kt deleted file mode 100644 index 53df8209..00000000 --- a/app/src/androidTest/java/com/zeapo/pwdstore/model/PasswordEntryAndroidTest.kt +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package com.zeapo.pwdstore.model - -import com.github.michaelbull.result.get -import com.zeapo.pwdstore.utils.Otp -import com.zeapo.pwdstore.utils.UriTotpFinder -import java.util.Date -import kotlin.test.assertEquals -import kotlin.test.assertFalse -import kotlin.test.assertNotNull -import kotlin.test.assertNull -import kotlin.test.assertTrue -import org.junit.Test - -class PasswordEntryAndroidTest { - - private fun makeEntry(content: String) = PasswordEntry(content, UriTotpFinder()) - - @Test fun testGetPassword() { - assertEquals("fooooo", makeEntry("fooooo\nbla\n").password) - assertEquals("fooooo", makeEntry("fooooo\nbla").password) - assertEquals("fooooo", makeEntry("fooooo\n").password) - assertEquals("fooooo", makeEntry("fooooo").password) - assertEquals("", makeEntry("\nblubb\n").password) - assertEquals("", makeEntry("\nblubb").password) - assertEquals("", makeEntry("\n").password) - assertEquals("", makeEntry("").password) - } - - @Test fun testGetExtraContent() { - assertEquals("bla\n", makeEntry("fooooo\nbla\n").extraContent) - assertEquals("bla", makeEntry("fooooo\nbla").extraContent) - assertEquals("", makeEntry("fooooo\n").extraContent) - assertEquals("", makeEntry("fooooo").extraContent) - assertEquals("blubb\n", makeEntry("\nblubb\n").extraContent) - assertEquals("blubb", makeEntry("\nblubb").extraContent) - assertEquals("", makeEntry("\n").extraContent) - assertEquals("", makeEntry("").extraContent) - } - - @Test fun testGetUsername() { - for (field in PasswordEntry.USERNAME_FIELDS) { - assertEquals("username", makeEntry("\n$field username").username) - assertEquals("username", makeEntry("\n${field.toUpperCase()} username").username) - } - assertEquals( - "username", - makeEntry("secret\nextra\nlogin: username\ncontent\n").username) - assertEquals( - "username", - makeEntry("\nextra\nusername: username\ncontent\n").username) - assertEquals( - "username", makeEntry("\nUSERNaMe: username\ncontent\n").username) - assertEquals("username", makeEntry("\nlogin: username").username) - assertEquals("foo@example.com", makeEntry("\nemail: foo@example.com").username) - assertEquals("username", makeEntry("\nidentity: username\nlogin: another_username").username) - assertEquals("username", makeEntry("\nLOGiN:username").username) - assertNull(makeEntry("secret\nextra\ncontent\n").username) - } - - @Test fun testHasUsername() { - assertTrue(makeEntry("secret\nextra\nlogin: username\ncontent\n").hasUsername()) - assertFalse(makeEntry("secret\nextra\ncontent\n").hasUsername()) - assertFalse(makeEntry("secret\nlogin failed\n").hasUsername()) - assertFalse(makeEntry("\n").hasUsername()) - assertFalse(makeEntry("").hasUsername()) - } - - @Test fun testGeneratesOtpFromTotpUri() { - val entry = makeEntry("secret\nextra\n$TOTP_URI") - assertTrue(entry.hasTotp()) - val code = Otp.calculateCode( - entry.totpSecret!!, - // The hardcoded date value allows this test to stay reproducible. - Date(8640000).time / (1000 * entry.totpPeriod), - entry.totpAlgorithm, - entry.digits - ).get() - assertNotNull(code) { "Generated OTP cannot be null" } - assertEquals(entry.digits.toInt(), code.length) - assertEquals("545293", code) - } - - @Test fun testGeneratesOtpWithOnlyUriInFile() { - val entry = makeEntry(TOTP_URI) - assertTrue(entry.password.isEmpty()) - assertTrue(entry.hasTotp()) - val code = Otp.calculateCode( - entry.totpSecret!!, - // The hardcoded date value allows this test to stay reproducible. - Date(8640000).time / (1000 * entry.totpPeriod), - entry.totpAlgorithm, - entry.digits - ).get() - assertNotNull(code) { "Generated OTP cannot be null" } - assertEquals(entry.digits.toInt(), code.length) - assertEquals("545293", code) - } - - @Test fun testOnlyLooksForUriInFirstLine() { - val entry = makeEntry("id:\n$TOTP_URI") - assertTrue(entry.password.isNotEmpty()) - assertTrue(entry.hasTotp()) - assertFalse(entry.hasUsername()) - } - - companion object { - - const val TOTP_URI = "otpauth://totp/ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30" - } -} diff --git a/app/src/androidTest/java/com/zeapo/pwdstore/utils/UriTotpFinderTest.kt b/app/src/androidTest/java/com/zeapo/pwdstore/utils/UriTotpFinderTest.kt deleted file mode 100644 index 0e4ac51c..00000000 --- a/app/src/androidTest/java/com/zeapo/pwdstore/utils/UriTotpFinderTest.kt +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package com.zeapo.pwdstore.utils - -import kotlin.test.assertEquals -import org.junit.Test - -class UriTotpFinderTest { - - private val totpFinder = UriTotpFinder() - - @Test - fun findSecret() { - assertEquals("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", totpFinder.findSecret(TOTP_URI)) - assertEquals("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", totpFinder.findSecret("name\npassword\ntotp: HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ")) - assertEquals("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", totpFinder.findSecret(PASS_FILE_CONTENT)) - } - - @Test - fun findDigits() { - assertEquals("12", totpFinder.findDigits(TOTP_URI)) - assertEquals("12", totpFinder.findDigits(PASS_FILE_CONTENT)) - } - - @Test - fun findPeriod() { - assertEquals(25, totpFinder.findPeriod(TOTP_URI)) - assertEquals(25, totpFinder.findPeriod(PASS_FILE_CONTENT)) - } - - @Test - fun findAlgorithm() { - assertEquals("SHA256", totpFinder.findAlgorithm(TOTP_URI)) - assertEquals("SHA256", totpFinder.findAlgorithm(PASS_FILE_CONTENT)) - } - - companion object { - - const val TOTP_URI = "otpauth://totp/ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA256&digits=12&period=25" - const val PASS_FILE_CONTENT = "password\n$TOTP_URI" - } -} diff --git a/app/src/androidTest/java/dev/msfjarvis/aps/data/password/PasswordEntryAndroidTest.kt b/app/src/androidTest/java/dev/msfjarvis/aps/data/password/PasswordEntryAndroidTest.kt new file mode 100644 index 00000000..521b4c4f --- /dev/null +++ b/app/src/androidTest/java/dev/msfjarvis/aps/data/password/PasswordEntryAndroidTest.kt @@ -0,0 +1,115 @@ +/* + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.data.password + +import com.github.michaelbull.result.get +import dev.msfjarvis.aps.util.totp.Otp +import dev.msfjarvis.aps.util.totp.UriTotpFinder +import java.util.Date +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue +import org.junit.Test + +class PasswordEntryAndroidTest { + + private fun makeEntry(content: String) = PasswordEntry(content, UriTotpFinder()) + + @Test fun testGetPassword() { + assertEquals("fooooo", makeEntry("fooooo\nbla\n").password) + assertEquals("fooooo", makeEntry("fooooo\nbla").password) + assertEquals("fooooo", makeEntry("fooooo\n").password) + assertEquals("fooooo", makeEntry("fooooo").password) + assertEquals("", makeEntry("\nblubb\n").password) + assertEquals("", makeEntry("\nblubb").password) + assertEquals("", makeEntry("\n").password) + assertEquals("", makeEntry("").password) + } + + @Test fun testGetExtraContent() { + assertEquals("bla\n", makeEntry("fooooo\nbla\n").extraContent) + assertEquals("bla", makeEntry("fooooo\nbla").extraContent) + assertEquals("", makeEntry("fooooo\n").extraContent) + assertEquals("", makeEntry("fooooo").extraContent) + assertEquals("blubb\n", makeEntry("\nblubb\n").extraContent) + assertEquals("blubb", makeEntry("\nblubb").extraContent) + assertEquals("", makeEntry("\n").extraContent) + assertEquals("", makeEntry("").extraContent) + } + + @Test fun testGetUsername() { + for (field in PasswordEntry.USERNAME_FIELDS) { + assertEquals("username", makeEntry("\n$field username").username) + assertEquals("username", makeEntry("\n${field.toUpperCase()} username").username) + } + assertEquals( + "username", + makeEntry("secret\nextra\nlogin: username\ncontent\n").username) + assertEquals( + "username", + makeEntry("\nextra\nusername: username\ncontent\n").username) + assertEquals( + "username", makeEntry("\nUSERNaMe: username\ncontent\n").username) + assertEquals("username", makeEntry("\nlogin: username").username) + assertEquals("foo@example.com", makeEntry("\nemail: foo@example.com").username) + assertEquals("username", makeEntry("\nidentity: username\nlogin: another_username").username) + assertEquals("username", makeEntry("\nLOGiN:username").username) + assertNull(makeEntry("secret\nextra\ncontent\n").username) + } + + @Test fun testHasUsername() { + assertTrue(makeEntry("secret\nextra\nlogin: username\ncontent\n").hasUsername()) + assertFalse(makeEntry("secret\nextra\ncontent\n").hasUsername()) + assertFalse(makeEntry("secret\nlogin failed\n").hasUsername()) + assertFalse(makeEntry("\n").hasUsername()) + assertFalse(makeEntry("").hasUsername()) + } + + @Test fun testGeneratesOtpFromTotpUri() { + val entry = makeEntry("secret\nextra\n$TOTP_URI") + assertTrue(entry.hasTotp()) + val code = Otp.calculateCode( + entry.totpSecret!!, + // The hardcoded date value allows this test to stay reproducible. + Date(8640000).time / (1000 * entry.totpPeriod), + entry.totpAlgorithm, + entry.digits + ).get() + assertNotNull(code) { "Generated OTP cannot be null" } + assertEquals(entry.digits.toInt(), code.length) + assertEquals("545293", code) + } + + @Test fun testGeneratesOtpWithOnlyUriInFile() { + val entry = makeEntry(TOTP_URI) + assertTrue(entry.password.isEmpty()) + assertTrue(entry.hasTotp()) + val code = Otp.calculateCode( + entry.totpSecret!!, + // The hardcoded date value allows this test to stay reproducible. + Date(8640000).time / (1000 * entry.totpPeriod), + entry.totpAlgorithm, + entry.digits + ).get() + assertNotNull(code) { "Generated OTP cannot be null" } + assertEquals(entry.digits.toInt(), code.length) + assertEquals("545293", code) + } + + @Test fun testOnlyLooksForUriInFirstLine() { + val entry = makeEntry("id:\n$TOTP_URI") + assertTrue(entry.password.isNotEmpty()) + assertTrue(entry.hasTotp()) + assertFalse(entry.hasUsername()) + } + + companion object { + + const val TOTP_URI = "otpauth://totp/ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30" + } +} diff --git a/app/src/androidTest/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt b/app/src/androidTest/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt new file mode 100644 index 00000000..d27b4c1c --- /dev/null +++ b/app/src/androidTest/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt @@ -0,0 +1,107 @@ +/* + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +@file:Suppress("DEPRECATION") + +package dev.msfjarvis.aps.util.settings + +import android.content.Context +import androidx.core.content.edit +import dev.msfjarvis.aps.Application +import dev.msfjarvis.aps.util.extensions.getString +import dev.msfjarvis.aps.util.extensions.sharedPrefs +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class MigrationsTest { + + private fun checkOldKeysAreRemoved(context: Context) = with(context.sharedPrefs) { + assertNull(getString(PreferenceKeys.GIT_REMOTE_PORT)) + assertNull(getString(PreferenceKeys.GIT_REMOTE_USERNAME)) + assertNull(getString(PreferenceKeys.GIT_REMOTE_SERVER)) + assertNull(getString(PreferenceKeys.GIT_REMOTE_LOCATION)) + assertNull(getString(PreferenceKeys.GIT_REMOTE_PROTOCOL)) + } + + @Test + fun verifySshWithCustomPortMigration() { + val context = Application.instance.applicationContext + context.sharedPrefs.edit { + clear() + putString(PreferenceKeys.GIT_REMOTE_PORT, "2200") + putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") + putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo") + putString(PreferenceKeys.GIT_REMOTE_SERVER, "192.168.0.102") + putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref) + putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.Password.pref) + } + runMigrations(context) + checkOldKeysAreRemoved(context) + assertEquals( + context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), + "ssh://msfjarvis@192.168.0.102:2200/mnt/disk3/pass-repo" + ) + } + + @Test + fun verifySshWithDefaultPortMigration() { + val context = Application.instance.applicationContext + context.sharedPrefs.edit { + clear() + putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") + putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo") + putString(PreferenceKeys.GIT_REMOTE_SERVER, "192.168.0.102") + putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref) + putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.SshKey.pref) + } + runMigrations(context) + checkOldKeysAreRemoved(context) + assertEquals( + context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), + "msfjarvis@192.168.0.102:/mnt/disk3/pass-repo" + ) + } + + @Test + fun verifyHttpsWithGitHubMigration() { + val context = Application.instance.applicationContext + context.sharedPrefs.edit { + clear() + putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") + putString(PreferenceKeys.GIT_REMOTE_LOCATION, "Android-Password-Store/pass-test") + putString(PreferenceKeys.GIT_REMOTE_SERVER, "github.com") + putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Https.pref) + putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.None.pref) + } + runMigrations(context) + checkOldKeysAreRemoved(context) + assertEquals( + context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), + "https://github.com/Android-Password-Store/pass-test" + ) + } + + @Test + fun verifyHiddenFoldersMigrationIfDisabled() { + val context = Application.instance.applicationContext + context.sharedPrefs.edit { clear() } + runMigrations(context) + assertEquals(true, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true)) + assertEquals(false, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false)) + } + + @Test + fun verifyHiddenFoldersMigrationIfEnabled() { + val context = Application.instance.applicationContext + context.sharedPrefs.edit { + clear() + putBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true) + } + runMigrations(context) + assertEquals(false, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false)) + assertEquals(true, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false)) + } +} diff --git a/app/src/androidTest/java/dev/msfjarvis/aps/util/totp/UriTotpFinderTest.kt b/app/src/androidTest/java/dev/msfjarvis/aps/util/totp/UriTotpFinderTest.kt new file mode 100644 index 00000000..6dba51b3 --- /dev/null +++ b/app/src/androidTest/java/dev/msfjarvis/aps/util/totp/UriTotpFinderTest.kt @@ -0,0 +1,45 @@ +/* + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.util.totp + +import kotlin.test.assertEquals +import org.junit.Test + +class UriTotpFinderTest { + + private val totpFinder = UriTotpFinder() + + @Test + fun findSecret() { + assertEquals("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", totpFinder.findSecret(TOTP_URI)) + assertEquals("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", totpFinder.findSecret("name\npassword\ntotp: HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ")) + assertEquals("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", totpFinder.findSecret(PASS_FILE_CONTENT)) + } + + @Test + fun findDigits() { + assertEquals("12", totpFinder.findDigits(TOTP_URI)) + assertEquals("12", totpFinder.findDigits(PASS_FILE_CONTENT)) + } + + @Test + fun findPeriod() { + assertEquals(25, totpFinder.findPeriod(TOTP_URI)) + assertEquals(25, totpFinder.findPeriod(PASS_FILE_CONTENT)) + } + + @Test + fun findAlgorithm() { + assertEquals("SHA256", totpFinder.findAlgorithm(TOTP_URI)) + assertEquals("SHA256", totpFinder.findAlgorithm(PASS_FILE_CONTENT)) + } + + companion object { + + const val TOTP_URI = "otpauth://totp/ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA256&digits=12&period=25" + const val PASS_FILE_CONTENT = "password\n$TOTP_URI" + } +} diff --git a/app/src/androidTest/java/dev/msfjarvis/aps/util/viewmodel/StrictDomainRegexTest.kt b/app/src/androidTest/java/dev/msfjarvis/aps/util/viewmodel/StrictDomainRegexTest.kt new file mode 100644 index 00000000..876a0478 --- /dev/null +++ b/app/src/androidTest/java/dev/msfjarvis/aps/util/viewmodel/StrictDomainRegexTest.kt @@ -0,0 +1,49 @@ +/* + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ +package dev.msfjarvis.aps.util.viewmodel + +import kotlin.test.assertFalse +import kotlin.test.assertNull +import kotlin.test.assertTrue +import org.junit.Test + +private infix fun String.matchedForDomain(domain: String) = + SearchableRepositoryViewModel.generateStrictDomainRegex(domain)?.containsMatchIn(this) == true + +class StrictDomainRegexTest { + + @Test fun acceptsLiteralDomain() { + assertTrue("work/example.org/john.doe@example.org.gpg" matchedForDomain "example.org") + assertTrue("example.org/john.doe@example.org.gpg" matchedForDomain "example.org") + assertTrue("example.org.gpg" matchedForDomain "example.org") + } + + @Test fun acceptsSubdomains() { + assertTrue("work/www.example.org/john.doe@example.org.gpg" matchedForDomain "example.org") + assertTrue("www2.example.org/john.doe@example.org.gpg" matchedForDomain "example.org") + assertTrue("www.login.example.org.gpg" matchedForDomain "example.org") + } + + @Test fun rejectsPhishingAttempts() { + assertFalse("example.org.gpg" matchedForDomain "xample.org") + assertFalse("login.example.org.gpg" matchedForDomain "xample.org") + assertFalse("example.org/john.doe@exmple.org.gpg" matchedForDomain "xample.org") + assertFalse("example.org.gpg" matchedForDomain "e/xample.org") + } + + @Test fun rejectNonGpgComponentMatches() { + assertFalse("work/example.org" matchedForDomain "example.org") + } + + @Test fun rejectsEmailAddresses() { + assertFalse("work/notexample.org/john.doe@example.org.gpg" matchedForDomain "example.org") + assertFalse("work/notexample.org/john.doe@www.example.org.gpg" matchedForDomain "example.org") + assertFalse("work/john.doe@www.example.org/foo.org" matchedForDomain "example.org") + } + + @Test fun rejectsPathSeparators() { + assertNull(SearchableRepositoryViewModel.generateStrictDomainRegex("ex/ample.org")) + } +} -- cgit v1.2.3