Debian Codegraph @

t/20-x2h-incremental-boundaries.t

raw
1 use strict;
2 use warnings;
3 use utf8;
4
5 use Encode qw(encode);
6 use Test::More 0.98;
7 use XML::Hash::XS qw(xml2hash);
8
9 ok(!XML::Hash::XS->can('new_parser'), 'legacy converter factory is not exposed');
10
11 my @documents = (
12 [
13 'markup boundaries',
14 '<!DOCTYPE root SYSTEM "test.dtd">' .
15 '<?xml-stylesheet href="default.css" title="Default"?>' .
16 '<root><!-- comment --><item name="a&amp;b"><![CDATA[x<y && z>q]]></item></root>',
17 ],
18 [
19 'single quoted attribute and references',
20 q{<root><item first='one &quot; two' second="three &apos; four">&#65;&#x42;</item></root>},
21 ],
22 [
23 'UTF-8 byte boundaries',
24 encode('UTF-8', qq{<?xml version="1.0" encoding="UTF-8"?><root name="тест">Привет 😀</root>}),
25 ],
26 );
27
28 for my $case (@documents) {
29 my ($name, $xml) = @$case;
30 my $expected = xml2hash($xml, keep_root => 1);
31
32 subtest $name => sub {
33 for my $at (0 .. length $xml) {
34 my $parser = XML::Hash::XS::Parser->new(keep_root => 1);
35 $parser->feed(substr($xml, 0, $at));
36 $parser->feed('');
37 $parser->feed(substr($xml, $at));
38 is_deeply($parser->finish, $expected, "split at byte $at");
39 }
40 };
41 }
42
43 {
44 my $chunk = '<root><completed>value</completed><ite';
45 my $parser = XML::Hash::XS::Parser->new(keep_root => 1);
46 $parser->feed($chunk);
47
48 # The parser must own the unfinished suffix after feed() returns.
49 substr($chunk, 0, length($chunk), 'x' x length($chunk));
50 $parser->feed('m attr="ok">tail</item></root>');
51
52 is_deeply(
53 $parser->finish,
54 { root => { completed => 'value', item => { attr => 'ok', content => 'tail' } } },
55 'unfinished suffix survives mutation of the source scalar',
56 );
57 }
58
59 {
60 my $value = '';
61 my $parser = XML::Hash::XS::Parser->new(keep_root => 1);
62 $parser->feed('<root><item attr="');
63
64 # Keep the attribute unfinished while the retained buffer repeatedly grows.
65 for my $size (1, 3, 17, 129, 1025, 8193, 32769) {
66 my $part = 'x' x $size;
67 $value .= $part;
68 $parser->feed($part);
69 }
70 $parser->feed('">ok</item></root>');
71
72 is_deeply(
73 $parser->finish,
74 { root => { item => { attr => $value, content => 'ok' } } },
75 'retained suffix remains valid across buffer reallocations',
76 );
77 }
78
79 {
80 for my $iteration (1 .. 2000) {
81 my $parser = XML::Hash::XS::Parser->new;
82 $parser->feed('<root><item attr="' . ('x' x ($iteration % 257)));
83 undef $parser;
84 }
85 pass('unfinished retained buffers can be repeatedly allocated and freed');
86 }
87
88 {
89 my $error = eval { XML::Hash::XS::Parser->new->finish; 1 } ? '' : $@;
90 like($error, qr/Invalid XML/, 'temporary parser remains alive while finish propagates an error');
91 }
92
93 {
94 for my $iteration (1 .. 500) {
95 my $parser = XML::Hash::XS::Parser->new;
96 $parser->feed('<root><item>');
97 eval { $parser->finish };
98 undef $parser;
99 }
100 pass('error-state parsers release retained buffers safely');
101 }
102
103 done_testing;