|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.xml.security.utils; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.w3c.dom.Document; |
| 23 | +import org.w3c.dom.Node; |
| 24 | +import org.w3c.dom.traversal.DocumentTraversal; |
| 25 | +import org.w3c.dom.traversal.TreeWalker; |
| 26 | +import org.xml.sax.InputSource; |
| 27 | +import org.xml.sax.SAXException; |
| 28 | + |
| 29 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 30 | +import javax.xml.parsers.ParserConfigurationException; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.StringReader; |
| 33 | + |
| 34 | +import static javax.xml.XMLConstants.DEFAULT_NS_PREFIX; |
| 35 | +import static javax.xml.XMLConstants.NULL_NS_URI; |
| 36 | +import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE; |
| 37 | +import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; |
| 38 | +import static javax.xml.XMLConstants.XML_NS_PREFIX; |
| 39 | +import static javax.xml.XMLConstants.XML_NS_URI; |
| 40 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 41 | +import static org.hamcrest.Matchers.equalTo; |
| 42 | +import static org.hamcrest.Matchers.is; |
| 43 | +import static org.hamcrest.Matchers.nullValue; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 45 | +import static org.w3c.dom.traversal.NodeFilter.SHOW_ELEMENT; |
| 46 | + |
| 47 | +public class DOMNamespaceContextTest { |
| 48 | + |
| 49 | + private static final DocumentBuilderFactory DEFAULT_DOCUMENT_BUILDER_FACTORY; |
| 50 | + |
| 51 | + static { |
| 52 | + DEFAULT_DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance(); |
| 53 | + DEFAULT_DOCUMENT_BUILDER_FACTORY.setNamespaceAware(true); |
| 54 | + } |
| 55 | + |
| 56 | + private static Document createDocument(String xml) throws IOException, SAXException, ParserConfigurationException { |
| 57 | + return DEFAULT_DOCUMENT_BUILDER_FACTORY.newDocumentBuilder().parse(new InputSource(new StringReader(xml))); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testUnboundDefaultNamespace() throws Exception { |
| 62 | + Document document = createDocument("<root/>"); |
| 63 | + DOMNamespaceContext namespaceContext = new DOMNamespaceContext(document); |
| 64 | + assertThrows(IllegalArgumentException.class, () -> namespaceContext.getNamespaceURI(null)); |
| 65 | + assertThat(namespaceContext.getNamespaceURI(DEFAULT_NS_PREFIX), is(equalTo(NULL_NS_URI))); |
| 66 | + assertThat(namespaceContext.getNamespaceURI(XML_NS_PREFIX), is(equalTo(XML_NS_URI))); |
| 67 | + assertThat(namespaceContext.getNamespaceURI(XMLNS_ATTRIBUTE), is(equalTo(XMLNS_ATTRIBUTE_NS_URI))); |
| 68 | + assertThat(namespaceContext.getNamespaceURI("unbound-ns"), is(equalTo(NULL_NS_URI))); |
| 69 | + assertThrows(IllegalArgumentException.class, () -> namespaceContext.getPrefix(null)); |
| 70 | + assertThat(namespaceContext.getPrefix(NULL_NS_URI), is(equalTo(DEFAULT_NS_PREFIX))); |
| 71 | + assertThat(namespaceContext.getPrefix(XML_NS_URI), is(equalTo(XML_NS_PREFIX))); |
| 72 | + assertThat(namespaceContext.getPrefix(XMLNS_ATTRIBUTE_NS_URI), is(equalTo(XMLNS_ATTRIBUTE))); |
| 73 | + assertThat(namespaceContext.getPrefix("urn:unbound-ns"), is(nullValue())); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testBoundDefaultNamespace() throws Exception { |
| 78 | + Document document = createDocument("<root xmlns='urn:ns'/>"); |
| 79 | + TreeWalker walker = ((DocumentTraversal) document).createTreeWalker(document, SHOW_ELEMENT, null, true); |
| 80 | + Node root = walker.nextNode(); |
| 81 | + DOMNamespaceContext namespaceContext = new DOMNamespaceContext(root); |
| 82 | + assertThrows(IllegalArgumentException.class, () -> namespaceContext.getNamespaceURI(null)); |
| 83 | + assertThat(namespaceContext.getNamespaceURI(DEFAULT_NS_PREFIX), is(equalTo("urn:ns"))); |
| 84 | + assertThat(namespaceContext.getNamespaceURI(XML_NS_PREFIX), is(equalTo(XML_NS_URI))); |
| 85 | + assertThat(namespaceContext.getNamespaceURI(XMLNS_ATTRIBUTE), is(equalTo(XMLNS_ATTRIBUTE_NS_URI))); |
| 86 | + assertThat(namespaceContext.getNamespaceURI("unbound-ns"), is(equalTo(NULL_NS_URI))); |
| 87 | + assertThrows(IllegalArgumentException.class, () -> namespaceContext.getPrefix(null)); |
| 88 | + assertThat(namespaceContext.getPrefix(NULL_NS_URI), is(nullValue())); |
| 89 | + assertThat(namespaceContext.getPrefix("urn:ns"), is(equalTo(DEFAULT_NS_PREFIX))); |
| 90 | + assertThat(namespaceContext.getPrefix(XML_NS_URI), is(equalTo(XML_NS_PREFIX))); |
| 91 | + assertThat(namespaceContext.getPrefix(XMLNS_ATTRIBUTE_NS_URI), is(equalTo(XMLNS_ATTRIBUTE))); |
| 92 | + assertThat(namespaceContext.getPrefix("urn:unbound-ns"), is(nullValue())); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testNamespaceInheritance() throws Exception { |
| 97 | + Document document = createDocument("<root xmlns='urn:ns'><branch xmlns:ns1='urn:ns1'/></root>"); |
| 98 | + TreeWalker walker = ((DocumentTraversal) document).createTreeWalker(document, SHOW_ELEMENT, null, true); |
| 99 | + Node root = walker.nextNode(); |
| 100 | + DOMNamespaceContext namespaceContext = new DOMNamespaceContext(root); |
| 101 | + assertThat(namespaceContext.getNamespaceURI(DEFAULT_NS_PREFIX), is(equalTo("urn:ns"))); |
| 102 | + assertThat(namespaceContext.getPrefix("urn:ns"), is(equalTo(DEFAULT_NS_PREFIX))); |
| 103 | + assertThat(namespaceContext.getNamespaceURI("urn:ns1"), is(equalTo(DEFAULT_NS_PREFIX))); |
| 104 | + assertThat(namespaceContext.getPrefix("ns1"), is(nullValue())); |
| 105 | + Node branch = walker.nextNode(); |
| 106 | + namespaceContext.setContext(branch); |
| 107 | + assertThat(namespaceContext.getNamespaceURI(DEFAULT_NS_PREFIX), is(equalTo("urn:ns"))); |
| 108 | + assertThat(namespaceContext.getPrefix("urn:ns"), is(equalTo(DEFAULT_NS_PREFIX))); |
| 109 | + assertThat(namespaceContext.getNamespaceURI("ns1"), is(equalTo("urn:ns1"))); |
| 110 | + assertThat(namespaceContext.getPrefix("urn:ns1"), is(equalTo("ns1"))); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testOverriddenDefaultNamespace() throws Exception { |
| 115 | + Document document = createDocument("<root xmlns='urn:ns1'><branch xmlns='urn:ns2'/></root>"); |
| 116 | + TreeWalker walker = ((DocumentTraversal) document).createTreeWalker(document, SHOW_ELEMENT, null, true); |
| 117 | + Node root = walker.nextNode(); |
| 118 | + DOMNamespaceContext namespaceContext = new DOMNamespaceContext(root); |
| 119 | + assertThat(namespaceContext.getNamespaceURI(DEFAULT_NS_PREFIX), is(equalTo("urn:ns1"))); |
| 120 | + assertThat(namespaceContext.getPrefix("urn:ns1"), is(equalTo(DEFAULT_NS_PREFIX))); |
| 121 | + assertThat(namespaceContext.getPrefix("urn:ns2"), is(nullValue())); |
| 122 | + Node branch = walker.nextNode(); |
| 123 | + namespaceContext.setContext(branch); |
| 124 | + assertThat(namespaceContext.getNamespaceURI(DEFAULT_NS_PREFIX), is(equalTo("urn:ns2"))); |
| 125 | + assertThat(namespaceContext.getPrefix("urn:ns2"), is(equalTo(DEFAULT_NS_PREFIX))); |
| 126 | + assertThat(namespaceContext.getPrefix("urn:ns1"), is(nullValue())); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testGetPrefixesIsUnsupported() throws Exception { |
| 131 | + Document document = createDocument("<root/>"); |
| 132 | + TreeWalker walker = ((DocumentTraversal) document).createTreeWalker(document, SHOW_ELEMENT, null, true); |
| 133 | + Node root = walker.nextNode(); |
| 134 | + DOMNamespaceContext namespaceContext = new DOMNamespaceContext(root); |
| 135 | + assertThrows(UnsupportedOperationException.class, () -> namespaceContext.getPrefixes(NULL_NS_URI)); |
| 136 | + } |
| 137 | +} |
0 commit comments