File: 400_evil.t

package info (click to toggle)
libsereal-decoder-perl 5.004%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,556 kB
  • sloc: ansic: 11,615; perl: 6,938; sh: 25; makefile: 9
file content (215 lines) | stat: -rw-r--r-- 6,622 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!perl
use strict;
use warnings;
use Data::Dumper;
use File::Spec;
use Scalar::Util qw(blessed);

# These tests use an installed Decoder to do testing on horrific
# Perl data structures such as overloaded and tied structures.

use lib File::Spec->catdir(qw(t lib));

BEGIN {
    lib->import('lib')
        if !-d 't';
}

use Sereal::TestSet qw(:all);
use Test::More;

if ( not have_encoder_and_decoder() ) {
    plan skip_all => 'Did not find right version of decoder';
    exit 0;
}

Sereal::Encoder->import(":all");
Sereal::Decoder->import(":all");

# First, test tied hashes. Expected behaviour: We don't segfault, we don't
# throw exceptions (unless the tied hash is not iterable repeatedly),
# we serialize the tied hash as if it was a normal hash - so no trace of
# tiedness in the output.
{
    SCOPE: {

        package TiedHash;
        require Tie::Hash;
        our @ISA= qw(Tie::StdHash);
    }

    my %testhash= (
        foo        => [qw(a b c)],
        baz        => 123,
        dfvgbnhmjk => "345ty6ujh",
        a          => undef,
    );

    my %tied_hash;
    tie %tied_hash => 'TiedHash';
    %{ tied(%tied_hash) }= %testhash;
    is_deeply( \%tied_hash, \%testhash );

    my ( $out, $ok, $err, $data );
    $ok= eval { $out= encode_sereal( \%tied_hash ); 1 };
    $err= $@ || 'Zombie error';
    ok( $ok, "serializing tied hash did not die" )
        or note("Error was '$err'");
    ok( defined $out, "serializing tied hash returns string" );

    $ok= eval { $data= decode_sereal($out); 1; };
    $err= $@ || 'Zombie error';
    ok( $ok, "deserializing tied hash did not die" )
        or note("Error was '$err', data was:\n"), hobodecode($out);
    ok( defined $data, "deserializing tied hash yields defined output" );
    is_deeply( $data, \%testhash, "deserializing tied hash yields expected output" );
}

# Now tied arrays.
{
    SCOPE: {

        package TiedArray;
        require Tie::Array;
        our @ISA= qw(Tie::StdArray);
    }

    my @testarray= ( 1, 2, "foo", "bar", [] );
    my @tied_array;
    tie @tied_array => 'TiedArray';
    @{ tied(@tied_array) }= @testarray;
    is_deeply( \@tied_array, \@testarray );

    my ( $out, $ok, $err, $data );
    $ok= eval { $out= encode_sereal( \@tied_array ); 1 };
    $err= $@ || 'Zombie error';
    ok( $ok, "serializing tied array did not die" )
        or note("Error was '$err'");
    ok( defined $out, "serializing tied array returns string" );

    $ok= eval { $data= decode_sereal($out); 1; };
    $err= $@ || 'Zombie error';
    ok( $ok, "deserializing tied array did not die" )
        or note("Error was '$err', data was:\n"), hobodecode($out);
    ok( defined $data, "deserializing tied array yields defined output" );
    is_deeply( $data, \@testarray, "deserializing tied array yields expected output" );
}

# Now tied scalars.
{

    SCOPE: {

        package TiedScalar;
        require Tie::Scalar;
        our @ISA= qw(Tie::StdScalar);
    }

    my $testscalar= [qw(foo bar baz)];
    my $tied_scalar;
    tie $tied_scalar => 'TiedScalar';
    ${ tied($tied_scalar) }= $testscalar;
    is_deeply( $tied_scalar, $testscalar );

    my ( $out, $ok, $err, $data );
    $ok= eval { $out= encode_sereal( \$tied_scalar ); 1 };
    $err= $@ || 'Zombie error';
    ok( $ok, "serializing tied scalar did not die" )
        or note("Error was '$err'");
    ok( defined $out, "serializing tied scalar returns string" );

    $ok= eval { $data= decode_sereal($out); 1; };
    $err= $@ || 'Zombie error';
    ok( $ok, "deserializing tied scalar did not die" )
        or note("Error was '$err', data was:\n"), hobodecode($out);
    ok( defined $data, "deserializing tied scalar yields defined output" );
    is_deeply( $data, \$testscalar, "deserializing tied scalar yields expected output" );
}

# Now test re-entrancy. DO NOT DO THIS AT HOME!
SCOPE: {
    my $enc= Sereal::Encoder->new;
    my $die_run= 0;
    eval {
        local $SIG{__DIE__}= sub {
            $die_run++;
            ok( defined( $enc->encode("foo") ), "encode does not segfault" );
            $die_run++;
        };
        $enc->encode( [ "foo", sub { } ] );
    };
    ok( $die_run == 2, "__DIE__ called, encode 2 did not die ($die_run)" );
}

# github Sereal/Sereal issue 7 regression test:
SCOPE: {
    {
        package    # hide from PAUSE
            Blessed::Sub::With::Overload;
        use overload '""' => sub { shift->() };
        sub new { bless $_[1] => $_[0] }
    }
    {
        package    # hide from PAUSE
            Blessed::Sub::With::Lazy::Overload;
        use overload '""' => sub {
            my ($self)= @_;
            return $self->[1] if defined $self->[1];
            return "OH NOES WE DON'T HAVE A SUB" unless ref $self->[0] eq 'CODE';
            return ( $self->[1]= $self->[0]->() );
        };

        sub new {
            bless [
                # The callback
                $_[1],

                # Cached value
                undef
            ] => $_[0];
        }
    }
    my $data;
    $data->[0]= sub { };
    $data->[1]= $data->[0];
    $data->[2]= Blessed::Sub::With::Overload->new( sub { "hello there" } );
    $data->[3]= $data->[2];
    $data->[4]= Blessed::Sub::With::Overload->new( sub { \"hello there" } );
    $data->[5]= $data->[4];
    my $called;
    $data->[6]= Blessed::Sub::With::Overload->new( sub { $called++; "hello there" } );
    $data->[7]= $data->[6];
    $data->[8]= $data->[6];
    $data->[9]= $data->[6];
    $data->[10]= Blessed::Sub::With::Lazy::Overload->new( sub { "hello there" } );
    $data->[11]= $data->[10];

    my $encode= encode_sereal( $data, { stringify_unknown => 1 } );

    # Before 48d5cdc3dc07fd29ac7be05678a0b614244fec4f, we'd
    # die here because $data->[1] is a ref to something that doesn't exist anymore
    my $decode= decode_sereal($encode);

    is( $decode->[0], $decode->[1] );
    is( $decode->[2], $decode->[3] );
    is( $decode->[4], $decode->[5] );
    is( $decode->[6], $decode->[$_] ) for 7 .. 9;
    is( $called, 4, "We'll call the sub every time, and won't re-use the initial return value" );
    ok( blessed( $decode->[10] ), "We won't be stringifying objects" );
    like(
        $decode->[10]->[0], qr/^CODE\(.*?\)$/,
        "And the subroutine we have will just be stringified as usual in Perl"
    );
    is(
        "$decode->[10]", "OH NOES WE DON'T HAVE A SUB",
        "So our subroutine won't survive the roundtrip, our object is broken"
    );
    is_deeply(
        $decode->[10], $decode->[11],
        "Both the original and the reference to it are equally screwed"
    );
}

pass("Alive at end");
done_testing();