DuressPolicyRecordTest.kt

1.3 KB · raw

1package com.peachpasswords.vault23import org.json.JSONObject4import org.junit.Assert.assertEquals5import org.junit.Test67/**8 * PB-26-127: duress configuration and its action must not be readable from the9 * stored record alone. EncryptedSharedPreferences pads nothing, so the stored10 * ciphertext length tracks the plaintext length — every state has to encode to11 * the same length with the same key set.12 */13class DuressPolicyRecordTest {1415    @Test fun every_state_encodes_to_the_same_length_and_key_set() {16        val off = encodeDuressPolicyRecord("off")17        val decoy = encodeDuressPolicyRecord(DuressAction.DECOY.wire)18        val destroy = encodeDuressPolicyRecord(DuressAction.DESTROY.wire)1920        assertEquals(off.length, decoy.length)21        assertEquals(off.length, destroy.length)22        assertEquals(keysOf(off), keysOf(decoy))23        assertEquals(keysOf(off), keysOf(destroy))24    }2526    @Test fun the_action_survives_a_round_trip_through_the_padding() {27        val record = encodeDuressPolicyRecord(DuressAction.DESTROY.wire)2829        assertEquals(DuressAction.DESTROY.wire, JSONObject(record.trimEnd('\u0000')).optString("action"))30    }3132    private fun keysOf(record: String): Set<String> =33        JSONObject(record.trimEnd('\u0000')).keys().asSequence().toSet()34}