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.Test;
  35 
  36 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
  37 
  38 /*
  39  * @bug 8153781
  40  * @summary Test if method skipDTD of class XMLDTDScannerImpl will correctly skip the DTD section,
  41  *          even if a call to XMLEntityScanner.scanData for skipping to the closing ']' returns true.
  42  */
  43 public class Bug8153781 {
  44     public static int DOCTYPE_SECTION_LENGTH = XMLEntityManager.DEFAULT_BUFFER_SIZE * 2;
  45     public static int DOCUMENT_LENGTH = DOCTYPE_SECTION_LENGTH + 4096;
  46 
  47     public String createXMLDocument(int doctypeoffset) {
  48         StringBuilder xmlcontentbuilder = new StringBuilder(DOCUMENT_LENGTH);
  49         xmlcontentbuilder.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n");
  50         xmlcontentbuilder.append("<!DOCTYPE dummy [\r\n");
  51         xmlcontentbuilder.append("  <!ELEMENT dummy EMPTY>\r\n");
  52         xmlcontentbuilder.append("  <!--\r\n");
  53         int doctypelines = DOCTYPE_SECTION_LENGTH / 3;
  54         for (int i = 0; i < doctypeoffset; i++)
  55             xmlcontentbuilder.append('a');
  56         for (int i = 0; i < doctypelines; i++)
  57             xmlcontentbuilder.append("a\r\n");
  58         xmlcontentbuilder.append("  -->\r\n");
  59         xmlcontentbuilder.append("  ]\r\n");
  60         xmlcontentbuilder.append(">\r\n");
  61         xmlcontentbuilder.append("<dummy>\r\n");
  62         xmlcontentbuilder.append("</dummy>\r\n");
  63         System.out.println("Document length:" + xmlcontentbuilder.length());
  64         return xmlcontentbuilder.toString();
  65     }
  66 
  67     public void runReader(XMLInputFactory factory, int offset) throws XMLStreamException {
  68         StringReader stringReader = new StringReader(createXMLDocument(offset));
  69         XMLEventReader reader = factory.createXMLEventReader(stringReader);
  70 
  71         while (reader.hasNext()) {
  72             XMLEvent event = reader.nextEvent();
  73             System.out.println("Event Type: " + event.getEventType());
  74         }
  75     }
  76 
  77     @Test
  78     public void test() {
  79         try {
  80             XMLInputFactory factory = XMLInputFactory.newInstance();
  81             factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
  82             // as the buffer ending we want to test needs to be "\r\n<any char>",
  83             // and the buffer consists of this sequence repeatedly, we try 3 runs
  84             // where we shift the offset by one each time and hence have each
  85             // of these chars at the last position once
  86             for (int i = 0; i < 3; i++) {
  87                 runReader(factory, i);
  88             }
  89         } catch (XMLStreamException xe) {
  90             xe.printStackTrace();
  91             Assert.fail(xe.getMessage());
  92         }
  93     }
  94 }