VMS exit codes weren't handled well enough and were unclear
[openssl.git] / test / cms-test.pl
1 # test/cms-test.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 # CMS, PKCS7 consistency test script. Run extensive tests on
54 # OpenSSL PKCS#7 and CMS implementations.
55
56 my $ossl_path;
57 my $redir = " 2> cms.err > cms.out";
58 # Make VMS work
59 if ( $^O eq "VMS" && -f "OSSLX:openssl.exe" ) {
60     $ossl_path = "pipe mcr OSSLX:openssl";
61     $null_path = "NL:";
62     # On VMS, the lowest 3 bits of the exit code indicates severity
63     # 1 is success (perl translates it to 0 for $?), 2 is error
64     # (perl doesn't translate it)
65     $failure_code = 512;        # 2 << 8 = 512
66 }
67 # Make MSYS work
68 elsif ( $^O eq "MSWin32" && -f "../apps/openssl.exe" ) {
69     $ossl_path = "cmd /c ..\\apps\\openssl";
70     $null_path = "/dev/null";
71     $failure_code = 256;
72 }
73 elsif ( -f "../apps/openssl$ENV{EXE_EXT}" ) {
74     $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
75     $null_path = "/dev/null";
76     $failure_code = 256;
77 }
78 elsif ( -f "..\\out32dll\\openssl.exe" ) {
79     $ossl_path = "..\\out32dll\\openssl.exe";
80     $null_path = "/dev/null";
81     $failure_code = 256;
82 }
83 elsif ( -f "..\\out32\\openssl.exe" ) {
84     $ossl_path = "..\\out32\\openssl.exe";
85     $null_path = "/dev/null";
86     $failure_code = 256;
87 }
88 else {
89     die "Can't find OpenSSL executable";
90 }
91
92 my $pk7cmd   = "$ossl_path smime ";
93 my $cmscmd   = "$ossl_path cms ";
94 my $smdir    = "smime-certs";
95 my $halt_err = 1;
96
97 my $badcmd = 0;
98 my $no_ec;
99 my $no_ec2m;
100 my $no_ecdh;
101 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
102
103 system ("$ossl_path no-ec > $null_path");
104 if ($? == 0)
105         {
106         $no_ec = 1;
107         }
108 elsif ($? == $failure_code)
109         {
110         $no_ec = 0;
111         }
112 else
113         {
114         die "Error checking for EC support\n";
115         }
116     
117 system ("$ossl_path no-ec2m > $null_path");
118 if ($? == 0)
119         {
120         $no_ec2m = 1;
121         }
122 elsif ($? == $failure_code)
123         {
124         $no_ec2m = 0;
125         }
126 else
127         {
128         die "Error checking for EC2M support\n";
129         }
130
131 system ("$ossl_path no-ecdh > $null_path");
132 if ($? == 0)
133         {
134         $no_ecdh = 1;
135         }
136 elsif ($? == $failure_code)
137         {
138         $no_ecdh = 0;
139         }
140 else
141         {
142         die "Error checking for ECDH support\n";
143         }
144     
145 my @smime_pkcs7_tests = (
146
147     [
148         "signed content DER format, RSA key",
149         "-sign -in smcont.txt -outform \"DER\" -nodetach"
150           . " -certfile $smdir/smroot.pem"
151           . " -signer $smdir/smrsa1.pem -out test.cms",
152         "-verify -in test.cms -inform \"DER\" "
153           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
154     ],
155
156     [
157         "signed detached content DER format, RSA key",
158         "-sign -in smcont.txt -outform \"DER\""
159           . " -signer $smdir/smrsa1.pem -out test.cms",
160         "-verify -in test.cms -inform \"DER\" "
161           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
162     ],
163
164     [
165         "signed content test streaming BER format, RSA",
166         "-sign -in smcont.txt -outform \"DER\" -nodetach"
167           . " -stream -signer $smdir/smrsa1.pem -out test.cms",
168         "-verify -in test.cms -inform \"DER\" "
169           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
170     ],
171
172     [
173         "signed content DER format, DSA key",
174         "-sign -in smcont.txt -outform \"DER\" -nodetach"
175           . " -signer $smdir/smdsa1.pem -out test.cms",
176         "-verify -in test.cms -inform \"DER\" "
177           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
178     ],
179
180     [
181         "signed detached content DER format, DSA key",
182         "-sign -in smcont.txt -outform \"DER\""
183           . " -signer $smdir/smdsa1.pem -out test.cms",
184         "-verify -in test.cms -inform \"DER\" "
185           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
186     ],
187
188     [
189         "signed detached content DER format, add RSA signer",
190         "-resign -inform \"DER\" -in test.cms -outform \"DER\""
191           . " -signer $smdir/smrsa1.pem -out test2.cms",
192         "-verify -in test2.cms -inform \"DER\" "
193           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
194     ],
195
196     [
197         "signed content test streaming BER format, DSA key",
198         "-sign -in smcont.txt -outform \"DER\" -nodetach"
199           . " -stream -signer $smdir/smdsa1.pem -out test.cms",
200         "-verify -in test.cms -inform \"DER\" "
201           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
202     ],
203
204     [
205         "signed content test streaming BER format, 2 DSA and 2 RSA keys",
206         "-sign -in smcont.txt -outform \"DER\" -nodetach"
207           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
208           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
209           . " -stream -out test.cms",
210         "-verify -in test.cms -inform \"DER\" "
211           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
212     ],
213
214     [
215 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
216         "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
217           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
218           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
219           . " -stream -out test.cms",
220         "-verify -in test.cms -inform \"DER\" "
221           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
222     ],
223
224     [
225         "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
226         "-sign -in smcont.txt -nodetach"
227           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
228           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
229           . " -stream -out test.cms",
230         "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
231     ],
232
233     [
234 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
235         "-sign -in smcont.txt"
236           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
237           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
238           . " -stream -out test.cms",
239         "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
240     ],
241
242     [
243         "enveloped content test streaming S/MIME format, 3 recipients",
244         "-encrypt -in smcont.txt"
245           . " -stream -out test.cms"
246           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
247         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
248     ],
249
250     [
251 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
252         "-encrypt -in smcont.txt"
253           . " -stream -out test.cms"
254           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
255         "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
256     ],
257
258     [
259 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
260         "-encrypt -in smcont.txt"
261           . " -stream -out test.cms"
262           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
263         "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
264     ],
265
266     [
267 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
268         "-encrypt -in smcont.txt"
269           . " -aes256 -stream -out test.cms"
270           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
271         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
272     ],
273
274 );
275
276 my @smime_cms_tests = (
277
278     [
279         "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
280         "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
281           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
282           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
283           . " -stream -out test.cms",
284         "-verify -in test.cms -inform \"DER\" "
285           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
286     ],
287
288     [
289         "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
290         "-sign -in smcont.txt -outform PEM -nodetach"
291           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
292           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
293           . " -stream -out test.cms",
294         "-verify -in test.cms -inform PEM "
295           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
296     ],
297
298     [
299         "signed content MIME format, RSA key, signed receipt request",
300         "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
301           . " -receipt_request_to test\@openssl.org -receipt_request_all"
302           . " -out test.cms",
303         "-verify -in test.cms "
304           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
305     ],
306
307     [
308         "signed receipt MIME format, RSA key",
309         "-sign_receipt -in test.cms"
310           . " -signer $smdir/smrsa2.pem"
311           . " -out test2.cms",
312         "-verify_receipt test2.cms -in test.cms"
313           . " \"-CAfile\" $smdir/smroot.pem"
314     ],
315
316     [
317         "enveloped content test streaming S/MIME format, 3 recipients, keyid",
318         "-encrypt -in smcont.txt"
319           . " -stream -out test.cms -keyid"
320           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
321         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
322     ],
323
324     [
325         "enveloped content test streaming PEM format, KEK",
326         "-encrypt -in smcont.txt -outform PEM -aes128"
327           . " -stream -out test.cms "
328           . " -secretkey 000102030405060708090A0B0C0D0E0F "
329           . " -secretkeyid C0FEE0",
330         "-decrypt -in test.cms -out smtst.txt -inform PEM"
331           . " -secretkey 000102030405060708090A0B0C0D0E0F "
332           . " -secretkeyid C0FEE0"
333     ],
334
335     [
336         "enveloped content test streaming PEM format, KEK, key only",
337         "-encrypt -in smcont.txt -outform PEM -aes128"
338           . " -stream -out test.cms "
339           . " -secretkey 000102030405060708090A0B0C0D0E0F "
340           . " -secretkeyid C0FEE0",
341         "-decrypt -in test.cms -out smtst.txt -inform PEM"
342           . " -secretkey 000102030405060708090A0B0C0D0E0F "
343     ],
344
345     [
346         "data content test streaming PEM format",
347         "-data_create -in smcont.txt -outform PEM -nodetach"
348           . " -stream -out test.cms",
349         "-data_out -in test.cms -inform PEM -out smtst.txt"
350     ],
351
352     [
353         "encrypted content test streaming PEM format, 128 bit RC2 key",
354         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
355           . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
356           . " -stream -out test.cms",
357         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
358           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
359     ],
360
361     [
362         "encrypted content test streaming PEM format, 40 bit RC2 key",
363         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
364           . " -rc2 -secretkey 0001020304"
365           . " -stream -out test.cms",
366         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
367           . " -secretkey 0001020304 -out smtst.txt"
368     ],
369
370     [
371         "encrypted content test streaming PEM format, triple DES key",
372         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
373           . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
374           . " -stream -out test.cms",
375         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
376           . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
377           . " -out smtst.txt"
378     ],
379
380     [
381         "encrypted content test streaming PEM format, 128 bit AES key",
382         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
383           . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
384           . " -stream -out test.cms",
385         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
386           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
387     ],
388
389 );
390
391 my @smime_cms_comp_tests = (
392
393     [
394         "compressed content test streaming PEM format",
395         "-compress -in smcont.txt -outform PEM -nodetach"
396           . " -stream -out test.cms",
397         "-uncompress -in test.cms -inform PEM -out smtst.txt"
398     ]
399
400 );
401
402 my @smime_cms_param_tests = (
403     [
404         "signed content test streaming PEM format, RSA keys, PSS signature",
405         "-sign -in smcont.txt -outform PEM -nodetach"
406           . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
407           . " -out test.cms",
408         "-verify -in test.cms -inform PEM "
409           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
410     ],
411
412     [
413         "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
414         "-sign -in smcont.txt -outform PEM -nodetach -noattr"
415           . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
416           . " -out test.cms",
417         "-verify -in test.cms -inform PEM "
418           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
419     ],
420
421     [
422         "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
423         "-sign -in smcont.txt -outform PEM -nodetach"
424           . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
425           . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
426         "-verify -in test.cms -inform PEM "
427           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
428     ],
429
430     [
431 "enveloped content test streaming S/MIME format, OAEP default parameters",
432         "-encrypt -in smcont.txt"
433           . " -stream -out test.cms"
434           . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
435         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
436     ],
437
438     [
439 "enveloped content test streaming S/MIME format, OAEP SHA256",
440         "-encrypt -in smcont.txt"
441           . " -stream -out test.cms"
442           . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
443           . " -keyopt rsa_oaep_md:sha256",
444         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
445     ],
446
447     [
448 "enveloped content test streaming S/MIME format, ECDH",
449         "-encrypt -in smcont.txt"
450           . " -stream -out test.cms"
451           . " -recip $smdir/smec1.pem",
452         "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
453     ],
454
455     [
456 "enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
457         "-encrypt -in smcont.txt"
458           . " -stream -out test.cms"
459           . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
460         "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
461     ],
462
463     [
464 "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
465         "-encrypt -in smcont.txt"
466           . " -stream -out test.cms"
467           . " -recip $smdir/smec2.pem -aes128"
468           . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
469         "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
470     ],
471
472     [
473 "enveloped content test streaming S/MIME format, X9.42 DH",
474         "-encrypt -in smcont.txt"
475           . " -stream -out test.cms"
476           . " -recip $smdir/smdh.pem -aes128",
477         "-decrypt -recip $smdir/smdh.pem -in test.cms -out smtst.txt"
478     ]
479 );
480
481 print "CMS => PKCS#7 compatibility tests\n";
482
483 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
484
485 print "CMS <= PKCS#7 compatibility tests\n";
486
487 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
488
489 print "CMS <=> CMS consistency tests\n";
490
491 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
492 run_smime_tests( \$badcmd, \@smime_cms_tests,   $cmscmd, $cmscmd );
493
494 print "CMS <=> CMS consistency tests, modified key parameters\n";
495 run_smime_tests( \$badcmd, \@smime_cms_param_tests,   $cmscmd, $cmscmd );
496
497 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
498     run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
499 }
500 else {
501     print "Zlib not supported: compression tests skipped\n";
502 }
503
504 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
505
506 if ($badcmd) {
507     print "$badcmd TESTS FAILED!!\n";
508 }
509 else {
510     print "ALL TESTS SUCCESSFUL.\n";
511 }
512
513 unlink "test.cms";
514 unlink "test2.cms";
515 unlink "smtst.txt";
516 unlink "cms.out";
517 unlink "cms.err";
518
519 sub run_smime_tests {
520     my ( $rv, $aref, $scmd, $vcmd ) = @_;
521
522     foreach $smtst (@$aref) {
523         my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
524         if ($ossl8)
525                 {
526                 # Skip smime resign: 0.9.8 smime doesn't support -resign        
527                 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
528                 # Disable streaming: option not supported in 0.9.8
529                 $tnam =~ s/streaming//; 
530                 $rscmd =~ s/-stream//;  
531                 $rvcmd =~ s/-stream//;
532                 }
533         if ($no_ec && $tnam =~ /ECDH/)
534                 {
535                 print "$tnam: skipped, EC disabled\n";
536                 next;
537                 }
538         if ($no_ecdh && $tnam =~ /ECDH/)
539                 {
540                 print "$tnam: skipped, ECDH disabled\n";
541                 next;
542                 }
543         if ($no_ec2m && $tnam =~ /K-283/)
544                 {
545                 print "$tnam: skipped, EC2M disabled\n";
546                 next;
547                 }
548         system("$scmd$rscmd$redir");
549         if ($?) {
550             print "$tnam: generation error\n";
551             $$rv++;
552             exit 1 if $halt_err;
553             next;
554         }
555         system("$vcmd$rvcmd$redir");
556         if ($?) {
557             print "$tnam: verify error\n";
558             $$rv++;
559             exit 1 if $halt_err;
560             next;
561         }
562         if (!cmp_files("smtst.txt", "smcont.txt")) {
563             print "$tnam: content verify error\n";
564             $$rv++;
565             exit 1 if $halt_err;
566             next;
567         }
568         print "$tnam: OK\n";
569     }
570 }
571
572 sub cmp_files {
573     use FileHandle;
574     my ( $f1, $f2 ) = @_;
575     my $fp1 = FileHandle->new();
576     my $fp2 = FileHandle->new();
577
578     my ( $rd1, $rd2 );
579
580     if ( !open( $fp1, "<$f1" ) ) {
581         print STDERR "Can't Open file $f1\n";
582         return 0;
583     }
584
585     if ( !open( $fp2, "<$f2" ) ) {
586         print STDERR "Can't Open file $f2\n";
587         return 0;
588     }
589
590     binmode $fp1;
591     binmode $fp2;
592
593     my $ret = 0;
594
595     for ( ; ; ) {
596         $n1 = sysread $fp1, $rd1, 4096;
597         $n2 = sysread $fp2, $rd2, 4096;
598         last if ( $n1 != $n2 );
599         last if ( $rd1 ne $rd2 );
600
601         if ( $n1 == 0 ) {
602             $ret = 1;
603             last;
604         }
605
606     }
607
608     close $fp1;
609     close $fp2;
610
611     return $ret;
612
613 }
614