feat(phase-1/m2.4a): scripted encrypted backup legs — the app round-trips ICDBKP1 envelopes

Phase 1 M2 task 4, desktop half (tools/DESKTOP_PHASE_PLAN.md §4). New
--backup-export-enc <path> + --backup-passphrase <pw> flags drive the
exact calls the RequirePassphrase/Passphrase dialogs make, headlessly.

Verified across two launches on fresh libraries:
  run A: import plain(27) → reimport skipped=27 → exported-enc=27 magic ok
  run B: preview encrypted=true → imported=27 → wrong-pw preview rejected
         → reimport imported=0 skipped=27

Gotcha found (and honoured): EncryptedBackupCodec zeroes the passphrase
CharArray in BOTH encrypt and decrypt — the array is single-use fuel.
The dialogs already comply (fresh toCharArray() per call); the scripted
block re-derives it via newPw() for the same reason.

Device half (4b: phone re-imports a desktop-made envelope + sees the
dedupe snackbar) follows on Waydroid with the M2 wrap-up.
This commit is contained in:
Incido Developer committed 2026-10-05 19:43:42 +13:00
1 parent 031d8570b4
commit 52ccceb732
2 files changed
+88 -18

No files matched your search

@@ -44,11 +44,12 @@ import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import java.io.File
import java.io.FileInputStream
import java.io.ByteArrayInputStream
import java.io.FileOutputStream
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.incido.compose.data.backup.BackupBridge
import org.incido.compose.data.backup.EncryptedBackupCodec
import org.incido.domain.model.Note
import org.incido.domain.repository.NoteRepository
@@ -67,7 +68,10 @@ import org.incido.domain.repository.NoteRepository
*
* Args: `--data-dir <path>` opens another library (e.g. a database pulled off
* a phone, to prove schema compatibility); `--exit-after <seconds>` auto-closes
* for scripted smoke runs.
* for scripted smoke runs; `--backup-import <path>` / `--backup-export <path>`
* drive the Import/Export menu's exact bridge calls headlessly;
* `--backup-export-enc <path>` + `--backup-passphrase <pw>` add the
* encrypted-envelope legs (M2 task 4).
*/
fun main(args: Array<String>) {
val dataDir = args.argAfter("--data-dir")
@@ -81,6 +85,9 @@ fun main(args: Array<String>) {
// Import/Export menu makes, without clicking through native dialogs.
val backupImport = args.argAfter("--backup-import")
val backupExport = args.argAfter("--backup-export")
// M2 task 4: encrypted-envelope legs (the RequirePassphrase dialog's calls).
val backupExportEnc = args.argAfter("--backup-export-enc")
val backupPassphrase = args.argAfter("--backup-passphrase")
println("=== Incido (desktop) — Phase 1 M1b ===")
println("JVM : ${System.getProperty("java.vendor")} ${System.getProperty("java.version")}")
@@ -100,6 +107,8 @@ fun main(args: Array<String>) {
selfTest = selfTest,
backupImport = backupImport,
backupExport = backupExport,
backupExportEnc = backupExportEnc,
backupPassphrase = backupPassphrase,
onExit = ::exitApplication,
)
}
@@ -116,6 +125,8 @@ private fun NotesWindow(
selfTest: Boolean,
backupImport: String?,
backupExport: String?,
backupExportEnc: String?,
backupPassphrase: String?,
onExit: () -> Unit,
) {
val notes by remember(repository) { repository.observeAll() }
@@ -219,29 +230,63 @@ private fun NotesWindow(
println("SELFTEST wrote note id=${saved.id}")
}
}
if (backupImport != null) {
if (backupImport != null || backupExportEnc != null) {
LaunchedEffect(Unit) {
delay(800) // mirror the self-test pacing; wait past first frame
val file = File(backupImport)
val preview = BackupBridge.preview(FileInputStream(file))
println(
"BACKUP preview notes=${preview?.noteCount} " +
"attachments=${preview?.attachmentCount} app=${preview?.appVersion}",
)
val first = BackupBridge.import(repository, FileInputStream(file))
println(
"BACKUP imported=${first.imported} skipped=${first.skipped} " +
"(same calls as the Import… menu)",
)
// The codec zeroes the CharArray it consumes (encrypt AND
// decrypt both call passphrase.fill), so every call needs a
// FRESH array — exactly what the dialogs do by re-deriving
// stage.passphrase?.toCharArray() per call.
fun newPw(): CharArray? = backupPassphrase?.toCharArray()
if (backupImport != null) {
val file = File(backupImport)
val bytes = file.readBytes()
val encrypted = EncryptedBackupCodec.isEncrypted(bytes)
// preview() takes the passphrase exactly like the
// RequirePassphrase dialog does, and returns null when the
// passphrase is wrong — the dialog's own reject gate.
val preview = BackupBridge.preview(ByteArrayInputStream(bytes), newPw())
println(
"BACKUP preview notes=${preview?.noteCount} " +
"attachments=${preview?.attachmentCount} app=${preview?.appVersion} " +
"encrypted=$encrypted",
)
if (preview != null) {
val first = BackupBridge.import(repository, ByteArrayInputStream(bytes), newPw())
println(
"BACKUP imported=${first.imported} skipped=${first.skipped} " +
"(same calls as the Import… menu)",
)
if (encrypted && backupPassphrase != null) {
val wrong = BackupBridge.preview(
ByteArrayInputStream(bytes),
"definitely-the-wrong-pass".toCharArray(),
)
println("BACKUP wrong-pw preview=${if (wrong == null) "null (rejected)" else "LEAKED"}")
}
// M2 task 3: importing the same file again must add nothing —
// the dedupe path the confirm dialog's checkbox controls.
val again = BackupBridge.import(repository, ByteArrayInputStream(bytes), newPw())
println("BACKUP reimport imported=${again.imported} skipped=${again.skipped} (dedupe ON)")
}
}
if (backupExport != null) {
val out = File(backupExport)
val exported = BackupBridge.exportPlain(repository, FileOutputStream(out), DESKTOP_APP_VERSION)
println("BACKUP exported=$exported notes to $out")
}
// M2 task 3: importing the same file again must add nothing —
// the dedupe path the confirm dialog's checkbox controls.
val again = BackupBridge.import(repository, FileInputStream(file))
println("BACKUP reimport imported=${again.imported} skipped=${again.skipped} (dedupe ON)")
if (backupExportEnc != null) {
val pass = newPw() ?: error("--backup-export-enc needs --backup-passphrase")
val out = File(backupExportEnc)
val n = BackupBridge.exportEncrypted(repository, FileOutputStream(out), pass, DESKTOP_APP_VERSION)
val magicOk = out.inputStream().use { ins ->
ins.readNBytes(EncryptedBackupCodec.MAGIC.size).contentEquals(EncryptedBackupCodec.MAGIC)
}
println(
"BACKUP exported-enc=$n notes to $out " +
"(magic=${if (magicOk) "ICDBKP1 ok" else "BAD"})",
)
}
}
}
if (exitAfterSeconds != null) {
+25
View File
@@ -401,6 +401,31 @@ Full suite green: `backupSmoke` (9 steps incl. dedupe), `assembleDebug`,
`:core:data-jvm:smoke`, scripted desktop run (clean exit),
`:desktopApp:run --self-test`.
**M2 task 4 (desktop leg) — encrypted round-trip via the app: DONE.**
New scripted flags `--backup-export-enc <path>` + `--backup-passphrase
<pw>` drive the passphrase dialogs' exact call chain headlessly:
`exportEncrypted` writes a real ICDBKP1 envelope, and importing it back
exercises `preview(stream, pw)` → `import(stream, pw)` → wrong-passphrase
preview → deduped re-import, in fresh libraries across two launches:
```
run A: plain import 27 → reimport skipped=27 → exported-enc=27 (magic=ICDBKP1 ok)
run B: preview encrypted=true → imported=27 → wrong-pw preview=null (rejected)
→ reimport imported=0 skipped=27
```
Caught while writing it: `EncryptedBackupCodec` zeroes the passphrase
`CharArray` it consumes — **both in `encrypt` AND `decrypt`** — so every
call needs a fresh array. The UI already complies (dialogs re-derive
`toCharArray()` per call); the scripted block initially reused one array
and died on the second decrypt with `AEADBadTagException`. Worth
remembering for the phone device pass and any future caller: treat the
passphrase array as single-use fuel. The envelope desktop produces is
byte-format-identical to the phone's because both sides now compile the
same `:core:backup` codec — the remaining proof for task 4 is the
on-device leg (Waydroid: phone re-imports a desktop-made encrypted file
+ dedupe snackbar), tracked as M2 task 4b.