convert all uses of Test::Exception to Test::Fatal.
[gitmo/MooseX-Storage.git] / t / 030_with_checksum.t
CommitLineData
c4a322ec 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
c86a46cc 6use Test::More;
9d3c60f5 7use Test::Fatal;
c4a322ec 8use Test::Deep;
9
0b173188 10use Test::Requires {
11 'Digest' => 0.01, # skip all if not installed
12 'Digest::SHA1' => 0.01,
13 'JSON::Any' => 0.01,
14};
15
c4a322ec 16BEGIN {
27444b02 17 plan tests => 26;
c4a322ec 18 use_ok('MooseX::Storage');
19}
20
21{
c4a322ec 22 package Foo;
23 use Moose;
24 use MooseX::Storage;
25
98ae09f0 26 with Storage(base => 'WithChecksum', format => "JSON");
c4a322ec 27
28 has 'number' => ( is => 'ro', isa => 'Int' );
29 has 'string' => ( is => 'ro', isa => 'Str' );
30 has 'float' => ( is => 'ro', isa => 'Num' );
31 has 'array' => ( is => 'ro', isa => 'ArrayRef' );
32 has 'hash' => ( is => 'ro', isa => 'HashRef' );
33 has 'object' => ( is => 'ro', isa => 'Foo' );
34}
35
36{
37 my $foo = Foo->new(
38 number => 10,
39 string => 'foo',
40 float => 10.5,
41 array => [ 1 .. 10 ],
42 hash => { map { $_ => undef } ( 1 .. 10 ) },
43 object => Foo->new( number => 2 ),
44 );
45 isa_ok( $foo, 'Foo' );
b5f363ac 46
c4a322ec 47 my $packed = $foo->pack;
b5f363ac 48
c4a322ec 49 cmp_deeply(
50 $packed,
51 {
52 __CLASS__ => 'Foo',
98ae09f0 53 __DIGEST__ => re('[0-9a-f]+'),
c4a322ec 54 number => 10,
55 string => 'foo',
56 float => 10.5,
57 array => [ 1 .. 10 ],
58 hash => { map { $_ => undef } ( 1 .. 10 ) },
b5f363ac 59 object => {
60 __CLASS__ => 'Foo',
61 __DIGEST__ => re('[0-9a-f]+'),
62 number => 2
63 },
c4a322ec 64 },
65 '... got the right frozen class'
66 );
67
68 my $foo2;
9d3c60f5 69 is( exception {
c4a322ec 70 $foo2 = Foo->unpack($packed);
9d3c60f5 71 }, undef, '... unpacked okay');
c4a322ec 72 isa_ok($foo2, 'Foo');
b5f363ac 73
c4a322ec 74 cmp_deeply(
75 $foo2->pack,
76 {
77 __CLASS__ => 'Foo',
98ae09f0 78 __DIGEST__ => re('[0-9a-f]+'),
c4a322ec 79 number => 10,
80 string => 'foo',
81 float => 10.5,
82 array => [ 1 .. 10 ],
83 hash => { map { $_ => undef } ( 1 .. 10 ) },
b5f363ac 84 object => {
85 __CLASS__ => 'Foo',
86 __DIGEST__ => re('[0-9a-f]+'),
87 number => 2
88 },
c4a322ec 89 },
90 '... got the right frozen class'
b5f363ac 91 );
c4a322ec 92}
93
98ae09f0 94{
95 my $foo = Foo->new(
96 number => 10,
97 string => 'foo',
98 float => 10.5,
99 array => [ 1 .. 10 ],
100 hash => { map { $_ => undef } ( 1 .. 10 ) },
101 object => Foo->new( number => 2 ),
102 );
103 isa_ok( $foo, 'Foo' );
104
105 my $frozen = $foo->freeze;
106
107 ok( length($frozen), "got frozen data" );
108
109 $frozen =~ s/foo/bar/;
110
111 my $foo2 = eval { Foo->thaw( $frozen ) };
112 my $e = $@;
113
114 ok( !$foo2, "not thawed" );
115 ok( $e, "has error" );
116 like( $e, qr/bad checksum/i, "bad checksum error" );
117}
118
119SKIP: {
120 eval { require Digest::HMAC_SHA1 };
4dcc2ca3 121 skip join( " ", "no Digest::HMAC", ( $@ =~ /\@INC/ ? () : do { chomp(my $e = $@); "($e)" } ) ), 15 if $@;
98ae09f0 122
34dcaa5d 123 local $::DEBUG = 1;
124
98ae09f0 125 my $foo = Foo->new(
126 number => 10,
127 string => 'foo',
128 float => 10.5,
129 array => [ 1 .. 10 ],
130 hash => { map { $_ => undef } ( 1 .. 10 ) },
131 object => Foo->new( number => 2 ),
132 );
133 isa_ok( $foo, 'Foo' );
134
a6ebb4c8 135 my $frozen1 = $foo->freeze( digest => [ "HMAC_SHA1", "secret" ] );
98ae09f0 136 ok( length($frozen1), "got frozen data" );
137
34dcaa5d 138 $::DEBUG = 0;
139
98ae09f0 140 my $d2 = Digest::HMAC_SHA1->new("s3cr3t");
141
a6ebb4c8 142 my $frozen2 = $foo->freeze( digest => $d2 );
98ae09f0 143 ok( length($frozen2), "got frozen data" );
144
145 cmp_ok( $frozen1, "ne", $frozen2, "versions are different" );
146
34dcaa5d 147 is( $frozen1, $foo->freeze( digest => [ HMAC_SHA1 => "secret" ] ), "refreeze" );
148
149$::DEBUG = 1;
150
a6ebb4c8 151 my $foo1 = eval { Foo->thaw( $frozen1, digest => [ "HMAC_SHA1", "secret" ] ) };
98ae09f0 152 my $e = $@;
153
154 ok( $foo1, "thawed" );
155 ok( !$e, "no error" ) || diag $e;
34dcaa5d 156
a6ebb4c8 157 my $foo2 = eval { Foo->thaw( $frozen2, digest => $d2 ) };
98ae09f0 158 $e = $@;
159
160 ok( $foo2, "thawed" );
161 ok( !$e, "no error" ) || diag $e;
162
a6ebb4c8 163 $foo1 = eval { Foo->thaw( $frozen1, digest => $d2 ) };
98ae09f0 164 $e = $@;
165
166 ok( !$foo1, "not thawed" );
167 ok( $e, "has error" );
168 like( $e, qr/bad checksum/i, "bad checksum error" );
169
170 $frozen1 =~ s/foo/bar/;
171
a6ebb4c8 172 $foo1 = eval { Foo->thaw( $frozen1, digest => [ "HMAC_SHA1", "secret" ] ) };
98ae09f0 173 $e = $@;
174
175 ok( !$foo1, "not thawed" );
176 ok( $e, "has error" );
177 like( $e, qr/bad checksum/i, "bad checksum error" );
178}