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', '../ext/Storable/t');
21 # This lets us distribute Test::More in t/
24 require Config; import Config;
25 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
26 print "1..0 # Skip: Storable was not built\n";
32 use vars qw($file_magic_str $other_magic $network_magic $byteorder
33 $major $minor $minor_write $fancy);
35 $byteorder = $Config{byteorder};
37 $file_magic_str = 'pst0';
38 $other_magic = 7 + length $byteorder;
42 $minor_write = $] > 5.005_50 ? 7 : 4;
46 # If it's 5.7.3 or later the hash will be stored with flags, which is
47 # 2 extra bytes. There are 2 * 2 * 2 tests per byte in the body and header
48 # common to normal and network order serialised objects (hence the 8)
49 # There are only 2 * 2 tests per byte in the parts of the header not present
50 # for network order, and 2 tests per byte on the 'pst0' "magic number" only
51 # present in files, but not in things store()ed to memory
52 $fancy = ($] > 5.007 ? 2 : 0);
54 plan tests => 368 + length ($byteorder) * 4 + $fancy * 8 + 1;
56 use Storable qw (store retrieve freeze thaw nstore nfreeze);
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");
88 skip ("No \$Config{prtsize} on this perl version ($])", 1)
89 unless defined $Config{ptrsize};
90 is ($header->{ptrsize}, $Config{ptrsize}, "long size");
92 is ($header->{nvsize}, $Config{nvsize} || $Config{doublesize} || 8,
93 "nv size"); # 5.00405 doesn't even have doublesize in config.
98 my ($data, $sub, $magic_len, $what) = @_;
99 for my $i (0 .. length ($data) - 1) {
100 my $short = substr $data, 0, $i;
102 # local $Storable::DEBUGME = 1;
103 my $clone = &$sub($short);
104 is (defined ($clone), '', "truncated $what to $i should fail");
105 if ($i < $magic_len) {
106 like ($@, "/^Magic number checking on storable $what failed/",
107 "Should croak with magic number warning");
109 is ($@, "", "Should not set \$\@");
115 my ($data, $sub, $what, $name) = @_;
117 my $clone = &$sub($data);
118 is (defined ($clone), '', "$name $what should fail");
119 like ($@, $what, $name);
123 my ($contents, $sub, $what, $isnetwork) = @_;
124 my $isfile = $what eq 'file';
125 my $file_magic = $isfile ? length $file_magic_str : 0;
127 my $header = Storable::read_magic ($contents);
128 test_header ($header, $isfile, $isnetwork);
130 # Test that if we re-write it, everything still works:
131 my $clone = &$sub ($contents);
133 is ($@, "", "There should be no error");
137 # Now lets check the short version:
138 test_truncated ($contents, $sub, $file_magic
139 + ($isnetwork ? $network_magic : $other_magic), $what);
144 substr ($copy, 0, 4) = 'iron';
145 test_corrupt ($copy, $sub, "/^File is not a perl storable/",
150 # Needs to be more than 1, as we're already coding a spread of 1 minor version
151 # number on writes (2.5, 2.4). May increase to 2 if we figure we can do 2.3
152 # on 5.005_03 (No utf8).
153 # 4 allows for a small safety margin
155 # Question: What is the value of pi?
156 # Mathematician answers "It's pi, isn't it"
157 # Physicist answers "3.1, within experimental error"
158 # Engineer answers "Well, allowing for a small safety margin, 18"
160 my $minor4 = $header->{minor} + 4;
161 substr ($copy, $file_magic + 1, 1) = chr $minor4;
163 # Now by default newer minor version numbers are not a pain.
164 $clone = &$sub($copy);
165 is ($@, "", "by default no error on higher minor");
168 local $Storable::accept_future_minor = 0;
169 test_corrupt ($copy, $sub,
170 "/^Storable binary image v$header->{major}\.$minor4 more recent than I am \\(v$header->{major}\.$minor\\)/",
175 my $major1 = $header->{major} + 1;
176 substr ($copy, $file_magic, 1) = chr 2*$major1;
177 test_corrupt ($copy, $sub,
178 "/^Storable binary image v$major1\.$header->{minor} more recent than I am \\(v$header->{major}\.$minor\\)/",
181 # Continue messing with the previous copy
182 my $minor1 = $header->{minor} - 1;
183 substr ($copy, $file_magic + 1, 1) = chr $minor1;
184 test_corrupt ($copy, $sub,
185 "/^Storable binary image v$major1\.$minor1 more recent than I am \\(v$header->{major}\.$minor\\)/",
186 "higher major, lower minor");
190 # All these are omitted from the network order header.
191 # I'm not sure if it's correct to omit the byte size stuff.
193 substr ($copy, $file_magic + 3, length $header->{byteorder})
194 = reverse $header->{byteorder};
196 test_corrupt ($copy, $sub, "/^Byte order is not compatible/",
198 $where = $file_magic + 3 + length $header->{byteorder};
199 foreach (['intsize', "Integer"],
200 ['longsize', "Long integer"],
201 ['ptrsize', "Pointer"],
202 ['nvsize', "Double"]) {
203 my ($key, $name) = @$_;
205 substr ($copy, $where++, 1) = chr 0;
206 test_corrupt ($copy, $sub, "/^$name size is not compatible/",
210 $where = $file_magic + $network_magic;
213 # Just the header and a tag 255. As 28 is currently the highest tag, this
215 $copy = substr ($contents, 0, $where) . chr 255;
217 test_corrupt ($copy, $sub,
218 "/^Corrupted storable $what \\(binary v$header->{major}.$header->{minor}\\)/",
221 # Now drop the minor version number
222 substr ($copy, $file_magic + 1, 1) = chr $minor1;
224 test_corrupt ($copy, $sub,
225 "/^Corrupted storable $what \\(binary v$header->{major}.$minor1\\)/",
226 "bogus tag, minor less 1");
227 # Now increase the minor version number
228 substr ($copy, $file_magic + 1, 1) = chr $minor4;
230 # local $Storable::DEBUGME = 1;
231 # This is the delayed croak
232 test_corrupt ($copy, $sub,
233 "/^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 28/",
234 "bogus tag, minor plus 4");
235 # And check again that this croak is not delayed:
237 # local $Storable::DEBUGME = 1;
238 local $Storable::accept_future_minor = 0;
239 test_corrupt ($copy, $sub,
240 "/^Storable binary image v$header->{major}\.$minor4 more recent than I am \\(v$header->{major}\.$minor\\)/",
245 ok (defined store(\%hash, $file));
247 my $expected = 20 + length ($file_magic_str) + $other_magic + $fancy;
248 my $length = -s $file;
250 die "Don't seem to have written file '$file' as I can't get its length: $!"
251 unless defined $file;
253 die "Expected file to be $expected bytes (sizeof long is $Config{longsize}) but it is $length"
254 unless $length == $expected;
256 # Read the contents into memory:
257 my $contents = slurp ($file);
259 # Test the original direct from disk
260 my $clone = retrieve $file;
264 test_things($contents, \&store_and_retrieve, 'file');
266 # And now try almost everything again with a Storable string
267 my $stored = freeze \%hash;
268 test_things($stored, \&freeze_and_thaw, 'string');
271 unlink $file or die "Can't unlink '$file': $!";
273 ok (defined nstore(\%hash, $file));
275 $expected = 20 + length ($file_magic_str) + $network_magic + $fancy;
278 die "Don't seem to have written file '$file' as I can't get its length: $!"
279 unless defined $file;
281 die "Expected file to be $expected bytes (sizeof long is $Config{longsize}) but it is $length"
282 unless $length == $expected;
284 # Read the contents into memory:
285 $contents = slurp ($file);
287 # Test the original direct from disk
288 $clone = retrieve $file;
292 test_things($contents, \&store_and_retrieve, 'file', 1);
294 # And now try almost everything again with a Storable string
295 $stored = nfreeze \%hash;
296 test_things($stored, \&freeze_and_thaw, 'string', 1);
298 # Test that the bug fixed by #20587 doesn't affect us under some older
301 chop(my $a = chr(0xDF).chr(256));
302 my %a = (chr(0xDF) => 1);
305 # If we were built with -DDEBUGGING, the assert() should have killed
306 # us, which will probably alert the user that something went wrong.