SourceConsentGateTest.kt

1.5 KB · raw

1package com.peachpasswords.sync23import kotlinx.coroutines.async4import kotlinx.coroutines.test.runCurrent5import kotlinx.coroutines.test.runTest6import org.junit.Assert.assertFalse7import org.junit.Assert.assertTrue8import org.junit.Test910/**11 * PB-26-116: the vault-owning device must approve a pairing before the root key12 * is read. The historical defect was a consent callback that returned true as13 * soon as the SAS had been displayed, so nothing on the source side ever asked.14 */15@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)16class SourceConsentGateTest {1718    @Test fun a_grant_waits_until_someone_decides() = runTest {19        val gate = SourceConsentGate()20        val decision = async { gate.await() }21        runCurrent()2223        assertFalse("must not approve before a decision exists", decision.isCompleted)2425        gate.resolve(true)26        assertTrue(decision.await())27    }2829    @Test fun leaving_the_screen_denies_rather_than_hanging() = runTest {30        val gate = SourceConsentGate()31        val decision = async { gate.await() }32        runCurrent()3334        gate.resolve(false)35        assertFalse(decision.await())36    }3738    @Test fun an_answer_with_no_live_prompt_is_ignored() = runTest {39        val gate = SourceConsentGate()40        gate.resolve(true)4142        val decision = async { gate.await() }43        runCurrent()44        assertFalse("a stale answer must not pre-approve a later prompt", decision.isCompleted)45        decision.cancel()46    }47}