3 # Copyright (c) 1995-2000, Raphael Manfredi
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
10 # Do this as the very first thing, in order to avoid problems with the
11 # PADTMP flag on pre-5.19.3 threaded Perls. On those Perls, compiling
12 # code that contains a constant-folded canonical truth value breaks
13 # the ability to take a reference to that canonical truth value later.
23 if ($ENV{PERL_CORE}) {
24 chdir 'dist/Storable' if -d 'dist/Storable';
25 @INC = ('../../lib', 't');
28 unshift @INC, 't/compat' if $] < 5.006002;
30 require Config; import Config;
31 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
32 print "1..0 # Skip: Storable was not built\n";
39 use Storable qw(freeze thaw store retrieve fd_retrieve);
42 (REF => \(my $aref = []),
43 VSTRING => \(my $vstring = v1.2.3),
44 'long VSTRING' => \(my $lvstring = eval "v" . 0 x 300),
45 LVALUE => \(my $substr = substr((my $str = "foo"), 0, 3)));
48 my $tests = $test + 41 + (2 * 6 * keys %::immortals) + (3 * keys %::weird_refs);
49 plan(tests => $tests);
53 sub make { bless [], shift }
55 package SHORT_NAME_WITH_HOOK;
57 sub make { bless [], shift }
68 die "STORABLE_thaw" unless $obj eq $self;
73 # Still less than 256 bytes, so long classname logic not fully exercised
74 # Identifier too long - 5.004
75 # parser.h: char tokenbuf[256]: cperl5.24 => 1024
76 my $m = ($Config{usecperl} and $] >= 5.024) ? 56 : 14;
77 my $longname = "LONG_NAME_" . ('xxxxxxxxxxxxx::' x $m) . "final";
82 \@ISA = ("SHORT_NAME");
87 package ${longname}_WITH_HOOK;
89 \@ISA = ("SHORT_NAME_WITH_HOOK");
93 # Construct a pool of objects
95 for (my $i = 0; $i < 10; $i++) {
96 push(@pool, SHORT_NAME->make);
97 push(@pool, SHORT_NAME_WITH_HOOK->make);
98 push(@pool, $longname->make);
99 push(@pool, "${longname}_WITH_HOOK"->make);
102 my $x = freeze \@pool;
103 pass("Freeze didn't crash");
107 is(scalar @{$y}, @pool);
109 is(ref $y->[0], 'SHORT_NAME');
110 is(ref $y->[1], 'SHORT_NAME_WITH_HOOK');
111 is(ref $y->[2], $longname);
112 is(ref $y->[3], "${longname}_WITH_HOOK");
115 for (my $i = 0; $i < 10; $i++) {
116 do { $good = 0; last } unless ref $y->[4*$i] eq 'SHORT_NAME';
117 do { $good = 0; last } unless ref $y->[4*$i+1] eq 'SHORT_NAME_WITH_HOOK';
118 do { $good = 0; last } unless ref $y->[4*$i+2] eq $longname;
119 do { $good = 0; last } unless ref $y->[4*$i+3] eq "${longname}_WITH_HOOK";
124 my $blessed_ref = bless \\[1,2,3], 'Foobar';
125 my $x = freeze $blessed_ref;
127 is(ref $y, 'Foobar');
131 package RETURNS_IMMORTALS;
133 sub make { my $self = shift; bless [@_], $self }
135 sub STORABLE_freeze {
136 # Some reference some number of times.
138 my ($what, $times) = @$self;
139 return ("$what$times", ($::immortals{$what}) x $times);
146 my ($what, $times) = $x =~ /(.)(\d+)/;
147 die "'$x' didn't match" unless defined $times;
148 main::is(scalar @refs, $times);
149 my $expect = $::immortals{$what};
150 die "'$x' did not give a reference" unless ref $expect;
153 $fail++ if $_ != $expect;
155 main::is($fail, undef);
160 # XXX Failed tests: 15, 27, 39 with 5.12 and 5.10 threaded.
161 # 15: 1 fail (y x 1), 27: 2 fail (y x 2), 39: 3 fail (y x 3)
162 # $Storable::DEBUGME = 1;
164 foreach $count (1..3) {
166 foreach $immortal (keys %::immortals) {
167 print "# $immortal x $count\n";
168 my $i = RETURNS_IMMORTALS->make ($immortal, $count);
172 # ref sv_true is not always sv_true, at least in older threaded perls.
173 local $TODO = "Some 5.10/12 do not preserve ref identity with freeze \\(1 == 1)"
174 if !defined($f) and $] < 5.013 and $] > 5.009 and $immortal eq 'y';
178 pass("thaw didn't crash");
182 # Test automatic require of packages to find thaw hook.
193 sub STORABLE_freeze {
200 my $f = freeze (HAS_HOOK->make);
202 is($HAS_HOOK::loaded_count, 0);
203 is($HAS_HOOK::thawed_count, 0);
206 is($HAS_HOOK::loaded_count, 1);
207 is($HAS_HOOK::thawed_count, 1);
209 is(ref $t, 'HAS_HOOK');
211 delete $INC{"HAS_HOOK.pm"};
212 delete $HAS_HOOK::{STORABLE_thaw};
215 is($HAS_HOOK::loaded_count, 2);
216 is($HAS_HOOK::thawed_count, 2);
218 is(ref $t, 'HAS_HOOK');
221 package STRESS_THE_STACK;
232 sub STORABLE_freeze {
235 return no_op(1..(++$stress * 2000)) ? die "can't happen" : '';
241 no_op(1..(++$stress * 2000)) && die "can't happen";
246 $STRESS_THE_STACK::freeze_count = 0;
247 $STRESS_THE_STACK::thaw_count = 0;
249 $f = freeze (STRESS_THE_STACK->make);
251 is($STRESS_THE_STACK::freeze_count, 1);
252 is($STRESS_THE_STACK::thaw_count, 0);
255 is($STRESS_THE_STACK::freeze_count, 1);
256 is($STRESS_THE_STACK::thaw_count, 1);
258 is(ref $t, 'STRESS_THE_STACK');
260 my $file = "storable-testfile.$$";
261 die "Temporary file '$file' already exists" if -e $file;
263 END { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }}
265 $STRESS_THE_STACK::freeze_count = 0;
266 $STRESS_THE_STACK::thaw_count = 0;
268 store (STRESS_THE_STACK->make, $file);
270 is($STRESS_THE_STACK::freeze_count, 1);
271 is($STRESS_THE_STACK::thaw_count, 0);
273 $t = retrieve ($file);
274 is($STRESS_THE_STACK::freeze_count, 1);
275 is($STRESS_THE_STACK::thaw_count, 1);
277 is(ref $t, 'STRESS_THE_STACK');
280 package ModifyARG112358;
281 sub STORABLE_freeze { $_[0] = "foo"; }
282 my $o= {str=>bless {}};
284 ::is ref $o->{str}, __PACKAGE__,
285 'assignment to $_[0] in STORABLE_freeze does not corrupt things';
291 package WeirdRefHook;
292 sub STORABLE_freeze { () }
293 $INC{'WeirdRefHook.pm'} = __FILE__;
296 for my $weird (keys %weird_refs) {
297 my $obj = $weird_refs{$weird};
298 bless $obj, 'WeirdRefHook';
300 my $success = eval { $frozen = freeze($obj); 1 };
301 ok($success, "can freeze $weird objects")
302 || diag("freezing failed: $@");
303 my $thawn = thaw($frozen);
304 # is_deeply ignores blessings
305 is ref $thawn, ref $obj, "get the right blessing back for $weird";
306 if ($weird =~ 'VSTRING') {
307 # It is not just Storable that did not support vstrings. :-)
308 # See https://rt.cpan.org/Ticket/Display.html?id=78678
309 my $newver = "version"->can("new")
310 ? sub { "version"->new(shift) }
313 $$thawn eq $$obj && &$newver($$thawn) eq &$newver($$obj),
314 "get the right value back"
316 diag "$$thawn vs $$obj";
317 diag &$newver($$thawn) eq &$newver($$obj) if &$newver(1);
321 is_deeply($thawn, $obj, "get the right value back");
334 die 'Bad data' unless defined $string;
335 my $self = { string => $string };
336 return bless $self, $class;
339 sub STORABLE_freeze {
343 return ($self->{string});
346 sub STORABLE_attach {
350 return $class->new($string);
354 my $x = [ RT118551->new('a'), RT118551->new('') ];
358 ok(eval {thaw($y)}, "empty serialized") or diag $@; # <-- dies here with "Bad data"
363 package FreezeHookDies;
364 sub STORABLE_freeze {
368 package ThawHookDies;
369 sub STORABLE_freeze {
370 my ($self, $cloning) = @_;
375 my ($self, $cloning, $str, $obj) = @_;
379 my $x = bless \(my $tmpx = "Foo"), "FreezeHookDies";
380 my $y = bless \(my $tmpy = []), "FreezeHookDies";
382 ok(!eval { store($x, "store$$"); 1 }, "store of hook which throws no NL died");
383 ok(!eval { store($y, "store$$"); 1 }, "store of hook which throws ref died");
385 ok(!eval { freeze($x); 1 }, "freeze of hook which throws no NL died");
386 ok(!eval { freeze($y); 1 }, "freeze of hook which throws ref died");
388 ok(!eval { dclone($x); 1 }, "dclone of hook which throws no NL died");
389 ok(!eval { dclone($y); 1 }, "dclone of hook which throws ref died");
391 my $ostr = bless \(my $tmpstr = "Foo"), "ThawHookDies";
392 my $oref = bless \(my $tmpref = []), "ThawHookDies";
393 ok(store($ostr, "store$$"), "save throw Foo on thaw");
394 ok(!eval { retrieve("store$$"); 1 }, "retrieve of throw Foo on thaw died");
395 open FH, "<", "store$$" or die;
397 ok(!eval { fd_retrieve(*FH); 1 }, "fd_retrieve of throw Foo on thaw died");
398 ok(!ref $@, "right thing thrown");
400 ok(store($oref, "store$$"), "save throw ref on thaw");
401 ok(!eval { retrieve("store$$"); 1 }, "retrieve of throw ref on thaw died");
402 open FH, "<", "store$$" or die;
404 ok(!eval { fd_retrieve(*FH); 1 }, "fd_retrieve of throw [] on thaw died");
405 ok(ref $@, "right thing thrown");
408 my $strdata = freeze($ostr);
409 ok(!eval { thaw($strdata); 1 }, "thaw of throw Foo on thaw died");
410 ok(!ref $@, "and a string thrown");
411 my $refdata = freeze($oref);
412 ok(!eval { thaw($refdata); 1 }, "thaw of throw [] on thaw died");
413 ok(ref $@, "and a ref thrown");