Blame t/restrict.t

Packit 14c646
#!./perl -w
Packit 14c646
#
Packit 14c646
#  Copyright 2002, Larry Wall.
Packit 14c646
#  
Packit 14c646
#  You may redistribute only under the same terms as Perl 5, as specified
Packit 14c646
#  in the README file that comes with the distribution.
Packit 14c646
#
Packit 14c646
Packit 14c646
sub BEGIN {
Packit 14c646
    unshift @INC, 't';
Packit 14c646
    unshift @INC, 't/compat' if $] < 5.006002;
Packit 14c646
    if ($ENV{PERL_CORE}){
Packit 14c646
        require Config;
Packit 14c646
        if ($Config::Config{'extensions'} !~ /\bStorable\b/) {
Packit 14c646
            print "1..0 # Skip: Storable was not built\n";
Packit 14c646
            exit 0;
Packit 14c646
        }
Packit 14c646
    } else {
Packit 14c646
	if ($] < 5.005) {
Packit 14c646
	    print "1..0 # Skip: No Hash::Util pre 5.005\n";
Packit 14c646
	    exit 0;
Packit 14c646
	    # And doing this seems on 5.004 seems to create bogus warnings about
Packit 14c646
	    # uninitialized variables, or coredumps in Perl_pp_padsv
Packit 14c646
	} elsif (!eval "require Hash::Util") {
Packit 14c646
            if ($@ =~ /Can\'t locate Hash\/Util\.pm in \@INC/s) {
Packit 14c646
                print "1..0 # Skip: No Hash::Util:\n";
Packit 14c646
                exit 0;
Packit 14c646
            } else {
Packit 14c646
                die;
Packit 14c646
            }
Packit 14c646
        }
Packit 14c646
	unshift @INC, 't';
Packit 14c646
    }
Packit 14c646
}
Packit 14c646
Packit 14c646
Packit 14c646
use Storable qw(dclone freeze thaw);
Packit 14c646
use Hash::Util qw(lock_hash unlock_value lock_keys);
Packit 14c646
use Config;
Packit 14c646
$Storable::DEBUGME = $ENV{STORABLE_DEBUGME};
Packit 14c646
use Test::More tests => (!$Storable::DEBUGME && $Config{usecperl} ? 105 : 304);
Packit 14c646
Packit 14c646
my %hash = (question => '?', answer => 42, extra => 'junk', undef => undef);
Packit 14c646
lock_hash %hash;
Packit 14c646
unlock_value %hash, 'answer';
Packit 14c646
unlock_value %hash, 'extra';
Packit 14c646
delete $hash{'extra'};
Packit 14c646
Packit 14c646
my $test;
Packit 14c646
Packit 14c646
package Restrict_Test;
Packit 14c646
Packit 14c646
sub me_second {
Packit 14c646
  return (undef, $_[0]);
Packit 14c646
}
Packit 14c646
Packit 14c646
package main;
Packit 14c646
Packit 14c646
sub freeze_thaw {
Packit 14c646
  my $temp = freeze $_[0];
Packit 14c646
  return thaw $temp;
Packit 14c646
}
Packit 14c646
Packit 14c646
sub testit {
Packit 14c646
  my $hash = shift;
Packit 14c646
  my $cloner = shift;
Packit 14c646
  my $copy = &$cloner($hash);
Packit 14c646
Packit 14c646
  my @in_keys = sort keys %$hash;
Packit 14c646
  my @out_keys = sort keys %$copy;
Packit 14c646
  is("@in_keys", "@out_keys", "keys match after deep clone");
Packit 14c646
Packit 14c646
  # $copy = $hash;	# used in initial debug of the tests
Packit 14c646
Packit 14c646
  is(Internals::SvREADONLY(%$copy), 1, "cloned hash restricted?");
Packit 14c646
Packit 14c646
  is(Internals::SvREADONLY($copy->{question}), 1,
Packit 14c646
     "key 'question' not locked in copy?");
Packit 14c646
Packit 14c646
  is(Internals::SvREADONLY($copy->{answer}), '',
Packit 14c646
     "key 'answer' not locked in copy?");
Packit 14c646
Packit 14c646
  eval { $copy->{extra} = 15 } ;
Packit 14c646
  is($@, '', "Can assign to reserved key 'extra'?");
Packit 14c646
Packit 14c646
  eval { $copy->{nono} = 7 } ;
Packit 14c646
  isnt($@, '', "Can not assign to invalid key 'nono'?");
Packit 14c646
Packit 14c646
  is(exists $copy->{undef}, 1, "key 'undef' exists");
Packit 14c646
Packit 14c646
  is($copy->{undef}, undef, "value for key 'undef' is undefined");
Packit 14c646
}
Packit 14c646
Packit 14c646
for $Storable::canonical (0, 1) {
Packit 14c646
  for my $cloner (\&dclone, \&freeze_thaw) {
Packit 14c646
    print "# \$Storable::canonical = $Storable::canonical\n";
Packit 14c646
    testit (\%hash, $cloner);
Packit 14c646
    my $object = \%hash;
Packit 14c646
    # bless {}, "Restrict_Test";
Packit 14c646
Packit 14c646
    my %hash2;
Packit 14c646
    $hash2{"k$_"} = "v$_" for 0..16;
Packit 14c646
    lock_hash %hash2;
Packit 14c646
    for (0..16) {
Packit 14c646
      unlock_value %hash2, "k$_";
Packit 14c646
      delete $hash2{"k$_"};
Packit 14c646
    }
Packit 14c646
    my $copy = &$cloner(\%hash2);
Packit 14c646
Packit 14c646
    for (0..16) {
Packit 14c646
      my $k = "k$_";
Packit 14c646
      eval { $copy->{$k} = undef } ;
Packit 14c646
      is($@, '', "Can assign to reserved key '$k'?");
Packit 14c646
    }
Packit 14c646
Packit 14c646
    my %hv;
Packit 14c646
    $hv{a} = __PACKAGE__;
Packit 14c646
    lock_keys %hv;
Packit 14c646
    my $hv2 = &$cloner(\%hv);
Packit 14c646
    ok eval { $$hv2{a} = 70 }, 'COWs do not become read-only';
Packit 14c646
  }
Packit 14c646
}
Packit 14c646
Packit 14c646
# [perl #73972]
Packit 14c646
# broken again with cperl PERL_PERTURB_KEYS_TOP.
Packit 14c646
SKIP: {
Packit 14c646
    skip "TODO restricted Storable hashes broken with PERL_PERTURB_KEYS_TOP", 1
Packit 14c646
         if !$Storable::DEBUGME && $Config{usecperl};
Packit 14c646
    for my $n (1..100) {
Packit 14c646
        my @keys = map { "FOO$_" } (1..$n);
Packit 14c646
Packit 14c646
        my $hash1 = {};
Packit 14c646
        lock_keys(%$hash1, @keys);
Packit 14c646
        my $hash2 = dclone($hash1);
Packit 14c646
Packit 14c646
        my $success;
Packit 14c646
Packit 14c646
        $success = eval { $hash2->{$_} = 'test' for @keys; 1 };
Packit 14c646
        my $err = $@;
Packit 14c646
        ok($success, "can store in all of the $n restricted slots")
Packit 14c646
            || diag("failed with $@");
Packit 14c646
Packit 14c646
        $success = !eval { $hash2->{a} = 'test'; 1 };
Packit 14c646
        ok($success, "the hash is still restricted");
Packit 14c646
    }
Packit 14c646
}