diff options
author | Elliott Hughes <enh@google.com> | 2009-11-17 18:05:40 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2009-11-18 14:01:02 -0800 |
commit | ff42219e3ea3d712f931ae7f26af236339b5cf23 (patch) | |
tree | e7804dad30bb57b124eb5ce42aae8b7d0cd5d150 /xml/src/test/java/org/kxml2/io | |
parent | 6afd1e297342111db635163614383d62937ce5eb (diff) | |
download | libcore-ff42219e3ea3d712f931ae7f26af236339b5cf23.zip libcore-ff42219e3ea3d712f931ae7f26af236339b5cf23.tar.gz libcore-ff42219e3ea3d712f931ae7f26af236339b5cf23.tar.bz2 |
Fix KXmlSerializer so it won't generate invalid XML.
We were allowing arbitrary characters to be output (which, surprisingly,
XML does not), and we weren't correctly escaping CDATA sections that
contained "]]>".
Pull out some of my test helpers from DocumentBuilderTest into Support_Xml,
because they're more generally useful when writing tests involving XML.
Also correct a bunch of spelling mistakes in XmlSerializer's javadoc, since
I happened to be reading through.
Diffstat (limited to 'xml/src/test/java/org/kxml2/io')
-rw-r--r-- | xml/src/test/java/org/kxml2/io/KXmlSerializerTest.java | 105 |
1 files changed, 88 insertions, 17 deletions
diff --git a/xml/src/test/java/org/kxml2/io/KXmlSerializerTest.java b/xml/src/test/java/org/kxml2/io/KXmlSerializerTest.java index 2d5ddf7..01c7393 100644 --- a/xml/src/test/java/org/kxml2/io/KXmlSerializerTest.java +++ b/xml/src/test/java/org/kxml2/io/KXmlSerializerTest.java @@ -20,29 +20,100 @@ import junit.framework.TestCase; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.StringWriter; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xmlpull.v1.XmlSerializer; -public class KXmlSerializerTest extends TestCase { - - /** the namespace */ - final String ns = null; +import static tests.support.Support_Xml.*; - public void testEmittingNullCharacterThrows() throws IOException { +public class KXmlSerializerTest extends TestCase { + private static final String NAMESPACE = null; + + private static boolean isValidXmlCodePoint(int c) { + // http://www.w3.org/TR/REC-xml/#charsets + return (c >= 0x20 && c <= 0xd7ff) || (c == 0x9) || (c == 0xa) || (c == 0xd) || + (c >= 0xe000 && c <= 0xfffd) || (c >= 0x10000 && c <= 0x10ffff); + } + + private static XmlSerializer newSerializer() throws IOException { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); - KXmlSerializer serializer = new KXmlSerializer(); + XmlSerializer serializer = new KXmlSerializer(); serializer.setOutput(bytesOut, "UTF-8"); serializer.startDocument("UTF-8", null); - - serializer.startTag(ns, "foo"); - try { - serializer.text("bar\0baz"); - fail(); - } catch (IllegalArgumentException expected) { + return serializer; + } + + public void testInvalidCharactersInText() throws IOException { + XmlSerializer serializer = newSerializer(); + serializer.startTag(NAMESPACE, "root"); + for (int ch = 0; ch <= 0xffff; ++ch) { + final String s = Character.toString((char) ch); + if (isValidXmlCodePoint(ch)) { + serializer.text("a" + s + "b"); + } else { + try { + serializer.text("a" + s + "b"); + fail(s); + } catch (IllegalArgumentException expected) { + } + } } - - serializer.startTag(ns, "bar"); - try { - serializer.attribute(ns, "baz", "qu\0ux"); - } catch (IllegalArgumentException expected) { + serializer.endTag(NAMESPACE, "root"); + } + + public void testInvalidCharactersInAttributeValues() throws IOException { + XmlSerializer serializer = newSerializer(); + serializer.startTag(NAMESPACE, "root"); + for (int ch = 0; ch <= 0xffff; ++ch) { + final String s = Character.toString((char) ch); + if (isValidXmlCodePoint(ch)) { + serializer.attribute(NAMESPACE, "a", "a" + s + "b"); + } else { + try { + serializer.attribute(NAMESPACE, "a", "a" + s + "b"); + fail(s); + } catch (IllegalArgumentException expected) { + } + } + } + serializer.endTag(NAMESPACE, "root"); + } + + public void testInvalidCharactersInCdataSections() throws IOException { + XmlSerializer serializer = newSerializer(); + serializer.startTag(NAMESPACE, "root"); + for (int ch = 0; ch <= 0xffff; ++ch) { + final String s = Character.toString((char) ch); + if (isValidXmlCodePoint(ch)) { + serializer.cdsect("a" + s + "b"); + } else { + try { + serializer.cdsect("a" + s + "b"); + fail(s); + } catch (IllegalArgumentException expected) { + } + } + } + serializer.endTag(NAMESPACE, "root"); + } + + public void testCdataWithTerminatorInside() throws Exception { + StringWriter writer = new StringWriter(); + XmlSerializer serializer = new KXmlSerializer(); + serializer.setOutput(writer); + serializer.startDocument("UTF-8", null); + serializer.startTag(NAMESPACE, "p"); + serializer.cdsect("a]]>b"); + serializer.endTag(NAMESPACE, "p"); + serializer.endDocument(); + // Adjacent CDATA sections aren't merged, so let's stick them together ourselves... + Document doc = domOf(writer.toString()); + NodeList children = doc.getFirstChild().getChildNodes(); + String text = ""; + for (int i = 0; i < children.getLength(); ++i) { + text += children.item(i).getNodeValue(); } + assertEquals("a]]>b", text); } } |