forked from sympa-community/sympa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools_SMIME.t
executable file
·162 lines (129 loc) · 4.59 KB
/
Tools_SMIME.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/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;
use Sympa::Tools::SMIME;
plan tests => 15;
#ok(
# !Sympa::Tools::SMIME::find_keys('/no/where', 'sign'),
# 'non existing directory'
#);
my $home_dir =
File::Temp->newdir("test.$$.XXXXX",
CLEANUP => ($ENV{TEST_DEBUG} ? 0 : 1));
$Conf::Conf{'home'} = $home_dir;
my $cert_dir = $home_dir . '/sympa';
mkdir $cert_dir;
ok(!Sympa::Tools::SMIME::find_keys('*', 'sign'), 'empty directory');
my $generic_cert_file = $cert_dir . '/cert.pem';
my $generic_key_file = $cert_dir . '/private_key';
my $encryption_cert_file = $cert_dir . '/cert.pem.enc';
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';
touch($generic_cert_file);
ok(!Sympa::Tools::SMIME::find_keys('*', 'sign'),
'directory with certificate only');
unlink($generic_cert_file);
touch($generic_key_file);
ok(!Sympa::Tools::SMIME::find_keys('*', 'sign'), 'directory with key only');
unlink($generic_key_file);
touch($generic_cert_file);
touch($generic_key_file);
is_deeply(
[Sympa::Tools::SMIME::find_keys('*', 'sign')],
[$generic_cert_file, $generic_key_file],
'directory with generic key/certificate only, signature operation'
);
is_deeply(
[Sympa::Tools::SMIME::find_keys('*', 'encrypt')],
[$generic_cert_file, $generic_key_file],
'directory with generic key/certificate only, encryption operation'
);
is_deeply(
[Sympa::Tools::SMIME::find_keys('*', 'decrypt')],
[[$generic_cert_file], [$generic_key_file]],
'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(
[Sympa::Tools::SMIME::find_keys('*', 'sign')],
[$signature_cert_file, $signature_key_file],
'directory with dedicated key/certificates, signature operation'
);
is_deeply(
[Sympa::Tools::SMIME::find_keys('*', 'encrypt')],
[$encryption_cert_file, $encryption_key_file],
'directory with dedicated key/certificates, encryption operation'
);
is_deeply(
[Sympa::Tools::SMIME::find_keys('*', 'decrypt')],
[ [$generic_cert_file, $encryption_cert_file, $signature_cert_file],
[$generic_key_file, $encryption_key_file, $signature_key_file],
],
'directory with dedicated key/certificates, decryption operation'
);
ok(!eval { Sympa::Tools::SMIME::parse_cert() },
'neither text nor file given');
ok( !Sympa::Tools::SMIME::parse_cert(file => '/no/where'),
'non-existing file',
);
ok(!eval { Sympa::Tools::SMIME::parse_cert(text => '') }, 'empty string',);
my $cert_file = 't/pki/crt/rousse.pem';
#my $cert_string = Sympa::Tools::File::slurp_file($cert_file);
my $cert_string = do { local (@ARGV, $/) = $cert_file; <> };
my $cert_data = {
purpose => {
sign => 1,
enc => 1
},
subject =>
'O=sympa developpers, OU=unit testing, CN=Guillaume Rousse, [email protected]',
email => {'[email protected]' => 1},
emails => ['[email protected]'],
notAfter => 'Feb 23 17:11:04 2023 GMT',
issuer =>
'O=sympa developpers, OU=unit testing, CN=Test CA, [email protected]',
};
SKIP: {
skip 'Crypt::OpenSSL::X509 not installed', 2
unless $Crypt::OpenSSL::X509::VERSION;
is_deeply(Sympa::Tools::SMIME::parse_cert(file => $cert_file,),
$cert_data, 'user certificate file parsing');
is_deeply(Sympa::Tools::SMIME::parse_cert(text => $cert_string,),
$cert_data, 'user certificate string parsing');
}
my $ca_cert_file = 't/pki/crt/ca.pem';
my $ca_cert_data = {
subject =>
'O=sympa developpers, OU=unit testing, CN=Test CA, [email protected]',
email => {'[email protected]' => 1},
emails => ['[email protected]'],
purpose => {
sign => '',
enc => ''
},
notAfter => 'Feb 24 17:05:48 2018 GMT',
issuer =>
'O=sympa developpers, OU=unit testing, CN=Test CA, [email protected]',
};
SKIP: {
skip 'Crypt::OpenSSL::X509 not installed', 1
unless $Crypt::OpenSSL::X509::VERSION;
is_deeply(Sympa::Tools::SMIME::parse_cert(file => $ca_cert_file,),
$ca_cert_data, 'CA certificate file parsing');
}
sub touch {
my ($file) = @_;
open(my $fh, '>', $file) or die "Can't create file: $ERRNO";
close $fh;
}