Misc cleanups (#1891)

This commit is contained in:
Harsh Shandilya 2022-05-03 01:48:16 +05:30 committed by GitHub
parent 07a83a33b8
commit c555609f16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 53 additions and 44 deletions

View file

@ -12,28 +12,28 @@ import kotlin.test.assertTrue
class GpgIdentifierTest { class GpgIdentifierTest {
@Test @Test
fun `parses hexadecimal key id without leading 0x`() { fun parseHexKeyIdWithout0xPrefix() {
val identifier = GpgIdentifier.fromString("79E8208280490C77") val identifier = GpgIdentifier.fromString("79E8208280490C77")
assertNotNull(identifier) assertNotNull(identifier)
assertTrue { identifier is GpgIdentifier.KeyId } assertTrue { identifier is GpgIdentifier.KeyId }
} }
@Test @Test
fun `parses hexadecimal key id`() { fun parseHexKeyId() {
val identifier = GpgIdentifier.fromString("0x79E8208280490C77") val identifier = GpgIdentifier.fromString("0x79E8208280490C77")
assertNotNull(identifier) assertNotNull(identifier)
assertTrue { identifier is GpgIdentifier.KeyId } assertTrue { identifier is GpgIdentifier.KeyId }
} }
@Test @Test
fun `parses email as user id`() { fun parseValidEmail() {
val identifier = GpgIdentifier.fromString("john.doe@example.org") val identifier = GpgIdentifier.fromString("john.doe@example.org")
assertNotNull(identifier) assertNotNull(identifier)
assertTrue { identifier is GpgIdentifier.UserId } assertTrue { identifier is GpgIdentifier.UserId }
} }
@Test @Test
fun `parses user@host without TLD`() { fun parseEmailWithoutTLD() {
val identifier = GpgIdentifier.fromString("john.doe@example") val identifier = GpgIdentifier.fromString("john.doe@example")
assertNotNull(identifier) assertNotNull(identifier)
assertTrue { identifier is GpgIdentifier.UserId } assertTrue { identifier is GpgIdentifier.UserId }

View file

@ -17,7 +17,10 @@ afterEvaluate {
} }
tasks.withType<KotlinCompile>().configureEach { tasks.withType<KotlinCompile>().configureEach {
kotlinOptions { jvmTarget = JavaVersion.VERSION_11.toString() } kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.toString()
freeCompilerArgs = freeCompilerArgs + "-Xsam-conversions=class"
}
} }
} }

View file

@ -17,7 +17,10 @@ afterEvaluate {
} }
tasks.withType<KotlinCompile>().configureEach { tasks.withType<KotlinCompile>().configureEach {
kotlinOptions { jvmTarget = JavaVersion.VERSION_11.toString() } kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.toString()
freeCompilerArgs = freeCompilerArgs + "-Xsam-conversions=class"
}
} }
} }

View file

@ -17,7 +17,10 @@ afterEvaluate {
} }
tasks.withType<KotlinCompile>().configureEach { tasks.withType<KotlinCompile>().configureEach {
kotlinOptions { jvmTarget = JavaVersion.VERSION_11.toString() } kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.toString()
freeCompilerArgs = freeCompilerArgs + "-Xsam-conversions=class"
}
} }
} }

View file

