xref: /openbsd/gnu/usr.bin/perl/dist/Storable/t/restrict.t (revision a6445c1d)
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 ($] < 5.005) {
20	    print "1..0 # Skip: No Hash::Util pre 5.005\n";
21	    exit 0;
22	    # And doing this seems on 5.004 seems to create bogus warnings about
23	    # uninitialized variables, or coredumps in Perl_pp_padsv
24	} elsif (!eval "require Hash::Util") {
25            if ($@ =~ /Can\'t locate Hash\/Util\.pm in \@INC/s) {
26                print "1..0 # Skip: No Hash::Util:\n";
27                exit 0;
28            } else {
29                die;
30            }
31        }
32	unshift @INC, 't';
33    }
34}
35
36
37use Storable qw(dclone freeze thaw);
38use Hash::Util qw(lock_hash unlock_value lock_keys);
39use Test::More tests => 304;
40
41my %hash = (question => '?', answer => 42, extra => 'junk', undef => undef);
42lock_hash %hash;
43unlock_value %hash, 'answer';
44unlock_value %hash, 'extra';
45delete $hash{'extra'};
46
47my $test;
48
49package Restrict_Test;
50
51sub me_second {
52  return (undef, $_[0]);
53}
54
55package main;
56
57sub freeze_thaw {
58  my $temp = freeze $_[0];
59  return thaw $temp;
60}
61
62sub testit {
63  my $hash = shift;
64  my $cloner = shift;
65  my $copy = &$cloner($hash);
66
67  my @in_keys = sort keys %$hash;
68  my @out_keys = sort keys %$copy;
69  is("@in_keys", "@out_keys", "keys match after deep clone");
70
71  # $copy = $hash;	# used in initial debug of the tests
72
73  is(Internals::SvREADONLY(%$copy), 1, "cloned hash restricted?");
74
75  is(Internals::SvREADONLY($copy->{question}), 1,
76     "key 'question' not locked in copy?");
77
78  is(Internals::SvREADONLY($copy->{answer}), '',
79     "key 'answer' not locked in copy?");
80
81  eval { $copy->{extra} = 15 } ;
82  is($@, '', "Can assign to reserved key 'extra'?");
83
84  eval { $copy->{nono} = 7 } ;
85  isnt($@, '', "Can not assign to invalid key 'nono'?");
86
87  is(exists $copy->{undef}, 1, "key 'undef' exists");
88
89  is($copy->{undef}, undef, "value for key 'undef' is undefined");
90}
91
92for $Storable::canonical (0, 1) {
93  for my $cloner (\&dclone, \&freeze_thaw) {
94    print "# \$Storable::canonical = $Storable::canonical\n";
95    testit (\%hash, $cloner);
96    my $object = \%hash;
97    # bless {}, "Restrict_Test";
98
99    my %hash2;
100    $hash2{"k$_"} = "v$_" for 0..16;
101    lock_hash %hash2;
102    for (0..16) {
103      unlock_value %hash2, "k$_";
104      delete $hash2{"k$_"};
105    }
106    my $copy = &$cloner(\%hash2);
107
108    for (0..16) {
109      my $k = "k$_";
110      eval { $copy->{$k} = undef } ;
111      is($@, '', "Can assign to reserved key '$k'?");
112    }
113
114    my %hv;
115    $hv{a} = __PACKAGE__;
116    lock_keys %hv;
117    my $hv2 = &$cloner(\%hv);
118    ok eval { $$hv2{a} = 70 }, 'COWs do not become read-only';
119  }
120}
121
122# [perl #73972]
123{
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