ApiCleCryptoTest.kt 54 lignes · 1604 octets
package fr.ebii.card2vcf.sync

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

class ApiCleCryptoTest {
    @Test
    fun decryptRustFixture() {
        val key = ApiCleCrypto.decryptApiKey(
            password = FIXTURE_MDP,
            saltB64 = FIXTURE_SEL_B64,
            cipherB64 = FIXTURE_BLOB_B64,
        )
        assertEquals(FIXTURE_CLE, key)
    }

    @Test
    fun wrongPasswordFails() {
        try {
            ApiCleCrypto.decryptApiKey(
                password = "wrong-password",
                saltB64 = FIXTURE_SEL_B64,
                cipherB64 = FIXTURE_BLOB_B64,
            )
            error("expected ApiCleCryptoException")
        } catch (_: ApiCleCryptoException) {
            // expected
        }
    }

    @Test
    fun blobTooShortFails() {
        try {
            ApiCleCrypto.decryptApiKey(
                password = FIXTURE_MDP,
                saltB64 = FIXTURE_SEL_B64,
                cipherB64 = SHORT_BLOB_B64,
            )
            error("expected ApiCleCryptoException")
        } catch (_: ApiCleCryptoException) {
            // expected
        }
    }

    private companion object {
        const val FIXTURE_MDP = "card2vcf-fixture-mdp"
        const val FIXTURE_CLE = "sk_fixture_aabbccddeeff00112233"
        const val FIXTURE_SEL_B64 = "Fc+EFikMN7Cb8YoNZB+SZw=="
        const val FIXTURE_BLOB_B64 =
            "uW9uB9fP7m5ng6PoH9PGCXBKdPoVj9SceWy9GWLjg4x3Zk7MirbHhSCBVyLzoL2tK4qDir7UCOn503+s9rUFvIT/Eg5NqaU="
        // 23 bytes (< 24 nonce minimum)
        const val SHORT_BLOB_B64 = "AAAAAAAAAAAAAAAAAAAAAAA="
    }
}