Modern Apps git

← Modern-Apps.git

securemail: inject MailStore and fetchers into inbound/key resolution

Commit
9119fa0ed8aa7f3184f1d95209d47e73906e1574
Author
Vayun Mathur <[email protected]>
When
2026-11-07 10:57 UTC (29 days ago)
Parents
f59e7c7
InboundStore.store and SenderKeys.resolve were object
entry points hard-wired to MailRepository.get, the
Keystore passphrase and the real relay/WKD fetchers, so
the decrypt-on-arrival and key-resolution logic could
not run in a JVM test at all (SQLCipher vault plus real
network). Extract internal cores taking a MailStore, the
resolved identity/passphrase, and fetcher lambdas; the
public entry points wire in exactly the same production
collaborators. No behavior changes.

Also adds the androidx.work:work-testing test dependency
(same catalog entry other modules use) for the sync
worker test.

(cherry picked from commit 6329c814c248593c1184a60576558891173a1f47)

Diff

securemail/build.gradle.kts
diff --git a/securemail/build.gradle.kts b/securemail/build.gradle.ktsindex 44336c3e8e..4d42dbfee1 100644--- a/securemail/build.gradle.kts+++ b/securemail/build.gradle.kts@@ -41,4 +41,5 @@ dependencies {     // Split out of :library:e2ee-p2p so non-mail apps don't link rPGP (#873);     // securemail is the only consumer of the PGP surface.     implementation(project(":library:e2ee-openpgp"))+    testImplementation(libs.androidx.work.testing) }
securemail/src/main/java/com/vayunmathur/securemail/data/InboundStore.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/data/InboundStore.kt b/securemail/src/main/java/com/vayunmathur/securemail/data/InboundStore.ktindex 626f0c7a8d..abea0e584f 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/data/InboundStore.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/data/InboundStore.kt@@ -26,8 +26,28 @@ object InboundStore {      */     suspend fun store(context: Context, msg: MailSocket.InboundMsg) {         val app = context.applicationContext-        val repo = MailRepository.get(app)-        val identity = Identities.forDelivery(app, msg.deliveredTo)+        store(+            store = MailRepository.get(app),+            identity = Identities.forDelivery(app, msg.deliveredTo),+            passphrase = KeyManager(app).getKeyPassphrase(),+            senderKeyResolver = { SenderKeys.resolve(app, it) },+            msg = msg,+        )+    }++    /**+     * Testable core of [store]: persistence, identity and key resolution+     * are injected so the decrypt-on-arrival logic can run against an+     * in-memory [MailStore] on a JVM host.+     */+    internal suspend fun store(+        store: MailStore,+        identity: MailIdentity?,+        passphrase: String?,+        senderKeyResolver: suspend (String) -> String?,+        msg: MailSocket.InboundMsg,+    ) {+        val repo = store         val deliveredTo = msg.deliveredTo.ifBlank { identity?.address.orEmpty() }         val folder = if (msg.flags.spam) MailFolder.SPAM else MailFolder.INBOX         val raw = msg.raw@@ -51,7 +71,6 @@ object InboundStore {             )             return         }-        val passphrase = KeyManager(app).getKeyPassphrase()         val stored = if (passphrase != null && identity != null) {             runCatching {                 val pgpB64 = MimeCodec.extractPgpPayloadB64(raw)@@ -71,7 +90,7 @@ object InboundStore {                 ).toString(Charsets.UTF_8)                 val (_, _, claimedFrom) = MimeCodec.parseMime(provisional)                 val (mime, verified) = if (claimedFrom.isNotBlank()) {-                    val senderKey = SenderKeys.resolve(app, claimedFrom)+                    val senderKey = senderKeyResolver(claimedFrom)                     if (senderKey != null) {                         val checked = PgpCrypto.decryptVerify(                             pgpBytes,
securemail/src/main/java/com/vayunmathur/securemail/data/SenderKeys.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/data/SenderKeys.kt b/securemail/src/main/java/com/vayunmathur/securemail/data/SenderKeys.ktindex ee96ad0cd8..96c26c94f7 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/data/SenderKeys.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/data/SenderKeys.kt@@ -20,28 +20,44 @@ object SenderKeys {      * Returns the armored public key for [address], or null when no WKD key      * exists (caller sends/stores unencrypted instead — never an error).      */-    suspend fun resolve(context: Context, address: String): String? {-        val app = context.applicationContext+    suspend fun resolve(context: Context, address: String): String? =+        resolve(+            store = MailRepository.get(context.applicationContext),+            address = address,+            socketKeyFetcher = { runCatching { MailSocket.getKey(it) }.getOrNull()?.armoredPublic },+            wkdKeyFetcher = { KeyLookupHttp.fetchWkdKey(it) },+        )++    /**+     * Testable core of [resolve]: the vault and both network fetchers are+     * injected so tests never reach the real relay or WKD hosts.+     */+    internal suspend fun resolve(+        store: MailStore,+        address: String,+        socketKeyFetcher: suspend (String) -> String?,+        wkdKeyFetcher: suspend (String) -> String?,+    ): String? {         val clean = address.trim().lowercase()         if (clean.isEmpty()) return null-        val repo = MailRepository.get(app)+        val repo = store         repo.keyDao().get(clean)?.let { return it.armoredPublic }         if (AddressValidator.isOwnDomain(clean)) {-            val hit = runCatching { MailSocket.getKey(clean) }.getOrNull()-            if (hit?.armoredPublic != null) {-                cacheKey(repo, clean, hit.armoredPublic, "OwnDirectory")-                return hit.armoredPublic+            val hit = socketKeyFetcher(clean)+            if (hit != null) {+                cacheKey(repo, clean, hit, "OwnDirectory")+                return hit             }         }         for (url in KeyLookupPolicy.wkdUrls(clean)) {-            val wkd = KeyLookupHttp.fetchWkdKey(url) ?: continue+            val wkd = wkdKeyFetcher(url) ?: continue             cacheKey(repo, clean, wkd, "Wkd")             return wkd         }         return null     } -    private suspend fun cacheKey(repo: MailRepository, address: String, armor: String, source: String) {+    private suspend fun cacheKey(repo: MailStore, address: String, armor: String, source: String) {         val fingerprint = runCatching { PgpCrypto.fingerprintHex(armor) }.getOrNull() ?: return         repo.keyDao().upsert(             CachedKey(