1d77c88d12fd85d8531359f2e23486be35687841
[openssl.git] / test / runex.pl
1 # test/runex.pl
2 # Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 # project.
4 #
5 # ====================================================================
6 # Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # 1. Redistributions of source code must retain the above copyright
13 #    notice, this list of conditions and the following disclaimer. 
14 #
15 # 2. Redistributions in binary form must reproduce the above copyright
16 #    notice, this list of conditions and the following disclaimer in
17 #    the documentation and/or other materials provided with the
18 #    distribution.
19 #
20 # 3. All advertising materials mentioning features or use of this
21 #    software must display the following acknowledgment:
22 #    "This product includes software developed by the OpenSSL Project
23 #    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 #
25 # 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 #    endorse or promote products derived from this software without
27 #    prior written permission. For written permission, please contact
28 #    licensing@OpenSSL.org.
29 #
30 # 5. Products derived from this software may not be called "OpenSSL"
31 #    nor may "OpenSSL" appear in their names without prior written
32 #    permission of the OpenSSL Project.
33 #
34 # 6. Redistributions of any form whatsoever must retain the following
35 #    acknowledgment:
36 #    "This product includes software developed by the OpenSSL Project
37 #    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 #
39 # THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 # OF THE POSSIBILITY OF SUCH DAMAGE.
51 # ====================================================================
52
53 # Perl script to run tests against S/MIME examples in RFC4134
54 # Assumes all files are extracted in an directory called "examples"
55
56 my $badttest = 0;
57 my $verbose = 1;
58
59 my $cmscmd = "../apps/openssl cms";
60 my $convcmd = "../apps/openssl x509 -inform DER";
61 my $exdir = "examples";
62
63 my @test_list = (
64 ["3.1.bin"      => "dataout"],
65 ["3.2.bin"      => "encode, dataout"],
66 ["4.1.bin"      => "encode, verifyder, content, dss"],
67 ["4.2.bin"      => "encode, verifyder, cont, rsa"],
68 ["4.3.bin"      => "encode, verifyder, cont_extern, dss"],
69 ["4.4.bin"      => "encode, verifyder, cont, dss"],
70 ["4.5.bin"      => "verifyder, content, rsa"],
71 ["4.6.bin"      => "encode, verifyder, cont, dss"],
72 ["4.7.bin"      => "encode, verifyder, cont, dss"],
73 ["4.8.eml"      => "verifymime, dss"],
74 ["4.9.eml"      => "verifymime, dss"],
75 ["4.10.bin"     => "encode, verifyder, cont, dss"],
76 ["4.11.bin"     => "encode"],
77 ["5.1.bin"      => "encode"],
78 ["5.2.bin"      => "encode"],
79 ["6.0.bin"      => "encode, digest, cont"],
80 ["7.1.bin"      => "encode"],
81 ["7.2.bin"      => "encode"]
82 );
83
84         if (!-d $exdir)
85                 {
86                 print STDERR "FATAL ERROR: examples directory missing!!\n";
87                 exit 1;
88                 }
89
90         system ("$convcmd -in $exdir/CarlDSSSelf.cer -out $exdir/CarlDSSSelf.pem");
91         system ("$convcmd -in $exdir/CarlRSASelf.cer -out $exdir/CarlRSASelf.pem");
92
93         $cafile = "$cmsdir/CarlRSASelf.pem" if $tlist =~ /rsa/;
94
95 foreach (@test_list) {
96         my ($file, $tlist) = @$_;
97         print "Example file $file:\n";
98         if ($tlist =~ /encode/)
99                 {
100                 run_reencode_test($exdir, $file);
101                 }
102         if ($tlist =~ /dataout/)
103                 {
104                 run_dataout_test($exdir, $file);
105                 }
106         if ($tlist =~ /verify/)
107                 {
108                 run_verify_test($exdir, $tlist, $file);
109                 }
110         if ($tlist =~ /digest/)
111                 {
112                 run_digest_test($exdir, $tlist, $file);
113                 }
114
115 }
116
117 unlink "cms.out";
118 unlink "cms.err";
119 unlink "tmp.der";
120 unlink "tmp.txt";
121
122 if ($badtest) {
123         print "\n$badtest TESTS FAILED!!\n";
124 } else {
125         print "\n***All tests successful***\n";
126 }
127
128
129 sub run_reencode_test
130         {
131         my ($cmsdir, $tfile) = @_;
132         unlink "tmp.der";
133
134         system ("$cmscmd -cmsout -inform DER -outform DER" .
135                 " -in $cmsdir/$tfile -out tmp.der");
136
137         if ($?)
138                 {
139                 print "\tReencode command FAILED!!\n";
140                 $badtest++;
141                 }
142         elsif (!cmp_files("$cmsdir/$tfile", "tmp.der"))
143                 {
144                 print "\tReencode FAILED!!\n";
145                 $badtest++;
146                 }
147         else
148                 {
149                 print "\tReencode passed\n" if $verbose;
150                 }
151         }
152
153 sub run_dataout_test
154         {
155         my ($cmsdir, $tfile) = @_;
156         unlink "tmp.txt";
157
158         system ("$cmscmd -data_out -inform DER" .
159                 " -in $cmsdir/$tfile -out tmp.txt");
160
161         if ($?)
162                 {
163                 print "\tDataout command FAILED!!\n";
164                 $badtest++;
165                 }
166         elsif (!cmp_files("$cmsdir/ExContent.bin", "tmp.txt"))
167                 {
168                 print "\tDataout compare FAILED!!\n";
169                 $badtest++;
170                 }
171         else
172                 {
173                 print "\tDataout passed\n" if $verbose;
174                 }
175         }
176
177 sub run_verify_test
178         {
179         my ($cmsdir, $tlist, $tfile) = @_;
180         unlink "tmp.txt";
181
182         $form = "DER" if $tlist =~ /verifyder/;
183         $form = "SMIME" if $tlist =~ /verifymime/;
184         $cafile = "$cmsdir/CarlDSSSelf.pem" if $tlist =~ /dss/;
185         $cafile = "$cmsdir/CarlRSASelf.pem" if $tlist =~ /rsa/;
186
187         $cmd = "$cmscmd -verify -inform $form" .
188                 " -CAfile $cafile" .
189                 " -in $cmsdir/$tfile -out tmp.txt";
190
191         $cmd .= " -content $cmsdir/ExContent.bin" if $tlist =~ /cont_extern/;   
192
193         system ("$cmd 2>cms.err 1>cms.out");
194
195         if ($?)
196                 {
197                 print "\tVerify command FAILED!!\n";
198                 $badtest++;
199                 }
200         elsif ($tlist =~ /cont/ &&
201                 !cmp_files("$cmsdir/ExContent.bin", "tmp.txt"))
202                 {
203                 print "\tVerify content compare FAILED!!\n";
204                 $badtest++;
205                 }
206         else
207                 {
208                 print "\tVerify passed\n" if $verbose;
209                 }
210         }
211
212 sub run_digest_test
213         {
214         my ($cmsdir, $tlist, $tfile) = @_;
215         unlink "tmp.txt";
216
217         system ("$cmscmd -digest_verify -inform DER" .
218                 " -in $cmsdir/$tfile -out tmp.txt");
219
220         if ($?)
221                 {
222                 print "\tDigest verify command FAILED!!\n";
223                 $badtest++;
224                 }
225         elsif ($tlist =~ /cont/ &&
226                 !cmp_files("$cmsdir/ExContent.bin", "tmp.txt"))
227                 {
228                 print "\tDigest verify content compare FAILED!!\n";
229                 $badtest++;
230                 }
231         else
232                 {
233                 print "\tDigest verify passed\n" if $verbose;
234                 }
235         }
236
237 sub cmp_files
238         {
239         my ($f1, $f2) = @_;
240         my ($fp1, $fp2);
241
242         my ($rd1, $rd2);
243
244         if (!open($fp1, "<$f1") ) {
245                 print STDERR "Can't Open file $f1\n";
246                 return 0;
247         }
248
249         if (!open($fp2, "<$f2") ) {
250                 print STDERR "Can't Open file $f2\n";
251                 return 0;
252         }
253
254         binmode $fp1;
255         binmode $fp2;
256
257         my $ret = 0;
258
259         for (;;)
260                 {
261                 $n1 = sysread $fp1, $rd1, 4096;
262                 $n2 = sysread $fp2, $rd2, 4096;
263                 last if ($n1 != $n2);
264                 last if ($rd1 ne $rd2);
265
266                 if ($n1 == 0)
267                         {
268                         $ret = 1;
269                         last;
270                         }
271
272                 }
273
274         close $fp1;
275         close $fp2;
276
277         return $ret;
278
279         }
280
281