package eu.europa.esig.dss.pades;
import static org.junit.Assert.fail;
import java.io.File;
import java.util.Date;
import org.junit.Test;
import eu.europa.esig.dss.DSSDocument;
import eu.europa.esig.dss.DSSException;
import eu.europa.esig.dss.FileDocument;
import eu.europa.esig.dss.SignatureAlgorithm;
import eu.europa.esig.dss.SignatureLevel;
import eu.europa.esig.dss.SignatureValue;
import eu.europa.esig.dss.ToBeSigned;
import eu.europa.esig.dss.pades.signature.PAdESService;
import eu.europa.esig.dss.signature.DocumentSignatureService;
import eu.europa.esig.dss.test.TestUtils;
import eu.europa.esig.dss.test.gen.CertificateService;
import eu.europa.esig.dss.test.mock.MockPrivateKeyEntry;
import eu.europa.esig.dss.validation.CommonCertificateVerifier;
public class PAdESLevelBNotEnoughSpaceForSignatureTest {
@Test
public void testException() throws Exception {
DSSDocument toBeSigned = new FileDocument(new File("src/test/resources/sample.pdf"));
CertificateService certificateService = new CertificateService();
MockPrivateKeyEntry privateKeyEntry = certificateService.generateCertificateChain(SignatureAlgorithm.RSA_SHA256);
Date signingDate = new Date();
PAdESSignatureParameters signatureParameters = new PAdESSignatureParameters();
signatureParameters.bLevel().setSigningDate(signingDate);
signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
signatureParameters.setSignatureLevel(SignatureLevel.PAdES_BASELINE_B);
signatureParameters.setSignatureSize(2); // 2 bytes
DocumentSignatureService<PAdESSignatureParameters> service = new PAdESService(new CommonCertificateVerifier());
try {
ToBeSigned dataToSign = service.getDataToSign(toBeSigned, signatureParameters);
SignatureValue signatureValue = TestUtils.sign(signatureParameters.getSignatureAlgorithm(), privateKeyEntry, dataToSign);
service.signDocument(toBeSigned, signatureParameters, signatureValue);
fail("Not enough space");
} catch (DSSException e) {
// assertTrue(ExceptionUtils.getStackTrace(e).contains("not enough space"));
}
}
}