1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
#include "common.hpp"
#include "writer_string.hpp"
#include <string>
#include <sstream>
TEST_XML(write_simple, "<node attr='1'><child>text</child></node>")
{
CHECK_NODE_EX(doc, STR("<node attr=\"1\">\n<child>text</child>\n</node>\n"), STR(""), 0);
}
TEST_XML(write_raw, "<node attr='1'><child>text</child></node>")
{
CHECK_NODE_EX(doc, STR("<node attr=\"1\"><child>text</child></node>"), STR(""), format_raw);
}
TEST_XML(write_indent, "<node attr='1'><child><sub>text</sub></child></node>")
{
CHECK_NODE_EX(doc, STR("<node attr=\"1\">\n\t<child>\n\t\t<sub>text</sub>\n\t</child>\n</node>\n"), STR("\t"), format_indent);
}
TEST_XML(write_pcdata, "<node attr='1'><child><sub/>text</child></node>")
{
CHECK_NODE_EX(doc, STR("<node attr=\"1\">\n\t<child>\n\t\t<sub />\n\t\ttext\n\t</child>\n</node>\n"), STR("\t"), format_indent);
}
TEST_XML_FLAGS(write_cdata, "<![CDATA[value]]>", parse_cdata | parse_fragment)
{
CHECK_NODE(doc, STR("<![CDATA[value]]>"));
CHECK_NODE_EX(doc, STR("<![CDATA[value]]>\n"), STR(""), 0);
}
TEST_XML_FLAGS(write_cdata_empty, "<![CDATA[]]>", parse_cdata | parse_fragment)
{
CHECK_NODE(doc, STR("<![CDATA[]]>"));
CHECK_NODE_EX(doc, STR("<![CDATA[]]>\n"), STR(""), 0);
}
TEST_XML_FLAGS(write_cdata_escape, "<![CDATA[value]]>", parse_cdata | parse_fragment)
{
CHECK_NODE(doc, STR("<![CDATA[value]]>"));
doc.first_child().set_value(STR("1]]>2]]>3"));
CHECK_NODE(doc, STR("<![CDATA[1]]]]><![CDATA[>2]]]]><![CDATA[>3]]>"));
}
TEST_XML(write_cdata_inner, "<node><![CDATA[value]]></node>")
{
CHECK_NODE(doc, STR("<node><![CDATA[value]]></node>"));
CHECK_NODE_EX(doc, STR("<node><![CDATA[value]]></node>\n"), STR(""), 0);
}
TEST_XML_FLAGS(write_comment, "<!--text-->", parse_comments | parse_fragment)
{
CHECK_NODE(doc, STR("<!--text-->"));
CHECK_NODE_EX(doc, STR("<!--text-->\n"), STR(""), 0);
}
TEST_XML_FLAGS(write_pi, "<?name value?>", parse_pi | parse_fragment)
{
CHECK_NODE(doc, STR("<?name value?>"));
CHECK_NODE_EX(doc, STR("<?name value?>\n"), STR(""), 0);
}
TEST_XML_FLAGS(write_declaration, "<?xml version='2.0'?>", parse_declaration | parse_fragment)
{
CHECK_NODE(doc, STR("<?xml version=\"2.0\"?>"));
CHECK_NODE_EX(doc, STR("<?xml version=\"2.0\"?>\n"), STR(""), 0);
}
TEST_XML_FLAGS(write_doctype, "<!DOCTYPE id [ foo ]>", parse_doctype | parse_fragment)
{
CHECK_NODE(doc, STR("<!DOCTYPE id [ foo ]>"));
CHECK_NODE_EX(doc, STR("<!DOCTYPE id [ foo ]>\n"), STR(""), 0);
}
TEST_XML(write_escape, "<node attr=''>text</node>")
{
doc.child(STR("node")).attribute(STR("attr")) = STR("<>'\"&\x04\r\n\t");
doc.child(STR("node")).first_child().set_value(STR("<>'\"&\x04\r\n\t"));
CHECK_NODE(doc, STR("<node attr=\"<>'"& \t\"><>'\"&\r\n\t</node>"));
}
TEST_XML(write_escape_unicode, "<node attr='㰀'/>")
{
#ifdef PUGIXML_WCHAR_MODE
#ifdef U_LITERALS
CHECK_NODE(doc, STR("<node attr=\"\u3c00\" />"));
#else
CHECK_NODE(doc, STR("<node attr=\"\x3c00\" />"));
#endif
#else
CHECK_NODE(doc, STR("<node attr=\"\xe3\xb0\x80\" />"));
#endif
}
TEST_XML(write_no_escapes, "<node attr=''>text</node>")
{
doc.child(STR("node")).attribute(STR("attr")) = STR("<>'\"&\x04\r\n\t");
doc.child(STR("node")).first_child().set_value(STR("<>'\"&\x04\r\n\t"));
CHECK_NODE_EX(doc, STR("<node attr=\"<>'\"&\x04\r\n\t\"><>'\"&\x04\r\n\t</node>"), STR(""), format_raw | format_no_escapes);
}
struct test_writer: xml_writer
{
std::basic_string<pugi::char_t> contents;
virtual void write(const void* data, size_t size)
{
CHECK(size % sizeof(pugi::char_t) == 0);
contents += std::basic_string<pugi::char_t>(static_cast<const pugi::char_t*>(data), static_cast<const pugi::char_t*>(data) + size / sizeof(pugi::char_t));
}
};
TEST_XML(write_print_writer, "<node/>")
{
test_writer writer;
doc.print(writer, STR(""), format_default, get_native_encoding());
CHECK(writer.contents == STR("<node />\n"));
}
#ifndef PUGIXML_NO_STL
TEST_XML(write_print_stream, "<node/>")
{
std::ostringstream oss;
doc.print(oss, STR(""), format_default, encoding_utf8);
CHECK(oss.str() == "<node />\n");
}
TEST_XML(write_print_stream_encode, "<n/>")
{
std::ostringstream oss;
doc.print(oss, STR(""), format_default, encoding_utf16_be);
CHECK(oss.str() == std::string("\x00<\x00n\x00 \x00/\x00>\x00\n", 12));
}
TEST_XML(write_print_stream_wide, "<node/>")
{
std::basic_ostringstream<wchar_t> oss;
doc.print(oss, STR(""), format_default, encoding_utf8);
CHECK(oss.str() == L"<node />\n");
}
#endif
TEST_XML(write_huge_chunk, "<node/>")
{
std::basic_string<pugi::char_t> name(10000, STR('n'));
doc.child(STR("node")).set_name(name.c_str());
test_writer writer;
doc.print(writer, STR(""), format_default, get_native_encoding());
CHECK(writer.contents == STR("<") + name + STR(" />\n"));
}
TEST(write_encodings)
{
static char s_utf8[] = "<\x54\xC2\xA2\xE2\x82\xAC\xF0\xA4\xAD\xA2/>";
xml_document doc;
CHECK(doc.load_buffer(s_utf8, sizeof(s_utf8), parse_default, encoding_utf8));
CHECK(write_narrow(doc, format_default, encoding_utf8) == "<\x54\xC2\xA2\xE2\x82\xAC\xF0\xA4\xAD\xA2 />\n");
CHECK(test_write_narrow(doc, format_default, encoding_utf32_le, "<\x00\x00\x00\x54\x00\x00\x00\xA2\x00\x00\x00\xAC\x20\x00\x00\x62\x4B\x02\x00 \x00\x00\x00/\x00\x00\x00>\x00\x00\x00\n\x00\x00\x00", 36));
CHECK(test_write_narrow(doc, format_default, encoding_utf32_be, "\x00\x00\x00<\x00\x00\x00\x54\x00\x00\x00\xA2\x00\x00\x20\xAC\x00\x02\x4B\x62\x00\x00\x00 \x00\x00\x00/\x00\x00\x00>\x00\x00\x00\n", 36));
CHECK(write_narrow(doc, format_default, encoding_utf32) == write_narrow(doc, format_default, is_little_endian() ? encoding_utf32_le : encoding_utf32_be));
CHECK(test_write_narrow(doc, format_default, encoding_utf16_le, "<\x00\x54\x00\xA2\x00\xAC\x20\x52\xd8\x62\xdf \x00/\x00>\x00\n\x00", 20));
CHECK(test_write_narrow(doc, format_default, encoding_utf16_be, "\x00<\x00\x54\x00\xA2\x20\xAC\xd8\x52\xdf\x62\x00 \x00/\x00>\x00\n", 20));
CHECK(write_narrow(doc, format_default, encoding_utf16) == write_narrow(doc, format_default, is_little_endian() ? encoding_utf16_le : encoding_utf16_be));
size_t wcharsize = sizeof(wchar_t);
std::basic_string<wchar_t> v = write_wide(doc, format_default, encoding_wchar);
if (wcharsize == 4)
{
CHECK(v.size() == 9 && v[0] == '<' && v[1] == 0x54 && v[2] == 0xA2 && v[3] == 0x20AC && v[4] == wchar_cast(0x24B62) && v[5] == ' ' && v[6] == '/' && v[7] == '>' && v[8] == '\n');
}
else
{
CHECK(v.size() == 10 && v[0] == '<' && v[1] == 0x54 && v[2] == 0xA2 && v[3] == 0x20AC && v[4] == wchar_cast(0xd852) && v[5] == wchar_cast(0xdf62) && v[6] == ' ' && v[7] == '/' && v[8] == '>' && v[9] == '\n');
}
CHECK(test_write_narrow(doc, format_default, encoding_latin1, "<\x54\xA2?? />\n", 9));
}
#ifdef PUGIXML_WCHAR_MODE
TEST(write_encoding_huge)
{
const unsigned int N = 16000;
// make a large utf16 name consisting of 6-byte char pairs (6 does not divide internal buffer size, so will need split correction)
std::string s_utf16 = std::string("\x00<", 2);
for (unsigned int i = 0; i < N; ++i) s_utf16 += "\x20\xAC\xd8\x52\xdf\x62";
s_utf16 += std::string("\x00/\x00>", 4);
xml_document doc;
CHECK(doc.load_buffer(&s_utf16[0], s_utf16.length(), parse_default, encoding_utf16_be));
std::string s_utf8 = "<";
for (unsigned int j = 0; j < N; ++j) s_utf8 += "\xE2\x82\xAC\xF0\xA4\xAD\xA2";
s_utf8 += " />\n";
CHECK(test_write_narrow(doc, format_default, encoding_utf8, s_utf8.c_str(), s_utf8.length()));
}
TEST(write_encoding_huge_invalid)
{
size_t wcharsize = sizeof(wchar_t);
if (wcharsize == 2)
{
const unsigned int N = 16000;
// make a large utf16 name consisting of leading surrogate chars
std::basic_string<wchar_t> s_utf16;
for (unsigned int i = 0; i < N; ++i) s_utf16 += static_cast<wchar_t>(0xd852);
xml_document doc;
doc.append_child().set_name(s_utf16.c_str());
CHECK(test_write_narrow(doc, format_default, encoding_utf8, "< />\n", 5));
}
}
#else
TEST(write_encoding_huge)
{
const unsigned int N = 16000;
// make a large utf8 name consisting of 3-byte chars (3 does not divide internal buffer size, so will need split correction)
std::string s_utf8 = "<";
for (unsigned int i = 0; i < N; ++i) s_utf8 += "\xE2\x82\xAC";
s_utf8 += "/>";
xml_document doc;
CHECK(doc.load_buffer(&s_utf8[0], s_utf8.length(), parse_default, encoding_utf8));
std::string s_utf16 = std::string("\x00<", 2);
for (unsigned int j = 0; j < N; ++j) s_utf16 += "\x20\xAC";
s_utf16 += std::string("\x00 \x00/\x00>\x00\n", 8);
CHECK(test_write_narrow(doc, format_default, encoding_utf16_be, s_utf16.c_str(), s_utf16.length()));
}
TEST(write_encoding_huge_invalid)
{
const unsigned int N = 16000;
// make a large utf8 name consisting of non-leading chars
std::string s_utf8;
for (unsigned int i = 0; i < N; ++i) s_utf8 += "\x82";
xml_document doc;
doc.append_child().set_name(s_utf8.c_str());
std::string s_utf16 = std::string("\x00<\x00 \x00/\x00>\x00\n", 10);
CHECK(test_write_narrow(doc, format_default, encoding_utf16_be, s_utf16.c_str(), s_utf16.length()));
}
#endif
TEST(write_unicode_escape)
{
char s_utf8[] = "<\xE2\x82\xAC \xC2\xA2='\"\xF0\xA4\xAD\xA2
\"'>&\x14\xF0\xA4\xAD\xA2<</\xE2\x82\xAC>";
xml_document doc;
CHECK(doc.load_buffer(s_utf8, sizeof(s_utf8), parse_default, encoding_utf8));
CHECK(write_narrow(doc, format_default, encoding_utf8) == "<\xE2\x82\xAC \xC2\xA2=\""\xF0\xA4\xAD\xA2 "\">&\xF0\xA4\xAD\xA2<</\xE2\x82\xAC>\n");
}
#ifdef PUGIXML_WCHAR_MODE
static bool test_write_unicode_invalid(const wchar_t* name, const char* expected)
{
xml_document doc;
doc.append_child(node_pcdata).set_value(name);
return write_narrow(doc, format_raw, encoding_utf8) == expected;
}
TEST(write_unicode_invalid_utf16)
{
size_t wcharsize = sizeof(wchar_t);
if (wcharsize == 2)
{
// check non-terminated degenerate handling
#ifdef U_LITERALS
CHECK(test_write_unicode_invalid(L"a\uda1d", "a"));
CHECK(test_write_unicode_invalid(L"a\uda1d_", "a_"));
#else
CHECK(test_write_unicode_invalid(L"a\xda1d", "a"));
CHECK(test_write_unicode_invalid(L"a\xda1d_", "a_"));
#endif
// check incorrect leading code
#ifdef U_LITERALS
CHECK(test_write_unicode_invalid(L"a\ude24", "a"));
CHECK(test_write_unicode_invalid(L"a\ude24_", "a_"));
#else
CHECK(test_write_unicode_invalid(L"a\xde24", "a"));
CHECK(test_write_unicode_invalid(L"a\xde24_", "a_"));
#endif
}
}
#else
static bool test_write_unicode_invalid(const char* name, const wchar_t* expected)
{
xml_document doc;
doc.append_child(node_pcdata).set_value(name);
return write_wide(doc, format_raw, encoding_wchar) == expected;
}
TEST(write_unicode_invalid_utf8)
{
// invalid 1-byte input
CHECK(test_write_unicode_invalid("a\xb0", L"a"));
CHECK(test_write_unicode_invalid("a\xb0_", L"a_"));
// invalid 2-byte input
CHECK(test_write_unicode_invalid("a\xc0", L"a"));
CHECK(test_write_unicode_invalid("a\xd0", L"a"));
CHECK(test_write_unicode_invalid("a\xc0_", L"a_"));
CHECK(test_write_unicode_invalid("a\xd0_", L"a_"));
// invalid 3-byte input
CHECK(test_write_unicode_invalid("a\xe2\x80", L"a"));
CHECK(test_write_unicode_invalid("a\xe2", L"a"));
CHECK(test_write_unicode_invalid("a\xe2\x80_", L"a_"));
CHECK(test_write_unicode_invalid("a\xe2_", L"a_"));
// invalid 4-byte input
CHECK(test_write_unicode_invalid("a\xf2\x97\x98", L"a"));
CHECK(test_write_unicode_invalid("a\xf2\x97", L"a"));
CHECK(test_write_unicode_invalid("a\xf2", L"a"));
CHECK(test_write_unicode_invalid("a\xf2\x97\x98_", L"a_"));
CHECK(test_write_unicode_invalid("a\xf2\x97_", L"a_"));
CHECK(test_write_unicode_invalid("a\xf2_", L"a_"));
// invalid 5-byte input
CHECK(test_write_unicode_invalid("a\xf8_", L"a_"));
}
#endif
TEST(write_no_name_element)
{
xml_document doc;
xml_node root = doc.append_child();
root.append_child();
root.append_child().append_child(node_pcdata).set_value(STR("text"));
CHECK_NODE(doc, STR("<:anonymous><:anonymous /><:anonymous>text</:anonymous></:anonymous>"));
CHECK_NODE_EX(doc, STR("<:anonymous>\n\t<:anonymous />\n\t<:anonymous>text</:anonymous>\n</:anonymous>\n"), STR("\t"), format_default);
}
TEST(write_no_name_pi)
{
xml_document doc;
doc.append_child(node_pi);
CHECK_NODE(doc, STR("<?:anonymous?>"));
}
TEST(write_no_name_attribute)
{
xml_document doc;
doc.append_child().set_name(STR("root"));
doc.child(STR("root")).append_attribute(STR(""));
CHECK_NODE(doc, STR("<root :anonymous=\"\" />"));
}
TEST(write_print_empty)
{
test_writer writer;
xml_node().print(writer);
}
#ifndef PUGIXML_NO_STL
TEST(write_print_stream_empty)
{
std::ostringstream oss;
xml_node().print(oss);
}
TEST(write_print_stream_empty_wide)
{
std::basic_ostringstream<wchar_t> oss;
xml_node().print(oss);
}
#endif
TEST(write_stackless)
{
unsigned int count = 20000;
std::basic_string<pugi::char_t> data;
for (unsigned int i = 0; i < count; ++i)
data += "<a>";
data += "text";
for (unsigned int i = 0; i < count; ++i)
data += "</a>";
xml_document doc;
CHECK(doc.load(data.c_str()));
CHECK_NODE(doc, data.c_str());
}
|