6b929bcc1884
exercise diversified 250-entry Codex PDF recovery on device (v2.17.39)
- time
- 2026-09-15 01:13 UTC · 2 months ago
- machine
- koh2
- agent
- gpt-5
- session
- android data path assurance
- parent
- d000a5ceb81e
↓ peach-android-6b929bcc.tar.gzatlas at this save
files changed3 files
VERSION+1 −1app/build.gradle+2 −0app/src/androidTest/java/com/peachpasswords/codex/CodexPdfRoundTripTest.kt+107 −61app/build.gradle+2 −0
@@ -148,6 +148,8 @@148148 testOptions {149149 unitTests.returnDefaultValues = true150150 unitTests.all {151+ def harnessNonce = System.getenv("PEACH_SYNC_HARNESS_NONCE")152+ if (harnessNonce != null) inputs.property("peachSyncHarnessNonce", harnessNonce)151153 def coreDir = rootProject.file("../peach-browser/crypto-core")152154 def nativeLibrary = new File(153155 coreDir,app/src/androidTest/java/com/peachpasswords/codex/CodexPdfRoundTripTest.kt+107 −61
@@ -2,81 +2,127 @@22 33 import androidx.test.ext.junit.runners.AndroidJUnit444 import androidx.test.platform.app.InstrumentationRegistry5+import com.peachpasswords.vault.CreateVaultResult6+import com.peachpasswords.vault.PasskeyData7+import com.peachpasswords.vault.StoredVault8+import com.peachpasswords.vault.UnlockResult9+import com.peachpasswords.vault.VaultEntry10+import com.peachpasswords.vault.VaultRepository11+import com.peachpasswords.vault.VaultSettings12+import com.peachpasswords.vault.VaultStorage13+import java.io.File14+import java.io.FileOutputStream15+import kotlinx.coroutines.runBlocking516 import org.junit.Assert.assertEquals17+import org.junit.Assert.assertTrue618 import org.junit.Test719 import org.junit.runner.RunWith8-import java.io.File9-import java.io.FileOutputStream1020 11-/**12- * End-to-end regression test for the Codex PDF round trip: render a page of13- * Aztec codes to PDF bytes, write them to disk, then decode the same file14- * back via [CodexRestore] exactly as the real restore flow does (SAF-style15- * file URI, [android.graphics.pdf.PdfRenderer], per-cell crop). This is the16- * real bug surface: the data-layer (compression, Reed-Solomon, envelope17- * integrity) already has coverage in [CodexSuiteTest], but nothing previously18- * exercised the actual PDF render → rasterize → decode pipeline, which is19- * exactly where the bug lived (PdfDocument's raster bitmap embedding20- * corrupted crisp module edges, and 640px crop resolution was independently21- * too low once codes needed more than a couple of Aztec layers).22- */2321 @RunWith(AndroidJUnit4::class)2422 class CodexPdfRoundTripTest {2523 2624 @Test27- fun pdfRoundTripDecodesAllCodesAtRealisticScale() {25+ fun diversified250EntryVaultRoundTripsThroughProductionPdf() = runBlocking {26+ val source = VaultRepository(MemoryStorage())27+ assertEquals(CreateVaultResult.Success, source.createVault(MASTER_PASSWORD))28+ diversifiedEntries().forEach { source.addEntry(it) }29+30+ val expected = source.getEntries()31+ assertEquals(250, expected.size)32+33+ val (pdfBytes, manifest) = CodexGenerator.generate(source, now = 1_700_000_000_000)34+ assertEquals(250, manifest.entryCount)35+ assertTrue(manifest.stripeCount > 1)36+2837 val context = InstrumentationRegistry.getInstrumentation().targetContext38+ val pdfFile = File(context.cacheDir, "codex_250_entry_roundtrip.pdf")39+ FileOutputStream(pdfFile).use { it.write(pdfBytes) }2940 30- // ~75 envelopes: matches the code count a real multi-hundred-entry31- // vault's Codex produces. Pseudo-random filler (not a single repeated32- // character) so deflate can't collapse it to a trivial size.33- val filler = StringBuilder()34- val rnd = java.util.Random(42)35- repeat(36000) { filler.append(('a' + rnd.nextInt(26))) }36- val fakePayload = CodexPayload(37- verifyCode = "PX-TEST-0000",38- vaultBlob = filler.toString(),39- timestamp = 0L,40- entryCount = 1,41- vaultFingerprint = CodexCodec.sha256Hex(filler.toString().toByteArray(Charsets.UTF_8)).take(32),42- generatedAt = 0L,43- )44- val payloadJson = CodexCodec.json.encodeToString(CodexPayload.serializer(), fakePayload)45- val compressed = CodexCodec.deflate(payloadJson.toByteArray(Charsets.UTF_8))46- val (envelopes, layout) = CodexCodec.encodeToEnvelopes(compressed, "PX-TEST-0000")41+ val decoded = CodexRestore.decodeFromUri(context, android.net.Uri.fromFile(pdfFile))42+ assertEquals(manifest.totalChunks, decoded.envelopes.size)43+ val payload = CodexRestore.reconstructPayload(decoded.envelopes)4744 48- val aztecs = envelopes.map { env ->49- val jsonStr = CodexCodec.json.encodeToString(ChunkEnvelope.serializer(), env)50- CodexAztec.encodeMatrix(jsonStr)51- }45+ val restored = VaultRepository(MemoryStorage())46+ assertEquals(UnlockResult.Success, restored.restoreFromCodex(payload.vaultBlob, MASTER_PASSWORD))47+ val actual = restored.getEntries()5248 53- val manifest = CodexManifest(54- verifyCode = "PX-TEST-0000",55- totalChunks = layout.totalChunks,56- dataChunks = layout.dataChunks,57- parityChunks = layout.parityChunks,58- stripeCount = layout.stripeCount,59- pageCount = Math.ceil(layout.totalChunks.toDouble() / CODES_PER_PAGE).toInt(),60- entryCount = 1,61- vaultFingerprint = "test",62- generatedAt = 0L,63- )64- val pdfBytes = CodexPdf.render(aztecs, manifest)49+ assertEquals(mapOf("login" to 63, "note" to 63, "card" to 62, "passkey" to 62), actual.groupingBy { it.type }.eachCount())50+ assertEquals(expected.map { it.title }.toSet(), actual.map { it.title }.toSet())51+ assertTrue(actual.any { it.password == "pāssword-000-🔐-${"x".repeat(64)}" })52+ assertTrue(actual.any { it.notes == "private note 249\n日本語 🌸" })53+ assertTrue(actual.any { it.cardNumber == "4000000000000246" && it.cvc == "246" })54+ assertTrue(actual.any { it.passkey?.credentialId == "credential-247" && it.passkey.privateKeyJwk?.contains("secret-247") == true })55+ }6556 66- val pdfFile = File(context.cacheDir, "roundtrip_test.pdf")67- FileOutputStream(pdfFile).use { it.write(pdfBytes) }68- val uri = android.net.Uri.fromFile(pdfFile)57+ private fun diversifiedEntries(): List<VaultEntry> = List(250) { index ->58+ val suffix = index.toString().padStart(3, '0')59+ val timestamp = 1_700_000_000_000 + index60+ when (index % 4) {61+ 0 -> VaultEntry(62+ id = "login-$suffix", type = "login", title = "Login $suffix — café",63+ username = "[email protected]", email = "[email protected]",64+ password = "pāssword-$suffix-🔐-${"x".repeat(64 + index % 17)}",65+ url = "https://login-$suffix.example.com/account",66+ additionalUrls = listOf("https://alt-$suffix.example.net/sign-in"),67+ totp = "JBSWY3DPEHPK3PXP", notes = "login note $index\nline two",68+ tags = listOf("seed", "login", "group-${index % 7}"),69+ createdAt = timestamp, modifiedAt = timestamp,70+ )71+ 1 -> VaultEntry(72+ id = "note-$suffix", type = "note", title = "Secure note $suffix",73+ notes = "private note $index\n日本語 🌸",74+ tags = listOf("seed", "note", "group-${index % 11}"),75+ createdAt = timestamp, modifiedAt = timestamp,76+ )77+ 2 -> VaultEntry(78+ id = "card-$suffix", type = "card", title = "Travel card $suffix",79+ cardholderName = "Card Holder $suffix", cardNumber = "4000000000000$suffix",80+ expMonth = ((index % 12) + 1).toString().padStart(2, '0'),81+ expYear = (2030 + index % 10).toString(), cvc = suffix,82+ cardNickname = "wallet-$suffix", cardNetwork = "visa", notes = "billing profile $index",83+ tags = listOf("seed", "card"), createdAt = timestamp, modifiedAt = timestamp,84+ )85+ else -> VaultEntry(86+ id = "passkey-$suffix", type = "passkey", title = "Passkey $suffix",87+ username = "passkey-user-$suffix",88+ passkey = PasskeyData(89+ credentialId = "credential-$index",90+ publicKeyCose = "cose-public-key-$suffix-${"q".repeat(48 + index % 13)}",91+ privateKeyJwk = """{"kty":"EC","crv":"P-256","x":"x-$suffix","y":"y-$suffix","d":"secret-$index"}""",92+ rpId = "service-$suffix.example.org", rpName = "Service $suffix",93+ userHandle = "handle-$suffix", userName = "passkey-user-$suffix",94+ userDisplayName = "Passkey User $suffix", signCount = index,95+ backupEligible = index % 2 == 0, backupState = index % 3 == 0,96+ transports = listOf("internal", "hybrid"), createdAt = timestamp,97+ ),98+ tags = listOf("seed", "passkey"), createdAt = timestamp, modifiedAt = timestamp,99+ )100+ }101+ }69102 70- val result = CodexRestore.decodeFromUri(context, uri)71- assertEquals(72- "Every generated code should decode back out of the rendered PDF",73- envelopes.size,74- result.envelopes.size,75- )103+ private class MemoryStorage : VaultStorage {104+ private var slotA: StoredVault? = null105+ private var slotB: String? = null106+ private var decoy: StoredVault? = null107+ private var version = 0L108+ private var settings = VaultSettings()76109 77- val payload = CodexRestore.reconstructPayload(result.envelopes)78- assertEquals("PX-TEST-0000", payload.verifyCode)79- assertEquals(fakePayload.vaultBlob, payload.vaultBlob)80- assertEquals(fakePayload.vaultFingerprint, payload.vaultFingerprint)110+ override fun loadSlotA() = slotA111+ override fun hasVault() = slotA != null112+ override fun writeSlotA(slot: StoredVault) = true.also { slotA = slot }113+ override fun loadSlotB() = slotB114+ override fun writeSlotB(blobBase64: String) = true.also { slotB = blobBase64 }115+ override fun loadDecoySlot() = decoy116+ override fun writeDecoySlot(slot: StoredVault) = true.also { decoy = slot }117+ override fun clearDecoySlot() { decoy = null }118+ override fun version() = version119+ override fun stampLocalMutation() = maxOf(System.currentTimeMillis(), version + 1).also { version = it }120+ override fun loadSettings() = settings121+ override fun writeSettings(settings: VaultSettings) = true.also { this.settings = settings }122+ override fun clearVault() = true.also { slotA = null; slotB = null; decoy = null; version = 0 }123+ }124+125+ private companion object {126+ const val MASTER_PASSWORD = "codex-device-seed-password"81127 }82128 }