<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.xades.signature;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import eu.europa.esig.dss.DomUtils;
import eu.europa.esig.dss.definition.XPathExpressionBuilder;
import eu.europa.esig.dss.diagnostic.CertificateRevocationWrapper;
import eu.europa.esig.dss.diagnostic.CertificateWrapper;
import eu.europa.esig.dss.diagnostic.DiagnosticData;
import eu.europa.esig.dss.diagnostic.SignatureWrapper;
import eu.europa.esig.dss.diagnostic.jaxb.XmlRelatedCertificate;
import eu.europa.esig.dss.diagnostic.jaxb.XmlRelatedRevocation;
import eu.europa.esig.dss.enumerations.CertificateOrigin;
import eu.europa.esig.dss.enumerations.RevocationOrigin;
import eu.europa.esig.dss.enumerations.SignatureLevel;
import eu.europa.esig.dss.enumerations.SignaturePackaging;
import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.model.FileDocument;
import eu.europa.esig.dss.model.SignatureValue;
import eu.europa.esig.dss.model.ToBeSigned;
import eu.europa.esig.dss.model.x509.CertificateToken;
import eu.europa.esig.dss.spi.x509.CommonCertificateSource;
import eu.europa.esig.dss.spi.x509.CommonTrustedCertificateSource;
import eu.europa.esig.dss.test.signature.PKIFactoryAccess;
import eu.europa.esig.dss.utils.Utils;
import eu.europa.esig.dss.validation.CommonCertificateVerifier;
import eu.europa.esig.dss.validation.SignedDocumentValidator;
import eu.europa.esig.dss.validation.reports.Reports;
import eu.europa.esig.dss.xades.XAdESSignatureParameters;
import eu.europa.esig.dss.xades.definition.xades141.XAdES141Element;

/**
 * See DSS-1806
 */
public class XAdESCrossCertificationDoubleLTATest extends PKIFactoryAccess {

	@Test
	public void test() throws Exception {
		
		DSSDocument documentToSign = new FileDocument("src/test/resources/sample.xml");

        XAdESSignatureParameters signatureParameters = new XAdESSignatureParameters();
        signatureParameters.bLevel().setSigningDate(new Date());
        signatureParameters.setSigningCertificate(getSigningCert());
        signatureParameters.setCertificateChain(getCertificateChain());
        signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_LTA);
		signatureParameters.setSignaturePackaging(SignaturePackaging.DETACHED);
        
        CertificateToken crossCertificate = getCertificateByPrimaryKey("external-ca", 2002);
        CommonCertificateSource trustedListsCertificateSource = new CommonCertificateSource();
        trustedListsCertificateSource.addCertificate(crossCertificate);
        trustedListsCertificateSource.addCertificate(getCertificate(ROOT_CA));
        
        CommonTrustedCertificateSource commonTrustedCertificateSource = new CommonTrustedCertificateSource();
        commonTrustedCertificateSource.importAsTrusted(trustedListsCertificateSource);
        
        CommonCertificateVerifier customCertificateVerifier = (CommonCertificateVerifier) getCompleteCertificateVerifier();
        customCertificateVerifier.clearTrustedCertSources();
        customCertificateVerifier.setTrustedCertSource(commonTrustedCertificateSource);
		
        XAdESService service = new XAdESService(customCertificateVerifier);
        service.setTspSource(getGoodTsa());

        ToBeSigned dataToSign = service.getDataToSign(documentToSign, signatureParameters);
        SignatureValue signatureValue = getToken().sign(dataToSign, signatureParameters.getDigestAlgorithm(), getPrivateKeyEntry());
        DSSDocument signedDocument = service.signDocument(documentToSign, signatureParameters, signatureValue);
        
        SignedDocumentValidator validator = SignedDocumentValidator.fromDocument(signedDocument);
        validator.setCertificateVerifier(customCertificateVerifier);
        validator.setDetachedContents(Arrays.asList(documentToSign));
        Reports reports = validator.validateDocument();
        // reports.print();
        
        DiagnosticData diagnosticData = reports.getDiagnosticData();
        SignatureWrapper signature = diagnosticData.getSignatureById(diagnosticData.getFirstSignatureId());
        
