Debian Codegraph @

t/40reader_mem_error.t

raw
1 #!/usr/bin/perl
2
3 # This code used to generate a memory error in valgrind/etc.
4 # Testing it.
5
6 use strict;
7 use warnings;
8
9 use Test::More;
10
11 use utf8;
12
13 use XML::LibXML;
14
15 BEGIN {
16 if (!XML::LibXML::HAVE_READER()) {
17 plan skip_all => 'Reader not supported in this libxml2 build';
18 exit;
19 }
20 else {
21 plan tests => 2;
22 }
23 }
24
25 package Test::XML::Ordered;
26
27 use XML::LibXML::Reader;
28
29 use Test::More;
30
31 use parent 'Exporter';
32
33 use vars '@EXPORT_OK';
34
35 @EXPORT_OK = (qw(is_xml_ordered));
36
37 sub new
38 {
39 my $class = shift;
40 my $self = {};
41
42 bless $self, $class;
43
44 $self->_init(@_);
45
46 return $self;
47 }
48
49 sub _got
50 {
51 return shift->{got_reader};
52 }
53
54 sub _expected
55 {
56 return shift->{expected_reader};
57 }
58
59 sub _init
60 {
61 my ($self, $args) = @_;
62
63 $self->{got_reader} =
64 XML::LibXML::Reader->new(@{$args->{got_params}});
65 $self->{expected_reader} =
66 XML::LibXML::Reader->new(@{$args->{expected_params}});
67
68 $self->{diag_message} = $args->{diag_message};
69
70 $self->{got_end} = 0;
71 $self->{expected_end} = 0;
72
73 return;
74 }
75
76 sub _got_end
77 {
78 return shift->{got_end};
79 }
80
81 sub _expected_end
82 {
83 return shift->{expected_end};
84 }
85
86 sub _read_got
87 {
88 my $self = shift;
89
90 if ($self->_got->read() <= 0)
91 {
92 $self->{got_end} = 1;
93 }
94
95 return;
96 }
97
98 sub _read_expected
99 {
100 my $self = shift;
101
102 if ($self->_expected->read() <= 0)
103 {
104 $self->{expected_end} = 1;
105 }
106
107 return;
108 }
109
110 sub _next_elem
111 {
112 my $self = shift;
113
114 $self->_read_got();
115 $self->_read_expected();
116
117 return;
118 }
119
120 sub _ns
121 {
122 my $elem = shift;
123 my $ns = $elem->namespaceURI();
124
125 return defined($ns) ? $ns : "";
126 }
127
128 sub _compare_loop
129 {
130 my $self = shift;
131
132 my $calc_prob = sub {
133 my $args = shift;
134
135 if (!exists($args->{param}))
136 {
137 die "No 'param' specified.";
138 }
139 return
140 {
141 verdict => 0,
142 param => $args->{param},
143 }
144 };
145
146 NODE_LOOP:
147 while ((!$self->_got_end()) && (!$self->_expected_end()))
148 {
149 my $type = $self->_got->nodeType();
150 my $exp_type = $self->_expected->nodeType();
151
152 if ($type == XML_READER_TYPE_SIGNIFICANT_WHITESPACE())
153 {
154 $self->_read_got();
155 redo NODE_LOOP;
156 }
157 elsif ($exp_type == XML_READER_TYPE_SIGNIFICANT_WHITESPACE())
158 {
159 $self->_read_expected();
160 redo NODE_LOOP;
161 }
162 elsif ($type != $exp_type)
163 {
164 return $calc_prob->({param => "nodeType"});
165 }
166 elsif ($type == XML_READER_TYPE_TEXT())
167 {
168 my $got_text = $self->_got->value();
169 my $expected_text = $self->_expected->value();
170
171 foreach my $t ($got_text, $expected_text)
172 {
173 $t =~ s{\A\s+}{}ms;
174 $t =~ s{\s+\z}{}ms;
175 $t =~ s{\s+}{ }ms;
176 }
177 if ($got_text ne $expected_text)
178 {
179 return $calc_prob->({param => "text"});
180 }
181 }
182 elsif ($type == XML_READER_TYPE_ELEMENT())
183 {
184 if ($self->_got->name() ne $self->_expected->name())
185 {
186 return $calc_prob->({param => "element_name"});
187 }
188 if (_ns($self->_got) ne _ns($self->_expected))
189 {
190 return $calc_prob->({param => "mismatch_ns"});
191 }
192 }
193 }
194 continue
195 {
196 $self->_next_elem();
197 }
198
199 return { verdict => 1};
200 }
201
202 sub _get_diag_message
203 {
204 my ($self, $status_struct) = @_;
205
206 if ($status_struct->{param} eq "nodeType")
207 {
208 return
209 "Different Node Type!\n"
210 . "Got: " . $self->_got->nodeType() . " at line " . $self->_got->lineNumber()
211 . "\n"
212 . "Expected: " . $self->_expected->nodeType() . " at line " . $self->_expected->lineNumber()
213 ;
214 }
215 elsif ($status_struct->{param} eq "text")
216 {
217 return
218 "Texts differ: Got at " . $self->_got->lineNumber(). " with value <<@{[$self->_got->value()]}>> ; Expected at ". $self->_expected->lineNumber() . " with value <<@{[$self->_expected->value()]}>>.";
219 }
220 elsif ($status_struct->{param} eq "element_name")
221 {
222 return
223 "Got name: " . $self->_got->name(). " at " . $self->_got->lineNumber() .
224 " ; " .
225 "Expected name: " . $self->_expected->name() . " at " .$self->_expected->lineNumber();
226 }
227 elsif ($status_struct->{param} eq "mismatch_ns")
228 {
229 return
230 "Got Namespace: " . _ns($self->_got). " at " . $self->_got->lineNumber() .
231 " ; " .
232 "Expected Namespace: " . _ns($self->_expected) . " at " .$self->_expected->lineNumber();
233 }
234
235 else
236 {
237 die "Unknown param";
238 }
239 }
240
241 sub compare
242 {
243 local $Test::Builder::Level = $Test::Builder::Level+1;
244
245 my $self = shift;
246
247 $self->_next_elem();
248
249 my $status_struct = $self->_compare_loop();
250 my $verdict = $status_struct->{verdict};
251
252 if (!$verdict)
253 {
254 diag($self->_get_diag_message($status_struct));
255 }
256
257 return ok($verdict, $self->{diag_message});
258 }
259
260 sub is_xml_ordered
261 {
262 local $Test::Builder::Level = $Test::Builder::Level+1;
263
264 my ($got_params, $expected_params, $message) = @_;
265
266 my $comparator =
267 Test::XML::Ordered->new(
268 {
269 got_params => $got_params,
270 expected_params => $expected_params,
271 diag_message => $message,
272 }
273 );
274
275 return $comparator->compare();
276 }
277
278 my $xml_source = <<'EOF';
279 <?xml version="1.0" encoding="UTF-8"?>
280 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
281 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fic="http://web-cpan.berlios.de/modules/XML-Grammar-Fortune/fiction-xml-0.2/">
282 <head>
283 <title>David vs. Goliath - Part I</title>
284 </head>
285 <body>
286 <div class="fiction story" xml:id="index">
287 <h1>David vs. Goliath - Part I</h1>
288 <div class="fiction section" xml:id="top">
289 <h2>The Top Section</h2>
290 <p>
291 King David and Goliath were standing by each other.
292 </p>
293 <p>
294 David said unto Goliath: "I will shoot you. I <b>swear</b> I will"
295 </p>
296 <div class="fiction section" xml:id="goliath">
297 <h3>Goliath's Response</h3>
298 <p>
299 Goliath was not amused.
300 </p>
301 <p>
302 He said to David: "Oh, really. <i>David</i>, the red-headed!".
303 </p>
304 <p>
305 David started listing Goliath's disadvantages:
306 </p>
307 </div>
308 </div>
309 </div>
310 </body>
311 </html>
312 EOF
313
314 my $final_source = <<'EOF';
315 <?xml version="1.0" encoding="UTF-8"?>
316 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
317 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fic="http://web-cpan.berlios.de/modules/XML-Grammar-Fortune/fiction-xml-0.2/">
318 <head>
319 <title>David vs. Goliath - Part I</title>
320 </head>
321 <body>
322 <div class="fiction story" xml:id="index">
323 <h1>David vs. Goliath - Part I</h1>
324 <div class="fiction section" xml:id="top">
325 <h2>The Top Section</h2>
326 <p>
327 King David and Goliath were standing by each other.
328 </p>
329 <p>
330 David said unto Goliath: "I will shoot you. I <b>swear</b> I will"
331 </p>
332 <div class="fiction section" xml:id="goliath">
333 <h3>Goliath's Response</h3>
334 <p>
335 Goliath was not amused.
336 </p>
337 <p>
338 He said to David: "Oh, really. <i>David</i>, the red-headed!".
339 </p>
340 <p>
341 David started listing Goliath's disadvantages:
342 </p>
343 </div>
344 </div>
345 </div>
346 </body>
347 </html>
348 EOF
349
350 SKIP: {
351 # RT #84564
352 # https://bugzilla.gnome.org/show_bug.cgi?id=447899
353 if (XML::LibXML::LIBXML_RUNTIME_VERSION() < 20704) {
354 skip('Known double-free with libxml2 < 2.7.4', 1);
355 }
356
357 my @common = (validation => 0, load_ext_dtd => 0, no_network => 1);
358 # TEST
359 Test::XML::Ordered::is_xml_ordered(
360 [ string => $final_source, @common,],
361 [ string => $xml_source, @common,],
362 "foo",
363 );
364 }
365
366 # TEST
367 ok (1, "Finished");