[ID 20020609.007] Not OK: perl v5.8.0 +DEVEL17060 on i686-linux 2.4.18-4smp (UNINSTALLED)
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / malice.t
CommitLineData
b8778c7c 1#!./perl -w
b8778c7c 2#
3# Copyright 2002, Larry Wall.
4#
5# You may redistribute only under the same terms as Perl 5, as specified
6# in the README file that comes with the distribution.
7#
8
9# I'm trying to keep this test easily backwards compatible to 5.004, so no
10# qr//;
b8778c7c 11
12# This test tries to craft malicious data to test out as many different
13# error traps in Storable as possible
14# It also acts as a test for read_header
15
16sub BEGIN {
17 if ($ENV{PERL_CORE}){
18 chdir('t') if -d 't';
372cb964 19 @INC = ('.', '../lib');
b8778c7c 20 }
21 require Config; import Config;
22 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
23 print "1..0 # Skip: Storable was not built\n";
24 exit 0;
25 }
b8778c7c 26}
27
28use strict;
677a847b 29use vars qw($file_magic_str $other_magic $network_magic $byteorder
30 $major $minor $minor_write $fancy);
31
32$byteorder = $Config{byteorder};
33
96ef0061 34$file_magic_str = 'pst0';
677a847b 35$other_magic = 7 + length $byteorder;
96ef0061 36$network_magic = 2;
37$major = 2;
38$minor = 5;
530b72ba 39$minor_write = $] > 5.007 ? 5 : 4;
b8778c7c 40
372cb964 41use Test::More;
42
43# If it's 5.7.3 or later the hash will be stored with flags, which is
44# 2 extra bytes. There are 2 * 2 * 2 tests per byte in the body and header
45# common to normal and network order serialised objects (hence the 8)
46# There are only 2 * 2 tests per byte in the parts of the header not present
47# for network order, and 2 tests per byte on the 'pst0' "magic number" only
48# present in files, but not in things store()ed to memory
49$fancy = ($] > 5.007 ? 2 : 0);
50
677a847b 51plan tests => 368 + length ($byteorder) * 4 + $fancy * 8;
b8778c7c 52
53use Storable qw (store retrieve freeze thaw nstore nfreeze);
54
55my $file = "malice.$$";
56die "Temporary file 'malice.$$' already exists" if -e $file;
57
58END { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }}
59
9d80fab7 60# The chr 256 is a hack to force the hash to always have the utf8 keys flag
61# set on 5.7.3 and later. Otherwise the test fails if run with -Mutf8 because
62# only there does the hash has the flag on, and hence only there is it stored
63# as a flagged hash, which is 2 bytes longer
64my %hash = (perl => 'rules', chr 256, '');
65delete $hash{chr 256};
b8778c7c 66
67sub test_hash {
68 my $clone = shift;
372cb964 69 is (ref $clone, "HASH", "Get hash back");
70 is (scalar keys %$clone, 1, "with 1 key");
71 is ((keys %$clone)[0], "perl", "which is correct");
72 is ($clone->{perl}, "rules");
b8778c7c 73}
74
75sub test_header {
76 my ($header, $isfile, $isnetorder) = @_;
372cb964 77 is (!!$header->{file}, !!$isfile, "is file");
78 is ($header->{major}, $major, "major number");
79 is ($header->{minor}, $minor_write, "minor number");
80 is (!!$header->{netorder}, !!$isnetorder, "is network order");
677a847b 81 if ($isnetorder) {
82 # Network order header has no sizes
83 } else {
84 is ($header->{byteorder}, $byteorder, "byte order");
372cb964 85 is ($header->{intsize}, $Config{intsize}, "int size");
86 is ($header->{longsize}, $Config{longsize}, "long size");
87 is ($header->{ptrsize}, $Config{ptrsize}, "long size");
88 is ($header->{nvsize}, $Config{nvsize} || $Config{doublesize} || 8,
b8778c7c 89 "nv size"); # 5.00405 doesn't even have doublesize in config.
90 }
91}
92
93sub store_and_retrieve {
94 my $data = shift;
95 unlink $file or die "Can't unlink '$file': $!";
96 open FH, ">$file" or die "Can't open '$file': $!";
97 binmode FH;
6e3dd797 98 eval { binmode(FH, ":bytes") }; # Perl 5.8.0+ only
b8778c7c 99 print FH $data or die "Can't print to '$file': $!";
100 close FH or die "Can't close '$file': $!";
101
102 return eval {retrieve $file};
103}
104
105sub freeze_and_thaw {
106 my $data = shift;
107 return eval {thaw $data};
108}
109
110sub test_truncated {
111 my ($data, $sub, $magic_len, $what) = @_;
112 for my $i (0 .. length ($data) - 1) {
113 my $short = substr $data, 0, $i;
114
115 my $clone = &$sub($short);
372cb964 116 is (defined ($clone), '', "truncated $what to $i should fail");
b8778c7c 117 if ($i < $magic_len) {
372cb964 118 like ($@, "/^Magic number checking on storable $what failed/",
b8778c7c 119 "Should croak with magic number warning");
120 } else {
372cb964 121 is ($@, "", "Should not set \$\@");
b8778c7c 122 }
123 }
124}
125
126sub test_corrupt {
127 my ($data, $sub, $what, $name) = @_;
128
129 my $clone = &$sub($data);
372cb964 130 is (defined ($clone), '', "$name $what should fail");
131 like ($@, $what, $name);
b8778c7c 132}
133
134sub test_things {
135 my ($contents, $sub, $what, $isnetwork) = @_;
136 my $isfile = $what eq 'file';
137 my $file_magic = $isfile ? length $file_magic_str : 0;
138
139 my $header = Storable::read_magic ($contents);
140 test_header ($header, $isfile, $isnetwork);
141
142 # Test that if we re-write it, everything still works:
143 my $clone = &$sub ($contents);
144
372cb964 145 is ($@, "", "There should be no error");
b8778c7c 146
147 test_hash ($clone);
148
149 # Now lets check the short version:
150 test_truncated ($contents, $sub, $file_magic
151 + ($isnetwork ? $network_magic : $other_magic), $what);
152
153 my $copy;
154 if ($isfile) {
155 $copy = $contents;
156 substr ($copy, 0, 4) = 'iron';
157 test_corrupt ($copy, $sub, "/^File is not a perl storable/",
158 "magic number");
159 }
160
161 $copy = $contents;
530b72ba 162 # Needs to be more than 1, as we're already coding a spread of 1 minor version
163 # number on writes (2.5, 2.4). May increase to 2 if we figure we can do 2.3
164 # on 5.005_03 (No utf8).
165 # 4 allows for a small safety margin
166 # (Joke:
167 # Question: What is the value of pi?
168 # Mathematician answers "It's pi, isn't it"
169 # Physicist answers "3.1, within experimental error"
170 # Engineer answers "Well, allowing for a small safety margin, 18"
171 # )
172 my $minor4 = $header->{minor} + 4;
173 substr ($copy, $file_magic + 1, 1) = chr $minor4;
e8189732 174 {
175 # Now by default newer minor version numbers are not a pain.
176 $clone = &$sub($copy);
372cb964 177 is ($@, "", "by default no error on higher minor");
e8189732 178 test_hash ($clone);
179
180 local $Storable::accept_future_minor = 0;
181 test_corrupt ($copy, $sub,
182 "/^Storable binary image v$header->{major}\.$minor4 more recent than I am \\(v$header->{major}\.$minor\\)/",
183 "higher minor");
184 }
b8778c7c 185
186 $copy = $contents;
187 my $major1 = $header->{major} + 1;
188 substr ($copy, $file_magic, 1) = chr 2*$major1;
189 test_corrupt ($copy, $sub,
530b72ba 190 "/^Storable binary image v$major1\.$header->{minor} more recent than I am \\(v$header->{major}\.$minor\\)/",
b8778c7c 191 "higher major");
192
193 # Continue messing with the previous copy
530b72ba 194 my $minor1 = $header->{minor} - 1;
b8778c7c 195 substr ($copy, $file_magic + 1, 1) = chr $minor1;
196 test_corrupt ($copy, $sub,
530b72ba 197 "/^Storable binary image v$major1\.$minor1 more recent than I am \\(v$header->{major}\.$minor\\)/",
b8778c7c 198 "higher major, lower minor");
199
200 my $where;
201 if (!$isnetwork) {
202 # All these are omitted from the network order header.
203 # I'm not sure if it's correct to omit the byte size stuff.
204 $copy = $contents;
205 substr ($copy, $file_magic + 3, length $header->{byteorder})
206 = reverse $header->{byteorder};
207
208 test_corrupt ($copy, $sub, "/^Byte order is not compatible/",
209 "byte order");
210 $where = $file_magic + 3 + length $header->{byteorder};
211 foreach (['intsize', "Integer"],
291cf09c 212 ['longsize', "Long integer"],
b8778c7c 213 ['ptrsize', "Pointer integer"],
214 ['nvsize', "Double"]) {
215 my ($key, $name) = @$_;
216 $copy = $contents;
217 substr ($copy, $where++, 1) = chr 0;
218 test_corrupt ($copy, $sub, "/^$name size is not compatible/",
219 "$name size");
220 }
221 } else {
222 $where = $file_magic + $network_magic;
223 }
224
225 # Just the header and a tag 255. As 26 is currently the highest tag, this
226 # is "unexpected"
227 $copy = substr ($contents, 0, $where) . chr 255;
228
229 test_corrupt ($copy, $sub,
230 "/^Corrupted storable $what \\(binary v$header->{major}.$header->{minor}\\)/",
231 "bogus tag");
e8189732 232
233 # Now drop the minor version number
234 substr ($copy, $file_magic + 1, 1) = chr $minor1;
235
236 test_corrupt ($copy, $sub,
237 "/^Corrupted storable $what \\(binary v$header->{major}.$minor1\\)/",
238 "bogus tag, minor less 1");
239 # Now increase the minor version number
240 substr ($copy, $file_magic + 1, 1) = chr $minor4;
241
242 # local $Storable::DEBUGME = 1;
243 # This is the delayed croak
244 test_corrupt ($copy, $sub,
0ba8809e 245 "/^Storable binary image v$header->{major}.$minor4 contains data of type 255. This Storable is v$header->{major}.$minor and can only handle data types up to 25/",
e8189732 246 "bogus tag, minor plus 4");
247 # And check again that this croak is not delayed:
248 {
249 # local $Storable::DEBUGME = 1;
250 local $Storable::accept_future_minor = 0;
251 test_corrupt ($copy, $sub,
252 "/^Storable binary image v$header->{major}\.$minor4 more recent than I am \\(v$header->{major}\.$minor\\)/",
253 "higher minor");
254 }
b8778c7c 255}
256
257sub slurp {
258 my $file = shift;
259 local (*FH, $/);
260 open FH, "<$file" or die "Can't open '$file': $!";
261 binmode FH;
6e3dd797 262 eval { binmode(FH, ":bytes") }; # Perl 5.8.0+ only
b8778c7c 263 my $contents = <FH>;
264 die "Can't read $file: $!" unless defined $contents;
265 return $contents;
266}
267
268
269ok (defined store(\%hash, $file));
270
9d80fab7 271my $expected = 20 + length ($file_magic_str) + $other_magic + $fancy;
b8778c7c 272my $length = -s $file;
273
274die "Don't seem to have written file '$file' as I can't get its length: $!"
275 unless defined $file;
276
291cf09c 277die "Expected file to be $expected bytes (sizeof long is $Config{longsize}) but it is $length"
b8778c7c 278 unless $length == $expected;
279
280# Read the contents into memory:
281my $contents = slurp $file;
282
283# Test the original direct from disk
284my $clone = retrieve $file;
285test_hash ($clone);
286
287# Then test it.
288test_things($contents, \&store_and_retrieve, 'file');
289
290# And now try almost everything again with a Storable string
291my $stored = freeze \%hash;
292test_things($stored, \&freeze_and_thaw, 'string');
293
294# Network order.
295unlink $file or die "Can't unlink '$file': $!";
296
297ok (defined nstore(\%hash, $file));
298
9d80fab7 299$expected = 20 + length ($file_magic_str) + $network_magic + $fancy;
b8778c7c 300$length = -s $file;
301
302die "Don't seem to have written file '$file' as I can't get its length: $!"
303 unless defined $file;
304
291cf09c 305die "Expected file to be $expected bytes (sizeof long is $Config{longsize}) but it is $length"
b8778c7c 306 unless $length == $expected;
307
308# Read the contents into memory:
309$contents = slurp $file;
310
311# Test the original direct from disk
312$clone = retrieve $file;
313test_hash ($clone);
314
315# Then test it.
316test_things($contents, \&store_and_retrieve, 'file', 1);
317
318# And now try almost everything again with a Storable string
319$stored = nfreeze \%hash;
320test_things($stored, \&freeze_and_thaw, 'string', 1);