diff options
Diffstat (limited to 'ext/Storable/t')
-rw-r--r-- | ext/Storable/t/malice.t | 48 | ||||
-rw-r--r-- | ext/Storable/t/testlib.pl | 38 | ||||
-rw-r--r-- | ext/Storable/t/weak.t | 147 |
3 files changed, 194 insertions, 39 deletions
diff --git a/ext/Storable/t/malice.t b/ext/Storable/t/malice.t index 955dcf1fa2..703ce47bd6 100644 --- a/ext/Storable/t/malice.t +++ b/ext/Storable/t/malice.t @@ -16,7 +16,7 @@ sub BEGIN { if ($ENV{PERL_CORE}){ chdir('t') if -d 't'; - @INC = ('.', '../lib'); + @INC = ('.', '../lib', '../ext/Storable/t'); } else { # This lets us distribute Test::More in t/ unshift @INC, 't'; @@ -38,8 +38,8 @@ $file_magic_str = 'pst0'; $other_magic = 7 + length $byteorder; $network_magic = 2; $major = 2; -$minor = 6; -$minor_write = $] > 5.007 ? 6 : 4; +$minor = 7; +$minor_write = $] > 5.005_50 ? 7 : 4; use Test::More; @@ -54,11 +54,8 @@ $fancy = ($] > 5.007 ? 2 : 0); plan tests => 368 + length ($byteorder) * 4 + $fancy * 8 + 1; 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': $!" }} +require 'testlib.pl'; +use vars '$file'; # The chr 256 is a hack to force the hash to always have the utf8 keys flag # set on 5.7.3 and later. Otherwise the test fails if run with -Mutf8 because @@ -97,22 +94,6 @@ sub test_header { } } -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) { @@ -229,7 +210,7 @@ sub test_things { $where = $file_magic + $network_magic; } - # Just the header and a tag 255. As 26 is currently the highest tag, this + # Just the header and a tag 255. As 28 is currently the highest tag, this # is "unexpected" $copy = substr ($contents, 0, $where) . chr 255; @@ -249,7 +230,7 @@ sub test_things { # local $Storable::DEBUGME = 1; # This is the delayed croak test_corrupt ($copy, $sub, - "/^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 26/", + "/^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/", "bogus tag, minor plus 4"); # And check again that this croak is not delayed: { @@ -261,17 +242,6 @@ sub test_things { } } -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 + $fancy; @@ -284,7 +254,7 @@ die "Expected file to be $expected bytes (sizeof long is $Config{longsize}) but unless $length == $expected; # Read the contents into memory: -my $contents = slurp $file; +my $contents = slurp ($file); # Test the original direct from disk my $clone = retrieve $file; @@ -312,7 +282,7 @@ die "Expected file to be $expected bytes (sizeof long is $Config{longsize}) but unless $length == $expected; # Read the contents into memory: -$contents = slurp $file; +$contents = slurp ($file); # Test the original direct from disk $clone = retrieve $file; diff --git a/ext/Storable/t/testlib.pl b/ext/Storable/t/testlib.pl new file mode 100644 index 0000000000..6d885d7f68 --- /dev/null +++ b/ext/Storable/t/testlib.pl @@ -0,0 +1,38 @@ +#!perl -w +use strict; +use vars '$file'; + +$file = "storable-testfile.$$"; +die "Temporary file '$file' already exists" if -e $file; + +END { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }} + +use Storable qw (store retrieve freeze thaw nstore nfreeze); + +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; +} + +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}; +} + +$file; diff --git a/ext/Storable/t/weak.t b/ext/Storable/t/weak.t new file mode 100644 index 0000000000..59e8e2b2c8 --- /dev/null +++ b/ext/Storable/t/weak.t @@ -0,0 +1,147 @@ +#!./perl -w +# +# Copyright 2004, Larry Wall. +# +# You may redistribute only under the same terms as Perl 5, as specified +# in the README file that comes with the distribution. +# + +sub BEGIN { + if ($ENV{PERL_CORE}){ + chdir('t') if -d 't'; + @INC = ('.', '../lib', '../ext/Storable/t'); + } else { + # This lets us distribute Test::More in t/ + unshift @INC, 't'; + } + require Config; import Config; + if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) { + print "1..0 # Skip: Storable was not built\n"; + exit 0; + } + if ($Config{extensions} !~ /\bList\/Util\b/) { + print "1..0 # Skip: List::Util was not built\n"; + exit 0; + } + + require Scalar::Util; + Scalar::Util->import qw(weaken isweak); + if (grep { /weaken/ } @Scalar::Util::EXPORT_FAIL) { + print("1..0 # Skip: No support for weaken in Scalar::Util\n"); + exit 0; + } +} + +use Test::More 'no_plan'; +use Storable qw (store retrieve freeze thaw nstore nfreeze); +require 'testlib.pl'; +use vars '$file'; +use strict; + +sub tester { + my ($contents, $sub, $testersub, $what) = @_; + # Test that if we re-write it, everything still works: + my $clone = &$sub ($contents); + is ($@, "", "There should be no error extracting for $what"); + &$testersub ($clone, $what); +} + +my $r = {}; +my $s1 = [$r, $r]; +weaken $s1->[1]; +ok (isweak($s1->[1]), "element 1 is a weak reference"); + +my $s0 = [$r, $r]; +weaken $s0->[0]; +ok (isweak($s0->[0]), "element 0 is a weak reference"); + +my $w = [$r]; +weaken $w->[0]; +ok (isweak($w->[0]), "element 0 is a weak reference"); + +package OVERLOADED; + +use overload + '""' => sub { $_[0][0] }; + +package main; + +$a = bless [77], 'OVERLOADED'; + +my $o = [$a, $a]; +weaken $o->[0]; +ok (isweak($o->[0]), "element 0 is a weak reference"); + +my @tests = ( +[$s1, + sub { + my ($clone, $what) = @_; + isa_ok($clone,'ARRAY'); + isa_ok($clone->[0],'HASH'); + isa_ok($clone->[1],'HASH'); + ok(!isweak $clone->[0], "Element 0 isn't weak"); + ok(isweak $clone->[1], "Element 1 is weak"); +} +], +# The weak reference needs to hang around long enough for other stuff to +# be able to make references to it. So try it second. +[$s0, + sub { + my ($clone, $what) = @_; + isa_ok($clone,'ARRAY'); + isa_ok($clone->[0],'HASH'); + isa_ok($clone->[1],'HASH'); + ok(isweak $clone->[0], "Element 0 is weak"); + ok(!isweak $clone->[1], "Element 1 isn't weak"); +} +], +[$w, + sub { + my ($clone, $what) = @_; + isa_ok($clone,'ARRAY'); + if ($what eq 'nothing') { + # We're the original, so we're still a weakref to a hash + isa_ok($clone->[0],'HASH'); + ok(isweak $clone->[0], "Element 0 is weak"); + } else { + is($clone->[0],undef); + } +} +], +[$o, +sub { + my ($clone, $what) = @_; + isa_ok($clone,'ARRAY'); + isa_ok($clone->[0],'OVERLOADED'); + isa_ok($clone->[1],'OVERLOADED'); + ok(isweak $clone->[0], "Element 0 is weak"); + ok(!isweak $clone->[1], "Element 1 isn't weak"); + is ("$clone->[0]", 77, "Element 0 stringifies to 77"); + is ("$clone->[1]", 77, "Element 1 stringifies to 77"); +} +], +); + +foreach (@tests) { + my ($input, $testsub) = @$_; + + tester($input, sub {return shift}, $testsub, 'nothing'); + + ok (defined store($input, $file)); + + # Read the contents into memory: + my $contents = slurp ($file); + + tester($contents, \&store_and_retrieve, $testsub, 'file'); + + # And now try almost everything again with a Storable string + my $stored = freeze $input; + tester($stored, \&freeze_and_thaw, $testsub, 'string'); + + ok (defined nstore($input, $file)); + + tester($contents, \&store_and_retrieve, $testsub, 'network file'); + + $stored = nfreeze $input; + tester($stored, \&freeze_and_thaw, $testsub, 'network string'); +} |