X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F030_with_checksum.t;h=720a633db5380ee459f9ea8462e411312fdaebbd;hb=98ae09f003486b7c70661507771598c6101f23be;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..720a633 100644 --- a/t/030_with_checksum.t +++ b/t/030_with_checksum.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 6; +use Test::More tests => 25; use Test::Exception; use Test::Deep; @@ -17,7 +17,7 @@ BEGIN { 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' ); @@ -44,7 +44,7 @@ BEGIN { $packed, { __CLASS__ => 'Foo', - checksum => re('[0-9a-f]+'), + __DIGEST__ => re('[0-9a-f]+'), number => 10, string => 'foo', float => 10.5, @@ -52,7 +52,7 @@ BEGIN { hash => { map { $_ => undef } ( 1 .. 10 ) }, object => { __CLASS__ => 'Foo', - checksum => re('[0-9a-f]+'), + __DIGEST__ => re('[0-9a-f]+'), number => 2 }, }, @@ -69,7 +69,7 @@ BEGIN { $foo2->pack, { __CLASS__ => 'Foo', - checksum => re('[0-9a-f]+'), + __DIGEST__ => re('[0-9a-f]+'), number => 10, string => 'foo', float => 10.5, @@ -77,12 +77,88 @@ BEGIN { hash => { map { $_ => undef } ( 1 .. 10 ) }, object => { __CLASS__ => 'Foo', - checksum => re('[0-9a-f]+'), + __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)" } ) ), 14 if $@; + + 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( "HMAC_SHA1", "secret" ); + ok( length($frozen1), "got frozen data" ); + + my $d2 = Digest::HMAC_SHA1->new("s3cr3t"); + + my $frozen2 = $foo->freeze( $d2 ); + ok( length($frozen2), "got frozen data" ); + + cmp_ok( $frozen1, "ne", $frozen2, "versions are different" ); + + my $foo1 = eval { Foo->thaw( $frozen1, "HMAC_SHA1", "secret" ) }; + my $e = $@; + + ok( $foo1, "thawed" ); + ok( !$e, "no error" ) || diag $e; + + my $foo2 = eval { Foo->thaw( $frozen2, $d2 ) }; + $e = $@; + + ok( $foo2, "thawed" ); + ok( !$e, "no error" ) || diag $e; + + $foo1 = eval { Foo->thaw( $frozen1, $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, "HMAC_SHA1", "secret" ) }; + $e = $@; + + ok( !$foo1, "not thawed" ); + ok( $e, "has error" ); + like( $e, qr/bad checksum/i, "bad checksum error" ); +}