RT 4.2.11, ticket#13852
[freeside.git] / rt / t / mail / smime / incoming.t
1 use strict;
2 use warnings;
3
4 use RT::Test::SMIME tests => undef, actual_server => 1;
5 my $test = 'RT::Test::SMIME';
6
7 use IPC::Run3 'run3';
8 use String::ShellQuote 'shell_quote';
9 use RT::Tickets;
10 use Test::Warn;
11
12 my ($url, $m) = RT::Test->started_ok;
13 ok $m->login, "logged in";
14
15 # configure key for General queue
16 RT::Test::SMIME->import_key('sender@example.com');
17 my $queue = RT::Test->load_or_create_queue(
18     Name              => 'General',
19     CorrespondAddress => 'sender@example.com',
20     CommentAddress    => 'sender@example.com',
21 );
22 ok $queue && $queue->id, 'loaded or created queue';
23
24 my $user = RT::Test->load_or_create_user(
25     Name => 'root@example.com',
26     EmailAddress => 'root@example.com',
27 );
28 RT::Test::SMIME->import_key('root@example.com.crt', $user);
29 RT::Test->add_rights( Principal => $user, Right => 'SuperUser', Object => RT->System );
30
31 my $mail = RT::Test->open_mailgate_ok($url);
32 print $mail <<EOF;
33 From: root\@localhost
34 To: rt\@$RT::rtname
35 Subject: This is a test of new ticket creation as root
36
37 Blah!
38 Foob!
39 EOF
40 RT::Test->close_mailgate_ok($mail);
41
42 {
43     my $tick = RT::Test->last_ticket;
44     is( $tick->Subject,
45         'This is a test of new ticket creation as root',
46         "Created the ticket"
47     );
48     my $txn = $tick->Transactions->First;
49     like(
50         $txn->Attachments->First->Headers,
51         qr/^X-RT-Incoming-Encryption: Not encrypted/m,
52         'recorded incoming mail that is not encrypted'
53     );
54     like( $txn->Attachments->First->Content, qr'Blah');
55 }
56
57 {
58     # test for encrypted mail
59     my $buf = '';
60     run3(
61         shell_quote(
62             qw(openssl smime -encrypt  -des3),
63             -from    => 'root@example.com',
64             -to      => 'sender@example.com',
65             -subject => "Encrypted message for queue",
66             $test->key_path('sender@example.com.crt'),
67         ),
68         \"Subject: test\n\norzzzzzz",
69         \$buf,
70         \*STDERR
71     );
72
73     my ($status, $tid) = RT::Test->send_via_mailgate( $buf );
74     is ($status >> 8, 0, "The mail gateway exited normally");
75
76     my $tick = RT::Ticket->new( $RT::SystemUser );
77     $tick->Load( $tid );
78     is( $tick->Subject, 'Encrypted message for queue',
79         "Created the ticket"
80     );
81
82     my $txn = $tick->Transactions->First;
83     my ($msg, $attach, $orig) = @{$txn->Attachments->ItemsArrayRef};
84     is( $msg->GetHeader('X-RT-Incoming-Encryption'),
85         'Success',
86         'recorded incoming mail that is encrypted'
87     );
88     is( $msg->GetHeader('X-RT-Privacy'),
89         'SMIME',
90         'recorded incoming mail that is encrypted'
91     );
92     like( $attach->Content, qr'orz');
93
94     is( $orig->GetHeader('Content-Type'), 'application/x-rt-original-message');
95 }
96
97 {
98     my $buf = '';
99
100     run3(
101         join(
102             ' ',
103             shell_quote(
104                 RT->Config->Get('SMIME')->{'OpenSSL'},
105                 qw( smime -sign -nodetach -passin pass:123456),
106                 -signer => $test->key_path('root@example.com.crt'),
107                 -inkey  => $test->key_path('root@example.com.key'),
108             ),
109             '|',
110             shell_quote(
111                 qw(openssl smime -encrypt -des3),
112                 -from    => 'root@example.com',
113                 -to      => 'sender@example.com',
114                 -subject => "Encrypted and signed message for queue",
115                 $test->key_path('sender@example.com.crt'),
116             )),
117             \"Subject: test\n\norzzzzzz",
118             \$buf,
119             \*STDERR
120     );
121
122     my ($status, $tid) = RT::Test->send_via_mailgate( $buf );
123
124     my $tick = RT::Ticket->new( $RT::SystemUser );
125     $tick->Load( $tid );
126     ok( $tick->Id, "found ticket " . $tick->Id );
127     is( $tick->Subject, 'Encrypted and signed message for queue',
128         "Created the ticket"
129     );
130
131     my $txn = $tick->Transactions->First;
132     my ($msg, $attach, $orig) = @{$txn->Attachments->ItemsArrayRef};
133     is( $msg->GetHeader('X-RT-Incoming-Encryption'),
134         'Success',
135         'recorded incoming mail that is encrypted'
136     );
137     like( $attach->Content, qr'orzzzz');
138 }
139
140 {
141     my $buf = '';
142
143     run3(
144         shell_quote(
145             RT->Config->Get('SMIME')->{'OpenSSL'},
146             qw( smime -sign -passin pass:123456),
147             -signer => $test->key_path('root@example.com.crt'),
148             -inkey  => $test->key_path('root@example.com.key'),
149         ),
150         \"Content-type: text/plain\n\nThis is the body",
151         \$buf,
152         \*STDERR
153     );
154     $buf = "Subject: Signed email\n"
155          . "From: root\@example.com\n"
156          . $buf;
157
158     {
159         my ($status, $tid) = RT::Test->send_via_mailgate( $buf );
160
161         my $tick = RT::Ticket->new( $RT::SystemUser );
162         $tick->Load( $tid );
163         ok( $tick->Id, "found ticket " . $tick->Id );
164         is( $tick->Subject, 'Signed email',
165             "Created the ticket"
166         );
167
168         my $txn = $tick->Transactions->First;
169         my ($msg, $attach, $orig) = @{$txn->Attachments->ItemsArrayRef};
170         is( $msg->GetHeader('X-RT-Incoming-Signature'),
171             '"Enoch Root" <root@example.com>',
172             "Message was signed"
173         );
174         like( $attach->Content, qr/This is the body/ );
175     }
176
177     # Make the signature not match
178     $buf =~ s/This is the body/This is not the body/;
179
180     warning_like {
181         my ($status, $tid) = RT::Test->send_via_mailgate( $buf );
182
183         my $tick = RT::Ticket->new( $RT::SystemUser );
184         $tick->Load( $tid );
185         ok( $tick->Id, "found ticket " . $tick->Id );
186         is( $tick->Subject, 'Signed email',
187             "Created the ticket"
188         );
189
190         my $txn = $tick->Transactions->First;
191         my ($msg, $attach, $orig) = @{$txn->Attachments->ItemsArrayRef};
192         isnt( $msg->GetHeader('X-RT-Incoming-Signature'),
193             '"Enoch Root" <root@example.com>',
194             "Message was not marked signed"
195         );
196         like( $attach->Content, qr/This is not the body/ );
197     } qr/Failure during SMIME verify: The signature did not verify/;
198
199 }
200
201 undef $m;
202 done_testing;