Tools_SMIME.t 4.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/perl
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id: tools_smime.t 8874 2013-03-14 18:59:35Z rousse $

use strict;

use English qw(-no_match_vars);
use File::Temp;
use MIME::Parser;
use Test::More;

#use Sympa::Tools::File;
14
use Sympa::Tools::SMIME;
15

16
plan tests => 15;
17

18
#ok(
19
#    !Sympa::Tools::SMIME::find_keys('/no/where', 'sign'),
20
21
#    'non existing directory'
#);
22

sikeda's avatar
sikeda committed
23
24
25
my $home_dir =
    File::Temp->newdir("test.$$.XXXXX",
    CLEANUP => ($ENV{TEST_DEBUG} ? 0 : 1));
26
27
28
$Conf::Conf{'home'} = $home_dir;
my $cert_dir = $home_dir . '/sympa';
mkdir $cert_dir;
29

30
ok(!Sympa::Tools::SMIME::find_keys('*', 'sign'), 'empty directory');
31

32
33
my $generic_cert_file    = $cert_dir . '/cert.pem';
my $generic_key_file     = $cert_dir . '/private_key';
34
my $encryption_cert_file = $cert_dir . '/cert.pem.enc';
35
36
37
my $encryption_key_file  = $cert_dir . '/private_key.enc';
my $signature_cert_file  = $cert_dir . '/cert.pem.sign';
my $signature_key_file   = $cert_dir . '/private_key.sign';
38
39
40

touch($generic_cert_file);

41
42
ok(!Sympa::Tools::SMIME::find_keys('*', 'sign'),
    'directory with certificate only');
43
44
45
46
47

unlink($generic_cert_file);

touch($generic_key_file);

48
ok(!Sympa::Tools::SMIME::find_keys('*', 'sign'), 'directory with key only');
49
50
51
52
53
54
55

unlink($generic_key_file);

touch($generic_cert_file);
touch($generic_key_file);

is_deeply(
56
    [Sympa::Tools::SMIME::find_keys('*', 'sign')],
57
    [$generic_cert_file, $generic_key_file],
58
59
60
61
    'directory with generic key/certificate only, signature operation'
);

is_deeply(
62
    [Sympa::Tools::SMIME::find_keys('*', 'encrypt')],
63
    [$generic_cert_file, $generic_key_file],
64
65
66
67
    'directory with generic key/certificate only, encryption operation'
);

is_deeply(
68
    [Sympa::Tools::SMIME::find_keys('*', 'decrypt')],
69
    [[$generic_cert_file], [$generic_key_file]],
70
71
72
73
74
75
76
77
78
    'directory with generic key/certificate only, decryption operation'
);

touch($signature_cert_file);
touch($signature_key_file);
touch($encryption_cert_file);
touch($encryption_key_file);

is_deeply(
79
    [Sympa::Tools::SMIME::find_keys('*', 'sign')],
80
    [$signature_cert_file, $signature_key_file],
81
82
83
84
    'directory with dedicated key/certificates, signature operation'
);

is_deeply(
85
    [Sympa::Tools::SMIME::find_keys('*', 'encrypt')],
86
    [$encryption_cert_file, $encryption_key_file],
87
88
89
90
    'directory with dedicated key/certificates, encryption operation'
);

is_deeply(
91
    [Sympa::Tools::SMIME::find_keys('*', 'decrypt')],
92
93
    [   [$generic_cert_file, $encryption_cert_file, $signature_cert_file],
        [$generic_key_file,  $encryption_key_file,  $signature_key_file],
94
95
96
97
    ],
    'directory with dedicated key/certificates, decryption operation'
);

98
99
ok(!eval { Sympa::Tools::SMIME::parse_cert() },
    'neither text nor file given');
100

101
102
103
ok( !Sympa::Tools::SMIME::parse_cert(file => '/no/where'),
    'non-existing file',
);
104

105
ok(!eval { Sympa::Tools::SMIME::parse_cert(text => '') }, 'empty string',);
106

107
my $cert_file = 't/pki/crt/rousse.pem';
108
109
#my $cert_string = Sympa::Tools::File::slurp_file($cert_file);
my $cert_string = do { local (@ARGV, $/) = $cert_file; <> };
110
my $cert_data = {
111
112
113
114
    purpose => {
        sign => 1,
        enc  => 1
    },
115
    subject =>
116
117
118
119
120
121
        'O=sympa developpers, OU=unit testing, CN=Guillaume Rousse, emailAddress=Guillaume.Rousse@sympa.org',
    email    => {'guillaume.rousse@sympa.org' => 1},
    emails   => ['guillaume.rousse@sympa.org'],
    notAfter => 'Feb 23 17:11:04 2023 GMT',
    issuer =>
        'O=sympa developpers, OU=unit testing, CN=Test CA, emailAddress=test@sympa.org',
122
123
};

124
SKIP: {
125
126
    skip 'Crypt::OpenSSL::X509 not installed', 2
        unless $Crypt::OpenSSL::X509::VERSION;
127

128
    is_deeply(Sympa::Tools::SMIME::parse_cert(file => $cert_file,),
129
130
        $cert_data, 'user certificate file parsing');

131
    is_deeply(Sympa::Tools::SMIME::parse_cert(text => $cert_string,),
132
        $cert_data, 'user certificate string parsing');
133
}
134

135
136
137
my $ca_cert_file = 't/pki/crt/ca.pem';
my $ca_cert_data = {
    subject =>
138
        'O=sympa developpers, OU=unit testing, CN=Test CA, emailAddress=test@sympa.org',
139
    email   => {'test@sympa.org' => 1},
140
    emails  => ['test@sympa.org'],
141
142
143
144
    purpose => {
        sign => '',
        enc  => ''
    },
145
146
147
    notAfter => 'Feb 24 17:05:48 2018 GMT',
    issuer =>
        'O=sympa developpers, OU=unit testing, CN=Test CA, emailAddress=test@sympa.org',
148
149
};

150
SKIP: {
151
152
    skip 'Crypt::OpenSSL::X509 not installed', 1
        unless $Crypt::OpenSSL::X509::VERSION;
153

154
    is_deeply(Sympa::Tools::SMIME::parse_cert(file => $ca_cert_file,),
155
        $ca_cert_data, 'CA certificate file parsing');
156
}
157
158
159

sub touch {
    my ($file) = @_;
160
    open(my $fh, '>', $file) or die "Can't create file: $ERRNO";
161
162
    close $fh;
}