aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorjoehw <unknown>2019-03-28 15:11:25 +0000
committerbell-sw <liberica@bell-sw.com>2019-07-22 19:22:21 +0300
commitbaf36d0b7488bda2cc2343fee1d6403ccdb41ab6 (patch)
treed128073ce3cde25157488900e10d9e71cc44ba11 /test
parent7b4a14ca346f8eed56d9c71e7f8286ae3855f11c (diff)
downloadjdk8u_jdk-baf36d0b7488bda2cc2343fee1d6403ccdb41ab6.tar.gz
8207760: SAXException: Invalid UTF-16 surrogate detected: d83c ?
Summary: Add JDK8207760.java jtreg test. Reviewed-by: lancea, dfuchs
Diffstat (limited to 'test')
-rw-r--r--test/javax/xml/jaxp/transform/8207760/JDK8207760.java171
1 files changed, 171 insertions, 0 deletions
diff --git a/test/javax/xml/jaxp/transform/8207760/JDK8207760.java b/test/javax/xml/jaxp/transform/8207760/JDK8207760.java
new file mode 100644
index 0000000000..5ad5f56822
--- /dev/null
+++ b/test/javax/xml/jaxp/transform/8207760/JDK8207760.java
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.testng.Assert;
+import org.testng.annotations.Listeners;
+import org.testng.annotations.Test;
+import java.util.Random;
+import javax.xml.transform.OutputKeys;
+import org.testng.annotations.DataProvider;
+
+/*
+ * @test
+ * @run testng/othervm JDK8207760
+ * @summary Verifies that a surrogate pair at the edge of a buffer is properly handled
+ * @bug 8207760
+ */
+public class JDK8207760 {
+ final String xsl8207760 =
+ "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +
+ " <xsl:output omit-xml-declaration=\"yes\" indent=\"no\" />\n" +
+ "\n" +
+ " <xsl:template match=\"node()|@*\">\n" +
+ " <xsl:copy>\n" +
+ " <xsl:apply-templates select=\"node()|@*\" />\n" +
+ " </xsl:copy>\n" +
+ " </xsl:template>\n" +
+ "</xsl:stylesheet>\n";
+
+ final String xsl8207760_2 = "<xsl:stylesheet \n" +
+ " version=\"1.0\" \n" +
+ " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +
+ "\n" +
+ " <xsl:output method=\"xml\" indent=\"no\" cdata-section-elements=\"source\"/>\n" +
+ "\n" +
+ " <xsl:template match=\"source\">\n" +
+ " <xsl:copy>\n" +
+ " <xsl:apply-templates select=\"node()\" />\n" +
+ " </xsl:copy>\n" +
+ " </xsl:template>\n" +
+ "\n" +
+ "</xsl:stylesheet>";
+
+ final String xsl8207760_3 = "<xsl:stylesheet \n" +
+ " version=\"1.0\" \n" +
+ " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +
+ "\n" +
+ " <xsl:output method=\"xml\" indent=\"no\" cdata-section-elements=\"source\"/>\n" +
+ "\n" +
+ " <xsl:template match=\"source\">\n" +
+ " <xsl:copy>\n" +
+ " <!-- Copy the attributes -->\n" +
+ " <xsl:apply-templates select=\"@*\"/>\n" +
+ " <!-- Convert the contained nodes (elements and text) into text -->\n" +
+ " <xsl:variable name=\"subElementsText\">\n" +
+ " <xsl:apply-templates select=\"node()\"/>\n" +
+ " </xsl:variable>\n" +
+ " <!-- Output the XML directive and the converted nodes -->\n" +
+ " <xsl:value-of select=\"$subElementsText\"/>\n" +
+ " </xsl:copy>\n" +
+ " </xsl:template>\n" +
+ "\n" +
+ "</xsl:stylesheet>";
+
+ @DataProvider(name = "xsls")
+ public Object[][] getDataBug8207760_cdata() {
+ return new Object[][]{
+ {xsl8207760_2},
+ {xsl8207760_3},
+ };
+ }
+
+ /*
+ * @bug 8207760
+ * Verifies that a surrogate pair at the edge of a buffer is properly handled
+ * when serializing into a Character section.
+ */
+ @Test
+ public final void testBug8207760() throws Exception {
+ String[] xmls = prepareXML(false);
+ Transformer t = createTransformerFromInputstream(
+ new ByteArrayInputStream(xsl8207760.getBytes(StandardCharsets.UTF_8)));
+ t.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
+ StringWriter sw = new StringWriter();
+ t.transform(new StreamSource(new StringReader(xmls[0])), new StreamResult(sw));
+ Assert.assertEquals(sw.toString().replaceAll(System.lineSeparator(), "\n"), xmls[1]);
+ }
+
+ /*
+ * @bug 8207760
+ * Verifies that a surrogate pair at the edge of a buffer is properly handled
+ * when serializing into a CDATA section.
+ */
+ @Test(dataProvider = "xsls")
+ public final void testBug8207760_cdata(String xsl) throws Exception {
+ String[] xmls = prepareXML(true);
+ Transformer t = createTransformerFromInputstream(
+ new ByteArrayInputStream(xsl.getBytes(StandardCharsets.UTF_8)));
+ t.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
+ StringWriter sw = new StringWriter();
+ t.transform(new StreamSource(new StringReader(xmls[0])), new StreamResult(sw));
+ Assert.assertEquals(sw.toString().replaceAll(System.lineSeparator(), "\n"), xmls[1]);
+ }
+
+ private String[] prepareXML(boolean cdata) {
+ String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><source>";
+ if (cdata) {
+ xml += "<![CDATA[";
+ }
+ String tail = "abc 123 </source>";
+ if (cdata) {
+ tail = "abc 123 ]]></source>";
+ }
+ String temp = generateString(1023);
+ xml = xml + temp + '\uD83C' + '\uDF42' + tail;
+ //xml = xml + temp + tail;
+ String expected = (!cdata) ? "<source>" + temp + "&#127810;" + tail
+ : xml;
+
+ return new String[]{xml, expected};
+ }
+
+ static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz \n".toCharArray();
+ StringBuilder sb = new StringBuilder(1024 << 4);
+ Random random = new Random();
+
+ private String generateString(int size) {
+ sb.setLength(0);
+ for (int i = 0; i < size; i++) {
+ char c = CHARS[random.nextInt(CHARS.length)];
+ sb.append(c);
+ }
+
+ return sb.toString();
+ }
+
+ private Transformer createTransformerFromInputstream(InputStream xslStream)
+ throws TransformerException {
+ return TransformerFactory.newInstance().newTransformer(new StreamSource(xslStream));
+ }
+}