package com.peachpasswords.vault import org.json.JSONObject import org.junit.Assert.assertEquals import org.junit.Test /** * PB-26-127: duress configuration and its action must not be readable from the * stored record alone. EncryptedSharedPreferences pads nothing, so the stored * ciphertext length tracks the plaintext length — every state has to encode to * the same length with the same key set. */ class DuressPolicyRecordTest { @Test fun every_state_encodes_to_the_same_length_and_key_set() { val off = encodeDuressPolicyRecord("off") val decoy = encodeDuressPolicyRecord(DuressAction.DECOY.wire) val destroy = encodeDuressPolicyRecord(DuressAction.DESTROY.wire) assertEquals(off.length, decoy.length) assertEquals(off.length, destroy.length) assertEquals(keysOf(off), keysOf(decoy)) assertEquals(keysOf(off), keysOf(destroy)) } @Test fun the_action_survives_a_round_trip_through_the_padding() { val record = encodeDuressPolicyRecord(DuressAction.DESTROY.wire) assertEquals(DuressAction.DESTROY.wire, JSONObject(record.trimEnd('\u0000')).optString("action")) } private fun keysOf(record: String): Set = JSONObject(record.trimEnd('\u0000')).keys().asSequence().toSet() }