remove useless shebangs in tests
[gitmo/MooseX-Storage.git] / t / 030_with_checksum.t
index 1c72b95..18934f8 100644 (file)
@@ -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' );
@@ -44,7 +48,7 @@ BEGIN {
         $packed,
         {
             __CLASS__ => 'Foo',
-            checksum  => re('[0-9a-f]+'),
+            __DIGEST__  => re('[0-9a-f]+'),
             number    => 10,
             string    => 'foo',
             float     => 10.5,
@@ -52,7 +56,7 @@ BEGIN {
             hash      => { map { $_ => undef } ( 1 .. 10 ) },
             object    => { 
                             __CLASS__ => 'Foo', 
-                            checksum  => re('[0-9a-f]+'),               
+                            __DIGEST__  => re('[0-9a-f]+'),               
                             number    => 2 
                          },            
         },
@@ -60,16 +64,16 @@ BEGIN {
     );
 
     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,
@@ -77,12 +81,96 @@ 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)" } ) ), 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" );
+}