        // check no duplicate revocations for a certificate
        List<CertificateWrapper> usedCertificates = diagnosticData.getUsedCertificates();
        for (CertificateWrapper certificateWrapper : usedCertificates) {
        	List<CertificateRevocationWrapper> certificateRevocationData = certificateWrapper.getCertificateRevocationData();
        	List<String> revocationIds = new ArrayList<>();
        	for (CertificateRevocationWrapper revocationWrapper : certificateRevocationData) {
        		assertTrue(!revocationIds.contains(revocationWrapper.getId()));
        		revocationIds.add(revocationWrapper.getId());
        	}
        	assertEquals(certificateRevocationData.size(), revocationIds.size());
        }
        assertEquals(7, usedCertificates.size());
        
        List<XmlRelatedCertificate> relatedCertificatesFirstLTA = signature.getRelatedCertificates();
        List<XmlRelatedRevocation> relatedRevocationsFirstLTA = signature.getRelatedRevocations();

        service = new XAdESService(getCompleteCertificateVerifier());
        service.setTspSource(getGoodTsa());
        
        XAdESSignatureParameters extendParameters = new XAdESSignatureParameters();
        extendParameters.setDetachedContents(Arrays.asList(documentToSign));
        extendParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_LTA);
        DSSDocument doubleLTADoc = service.extendDocument(signedDocument, extendParameters);
        // doubleLTADoc.save("target/doubleLTA.xml");
        
        validator = SignedDocumentValidator.fromDocument(doubleLTADoc);
        validator.setCertificateVerifier(getOfflineCertificateVerifier());
        validator.setDetachedContents(Arrays.asList(documentToSign));
        reports = validator.validateDocument();
        // reports.print();
        
        diagnosticData = reports.getDiagnosticData();
        signature = diagnosticData.getSignatureById(diagnosticData.getFirstSignatureId());
        
        List<XmlRelatedCertificate> relatedCertificatesSecondLTA = signature.getRelatedCertificates();
        List<XmlRelatedRevocation> relatedRevocationsSecondLTA = signature.getRelatedRevocations();
        
        assertEquals(relatedCertificatesFirstLTA.size(), relatedCertificatesSecondLTA.size());
        assertEquals(relatedRevocationsFirstLTA.size(), relatedRevocationsSecondLTA.size());
        
        Collection<XmlRelatedCertificate> tstValidationDataCerts = signature.getRelatedCertificatesByOrigin(CertificateOrigin.TIMESTAMP_VALIDATION_DATA);
        assertTrue(Utils.isCollectionEmpty(tstValidationDataCerts));
        
        Collection<XmlRelatedRevocation> tstValidationDataRevocations = signature.getRelatedRevocationsByOrigin(RevocationOrigin.TIMESTAMP_VALIDATION_DATA);
        assertTrue(Utils.isCollectionEmpty(tstValidationDataRevocations));
        
        Document document = DomUtils.buildDOM(doubleLTADoc);
        assertNotNull(document);
        
        Element timeStampValidationDataElement = DomUtils.getElement(document, 
        		new XPathExpressionBuilder().all().element(XAdES141Element.TIMESTAMP_VALIDATION_DATA).build());
        assertNull(timeStampValidationDataElement);
		
	}

	@Override
	protected String getSigningAlias() {
		return GOOD_USER_CROSS_CERTIF;
	}

}<commit_msg>mixed filecache unit test fix<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.xades.signature;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import eu.europa.esig.dss.DomUtils;
import eu.europa.esig.dss.definition.XPathExpressionBuilder;
import eu.europa.esig.dss.diagnostic.CertificateRevocationWrapper;
import eu.europa.esig.dss.diagnostic.CertificateWrapper;
import eu.europa.esig.dss.diagnostic.DiagnosticData;
import eu.europa.esig.dss.diagnostic.SignatureWrapper;
import eu.europa.esig.dss.diagnostic.jaxb.XmlRelatedCertificate;
import eu.europa.esig.dss.diagnostic.jaxb.XmlRelatedRevocation;
import eu.europa.esig.dss.enumerations.CertificateOrigin;
import eu.europa.esig.dss.enumerations.RevocationOrigin;
import eu.europa.esig.dss.enumerations.SignatureLevel;
import eu.europa.esig.dss.enumerations.SignaturePackaging;
import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.model.FileDocument;
import eu.europa.esig.dss.model.SignatureValue;
import eu.europa.esig.dss.model.ToBeSigned;
import eu.europa.esig.dss.model.x509.CertificateToken;
import eu.europa.esig.dss.service.crl.OnlineCRLSource;
import eu.europa.esig.dss.service.http.commons.CommonsDataLoader;
import eu.europa.esig.dss.service.http.commons.FileCacheDataLoader;
import eu.europa.esig.dss.spi.client.http.DataLoader;
import eu.europa.esig.dss.spi.x509.CommonCertificateSource;
import eu.europa.esig.dss.spi.x509.CommonTrustedCertificateSource;
import eu.europa.esig.dss.test.signature.PKIFactoryAccess;
import eu.europa.esig.dss.utils.Utils;
import eu.europa.esig.dss.validation.CommonCertificateVerifier;
import eu.europa.esig.dss.validation.SignedDocumentValidator;
import eu.europa.esig.dss.validation.reports.Reports;
import eu.europa.esig.dss.xades.XAdESSignatureParameters;
import eu.europa.esig.dss.xades.definition.xades141.XAdES141Element;

