1#!./perl
2#
3#  Copyright (c) 1995-2000, Raphael Manfredi
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    require Config; import Config;
13    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
14        print "1..0 # Skip: Storable was not built\n";
15        exit 0;
16    }
17    require 'st-dump.pl';
18}
19
20# $Storable::DEBUGME = 1;
21use Storable qw(store retrieve store_fd nstore_fd fd_retrieve);
22
23use Test::More tests => 25;
24
25$a = 'toto';
26$b = \$a;
27$c = bless {}, CLASS;
28$c->{attribute} = 'attrval';
29%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$c);
30@a = ('first', undef, 3, -4, -3.14159, 456, 4.5,
31	$b, \$a, $a, $c, \$c, \%a);
32
33isnt(store(\@a, "store$$"), undef);
34
35$dumped = &dump(\@a);
36isnt($dumped, undef);
37
38$root = retrieve("store$$");
39isnt($root, undef);
40
41$got = &dump($root);
42isnt($got, undef);
43
44is($got, $dumped);
45
461 while unlink "store$$";
47
48package FOO; @ISA = qw(Storable);
49
50sub make {
51	my $self = bless {};
52	$self->{key} = \%main::a;
53	return $self;
54};
55
56package main;
57
58$foo = FOO->make;
59isnt($foo->store("store$$"), undef);
60
61isnt(open(OUT, '>>', "store$$"), undef);
62binmode OUT;
63
64isnt(store_fd(\@a, ::OUT), undef);
65isnt(nstore_fd($foo, ::OUT), undef);
66isnt(nstore_fd(\%a, ::OUT), undef);
67
68isnt(close(OUT), undef);
69
70isnt(open(OUT, "store$$"), undef);
71
72$r = fd_retrieve(::OUT);
73isnt($r, undef);
74is(&dump($r), &dump($foo));
75
76$r = fd_retrieve(::OUT);
77isnt($r, undef);
78is(&dump($r), &dump(\@a));
79
80$r = fd_retrieve(main::OUT);
81isnt($r, undef);
82is(&dump($r), &dump($foo));
83
84$r = fd_retrieve(::OUT);
85isnt($r, undef);
86is(&dump($r), &dump(\%a));
87
88eval { $r = fd_retrieve(::OUT); };
89isnt($@, '');
90
91{
92    my %test = (
93        old_retrieve_array => "\x70\x73\x74\x30\x01\x0a\x02\x02\x02\x02\x00\x3d\x08\x84\x08\x85\x08\x06\x04\x00\x00\x01\x1b",
94        old_retrieve_hash  => "\x70\x73\x74\x30\x01\x0a\x03\x00\xe8\x03\x00\x00\x81\x00\x00\x00\x01\x61",
95        retrieve_code      => "\x70\x73\x74\x30\x05\x0a\x19\xf0\x00\xff\xe8\x03\x1a\x0a\x0e\x01",
96    );
97
98    for my $k (sort keys %test) {
99        open my $fh, '<', \$test{$k};
100        eval { Storable::fd_retrieve($fh); };
101        is($?, 0, 'RT 130098:  no segfault in Storable::fd_retrieve()');
102    }
103}
104
105{
106
107    my $frozen =
108      "\x70\x73\x74\x30\x04\x0a\x08\x31\x32\x33\x34\x35\x36\x37\x38\x04\x08\x08\x08\x03\xff\x00\x00\x00\x19\x08\xff\x00\x00\x00\x08\x08\xf9\x16\x16\x13\x16\x10\x10\x10\xff\x15\x16\x16\x16\x1e\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x13\xf0\x16\x16\x16\xfe\x16\x41\x41\x41\x41\xe8\x03\x41\x41\x41\x41\x41\x41\x41\x41\x51\x41\xa9\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xb8\xac\xac\xac\xac\xac\xac\xac\xac\x9a\xac\xac\xac\xac\xac\xac\xac\xac\xac\x93\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\x00\x64\xac\xa8\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\x2c\xac\x41\x41\x41\x41\x41\x41\x41\x41\x41\x00\x80\x41\x80\x41\x41\x41\x41\x41\x41\x51\x41\xac\xac\xac";
109    open my $fh, '<', \$frozen;
110    eval { Storable::fd_retrieve($fh); };
111    pass('RT 130635:  no stack smashing error when retrieving hook');
112
113}
114
115close OUT or die "Could not close: $!";
116END { 1 while unlink "store$$" }
117