3 # Copyright (c) 2002 Slaven Rezic
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
12 @INC = ('.', '../lib');
16 require Config; import Config;
17 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
18 print "1..0 # Skip: Storable was not built\n";
31 print "1..0 # skip: tests only work with B::Deparse 0.61 and at least perl 5.6.0\n";
35 if ($File::Spec::VERSION < 0.8) {
36 print "1..0 # Skip: newer File::Spec needed\n";
41 BEGIN { plan tests => 47 }
43 use Storable qw(retrieve store nstore freeze nfreeze thaw dclone);
46 #$Storable::DEBUGME = 1;
48 use vars qw($freezed $thawed @obj @res $blessed_code);
51 $blessed_code = bless sub { "blessed" }, "Some::Package";
52 { package Another::Package; sub foo { __PACKAGE__ } }
55 ([\&code, # code reference
57 $blessed_code, # blessed code reference
58 \&Another::Package::foo, # code in another package
59 sub ($$;$) { 0 }, # prototypes
60 sub { print "test\n" },
61 \&Test::ok, # large scalar
64 {"a" => sub { "srt" }, "b" => \&code},
66 sub { ord("a")-ord("7") },
70 \&dclone, # XS function
72 sub { open FOO, "/" },
75 $Storable::Deparse = 1;
78 ######################################################################
81 $freezed = freeze $obj[0];
82 $thawed = thaw $freezed;
84 ok($thawed->[0]->(), "JAPH");
85 ok($thawed->[1]->(), 42);
86 ok($thawed->[2]->(), "blessed");
87 ok($thawed->[3]->(), "Another::Package");
88 ok(prototype($thawed->[4]), prototype($obj[0]->[4]));
90 ######################################################################
92 $freezed = freeze $obj[1];
93 $thawed = thaw $freezed;
95 ok($thawed->{"a"}->(), "srt");
96 ok($thawed->{"b"}->(), "JAPH");
98 ######################################################################
100 $freezed = freeze $obj[2];
101 $thawed = thaw $freezed;
105 ######################################################################
107 $freezed = freeze $obj[3];
108 $thawed = thaw $freezed;
110 ok($thawed->(), "JAPH");
112 ######################################################################
114 eval { $freezed = freeze $obj[4] };
115 ok($@ =~ /The result of B::Deparse::coderef2text was empty/);
117 ######################################################################
120 my $new_sub = dclone($obj[2]);
121 ok($new_sub->(), $obj[2]->());
123 ######################################################################
124 # Test retrieve & store
126 store $obj[0], 'store';
127 $thawed = retrieve 'store';
129 ok($thawed->[0]->(), "JAPH");
130 ok($thawed->[1]->(), 42);
131 ok($thawed->[2]->(), "blessed");
132 ok($thawed->[3]->(), "Another::Package");
133 ok(prototype($thawed->[4]), prototype($obj[0]->[4]));
135 ######################################################################
137 nstore $obj[0], 'store';
138 $thawed = retrieve 'store';
141 ok($thawed->[0]->(), "JAPH");
142 ok($thawed->[1]->(), 42);
143 ok($thawed->[2]->(), "blessed");
144 ok($thawed->[3]->(), "Another::Package");
145 ok(prototype($thawed->[4]), prototype($obj[0]->[4]));
147 ######################################################################
153 local $Storable::Eval = 0;
156 $freezed = freeze $obj[$i];
158 eval { $thawed = thaw $freezed };
159 ok($@ =~ /Can\'t eval/);
165 local $Storable::Deparse = 0;
168 eval { $freezed = freeze $obj[$i] };
169 ok($@ =~ /Can\'t store CODE items/);
174 local $Storable::Eval = 0;
175 local $Storable::forgive_me = 1;
177 $freezed = freeze $obj[0]->[$i];
179 eval { $thawed = thaw $freezed };
181 ok($$thawed =~ /^sub/);
186 local $Storable::Deparse = 0;
187 local $Storable::forgive_me = 1;
189 my $devnull = File::Spec->devnull;
191 open(SAVEERR, ">&STDERR");
192 open(STDERR, ">$devnull") or
193 ( print SAVEERR "Unable to redirect STDERR: $!\n" and exit(1) );
195 eval { $freezed = freeze $obj[0]->[0] };
197 open(STDERR, ">&SAVEERR");
205 $safe->permit(qw(:default require));
206 local $Storable::Eval = sub { $safe->reval(shift) };
208 for my $def ([0 => "JAPH",
212 my($i, $res) = @$def;
213 $freezed = freeze $obj[0]->[$i];
215 eval { $thawed = thaw $freezed };
217 ok($thawed->(), $res);
220 $freezed = freeze $obj[0]->[6];
221 eval { $thawed = thaw $freezed };
225 # Disable or fix this test if the internal representation of Storable
227 skip("no malicious storable file check", 1);
229 # Construct malicious storable code
230 $freezed = nfreeze $obj[0]->[0];
231 my $bad_code = ';open FOO, "/badfile"';
232 # 5th byte is (short) length of scalar
233 my $len = ord(substr($freezed, 4, 1));
234 substr($freezed, 4, 1, chr($len+length($bad_code)));
235 substr($freezed, -1, 0, $bad_code);
237 eval { $thawed = thaw $freezed };
245 sub new { bless {}, shift }
248 # Here you can apply some nifty regexpes to ensure the
249 # safeness of the source code.
250 my $coderef = eval $source;
255 my $safe = new MySafe;
256 local $Storable::Eval = sub { $safe->reval($_[0]) };
258 $freezed = freeze $obj[0];
259 eval { $thawed = thaw $freezed };
265 ok($thawed->[0]->(), "JAPH");
266 ok($thawed->[1]->(), 42);
267 ok($thawed->[2]->(), "blessed");
268 ok($thawed->[3]->(), "Another::Package");
269 ok(prototype($thawed->[4]), prototype($obj[0]->[4]));