4 # Copyright 2002, Larry Wall.
6 # You may redistribute only under the same terms as Perl 5, as specified
7 # in the README file that comes with the distribution.
10 # I ought to keep this test easily backwards compatible to 5.004, so no
13 # This test checks downgrade behaviour on pre-5.8 perls when new 5.8 features
22 require Config; import Config;
23 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
24 print "1..0 # Skip: Storable was not built\n";
27 # require 'lib/st-dump.pl';
33 This test doesn't have EBCDIC data yet. Please run t/make_downgrade.pl using
34 perl 5.8 (or later) and append its output to the end of the test.
35 Please also mail the output to perlbug@perl.org so that the CPAN copy of
36 Storable can be updated.
44 use vars qw(@RESTRICT_TESTS %R_HASH %U_HASH $UTF8_CROAK $RESTRICTED_CROAK);
46 @RESTRICT_TESTS = ('Locked hash', 'Locked hash placeholder',
47 'Locked keys', 'Locked keys placeholder',
49 %R_HASH = (perl => 'rules');
52 my $utf8 = "Schlo\xdf" . chr 256;
55 %U_HASH = (map {$_, $_} 'castle', "ch\xe5teau", $utf8, chr 0x57CE);
57 } elsif ($] >= 5.006) {
63 $UTF8_CROAK = qr/^Cannot retrieve UTF8 data in non-UTF8 perl/;
64 $RESTRICTED_CROAK = qr/^Cannot retrieve restricted hash/;
68 local $/ = "\n\nend\n";
71 unless (/begin ([0-7]{3}) ([^\n]*)\n(.*)$/s) {
73 warn "Dodgy data in section starting '$_'";
76 next unless oct $1 == ord 'A'; # Skip ASCII on EBCDIC, and vice versa
77 my $data = unpack 'u', $3;
82 # use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper \%tests;
84 my ($name, $expected) = @_;
85 my $hash = eval {thaw $tests{$name}};
86 is ($@, '', "Thawed $name without error?");
87 isa_ok ($hash, 'HASH');
88 ok (defined $hash && eq_hash($hash, $expected),
89 "And it is the hash we expected?");
94 my ($name, $expected) = @_;
95 my $scalar = eval {thaw $tests{$name}};
96 is ($@, '', "Thawed $name without error?");
97 isa_ok ($scalar, 'SCALAR', "Thawed $name?");
98 is ($$scalar, $expected, "And it is the data we expected?");
103 my ($name, $expected) = @_;
104 my $thing = eval {thaw $tests{$name}};
105 is ($thing, undef, "Thawed $name failed as expected?");
106 like ($@, $expected, "Error as predicted?");
109 sub test_locked_hash {
111 my @keys = keys %$hash;
112 my ($key, $value) = each %$hash;
113 eval {$hash->{$key} = reverse $value};
114 like( $@, qr/^Modification of a read-only value attempted/,
115 'trying to change a locked key' );
116 is ($hash->{$key}, $value, "hash should not change?");
117 eval {$hash->{use} = 'perl'};
118 like( $@, qr/^Attempt to access disallowed key 'use' in a restricted hash/,
119 'trying to add another key' );
120 ok (eq_array([keys %$hash], \@keys), "Still the same keys?");
123 sub test_restricted_hash {
125 my @keys = keys %$hash;
126 my ($key, $value) = each %$hash;
127 eval {$hash->{$key} = reverse $value};
129 'trying to change a restricted key' );
130 is ($hash->{$key}, reverse ($value), "hash should change");
131 eval {$hash->{use} = 'perl'};
132 like( $@, qr/^Attempt to access disallowed key 'use' in a restricted hash/,
133 'trying to add another key' );
134 ok (eq_array([keys %$hash], \@keys), "Still the same keys?");
137 sub test_placeholder {
139 eval {$hash->{rules} = 42};
140 is ($@, '', 'No errors');
141 is ($hash->{rules}, 42, "New value added");
146 eval {$hash->{nms} = "http://nms-cgi.sourceforge.net/"};
147 is ($@, '', 'No errors');
148 is ($hash->{nms}, "http://nms-cgi.sourceforge.net/", "New value added");
151 # $Storable::DEBUGME = 1;
152 thaw_hash ('Hash with utf8 flag but no utf8 keys', \%R_HASH);
154 if (eval "use Hash::Util; 1") {
155 print "# We have Hash::Util, so test that the restricted hashes in <DATA> are valid\n";
156 for $Storable::downgrade_restricted (0, 1, undef, "cheese") {
157 my $hash = thaw_hash ('Locked hash', \%R_HASH);
158 test_locked_hash ($hash);
159 $hash = thaw_hash ('Locked hash placeholder', \%R_HASH);
160 test_locked_hash ($hash);
161 test_placeholder ($hash);
163 $hash = thaw_hash ('Locked keys', \%R_HASH);
164 test_restricted_hash ($hash);
165 $hash = thaw_hash ('Locked keys placeholder', \%R_HASH);
166 test_restricted_hash ($hash);
167 test_placeholder ($hash);
170 print "# We don't have Hash::Util, so test that the restricted hashes downgrade\n";
171 my $hash = thaw_hash ('Locked hash', \%R_HASH);
173 $hash = thaw_hash ('Locked hash placeholder', \%R_HASH);
175 $hash = thaw_hash ('Locked keys', \%R_HASH);
177 $hash = thaw_hash ('Locked keys placeholder', \%R_HASH);
179 local $Storable::downgrade_restricted = 0;
180 thaw_fail ('Locked hash', $RESTRICTED_CROAK);
181 thaw_fail ('Locked hash placeholder', $RESTRICTED_CROAK);
182 thaw_fail ('Locked keys', $RESTRICTED_CROAK);
183 thaw_fail ('Locked keys placeholder', $RESTRICTED_CROAK);
187 print "# We have utf8 scalars, so test that the utf8 scalars in <DATA> are valid\n";
188 print "# These seem to fail on 5.6 - you should seriously consider upgrading to 5.6.1\n" if $] == 5.006;
189 thaw_scalar ('Short 8 bit utf8 data', "\xDF");
190 thaw_scalar ('Long 8 bit utf8 data', "\xDF" x 256);
191 thaw_scalar ('Short 24 bit utf8 data', chr 0xC0FFEE);
192 thaw_scalar ('Long 24 bit utf8 data', chr (0xC0FFEE) x 256);
194 print "# We don't have utf8 scalars, so test that the utf8 scalars downgrade\n";
195 thaw_fail ('Short 8 bit utf8 data', $UTF8_CROAK);
196 thaw_fail ('Long 8 bit utf8 data', $UTF8_CROAK);
197 thaw_fail ('Short 24 bit utf8 data', $UTF8_CROAK);
198 thaw_fail ('Long 24 bit utf8 data', $UTF8_CROAK);
199 local $Storable::drop_utf8 = 1;
200 my $bytes = thaw $tests{'Short 8 bit utf8 data as bytes'};
201 thaw_scalar ('Short 8 bit utf8 data', $$bytes);
202 thaw_scalar ('Long 8 bit utf8 data', $$bytes x 256);
203 $bytes = thaw $tests{'Short 24 bit utf8 data as bytes'};
204 thaw_scalar ('Short 24 bit utf8 data', $$bytes);
205 thaw_scalar ('Long 24 bit utf8 data', $$bytes x 256);
208 if ($] >= 5.007003) {
209 print "# We have utf8 hashes, so test that the utf8 hashes in <DATA> are valid\n";
210 my $hash = thaw_hash ('Hash with utf8 keys', \%U_HASH);
213 my $r = 0 + $hash->{$_} =~ /^\w+$/;
214 cmp_ok ($l, '==', $r, sprintf "key length %d", length $_);
215 cmp_ok ($l, '==', $_ eq "ch\xe5teau" ? 0 : 1);
217 if (eval "use Hash::Util; 1") {
218 print "# We have Hash::Util, so test that the restricted utf8 hash is valid\n";
219 my $hash = thaw_hash ('Locked hash with utf8 keys', \%U_HASH);
222 my $r = 0 + $hash->{$_} =~ /^\w+$/;
223 cmp_ok ($l, '==', $r, sprintf "key length %d", length $_);
224 cmp_ok ($l, '==', $_ eq "ch\xe5teau" ? 0 : 1);
226 test_locked_hash ($hash);
228 print "# We don't have Hash::Util, so test that the utf8 hash downgrades\n";
229 fail ("You can't get here [perl version $]]. This is a bug in the test.
230 # Please send the output of perl -V to perlbug\@perl.org");
233 print "# We don't have utf8 hashes, so test that the utf8 hashes downgrade\n";
234 thaw_fail ('Hash with utf8 keys', $UTF8_CROAK);
235 thaw_fail ('Locked hash with utf8 keys', $UTF8_CROAK);
236 local $Storable::drop_utf8 = 1;
237 my $what = $] < 5.006 ? 'pre 5.6' : '5.6';
238 my $expect = thaw $tests{"Hash with utf8 keys for $what"};
239 thaw_hash ('Hash with utf8 keys', $expect);
240 #foreach (keys %$expect) { print "'$_':\t'$expect->{$_}'\n"; }
241 #foreach (keys %$got) { print "'$_':\t'$got->{$_}'\n"; }
242 if (eval "use Hash::Util; 1") {
243 print "# We have Hash::Util, so test that the restricted hashes in <DATA> are valid\n";
244 fail ("You can't get here [perl version $]]. This is a bug in the test.
245 # Please send the output of perl -V to perlbug\@perl.org");
247 print "# We don't have Hash::Util, so test that the restricted hashes downgrade\n";
248 my $hash = thaw_hash ('Locked hash with utf8 keys', $expect);
250 local $Storable::downgrade_restricted = 0;
251 thaw_fail ('Locked hash with utf8 keys', $RESTRICTED_CROAK);
252 # Which croak comes first is a bit of an implementation issue :-)
253 local $Storable::drop_utf8 = 0;
254 thaw_fail ('Locked hash with utf8 keys', $RESTRICTED_CROAK);
258 # A whole run of 2.x nfreeze data, uuencoded. The "mode bits" are the octal
259 # value of 'A', the "file name" is the test name. Use make_downgrade.pl to
261 begin 101 Locked hash
262 8!049`0````$*!7)U;&5S!`````1P97)L
266 begin 101 Locked hash placeholder
267 C!049`0````(*!7)U;&5S!`````1P97)L#A0````%<G5L97,`
271 begin 101 Locked keys
272 8!049`0````$*!7)U;&5S``````1P97)L
276 begin 101 Locked keys placeholder
277 C!049`0````(*!7)U;&5S``````1P97)L#A0````%<G5L97,`
281 begin 101 Short 8 bit utf8 data
286 begin 101 Short 8 bit utf8 data as bytes
291 begin 101 Long 8 bit utf8 data
292 M!048```"`,.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?
293 MPY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#
294 MG\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?
295 MPY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#
296 MG\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?
297 MPY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#
298 MG\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?
299 MPY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#
300 MG\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?
301 MPY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#
302 MG\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_#G\.?PY_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f8 flag but no utf8 keys
351 8!049``````$*!7)U;&5S``````1P97)L
355 begin 101 Hash with utf8 keys
356 M!049``````0*!F-A<W1L90`````&8V%S=&QE"@=C:.5T96%U``````=C:.5T
357 D96%U%P/EGXX!`````^6?CA<'4V-H;&_#GP(````&4V-H;&_?
361 begin 101 Locked hash with utf8 keys
362 M!049`0````0*!F-A<W1L900````&8V%S=&QE"@=C:.5T96%U!`````=C:.5T
363 D96%U%P/EGXX%`````^6?CA<'4V-H;&_#GP8````&4V-H;&_?
367 begin 101 Hash with utf8 keys for pre 5.6
368 M!049``````0*!F-A<W1L90`````&8V%S=&QE"@=C:.5T96%U``````=C:.5T
369 D96%U"@/EGXX``````^6?C@H'4V-H;&_#GP(````&4V-H;&_?
373 begin 101 Hash with utf8 keys for 5.6
374 M!049``````0*!F-A<W1L90`````&8V%S=&QE"@=C:.5T96%U``````=C:.5T
375 D96%U%P/EGXX``````^6?CA<'4V-H;&_#GP(````&4V-H;&_?