/**
 * See DSS-1806
 */
public class XAdESCrossCertificationDoubleLTATest extends PKIFactoryAccess {

	@Test
	public void test() throws Exception {
		
		DSSDocument documentToSign = new FileDocument("src/test/resources/sample.xml");

        XAdESSignatureParameters signatureParameters = new XAdESSignatureParameters();
        signatureParameters.bLevel().setSigningDate(new Date());
        signatureParameters.setSigningCertificate(getSigningCert());
        signatureParameters.setCertificateChain(getCertificateChain());
        signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_LTA);
		signatureParameters.setSignaturePackaging(SignaturePackaging.DETACHED);
        
        CertificateToken crossCertificate = getCertificateByPrimaryKey("external-ca", 2002);
        CommonCertificateSource trustedListsCertificateSource = new CommonCertificateSource();
        trustedListsCertificateSource.addCertificate(crossCertificate);
        trustedListsCertificateSource.addCertificate(getCertificate(ROOT_CA));
        
        CommonTrustedCertificateSource commonTrustedCertificateSource = new CommonTrustedCertificateSource();
        commonTrustedCertificateSource.importAsTrusted(trustedListsCertificateSource);
        
        CommonCertificateVerifier customCertificateVerifier = (CommonCertificateVerifier) getCompleteCertificateVerifier();
        customCertificateVerifier.clearTrustedCertSources();
        customCertificateVerifier.setCrlSource(new OnlineCRLSource(getFileCacheDataLoader()));
        customCertificateVerifier.setTrustedCertSource(commonTrustedCertificateSource);
		
        XAdESService service = new XAdESService(customCertificateVerifier);
        service.setTspSource(getGoodTsa());

        ToBeSigned dataToSign = service.getDataToSign(documentToSign, signatureParameters);
        SignatureValue signatureValue = getToken().sign(dataToSign, signatureParameters.getDigestAlgorithm(), getPrivateKeyEntry());
        DSSDocument signedDocument = service.signDocument(documentToSign, signatureParameters, signatureValue);
        
        SignedDocumentValidator validator = SignedDocumentValidator.fromDocument(signedDocument);
        validator.setCertificateVerifier(customCertificateVerifier);
        validator.setDetachedContents(Arrays.asList(documentToSign));
        Reports reports = validator.validateDocument();
        // reports.print();
        
        DiagnosticData diagnosticData = reports.getDiagnosticData();
        SignatureWrapper signature = diagnosticData.getSignatureById(diagnosticData.getFirstSignatureId());
        
