summaryrefslogtreecommitdiff
path: root/t/astress.t
blob: 210760b0006ea7d7d841ee3022d23d3efd9a83a7 (plain)
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
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

######################### We start with some black magic to print on failure.

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN {print "1..27\n";}
END {print "not ok 1\n" unless $loaded;}
use XML::Parser;
$loaded = 1;
print "ok 1\n";

######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):

# Test 2


my $parser = new XML::Parser(ProtocolEncoding => 'ISO-8859-1');
if ($parser)
{
    print "ok 2\n";
}
else
{
    print "not ok 2\n";
    exit;
}

my @ndxstack;
my $indexok = 1;

# Need this external entity

open(ZOE, '>zoe.ent');
print ZOE "'cute'";
close(ZOE);

# XML string for tests

my $xmlstring =<<"End_of_XML;";
<!DOCTYPE foo
  [
    <!NOTATION bar PUBLIC "qrs">
    <!ENTITY zinger PUBLIC "xyz" "abc" NDATA bar>
    <!ENTITY fran SYSTEM "fran-def">
    <!ENTITY zoe  SYSTEM "zoe.ent">
   ]>
<foo>
  First line in foo
  <boom>Fran is &fran; and Zoe is &zoe;</boom>
  <bar id="jack" stomp="jill">
  <?line-noise *&*&^&<< ?>
    1st line in bar
    <blah> 2nd line in bar </blah>
    3rd line in bar <!-- Isn't this a doozy -->
  </bar>
  <zap ref="zing" />
  This, '\240', would be a bad character in UTF-8.
</foo>
End_of_XML;

# Handlers
my @tests;
my $pos ='';

sub ch
{
    my ($p, $str) = @_;
    $tests[4]++;
    $tests[5]++ if ($str =~ /2nd line/ and $p->in_element('blah'));
    if ($p->in_element('boom'))
    {
	$tests[17]++ if $str =~ /pretty/;
	$tests[18]++ if $str =~ /cute/;
    }
}

sub st
{
    my ($p, $el, %atts) = @_;

    $ndxstack[$p->depth] = $p->element_index;
    $tests[6]++ if ($el eq 'bar' and $atts{stomp} eq 'jill');
    if ($el eq 'zap' and $atts{'ref'} eq 'zing')
    {
	$tests[7]++;
	$p->default_current;
    }
    elsif ($el eq 'bar') {
      $tests[22]++ if $p->recognized_string eq '<bar id="jack" stomp="jill">';
    }
}

sub eh
{
    my ($p, $el) = @_;
    $indexok = 0 unless $p->element_index == $ndxstack[$p->depth];
    if ($el eq 'zap')
    {
	$tests[8]++;
	my @old = $p->setHandlers('Char', \&newch);
	$tests[19]++ if $p->current_line == 17;
	$tests[20]++ if $p->current_column == 20;
	$tests[23]++ if ($old[0] eq 'Char' and $old[1] == \&ch);
    }
    if ($el eq 'boom')
    {
	$p->setHandlers('Default', \&dh);
    }
}

sub dh
{
    my ($p, $str) = @_;
    if ($str =~ /doozy/)
    {
	$tests[9]++;
	$pos = $p->position_in_context(1);
    }
    $tests[10]++ if $str =~ /^<zap/;
}

sub pi
{
    my ($p, $tar, $data) = @_;

    $tests[11]++ if ($tar eq 'line-noise' and $data =~ /&\^&<</);
}

sub note
{
    my ($p, $name, $base, $sysid, $pubid) = @_;

    $tests[12]++ if ($name eq 'bar' and $pubid eq 'qrs');
}

sub unp
{
    my ($p, $name, $base, $sysid, $pubid, $notation) = @_;

    $tests[13]++ if ($name eq 'zinger' and $pubid eq 'xyz'
		     and $sysid eq 'abc' and $notation eq 'bar');
}

sub newch
{
    my ($p, $str) = @_;

    if ($] < 5.007001) {
	$tests[14]++ if $str =~ /'\302\240'/;
    }
    else {
	$tests[14]++ if $str =~ /'\xa0'/;
    }
}

sub extent
{
    my ($p, $base, $sys, $pub) = @_;

    if ($sys eq 'fran-def')
    {
	$tests[15]++;
	return 'pretty';
    }
    elsif ($sys eq 'zoe.ent')
    {
	$tests[16]++;

	open(FOO, $sys) or die "Couldn't open $sys";
	return *FOO;
    }
}

eval {
    $parser->setHandlers('Char'  => \&ch,
			 'Start' => \&st,
			 'End'   => \&eh,
			 'Proc'  => \&pi,
			 'Notation' => \&note,
			 'Unparsed' => \&unp,
			 'ExternEnt' => \&extent,
			 'ExternEntFin' => sub {close(FOO);}
			);
};

if ($@)
{
    print "not ok 3\n";
    exit;
}

print "ok 3\n";

# Test 4..20
eval {
    $parser->parsestring($xmlstring);
};

if ($@)
{
    print "Parse error:\n$@";
}
else
{
    $tests[21]++;
}

unlink('zoe.ent') if (-f 'zoe.ent');

for (4 .. 23)
{
    print "not " unless $tests[$_];
    print "ok $_\n";
}

$cmpstr =<< 'End_of_Cmp;';
    <blah> 2nd line in bar </blah>
    3rd line in bar <!-- Isn't this a doozy -->
===================^
  </bar>
End_of_Cmp;

if ($cmpstr ne $pos)
{
    print "not ";
}
print "ok 24\n";

print "not " unless $indexok;
print "ok 25\n";


# Test that memory leak through autovivifying symbol table entries is fixed.

my $count = 0;
$parser = new XML::Parser(
  Handlers => { 
    Start => sub { $count++ }
  }
);

$xmlstring = '<a><b>Sea</b></a>';

eval {
    $parser->parsestring($xmlstring);
};

if($count != 2) {
  print "not ";
}
print "ok 26\n";

if(defined(*{$xmlstring})) {
  print "not ";
}
print "ok 27\n";