9 RT::Test::get_abs_relocatable_dir( File::Spec->updir(),
10 qw/data gnupg keyrings/ );
17 passphrase => 'rt-test',
21 use String::ShellQuote 'shell_quote';
24 my ($baseurl, $m) = RT::Test->started_ok;
26 # configure key for General queue
27 ok( $m->login, 'we did log in' );
28 $m->get( $baseurl.'/Admin/Queues/');
29 $m->follow_link_ok( {text => 'General'} );
30 $m->submit_form( form_number => 3,
31 fields => { CorrespondAddress => 'general@example.com' } );
32 $m->content_like(qr/general\@example.com.* - never/, 'has key info.');
34 ok(my $user = RT::User->new(RT->SystemUser));
35 ok($user->Load('root'), "Loaded user 'root'");
36 $user->SetEmailAddress('recipient@example.com');
38 # test simple mail. supposedly this should fail when
39 # 1. the queue requires signature
40 # 2. the from is not what the key is associated with
41 my $mail = RT::Test->open_mailgate_ok($baseurl);
43 From: recipient\@example.com
44 To: general\@$RT::rtname
45 Subject: This is a test of new ticket creation as root
50 RT::Test->close_mailgate_ok($mail);
53 my $tick = RT::Test->last_ticket;
55 'This is a test of new ticket creation as root',
58 my $txn = $tick->Transactions->First;
60 $txn->Attachments->First->Headers,
61 qr/^X-RT-Incoming-Encryption: Not encrypted/m,
62 'recorded incoming mail that is not encrypted'
64 like( $txn->Attachments->First->Content, qr/Blah/);
67 # test for signed mail
72 qw(gpg --batch --no-tty --armor --sign),
73 '--default-key' => 'recipient@example.com',
74 '--homedir' => $homedir,
75 '--passphrase' => 'recipient',
82 $mail = RT::Test->open_mailgate_ok($baseurl);
84 From: recipient\@example.com
85 To: general\@$RT::rtname
86 Subject: signed message for queue
90 RT::Test->close_mailgate_ok($mail);
93 my $tick = RT::Test->last_ticket;
94 is( $tick->Subject, 'signed message for queue',
98 my $txn = $tick->Transactions->First;
99 my ($msg, $attach) = @{$txn->Attachments->ItemsArrayRef};
101 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
103 'recorded incoming mail that is encrypted'
105 # test for some kind of PGP-Signed-By: Header
106 like( $attach->Content, qr/fnord/);
109 # test for clear-signed mail
114 qw(gpg --batch --no-tty --armor --sign --clearsign),
115 '--default-key' => 'recipient@example.com',
116 '--homedir' => $homedir,
117 '--passphrase' => 'recipient',
124 $mail = RT::Test->open_mailgate_ok($baseurl);
126 From: recipient\@example.com
127 To: general\@$RT::rtname
128 Subject: signed message for queue
132 RT::Test->close_mailgate_ok($mail);
135 my $tick = RT::Test->last_ticket;
136 is( $tick->Subject, 'signed message for queue',
140 my $txn = $tick->Transactions->First;
141 my ($msg, $attach) = @{$txn->Attachments->ItemsArrayRef};
142 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
144 'recorded incoming mail that is encrypted'
146 # test for some kind of PGP-Signed-By: Header
147 like( $attach->Content, qr/clearfnord/);
150 # test for signed and encrypted mail
155 qw(gpg --batch --no-tty --encrypt --armor --sign),
156 '--recipient' => 'general@example.com',
157 '--default-key' => 'recipient@example.com',
158 '--homedir' => $homedir,
159 '--passphrase' => 'recipient',
166 $mail = RT::Test->open_mailgate_ok($baseurl);
168 From: recipient\@example.com
169 To: general\@$RT::rtname
170 Subject: Encrypted message for queue
174 RT::Test->close_mailgate_ok($mail);
177 my $tick = RT::Test->last_ticket;
178 is( $tick->Subject, 'Encrypted message for queue',
182 my $txn = $tick->Transactions->First;
183 my ($msg, $attach, $orig) = @{$txn->Attachments->ItemsArrayRef};
185 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
187 'recorded incoming mail that is encrypted'
189 is( $msg->GetHeader('X-RT-Privacy'),
191 'recorded incoming mail that is encrypted'
193 like( $attach->Content, qr/orz/);
195 is( $orig->GetHeader('Content-Type'), 'application/x-rt-original-message');
196 ok(index($orig->Content, $buf) != -1, 'found original msg');
199 # test for signed mail by other key
204 qw(gpg --batch --no-tty --armor --sign),
205 '--default-key' => 'rt@example.com',
206 '--homedir' => $homedir,
207 '--passphrase' => 'test',
214 $mail = RT::Test->open_mailgate_ok($baseurl);
216 From: recipient\@example.com
217 To: general\@$RT::rtname
218 Subject: signed message for queue
222 RT::Test->close_mailgate_ok($mail);
225 my $tick = RT::Test->last_ticket;
226 my $txn = $tick->Transactions->First;
227 my ($msg, $attach) = @{$txn->Attachments->ItemsArrayRef};
228 # XXX: in this case, which credential should we be using?
229 is( $msg->GetHeader('X-RT-Incoming-Signature'),
230 'Test User <rt@example.com>',
231 'recorded incoming mail signed by others'
235 # test for encrypted mail with key not associated to the queue
240 qw(gpg --batch --no-tty --armor --encrypt),
241 '--recipient' => 'random@localhost',
242 '--homedir' => $homedir,
244 \"should not be there either\r\n",
249 $mail = RT::Test->open_mailgate_ok($baseurl);
251 From: recipient\@example.com
252 To: general\@$RT::rtname
253 Subject: encrypted message for queue
257 RT::Test->close_mailgate_ok($mail);
260 my $tick = RT::Test->last_ticket;
261 my $txn = $tick->Transactions->First;
262 my ($msg, $attach) = @{$txn->Attachments->ItemsArrayRef};
266 local $TODO = "this test requires keys associated with queues";
267 unlike( $attach->Content, qr/should not be there either/);
271 # test for badly encrypted mail
277 qw(gpg --batch --no-tty --armor --encrypt),
278 '--recipient' => 'rt@example.com',
279 '--homedir' => $homedir,
281 \"really should not be there either\r\n",
286 $buf =~ s/PGP MESSAGE/SCREWED UP/g;
288 RT::Test->fetch_caught_mails;
290 $mail = RT::Test->open_mailgate_ok($baseurl);
292 From: recipient\@example.com
293 To: general\@$RT::rtname
294 Subject: encrypted message for queue
298 RT::Test->close_mailgate_ok($mail);
299 my @mail = RT::Test->fetch_caught_mails;
300 is(@mail, 1, 'caught outgoing mail.');
304 my $tick = RT::Test->last_ticket;
305 my $txn = $tick->Transactions->First;
306 my ($msg, $attach) = @{$txn->Attachments->ItemsArrayRef};
307 unlike( ($attach ? $attach->Content : ''), qr/really should not be there either/);