3 # Copyright 2002, Larry Wall.
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
9 # I'm trying to keep this test easily backwards compatible to 5.004, so no
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
19 @INC = ('.', '../lib');
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";
29 use vars qw($file_magic_str $other_magic $network_magic $byteorder
30 $major $minor $minor_write $fancy);
32 $byteorder = $Config{byteorder};
34 $file_magic_str = 'pst0';
35 $other_magic = 7 + length $byteorder;
39 $minor_write = $] > 5.007 ? 5 : 4;
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);
51 plan tests => 368 + length ($byteorder) * 4 + $fancy * 8;
53 use Storable qw (store retrieve freeze thaw nstore nfreeze);
55 my $file = "malice.$$";
56 die "Temporary file 'malice.$$' already exists" if -e $file;
58 END { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }}
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
64 my %hash = (perl => 'rules', chr 256, '');
65 delete $hash{chr 256};
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");
76 my ($header, $isfile, $isnetorder) = @_;
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");
82 # Network order header has no sizes
84 is ($header->{byteorder}, $byteorder, "byte order");
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,
89 "nv size"); # 5.00405 doesn't even have doublesize in config.
93 sub store_and_retrieve {
95 unlink $file or die "Can't unlink '$file': $!";
96 open FH, ">$file" or die "Can't open '$file': $!";
98 eval { binmode(FH, ":bytes") }; # Perl 5.8.0+ only
99 print FH $data or die "Can't print to '$file': $!";
100 close FH or die "Can't close '$file': $!";
102 return eval {retrieve $file};
105 sub freeze_and_thaw {
107 return eval {thaw $data};
111 my ($data, $sub, $magic_len, $what) = @_;
112 for my $i (0 .. length ($data) - 1) {
113 my $short = substr $data, 0, $i;
115 my $clone = &$sub($short);
116 is (defined ($clone), '', "truncated $what to $i should fail");
117 if ($i < $magic_len) {
118 like ($@, "/^Magic number checking on storable $what failed/",
119 "Should croak with magic number warning");
121 is ($@, "", "Should not set \$\@");
127 my ($data, $sub, $what, $name) = @_;
129 my $clone = &$sub($data);
130 is (defined ($clone), '', "$name $what should fail");
131 like ($@, $what, $name);
135 my ($contents, $sub, $what, $isnetwork) = @_;
136 my $isfile = $what eq 'file';
137 my $file_magic = $isfile ? length $file_magic_str : 0;
139 my $header = Storable::read_magic ($contents);
140 test_header ($header, $isfile, $isnetwork);
142 # Test that if we re-write it, everything still works:
143 my $clone = &$sub ($contents);
145 is ($@, "", "There should be no error");
149 # Now lets check the short version:
150 test_truncated ($contents, $sub, $file_magic
151 + ($isnetwork ? $network_magic : $other_magic), $what);
156 substr ($copy, 0, 4) = 'iron';
157 test_corrupt ($copy, $sub, "/^File is not a perl storable/",
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
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"
172 my $minor4 = $header->{minor} + 4;
173 substr ($copy, $file_magic + 1, 1) = chr $minor4;
175 # Now by default newer minor version numbers are not a pain.
176 $clone = &$sub($copy);
177 is ($@, "", "by default no error on higher minor");
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\\)/",
187 my $major1 = $header->{major} + 1;
188 substr ($copy, $file_magic, 1) = chr 2*$major1;
189 test_corrupt ($copy, $sub,
190 "/^Storable binary image v$major1\.$header->{minor} more recent than I am \\(v$header->{major}\.$minor\\)/",
193 # Continue messing with the previous copy
194 my $minor1 = $header->{minor} - 1;
195 substr ($copy, $file_magic + 1, 1) = chr $minor1;
196 test_corrupt ($copy, $sub,
197 "/^Storable binary image v$major1\.$minor1 more recent than I am \\(v$header->{major}\.$minor\\)/",
198 "higher major, lower minor");
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.
205 substr ($copy, $file_magic + 3, length $header->{byteorder})
206 = reverse $header->{byteorder};
208 test_corrupt ($copy, $sub, "/^Byte order is not compatible/",
210 $where = $file_magic + 3 + length $header->{byteorder};
211 foreach (['intsize', "Integer"],
212 ['longsize', "Long integer"],
213 ['ptrsize', "Pointer integer"],
214 ['nvsize', "Double"]) {
215 my ($key, $name) = @$_;
217 substr ($copy, $where++, 1) = chr 0;
218 test_corrupt ($copy, $sub, "/^$name size is not compatible/",
222 $where = $file_magic + $network_magic;
225 # Just the header and a tag 255. As 26 is currently the highest tag, this
227 $copy = substr ($contents, 0, $where) . chr 255;
229 test_corrupt ($copy, $sub,
230 "/^Corrupted storable $what \\(binary v$header->{major}.$header->{minor}\\)/",
233 # Now drop the minor version number
234 substr ($copy, $file_magic + 1, 1) = chr $minor1;
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;
242 # local $Storable::DEBUGME = 1;
243 # This is the delayed croak
244 test_corrupt ($copy, $sub,
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/",
246 "bogus tag, minor plus 4");
247 # And check again that this croak is not delayed:
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\\)/",
260 open FH, "<$file" or die "Can't open '$file': $!";
262 eval { binmode(FH, ":bytes") }; # Perl 5.8.0+ only
264 die "Can't read $file: $!" unless defined $contents;
269 ok (defined store(\%hash, $file));
271 my $expected = 20 + length ($file_magic_str) + $other_magic + $fancy;
272 my $length = -s $file;
274 die "Don't seem to have written file '$file' as I can't get its length: $!"
275 unless defined $file;
277 die "Expected file to be $expected bytes (sizeof long is $Config{longsize}) but it is $length"
278 unless $length == $expected;
280 # Read the contents into memory:
281 my $contents = slurp $file;
283 # Test the original direct from disk
284 my $clone = retrieve $file;
288 test_things($contents, \&store_and_retrieve, 'file');
290 # And now try almost everything again with a Storable string
291 my $stored = freeze \%hash;
292 test_things($stored, \&freeze_and_thaw, 'string');
295 unlink $file or die "Can't unlink '$file': $!";
297 ok (defined nstore(\%hash, $file));
299 $expected = 20 + length ($file_magic_str) + $network_magic + $fancy;
302 die "Don't seem to have written file '$file' as I can't get its length: $!"
303 unless defined $file;
305 die "Expected file to be $expected bytes (sizeof long is $Config{longsize}) but it is $length"
306 unless $length == $expected;
308 # Read the contents into memory:
309 $contents = slurp $file;
311 # Test the original direct from disk
312 $clone = retrieve $file;
316 test_things($contents, \&store_and_retrieve, 'file', 1);
318 # And now try almost everything again with a Storable string
319 $stored = nfreeze \%hash;
320 test_things($stored, \&freeze_and_thaw, 'string', 1);