[Rt-commit] r6728 - in rt/branches/3.7-EXPERIMENTAL: . lib/RT/Crypt

ruz at bestpractical.com ruz at bestpractical.com
Tue Jan 9 15:56:55 EST 2007


Author: ruz
Date: Tue Jan  9 15:56:35 2007
New Revision: 6728

Modified:
   rt/branches/3.7-EXPERIMENTAL/   (props changed)
   rt/branches/3.7-EXPERIMENTAL/lib/RT/Crypt/GnuPG.pm
   rt/branches/3.7-EXPERIMENTAL/lib/t/regression/06-crypt-gnupg.t

Log:
 r4333 at cubic-pc:  cubic | 2007-01-09 23:59:30 +0300
 * update tests according to the latest changes
 * fix signature detaching bug


Modified: rt/branches/3.7-EXPERIMENTAL/lib/RT/Crypt/GnuPG.pm
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/lib/RT/Crypt/GnuPG.pm	(original)
+++ rt/branches/3.7-EXPERIMENTAL/lib/RT/Crypt/GnuPG.pm	Tue Jan  9 15:56:35 2007
@@ -51,8 +51,6 @@
     );
     my $entity = $args{'Entity'};
 
-    local @ENV{'LANG', 'LC_ALL'} = ('C', 'C');
-
     my $gnupg = new GnuPG::Interface;
     my %opt = RT->Config->Get('GnuPG');
     $opt{'digest-algo'} ||= 'SHA1';
@@ -79,6 +77,8 @@
 
         eval {
             local $SIG{'CHLD'} = 'DEFAULT';
+            local @ENV{'LANG', 'LC_ALL'} = ('C', 'C');
+
             my $pid = $gnupg->detach_sign( handles => $handles );
             $entity->make_multipart( 'mixed', Force => 1 );
             $entity->parts(0)->print( $handle{'input'} );
@@ -100,7 +100,7 @@
         $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
         if ( $@ || $? ) {
             $res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
-            return (undef, \%res);
+            return %res;
         }
 
         # setup RFC1847(Ch.2.1) requirements
@@ -138,6 +138,8 @@
 
         eval {
             local $SIG{'CHLD'} = 'DEFAULT';
+            local @ENV{'LANG', 'LC_ALL'} = ('C', 'C');
+
             my $pid = $args{'Sign'}?
                 $gnupg->sign_and_encrypt( handles => $handles ):
                 $gnupg->encrypt( handles => $handles );
@@ -158,7 +160,7 @@
         $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
         if ( $@ || $? ) {
             $res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
-            return (undef, \%res);
+            return %res;
         }
 
         my $protocol = 'application/pgp-encrypted';
@@ -180,7 +182,7 @@
         );
         $entity->parts(-1)->bodyhandle->{'_dirty_hack_to_save_a_ref_tmp_fh'} = $tmp_fh;
     }
-    return ($entity, \%res);
+    %res;
 }
 
 sub FindProtectedParts {
@@ -291,7 +293,7 @@
         if ( $item->{'Format'} eq 'RFC3156' ) {
             push @res, { VerifyRFC3156( %$item ) };
             if ( $args{'Detach'} ) {
-                $item->{'Top'}->parts( [ $item->{'Body'} ] );
+                $item->{'Top'}->parts( [ $item->{'Data'} ] );
                 $item->{'Top'}->make_singlepart;
             }
         } elsif ( $item->{'Format'} eq 'Inline' ) {
@@ -317,7 +319,7 @@
 #            }
         }
     }
