X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F030_with_checksum.t;h=0715831195f0f01fe45841b86b42ecb282902c29;hb=17cdf693cb985458ab6b8682b15b696d487b966d;hp=1c72b951499b7f6792501fb6ecf5fae7ec0b4e58;hpb=c4a322ec86aff913da11191ac43d3edfbc403ab5;p=gitmo%2FMooseX-Storage.git diff --git a/t/030_with_checksum.t b/t/030_with_checksum.t index 1c72b95..0715831 100644 --- a/t/030_with_checksum.t +++ b/t/030_with_checksum.t @@ -1,23 +1,27 @@ -#!/usr/bin/perl - use strict; use warnings; -use Test::More tests => 6; -use Test::Exception; +use Test::More; +use Test::Fatal; use Test::Deep; +use Test::Requires { + 'Digest' => 0.01, # skip all if not installed + 'Digest::SHA' => 0.00, + 'JSON::Any' => 0.01, +}; + BEGIN { + plan tests => 26; use_ok('MooseX::Storage'); } { - package Foo; use Moose; use MooseX::Storage; - with Storage(base => 'WithChecksum'); + with Storage(base => 'WithChecksum', format => "JSON"); has 'number' => ( is => 'ro', isa => 'Int' ); has 'string' => ( is => 'ro', isa => 'Str' ); @@ -37,52 +41,136 @@ BEGIN { object => Foo->new( number => 2 ), ); isa_ok( $foo, 'Foo' ); - + my $packed = $foo->pack; - + cmp_deeply( $packed, { __CLASS__ => 'Foo', - checksum => re('[0-9a-f]+'), + __DIGEST__ => re('[0-9a-f]+'), number => 10, string => 'foo', float => 10.5, array => [ 1 .. 10 ], hash => { map { $_ => undef } ( 1 .. 10 ) }, - object => { - __CLASS__ => 'Foo', - checksum => re('[0-9a-f]+'), - number => 2 - }, + object => { + __CLASS__ => 'Foo', + __DIGEST__ => re('[0-9a-f]+'), + number => 2 + }, }, '... got the right frozen class' ); my $foo2; - lives_ok { + is( exception { $foo2 = Foo->unpack($packed); - } '... unpacked okay'; + }, undef, '... unpacked okay'); isa_ok($foo2, 'Foo'); - + cmp_deeply( $foo2->pack, { __CLASS__ => 'Foo', - checksum => re('[0-9a-f]+'), + __DIGEST__ => re('[0-9a-f]+'), number => 10, string => 'foo', float => 10.5, array => [ 1 .. 10 ], hash => { map { $_ => undef } ( 1 .. 10 ) }, - object => { - __CLASS__ => 'Foo', - checksum => re('[0-9a-f]+'), - number => 2 - }, + object => { + __CLASS__ => 'Foo', + __DIGEST__ => re('[0-9a-f]+'), + number => 2 + }, }, '... got the right frozen class' - ); - + ); +} + +{ + my $foo = Foo->new( + number => 10, + string => 'foo', + float => 10.5, + array => [ 1 .. 10 ], + hash => { map { $_ => undef } ( 1 .. 10 ) }, + object => Foo->new( number => 2 ), + ); + isa_ok( $foo, 'Foo' ); + + my $frozen = $foo->freeze; + + ok( length($frozen), "got frozen data" ); + + $frozen =~ s/foo/bar/; + + my $foo2 = eval { Foo->thaw( $frozen ) }; + my $e = $@; + + ok( !$foo2, "not thawed" ); + ok( $e, "has error" ); + like( $e, qr/bad checksum/i, "bad checksum error" ); } +SKIP: { + eval { require Digest::HMAC_SHA1 }; + skip join( " ", "no Digest::HMAC", ( $@ =~ /\@INC/ ? () : do { chomp(my $e = $@); "($e)" } ) ), 15 if $@; + + local $::DEBUG = 1; + + my $foo = Foo->new( + number => 10, + string => 'foo', + float => 10.5, + array => [ 1 .. 10 ], + hash => { map { $_ => undef } ( 1 .. 10 ) }, + object => Foo->new( number => 2 ), + ); + isa_ok( $foo, 'Foo' ); + + my $frozen1 = $foo->freeze( digest => [ "HMAC_SHA1", "secret" ] ); + ok( length($frozen1), "got frozen data" ); + + $::DEBUG = 0; + + my $d2 = Digest::HMAC_SHA1->new("s3cr3t"); + + my $frozen2 = $foo->freeze( digest => $d2 ); + ok( length($frozen2), "got frozen data" ); + + cmp_ok( $frozen1, "ne", $frozen2, "versions are different" ); + + is( $frozen1, $foo->freeze( digest => [ HMAC_SHA1 => "secret" ] ), "refreeze" ); + +$::DEBUG = 1; + + my $foo1 = eval { Foo->thaw( $frozen1, digest => [ "HMAC_SHA1", "secret" ] ) }; + my $e = $@; + + ok( $foo1, "thawed" ); + ok( !$e, "no error" ) || diag $e; + + my $foo2 = eval { Foo->thaw( $frozen2, digest => $d2 ) }; + $e = $@; + + ok( $foo2, "thawed" ); + ok( !$e, "no error" ) || diag $e; + + $foo1 = eval { Foo->thaw( $frozen1, digest => $d2 ) }; + $e = $@; + + ok( !$foo1, "not thawed" ); + ok( $e, "has error" ); + like( $e, qr/bad checksum/i, "bad checksum error" ); + + $frozen1 =~ s/foo/bar/; + + $foo1 = eval { Foo->thaw( $frozen1, digest => [ "HMAC_SHA1", "secret" ] ) }; + $e = $@; + + ok( !$foo1, "not thawed" ); + ok( $e, "has error" ); + like( $e, qr/bad checksum/i, "bad checksum error" ); +}