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.assertNotSame;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.when;
26  
27  import java.lang.reflect.InvocationTargetException;
28  import java.lang.reflect.Method;
29  
30  import javax.xml.parsers.DocumentBuilderFactory;
31  import javax.xml.parsers.ParserConfigurationException;
32  import javax.xml.transform.TransformerException;
33  import javax.xml.transform.URIResolver;
34  import javax.xml.transform.dom.DOMSource;
35  
36  import org.junit.jupiter.api.Test;
37  import org.junit.jupiter.api.condition.DisabledInNativeImage;
38  import org.w3c.dom.Document;
39  
40  class FallbackIgnoreURIResolverTest {
41  
42      @Test
43      // Mockito generates the mock class and its plugin proxies at run time — impossible in a closed-world native image,
44      // so the stubbed factory this error path needs cannot be built there.
45      @DisabledInNativeImage
46      void newEmptyDocumentWrapsConfigurationFailure() throws Exception {
47          final DocumentBuilderFactory factory = mock(DocumentBuilderFactory.class);
48          final ParserConfigurationException failure = new ParserConfigurationException("test");
49          when(factory.newDocumentBuilder()).thenThrow(failure);
50          final Method method = FallbackIgnoreURIResolver.class.getDeclaredMethod("newEmptyDocument", DocumentBuilderFactory.class);
51          method.setAccessible(true);
52          final InvocationTargetException exception = assertThrows(InvocationTargetException.class, () -> method.invoke(null, factory));
53          final IllegalStateException error = (IllegalStateException) exception.getCause();
54          assertSame(failure, error.getCause());
55      }
56  
57      @Test
58      void resolvesDelegatedAndFallbackSources() throws Exception {
59          final DOMSource empty = new DOMSource();
60          final FallbackIgnoreURIResolver resolver = new FallbackIgnoreURIResolver(null, () -> empty, () -> false);
61          assertSame(empty, resolver.resolve("href", "base"));
62          final DOMSource delegated = new DOMSource();
63          final URIResolver delegate = (href, base) -> delegated;
64          resolver.setDelegate(delegate);
65          assertSame(delegate, resolver.getDelegate());
66          assertSame(delegated, resolver.resolve("href", "base"));
67          resolver.setDelegate(null);
68          System.setProperty(SecureException.THROW_ON_UNRESOLVED, "true");
69          try {
70              assertThrows(TransformerException.class, () -> resolver.resolve("href", "base"));
71          } finally {
72              System.clearProperty(SecureException.THROW_ON_UNRESOLVED);
73          }
74      }
75  
76      @Test
77      void resolvesFreshEmptyDocumentPerResolution() throws Exception {
78          final FallbackIgnoreURIResolver resolver = new FallbackIgnoreURIResolver(null, null, () -> false);
79          final Document first = (Document) ((DOMSource) resolver.resolve("href", "base")).getNode();
80          assertNull(first.getDocumentElement());
81          first.appendChild(first.createElement("planted"));
82          final Document second = (Document) ((DOMSource) resolver.resolve("href", "base")).getNode();
83          assertNotSame(first, second);
84          assertNull(second.getDocumentElement());
85      }
86  }