X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=rt%2Ft%2Fmail%2Fcrypt-gnupg.t;h=567573e934d5336bcc973aebcfcdaef98053f9ee;hb=8c72aca69588468b2e5b35397e4d6fb3d543155e;hp=f33fbab1c489fcc0890876e587dffe570df1f481;hpb=b4b0c7e72d7eaee2fbfc7022022c9698323203dd;p=freeside.git diff --git a/rt/t/mail/crypt-gnupg.t b/rt/t/mail/crypt-gnupg.t index f33fbab1c..567573e93 100644 --- a/rt/t/mail/crypt-gnupg.t +++ b/rt/t/mail/crypt-gnupg.t @@ -1,47 +1,34 @@ -#!/usr/bin/perl use strict; use warnings; -use RT::Test nodata => 1, tests => 92; -plan skip_all => 'GnuPG required.' - unless eval 'use GnuPG::Interface; 1'; -plan skip_all => 'gpg executable is required.' - unless RT::Test->find_executable('gpg'); - - -use File::Spec (); -use Cwd; - -my $homedir = RT::Test::get_abs_relocatable_dir(File::Spec->updir(), - qw(data gnupg keyrings) ); +my $homedir; +BEGIN { + require RT::Test; + $homedir = + RT::Test::get_abs_relocatable_dir( File::Spec->updir(), + qw/data gnupg keyrings/ ); +} -mkdir $homedir; +use RT::Test::GnuPG tests => 100, gnupg_options => { homedir => $homedir }; +use Test::Warn; -use_ok('RT::Crypt::GnuPG'); +use_ok('RT::Crypt'); use_ok('MIME::Entity'); -RT->Config->Set( 'GnuPG', - Enable => 1, - OutgoingMessagesFormat => 'RFC' ); - -RT->Config->Set( 'GnuPGOptions', - homedir => $homedir, - 'no-permission-warning' => undef, -); - - -diag 'only signing. correct passphrase' if $ENV{'TEST_VERBOSE'}; +diag 'only signing. correct passphrase'; { my $entity = MIME::Entity->build( From => 'rt@example.com', Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'test' ); + my %res = RT::Crypt->SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'test' ); ok( $entity, 'signed entity'); ok( !$res{'logger'}, "log is here as well" ) or diag $res{'logger'}; - my @status = RT::Crypt::GnuPG::ParseStatus( $res{'status'} ); + my @status = RT::Crypt->ParseStatus( + Protocol => $res{'Protocol'}, Status => $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'); @@ -52,56 +39,78 @@ diag 'only signing. correct passphrase' if $ENV{'TEST_VERBOSE'}; ok( $entity->is_multipart, 'signed message is multipart' ); is( $entity->parts, 2, 'two parts' ); - my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity ); + my @parts = RT::Crypt->FindProtectedParts( Entity => $entity ); is( scalar @parts, 1, 'one protected part' ); 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" ); - my @res = RT::Crypt::GnuPG::VerifyDecrypt( Entity => $entity ); + my @res = RT::Crypt->VerifyDecrypt( Entity => $entity ); is scalar @res, 1, 'one operation'; - @status = RT::Crypt::GnuPG::ParseStatus( $res[0]{'status'} ); + @status = RT::Crypt->ParseStatus( + Protocol => $res[0]{'Protocol'}, Status => $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. missing passphrase' if $ENV{'TEST_VERBOSE'}; +diag 'only signing. missing passphrase'; { my $entity = MIME::Entity->build( From => 'rt@example.com', Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => '' ); + my %res; + warning_like { + %res = RT::Crypt->SignEncrypt( + Entity => $entity, + Encrypt => 0, + Passphrase => '' + ); + } qr/can't query passphrase in batch mode/; ok( $res{'exit_code'}, "couldn't sign without passphrase"); ok( $res{'error'} || $res{'logger'}, "error is here" ); - my @status = RT::Crypt::GnuPG::ParseStatus( $res{'status'} ); + my @status = RT::Crypt->ParseStatus( + Protocol => $res{'Protocol'}, Status => $res{'status'} + ); is( scalar @status, 1, 'one record'); is( $status[0]->{'Operation'}, 'PassphraseCheck', 'operation is correct'); is( $status[0]->{'Status'}, 'MISSING', 'missing passphrase'); } -diag 'only signing. wrong passphrase' if $ENV{'TEST_VERBOSE'}; +diag 'only signing. wrong passphrase'; { my $entity = MIME::Entity->build( From => 'rt@example.com', Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'wrong' ); + + my %res; + warning_like { + %res = RT::Crypt->SignEncrypt( + Entity => $entity, + Encrypt => 0, + Passphrase => 'wrong', + ); + } qr/bad passphrase/; + ok( $res{'exit_code'}, "couldn't sign with bad passphrase"); ok( $res{'error'} || $res{'logger'}, "error is here" ); - my @status = RT::Crypt::GnuPG::ParseStatus( $res{'status'} ); + my @status = RT::Crypt->ParseStatus( + Protocol => $res{'Protocol'}, Status => $res{'status'} + ); is( scalar @status, 1, 'one record'); is( $status[0]->{'Operation'}, 'PassphraseCheck', 'operation is correct'); is( $status[0]->{'Status'}, 'BAD', 'wrong passphrase'); } -diag 'encryption only' if $ENV{'TEST_VERBOSE'}; +diag 'encryption only'; { my $entity = MIME::Entity->build( From => 'rt@example.com', @@ -109,25 +118,27 @@ diag 'encryption only' if $ENV{'TEST_VERBOSE'}; Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 ); + my %res = RT::Crypt->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'} ); + my @status = RT::Crypt->ParseStatus( + Protocol => $res{'Protocol'}, Status => $res{'status'} + ); is( scalar @status, 1, 'one record'); is( $status[0]->{'Operation'}, 'Encrypt', 'operation is correct'); is( $status[0]->{'Status'}, 'DONE', 'done'); ok($entity, 'get an encrypted part'); - my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity ); + my @parts = RT::Crypt->FindProtectedParts( Entity => $entity ); 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, "it's the same entity" ); } -diag 'encryption only, bad recipient' if $ENV{'TEST_VERBOSE'}; +diag 'encryption only, bad recipient'; { my $entity = MIME::Entity->build( From => 'rt@example.com', @@ -135,16 +146,26 @@ diag 'encryption only, bad recipient' if $ENV{'TEST_VERBOSE'}; Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 ); + + my %res; + warning_like { + %res = RT::Crypt->SignEncrypt( + Entity => $entity, + Sign => 0, + ); + } qr/public key not found/; + 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'} ); + my @status = RT::Crypt->ParseStatus( + Protocol => $res{'Protocol'}, Status => $res{'status'} + ); is( scalar @status, 1, 'one record'); is( $status[0]->{'Keyword'}, 'INV_RECP', 'invalid recipient'); } -diag 'encryption and signing with combined method' if $ENV{'TEST_VERBOSE'}; +diag 'encryption and signing with combined method'; { my $entity = MIME::Entity->build( From => 'rt@example.com', @@ -152,11 +173,13 @@ diag 'encryption and signing with combined method' if $ENV{'TEST_VERBOSE'}; Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Passphrase => 'test' ); + my %res = RT::Crypt->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'} ); + my @status = RT::Crypt->ParseStatus( + Protocol => $res{'Protocol'}, Status => $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'); @@ -167,14 +190,14 @@ diag 'encryption and signing with combined method' if $ENV{'TEST_VERBOSE'}; ok($entity, 'get an encrypted and signed part'); - my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity ); + my @parts = RT::Crypt->FindProtectedParts( Entity => $entity ); 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, "it's the same entity" ); } -diag 'encryption and signing with cascading, sign on encrypted' if $ENV{'TEST_VERBOSE'}; +diag 'encryption and signing with cascading, sign on encrypted'; { my $entity = MIME::Entity->build( From => 'rt@example.com', @@ -182,21 +205,21 @@ diag 'encryption and signing with cascading, sign on encrypted' if $ENV{'TEST_VE Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 ); + my %res = RT::Crypt->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' ); + %res = RT::Crypt->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 ); + my @parts = RT::Crypt->FindProtectedParts( Entity => $entity ); 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" ); } -diag 'find signed/encrypted part deep inside' if $ENV{'TEST_VERBOSE'}; +diag 'find signed/encrypted part deep inside'; { my $entity = MIME::Entity->build( From => 'rt@example.com', @@ -204,7 +227,7 @@ diag 'find signed/encrypted part deep inside' if $ENV{'TEST_VERBOSE'}; Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 ); + my %res = RT::Crypt->SignEncrypt( Entity => $entity, Sign => 0 ); ok( !$res{'exit_code'}, "success" ); $entity->make_multipart( 'mixed', Force => 1 ); $entity->attach( @@ -212,14 +235,14 @@ diag 'find signed/encrypted part deep inside' if $ENV{'TEST_VERBOSE'}; Data => ['-'x76, 'this is mailing list'], ); - my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity ); + my @parts = RT::Crypt->FindProtectedParts( Entity => $entity ); 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" ); } -diag 'wrong signed/encrypted parts: no protocol' if $ENV{'TEST_VERBOSE'}; +diag 'wrong signed/encrypted parts: no protocol'; { my $entity = MIME::Entity->build( From => 'rt@example.com', @@ -227,15 +250,25 @@ diag 'wrong signed/encrypted parts: no protocol' if $ENV{'TEST_VERBOSE'}; Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 ); + + my %res = RT::Crypt->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 ); - is( scalar @parts, 0, 'no protected parts' ); + my @parts; + warning_like { @parts = RT::Crypt->FindProtectedParts( Entity => $entity ) } + qr{Entity is 'multipart/encrypted', but has no protocol defined. Checking for PGP part}; + 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, "it's the same entity" ); } -diag 'wrong signed/encrypted parts: not enought parts' if $ENV{'TEST_VERBOSE'}; +diag 'wrong signed/encrypted parts: not enought parts'; { my $entity = MIME::Entity->build( From => 'rt@example.com', @@ -243,15 +276,23 @@ diag 'wrong signed/encrypted parts: not enought parts' if $ENV{'TEST_VERBOSE'}; Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 ); + + my %res = RT::Crypt->SignEncrypt( + Entity => $entity, + Sign => 0, + ); + ok( !$res{'exit_code'}, 'success' ); $entity->parts([ $entity->parts(0) ]); - my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity ); + my @parts; + warning_like { + @parts = RT::Crypt->FindProtectedParts( Entity => $entity ); + } qr/Encrypted or signed entity must has two subparts. Skipped/; is( scalar @parts, 0, 'no protected parts' ); } -diag 'wrong signed/encrypted parts: wrong proto' if $ENV{'TEST_VERBOSE'}; +diag 'wrong signed/encrypted parts: wrong proto'; { my $entity = MIME::Entity->build( From => 'rt@example.com', @@ -259,15 +300,15 @@ diag 'wrong signed/encrypted parts: wrong proto' if $ENV{'TEST_VERBOSE'}; Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Sign => 0 ); + my %res = RT::Crypt->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 ); + my @parts = RT::Crypt->FindProtectedParts( Entity => $entity ); is( scalar @parts, 0, 'no protected parts' ); } -diag 'wrong signed/encrypted parts: wrong proto' if $ENV{'TEST_VERBOSE'}; +diag 'wrong signed/encrypted parts: wrong proto'; { my $entity = MIME::Entity->build( From => 'rt@example.com', @@ -275,21 +316,21 @@ diag 'wrong signed/encrypted parts: wrong proto' if $ENV{'TEST_VERBOSE'}; Subject => 'test', Data => ['test'], ); - my %res = RT::Crypt::GnuPG::SignEncrypt( Entity => $entity, Encrypt => 0, Passphrase => 'test' ); + my %res = RT::Crypt->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 ); + my @parts = RT::Crypt->FindProtectedParts( Entity => $entity ); is( scalar @parts, 0, 'no protected parts' ); } -diag 'verify inline and in attachment signatures' if $ENV{'TEST_VERBOSE'}; +diag 'verify inline and in attachment signatures'; { - open my $fh, "$homedir/signed_old_style_with_attachment.eml"; + open( my $fh, '<', "$homedir/signed_old_style_with_attachment.eml" ) or die $!; my $parser = new MIME::Parser; my $entity = $parser->parse( $fh ); - my @parts = RT::Crypt::GnuPG::FindProtectedParts( Entity => $entity ); + my @parts = RT::Crypt->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" ); @@ -300,8 +341,10 @@ diag 'verify inline and in attachment signatures' if $ENV{'TEST_VERBOSE'}; 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'} ); + my @res = RT::Crypt->VerifyDecrypt( Entity => $entity ); + my @status = RT::Crypt->ParseStatus( + Protocol => $res[0]{'Protocol'}, Status => $res[0]{'status'} + ); is( scalar @status, 1, 'one record'); is( $status[0]->{'Operation'}, 'Verify', 'operation is correct'); is( $status[0]->{'Status'}, 'DONE', 'good passphrase');