-    return @res[0];
+    return @res;
 }
 
 sub VerifyInline {

Modified: rt/branches/3.7-EXPERIMENTAL/lib/t/regression/06-crypt-gnupg.t
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/lib/t/regression/06-crypt-gnupg.t	(original)
+++ rt/branches/3.7-EXPERIMENTAL/lib/t/regression/06-crypt-gnupg.t	Tue Jan  9 15:56:35 2007
@@ -1,12 +1,13 @@
 #!/usr/bin/perl
 use strict;
 use warnings;
-use Test::More tests => 84;
+use Test::More tests => 95;
 
 use_ok("RT");
 
 RT::LoadConfig();
 RT->Config->Set( LogToScreen => 'debug' );
+RT->Config->Set( LogStackTraces => 'error' );
 use Data::Dumper;
 
 RT::Init();
@@ -25,41 +26,15 @@
 
 diag 'only signing. correct passphrase' if $ENV{'TEST_VERBOSE'};
 {
-    open my $fh, "$homedir/signed_old_style_with_attachment.eml";
-    my $parser = new MIME::Parser;
-    my $entity = $parser->parse( $fh );
-
-    my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity );
-    is( scalar @parts, 2, 'two protected parts' );
-    is( $parts[1]->{'Type'}, 'signed', "have signed part" );
-    is( $parts[1]->{'Format'}, 'Inline', "inline format" );
-    is( $parts[1]->{'Data'}, $entity->parts(0), "it's first part" );
-
-    is( $parts[0]->{'Type'}, 'signed', "have signed part" );
-    is( $parts[0]->{'Format'}, 'Attachment', "attachment format" );
-    is( $parts[0]->{'Data'}, $entity->parts(1), "data in second part" );
-    is( $parts[0]->{'Signature'}, $entity->parts(2), "file's signature in third part" );
-
-    my @res = RT::Crypt::GnuPG::VerifyDecrypt( Entity => $entity );
-    my @status = RT::Crypt::GnuPG::ParseStatus( $res[0]->{'status'} );
-    is( scalar @status, 1, 'one record');
-    is( $status[0]->{'Operation'}, 'Verify', 'operation is correct');
-    is( $status[0]->{'Status'}, 'DONE', 'good passphrase');
-    is( $status[0]->{'Trust'}, 'ULTIMATE', 'have trust value');
-}
-
-diag 'only signing. correct passphrase' if $ENV{'TEST_VERBOSE'};
-{
     my $entity = MIME::Entity->build(
         From    => 'rt at example.com',
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'test' );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'test' );
     ok( $entity, 'signed entity');
-    ok( !$res->{'logger'}, "log is here as well" );
-    my @status = RT::Crypt::GnuPG::ParseStatus( $res->{'status'} );
+    ok( !$res{'logger'}, "log is here as well" );
+    my @status = RT::Crypt::GnuPG::ParseStatus( $res{'status'} );
     is( scalar @status, 2, 'two records: passphrase, signing');
     is( $status[0]->{'Operation'}, 'PassphraseCheck', 'operation is correct');
     is( $status[0]->{'Status'}, 'DONE', 'good passphrase');
@@ -76,8 +51,9 @@
     is( $parts[0]->{'Format'}, 'RFC3156', "RFC3156 format" );
     is( $parts[0]->{'Top'}, $entity, "it's the same entity" );
 
-    ($entity, $res) = RT::Crypt::GnuPG::Verify( Entity => $entity );
-    @status = RT::Crypt::GnuPG::ParseStatus( $res->{'status'} );
+    my @res = RT::Crypt::GnuPG::VerifyDecrypt( Entity => $entity );
+    is scalar @res, 1, 'one operation';
+    @status = RT::Crypt::GnuPG::ParseStatus( $res[0]{'status'} );
     is( scalar @status, 1, 'one record');
     is( $status[0]->{'Operation'}, 'Verify', 'operation is correct');
     is( $status[0]->{'Status'}, 'DONE', 'good passphrase');
@@ -91,12 +67,12 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0 );
-    ok(!$entity, "couldn't sign without passphrase");
-    ok( $res->{'error'}, "error is here" );
-    ok( $res->{'logger'}, "log is here as well" );
-    my @status = RT::Crypt::GnuPG::ParseStatus( $res->{'status'} );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0 );
+    ok( $res{'exit_code'}, "couldn't sign without passphrase");
+    ok( $res{'error'}, "error is here" );
+    ok( $res{'logger'}, "log is here as well" );
+
+    my @status = RT::Crypt::GnuPG::ParseStatus( $res{'status'} );
     is( scalar @status, 1, 'one record');
     is( $status[0]->{'Operation'}, 'PassphraseCheck', 'operation is correct');
     is( $status[0]->{'Status'}, 'MISSING', 'missing passphrase');
