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.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 }