ApiCleCryptoTest.swift 45 lignes · 1539 octets
import XCTest
@testable import Card2vcf

final class ApiCleCryptoTest: XCTestCase {
    private static let FIXTURE_MDP = "card2vcf-fixture-mdp"
    private static let FIXTURE_CLE = "sk_fixture_aabbccddeeff00112233"
    private static let FIXTURE_SEL_B64 = "Fc+EFikMN7Cb8YoNZB+SZw=="
    private static let FIXTURE_BLOB_B64 =
        "uW9uB9fP7m5ng6PoH9PGCXBKdPoVj9SceWy9GWLjg4x3Zk7MirbHhSCBVyLzoL2tK4qDir7UCOn503+s9rUFvIT/Eg5NqaU="
    // 23 bytes (< 24 nonce minimum)
    private static let SHORT_BLOB_B64 = "AAAAAAAAAAAAAAAAAAAAAAA="

    func testDecryptRustFixture() throws {
        let key = try ApiCleCrypto.decryptApiKey(
            password: Self.FIXTURE_MDP,
            saltB64: Self.FIXTURE_SEL_B64,
            cipherB64: Self.FIXTURE_BLOB_B64
        )
        XCTAssertEqual(Self.FIXTURE_CLE, key)
    }

    func testWrongPasswordFails() {
        XCTAssertThrowsError(
            try ApiCleCrypto.decryptApiKey(
                password: "wrong-password",
                saltB64: Self.FIXTURE_SEL_B64,
                cipherB64: Self.FIXTURE_BLOB_B64
            )
        ) { error in
            XCTAssertTrue(error is ApiCleCryptoException)
        }
    }

    func testBlobTooShortFails() {
        XCTAssertThrowsError(
            try ApiCleCrypto.decryptApiKey(
                password: Self.FIXTURE_MDP,
                saltB64: Self.FIXTURE_SEL_B64,
                cipherB64: Self.SHORT_BLOB_B64
            )
        ) { error in
            XCTAssertTrue(error is ApiCleCryptoException)
        }
    }
}