/**
 * Copyright © 2012-2025 by Data Virtuality GmbH
 */
package org.teiid.jdbc;

import org.junit.jupiter.api.Test;

import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.spec.ECGenParameterSpec;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
 *
 *
 * @author Roman Pedchenko
 * @date 2025.12.02
 */
public class TestFipsKeyValidation {
    @Test
    public void testRsa1024Fails() throws Exception {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(1024);
        KeyPair kp = kpg.generateKeyPair();

        assertThatThrownBy(() -> FipsManager.validatePublicKeyStrength(kp.getPublic()))
                .as("RSA 1024 must fail FIPS validation")
                .isInstanceOf(GeneralSecurityException.class);
    }

    @Test
    public void testRsa2048Passes() throws Exception {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(2048);
        KeyPair kp = kpg.generateKeyPair();

        FipsManager.validatePublicKeyStrength(kp.getPublic());
    }

    @Test
    public void testDsa1024Fails() throws Exception {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
        kpg.initialize(1024);
        KeyPair kp = kpg.generateKeyPair();

        assertThatThrownBy(() -> FipsManager.validatePublicKeyStrength(kp.getPublic()))
                .as("DSA 1024 must fail FIPS validation")
                .isInstanceOf(GeneralSecurityException.class);
    }

    @Test
    public void testDsa2048Passes() throws Exception {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
        kpg.initialize(2048);
        KeyPair kp = kpg.generateKeyPair();

        FipsManager.validatePublicKeyStrength(kp.getPublic());
    }

    @Test
    public void testEc256Passes() throws Exception {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
        kpg.initialize(new ECGenParameterSpec("secp256r1"), new SecureRandom());
        KeyPair kp = kpg.generateKeyPair();
        FipsManager.validatePublicKeyStrength(kp.getPublic());
    }

    @Test
    public void testEd25519Passes() throws Exception {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("Ed25519");
        KeyPair kp = kpg.generateKeyPair();
        FipsManager.validatePublicKeyStrength(kp.getPublic());
    }
}