@ -22,15 +22,15 @@ public object KeyDeletionFailedException : KeyManagerException("Couldn't delete
public object InvalidKeyException : public object InvalidKeyException :
KeyManagerException("Given key cannot be parsed as a known key type") KeyManagerException("Given key cannot be parsed as a known key type")
/** No key matching [keyId] could be found. */ /** No key matching `keyId` could be found. */
public class KeyNotFoundException(keyId: String) : public class KeyNotFoundException(keyId: String) :
KeyManagerException("No key found with id: $keyId") KeyManagerException("No key found with id: $keyId")
/** Attempting to add another key for [keyId] without requesting a replace. */ /** Attempting to add another key for `keyId` without requesting a replace. */
public class KeyAlreadyExistsException(keyId: String) : public class KeyAlreadyExistsException(keyId: String) :
KeyManagerException("Pre-existing key was found for $keyId") KeyManagerException("Pre-existing key was found for $keyId")
/** Sealed exception types for [CryptoHandler]. */ /** Sealed exception types for [dev.msfjarvis.aps.crypto.CryptoHandler]. */
public sealed class CryptoHandlerException(message: String? = null, cause: Throwable? = null) : public sealed class CryptoHandlerException(message: String? = null, cause: Throwable? = null) :
CryptoException(message, cause) CryptoException(message, cause)

View file

@ -12,28 +12,28 @@ import kotlin.test.assertTrue
class GpgIdentifierTest { class GpgIdentifierTest {
@Test @Test
fun `parses hexadecimal key id without leading 0x`() { fun parseHexKeyIdWithout0xPrefix() {
val identifier = GpgIdentifier.fromString("79E8208280490C77") val identifier = GpgIdentifier.fromString("79E8208280490C77")
assertNotNull(identifier) assertNotNull(identifier)
assertTrue { identifier is GpgIdentifier.KeyId } assertTrue { identifier is GpgIdentifier.KeyId }
} }
@Test @Test
fun `parses hexadecimal key id`() { fun parseHexKeyId() {
val identifier = GpgIdentifier.fromString("0x79E8208280490C77") val identifier = GpgIdentifier.fromString("0x79E8208280490C77")
assertNotNull(identifier) assertNotNull(identifier)
assertTrue { identifier is GpgIdentifier.KeyId } assertTrue { identifier is GpgIdentifier.KeyId }
} }
@Test @Test
fun `parses email as user id`() { fun parseValidEmail() {
val identifier = GpgIdentifier.fromString("john.doe@example.org") val identifier = GpgIdentifier.fromString("john.doe@example.org")
assertNotNull(identifier) assertNotNull(identifier)
assertTrue { identifier is GpgIdentifier.UserId } assertTrue { identifier is GpgIdentifier.UserId }
} }
@Test @Test
fun `parses user@host without TLD`() { fun parseEmailWithoutTLD() {
val identifier = GpgIdentifier.fromString("john.doe@example") val identifier = GpgIdentifier.fromString("john.doe@example")
assertNotNull(identifier) assertNotNull(identifier)
assertTrue { identifier is GpgIdentifier.UserId } assertTrue { identifier is GpgIdentifier.UserId }

View file

@ -11,7 +11,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing
class KeyUtilsTest { class KeyUtilsTest {
@Test @Test
fun `parse GPG key with multiple identities`() { fun parseKeyWithMultipleIdentities() {
val key = PGPKey(getArmoredPrivateKeyWithMultipleIdentities()) val key = PGPKey(getArmoredPrivateKeyWithMultipleIdentities())
val keyring = tryParseKeyring(key) val keyring = tryParseKeyring(key)
assertNotNull(keyring) assertNotNull(keyring)

View file

@ -51,7 +51,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testAddingKey() = fun addKey() =
scope.runTest { scope.runTest {
// Check if the key id returned is correct // Check if the key id returned is correct
val keyId = keyManager.getKeyId(keyManager.addKey(key).unwrap()) val keyId = keyManager.getKeyId(keyManager.addKey(key).unwrap())
@ -66,7 +66,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testAddingKeyWithoutReplaceFlag() = fun addKeyWithoutReplaceFlag() =
scope.runTest { scope.runTest {
// Check adding the keys twice // Check adding the keys twice
keyManager.addKey(key, false).unwrap() keyManager.addKey(key, false).unwrap()
@ -76,7 +76,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testAddingKeyWithReplaceFlag() = fun addKeyWithReplaceFlag() =
scope.runTest { scope.runTest {
// Check adding the keys twice // Check adding the keys twice
keyManager.addKey(key, true).unwrap() keyManager.addKey(key, true).unwrap()
@ -86,7 +86,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testRemovingKey() = fun removeKey() =
scope.runTest { scope.runTest {
// Add key using KeyManager // Add key using KeyManager
keyManager.addKey(key).unwrap() keyManager.addKey(key).unwrap()
@ -101,7 +101,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testGetExistingKeyById() = fun getKeyById() =
scope.runTest { scope.runTest {
// Add key using KeyManager // Add key using KeyManager
keyManager.addKey(key).unwrap() keyManager.addKey(key).unwrap()
@ -116,7 +116,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testGetExistingKeyByFullUserId() = fun getKeyByFullUserId() =
scope.runTest { scope.runTest {
keyManager.addKey(key).unwrap() keyManager.addKey(key).unwrap()
@ -126,7 +126,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testGetExistingKeyByEmailUserId() = fun getKeyByEmailUserId() =
scope.runTest { scope.runTest {
keyManager.addKey(key).unwrap() keyManager.addKey(key).unwrap()
@ -136,7 +136,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testGetNonExistentKey() = fun getNonExistentKey() =
scope.runTest { scope.runTest {
// Add key using KeyManager // Add key using KeyManager
keyManager.addKey(key).unwrap() keyManager.addKey(key).unwrap()
@ -150,7 +150,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testFindKeysWithoutAdding() = fun findNonExistentKey() =
scope.runTest { scope.runTest {
// Check returned key // Check returned key
val error = keyManager.getKeyById(KeyId(0x08edf7567183ce44)).unwrapError() val error = keyManager.getKeyById(KeyId(0x08edf7567183ce44)).unwrapError()
@ -159,7 +159,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testGettingAllKeys() = fun getAllKeys() =
scope.runTest { scope.runTest {
// TODO: Should we check for more than 1 keys? // TODO: Should we check for more than 1 keys?
// Check if KeyManager returns no key // Check if KeyManager returns no key
@ -175,7 +175,7 @@ class PGPKeyManagerTest {
} }
@Test @Test
fun testGettingMultipleIdentityKeyWithBothUserIDs() { fun getMultipleIdentityKeyWithAllIdentities() {
scope.runTest { scope.runTest {
val key = PGPKey(getArmoredPrivateKeyWithMultipleIdentities()) val key = PGPKey(getArmoredPrivateKeyWithMultipleIdentities())
keyManager.addKey(key).unwrap() keyManager.addKey(key).unwrap()

View file

@ -34,7 +34,7 @@ class PasswordEntryTest {
) )
@Test @Test
fun testGetPassword() { fun getPassword() {
assertEquals("fooooo", makeEntry("fooooo\nbla\n").password) assertEquals("fooooo", makeEntry("fooooo\nbla\n").password)
assertEquals("fooooo", makeEntry("fooooo\nbla").password) assertEquals("fooooo", makeEntry("fooooo\nbla").password)
assertEquals("fooooo", makeEntry("fooooo\n").password) assertEquals("fooooo", makeEntry("fooooo\n").password)
@ -52,7 +52,7 @@ class PasswordEntryTest {
} }
@Test @Test
fun testGetExtraContent() { fun getExtraContent() {
assertEquals("bla\n", makeEntry("fooooo\nbla\n").extraContentString) assertEquals("bla\n", makeEntry("fooooo\nbla\n").extraContentString)
assertEquals("bla", makeEntry("fooooo\nbla").extraContentString) assertEquals("bla", makeEntry("fooooo\nbla").extraContentString)
assertEquals("", makeEntry("fooooo\n").extraContentString) assertEquals("", makeEntry("fooooo\n").extraContentString)
@ -103,7 +103,7 @@ class PasswordEntryTest {
} }
@Test @Test
fun testGetUsername() { fun getUsername() {
for (field in PasswordEntry.USERNAME_FIELDS) { for (field in PasswordEntry.USERNAME_FIELDS) {
assertEquals("username", makeEntry("\n$field username").username) assertEquals("username", makeEntry("\n$field username").username)
assertEquals( assertEquals(
@ -123,7 +123,7 @@ class PasswordEntryTest {
} }
@Test @Test
fun testHasUsername() { fun hasUsername() {
assertNotNull(makeEntry("secret\nextra\nlogin: username\ncontent\n").username) assertNotNull(makeEntry("secret\nextra\nlogin: username\ncontent\n").username)
assertNull(makeEntry("secret\nextra\ncontent\n").username) assertNull(makeEntry("secret\nextra\ncontent\n").username)
assertNull(makeEntry("secret\nlogin failed\n").username) assertNull(makeEntry("secret\nlogin failed\n").username)
@ -132,7 +132,7 @@ class PasswordEntryTest {
} }
@Test @Test
fun testGeneratesOtpFromTotpUri() = runTest { fun generatesOtpFromTotpUri() = runTest {
val entry = makeEntry("secret\nextra\n$TOTP_URI") val entry = makeEntry("secret\nextra\n$TOTP_URI")
assertTrue(entry.hasTotp()) assertTrue(entry.hasTotp())
entry.totp.test { entry.totp.test {
@ -149,7 +149,7 @@ class PasswordEntryTest {
* blocked https://msfjarvis.dev/aps/issue/1550. * blocked https://msfjarvis.dev/aps/issue/1550.
*/ */
@Test @Test
fun testGeneratedOtpHasCorrectRemainingTime() = runTest { fun generatedOtpHasCorrectRemainingTime() = runTest {
val entry = makeEntry("secret\nextra\n$TOTP_URI", TestUserClock.withAddedSeconds(5)) val entry = makeEntry("secret\nextra\n$TOTP_URI", TestUserClock.withAddedSeconds(5))
assertTrue(entry.hasTotp()) assertTrue(entry.hasTotp())
entry.totp.test { entry.totp.test {
@ -161,7 +161,7 @@ class PasswordEntryTest {
} }
@Test @Test
fun testGeneratesOtpWithOnlyUriInFile() = runTest { fun generatesOtpWithOnlyUriInFile() = runTest {
val entry = makeEntry(TOTP_URI) val entry = makeEntry(TOTP_URI)
assertNull(entry.password) assertNull(entry.password)
entry.totp.test { entry.totp.test {
@ -173,7 +173,7 @@ class PasswordEntryTest {
} }
@Test @Test
fun testOnlyLooksForUriInFirstLine() { fun onlyLooksForUriInFirstLine() {
val entry = makeEntry("id:\n$TOTP_URI") val entry = makeEntry("id:\n$TOTP_URI")
assertNotNull(entry.password) assertNotNull(entry.password)
assertTrue(entry.hasTotp()) assertTrue(entry.hasTotp())

View file

@ -24,7 +24,7 @@ class OtpTest {
} }
@Test @Test
fun testOtpGeneration6Digits() { fun otpGeneration6Digits() {
assertEquals( assertEquals(
"953550", "953550",
generateOtp( generateOtp(
@ -46,7 +46,7 @@ class OtpTest {
} }
@Test @Test
fun testOtpGeneration10Digits() { fun otpGeneration10Digits() {
assertEquals( assertEquals(
"0740900914", "0740900914",
generateOtp( generateOtp(
@ -71,7 +71,7 @@ class OtpTest {
} }
@Test @Test
fun testOtpGenerationIllegalInput() { fun otpGenerationIllegalInput() {
assertNull( assertNull(
generateOtp( generateOtp(
counter = 10000, counter = 10000,
@ -108,7 +108,7 @@ class OtpTest {
} }
@Test @Test
fun testOtpGenerationUnusualSecrets() { fun otpGenerationUnusualSecrets() {
assertEquals( assertEquals(
"127764", "127764",
generateOtp( generateOtp(
@ -126,7 +126,7 @@ class OtpTest {
} }
@Test @Test
fun testOtpGenerationUnpaddedSecrets() { fun otpGenerationUnpaddedSecrets() {
// Secret was generated with `echo 'string with some padding needed' | base32` // Secret was generated with `echo 'string with some padding needed' | base32`
// We don't care for the resultant OTP's actual value, we just want both the padded and // We don't care for the resultant OTP's actual value, we just want both the padded and
// unpadded variant to generate the same one. // unpadded variant to generate the same one.

View file

@ -15,7 +15,7 @@ class DicewarePassphraseGeneratorTest {
private val intGenerator = RandomIntGenerator { it.random(random) } private val intGenerator = RandomIntGenerator { it.random(random) }
@Test @Test
fun generate_passphrase() { fun generatePassphrase() {
val die = Die(6, intGenerator) val die = Die(6, intGenerator)
val generator = val generator =

View file

@ -17,19 +17,19 @@ class DieTest {
private val intGenerator = RandomIntGenerator { it.random(random) } private val intGenerator = RandomIntGenerator { it.random(random) }
@Test @Test
fun test_one_roll() { fun oneRoll() {
val die = Die(6, intGenerator) val die = Die(6, intGenerator)
assertEquals(5, die.roll()) assertEquals(5, die.roll())
} }
@Test @Test
fun test_multiple_rolls() { fun multipleRolls() {
val die = Die(6, intGenerator) val die = Die(6, intGenerator)
assertEquals(526242, die.rollMultiple(6)) assertEquals(526242, die.rollMultiple(6))
} }
@Test @Test
fun test_consecutive_rolls() { fun consecutiveRolls() {
val die = Die(6, intGenerator) val die = Die(6, intGenerator)
assertEquals(5, die.roll()) assertEquals(5, die.roll())
assertEquals(2, die.roll()) assertEquals(2, die.roll())
@ -40,7 +40,7 @@ class DieTest {
} }
@Test @Test
fun test_100_sides() { fun hundredSides() {
val die = Die(100, intGenerator) val die = Die(100, intGenerator)
assertEquals(67, die.roll()) assertEquals(67, die.roll())
} }