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.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertSame;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  
26  import org.junit.jupiter.api.Test;
27  import org.xml.sax.EntityResolver;
28  import org.xml.sax.InputSource;
29  import org.xml.sax.SAXException;
30  import org.xml.sax.ext.DefaultHandler2;
31  import org.xml.sax.ext.EntityResolver2;
32  
33  class FallbackIgnoreEntityResolver2Test {
34  
35      @Test
36      void forwardsGetExternalSubsetOnlyToExtendedDelegates() throws Exception {
37          final FallbackIgnoreEntityResolver2 floor = new FallbackIgnoreEntityResolver2(null);
38          assertNull(floor.getExternalSubset("name", "https://example.test/base/"));
39          floor.setDelegate((publicId, systemId) -> new InputSource());
40          assertNull(floor.getExternalSubset("name", "https://example.test/base/"));
41          final InputSource expected = new InputSource();
42          floor.setDelegate(new DefaultHandler2() {
43  
44              @Override
45              public InputSource getExternalSubset(final String name, final String baseURI) {
46                  return expected;
47              }
48          });
49          assertSame(expected, floor.getExternalSubset("name", "https://example.test/base/"));
50      }
51  
52      @Test
53      void resolvesDelegatesAndAllFallbackPaths() throws Exception {
54          final FallbackIgnoreEntityResolver2 floor = new FallbackIgnoreEntityResolver2(null);
55          assertEquals("https://example.test/base/entity.dtd", floor.resolveEntity("name", "public", "https://example.test/base/", "entity.dtd").getSystemId());
56          assertEquals("entity.dtd", floor.resolveEntity("name", "public", "not a URI", "entity.dtd").getSystemId());
57          assertNotNull(floor.resolveEntity("public", null));
58          final InputSource expected = new InputSource();
59          final EntityResolver plain = (publicId, systemId) -> expected;
60          floor.setDelegate(plain);
61          assertSame(expected, floor.resolveEntity("name", "public", "https://example.test/base/", "entity.dtd"));
62          final EntityResolver2 extended = new DefaultHandler2() {
63  
64              @Override
65              public InputSource resolveEntity(final String name, final String publicId, final String base, final String system) {
66                  return expected;
67              }
68          };
69          floor.setDelegate(extended);
70          assertSame(expected, floor.resolveEntity("name", "public", "base", "system"));
71          floor.setDelegate(null);
72          System.setProperty(SecureException.THROW_ON_UNRESOLVED, "true");
73          try {
74              assertThrows(SAXException.class, () -> floor.resolveEntity("p", "s"));
75          } finally {
76              System.clearProperty(SecureException.THROW_ON_UNRESOLVED);
77          }
78      }
79  }