1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44
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 }