        // check no duplicate revocations for a certificate
        List<CertificateWrapper> usedCertificates = diagnosticData.getUsedCertificates();
        for (CertificateWrapper certificateWrapper : usedCertificates) {
        	List<CertificateRevocationWrapper> certificateRevocationData = certificateWrapper.getCertificateRevocationData();
        	List<String> revocationIds = new ArrayList<>();
        	for (CertificateRevocationWrapper revocationWrapper : certificateRevocationData) {
        		assertTrue(!revocationIds.contains(revocationWrapper.getId()));
        		revocationIds.add(revocationWrapper.getId());
        	}
        	assertEquals(certificateRevocationData.size(), revocationIds.size());
        }
        assertEquals(7, usedCertificates.size());
        
        List<XmlRelatedCertificate> relatedCertificatesFirstLTA = signature.getRelatedCertificates();
        List<XmlRelatedRevocation> relatedRevocationsFirstLTA = signature.getRelatedRevocations();
        
        customCertificateVerifier = (CommonCertificateVerifier) getCompleteCertificateVerifier();
        customCertificateVerifier.setCrlSource(new OnlineCRLSource(getFileCacheDataLoader()));

        service = new XAdESService(customCertificateVerifier);
        service.setTspSource(getGoodTsa());
        
        XAdESSignatureParameters extendParameters = new XAdESSignatureParameters();
        extendParameters.setDetachedContents(Arrays.asList(documentToSign));
        extendParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_LTA);
        DSSDocument doubleLTADoc = service.extendDocument(signedDocument, extendParameters);
        // doubleLTADoc.save("target/doubleLTA.xml");
        
        validator = SignedDocumentValidator.fromDocument(doubleLTADoc);
        validator.setCertificateVerifier(getOfflineCertificateVerifier());
        validator.setDetachedContents(Arrays.asList(documentToSign));
        reports = validator.validateDocument();
        // reports.print();
        
        diagnosticData = reports.getDiagnosticData();
        signature = diagnosticData.getSignatureById(diagnosticData.getFirstSignatureId());
        
        List<XmlRelatedCertificate> relatedCertificatesSecondLTA = signature.getRelatedCertificates();
        List<XmlRelatedRevocation> relatedRevocationsSecondLTA = signature.getRelatedRevocations();
        
        assertEquals(relatedCertificatesFirstLTA.size(), relatedCertificatesSecondLTA.size());
        assertEquals(relatedRevocationsFirstLTA.size(), relatedRevocationsSecondLTA.size());
        
        Collection<XmlRelatedCertificate> tstValidationDataCerts = signature.getRelatedCertificatesByOrigin(CertificateOrigin.TIMESTAMP_VALIDATION_DATA);
        assertTrue(Utils.isCollectionEmpty(tstValidationDataCerts));
        
        Collection<XmlRelatedRevocation> tstValidationDataRevocations = signature.getRelatedRevocationsByOrigin(RevocationOrigin.TIMESTAMP_VALIDATION_DATA);
        assertTrue(Utils.isCollectionEmpty(tstValidationDataRevocations));
        
        Document document = DomUtils.buildDOM(doubleLTADoc);
        assertNotNull(document);
        
        Element timeStampValidationDataElement = DomUtils.getElement(document, 
        		new XPathExpressionBuilder().all().element(XAdES141Element.TIMESTAMP_VALIDATION_DATA).build());
        assertNull(timeStampValidationDataElement);
		
	}
	
	private DataLoader getFileCacheDataLoader() {
		FileCacheDataLoader cacheDataLoader = new FileCacheDataLoader();
		CommonsDataLoader dataLoader = new CommonsDataLoader();
		dataLoader.setProxyConfig(getProxyConfig());
		cacheDataLoader.setDataLoader(dataLoader);
		cacheDataLoader.setFileCacheDirectory(new File("target/test"));
		cacheDataLoader.setCacheExpirationTime(3600000L);
		return cacheDataLoader;
	}

	@Override
	protected String getSigningAlias() {
		return GOOD_USER_CROSS_CERTIF;
	}

}
{'fraction_non_alphanumeric': 0.08326548336318731, 'fraction_numerical': 0.00537371763556424, 'mean_word_length': 4.84147114774889, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '5330819', 'n_tokens_mistral': 5113, 'n_tokens_neox': 4923, 'n_words': 845}