ed16ad4399c3c31e420be2cfc60f21103bde8c9c
[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 = "../util/shlib_wrap.sh ../apps/openssl cms";
60 my $convcmd = "../util/shlib_wrap.sh ../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, encrypted, cont"],
81 ["7.2.bin"      => "encode, encrypted, cont"]
82 );
83
84 my $secretkey = "73:7c:79:1f:25:ea:d0:e0:46:29:25:43:52:f7:dc:62:91:e5:cb:26:91:7a:da:32";
85
86         if (!-d $exdir)
87                 {
88                 print STDERR "FATAL ERROR: examples directory missing!!\n";
89                 exit 1;
90                 }
91
92         system ("$convcmd -in $exdir/CarlDSSSelf.cer -out $exdir/CarlDSSSelf.pem");
93         system ("$convcmd -in $exdir/CarlRSASelf.cer -out $exdir/CarlRSASelf.pem");
94
95         $cafile = "$cmsdir/CarlRSASelf.pem" if $tlist =~ /rsa/;
96
97 foreach (@test_list) {
98         my ($file, $tlist) = @$_;
99         print "Example file $file:\n";
100         if ($tlist =~ /encode/)
101                 {
102                 run_reencode_test($exdir, $file);
103                 }
104         if ($tlist =~ /dataout/)
105                 {
106                 run_dataout_test($exdir, $file);
107                 }
108         if ($tlist =~ /verify/)
109                 {
110                 run_verify_test($exdir, $tlist, $file);
111                 }
112         if ($tlist =~ /digest/)
113                 {
114                 run_digest_test($exdir, $tlist, $file);
115                 }
116         if ($tlist =~ /encrypted/)
117                 {
118                 run_encrypted_test($exdir, $tlist, $file, $secretkey);
119                 }
120
121 }
122
123 unlink "cms.out";
124 unlink "cms.err";
125 unlink "tmp.der";
126 unlink "tmp.txt";
127
128 if ($badtest) {
129         print "\n$badtest TESTS FAILED!!\n";
130 } else {
131         print "\n***All tests successful***\n";
132 }
133
134
135 sub run_reencode_test
136         {
137         my ($cmsdir, $tfile) = @_;
138         unlink "tmp.der";
139
140         system ("$cmscmd -cmsout -inform DER -outform DER" .
141                 " -in $cmsdir/$tfile -out tmp.der");
142
143         if ($?)
144                 {
145                 print "\tReencode command FAILED!!\n";
146                 $badtest++;
147                 }
148         elsif (!cmp_files("$cmsdir/$tfile", "tmp.der"))
149                 {
150                 print "\tReencode FAILED!!\n";
151                 $badtest++;
152                 }
153         else
154                 {
155                 print "\tReencode passed\n" if $verbose;
156                 }
157         }
158
159 sub run_dataout_test
160         {
161         my ($cmsdir, $tfile) = @_;
162         unlink "tmp.txt";
163
164         system ("$cmscmd -data_out -inform DER" .
165                 " -in $cmsdir/$tfile -out tmp.txt");
166
167         if ($?)
168                 {
169                 print "\tDataout command FAILED!!\n";
170                 $badtest++;
171                 }
172         elsif (!cmp_files("$cmsdir/ExContent.bin", "tmp.txt"))
173                 {
174                 print "\tDataout compare FAILED!!\n";
175                 $badtest++;
176                 }
177         else
178                 {
179                 print "\tDataout passed\n" if $verbose;
180                 }
181         }
182
183 sub run_verify_test
184         {
185         my ($cmsdir, $tlist, $tfile) = @_;
186         unlink "tmp.txt";
187
188         $form = "DER" if $tlist =~ /verifyder/;
189         $form = "SMIME" if $tlist =~ /verifymime/;
190         $cafile = "$cmsdir/CarlDSSSelf.pem" if $tlist =~ /dss/;
191         $cafile = "$cmsdir/CarlRSASelf.pem" if $tlist =~ /rsa/;
192
193         $cmd = "$cmscmd -verify -inform $form" .
194                 " -CAfile $cafile" .
195                 " -in $cmsdir/$tfile -out tmp.txt";
196
197         $cmd .= " -content $cmsdir/ExContent.bin" if $tlist =~ /cont_extern/;   
198
199         system ("$cmd 2>cms.err 1>cms.out");
200
201         if ($?)
202                 {
203                 print "\tVerify command FAILED!!\n";
204                 $badtest++;
205                 }
206         elsif ($tlist =~ /cont/ &&
207                 !cmp_files("$cmsdir/ExContent.bin", "tmp.txt"))
208                 {
209                 print "\tVerify content compare FAILED!!\n";
210                 $badtest++;
211                 }
212         else
213                 {
214                 print "\tVerify passed\n" if $verbose;
215                 }
216         }
217
218 sub run_digest_test
219         {
220         my ($cmsdir, $tlist, $tfile) = @_;
221         unlink "tmp.txt";
222
223         system ("$cmscmd -digest_verify -inform DER" .
224                 " -in $cmsdir/$tfile -out tmp.txt");
225
226         if ($?)
227                 {
228                 print "\tDigest verify command FAILED!!\n";
229                 $badtest++;
230                 }
231         elsif ($tlist =~ /cont/ &&
232                 !cmp_files("$cmsdir/ExContent.bin", "tmp.txt"))
233                 {
234                 print "\tDigest verify content compare FAILED!!\n";
235                 $badtest++;
236                 }
237         else
238                 {
239                 print "\tDigest verify passed\n" if $verbose;
240                 }
241         }
242
243 sub run_encrypted_test
244         {
245         my ($cmsdir, $tlist, $tfile, $key) = @_;
246         unlink "tmp.txt";
247
248         system ("$cmscmd -EncryptedData_decrypt -inform DER" .
249                 " -secretkey $key" .
250                 " -in $cmsdir/$tfile -out tmp.txt");
251
252         if ($?)
253                 {
254                 print "\tEncrypted Data command FAILED!!\n";
255                 $badtest++;
256                 }
257         elsif ($tlist =~ /cont/ &&
258                 !cmp_files("$cmsdir/ExContent.bin", "tmp.txt"))
259                 {
260                 print "\tEncrypted Data content compare FAILED!!\n";
261                 $badtest++;
262                 }
263         else
264                 {
265                 print "\tEncryptedData verify passed\n" if $verbose;
266                 }
267         }
268
269 sub cmp_files
270         {
271         my ($f1, $f2) = @_;
272         my ($fp1, $fp2);
273
274         my ($rd1, $rd2);
275
276         if (!open($fp1, "<$f1") ) {
277                 print STDERR "Can't Open file $f1\n";
278                 return 0;
279         }
280
281         if (!open($fp2, "<$f2") ) {
282                 print STDERR "Can't Open file $f2\n";
283                 return 0;
284         }
285
286         binmode $fp1;
287         binmode $fp2;
288
289         my $ret = 0;
290
291         for (;;)
292                 {
293                 $n1 = sysread $fp1, $rd1, 4096;
294                 $n2 = sysread $fp2, $rd2, 4096;
295                 last if ($n1 != $n2);
296                 last if ($rd1 ne $rd2);
297
298                 if ($n1 == 0)
299                         {
300                         $ret = 1;
301                         last;
302                         }
303
304                 }
305
306         close $fp1;
307         close $fp2;
308
309         return $ret;
310
311         }
312
313