@@ -109,12 +85,12 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'wrong' );
-    ok(!$entity, "couldn't sign with bad passphrase");
-    ok( $res->{'error'}, "error is here" );
-    ok( $res->{'logger'}, "log is here as well" );
-    my @status = RT::Crypt::GnuPG::ParseStatus( $res->{'status'} );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'wrong' );
+    ok( $res{'exit_code'}, "couldn't sign with bad passphrase");
+    ok( $res{'error'}, "error is here" );
+    ok( $res{'logger'}, "log is here as well" );
+
+    my @status = RT::Crypt::GnuPG::ParseStatus( $res{'status'} );
     is( scalar @status, 1, 'one record');
     is( $status[0]->{'Operation'}, 'PassphraseCheck', 'operation is correct');
     is( $status[0]->{'Status'}, 'BAD', 'wrong passphrase');
@@ -128,10 +104,11 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
-    ok( !$res->{'logger'}, "no records in logger" );
-    my @status = RT::Crypt::GnuPG::ParseStatus( $res->{'status'} );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
+    ok( !$res{'exit_code'}, "successful encryption" );
+    ok( !$res{'logger'}, "no records in logger" );
+
+    my @status = RT::Crypt::GnuPG::ParseStatus( $res{'status'} );
     is( scalar @status, 1, 'one record');
     is( $status[0]->{'Operation'}, 'Encrypt', 'operation is correct');
     is( $status[0]->{'Status'}, 'DONE', 'done');
@@ -153,11 +130,11 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
-    ok( !$entity, 'no way to encrypt without all keys of recipients');
-    ok( $res->{'logger'}, "errors are in logger" );
-    my @status = RT::Crypt::GnuPG::ParseStatus( $res->{'status'} );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
+    ok( $res{'exit_code'}, 'no way to encrypt without keys of recipients');
+    ok( $res{'logger'}, "errors are in logger" );
+
+    my @status = RT::Crypt::GnuPG::ParseStatus( $res{'status'} );
     is( scalar @status, 1, 'one record');
     is( $status[0]->{'Keyword'}, 'INV_RECP', 'invalid recipient');
 }
@@ -170,10 +147,11 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Passphrase => 'test' );
-    ok( !$res->{'logger'}, "no records in logger" );
-    my @status = RT::Crypt::GnuPG::ParseStatus( $res->{'status'} );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Passphrase => 'test' );
+    ok( !$res{'exit_code'}, "successful encryption with signing" );
+    ok( !$res{'logger'}, "no records in logger" );
+
+    my @status = RT::Crypt::GnuPG::ParseStatus( $res{'status'} );
     is( scalar @status, 3, 'three records: passphrase, sign and encrypt');
     is( $status[0]->{'Operation'}, 'PassphraseCheck', 'operation is correct');
     is( $status[0]->{'Status'}, 'DONE', 'done');
@@ -199,22 +177,18 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
-    ok( $entity, 'get an encrypted entity' );
-    ok( !$res->{'logger'}, "no records in logger" );
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'test' );
-    ok( $entity, 'get an signed entity' );
-    ok( !$res->{'logger'}, "no records in logger" );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
+    ok( !$res{'exit_code'}, 'successful encryption' );
+    ok( !$res{'logger'}, "no records in logger" );
+    %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'test' );
+    ok( !$res{'exit_code'}, 'successful signing' );
+    ok( !$res{'logger'}, "no records in logger" );
 
     my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity );
-    is( scalar @parts, 2, 'two protected parts' );
+    is( scalar @parts, 1, 'one protected part, top most' );
     is( $parts[0]->{'Type'}, 'signed', "have signed part" );
     is( $parts[0]->{'Format'}, 'RFC3156', "RFC3156 format" );
     is( $parts[0]->{'Top'}, $entity, "it's the same entity" );
