View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.xml.secure;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import javax.xml.XMLConstants;
27  import javax.xml.transform.Source;
28  import javax.xml.validation.Schema;
29  import javax.xml.validation.SchemaFactory;
30  
31  import org.junit.jupiter.api.Tag;
32  import org.junit.jupiter.api.Test;
33  import org.w3c.dom.ls.LSResourceResolver;
34  import org.xml.sax.ErrorHandler;
35  import org.xml.sax.SAXException;
36  import org.xml.sax.SAXNotRecognizedException;
37  import org.xml.sax.SAXNotSupportedException;
38  import org.xml.sax.helpers.DefaultHandler;
39  
40  @Tag("schema")
41  class SecureSchemaFactoryTest {
42  
43      private static final class PropertySchemaFactory extends SchemaFactory {
44  
45          private final SchemaFactory delegate = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
46  
47          @Override
48          public ErrorHandler getErrorHandler() {
49              return delegate.getErrorHandler();
50          }
51  
52          @Override
53          public boolean getFeature(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
54              return delegate.getFeature(name);
55          }
56  
57          @Override
58          public Object getProperty(final String name) {
59              return "value";
60          }
61  
62          @Override
63          public LSResourceResolver getResourceResolver() {
64              return delegate.getResourceResolver();
65          }
66  
67          @Override
68          public boolean isSchemaLanguageSupported(final String language) {
69              return delegate.isSchemaLanguageSupported(language);
70          }
71  
72          @Override
73          public Schema newSchema() throws SAXException {
74              return delegate.newSchema();
75          }
76  
77          @Override
78          public Schema newSchema(final Source[] sources) throws SAXException {
79              return delegate.newSchema(sources);
80          }
81  
82          @Override
83          public void setErrorHandler(final ErrorHandler handler) {
84              delegate.setErrorHandler(handler);
85          }
86  
87          @Override
88          public void setFeature(final String name, final boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
89              delegate.setFeature(name, value);
90          }
91  
92          @Override
93          public void setProperty(final String name, final Object value) {
94          }
95  
96          @Override
97          public void setResourceResolver(final LSResourceResolver resolver) {
98              delegate.setResourceResolver(resolver);
99          }
100     }
101 
102     @Test
103     void createsSecureSchemas() throws Exception {
104         assertInstanceOf(SecureSchema.class, SecureSchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema());
105         assertInstanceOf(SecureSchema.class, SecureSchemaFactory.newDefaultInstance().newSchema());
106     }
107 
108     @Test
109     void forwardsSchemaFactoryConfigurationAndAccessors() throws Exception {
110         final SchemaFactory factory = SecureSchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
111         final DefaultHandler errorHandler = new DefaultHandler();
112         final LSResourceResolver resolver = (type, namespace, publicId, systemId, base) -> null;
113         factory.setErrorHandler(errorHandler);
114         factory.setResourceResolver(resolver);
115         factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
116         factory.setProperty(TestConstants.EXTERNAL_SCHEMA_LOCATION, "urn:example schema.xsd");
117         assertEquals("urn:example schema.xsd", factory.getProperty(TestConstants.EXTERNAL_SCHEMA_LOCATION));
118         assertSame(errorHandler, factory.getErrorHandler());
119         assertSame(resolver, factory.getResourceResolver());
120         assertTrue(factory.isSchemaLanguageSupported(XMLConstants.W3C_XML_SCHEMA_NS_URI));
121         assertTrue(factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING));
122         assertThrows(SAXNotRecognizedException.class, () -> factory.getProperty("foo"));
123     }
124 
125     @Test
126     void returnsPropertiesFromTheDelegate() throws Exception {
127         final SchemaFactory factory = SecureSchemaFactory.secure(new PropertySchemaFactory());
128         assertEquals("value", factory.getProperty("test"));
129     }
130 }