5 use RT::Test tests => 39;
7 plan skip_all => 'GnuPG required.'
8 unless eval 'use GnuPG::Interface; 1';
9 plan skip_all => 'gpg executable is required.'
10 unless RT::Test->find_executable('gpg');
15 use String::ShellQuote 'shell_quote';
18 my $homedir = RT::Test::get_abs_relocatable_dir(File::Spec->updir(),
19 qw(data gnupg keyrings));
21 # catch any outgoing emails
22 RT::Test->set_mail_catcher;
24 RT->Config->Set( 'GnuPG',
26 OutgoingMessagesFormat => 'RFC' );
28 RT->Config->Set( 'GnuPGOptions',
30 'no-permission-warning' => undef);
32 RT->Config->Set( 'MailPlugins' => 'Auth::MailFrom', 'Auth::GnuPG' );
34 my ($baseurl, $m) = RT::Test->started_ok;
36 # configure key for General queue
37 ok( $m->login, 'we did log in' );
38 $m->get( $baseurl.'/Admin/Queues/');
39 $m->follow_link_ok( {text => 'General'} );
40 $m->submit_form( form_number => 3,
41 fields => { CorrespondAddress => 'general@example.com' } );
42 $m->content_like(qr/general\@example.com.* - never/, 'has key info.');
44 ok(my $user = RT::User->new($RT::SystemUser));
45 ok($user->Load('root'), "Loaded user 'root'");
46 $user->SetEmailAddress('recipient@example.com');
48 # test simple mail. supposedly this should fail when
49 # 1. the queue requires signature
50 # 2. the from is not what the key is associated with
51 my $mail = RT::Test->open_mailgate_ok($baseurl);
53 From: recipient\@example.com
54 To: general\@$RT::rtname
55 Subject: This is a test of new ticket creation as root
60 RT::Test->close_mailgate_ok($mail);
63 my $tick = RT::Test->last_ticket;
65 'This is a test of new ticket creation as root',
68 my $txn = $tick->Transactions->First;
70 $txn->Attachments->First->Headers,
71 qr/^X-RT-Incoming-Encryption: Not encrypted/m,
72 'recorded incoming mail that is not encrypted'
74 like( $txn->Attachments->First->Content, qr'Blah');
77 # test for signed mail
82 qw(gpg --armor --sign),
83 '--default-key' => 'recipient@example.com',
84 '--homedir' => $homedir,
85 '--passphrase' => 'recipient',
92 $mail = RT::Test->open_mailgate_ok($baseurl);
94 From: recipient\@example.com
95 To: general\@$RT::rtname
96 Subject: signed message for queue
100 RT::Test->close_mailgate_ok($mail);
103 my $tick = RT::Test->last_ticket;
104 is( $tick->Subject, 'signed message for queue',
108 my $txn = $tick->Transactions->First;
109 my ($msg, $attach) = @{$txn->Attachments->ItemsArrayRef};
111 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
113 'recorded incoming mail that is encrypted'
115 # test for some kind of PGP-Signed-By: Header
116 like( $attach->Content, qr'fnord');
119 # test for clear-signed mail
124 qw(gpg --armor --sign --clearsign),
125 '--default-key' => 'recipient@example.com',
126 '--homedir' => $homedir,
127 '--passphrase' => 'recipient',
134 $mail = RT::Test->open_mailgate_ok($baseurl);
136 From: recipient\@example.com
137 To: general\@$RT::rtname
138 Subject: signed message for queue
142 RT::Test->close_mailgate_ok($mail);
145 my $tick = RT::Test->last_ticket;
146 is( $tick->Subject, 'signed message for queue',
150 my $txn = $tick->Transactions->First;
151 my ($msg, $attach) = @{$txn->Attachments->ItemsArrayRef};
152 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
154 'recorded incoming mail that is encrypted'
156 # test for some kind of PGP-Signed-By: Header
157 like( $attach->Content, qr'clearfnord');
160 # test for signed and encrypted mail
165 qw(gpg --encrypt --armor --sign),
166 '--recipient' => 'general@example.com',
167 '--default-key' => 'recipient@example.com',
168 '--homedir' => $homedir,
169 '--passphrase' => 'recipient',
176 $mail = RT::Test->open_mailgate_ok($baseurl);
178 From: recipient\@example.com
179 To: general\@$RT::rtname
180 Subject: Encrypted message for queue
184 RT::Test->close_mailgate_ok($mail);
187 my $tick = RT::Test->last_ticket;
188 is( $tick->Subject, 'Encrypted message for queue',
192 my $txn = $tick->Transactions->First;
193 my ($msg, $attach, $orig) = @{$txn->Attachments->ItemsArrayRef};
195 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
197 'recorded incoming mail that is encrypted'
199 is( $msg->GetHeader('X-RT-Privacy'),
201 'recorded incoming mail that is encrypted'
203 like( $attach->Content, qr'orz');
205 is( $orig->GetHeader('Content-Type'), 'application/x-rt-original-message');
206 ok(index($orig->Content, $buf) != -1, 'found original msg');
209 # test for signed mail by other key
214 qw(gpg --armor --sign),
215 '--default-key' => 'rt@example.com',
216 '--homedir' => $homedir,
217 '--passphrase' => 'test',
224 $mail = RT::Test->open_mailgate_ok($baseurl);
226 From: recipient\@example.com
227 To: general\@$RT::rtname
228 Subject: signed message for queue
232 RT::Test->close_mailgate_ok($mail);
235 my $tick = RT::Test->last_ticket;
236 my $txn = $tick->Transactions->First;
237 my ($msg, $attach) = @{$txn->Attachments->ItemsArrayRef};
238 # XXX: in this case, which credential should we be using?
239 is( $msg->GetHeader('X-RT-Incoming-Signature'),
240 'Test User <rt@example.com>',
241 'recorded incoming mail signed by others'
245 # test for encrypted mail with key not associated to the queue
250 qw(gpg --armor --encrypt),
251 '--recipient' => 'random@localhost',
252 '--homedir' => $homedir,
254 \"should not be there either\r\n",
259 $mail = RT::Test->open_mailgate_ok($baseurl);
261 From: recipient\@example.com
262 To: general\@$RT::rtname
263 Subject: encrypted message for queue
267 RT::Test->close_mailgate_ok($mail);
270 my $tick = RT::Test->last_ticket;
271 my $txn = $tick->Transactions->First;
272 my ($msg, $attach) = @{$txn->Attachments->ItemsArrayRef};
276 local $TODO = "this test requires keys associated with queues";
277 unlike( $attach->Content, qr'should not be there either');
281 # test for badly encrypted mail
287 qw(gpg --armor --encrypt),
288 '--recipient' => 'rt@example.com',
289 '--homedir' => $homedir,
291 \"really should not be there either\r\n",
296 $buf =~ s/PGP MESSAGE/SCREWED UP/g;
298 RT::Test->fetch_caught_mails;
300 $mail = RT::Test->open_mailgate_ok($baseurl);
302 From: recipient\@example.com
303 To: general\@$RT::rtname
304 Subject: encrypted message for queue
308 RT::Test->close_mailgate_ok($mail);
309 my @mail = RT::Test->fetch_caught_mails;
310 is(@mail, 1, 'caught outgoing mail.');
314 my $tick = RT::Test->last_ticket;
315 my $txn = $tick->Transactions->First;
316 my ($msg, $attach) = @{$txn->Attachments->ItemsArrayRef};
317 unlike( ($attach ? $attach->Content : ''), qr'really should not be there either');