Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / securemail / src / test / java / com / vayunmathur / securemail / platform / OutboxRetryTest.kt

OutboxRetryTest.kt126 lines · 6.0 KiBRaw
1package com.vayunmathur.securemail.platform
2
3import com.vayunmathur.securemail.data.CachedKey
4import com.vayunmathur.securemail.data.MailIdentity
5import com.vayunmathur.securemail.data.OutboxMessage
6import com.vayunmathur.securemail.domain.pgp.PgpCrypto
7import kotlinx.coroutines.runBlocking
8import org.junit.runner.RunWith
9import org.robolectric.RobolectricTestRunner
10import kotlin.test.Test
11import kotlin.test.assertEquals
12import kotlin.test.assertFailsWith
13import kotlin.test.assertTrue
14
15/**
16 * [OutboxRetry] against a fake [com.vayunmathur.securemail.data.MailStore].
17 * The socket has no live session in tests, so publishes fail with the real
18 * "no session" reason — which is exactly the retry-stays-queued behaviour.
19 */
20@RunWith(RobolectricTestRunner::class)
21class OutboxRetryTest {
22 private val sender = MailIdentity(
23 address = "[email protected]", label = "", publicArmor = "pub", fingerprintHex = "AA",
24 secretArmor = "secret", isPrimary = true, createdMillis = 1,
25 )
26
27 private fun retry(
28 store: FakeMailStore,
29 passphrase: () -> String = { "passphrase-one" },
30 identity: suspend (String?) -> MailIdentity = { sender },
31 ) = OutboxRetry(store, passphrase, identity)
32
33 private fun entry(toJson: String, from: String? = "[email protected]", created: Long = 1) =
34 OutboxMessage(toJson = toJson, subject = "Hi", body = "Body", createdMillis = created, fromAddress = from)
35
36 @Test fun `empty outbox sends and fails nothing`() = runBlocking {
37 val store = FakeMailStore()
38 assertEquals(0 to 0, retry(store).retryAll())
39 assertTrue(store.outbox.noteAttemptCalls.isEmpty())
40 }
41
42 @Test fun `plain recipient stays queued with publish reason when offline`() = runBlocking {
43 val store = FakeMailStore()
44 val id = store.outbox.insert(entry("[email protected]"))
45 assertEquals(0 to 1, retry(store).retryAll())
46 assertEquals(1, store.outbox.all().size)
47 val attempted = store.outbox.all().single()
48 assertEquals(1, attempted.attemptCount)
49 assertTrue(attempted.lastError!!.contains("publish failed"), attempted.lastError)
50 assertTrue(attempted.lastError!!.contains("no session"), attempted.lastError)
51 assertEquals(listOf(id to attempted.lastError), store.outbox.noteAttemptCalls.map { it.first to it.second })
52 }
53
54 @Test fun `one entry with two recipients fails once and records one attempt`() = runBlocking {
55 val store = FakeMailStore()
56 store.outbox.insert(entry("[email protected], [email protected]"))
57 assertEquals(0 to 1, retry(store).retryAll())
58 assertEquals(1, store.outbox.noteAttemptCalls.size)
59 assertEquals(1, store.outbox.all().single().attemptCount)
60 }
61
62 @Test fun `blank recipient tokens are ignored but entry still attempts publish path`() = runBlocking {
63 val store = FakeMailStore()
64 store.outbox.insert(entry(" , [email protected] , "))
65 assertEquals(0 to 1, retry(store).retryAll())
66 assertEquals(1, store.outbox.noteAttemptCalls.size)
67 }
68
69 @Test fun `cached key with malformed armor records encrypt failure without publishing`() = runBlocking {
70 val store = FakeMailStore()
71 store.keys.upsert(CachedKey("[email protected]", "not a key", "FF", "Cached", 1))
72 store.outbox.insert(entry("[email protected]"))
73 assertEquals(0 to 1, retry(store).retryAll())
74 val error = store.outbox.all().single().lastError!!
75 assertTrue(error.contains("encrypt failed"), error)
76 }
77
78 @Test fun `cached key encrypts with real keys then fails at offline publish`() = runBlocking {
79 val store = FakeMailStore()
80 val recipient = PgpCrypto.generateIdentity("Ada <[email protected]>", "recipient-pass")
81 val mine = PgpCrypto.generateIdentity("Me <[email protected]>", "passphrase-one")
82 store.keys.upsert(CachedKey("[email protected]", recipient.armoredPublic, recipient.fingerprintHex, "Cached", 1))
83 store.outbox.insert(entry("[email protected]"))
84 val realSender = sender.copy(secretArmor = mine.armoredSecret, publicArmor = mine.armoredPublic)
85 assertEquals(0 to 1, retry(store, identity = { realSender }).retryAll())
86 val error = store.outbox.all().single().lastError!!
87 assertTrue(error.contains("publish failed"), error)
88 }
89
90 @Test fun `sender identity failure is recorded as outbox error`() = runBlocking {
91 val store = FakeMailStore()
92 store.outbox.insert(entry("[email protected]"))
93 val failing = retry(store, identity = { throw IllegalStateException("no identity yet") })
94 assertEquals(0 to 1, failing.retryAll())
95 assertEquals("outbox error: no identity yet", store.outbox.all().single().lastError)
96 }
97
98 @Test fun `multiple entries are attempted in order and counted separately`() = runBlocking {
99 val store = FakeMailStore()
100 store.outbox.insert(entry("[email protected]", created = 2))
101 store.outbox.insert(entry("[email protected]", created = 1))
102 assertEquals(0 to 2, retry(store).retryAll())
103 assertEquals(2, store.outbox.noteAttemptCalls.size)
104 assertTrue(store.outbox.all().all { it.attemptCount == 1 })
105 }
106
107 @Test fun `missing passphrase aborts the whole retry before any attempt`() = runBlocking {
108 val store = FakeMailStore()
109 store.outbox.insert(entry("[email protected]"))
110 assertFailsWith<IllegalStateException> {
111 retry(store, passphrase = { throw IllegalStateException("locked") }).retryAll()
112 }
113 assertTrue(store.outbox.noteAttemptCalls.isEmpty())
114 assertEquals(0, store.outbox.all().single().attemptCount)
115 }
116
117 @Test fun `buildMimeBody and buildEncryptedBody wrap content`() {
118 val mime = buildMimeBody("[email protected]", "Hello", "Body text")
119 val text = mime.toString(Charsets.UTF_8)
120 assertTrue(text.contains("Hello"), text)
121 assertTrue(text.contains("Body text"), text)
122 val envelope = buildEncryptedBody(byteArrayOf(1, 2, 3))
123 assertTrue(envelope.isNotEmpty())
124 assertTrue(envelope.toString(Charsets.UTF_8).contains("PGP MESSAGE"))
125 }
126}