import Signature from '../Signature';
import fs from 'fs';
import path from 'path';
import { DOMParser } from '@xmldom/xmldom';
import xpath from 'xpath';
import {
hasTestCertificate,
readTestCertificate,
} from '../../test_cert/hasTestCertificate';
const describeWithCertificate = hasTestCertificate() ? describe : describe.skip;
describeWithCertificate('Sign Arbitrary XML Documents', () => {
it('Should sign Postulacion XML document', () => {
const certs = readTestCertificate();
if (!certs.key || !certs.cert) {
return;
}
const postulacionXml = fs.readFileSync(
path.resolve(__dirname, 'sample/Postulacion.xml'),
'utf-8'
);
const signature = new Signature(certs.key, certs.cert);
// Sign arbitrary XML document with custom root element name
const signedXml = signature.signXml(postulacionXml, 'Postulacion');
// Verify the signature was embedded in the document
const doc = new DOMParser().parseFromString(signedXml) as any;
// Check that Signature element exists
const signatureNodes = xpath.select(
"//*[local-name(.)='Signature']",
doc
) as any;
expect(signatureNodes.length).toBeGreaterThan(0);
// Check that DigestValue exists
const digestNodes = xpath.select(
"//*[local-name(.)='DigestValue']",
doc
) as any;
expect(digestNodes.length).toBeGreaterThan(0);
expect(digestNodes[0].firstChild.data).toBeTruthy();
// Check that SignatureValue exists
const signatureValueNodes = xpath.select(
"//*[local-name(.)='SignatureValue']",
doc
) as any;
expect(signatureValueNodes.length).toBeGreaterThan(0);
expect(signatureValueNodes[0].firstChild.data).toBeTruthy();
// Verify original content is preserved
const postulacionNodes = xpath.select(
"//*[local-name(.)='Postulacion']",
doc
) as any;
expect(postulacionNodes.length).toBe(1);
const postulacionIDNodes = xpath.select(
"//*[local-name(.)='PostulacionID']",
doc
) as any;
expect(postulacionIDNodes[0].firstChild.data).toBe('12345');
});
it('Should sign any custom XML root element', () => {
const certs = readTestCertificate();
if (!certs.key || !certs.cert) {
return;
}
// Create a custom XML document
const customXml = `
12345
Test Document
This is a test
`;
const signature = new Signature(certs.key, certs.cert);
// Sign with custom root element name
const signedXml = signature.signXml(customXml, 'CustomDocument');
// Verify the signature was embedded
const doc = new DOMParser().parseFromString(signedXml) as any;
const signatureNodes = xpath.select(
"//*[local-name(.)='Signature']",
doc
) as any;
expect(signatureNodes.length).toBeGreaterThan(0);
// Verify original content is preserved
const customDocNodes = xpath.select(
"//*[local-name(.)='CustomDocument']",
doc
) as any;
expect(customDocNodes.length).toBe(1);
});
it('Should auto-detect root element from Postulacion XML', () => {
const certs = readTestCertificate();
if (!certs.key || !certs.cert) {
return;
}
const postulacionXml = fs.readFileSync(
path.resolve(__dirname, 'sample/Postulacion.xml'),
'utf-8'
);
const signature = new Signature(certs.key, certs.cert);
// Sign without specifying root element - should auto-detect 'Postulacion'
const signedXml = signature.signXml(postulacionXml);
// Verify the signature was embedded
const doc = new DOMParser().parseFromString(signedXml) as any;
const signatureNodes = xpath.select(
"//*[local-name(.)='Signature']",
doc
) as any;
expect(signatureNodes.length).toBeGreaterThan(0);
// Verify original content is preserved
const postulacionNodes = xpath.select(
"//*[local-name(.)='Postulacion']",
doc
) as any;
expect(postulacionNodes.length).toBe(1);
const postulacionIDNodes = xpath.select(
"//*[local-name(.)='PostulacionID']",
doc
) as any;
expect(postulacionIDNodes[0].firstChild.data).toBe('12345');
});
it('Should auto-detect root element from any custom XML', () => {
const certs = readTestCertificate();
if (!certs.key || !certs.cert) {
return;
}
// Create a custom XML document
const customXml = `
Value1
Value2
`;
const signature = new Signature(certs.key, certs.cert);
// Sign without specifying root element - should auto-detect 'MyCustomRoot'
const signedXml = signature.signXml(customXml);
// Verify the signature was embedded
const doc = new DOMParser().parseFromString(signedXml) as any;
const signatureNodes = xpath.select(
"//*[local-name(.)='Signature']",
doc
) as any;
expect(signatureNodes.length).toBeGreaterThan(0);
// Verify original content is preserved
const rootNodes = xpath.select(
"//*[local-name(.)='MyCustomRoot']",
doc
) as any;
expect(rootNodes.length).toBe(1);
});
it('Should throw error for invalid XML with no root element (auto-detect)', () => {
const certs = readTestCertificate();
if (!certs.key || !certs.cert) {
return;
}
// Garbage input - no valid XML structure
const invalidXml = 'this is not xml at all';
const signature = new Signature(certs.key, certs.cert);
// Should throw error - no root element can be detected
expect(() => signature.signXml(invalidXml)).toThrow();
});
it('Should throw error for XML with invalid structure (explicit root)', () => {
const certs = readTestCertificate();
if (!certs.key || !certs.cert) {
return;
}
// XML with multiple root elements - triggers parser error
const invalidXml = '';
const signature = new Signature(certs.key, certs.cert);
// Should throw error even when explicit root element is provided
// (validation happens consistently regardless of explicit/auto-detect)
expect(() => signature.signXml(invalidXml, 'Doc')).toThrow('Invalid XML');
});
});