22
22
import junit .framework .TestCase ;
23
23
import org .kxml2 .io .KXmlSerializer ;
24
24
import org .w3c .dom .Document ;
25
+ import org .w3c .dom .Node ;
25
26
import org .w3c .dom .NodeList ;
27
+ import org .w3c .dom .Text ;
26
28
import org .xmlpull .v1 .XmlSerializer ;
27
29
import static tests .support .Support_Xml .domOf ;
28
30
@@ -87,12 +89,67 @@ private static XmlSerializer newSerializer() throws IOException {
87
89
return serializer ;
88
90
}
89
91
92
+ public String fromCodePoint (int codePoint ) {
93
+ if (codePoint > Character .MAX_VALUE ) {
94
+ return new String (Character .toChars (codePoint ));
95
+ }
96
+ return Character .toString ((char ) codePoint );
97
+ }
98
+
99
+ // http://b/17960630
100
+ public void testSpeakNoEvilMonkeys () throws Exception {
101
+ StringWriter stringWriter = new StringWriter ();
102
+ XmlSerializer serializer = new KXmlSerializer ();
103
+ serializer .setOutput (stringWriter );
104
+ serializer .startDocument ("UTF-8" , null );
105
+ serializer .startTag (NAMESPACE , "tag" );
106
+ serializer .attribute (NAMESPACE , "attr" , "a\ud83d \ude4a b" );
107
+ serializer .text ("c\ud83d \ude4a d" );
108
+ serializer .cdsect ("e\ud83d \ude4a f" );
109
+ serializer .endTag (NAMESPACE , "tag" );
110
+ serializer .endDocument ();
111
+ assertXmlEquals ("<tag attr=\" a🙊b\" >" +
112
+ "c🙊d" +
113
+ "<![CDATA[e]]>🙊<![CDATA[f]]>" +
114
+ "</tag>" , stringWriter .toString ());
115
+
116
+ // Check we can parse what we just output.
117
+ Document doc = domOf (stringWriter .toString ());
118
+ Node root = doc .getDocumentElement ();
119
+ assertEquals ("a\ud83d \ude4a b" , root .getAttributes ().getNamedItem ("attr" ).getNodeValue ());
120
+ Text text = (Text ) root .getFirstChild ();
121
+ assertEquals ("c\ud83d \ude4a de\ud83d \ude4a f" , text .getNodeValue ());
122
+ }
123
+
124
+ public void testBadSurrogates () throws Exception {
125
+ StringWriter stringWriter = new StringWriter ();
126
+ XmlSerializer serializer = new KXmlSerializer ();
127
+ serializer .setOutput (stringWriter );
128
+ serializer .startDocument ("UTF-8" , null );
129
+ serializer .startTag (NAMESPACE , "tag" );
130
+ try {
131
+ serializer .attribute (NAMESPACE , "attr" , "a\ud83d \u0040 b" );
132
+ } catch (IllegalArgumentException expected ) {
133
+ }
134
+ try {
135
+ serializer .text ("c\ud83d \u0040 d" );
136
+ } catch (IllegalArgumentException expected ) {
137
+ }
138
+ try {
139
+ serializer .cdsect ("e\ud83d \u0040 f" );
140
+ } catch (IllegalArgumentException expected ) {
141
+ }
142
+ }
143
+
144
+ // Cover all the BMP code points plus a few that require us to use surrogates.
145
+ private static int MAX_TEST_CODE_POINT = 0x10008 ;
146
+
90
147
public void testInvalidCharactersInText () throws IOException {
91
148
XmlSerializer serializer = newSerializer ();
92
149
serializer .startTag (NAMESPACE , "root" );
93
- for (int ch = 0 ; ch <= 0xffff ; ++ch ) {
94
- final String s = Character . toString (( char ) ch );
95
- if (isValidXmlCodePoint (ch )) {
150
+ for (int c = 0 ; c <= MAX_TEST_CODE_POINT ; ++c ) {
151
+ final String s = fromCodePoint ( c );
152
+ if (isValidXmlCodePoint (c )) {
96
153
serializer .text ("a" + s + "b" );
97
154
} else {
98
155
try {
@@ -108,9 +165,9 @@ public void testInvalidCharactersInText() throws IOException {
108
165
public void testInvalidCharactersInAttributeValues () throws IOException {
109
166
XmlSerializer serializer = newSerializer ();
110
167
serializer .startTag (NAMESPACE , "root" );
111
- for (int ch = 0 ; ch <= 0xffff ; ++ch ) {
112
- final String s = Character . toString (( char ) ch );
113
- if (isValidXmlCodePoint (ch )) {
168
+ for (int c = 0 ; c <= MAX_TEST_CODE_POINT ; ++c ) {
169
+ final String s = fromCodePoint ( c );
170
+ if (isValidXmlCodePoint (c )) {
114
171
serializer .attribute (NAMESPACE , "a" , "a" + s + "b" );
115
172
} else {
116
173
try {
@@ -126,9 +183,9 @@ public void testInvalidCharactersInAttributeValues() throws IOException {
126
183
public void testInvalidCharactersInCdataSections () throws IOException {
127
184
XmlSerializer serializer = newSerializer ();
128
185
serializer .startTag (NAMESPACE , "root" );
129
- for (int ch = 0 ; ch <= 0xffff ; ++ch ) {
130
- final String s = Character . toString (( char ) ch );
131
- if (isValidXmlCodePoint (ch )) {
186
+ for (int c = 0 ; c <= MAX_TEST_CODE_POINT ; ++c ) {
187
+ final String s = fromCodePoint ( c );
188
+ if (isValidXmlCodePoint (c )) {
132
189
serializer .cdsect ("a" + s + "b" );
133
190
} else {
134
191
try {
0 commit comments