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

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.teiid.net.TeiidURL;

import javax.net.ssl.SSLContext;
import java.security.GeneralSecurityException;
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.teiid.core.crypto.FipsUtil.BCFIPS_PROVIDER;
import static org.teiid.core.crypto.FipsUtil.BCJSSE_PROVIDER;

/**
 *
 *
 * @author Roman Pedchenko
 * @date 2025.10.07
 */
public class TestFipsInit {
    @AfterEach
    void tearDown() {
        FipsManager.removeFipsProvider();
    }

    @Test
    public void testEnabled() throws Exception {
        Properties props = new Properties();
        props.setProperty(TeiidURL.CONNECTION.FIPS, "enabled");
        FipsManager.initializeFipsMode(props);
        assertThat(FipsManager.isFipsModeEnabled()).isTrue();
        assertThat(FipsManager.isFipsJsseModeEnabled()).isTrue();
        assertThat(FipsManager.getCurrentFipsProvider()).isEqualTo(BCFIPS_PROVIDER);
        assertThat(FipsManager.getCurrentJsseProvider()).isEqualTo(BCJSSE_PROVIDER);
        FipsManager.validateContext(SSLContext.getInstance("TLS"));
        assertThatThrownBy(() -> FipsManager.validateContext(SSLContext.getInstance("TLS", "SunJSSE")))
                .isInstanceOf(GeneralSecurityException.class)
                .hasMessage(JDBCPlugin.Util.getString("fips_context_mismatch"));
    }
}
