3 # Copyright 2005, Adam Kennedy.
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
9 # Man, blessed.t scared the hell out of me. For a second there I thought
10 # I'd lose Test::More...
12 # This file tests several known-error cases relating to STORABLE_attach, in
13 # which Storable should (correctly) throw errors.
17 require Config; import Config;
18 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
19 print "1..0 # Skip: Storable was not built\n";
24 use Test::More tests => 35;
31 #####################################################################
34 # Classes that implement STORABLE_thaw _cannot_ have references
35 # returned by their STORABLE_freeze method. When they do, Storable
36 # should throw an exception
40 # Good Case - should not die
42 my $goodfreeze = bless {}, 'My::GoodFreeze';
45 $frozen = Storable::freeze( $goodfreeze );
47 ok( ! $@, 'Storable does not die when STORABLE_freeze does not return references' );
48 ok( $frozen, 'Storable freezes to a string successfully' );
50 package My::GoodFreeze;
53 my ($self, $clone) = @_;
55 # Illegally include a reference in this return
60 my ($class, $clone, $string) = @_;
61 return bless { }, 'My::GoodFreeze';
67 # Error Case - should die on freeze
69 my $badfreeze = bless {}, 'My::BadFreeze';
71 Storable::freeze( $badfreeze );
73 ok( $@, 'Storable dies correctly when STORABLE_freeze returns a referece' );
74 # Check for a unique substring of the error message
75 ok( $@ =~ /cannot return references/, 'Storable dies with the expected error' );
77 package My::BadFreeze;
80 my ($self, $clone) = @_;
82 # Illegally include a reference in this return
87 my ($class, $clone, $string) = @_;
88 return bless { }, 'My::BadFreeze';
96 #####################################################################
99 # If, for some reason, a STORABLE_attach object is accidentally stored
100 # with references, this should be checked and and error should be throw.
104 # Good Case - should not die
106 my $goodthaw = bless {}, 'My::GoodThaw';
109 $frozen = Storable::freeze( $goodthaw );
111 ok( $frozen, 'Storable freezes to a string as expected' );
113 Storable::thaw( $frozen );
115 isa_ok( $thawed, 'My::GoodThaw' );
116 is( $thawed->{foo}, 'bar', 'My::GoodThaw thawed correctly as expected' );
118 package My::GoodThaw;
120 sub STORABLE_freeze {
121 my ($self, $clone) = @_;
126 sub STORABLE_attach {
127 my ($class, $clone, $string) = @_;
128 return bless { 'foo' => 'bar' }, 'My::GoodThaw';
134 # Bad Case - should die on thaw
136 # Create the frozen string normally
137 my $badthaw = bless { }, 'My::BadThaw';
140 $frozen = Storable::freeze( $badthaw );
142 ok( $frozen, 'BadThaw was frozen with references correctly' );
144 # Set up the error condition by deleting the normal STORABLE_thaw,
145 # and creating a STORABLE_attach.
146 *My::BadThaw::STORABLE_attach = *My::BadThaw::STORABLE_thaw;
147 *My::BadThaw::STORABLE_attach = *My::BadThaw::STORABLE_thaw; # Suppress a warning
148 delete ${'My::BadThaw::'}{STORABLE_thaw};
150 # Trigger the error condition
153 $thawed = Storable::thaw( $frozen );
155 ok( $@, 'My::BadThaw object dies when thawing as expected' );
156 # Check for a snippet from the error message
157 ok( $@ =~ /unexpected references/, 'Dies with the expected error message' );
161 sub STORABLE_freeze {
162 my ($self, $clone) = @_;
167 # Start with no STORABLE_attach method so we can get a
168 # frozen object-containing-a-reference into the freeze string.
170 my ($class, $clone, $string) = @_;
171 return bless { 'foo' => 'bar' }, 'My::BadThaw';
178 #####################################################################
181 # Die if what is returned by STORABLE_attach is not something of that class
185 # Good Case - should not die
187 my $goodattach = bless { }, 'My::GoodAttach';
188 my $frozen = Storable::freeze( $goodattach );
189 ok( $frozen, 'My::GoodAttach return as expected' );
191 Storable::thaw( $frozen );
193 isa_ok( $thawed, 'My::GoodAttach' );
194 is( ref($thawed), 'My::GoodAttach::Subclass',
195 'The slightly-tricky good "returns a subclass" case returns as expected' );
197 package My::GoodAttach;
199 sub STORABLE_freeze {
200 my ($self, $cloning) = @_;
204 sub STORABLE_attach {
205 my ($class, $cloning, $string) = @_;
207 return bless { }, 'My::GoodAttach::Subclass';
210 package My::GoodAttach::Subclass;
213 @ISA = 'My::GoodAttach';
219 # Bad Cases - die on thaw
221 my $returnvalue = undef;
223 # Create and freeze the object
224 my $badattach = bless { }, 'My::BadAttach';
225 my $frozen = Storable::freeze( $badattach );
226 ok( $frozen, 'BadAttach freezes as expected' );
228 # Try a number of different return values, all of which
229 # should cause Storable to die.
239 foreach ( @badthings ) {
244 $thawed = Storable::thaw( $frozen );
246 ok( $@, 'BadAttach dies on thaw' );
247 ok( $@ =~ /STORABLE_attach did not return a My::BadAttach object/,
248 'BadAttach dies on thaw with the expected error message' );
249 is( $thawed, undef, 'Double checking $thawed was not set' );
252 package My::BadAttach;
254 sub STORABLE_freeze {
255 my ($self, $cloning) = @_;
259 sub STORABLE_attach {
260 my ($class, $cloning, $string) = @_;