RT 4.2.11, ticket#13852
[freeside.git] / rt / t / mail / smime / other-signed.t
1 use strict;
2 use warnings;
3
4 use RT::Test::SMIME tests => undef;
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 # configure key for General queue
13 RT::Test::SMIME->import_key('sender@example.com');
14 my $queue = RT::Test->load_or_create_queue(
15     Name              => 'General',
16     CorrespondAddress => 'sender@example.com',
17     CommentAddress    => 'sender@example.com',
18 );
19 ok $queue && $queue->id, 'loaded or created queue';
20
21 my $user = RT::Test->load_or_create_user(
22     Name => 'root@example.com',
23     EmailAddress => 'root@example.com',
24 );
25 RT::Test::SMIME->import_key('root@example.com.crt', $user);
26 RT::Test->add_rights( Principal => $user, Right => 'SuperUser', Object => RT->System );
27
28 my $buf = '';
29
30 run3(
31     shell_quote(
32         RT->Config->Get('SMIME')->{'OpenSSL'},
33         qw( smime -sign -passin pass:123456),
34         -signer => $test->key_path('root@example.com.crt'),
35         -inkey  => $test->key_path('root@example.com.key'),
36     ),
37     \"Content-type: text/plain\n\nThis is the body",
38     \$buf,
39     \*STDERR
40 );
41 $buf = "Subject: Signed email\n"
42      . "From: root\@example.com\n"
43      . $buf;
44
45 my $send_mail = sub {
46     my %args = ( CAPath => undef, AcceptUntrustedCAs => undef, @_ );
47
48     RT->Config->Get('SMIME')->{$_} = $args{$_} for keys %args;
49
50     my ($status, $tid) = RT::Test->send_via_mailgate( $buf );
51
52     my $tick = RT::Ticket->new( $RT::SystemUser );
53     $tick->Load( $tid );
54     ok( $tick->Id, "found ticket " . $tick->Id );
55     is( $tick->Subject, 'Signed email',
56         "Created the ticket"
57     );
58
59     my $txn = $tick->Transactions->First;
60     my ($msg, $attach, $orig) = @{$txn->Attachments->ItemsArrayRef};
61
62     ($status) = RT::Crypt->ParseStatus(
63         Protocol => 'SMIME',
64         Status => $msg->GetHeader('X-RT-SMIME-Status')
65     );
66
67     return ($msg, $status);
68 };
69
70 # Test with no CA path; should not be marked as signed
71 warning_like {
72     my ($msg, $status) = $send_mail->( CAPath => undef );
73     is( $msg->GetHeader('X-RT-Incoming-Signature'),
74         undef,
75         "Message was not marked as signed"
76     );
77
78     is($status->{Operation}, "Verify", "Found the Verify operation");
79     is($status->{Status}, "BAD", "Verify was a failure");
80     is($status->{Trust}, "NONE", "Noted the no trust level");
81     like($status->{Message}, qr/not trusted/, "Verify was a failure");
82 } qr/Failure during SMIME verify: The signing CA was not trusted/;
83
84 # Test with the correct CA path; marked as signed, trusted
85 {
86     my ($msg, $status) = $send_mail->( CAPath => $test->key_path . "/demoCA/cacert.pem" );
87     is( $msg->GetHeader('X-RT-Incoming-Signature'),
88         '"Enoch Root" <root@example.com>', "Message is signed" );
89
90     is($status->{Operation}, "Verify", "Found the Verify operation");
91     is($status->{Status}, "DONE", "Verify was a success");
92     is($status->{Trust}, "FULL", "Noted the full trust level");
93 }
94
95 # Test with the other CA
96 warning_like {
97     my ($msg, $status) = $send_mail->( CAPath => $test->key_path . "/otherCA/cacert.pem" );
98     is( $msg->GetHeader('X-RT-Incoming-Signature'),
99         undef,
100         "Message was not marked as signed"
101     );
102
103     is($status->{Operation}, "Verify", "Found the Verify operation");
104     is($status->{Status}, "BAD", "Verify was a failure");
105     is($status->{Trust}, "NONE", "Noted the no trust level");
106     like($status->{Message}, qr/not trusted/, "Verify was a failure");
107 } qr/Failure during SMIME verify: The signing CA was not trusted/;
108
109 # Other CA, but allow all CAs
110 {
111     my ($msg, $status) = $send_mail->( CAPath => $test->key_path . "/otherCA/cacert.pem", AcceptUntrustedCAs => 1 );
112     is( $msg->GetHeader('X-RT-Incoming-Signature'),
113         '"Enoch Root" <root@example.com>',
114         "Message was marked as signed"
115     );
116
117     is($status->{Operation}, "Verify", "Found the Verify operation");
118     is($status->{Status}, "DONE", "Verify was a success");
119     is($status->{Trust}, "NONE", "Noted the no trust level");
120 }
121
122 # No CA path, but allow all CAs
123 {
124     my ($msg, $status) = $send_mail->( CAPath => undef, AcceptUntrustedCAs => 1 );
125     is( $msg->GetHeader('X-RT-Incoming-Signature'),
126         '"Enoch Root" <root@example.com>',
127         "Message was marked as signed"
128     );
129
130     is($status->{Operation}, "Verify", "Found the Verify operation");
131     is($status->{Status}, "DONE", "Verify was a success");
132     is($status->{Trust}, "UNKNOWN", "Noted the no trust level");
133 }
134
135 done_testing;