<commit_before>/**
 * DSS - Digital Signature Services
 * Copyright (C) 2015 European Commission, provided under the CEF programme
 * 
 * This file is part of the "DSS - Digital Signature Services" project.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package eu.europa.esig.dss.service.http.commons;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.model.DSSException;
import eu.europa.esig.dss.model.FileDocument;
import eu.europa.esig.dss.model.x509.CertificateToken;
import eu.europa.esig.dss.spi.DSSUtils;

public class SSLCommonDataLoaderTest {

	private static final String URL = "https://github.com/esig/dss";

	private static final String SSL_CERT = "<KEY>";
	private static final String WRONG_CERT = "<KEY>;

	private static final String KS_TYPE = "PKCS12";
	private static final String CORRECT_KS_PATH = "target/ks.p12";
	private static final String WRONG_KS_PATH = "target/wrong.p12";
	private static final char[] KS_PASSWORD = new char[] { 'a', 'z', 'e', 'r', 't' };
	
	private static DSSDocument correctKeyStore;
	private static DSSDocument wrongKeyStore;

	@BeforeAll
	public static void initKS() {
		CertificateToken sslCert = DSSUtils.loadCertificateFromBase64EncodedString(SSL_CERT);

		try (OutputStream os = new FileOutputStream(CORRECT_KS_PATH)) {
			KeyStore ks = KeyStore.getInstance(KS_TYPE);
			ks.load(null);
			ks.setCertificateEntry("cef", sslCert.getCertificate());
			ks.store(os, KS_PASSWORD);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		correctKeyStore = new FileDocument(CORRECT_KS_PATH);

		CertificateToken wrongCert = DSSUtils.loadCertificateFromBase64EncodedString(WRONG_CERT);

		try (OutputStream os = new FileOutputStream(WRONG_KS_PATH)) {
			KeyStore ks = KeyStore.getInstance(KS_TYPE);
			ks.load(null);
			ks.setCertificateEntry("cef", wrongCert.getCertificate());
			ks.store(os, KS_PASSWORD);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		wrongKeyStore = new FileDocument(WRONG_KS_PATH);
		<commit_msg>unit test fix (test fails due to SSL certificate update)<commit_after>/**
 * DSS - Digital Signature Services
 * Copyright (C) 2015 European Commission, provided under the CEF programme
 * 
 * This file is part of the "DSS - Digital Signature Services" project.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package eu.europa.esig.dss.service.http.commons;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.List;

import eu.europa.esig.dss.utils.Utils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.model.DSSException;
import eu.europa.esig.dss.model.FileDocument;
import eu.europa.esig.dss.model.x509.CertificateToken;
import eu.europa.esig.dss.spi.DSSUtils;

public class SSLCommonDataLoaderTest {

	private static final String URL = "https://github.com/esig/dss";

	private static final String WRONG_CERT = "<KEY>;

	private static final String KS_TYPE = "PKCS12";
	private static final String CORRECT_KS_PATH = "target/ks.p12";
	private static final String WRONG_KS_PATH = "target/wrong.p12";
	private static final char[] KS_PASSWORD = new char[] { 'a', 'z', 'e', 'r', 't' };
	
	private static DSSDocument correctKeyStore;
	private static DSSDocument wrongKeyStore;

	@BeforeAll
	public static void initKS() {
		// load signing certificate automatically, in order to resolve SSL cert update issue
		SSLCertificateLoader sslCertificateDataLoader = new SSLCertificateLoader();
		List<CertificateToken> certificateTokens = sslCertificateDataLoader.getCertificates(URL);
		assertTrue(Utils.isCollectionNotEmpty(certificateTokens));

		try (OutputStream os = new FileOutputStream(CORRECT_KS_PATH)) {
			KeyStore ks = KeyStore.getInstance(KS_TYPE);
			ks.load(null);
			ks.setCertificateEntry("cef", certificateTokens.iterator().next().getCertificate());
			ks.store(os, KS_PASSWORD);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		correctKeyStore = new FileDocument(CORRECT_KS_PATH);

		CertificateToken wrongCert = DSSUtils.loadCertificateFromBase64EncodedString(WRONG_CERT);

		try (OutputStream os = new FileOutputStream(WRONG_KS_PATH)) {
			KeyStore ks = KeyStore.getInstance(KS_TYPE);
			ks.load(null);
			ks.setCertificateEntry("cef", wrongCert.getCertificate());
			ks.store(os, KS_PASSWORD);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		wrongKeyStore = new FileDocument(WRONG_KS_PATH);
{'fraction_non_alphanumeric': 0.09342403628117914, 'fraction_numerical': 0.008767951625094483, 'mean_word_length': 4.84452296819788, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2566876', 'n_tokens_mistral': 2109, 'n_tokens_neox': 1941, 'n_words': 606}