Search in sources :

Example 1 with IgnoreAllErrorHandler

use of org.apache.xml.security.utils.IgnoreAllErrorHandler in project santuario-java by apache.

the class Canonicalizer20010315Test method doTestXMLAttributes.

/**
 * Method doTestXMLAttributes
 *
 * @param input
 * @param definedOutput
 * @param writeResultsToFile
 *
 * @throws CanonicalizationException
 * @throws FileNotFoundException
 * @throws IOException
 * @throws InvalidCanonicalizerException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 * @throws XPathExpressionException
 */
private boolean doTestXMLAttributes(String input, String definedOutput) throws IOException, FileNotFoundException, SAXException, ParserConfigurationException, CanonicalizationException, InvalidCanonicalizerException, TransformerException, XPathExpressionException {
    DocumentBuilder db = XMLUtils.createDocumentBuilder(true);
    db.setErrorHandler(new IgnoreAllErrorHandler());
    Document doc = null;
    try (InputStream is = new ByteArrayInputStream(input.getBytes())) {
        doc = db.parse(is);
    }
    Canonicalizer c14nizer = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    // XMLUtils.circumventBug2650(doc);
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xPath = xpf.newXPath();
    xPath.setNamespaceContext(new DSNamespaceContext());
    String xpath = "(//*[local-name()='included'] | //@*[parent::node()[local-name()='included']])";
    NodeList nodes = (NodeList) xPath.evaluate(xpath, doc, XPathConstants.NODESET);
    byte[] result = c14nizer.canonicalizeXPathNodeSet(nodes);
    byte[] defined = definedOutput.getBytes();
    assertEquals(definedOutput, new String(result));
    return java.security.MessageDigest.isEqual(defined, result);
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) IgnoreAllErrorHandler(org.apache.xml.security.utils.IgnoreAllErrorHandler) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Example 2 with IgnoreAllErrorHandler

use of org.apache.xml.security.utils.IgnoreAllErrorHandler in project santuario-java by apache.

the class Canonicalizer20010315Test method c14nAndCompare.

private boolean c14nAndCompare(String fileIn, String fileRef, String fileOut, String c14nURI, boolean validating, String xpath, Map<String, String> namespaces) throws IOException, FileNotFoundException, SAXException, ParserConfigurationException, CanonicalizationException, InvalidCanonicalizerException, TransformerException, XPathExpressionException {
    DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(validating, false);
    // throw away all warnings and errors
    documentBuilder.setErrorHandler(new IgnoreAllErrorHandler());
    // org.xml.sax.EntityResolver resolver = new TestVectorResolver();
    // documentBuilder.setEntityResolver(resolver);
    // Document doc = documentBuilder.parse(resolver.resolveEntity(null, fileIn));
    Document doc = documentBuilder.parse(fileIn);
    Canonicalizer c14n = Canonicalizer.getInstance(c14nURI);
    byte[] c14nBytes = null;
    if (xpath == null) {
        c14nBytes = c14n.canonicalizeSubtree(doc);
    } else {
        NodeList nl = null;
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xPath = xpf.newXPath();
        DSNamespaceContext namespaceContext = new DSNamespaceContext(namespaces);
        xPath.setNamespaceContext(namespaceContext);
        nl = (NodeList) xPath.evaluate(xpath, doc, XPathConstants.NODESET);
        c14nBytes = c14n.canonicalizeXPathNodeSet(nl);
    }
    // org.xml.sax.InputSource refIs = resolver.resolveEntity(null, fileRef);
    // byte refBytes[] = JavaUtils.getBytesFromStream(refIs.getByteStream());
    byte[] refBytes = JavaUtils.getBytesFromFile(fileRef);
    // if everything is OK, result is true; we do a binary compare, byte by byte
    boolean result = java.security.MessageDigest.isEqual(refBytes, c14nBytes);
    if (!result) {
        File f = new File(fileOut);
        if (!f.exists()) {
            File parent = new File(f.getParent());
            parent.mkdirs();
            f.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(c14nBytes);
        LOG.debug("Wrote erroneous result to file " + f.toURI().toURL().toString());
        assertEquals(new String(refBytes), new String(c14nBytes));
        fos.close();
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) IgnoreAllErrorHandler(org.apache.xml.security.utils.IgnoreAllErrorHandler) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Example 3 with IgnoreAllErrorHandler

use of org.apache.xml.security.utils.IgnoreAllErrorHandler in project santuario-java by apache.

the class Canonicalizer11Test method c14nAndCompare.

private boolean c14nAndCompare(String fileIn, String fileRef, String fileOut, String c14nURI, boolean validating, String xpath, Map<String, String> namespaces) throws IOException, FileNotFoundException, SAXException, ParserConfigurationException, CanonicalizationException, InvalidCanonicalizerException, TransformerException, XPathExpressionException {
    DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(validating, false);
    // throw away all warnings and errors
    documentBuilder.setErrorHandler(new IgnoreAllErrorHandler());
    // org.xml.sax.EntityResolver resolver = new TestVectorResolver();
    // documentBuilder.setEntityResolver(resolver);
    // Document doc = documentBuilder.parse(resolver.resolveEntity(null, fileIn));
    Document doc = documentBuilder.parse(fileIn);
    Canonicalizer c14n = Canonicalizer.getInstance(c14nURI);
    byte[] c14nBytes = null;
    if (xpath == null) {
        c14nBytes = c14n.canonicalizeSubtree(doc);
    } else {
        NodeList nl = null;
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xPath = xpf.newXPath();
        DSNamespaceContext namespaceContext = new DSNamespaceContext(namespaces);
        xPath.setNamespaceContext(namespaceContext);
        nl = (NodeList) xPath.evaluate(xpath, doc, XPathConstants.NODESET);
        c14nBytes = c14n.canonicalizeXPathNodeSet(nl);
    }
    // org.xml.sax.InputSource refIs = resolver.resolveEntity(null, fileRef);
    // byte refBytes[] = JavaUtils.getBytesFromStream(refIs.getByteStream());
    byte[] refBytes = JavaUtils.getBytesFromFile(fileRef);
    // if everything is OK, result is true; we do a binary compare, byte by byte
    boolean result = java.security.MessageDigest.isEqual(refBytes, c14nBytes);
    if (!result) {
        File f = new File(fileOut);
        if (!f.exists()) {
            File parent = new File(f.getParent());
            parent.mkdirs();
            f.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(c14nBytes);
        LOG.debug("Wrote erroneous result to file " + f.toURI().toURL().toString());
        assertEquals(new String(refBytes), new String(c14nBytes));
        fos.close();
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) IgnoreAllErrorHandler(org.apache.xml.security.utils.IgnoreAllErrorHandler) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Aggregations

DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 XPath (javax.xml.xpath.XPath)3 XPathFactory (javax.xml.xpath.XPathFactory)3 Canonicalizer (org.apache.xml.security.c14n.Canonicalizer)3 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)3 IgnoreAllErrorHandler (org.apache.xml.security.utils.IgnoreAllErrorHandler)3 Document (org.w3c.dom.Document)3 NodeList (org.w3c.dom.NodeList)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1