package MooseX::Storage::Base::WithChecksum;
use Moose::Role;
-use Digest::MD5 ('md5_hex');
-use Data::Dumper ();
+use Digest ();
+use Storable ();
use MooseX::Storage::Engine;
our $VERSION = '0.01';
sub pack {
- my ($self, $salt) = @_;
+ my ($self, @args ) = @_;
+
my $e = MooseX::Storage::Engine->new( object => $self );
- my $collapsed = $e->collapse_object;
-
- # create checksum
-
- local $Data::Dumper::Sortkeys = 1;
- my $dumped = Data::Dumper::Dumper($collapsed);
- #warn $dumped;
-
- $salt ||= $dumped;
+ my $collapsed = $e->collapse_object;
- $collapsed->{checksum} = md5_hex($dumped, $salt);
+ $collapsed->{__DIGEST__} = $self->_digest_packed($collapsed, @args);
return $collapsed;
}
sub unpack {
- my ($class, $data, $salt) = @_;
+ my ($class, $data, @args) = @_;
# check checksum on data
- my $old_checksum = $data->{checksum};
- delete $data->{checksum};
-
- local $Data::Dumper::Sortkeys = 1;
- my $dumped = Data::Dumper::Dumper($data);
-
- #warn $dumped;
-
- $salt ||= $dumped;
-
- my $checksum = md5_hex($dumped, $salt);
+ my $old_checksum = $data->{__DIGEST__};
+ delete $data->{__DIGEST__};
+ my $checksum = $class->_digest_packed($data, @args);
+
($checksum eq $old_checksum)
- || confess "Bad Checksum got=($checksum) expected=($data->{checksum})";
+ || confess "Bad Checksum got=($checksum) expected=($old_checksum)";
my $e = MooseX::Storage::Engine->new(class => $class);
$class->new($e->expand_object($data));
}
+
+sub _digest_packed {
+ my ( $self, $collapsed, @args ) = @_;
+
+ my $d = shift @args;
+
+ if ( ref $d ) {
+ if ( $d->can("clone") ) {
+ $d = $d->clone;
+ } elsif ( $d->can("reset") ) {
+ $d->reset;
+ } else {
+ die "Can't clone or reset digest object: $d";
+ }
+ } else {
+ $d = Digest->new($d || "SHA1", @args);
+ }
+
+ {
+ local $Storable::canonical = 1;
+ $d->add( Storable::nfreeze($collapsed) );
+ }
+
+ return $d->hexdigest;
+}
+
+
1;
__END__
use strict;
use warnings;
-use Test::More tests => 6;
+use Test::More tests => 25;
use Test::Exception;
use Test::Deep;
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' );
$packed,
{
__CLASS__ => 'Foo',
- checksum => re('[0-9a-f]+'),
+ __DIGEST__ => re('[0-9a-f]+'),
number => 10,
string => 'foo',
float => 10.5,
hash => { map { $_ => undef } ( 1 .. 10 ) },
object => {
__CLASS__ => 'Foo',
- checksum => re('[0-9a-f]+'),
+ __DIGEST__ => re('[0-9a-f]+'),
number => 2
},
},
$foo2->pack,
{
__CLASS__ => 'Foo',
- checksum => re('[0-9a-f]+'),
+ __DIGEST__ => re('[0-9a-f]+'),
number => 10,
string => 'foo',
float => 10.5,
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" );
+}