From 64a6e0f4e916122fdd00df2e2b48d96ea7d49c1c Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 25 Jul 2020 14:37:16 +0530 Subject: Properly handle files without passwords (#969) * Properly handle files without passwords Fixes #967 Signed-off-by: Harsh Shandilya * Fix tests Signed-off-by: Harsh Shandilya * Only look for TOTP URI Signed-off-by: Harsh Shandilya (cherry picked from commit 62dbc183d52d93860228316b209ec5aa15f16a08) --- .../com/zeapo/pwdstore/model/PasswordEntryTest.kt | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'app/src/test') diff --git a/app/src/test/java/com/zeapo/pwdstore/model/PasswordEntryTest.kt b/app/src/test/java/com/zeapo/pwdstore/model/PasswordEntryTest.kt index 16cbd855..f9c55167 100644 --- a/app/src/test/java/com/zeapo/pwdstore/model/PasswordEntryTest.kt +++ b/app/src/test/java/com/zeapo/pwdstore/model/PasswordEntryTest.kt @@ -83,6 +83,29 @@ class PasswordEntryTest { 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 + ) + 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" -- cgit v1.2.3