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
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#!./perl -w
#
# Copyright 2002, Larry Wall.
#
# You may redistribute only under the same terms as Perl 5, as specified
# in the README file that comes with the distribution.
#
# I'm trying to keep this test easily backwards compatible to 5.004, so no
# qr//;
# Currently using Test not Test::More, as Test is in core that far back.
# This test tries to craft malicious data to test out as many different
# error traps in Storable as possible
# It also acts as a test for read_header
sub BEGIN {
if ($ENV{PERL_CORE}){
chdir('t') if -d 't';
@INC = '.';
push @INC, '../lib';
}
require Config; import Config;
if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
print "1..0 # Skip: Storable was not built\n";
exit 0;
}
# require 'lib/st-dump.pl';
}
use strict;
use vars qw($file_magic_str $other_magic $network_magic $major $minor
$minor_write);
$file_magic_str = 'pst0';
$other_magic = 7 + length($Config{byteorder});
$network_magic = 2;
$major = 2;
$minor = 5;
$minor_write = $] > 5.007 ? 5 : 4;
use Test;
BEGIN { plan tests => 334 + length($Config{byteorder}) * 4}
use Storable qw (store retrieve freeze thaw nstore nfreeze);
my $file = "malice.$$";
die "Temporary file 'malice.$$' already exists" if -e $file;
END { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }}
my %hash = (perl => 'rules');
sub test_hash {
my $clone = shift;
ok (ref $clone, "HASH", "Get hash back");
ok (scalar keys %$clone, 1, "with 1 key");
ok ((keys %$clone)[0], "perl", "which is correct");
ok ($clone->{perl}, "rules");
}
sub test_header {
my ($header, $isfile, $isnetorder) = @_;
ok (!!$header->{file}, !!$isfile, "is file");
ok ($header->{major}, $major, "major number");
ok ($header->{minor}, $minor_write, "minor number");
ok (!!$header->{netorder}, !!$isnetorder, "is network order");
if ($isnetorder) {
# Skip these
for (1..5) {
ok (1, 1, "Network order header has no sizes");
}
} else {
ok ($header->{byteorder}, $Config{byteorder}, "byte order");
ok ($header->{intsize}, $Config{intsize}, "int size");
ok ($header->{longsize}, $Config{longsize}, "long size");
ok ($header->{ptrsize}, $Config{ptrsize}, "long size");
ok ($header->{nvsize}, $Config{nvsize} || $Config{doublesize} || 8,
"nv size"); # 5.00405 doesn't even have doublesize in config.
}
}
sub store_and_retrieve {
my $data = shift;
unlink $file or die "Can't unlink '$file': $!";
open FH, ">$file" or die "Can't open '$file': $!";
binmode FH;
print FH $data or die "Can't print to '$file': $!";
close FH or die "Can't close '$file': $!";
return eval {retrieve $file};
}
sub freeze_and_thaw {
my $data = shift;
return eval {thaw $data};
}
sub test_truncated {
my ($data, $sub, $magic_len, $what) = @_;
for my $i (0 .. length ($data) - 1) {
my $short = substr $data, 0, $i;
my $clone = &$sub($short);
ok (defined ($clone), '', "truncated $what to $i should fail");
if ($i < $magic_len) {
ok ($@, "/^Magic number checking on storable $what failed/",
"Should croak with magic number warning");
} else {
ok ($@, "", "Should not set \$\@");
}
}
}
sub test_corrupt {
my ($data, $sub, $what, $name) = @_;
my $clone = &$sub($data);
ok (defined ($clone), '', "$name $what should fail");
ok ($@, $what, $name);
}
sub test_things {
my ($contents, $sub, $what, $isnetwork) = @_;
my $isfile = $what eq 'file';
my $file_magic = $isfile ? length $file_magic_str : 0;
my $header = Storable::read_magic ($contents);
test_header ($header, $isfile, $isnetwork);
# Test that if we re-write it, everything still works:
my $clone = &$sub ($contents);
ok ($@, "", "There should be no error");
test_hash ($clone);
# Now lets check the short version:
test_truncated ($contents, $sub, $file_magic
+ ($isnetwork ? $network_magic : $other_magic), $what);
my $copy;
if ($isfile) {
$copy = $contents;
substr ($copy, 0, 4) = 'iron';
test_corrupt ($copy, $sub, "/^File is not a perl storable/",
"magic number");
}
$copy = $contents;
# Needs to be more than 1, as we're already coding a spread of 1 minor version
# number on writes (2.5, 2.4). May increase to 2 if we figure we can do 2.3
# on 5.005_03 (No utf8).
# 4 allows for a small safety margin
# (Joke:
# Question: What is the value of pi?
# Mathematician answers "It's pi, isn't it"
# Physicist answers "3.1, within experimental error"
# Engineer answers "Well, allowing for a small safety margin, 18"
# )
my $minor4 = $header->{minor} + 4;
substr ($copy, $file_magic + 1, 1) = chr $minor4;
test_corrupt ($copy, $sub,
"/^Storable binary image v$header->{major}\.$minor4 more recent than I am \\(v$header->{major}\.$minor\\)/",
"higher minor");
$copy = $contents;
my $major1 = $header->{major} + 1;
substr ($copy, $file_magic, 1) = chr 2*$major1;
test_corrupt ($copy, $sub,
"/^Storable binary image v$major1\.$header->{minor} more recent than I am \\(v$header->{major}\.$minor\\)/",
"higher major");
# Continue messing with the previous copy
my $minor1 = $header->{minor} - 1;
substr ($copy, $file_magic + 1, 1) = chr $minor1;
test_corrupt ($copy, $sub,
"/^Storable binary image v$major1\.$minor1 more recent than I am \\(v$header->{major}\.$minor\\)/",
"higher major, lower minor");
my $where;
if (!$isnetwork) {
# All these are omitted from the network order header.
# I'm not sure if it's correct to omit the byte size stuff.
$copy = $contents;
substr ($copy, $file_magic + 3, length $header->{byteorder})
= reverse $header->{byteorder};
test_corrupt ($copy, $sub, "/^Byte order is not compatible/",
"byte order");
$where = $file_magic + 3 + length $header->{byteorder};
foreach (['intsize', "Integer"],
['longsize', "Long integer"],
['ptrsize', "Pointer integer"],
['nvsize', "Double"]) {
my ($key, $name) = @$_;
$copy = $contents;
substr ($copy, $where++, 1) = chr 0;
test_corrupt ($copy, $sub, "/^$name size is not compatible/",
"$name size");
}
} else {
$where = $file_magic + $network_magic;
}
# Just the header and a tag 255. As 26 is currently the highest tag, this
# is "unexpected"
$copy = substr ($contents, 0, $where) . chr 255;
test_corrupt ($copy, $sub,
"/^Corrupted storable $what \\(binary v$header->{major}.$header->{minor}\\)/",
"bogus tag");
}
sub slurp {
my $file = shift;
local (*FH, $/);
open FH, "<$file" or die "Can't open '$file': $!";
binmode FH;
my $contents = <FH>;
die "Can't read $file: $!" unless defined $contents;
return $contents;
}
ok (defined store(\%hash, $file));
my $expected = 20 + length ($file_magic_str) + $other_magic;
my $length = -s $file;
die "Don't seem to have written file '$file' as I can't get its length: $!"
unless defined $file;
die "Expected file to be $expected bytes (sizeof long is $Config{longsize}) but it is $length"
unless $length == $expected;
# Read the contents into memory:
my $contents = slurp $file;
# Test the original direct from disk
my $clone = retrieve $file;
test_hash ($clone);
# Then test it.
test_things($contents, \&store_and_retrieve, 'file');
# And now try almost everything again with a Storable string
my $stored = freeze \%hash;
test_things($stored, \&freeze_and_thaw, 'string');
# Network order.
unlink $file or die "Can't unlink '$file': $!";
ok (defined nstore(\%hash, $file));
$expected = 20 + length ($file_magic_str) + $network_magic;
$length = -s $file;
die "Don't seem to have written file '$file' as I can't get its length: $!"
unless defined $file;
die "Expected file to be $expected bytes (sizeof long is $Config{longsize}) but it is $length"
unless $length == $expected;
# Read the contents into memory:
$contents = slurp $file;
# Test the original direct from disk
$clone = retrieve $file;
test_hash ($clone);
# Then test it.
test_things($contents, \&store_and_retrieve, 'file', 1);
# And now try almost everything again with a Storable string
$stored = nfreeze \%hash;
test_things($stored, \&freeze_and_thaw, 'string', 1);
|