1 /*
   2  * Copyright (c) 2016, 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 
  24 package stream.XMLEventReaderTest;
  25 
  26 import java.io.StringReader;
  27 
  28 import javax.xml.stream.XMLEventReader;
  29 import javax.xml.stream.XMLInputFactory;
  30 import javax.xml.stream.XMLStreamException;
  31 import javax.xml.stream.events.XMLEvent;
  32 
  33 import org.testng.Assert;
  34 import org.testng.annotations.Listeners;
  35 import org.testng.annotations.Test;
  36 
  37 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
  38 
  39 /*
  40  * @test
  41  * @bug 8153781
  42  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  43  * @run testng/othervm -DrunSecMngr=true stream.XMLEventReaderTest.Bug8153781
  44  * @run testng/othervm stream.XMLEventReaderTest.Bug8153781
  45  * @summary Test if method skipDTD of class XMLDTDScannerImpl will correctly skip the DTD section,
  46  *          even if a call to XMLEntityScanner.scanData for skipping to the closing ']' returns true.
  47  */
  48 @Listeners({jaxp.library.BasePolicy.class})
  49 public class Bug8153781 {
  50     public static int DOCTYPE_SECTION_LENGTH = XMLEntityManager.DEFAULT_BUFFER_SIZE * 2;
  51     public static int DOCUMENT_LENGTH = DOCTYPE_SECTION_LENGTH + 4096;
  52 
  53     public String createXMLDocument(int doctypeoffset) {
  54         StringBuilder xmlcontentbuilder = new StringBuilder(DOCUMENT_LENGTH);
  55         xmlcontentbuilder.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n");
  56         xmlcontentbuilder.append("<!DOCTYPE dummy [\r\n");
  57         xmlcontentbuilder.append("  <!ELEMENT dummy EMPTY>\r\n");
  58         xmlcontentbuilder.append("  <!--\r\n");
  59         int doctypelines = DOCTYPE_SECTION_LENGTH / 3;
  60         for (int i = 0; i < doctypeoffset; i++)
  61             xmlcontentbuilder.append('a');
  62         for (int i = 0; i < doctypelines; i++)
  63             xmlcontentbuilder.append("a\r\n");
  64         xmlcontentbuilder.append("  -->\r\n");
  65         xmlcontentbuilder.append("  ]\r\n");
  66         xmlcontentbuilder.append(">\r\n");
  67         xmlcontentbuilder.append("<dummy>\r\n");
  68         xmlcontentbuilder.append("</dummy>\r\n");
  69         System.out.println("Document length:" + xmlcontentbuilder.length());
  70         return xmlcontentbuilder.toString();
  71     }
  72 
  73     public void runReader(XMLInputFactory factory, int offset) throws XMLStreamException {
  74         StringReader stringReader = new StringReader(createXMLDocument(offset));
  75         XMLEventReader reader = factory.createXMLEventReader(stringReader);
  76 
  77         while (reader.hasNext()) {
  78             XMLEvent event = reader.nextEvent();
  79             System.out.println("Event Type: " + event.getEventType());
  80         }
  81     }
  82 
  83     @Test
  84     public void test() {
  85         try {
  86             XMLInputFactory factory = XMLInputFactory.newInstance();
  87             factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
  88             for (int i = 0; i < 3; i++) {
  89                 runReader(factory, i);
  90             }
  91         } catch (XMLStreamException xe) {
  92             xe.printStackTrace();
  93             Assert.fail(xe.getMessage());
  94         }
  95     }
  96 }
  97