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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
|
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE 0
#include <string.h> // because Borland's STL is braindead, we have to include <string.h> _before_ <string> in order to get memcpy
#include "common.hpp"
#include "writer_string.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#ifdef __MINGW32__
# include <io.h> // for unlink in C++0x mode
#endif
#if defined(__CELLOS_LV2__)
# include <unistd.h> // for unlink
#endif
TEST(document_create_empty)
{
pugi::xml_document doc;
CHECK_NODE(doc, STR(""));
}
TEST(document_create)
{
pugi::xml_document doc;
doc.append_child().set_name(STR("node"));
CHECK_NODE(doc, STR("<node />"));
}
#ifndef PUGIXML_NO_STL
TEST(document_load_stream)
{
pugi::xml_document doc;
std::istringstream iss("<node/>");
CHECK(doc.load(iss));
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_load_stream_offset)
{
pugi::xml_document doc;
std::istringstream iss("<foobar> <node/>");
std::string s;
iss >> s;
CHECK(doc.load(iss));
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_load_stream_text)
{
pugi::xml_document doc;
std::ifstream iss("tests/data/multiline.xml");
CHECK(doc.load(iss));
CHECK_NODE(doc, STR("<node1 /><node2 /><node3 />"));
}
TEST(document_load_stream_error)
{
pugi::xml_document doc;
std::ifstream fs("filedoesnotexist");
CHECK(doc.load(fs).status == status_io_error);
std::istringstream iss("<node/>");
test_runner::_memory_fail_threshold = 1;
CHECK(doc.load(iss).status == status_out_of_memory);
}
TEST(document_load_stream_empty)
{
std::istringstream iss;
pugi::xml_document doc;
doc.load(iss); // parse result depends on STL implementation
CHECK(!doc.first_child());
}
TEST(document_load_stream_wide)
{
pugi::xml_document doc;
std::basic_istringstream<wchar_t> iss(L"<node/>");
CHECK(doc.load(iss));
CHECK_NODE(doc, STR("<node />"));
}
#ifndef PUGIXML_NO_EXCEPTIONS
TEST(document_load_stream_exceptions)
{
pugi::xml_document doc;
// Windows has newline translation for text-mode files, so reading from this stream reaches eof and sets fail|eof bits.
// This test does not cause stream to throw an exception on Linux - I have no idea how to get read() to fail except
// newline translation.
std::ifstream iss("tests/data/multiline.xml");
iss.exceptions(std::ios::eofbit | std::ios::badbit | std::ios::failbit);
try
{
doc.load(iss);
CHECK(iss.good()); // if the exception was not thrown, stream reading should succeed without errors
}
catch (const std::ios_base::failure&)
{
CHECK(!doc.first_child());
}
}
#endif
TEST(document_load_stream_error_previous)
{
pugi::xml_document doc;
CHECK(doc.load(STR("<node/>")));
CHECK(doc.first_child());
std::ifstream fs1("filedoesnotexist");
CHECK(doc.load(fs1).status == status_io_error);
CHECK(!doc.first_child());
}
TEST(document_load_stream_wide_error_previous)
{
pugi::xml_document doc;
CHECK(doc.load(STR("<node/>")));
CHECK(doc.first_child());
std::basic_ifstream<wchar_t> fs1("filedoesnotexist");
CHECK(doc.load(fs1).status == status_io_error);
CHECK(!doc.first_child());
}
template <typename T> class char_array_buffer: public std::basic_streambuf<T>
{
public:
char_array_buffer(T* begin, T* end)
{
this->setg(begin, begin, end);
}
typename std::basic_streambuf<T>::int_type underflow()
{
return this->gptr() == this->egptr() ? std::basic_streambuf<T>::traits_type::eof() : std::basic_streambuf<T>::traits_type::to_int_type(*this->gptr());
}
};
TEST(document_load_stream_nonseekable)
{
char contents[] = "<node />";
char_array_buffer<char> buffer(contents, contents + sizeof(contents) / sizeof(contents[0]));
std::istream in(&buffer);
pugi::xml_document doc;
CHECK(doc.load(in));
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_load_stream_wide_nonseekable)
{
wchar_t contents[] = L"<node />";
char_array_buffer<wchar_t> buffer(contents, contents + sizeof(contents) / sizeof(contents[0]));
std::basic_istream<wchar_t> in(&buffer);
pugi::xml_document doc;
CHECK(doc.load(in));
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_load_stream_nonseekable_large)
{
std::basic_string<pugi::char_t> str;
str += STR("<node>");
for (int i = 0; i < 10000; ++i) str += STR("<node />");
str += STR("</node>");
char_array_buffer<pugi::char_t> buffer(&str[0], &str[0] + str.length());
std::basic_istream<pugi::char_t> in(&buffer);
pugi::xml_document doc;
CHECK(doc.load(in));
CHECK_NODE(doc, str.c_str());
}
#endif
TEST(document_load_string)
{
pugi::xml_document doc;
CHECK(doc.load(STR("<node/>")));
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_load_file)
{
pugi::xml_document doc;
CHECK(doc.load_file("tests/data/small.xml"));
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_load_file_empty)
{
pugi::xml_document doc;
CHECK(doc.load_file("tests/data/empty.xml"));
CHECK(!doc.first_child());
}
TEST(document_load_file_large)
{
pugi::xml_document doc;
CHECK(doc.load_file("tests/data/large.xml"));
std::basic_string<pugi::char_t> str;
str += STR("<node>");
for (int i = 0; i < 10000; ++i) str += STR("<node />");
str += STR("</node>");
CHECK_NODE(doc, str.c_str());
}
TEST(document_load_file_error)
{
pugi::xml_document doc;
CHECK(doc.load_file("filedoesnotexist").status == status_file_not_found);
#ifdef _WIN32
CHECK(doc.load_file("con").status == status_io_error);
#endif
test_runner::_memory_fail_threshold = 1;
CHECK(doc.load_file("tests/data/small.xml").status == status_out_of_memory);
}
TEST(document_load_file_error_previous)
{
pugi::xml_document doc;
CHECK(doc.load(STR("<node/>")));
CHECK(doc.first_child());
CHECK(doc.load_file("filedoesnotexist").status == status_file_not_found);
CHECK(!doc.first_child());
}
TEST(document_load_file_wide_ascii)
{
pugi::xml_document doc;
CHECK(doc.load_file(L"tests/data/small.xml"));
CHECK_NODE(doc, STR("<node />"));
}
TEST_XML(document_save, "<node/>")
{
xml_writer_string writer;
doc.save(writer, STR(""), pugi::format_no_declaration | pugi::format_raw, get_native_encoding());
CHECK(writer.as_string() == STR("<node />"));
}
#ifndef PUGIXML_NO_STL
TEST_XML(document_save_stream, "<node/>")
{
std::ostringstream oss;
doc.save(oss, STR(""), pugi::format_no_declaration | pugi::format_raw);
CHECK(oss.str() == "<node />");
}
TEST_XML(document_save_stream_wide, "<node/>")
{
std::basic_ostringstream<wchar_t> oss;
doc.save(oss, STR(""), pugi::format_no_declaration | pugi::format_raw);
CHECK(oss.str() == L"<node />");
}
#endif
TEST_XML(document_save_bom, "<n/>")
{
unsigned int flags = format_no_declaration | format_raw | format_write_bom;
// specific encodings
CHECK(test_save_narrow(doc, flags, encoding_utf8, "\xef\xbb\xbf<n />", 8));
CHECK(test_save_narrow(doc, flags, encoding_utf16_be, "\xfe\xff\x00<\x00n\x00 \x00/\x00>", 12));
CHECK(test_save_narrow(doc, flags, encoding_utf16_le, "\xff\xfe<\x00n\x00 \x00/\x00>\x00", 12));
CHECK(test_save_narrow(doc, flags, encoding_utf32_be, "\x00\x00\xfe\xff\x00\x00\x00<\x00\x00\x00n\x00\x00\x00 \x00\x00\x00/\x00\x00\x00>", 24));
CHECK(test_save_narrow(doc, flags, encoding_utf32_le, "\xff\xfe\x00\x00<\x00\x00\x00n\x00\x00\x00 \x00\x00\x00/\x00\x00\x00>\x00\x00\x00", 24));
CHECK(test_save_narrow(doc, flags, encoding_latin1, "<n />", 5));
// encodings synonyms
CHECK(save_narrow(doc, flags, encoding_utf16) == save_narrow(doc, flags, (is_little_endian() ? encoding_utf16_le : encoding_utf16_be)));
CHECK(save_narrow(doc, flags, encoding_utf32) == save_narrow(doc, flags, (is_little_endian() ? encoding_utf32_le : encoding_utf32_be)));
size_t wcharsize = sizeof(wchar_t);
CHECK(save_narrow(doc, flags, encoding_wchar) == save_narrow(doc, flags, (wcharsize == 2 ? encoding_utf16 : encoding_utf32)));
}
TEST_XML(document_save_declaration, "<node/>")
{
xml_writer_string writer;
doc.save(writer, STR(""), pugi::format_default, get_native_encoding());
CHECK(writer.as_string() == STR("<?xml version=\"1.0\"?>\n<node />\n"));
}
TEST(document_save_declaration_empty)
{
xml_document doc;
xml_writer_string writer;
doc.save(writer, STR(""), pugi::format_default, get_native_encoding());
CHECK(writer.as_string() == STR("<?xml version=\"1.0\"?>\n"));
}
TEST_XML(document_save_declaration_present_first, "<node/>")
{
doc.insert_child_before(node_declaration, doc.first_child()).append_attribute(STR("encoding")) = STR("utf8");
xml_writer_string writer;
doc.save(writer, STR(""), pugi::format_default, get_native_encoding());
CHECK(writer.as_string() == STR("<?xml encoding=\"utf8\"?>\n<node />\n"));
}
TEST_XML(document_save_declaration_present_second, "<node/>")
{
doc.insert_child_before(node_declaration, doc.first_child()).append_attribute(STR("encoding")) = STR("utf8");
doc.insert_child_before(node_comment, doc.first_child()).set_value(STR("text"));
xml_writer_string writer;
doc.save(writer, STR(""), pugi::format_default, get_native_encoding());
CHECK(writer.as_string() == STR("<!--text-->\n<?xml encoding=\"utf8\"?>\n<node />\n"));
}
TEST_XML(document_save_declaration_present_last, "<node/>")
{
doc.append_child(node_declaration).append_attribute(STR("encoding")) = STR("utf8");
xml_writer_string writer;
doc.save(writer, STR(""), pugi::format_default, get_native_encoding());
// node writer only looks for declaration before the first element child
CHECK(writer.as_string() == STR("<?xml version=\"1.0\"?>\n<node />\n<?xml encoding=\"utf8\"?>\n"));
}
TEST_XML(document_save_declaration_latin1, "<node/>")
{
xml_writer_string writer;
doc.save(writer, STR(""), pugi::format_default, encoding_latin1);
CHECK(writer.as_narrow() == "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<node />\n");
}
struct temp_file
{
char path[512];
int fd;
temp_file(): fd(0)
{
#ifdef __unix
strcpy(path, "/tmp/pugiXXXXXX");
fd = mkstemp(path);
CHECK(fd != -1);
#elif defined(__CELLOS_LV2__)
path[0] = 0; // no temporary file support
#else
tmpnam(path);
#endif
}
~temp_file()
{
CHECK(unlink(path) == 0);
#ifdef __unix
CHECK(close(fd) == 0);
#endif
}
};
TEST_XML(document_save_file, "<node/>")
{
temp_file f;
CHECK(doc.save_file(f.path));
CHECK(doc.load_file(f.path, pugi::parse_default | pugi::parse_declaration));
CHECK_NODE(doc, STR("<?xml version=\"1.0\"?><node />"));
}
TEST_XML(document_save_file_wide, "<node/>")
{
temp_file f;
// widen the path
wchar_t wpath[32];
std::copy(f.path, f.path + strlen(f.path) + 1, wpath + 0);
CHECK(doc.save_file(wpath));
CHECK(doc.load_file(f.path, pugi::parse_default | pugi::parse_declaration));
CHECK_NODE(doc, STR("<?xml version=\"1.0\"?><node />"));
}
TEST_XML(document_save_file_error, "<node/>")
{
CHECK(!doc.save_file("tests/data/unknown/output.xml"));
}
TEST(document_load_buffer)
{
const pugi::char_t text[] = STR("<?xml?><node/>");
pugi::xml_document doc;
CHECK(doc.load_buffer(text, sizeof(text)));
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_load_buffer_inplace)
{
pugi::char_t text[] = STR("<?xml?><node/>");
pugi::xml_document doc;
CHECK(doc.load_buffer_inplace(text, sizeof(text)));
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_load_buffer_inplace_own)
{
allocation_function alloc = get_memory_allocation_function();
size_t size = strlen("<?xml?><node/>") * sizeof(pugi::char_t);
pugi::char_t* text = static_cast<pugi::char_t*>(alloc(size));
CHECK(text);
memcpy(text, STR("<?xml?><node/>"), size);
pugi::xml_document doc;
CHECK(doc.load_buffer_inplace_own(text, size));
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_parse_result_bool)
{
xml_parse_result result;
result.status = status_ok;
CHECK(result);
CHECK(!!result);
CHECK(result == true);
for (int i = 1; i < 20; ++i)
{
result.status = static_cast<xml_parse_status>(i);
CHECK(!result);
CHECK(result == false);
}
}
TEST(document_parse_result_description)
{
xml_parse_result result;
for (int i = 0; i < 20; ++i)
{
result.status = static_cast<xml_parse_status>(i);
CHECK(result.description() != 0);
CHECK(result.description()[0] != 0);
}
}
TEST(document_load_fail)
{
xml_document doc;
CHECK(!doc.load(STR("<foo><bar/>")));
CHECK(doc.child(STR("foo")).child(STR("bar")));
}
inline void check_utftest_document(const xml_document& doc)
{
// ascii text
CHECK_STRING(doc.last_child().first_child().name(), STR("English"));
// check that we have parsed some non-ascii text
CHECK(static_cast<unsigned int>(doc.last_child().last_child().name()[0]) >= 0x80);
// check magic string
const pugi::char_t* v = doc.last_child().child(STR("Heavy")).previous_sibling().child_value();
#ifdef PUGIXML_WCHAR_MODE
CHECK(v[0] == 0x4e16 && v[1] == 0x754c && v[2] == 0x6709 && v[3] == 0x5f88 && v[4] == 0x591a && v[5] == 0x8bed && v[6] == 0x8a00);
// last character is a surrogate pair
unsigned int v7 = v[7];
size_t wcharsize = sizeof(wchar_t);
CHECK(wcharsize == 2 ? (v[7] == 0xd852 && v[8] == 0xdf62) : (v7 == 0x24b62));
#else
// unicode string
CHECK_STRING(v, "\xe4\xb8\x96\xe7\x95\x8c\xe6\x9c\x89\xe5\xbe\x88\xe5\xa4\x9a\xe8\xaf\xad\xe8\xa8\x80\xf0\xa4\xad\xa2");
#endif
}
TEST(document_load_file_convert_auto)
{
const char* files[] =
{
"tests/data/utftest_utf16_be.xml",
"tests/data/utftest_utf16_be_bom.xml",
"tests/data/utftest_utf16_be_nodecl.xml",
"tests/data/utftest_utf16_le.xml",
"tests/data/utftest_utf16_le_bom.xml",
"tests/data/utftest_utf16_le_nodecl.xml",
"tests/data/utftest_utf32_be.xml",
"tests/data/utftest_utf32_be_bom.xml",
"tests/data/utftest_utf32_be_nodecl.xml",
"tests/data/utftest_utf32_le.xml",
"tests/data/utftest_utf32_le_bom.xml",
"tests/data/utftest_utf32_le_nodecl.xml",
"tests/data/utftest_utf8.xml",
"tests/data/utftest_utf8_bom.xml",
"tests/data/utftest_utf8_nodecl.xml"
};
xml_encoding encodings[] =
{
encoding_utf16_be, encoding_utf16_be, encoding_utf16_be,
encoding_utf16_le, encoding_utf16_le, encoding_utf16_le,
encoding_utf32_be, encoding_utf32_be, encoding_utf32_be,
encoding_utf32_le, encoding_utf32_le, encoding_utf32_le,
encoding_utf8, encoding_utf8, encoding_utf8
};
for (unsigned int i = 0; i < sizeof(files) / sizeof(files[0]); ++i)
{
xml_document doc;
xml_parse_result res = doc.load_file(files[i]);
CHECK(res);
CHECK(res.encoding == encodings[i]);
check_utftest_document(doc);
}
}
TEST(document_load_file_convert_specific)
{
const char* files[] =
{
"tests/data/utftest_utf16_be.xml",
"tests/data/utftest_utf16_be_bom.xml",
"tests/data/utftest_utf16_be_nodecl.xml",
"tests/data/utftest_utf16_le.xml",
"tests/data/utftest_utf16_le_bom.xml",
"tests/data/utftest_utf16_le_nodecl.xml",
"tests/data/utftest_utf32_be.xml",
"tests/data/utftest_utf32_be_bom.xml",
"tests/data/utftest_utf32_be_nodecl.xml",
"tests/data/utftest_utf32_le.xml",
"tests/data/utftest_utf32_le_bom.xml",
"tests/data/utftest_utf32_le_nodecl.xml",
"tests/data/utftest_utf8.xml",
"tests/data/utftest_utf8_bom.xml",
"tests/data/utftest_utf8_nodecl.xml"
};
xml_encoding encodings[] =
{
encoding_utf16_be, encoding_utf16_be, encoding_utf16_be,
encoding_utf16_le, encoding_utf16_le, encoding_utf16_le,
encoding_utf32_be, encoding_utf32_be, encoding_utf32_be,
encoding_utf32_le, encoding_utf32_le, encoding_utf32_le,
encoding_utf8, encoding_utf8, encoding_utf8
};
for (unsigned int i = 0; i < sizeof(files) / sizeof(files[0]); ++i)
{
for (unsigned int j = 0; j < sizeof(files) / sizeof(files[0]); ++j)
{
xml_encoding encoding = encodings[j];
xml_document doc;
xml_parse_result res = doc.load_file(files[i], parse_default, encoding);
if (encoding == encodings[i])
{
CHECK(res);
CHECK(res.encoding == encoding);
check_utftest_document(doc);
}
else
{
// should not get past first tag
CHECK(!doc.first_child());
}
}
}
}
TEST(document_load_file_convert_native_endianness)
{
const char* files[2][6] =
{
{
"tests/data/utftest_utf16_be.xml",
"tests/data/utftest_utf16_be_bom.xml",
"tests/data/utftest_utf16_be_nodecl.xml",
"tests/data/utftest_utf32_be.xml",
"tests/data/utftest_utf32_be_bom.xml",
"tests/data/utftest_utf32_be_nodecl.xml",
},
{
"tests/data/utftest_utf16_le.xml",
"tests/data/utftest_utf16_le_bom.xml",
"tests/data/utftest_utf16_le_nodecl.xml",
"tests/data/utftest_utf32_le.xml",
"tests/data/utftest_utf32_le_bom.xml",
"tests/data/utftest_utf32_le_nodecl.xml",
}
};
xml_encoding encodings[] =
{
encoding_utf16, encoding_utf16, encoding_utf16,
encoding_utf32, encoding_utf32, encoding_utf32
};
for (unsigned int i = 0; i < sizeof(files[0]) / sizeof(files[0][0]); ++i)
{
const char* right_file = files[is_little_endian()][i];
const char* wrong_file = files[!is_little_endian()][i];
for (unsigned int j = 0; j < sizeof(encodings) / sizeof(encodings[0]); ++j)
{
xml_encoding encoding = encodings[j];
// check file with right endianness
{
xml_document doc;
xml_parse_result res = doc.load_file(right_file, parse_default, encoding);
if (encoding == encodings[i])
{
CHECK(res);
check_utftest_document(doc);
}
else
{
// should not get past first tag
CHECK(!doc.first_child());
}
}
// check file with wrong endianness
{
xml_document doc;
doc.load_file(wrong_file, parse_default, encoding);
CHECK(!doc.first_child());
}
}
}
}
static bool load_file_in_memory(const char* path, char*& data, size_t& size)
{
FILE* file = fopen(path, "rb");
if (!file) return false;
fseek(file, 0, SEEK_END);
size = static_cast<size_t>(ftell(file));
fseek(file, 0, SEEK_SET);
data = new char[size];
CHECK(fread(data, 1, size, file) == size);
fclose(file);
return true;
}
struct file_data_t
{
const char* path;
xml_encoding encoding;
char* data;
size_t size;
};
TEST(document_contents_preserve)
{
file_data_t files[] =
{
{"tests/data/utftest_utf16_be_clean.xml", encoding_utf16_be, 0, 0},
{"tests/data/utftest_utf16_le_clean.xml", encoding_utf16_le, 0, 0},
{"tests/data/utftest_utf32_be_clean.xml", encoding_utf32_be, 0, 0},
{"tests/data/utftest_utf32_le_clean.xml", encoding_utf32_le, 0, 0},
{"tests/data/utftest_utf8_clean.xml", encoding_utf8, 0, 0}
};
// load files in memory
for (unsigned int i = 0; i < sizeof(files) / sizeof(files[0]); ++i)
{
CHECK(load_file_in_memory(files[i].path, files[i].data, files[i].size));
}
// convert each file to each format and compare bitwise
for (unsigned int src = 0; src < sizeof(files) / sizeof(files[0]); ++src)
{
for (unsigned int dst = 0; dst < sizeof(files) / sizeof(files[0]); ++dst)
{
// parse into document (preserve comments, declaration and whitespace pcdata)
xml_document doc;
CHECK(doc.load_buffer(files[src].data, files[src].size, parse_default | parse_ws_pcdata | parse_declaration | parse_comments));
// compare saved document with the original (raw formatting, without extra declaration, write bom if it was in original file)
CHECK(test_save_narrow(doc, format_raw | format_no_declaration | format_write_bom, files[dst].encoding, files[dst].data, files[dst].size));
}
}
// cleanup
for (unsigned int j = 0; j < sizeof(files) / sizeof(files[0]); ++j)
{
delete[] files[j].data;
}
}
TEST(document_contents_preserve_latin1)
{
file_data_t files[] =
{
{"tests/data/latintest_utf8.xml", encoding_utf8, 0, 0},
{"tests/data/latintest_latin1.xml", encoding_latin1, 0, 0}
};
// load files in memory
for (unsigned int i = 0; i < sizeof(files) / sizeof(files[0]); ++i)
{
CHECK(load_file_in_memory(files[i].path, files[i].data, files[i].size));
}
// convert each file to each format and compare bitwise
for (unsigned int src = 0; src < sizeof(files) / sizeof(files[0]); ++src)
{
for (unsigned int dst = 0; dst < sizeof(files) / sizeof(files[0]); ++dst)
{
// parse into document (preserve comments, declaration and whitespace pcdata)
xml_document doc;
CHECK(doc.load_buffer(files[src].data, files[src].size, parse_default | parse_ws_pcdata | parse_declaration | parse_comments, files[src].encoding));
// compare saved document with the original (raw formatting, without extra declaration, write bom if it was in original file)
CHECK(test_save_narrow(doc, format_raw | format_no_declaration | format_write_bom, files[dst].encoding, files[dst].data, files[dst].size));
}
}
// cleanup
for (unsigned int j = 0; j < sizeof(files) / sizeof(files[0]); ++j)
{
delete[] files[j].data;
}
}
static bool test_parse_fail(const void* buffer, size_t size, xml_encoding encoding = encoding_utf8)
{
// copy buffer to heap (to enable out-of-bounds checks)
void* temp = malloc(size);
memcpy(temp, buffer, size);
// check that this parses without buffer overflows (yielding an error)
xml_document doc;
bool result = doc.load_buffer_inplace(temp, size, parse_default, encoding);
free(temp);
return !result;
}
TEST(document_convert_invalid_utf8)
{
// invalid 1-byte input
CHECK(test_parse_fail("<\xb0", 2));
// invalid 2-byte input
CHECK(test_parse_fail("<\xc0", 2));
CHECK(test_parse_fail("<\xd0", 2));
// invalid 3-byte input
CHECK(test_parse_fail("<\xe2\x80", 3));
CHECK(test_parse_fail("<\xe2", 2));
// invalid 4-byte input
CHECK(test_parse_fail("<\xf2\x97\x98", 4));
CHECK(test_parse_fail("<\xf2\x97", 3));
CHECK(test_parse_fail("<\xf2", 2));
// invalid 5-byte input
CHECK(test_parse_fail("<\xf8", 2));
}
TEST(document_convert_invalid_utf16)
{
// check non-terminated degenerate handling
CHECK(test_parse_fail("\x00<\xda\x1d", 4, encoding_utf16_be));
CHECK(test_parse_fail("<\x00\x1d\xda", 4, encoding_utf16_le));
// check incorrect leading code
CHECK(test_parse_fail("\x00<\xde\x24", 4, encoding_utf16_be));
CHECK(test_parse_fail("<\x00\x24\xde", 4, encoding_utf16_le));
}
TEST(document_load_buffer_empty)
{
xml_encoding encodings[] =
{
encoding_auto,
encoding_utf8,
encoding_utf16_le,
encoding_utf16_be,
encoding_utf16,
encoding_utf32_le,
encoding_utf32_be,
encoding_utf32,
encoding_wchar,
encoding_latin1
};
char buffer[1];
for (unsigned int i = 0; i < sizeof(encodings) / sizeof(encodings[0]); ++i)
{
xml_encoding encoding = encodings[i];
xml_document doc;
CHECK(doc.load_buffer(buffer, 0, parse_default, encoding) && !doc.first_child());
CHECK(doc.load_buffer(0, 0, parse_default, encoding) && !doc.first_child());
CHECK(doc.load_buffer_inplace(buffer, 0, parse_default, encoding) && !doc.first_child());
CHECK(doc.load_buffer_inplace(0, 0, parse_default, encoding) && !doc.first_child());
void* own_buffer = pugi::get_memory_allocation_function()(1);
CHECK(doc.load_buffer_inplace_own(own_buffer, 0, parse_default, encoding) && !doc.first_child());
CHECK(doc.load_buffer_inplace_own(0, 0, parse_default, encoding) && !doc.first_child());
}
}
TEST(document_progressive_truncation)
{
char* original_data;
size_t original_size;
CHECK(load_file_in_memory("tests/data/truncation.xml", original_data, original_size));
char* buffer = new char[original_size];
for (size_t i = 1; i < original_size; ++i)
{
char* truncated_data = buffer + original_size - i;
memcpy(truncated_data, original_data, i);
xml_document doc;
bool result = doc.load_buffer_inplace(truncated_data, i);
// some truncate locations are parseable - those that come after declaration, declaration + doctype, declaration + doctype + comment and eof
CHECK(((i - 21) < 3 || (i - 66) < 3 || (i - 95) < 3 || i >= 3325) ? result : !result);
}
delete[] buffer;
delete[] original_data;
}
TEST(document_load_buffer_short)
{
char* data = new char[4];
memcpy(data, "abcd", 4);
xml_document doc;
CHECK(doc.load_buffer(data, 4));
CHECK(doc.load_buffer(data + 1, 3));
CHECK(doc.load_buffer(data + 2, 2));
CHECK(doc.load_buffer(data + 3, 1));
CHECK(doc.load_buffer(data + 4, 0));
CHECK(doc.load_buffer(0, 0));
delete[] data;
}
TEST(document_load_buffer_inplace_short)
{
char* data = new char[4];
memcpy(data, "abcd", 4);
xml_document doc;
CHECK(doc.load_buffer_inplace(data, 4));
CHECK(doc.load_buffer_inplace(data + 1, 3));
CHECK(doc.load_buffer_inplace(data + 2, 2));
CHECK(doc.load_buffer_inplace(data + 3, 1));
CHECK(doc.load_buffer_inplace(data + 4, 0));
CHECK(doc.load_buffer_inplace(0, 0));
delete[] data;
}
#ifndef PUGIXML_NO_EXCEPTIONS
TEST(document_load_exceptions)
{
bool thrown = false;
try
{
pugi::xml_document doc;
if (!doc.load(STR("<node attribute='value"))) throw std::bad_alloc();
CHECK_FORCE_FAIL("Expected parsing failure");
}
catch (const std::bad_alloc&)
{
thrown = true;
}
CHECK(thrown);
}
#endif
TEST_XML_FLAGS(document_element, "<?xml version='1.0'?><node><child/></node><!---->", parse_default | parse_declaration | parse_comments)
{
CHECK(doc.document_element() == doc.child(STR("node")));
}
TEST_XML_FLAGS(document_element_absent, "<!---->", parse_comments)
{
CHECK(doc.document_element() == xml_node());
}
TEST_XML(document_reset, "<node><child/></node>")
{
CHECK(doc.first_child());
doc.reset();
CHECK(!doc.first_child());
CHECK_NODE(doc, STR(""));
doc.reset();
CHECK(!doc.first_child());
CHECK_NODE(doc, STR(""));
CHECK(doc.load(STR("<node/>")));
CHECK(doc.first_child());
CHECK_NODE(doc, STR("<node />"));
doc.reset();
CHECK(!doc.first_child());
CHECK_NODE(doc, STR(""));
}
TEST(document_reset_empty)
{
xml_document doc;
doc.reset();
CHECK(!doc.first_child());
CHECK_NODE(doc, STR(""));
}
TEST_XML(document_reset_copy, "<node><child/></node>")
{
xml_document doc2;
CHECK_NODE(doc2, STR(""));
doc2.reset(doc);
CHECK_NODE(doc2, STR("<node><child /></node>"));
CHECK(doc.first_child() != doc2.first_child());
doc.reset(doc2);
CHECK_NODE(doc, STR("<node><child /></node>"));
CHECK(doc.first_child() != doc2.first_child());
CHECK(doc.first_child().offset_debug() == -1);
}
TEST_XML(document_reset_copy_self, "<node><child/></node>")
{
CHECK_NODE(doc, STR("<node><child /></node>"));
doc.reset(doc);
CHECK(!doc.first_child());
CHECK_NODE(doc, STR(""));
}
|