1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package stream.XMLEventReaderTest; 24 25 import java.io.FilePermission; 26 import java.io.StringReader; 27 28 import javax.xml.stream.XMLEventReader; 29 import javax.xml.stream.XMLInputFactory; 30 import javax.xml.stream.events.EntityReference; 31 import javax.xml.stream.events.XMLEvent; 32 33 import jaxp.library.JAXPTestUtilities; 34 35 import org.testng.Assert; 36 import org.testng.annotations.Listeners; 37 import org.testng.annotations.Test; 38 39 /* 40 * @bug 6555001 41 * @summary Test StAX parser replaces the entity reference as setting. 42 */ 43 @Listeners({ jaxp.library.BasePolicy.class }) 44 public class Bug6555001 { 45 private static final String XML = "" 46 + "<!DOCTYPE doc SYSTEM 'file:///tmp/this/does/not/exist/but/that/is/ok' [" 47 + "<!ENTITY def '<para/>'>" + "]>" + "<doc>&def;&undef;</doc>"; 48 49 @Test 50 public void testReplacing() throws Exception { 51 JAXPTestUtilities.tryRunWithTmpPermission(() -> { 52 XMLInputFactory factory = XMLInputFactory.newInstance(); 53 factory.setProperty("javax.xml.stream.isReplacingEntityReferences", true); 54 55 StringReader sr = new StringReader(XML); 56 XMLEventReader reader = factory.createXMLEventReader(sr); 57 58 boolean sawUndef = false; 59 boolean sawDef = false; 60 61 while (reader.hasNext()) { 62 XMLEvent event = reader.nextEvent(); 63 // System.out.println("Event: " + event); 64 if (event.isEntityReference()) { 65 EntityReference ref = (EntityReference) event; 66 if ("def".equals(ref.getName())) { 67 sawDef = true; 68 } else if ("undef".equals(ref.getName())) { 69 sawUndef = true; 70 } else { 71 throw new IllegalArgumentException("Unexpected entity name"); 72 } 73 } 74 } 75 76 Assert.assertEquals(false, sawDef); 77 Assert.assertEquals(true, sawUndef); 78 reader.close(); 79 }, new FilePermission("/tmp/this/does/not/exist/but/that/is/ok", "read")); 80 } 81 82 @Test 83 public void testNotReplacing() throws Exception { 84 JAXPTestUtilities.tryRunWithTmpPermission(() -> { 85 XMLInputFactory factory = XMLInputFactory.newInstance(); 86 factory.setProperty("javax.xml.stream.isReplacingEntityReferences", false); 87 88 StringReader sr = new StringReader(XML); 89 XMLEventReader reader = factory.createXMLEventReader(sr); 90 91 boolean sawUndef = false; 92 boolean sawDef = false; 93 94 while (reader.hasNext()) { 95 XMLEvent event = reader.nextEvent(); 96 // System.out.println("Event: " + event); 97 if (event.isEntityReference()) { 98 EntityReference ref = (EntityReference) event; 99 if ("def".equals(ref.getName())) { 100 sawDef = true; 101 } else if ("undef".equals(ref.getName())) { 102 sawUndef = true; 103 } else { 104 throw new IllegalArgumentException("Unexpected entity name"); 105 } 106 } 107 } 108 109 Assert.assertEquals(true, sawDef); 110 Assert.assertEquals(true, sawUndef); 111 reader.close(); 112 }, new FilePermission("/tmp/this/does/not/exist/but/that/is/ok", "read")); 113 } 114 }