package com.ponzischeme89.memby.update

import org.junit.Assert.assertEquals
import org.junit.Test

/**
 * The rule that decides whether an update is allowed to reach the installer.
 *
 * The case worth pinning is [SignerVerdict.UNVERIFIABLE]: an unreadable signature on the
 * downloaded APK used to be reported as the wrong signing key, which rejected correctly
 * signed updates on the televisions where `getPackageArchiveInfo` returns no signing
 * information, and left a manual reinstall as the only way to move a version.
 */
class SignerVerdictTest {

    private val key = setOf("0b61ec2a364d61b765949e5cf281a131d91e60f20ac934282feea41bf509d894")
    private val otherKey = setOf("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")

    @Test
    fun `the same key matches`() {
        assertEquals(SignerVerdict.MATCH, signerVerdict(key, key))
    }

    @Test
    fun `a different key is a mismatch`() {
        assertEquals(SignerVerdict.MISMATCH, signerVerdict(otherKey, key))
    }

    @Test
    fun `an unreadable archive signature is not a mismatch`() {
        assertEquals(SignerVerdict.UNVERIFIABLE, signerVerdict(emptySet(), key))
    }

    @Test
    fun `an unreadable installed signature is not a mismatch either`() {
        assertEquals(SignerVerdict.UNVERIFIABLE, signerVerdict(key, emptySet()))
    }

    @Test
    fun `signer order does not matter`() {
        val a = setOf("aa", "bb")
        val b = setOf("bb", "aa")
        assertEquals(SignerVerdict.MATCH, signerVerdict(a, b))
    }
}
