|
| 1 | +package dev.pi.postbox.question |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.test.platform.app.InstrumentationRegistry |
| 5 | +import dev.pi.postbox.R |
| 6 | +import java.io.File |
| 7 | +import java.io.IOException |
| 8 | +import java.security.KeyStore |
| 9 | +import java.util.UUID |
| 10 | +import kotlinx.coroutines.runBlocking |
| 11 | +import org.junit.After |
| 12 | +import org.junit.Assert.assertEquals |
| 13 | +import org.junit.Assert.assertFalse |
| 14 | +import org.junit.Assert.assertNull |
| 15 | +import org.junit.Assert.assertTrue |
| 16 | +import org.junit.Before |
| 17 | +import org.junit.Test |
| 18 | +import org.xmlpull.v1.XmlPullParser |
| 19 | + |
| 20 | +class AndroidKeystoreQuestionDraftStoreTest { |
| 21 | + private lateinit var context: Context |
| 22 | + private lateinit var rootDirectory: File |
| 23 | + private lateinit var keyAlias: String |
| 24 | + |
| 25 | + @Before |
| 26 | + fun setUp() { |
| 27 | + context = InstrumentationRegistry.getInstrumentation().targetContext |
| 28 | + rootDirectory = File(context.noBackupFilesDir, "question_drafts_test_${UUID.randomUUID()}") |
| 29 | + keyAlias = "dev.pi.postbox.question_drafts_test_${UUID.randomUUID()}" |
| 30 | + } |
| 31 | + |
| 32 | + @After |
| 33 | + fun tearDown() { |
| 34 | + rootDirectory.deleteRecursively() |
| 35 | + val keyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) } |
| 36 | + if (keyStore.containsAlias(keyAlias)) keyStore.deleteEntry(keyAlias) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun productionRootAndBothRuleFormatsExcludeDraftsFromBackupAndTransfer() { |
| 41 | + val productionRoot = AndroidKeystoreQuestionDraftStore.defaultRootDirectory(context) |
| 42 | + assertTrue( |
| 43 | + productionRoot.canonicalPath.startsWith(context.noBackupFilesDir.canonicalPath + File.separator) |
| 44 | + ) |
| 45 | + assertEquals("question_drafts_v1", productionRoot.name) |
| 46 | + assertEquals(1, countDraftExclusions(R.xml.backup_rules)) |
| 47 | + assertEquals(2, countDraftExclusions(R.xml.data_extraction_rules)) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun encryptedDraftRoundTripsAcrossStoreRecreationWithoutPlaintextIdentityOrContent() = runBlocking { |
| 52 | + val key = QuestionDraftKey("https://postbox.private.example/", "ask-secret-request") |
| 53 | + val draft = QuestionAnswerDraft( |
| 54 | + selectedValues = listOf("private-choice", "other"), |
| 55 | + note = "private note contents" |
| 56 | + ) |
| 57 | + val firstStore = store() |
| 58 | + |
| 59 | + assertEquals(QuestionDraftStoreResult.Success(Unit), firstStore.save(key, draft)) |
| 60 | + assertEquals(QuestionDraftStoreResult.Success(draft), store().load(key)) |
| 61 | + |
| 62 | + val files = rootDirectory.walkTopDown().filter { it.isFile }.toList() |
| 63 | + assertEquals(1, files.size) |
| 64 | + val path = files.single().relativeTo(rootDirectory).path |
| 65 | + val ciphertextText = files.single().readBytes().toString(Charsets.ISO_8859_1) |
| 66 | + listOf( |
| 67 | + "postbox.private.example", |
| 68 | + "ask-secret-request", |
| 69 | + "private-choice", |
| 70 | + "private note contents" |
| 71 | + ).forEach { secret -> |
| 72 | + assertFalse(path.contains(secret)) |
| 73 | + assertFalse(ciphertextText.contains(secret)) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun tamperedCiphertextReturnsFiniteCorruptFailure() = runBlocking { |
| 79 | + val key = QuestionDraftKey("https://postbox.example/", "ask-tamper") |
| 80 | + val draft = QuestionAnswerDraft(listOf("one"), "tamper check") |
| 81 | + val store = store() |
| 82 | + assertEquals(QuestionDraftStoreResult.Success(Unit), store.save(key, draft)) |
| 83 | + val encryptedFile = rootDirectory.walkTopDown().single { it.isFile } |
| 84 | + val tampered = encryptedFile.readBytes().also { bytes -> |
| 85 | + bytes[bytes.lastIndex] = (bytes.last().toInt() xor 0x01).toByte() |
| 86 | + } |
| 87 | + encryptedFile.writeBytes(tampered) |
| 88 | + |
| 89 | + assertEquals( |
| 90 | + QuestionDraftStoreResult.Failure(QuestionDraftStoreFailure.CORRUPT), |
| 91 | + store.load(key) |
| 92 | + ) |
| 93 | + assertEquals(QuestionDraftStoreResult.Success(null), store.load(key)) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun oversizedCiphertextReturnsFiniteCorruptFailureAndIsDiscarded() = runBlocking { |
| 98 | + val key = QuestionDraftKey("https://postbox.example/", "ask-oversized") |
| 99 | + val store = store() |
| 100 | + assertEquals( |
| 101 | + QuestionDraftStoreResult.Success(Unit), |
| 102 | + store.save(key, QuestionAnswerDraft(listOf("one"), "bounded")) |
| 103 | + ) |
| 104 | + rootDirectory.walkTopDown().single { it.isFile } |
| 105 | + .writeBytes(ByteArray(600_000)) |
| 106 | + |
| 107 | + assertEquals( |
| 108 | + QuestionDraftStoreResult.Failure(QuestionDraftStoreFailure.CORRUPT), |
| 109 | + store.load(key) |
| 110 | + ) |
| 111 | + assertEquals(QuestionDraftStoreResult.Success(null), store.load(key)) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun ciphertextSwappedBetweenDraftKeysFailsAuthenticationAndIsDiscarded() = runBlocking { |
| 116 | + val firstKey = QuestionDraftKey("https://first.example/", "ask-shared") |
| 117 | + val secondKey = QuestionDraftKey("https://second.example/", "ask-shared") |
| 118 | + val firstDraft = QuestionAnswerDraft(listOf("one"), "first") |
| 119 | + val secondDraft = QuestionAnswerDraft(listOf("other"), "second") |
| 120 | + val store = store() |
| 121 | + assertEquals(QuestionDraftStoreResult.Success(Unit), store.save(firstKey, firstDraft)) |
| 122 | + val firstFile = rootDirectory.walkTopDown().single { it.isFile } |
| 123 | + assertEquals(QuestionDraftStoreResult.Success(Unit), store.save(secondKey, secondDraft)) |
| 124 | + val secondFile = rootDirectory.walkTopDown() |
| 125 | + .filter { it.isFile && it != firstFile } |
| 126 | + .single() |
| 127 | + secondFile.writeBytes(firstFile.readBytes()) |
| 128 | + |
| 129 | + assertEquals( |
| 130 | + QuestionDraftStoreResult.Failure(QuestionDraftStoreFailure.CORRUPT), |
| 131 | + store.load(secondKey) |
| 132 | + ) |
| 133 | + assertEquals(QuestionDraftStoreResult.Success(null), store.load(secondKey)) |
| 134 | + assertEquals(QuestionDraftStoreResult.Success(firstDraft), store.load(firstKey)) |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + fun outOfBoundsDraftReturnsFiniteWriteFailureWithoutCreatingAFile() = runBlocking { |
| 139 | + val key = QuestionDraftKey("https://postbox.example/", "ask-too-large") |
| 140 | + val draft = QuestionAnswerDraft( |
| 141 | + selectedValues = List(MAX_QUESTION_DRAFT_SELECTED_VALUES + 1) { index -> "value-$index" }, |
| 142 | + note = "still in memory" |
| 143 | + ) |
| 144 | + val store = store() |
| 145 | + |
| 146 | + assertEquals( |
| 147 | + QuestionDraftStoreResult.Failure(QuestionDraftStoreFailure.WRITE_FAILED), |
| 148 | + store.save(key, draft) |
| 149 | + ) |
| 150 | + assertFalse(rootDirectory.walkTopDown().any { it.isFile }) |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + fun missingKeyReturnsFiniteFailureAndRetrySaveRecovers() = runBlocking { |
| 155 | + val key = QuestionDraftKey("https://postbox.example/", "ask-key-loss") |
| 156 | + val original = QuestionAnswerDraft(listOf("one"), "before key loss") |
| 157 | + val recovered = QuestionAnswerDraft(listOf("other"), "after key loss") |
| 158 | + val store = store() |
| 159 | + assertEquals(QuestionDraftStoreResult.Success(Unit), store.save(key, original)) |
| 160 | + KeyStore.getInstance("AndroidKeyStore").apply { |
| 161 | + load(null) |
| 162 | + deleteEntry(keyAlias) |
| 163 | + } |
| 164 | + |
| 165 | + assertEquals( |
| 166 | + QuestionDraftStoreResult.Failure(QuestionDraftStoreFailure.KEY_INVALIDATED), |
| 167 | + store().load(key) |
| 168 | + ) |
| 169 | + assertEquals(QuestionDraftStoreResult.Success(null), store().load(key)) |
| 170 | + assertEquals(QuestionDraftStoreResult.Success(Unit), store().save(key, recovered)) |
| 171 | + assertEquals(QuestionDraftStoreResult.Success(recovered), store().load(key)) |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + fun failedAtomicCommitPreservesOldDraftAndLaterSaveRecovers() = runBlocking { |
| 176 | + val key = QuestionDraftKey("https://postbox.example/", "ask-atomic") |
| 177 | + val oldDraft = QuestionAnswerDraft(listOf("one"), "old draft") |
| 178 | + val newDraft = QuestionAnswerDraft(listOf("other"), "new draft") |
| 179 | + assertEquals(QuestionDraftStoreResult.Success(Unit), store().save(key, oldDraft)) |
| 180 | + val failingStore = AndroidKeystoreQuestionDraftStore( |
| 181 | + rootDirectory = rootDirectory, |
| 182 | + keyAlias = keyAlias, |
| 183 | + atomicWriter = AndroidAtomicDraftWriter { |
| 184 | + throw IOException("simulated commit failure") |
| 185 | + } |
| 186 | + ) |
| 187 | + |
| 188 | + assertEquals( |
| 189 | + QuestionDraftStoreResult.Failure(QuestionDraftStoreFailure.WRITE_FAILED), |
| 190 | + failingStore.save(key, newDraft) |
| 191 | + ) |
| 192 | + assertEquals(QuestionDraftStoreResult.Success(oldDraft), store().load(key)) |
| 193 | + assertEquals(QuestionDraftStoreResult.Success(Unit), store().save(key, newDraft)) |
| 194 | + assertEquals(QuestionDraftStoreResult.Success(newDraft), store().load(key)) |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + fun deleteAndServerReconciliationRemoveOnlyTargetedDrafts() = runBlocking { |
| 199 | + val server = "https://postbox.example/" |
| 200 | + val keep = QuestionDraftKey(server, "ask-keep") |
| 201 | + val remove = QuestionDraftKey(server, "ask-remove") |
| 202 | + val otherServer = QuestionDraftKey("https://other.example/", "ask-remove") |
| 203 | + val draft = QuestionAnswerDraft(listOf("other"), "draft") |
| 204 | + val store = store() |
| 205 | + listOf(keep, remove, otherServer).forEach { key -> |
| 206 | + assertEquals(QuestionDraftStoreResult.Success(Unit), store.save(key, draft)) |
| 207 | + } |
| 208 | + |
| 209 | + assertEquals( |
| 210 | + QuestionDraftStoreResult.Success(Unit), |
| 211 | + store.reconcileServer(server, setOf(keep.requestId)) |
| 212 | + ) |
| 213 | + assertEquals(QuestionDraftStoreResult.Success(draft), store.load(keep)) |
| 214 | + assertEquals(QuestionDraftStoreResult.Success(null), store.load(remove)) |
| 215 | + assertEquals(QuestionDraftStoreResult.Success(draft), store.load(otherServer)) |
| 216 | + |
| 217 | + assertEquals(QuestionDraftStoreResult.Success(Unit), store.delete(keep)) |
| 218 | + assertEquals(QuestionDraftStoreResult.Success(null), store.load(keep)) |
| 219 | + assertTrue(rootDirectory.exists()) |
| 220 | + assertNull(rootDirectory.listFiles()?.firstOrNull { it.name.contains("ask-keep") }) |
| 221 | + } |
| 222 | + |
| 223 | + private fun countDraftExclusions(resourceId: Int): Int { |
| 224 | + val parser = context.resources.getXml(resourceId) |
| 225 | + var count = 0 |
| 226 | + while (parser.eventType != XmlPullParser.END_DOCUMENT) { |
| 227 | + if ( |
| 228 | + parser.eventType == XmlPullParser.START_TAG && |
| 229 | + parser.name == "exclude" && |
| 230 | + parser.getAttributeValue(null, "domain") == "root" && |
| 231 | + parser.getAttributeValue(null, "path") == "no_backup/question_drafts_v1" |
| 232 | + ) { |
| 233 | + count += 1 |
| 234 | + } |
| 235 | + parser.next() |
| 236 | + } |
| 237 | + parser.close() |
| 238 | + return count |
| 239 | + } |
| 240 | + |
| 241 | + private fun store() = AndroidKeystoreQuestionDraftStore( |
| 242 | + rootDirectory = rootDirectory, |
| 243 | + keyAlias = keyAlias |
| 244 | + ) |
| 245 | +} |
0 commit comments