1#!./perl -w
2#
3#  Copyright 2002, Larry Wall.
4#  
5#  You may redistribute only under the same terms as Perl 5, as specified
6#  in the README file that comes with the distribution.
7#
8
9sub BEGIN {
10    unshift @INC, 't';
11    unshift @INC, 't/compat' if $] < 5.006002;
12    if ($ENV{PERL_CORE}){
13        require Config;
14        if ($Config::Config{'extensions'} !~ /\bStorable\b/) {
15            print "1..0 # Skip: Storable was not built\n";
16            exit 0;
17        }
18    } else {
19	if (!eval "require Hash::Util") {
20            if ($@ =~ /Can\'t locate Hash\/Util\.pm in \@INC/s) {
21                print "1..0 # Skip: No Hash::Util:\n";
22                exit 0;
23            } else {
24                die;
25            }
26        }
27	unshift @INC, 't';
28    }
29}
30
31
32use Storable qw(dclone freeze thaw);
33use Hash::Util qw(lock_hash unlock_value lock_keys);
34use Config;
35$Storable::DEBUGME = $ENV{STORABLE_DEBUGME};
36use Test::More tests => (!$Storable::DEBUGME && $Config{usecperl} ? 105 : 304);
37
38my %hash = (question => '?', answer => 42, extra => 'junk', undef => undef);
39lock_hash %hash;
40unlock_value %hash, 'answer';
41unlock_value %hash, 'extra';
42delete $hash{'extra'};
43
44my $test;
45
46package Restrict_Test;
47
48sub me_second {
49  return (undef, $_[0]);
50}
51
52package main;
53
54sub freeze_thaw {
55  my $temp = freeze $_[0];
56  return thaw $temp;
57}
58
59sub testit {
60  my $hash = shift;
61  my $cloner = shift;
62  my $copy = &$cloner($hash);
63
64  my @in_keys = sort keys %$hash;
65  my @out_keys = sort keys %$copy;
66  is("@in_keys", "@out_keys", "keys match after deep clone");
67
68  # $copy = $hash;	# used in initial debug of the tests
69
70  is(Internals::SvREADONLY(%$copy), 1, "cloned hash restricted?");
71
72  is(Internals::SvREADONLY($copy->{question}), 1,
73     "key 'question' not locked in copy?");
74
75  is(Internals::SvREADONLY($copy->{answer}), '',
76     "key 'answer' not locked in copy?");
77
78  eval { $copy->{extra} = 15 } ;
79  is($@, '', "Can assign to reserved key 'extra'?");
80
81  eval { $copy->{nono} = 7 } ;
82  isnt($@, '', "Can not assign to invalid key 'nono'?");
83
84  is(exists $copy->{undef}, 1, "key 'undef' exists");
85
86  is($copy->{undef}, undef, "value for key 'undef' is undefined");
87}
88
89for $Storable::canonical (0, 1) {
90  for my $cloner (\&dclone, \&freeze_thaw) {
91    print "# \$Storable::canonical = $Storable::canonical\n";
92    testit (\%hash, $cloner);
93    my $object = \%hash;
94    # bless {}, "Restrict_Test";
95
96    my %hash2;
97    $hash2{"k$_"} = "v$_" for 0..16;
98    lock_hash %hash2;
99    for (0..16) {
100      unlock_value %hash2, "k$_";
101      delete $hash2{"k$_"};
102    }
103    my $copy = &$cloner(\%hash2);
104
105    for (0..16) {
106      my $k = "k$_";
107      eval { $copy->{$k} = undef } ;
108      is($@, '', "Can assign to reserved key '$k'?");
109    }
110
111    my %hv;
112    $hv{a} = __PACKAGE__;
113    lock_keys %hv;
114    my $hv2 = &$cloner(\%hv);
115    ok eval { $$hv2{a} = 70 }, 'COWs do not become read-only';
116  }
117}
118
119# [perl #73972]
120# broken again with cperl PERL_PERTURB_KEYS_TOP.
121SKIP: {
122    skip "TODO restricted Storable hashes broken with PERL_PERTURB_KEYS_TOP", 1
123         if !$Storable::DEBUGME && $Config{usecperl};
124    for my $n (1..100) {
125        my @keys = map { "FOO$_" } (1..$n);
126
127        my $hash1 = {};
128        lock_keys(%$hash1, @keys);
129        my $hash2 = dclone($hash1);
130
131        my $success;
132
133        $success = eval { $hash2->{$_} = 'test' for @keys; 1 };
134        my $err = $@;
135        ok($success, "can store in all of the $n restricted slots")
136            || diag("failed with $@");
137
138        $success = !eval { $hash2->{a} = 'test'; 1 };
139        ok($success, "the hash is still restricted");
140    }
141}
142