Path: blob/master/test/jdk/javax/xml/jaxp/parsers/8027359/FragmentScannerBufferLimitTest.java
41155 views
/*1* Copyright (c) 2014 SAP SE. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 8034087 802735926* @summary XML parser may overwrite element content if that content falls onto the border of an entity scanner buffer27* @run main FragmentScannerBufferLimitTest28*/29import java.io.*;3031import javax.xml.parsers.*;32import javax.xml.transform.*;33import org.w3c.dom.*;34import org.xml.sax.*;3536/**37* Test for overwriting of XML element content by the XML parser when reading over buffer38* limits.39*40* We create a simple XML document of the form:41*42* <?xml version=\"1.1\"?>43* <ROOT>44* <FILLER>ffffffff...fffff</FILLER>45* <TEST>content</TEST><TEST2>content2</TEST2>46* <FILLER>ffffffff...fffffffff</FILLER>47* </ROOT>48*49* What's important here is, that the test content is at the border of an entity scanner50* buffer (XMLEntityScanner uses 8192 byte buffers). That's why there are filler elements51* of sufficient length that ensure there is a buffer break inside the test content52* and there is enough to read to require another buffer read after the content has been53* read.54*55* With the faulty implementation, the test content gets overwritten with characters56* read from the next buffer, i.e. 'f's.57*58* @author [email protected]59*/60public class FragmentScannerBufferLimitTest {6162static int errCount = 0;6364/**65* Check the test content.66*/67public static void main(String[] args) throws ParserConfigurationException,68SAXException, IOException, TransformerConfigurationException,69TransformerException, TransformerFactoryConfigurationError {7071String testString = "<TEST>content</TEST><TEST2>content2</TEST2>";7273for (int i = 0; i < testString.length(); i++) {74test(createDocument(testString.toString(), i), ""+ i);75}7677if (errCount == 0) {78System.out.println("OK");79}80else {81System.out.println("ERROR");82throw new RuntimeException("Parsing error: element content has been overwritten");83}84}8586/**87* Create the test XML document.88* @param testString the test content string89* @param bufferLimitPosition the position in the string where the buffer should break90* @return the document91*/92private static String createDocument(String testString, int bufferLimitPosition) throws UnsupportedEncodingException {93StringBuilder result = new StringBuilder();94result.append("<?xml version=\"1.1\"?>");95result.append("<ROOT>");9697int fillerLength = 8192 - bufferLimitPosition;98createFiller(result, fillerLength);99100result.append(testString);101102createFiller(result, 9000);103result.append("</ROOT>");104return result.toString();105}106107/**108* Create the filler element of the given length.109* @param buffer the output buffer110* @param length the required length of the element, including the element tags111*/112private static void createFiller(StringBuilder buffer, int length) {113buffer.append("<FILLER>");114int fillLength = length - "<FILLER></FILLER>".length();115for (int i=0; i<fillLength; i++) {116buffer.append('f');117}118buffer.append("</FILLER>");119}120121122private static void test(String document, String testName) throws SAXException, IOException, ParserConfigurationException {123DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();124DocumentBuilder builder = factory.newDocumentBuilder();125126Document doc = builder.parse(new ByteArrayInputStream(document.getBytes("UTF-8")));127128// check that there is the root node129NodeList roots = doc.getElementsByTagName("ROOT");130assert roots.getLength() == 1;131Node root = roots.item(0);132133// check that root has children "FILLER" and "TEST"134NodeList children = root.getChildNodes();135assert children.getLength() == 4;136assert children.item(0).getNodeName().equals("FILLER");137assert children.item(1).getNodeName().equals("TEST");138assert children.item(2).getNodeName().equals("TEST2");139assert children.item(3).getNodeName().equals("FILLER");140141// check that the test node has content "content"142checkContent(children.item(1).getTextContent(), "content", document);143checkContent(children.item(2).getTextContent(), "content2", document);144}145146private static void checkContent(String found, String expected, String document) {147if (! (found.equals(expected))) {148errCount++;149int bufferStart = "<?xml version=\"1.1\"?><ROOT>".length() +1;150int bufferStart2 = bufferStart + 8192;151System.err.println("\nError:: expected \"" + expected152+ "\", but found \"" + found + "\"!");153System.err.println("Buffer was (probably): [ ... "154+ document.substring(bufferStart2 - 20, bufferStart2) + "] ["155+ document.substring(bufferStart2, bufferStart2 + 30) + " ... ]");156}157}158}159160161