-    is( $parts[1]->{'Type'}, 'encrypted', "have encrypted part" );
-    is( $parts[1]->{'Format'}, 'RFC3156', "RFC3156 format" );
-    is( $parts[1]->{'Top'}, $entity->parts(0), "it's the same entity" );
 }
 
 diag 'find signed/encrypted part deep inside' if $ENV{'TEST_VERBOSE'};
@@ -225,18 +199,16 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
-    ok( $entity, 'get an encrypted entity' );
-    ok( !$res->{'logger'}, "no records in logger" );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
+    ok( !$res{'exit_code'}, "success" );
     $entity->make_multipart( 'mixed', Force => 1 );
     $entity->attach(
         Type => 'text/plain',
-        Data => ['-'x76, 'this is fucking mailing list'],
+        Data => ['-'x76, 'this is mailing list'],
     );
 
     my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity );
-    is( scalar @parts, 1, 'two protected parts' );
+    is( scalar @parts, 1, 'one protected part' );
     is( $parts[0]->{'Type'}, 'encrypted', "have encrypted part" );
     is( $parts[0]->{'Format'}, 'RFC3156', "RFC3156 format" );
     is( $parts[0]->{'Top'}, $entity->parts(0), "it's the same entity" );
@@ -250,9 +222,8 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
-    ok( $entity, 'get an encrypted entity' );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
+    ok( !$res{'exit_code'}, 'success' );
     $entity->head->mime_attr( 'Content-Type.protocol' => undef );
 
     my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity );
@@ -267,9 +238,8 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
-    ok( $entity, 'get an encrypted entity' );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
+    ok( !$res{'exit_code'}, 'success' );
     $entity->parts([ $entity->parts(0) ]);
 
     my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity );
@@ -284,9 +254,8 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
-    ok( $entity, 'get an encrypted entity' );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 );
+    ok( !$res{'exit_code'}, 'success' );
     $entity->head->mime_attr( 'Content-Type.protocol' => 'application/bad-proto' );
 
     my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity );
@@ -301,11 +270,36 @@
         Subject => 'test',
         Data    => ['test'],
     );
-    my $res;
-    ($entity, $res) = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'test' );
-    ok( $entity, 'get an encrypted entity' );
+    my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'test' );
+    ok( !$res{'exit_code'}, 'success' );
     $entity->head->mime_attr( 'Content-Type.protocol' => 'application/bad-proto' );
 
     my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity );
     is( scalar @parts, 0, 'no protected parts' );
 }
+
+diag 'verify inline and in attachment signatures' if $ENV{'TEST_VERBOSE'};
+{
+    open my $fh, "$homedir/signed_old_style_with_attachment.eml";
+    my $parser = new MIME::Parser;
+    my $entity = $parser->parse( $fh );
+
+    my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity );
+    is( scalar @parts, 2, 'two protected parts' );
+    is( $parts[1]->{'Type'}, 'signed', "have signed part" );
+    is( $parts[1]->{'Format'}, 'Inline', "inline format" );
+    is( $parts[1]->{'Data'}, $entity->parts(0), "it's first part" );
+
+    is( $parts[0]->{'Type'}, 'signed', "have signed part" );
+    is( $parts[0]->{'Format'}, 'Attachment', "attachment format" );
+    is( $parts[0]->{'Data'}, $entity->parts(1), "data in second part" );
+    is( $parts[0]->{'Signature'}, $entity->parts(2), "file's signature in third part" );
+
+    my @res = RT::Crypt::GnuPG::VerifyDecrypt( Entity => $entity );
+    my @status = RT::Crypt::GnuPG::ParseStatus( $res[0]->{'status'} );
+    is( scalar @status, 1, 'one record');
+    is( $status[0]->{'Operation'}, 'Verify', 'operation is correct');
+    is( $status[0]->{'Status'}, 'DONE', 'good passphrase');
+    is( $status[0]->{'Trust'}, 'ULTIMATE', 'have trust value');
+}
+


More information about the Rt-commit mailing list