1#!perl
2#===============================================================================
3#
4# t/02_function.t
5#
6# DESCRIPTION
7#   Test script to check crypt_file() function (and decryption filter).
8#
9# COPYRIGHT
10#   Copyright (C) 2004-2006, 2009, 2014, 2020 Steve Hay.  All rights reserved.
11#
12# LICENCE
13#   This script is free software; you can redistribute it and/or modify it under
14#   the same terms as Perl itself, i.e. under the terms of either the GNU
15#   General Public License or the Artistic License, as specified in the LICENCE
16#   file.
17#
18#===============================================================================
19
20use 5.008001;
21
22use strict;
23use warnings;
24
25use Cwd qw(abs_path);
26use File::Path qw(mkpath rmtree);
27use File::Spec::Functions qw(canonpath catdir catfile updir);
28use FindBin qw($Bin);
29use Test::More;
30
31## no critic (Subroutines::ProhibitSubroutinePrototypes)
32
33sub new_ifilename();
34sub new_ofilename();
35
36#===============================================================================
37# INITIALIZATION
38#===============================================================================
39
40my($have_decrypt);
41
42BEGIN {
43    my $i = 0;
44    sub new_ifilename() {
45        $i++;
46        return "test$i.pl";
47    }
48    my $j = 0;
49    sub new_ofilename() {
50        $j++;
51        return "test$j.enc.pl";
52    }
53
54    my $top_dir = canonpath(abs_path(catdir($Bin, updir())));
55    my $lib_dir = catfile($top_dir, 'blib', 'lib', 'Filter', 'Crypto');
56
57    if (-f catfile($lib_dir, 'CryptFile.pm')) {
58        require Filter::Crypto::CryptFile;
59        Filter::Crypto::CryptFile->import(qw(:DEFAULT $ErrStr));
60        plan tests => 295;
61    }
62    else {
63        plan skip_all => 'CryptFile component not built';
64    }
65
66    $have_decrypt = -f catfile($lib_dir, 'Decrypt.pm');
67}
68
69#===============================================================================
70# MAIN PROGRAM
71#===============================================================================
72
73MAIN: {
74    my $script  = 'foo.pl';
75    my $modroot = 'Filter';
76    my $moddir  = catdir($modroot, 'Crypto');
77    my $module  = catfile($moddir, 'Foo.pm');
78    my $str     = 'Hello, world.';
79    my $prog    = qq[print "$str\\n";\n];
80    my $scrsrc  = qq[use Carp;\nuse Filter::Crypto::Foo;\nFilter::Crypto::Foo::foo();\n];
81    my $modsrc  = qq[package Filter::Crypto::Foo;\nsub foo() { print "$str\\n" }\n1;\n];
82    my $head    = 'use Filter::Crypto::Decrypt;';
83    my $qrhead  = qr/^\Q$head\E/o;
84    my $buf     = '';
85
86    my $perl_exe = $^X =~ / /o ? qq["$^X"] : $^X;
87    my $perl = qq[$perl_exe -Mblib];
88
89    my($ifile, $ofile, $iofile);
90    my($fh, $ifh, $ofh, $iofh, $contents, $saved_contents, $line, $i, $n);
91
92    {
93        $ifile = new_ifilename();
94        $iofile = $ifile;
95
96        open $fh, '>', $ifile or die "Can't create file '$ifile': $!\n";
97        print $fh $prog;
98        close $fh;
99
100        open $iofh, '+<', $iofile or die "Can't update file '$iofile': $!\n";
101        binmode $iofh;
102        ok(crypt_file($iofh), 'crypt_file($fh) returned OK') or
103            diag("\$ErrStr = '$ErrStr'");
104        close $iofh;
105
106        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
107        $contents = do { local $/; <$fh> };
108        close $fh;
109        like($contents, $qrhead, '... and encrypted file OK');
110
111        SKIP: {
112            skip 'Decrypt component not built', 1 unless $have_decrypt;
113            chomp($line = qx{$perl $iofile});
114            is($line, $str, '... and encrypted file runs OK');
115        }
116
117        ok(crypt_file($iofile), 'crypt_file($file) returned OK') or
118            diag("\$ErrStr = '$ErrStr'");
119
120        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
121        $contents = do { local $/; <$fh> };
122        close $fh;
123        is($contents, $prog, '... and decrypted file OK');
124
125        chomp($line = qx{$perl $iofile});
126        is($line, $str, '... and decrypted file runs OK');
127
128        open $iofh, '+<', $iofile or die "Can't update file '$iofile': $!\n";
129        binmode $iofh;
130        ok(crypt_file($iofh, CRYPT_MODE_AUTO()),
131           'crypt_file($fh, CRYPT_MODE_AUTO) returned OK') or
132           diag("\$ErrStr = '$ErrStr'");
133        close $iofh;
134
135        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
136        $contents = do { local $/; <$fh> };
137        close $fh;
138        like($contents, $qrhead, '... and encrypted file OK');
139
140        SKIP: {
141            skip 'Decrypt component not built', 1 unless $have_decrypt;
142            chomp($line = qx{$perl $iofile});
143            is($line, $str, '... and encrypted file runs OK');
144        }
145
146        ok(crypt_file($iofile, CRYPT_MODE_AUTO()),
147           'crypt_file($file, CRYPT_MODE_AUTO) returned OK') or
148           diag("\$ErrStr = '$ErrStr'");
149
150        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
151        $contents = do { local $/; <$fh> };
152        close $fh;
153        is($contents, $prog, '... and decrypted file OK');
154
155        chomp($line = qx{$perl $iofile});
156        is($line, $str, '... and decrypted file runs OK');
157
158        open $iofh, '+<', $iofile or die "Can't update file '$iofile': $!\n";
159        binmode $iofh;
160        ok(crypt_file($iofh, CRYPT_MODE_ENCRYPT()),
161           'crypt_file($fh, CRYPT_MODE_ENCRYPT) returned OK') or
162           diag("\$ErrStr = '$ErrStr'");
163        close $iofh;
164
165        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
166        $contents = do { local $/; <$fh> };
167        close $fh;
168        like($contents, $qrhead, '... and encrypted file OK');
169
170        SKIP: {
171            skip 'Decrypt component not built', 1 unless $have_decrypt;
172            chomp($line = qx{$perl $iofile});
173            is($line, $str, '... and encrypted file runs OK');
174        }
175
176        $saved_contents = $contents;
177
178        open $iofh, '+<', $iofile or die "Can't update file '$iofile': $!\n";
179        binmode $iofh;
180        ok(crypt_file($iofh, CRYPT_MODE_ENCRYPTED()),
181           'crypt_file($fh, CRYPT_MODE_ENCRYPTED) returned OK') or
182           diag("\$ErrStr = '$ErrStr'");
183        close $iofh;
184
185        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
186        $contents = do { local $/; <$fh> };
187        close $fh;
188        is($contents, $saved_contents, '... and left file encrypted');
189
190        SKIP: {
191            skip 'Decrypt component not built', 1 unless $have_decrypt;
192            chomp($line = qx{$perl $iofile});
193            is($line, $str, '... and encrypted file still runs OK');
194        }
195
196        ok(crypt_file($iofile, CRYPT_MODE_DECRYPT()),
197           'crypt_file($file, CRYPT_MODE_DECRYPT) returned OK') or
198           diag("\$ErrStr = '$ErrStr'");
199
200        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
201        $contents = do { local $/; <$fh> };
202        close $fh;
203        is($contents, $prog, '... and decrypted file OK');
204
205        chomp($line = qx{$perl $iofile});
206        is($line, $str, '... and decrypted file runs OK');
207
208        ok(crypt_file($iofile, CRYPT_MODE_DECRYPTED()),
209           'crypt_file($file, CRYPT_MODE_DECRYPTED) returned OK') or
210           diag("\$ErrStr = '$ErrStr'");
211
212        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
213        $contents = do { local $/; <$fh> };
214        close $fh;
215        is($contents, $prog, '... and left file decrypted');
216
217        chomp($line = qx{$perl $iofile});
218        is($line, $str, '... and decrypted file still runs OK');
219
220        $ofile = new_ofilename();
221
222        open $ifh, '<', $ifile or die "Can't read file '$ifile': $!\n";
223        binmode $ifh;
224        open $ofh, '>', $ofile or die "Can't write file '$ofile': $!\n";
225        binmode $ofh;
226        ok(crypt_file($ifh, $ofh), 'crypt_file($fh1, $fh2) returned OK') or
227            diag("\$ErrStr = '$ErrStr'");
228        close $ofh;
229        close $ifh;
230
231        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
232        $contents = do { local $/; <$fh> };
233        close $fh;
234        is($contents, $prog, '... and left input file unencrypted');
235        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
236        $contents = do { local $/; <$fh> };
237        close $fh;
238        like($contents, $qrhead, '... and encrypted output file OK');
239
240        SKIP: {
241            skip 'Decrypt component not built', 1 unless $have_decrypt;
242            chomp($line = qx{$perl $ofile});
243            is($line, $str, '... and encrypted output file runs OK');
244        }
245
246        unlink $ifile;
247        $ifile = new_ifilename();
248
249        open $ofh, '<', $ofile or die "Can't read file '$ofile': $!\n";
250        binmode $ofh;
251        ok(crypt_file($ofh, $ifile), 'crypt_file($fh, $file) returned OK') or
252            diag("\$ErrStr = '$ErrStr'");
253        close $ofh;
254
255        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
256        $contents = do { local $/; <$fh> };
257        close $fh;
258        like($contents, $qrhead, '... and left input file encrypted');
259        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
260        $contents = do { local $/; <$fh> };
261        close $fh;
262        is($contents, $prog, '... and decrypted output file OK');
263
264        chomp($line = qx{$perl $ifile});
265        is($line, $str, '... and decrypted output file runs OK');
266
267        unlink $ofile;
268        $ofile = new_ofilename();
269
270        open $ofh, '>', $ofile or die "Can't write file '$ofile': $!\n";
271        binmode $ofh;
272        ok(crypt_file($ifile, $ofh), 'crypt_file($file, $fh) returned OK') or
273            diag("\$ErrStr = '$ErrStr'");
274        close $ofh;
275
276        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
277        $contents = do { local $/; <$fh> };
278        close $fh;
279        is($contents, $prog, '... and left input file unencrypted');
280        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
281        $contents = do { local $/; <$fh> };
282        close $fh;
283        like($contents, $qrhead, '... and encrypted output file OK');
284
285        SKIP: {
286            skip 'Decrypt component not built', 1 unless $have_decrypt;
287            chomp($line = qx{$perl $ofile});
288            is($line, $str, '... and encrypted output file runs OK');
289        }
290
291        unlink $ifile;
292        $ifile = new_ifilename();
293
294        ok(crypt_file($ofile, $ifile), 'crypt_file($file1, $file2) returned OK') or
295            diag("\$ErrStr = '$ErrStr'");
296
297        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
298        $contents = do { local $/; <$fh> };
299        close $fh;
300        like($contents, $qrhead, '... and left input file encrypted');
301        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
302        $contents = do { local $/; <$fh> };
303        close $fh;
304        is($contents, $prog, '... and decrypted output file OK');
305
306        chomp($line = qx{$perl $ifile});
307        is($line, $str, '... and decrypted output file runs OK');
308
309        unlink $ofile;
310        $ofile = new_ofilename();
311
312        open $ifh, '<', $ifile or die "Can't read file '$ifile': $!\n";
313        binmode $ifh;
314        open $ofh, '>', $ofile or die "Can't write file '$ofile': $!\n";
315        binmode $ofh;
316        ok(crypt_file($ifh, $ofh, CRYPT_MODE_AUTO()),
317           'crypt_file($fh1, $fh2, CRYPT_MODE_AUTO) returned OK') or
318           diag("\$ErrStr = '$ErrStr'");
319        close $ofh;
320        close $ifh;
321
322        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
323        $contents = do { local $/; <$fh> };
324        close $fh;
325        is($contents, $prog, '... and left input file unencrypted');
326        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
327        $contents = do { local $/; <$fh> };
328        close $fh;
329        like($contents, $qrhead, '... and encrypted output file OK');
330
331        SKIP: {
332            skip 'Decrypt component not built', 1 unless $have_decrypt;
333            chomp($line = qx{$perl $ofile});
334            is($line, $str, '... and encrypted output file runs OK');
335        }
336
337        unlink $ifile;
338        $ifile = new_ifilename();
339
340        open $ofh, '<', $ofile or die "Can't read file '$ofile': $!\n";
341        binmode $ofh;
342        ok(crypt_file($ofh, $ifile, CRYPT_MODE_AUTO()),
343           'crypt_file($fh, $file, CRYPT_MODE_AUTO) returned OK') or
344           diag("\$ErrStr = '$ErrStr'");
345        close $ofh;
346
347        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
348        $contents = do { local $/; <$fh> };
349        close $fh;
350        like($contents, $qrhead, '... and left input file encrypted');
351        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
352        $contents = do { local $/; <$fh> };
353        close $fh;
354        is($contents, $prog, '... and decrypted output file OK');
355
356        chomp($line = qx{$perl $ifile});
357        is($line, $str, '... and decrypted output file runs OK');
358
359        unlink $ofile;
360        $ofile = new_ofilename();
361
362        open $ofh, '>', $ofile or die "Can't write file '$ofile': $!\n";
363        binmode $ofh;
364        ok(crypt_file($ifile, $ofh, CRYPT_MODE_AUTO()),
365           'crypt_file($file, $fh, CRYPT_MODE_AUTO) returned OK') or
366           diag("\$ErrStr = '$ErrStr'");
367        close $ofh;
368
369        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
370        $contents = do { local $/; <$fh> };
371        close $fh;
372        is($contents, $prog, '... and left input file unencrypted');
373        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
374        $contents = do { local $/; <$fh> };
375        close $fh;
376        like($contents, $qrhead, '... and encrypted output file OK');
377
378        SKIP: {
379            skip 'Decrypt component not built', 1 unless $have_decrypt;
380            chomp($line = qx{$perl $ofile});
381            is($line, $str, '... and encrypted output file runs OK');
382        }
383
384        unlink $ifile;
385        $ifile = new_ifilename();
386
387        ok(crypt_file($ofile, $ifile, CRYPT_MODE_AUTO()),
388           'crypt_file($file1, $file2, CRYPT_MODE_AUTO) returned OK') or
389           diag("\$ErrStr = '$ErrStr'");
390        close $ofh;
391
392        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
393        $contents = do { local $/; <$fh> };
394        close $fh;
395        like($contents, $qrhead, '... and left input file encrypted');
396        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
397        $contents = do { local $/; <$fh> };
398        close $fh;
399        is($contents, $prog, '... and decrypted output file OK');
400
401        chomp($line = qx{$perl $ifile});
402        is($line, $str, '... and decrypted output file runs OK');
403
404        unlink $ofile;
405        $ofile = new_ofilename();
406
407        open $ifh, '<', $ifile or die "Can't read file '$ifile': $!\n";
408        binmode $ifh;
409        open $ofh, '>', $ofile or die "Can't write file '$ofile': $!\n";
410        binmode $ofh;
411        ok(crypt_file($ifh, $ofh, CRYPT_MODE_ENCRYPT()),
412           'crypt_file($fh1, $fh2, CRYPT_MODE_ENCRYPT) returned OK') or
413           diag("\$ErrStr = '$ErrStr'");
414        close $ofh;
415        close $ifh;
416
417        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
418        $contents = do { local $/; <$fh> };
419        close $fh;
420        is($contents, $prog, '... and left intput file unencrypted');
421        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
422        $contents = do { local $/; <$fh> };
423        close $fh;
424        like($contents, $qrhead, '... and encrypted output file OK');
425
426        SKIP: {
427            skip 'Decrypt component not built', 1 unless $have_decrypt;
428            chomp($line = qx{$perl $ofile});
429            is($line, $str, '... and encrypted output file runs OK');
430        }
431
432        unlink $ofile;
433        $ofile = new_ofilename();
434
435        open $ifh, '<', $ifile or die "Can't read file '$ifile': $!\n";
436        binmode $ifh;
437        open $ofh, '>', $ofile or die "Can't write file '$ofile': $!\n";
438        binmode $ofh;
439        ok(crypt_file($ifh, $ofh, CRYPT_MODE_ENCRYPTED()),
440           'crypt_file($fh1, $fh2, CRYPT_MODE_ENCRYPTED) returned OK') or
441           diag("\$ErrStr = '$ErrStr'");
442        close $ofh;
443        close $ifh;
444
445        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
446        $contents = do { local $/; <$fh> };
447        close $fh;
448        is($contents, $prog, '... and left input file unencrypted');
449        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
450        $contents = do { local $/; <$fh> };
451        close $fh;
452        like($contents, $qrhead, '... and encrypted output file OK');
453
454        SKIP: {
455            skip 'Decrypt component not built', 1 unless $have_decrypt;
456            chomp($line = qx{$perl $ofile});
457            is($line, $str, '... and encrypted output file runs OK');
458        }
459
460        unlink $ifile;
461        $ifile = new_ifilename();
462
463        open $ofh, '<', $ofile or die "Can't read file '$ofile': $!\n";
464        binmode $ofh;
465        ok(crypt_file($ofh, $ifile, CRYPT_MODE_DECRYPT()),
466           'crypt_file($fh, $file, CRYPT_MODE_DECRYPT) returned OK') or
467           diag("\$ErrStr = '$ErrStr'");
468        close $ofh;
469
470        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
471        $contents = do { local $/; <$fh> };
472        close $fh;
473        like($contents, $qrhead, '... and left intput file encrypted');
474        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
475        $contents = do { local $/; <$fh> };
476        close $fh;
477        is($contents, $prog, '... and decrypted output file OK');
478
479        chomp($line = qx{$perl $ifile});
480        is($line, $str, '... and decrypted output file runs OK');
481
482        unlink $ifile;
483        $ifile = new_ifilename();
484
485        open $ofh, '<', $ofile or die "Can't read file '$ofile': $!\n";
486        binmode $ofh;
487        ok(crypt_file($ofh, $ifile, CRYPT_MODE_DECRYPTED()),
488           'crypt_file($fh, $file, CRYPT_MODE_DECRYPTED) returned OK') or
489           diag("\$ErrStr = '$ErrStr'");
490        close $ofh;
491
492        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
493        $contents = do { local $/; <$fh> };
494        close $fh;
495        like($contents, $qrhead, '... and left input file encrypted');
496        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
497        $contents = do { local $/; <$fh> };
498        close $fh;
499        is($contents, $prog, '... and decrypted output file OK');
500
501        chomp($line = qx{$perl $ifile});
502        is($line, $str, '... and decrypted output file runs OK');
503
504        unlink $ofile;
505        $ofile = new_ofilename();
506
507        open $ofh, '>', $ofile or die "Can't write file '$ofile': $!\n";
508        binmode $ofh;
509        ok(crypt_file($ifile, $ofh, CRYPT_MODE_ENCRYPT()),
510           'crypt_file($file, $fh, CRYPT_MODE_ENCRYPT) returned OK') or
511           diag("\$ErrStr = '$ErrStr'");
512        close $ofh;
513
514        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
515        $contents = do { local $/; <$fh> };
516        close $fh;
517        is($contents, $prog, '... and left input file unencrypted');
518        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
519        $contents = do { local $/; <$fh> };
520        close $fh;
521        like($contents, $qrhead, '... and encrypted output file OK');
522
523        SKIP: {
524            skip 'Decrypt component not built', 1 unless $have_decrypt;
525            chomp($line = qx{$perl $ofile});
526            is($line, $str, '... and encrypted output file runs OK');
527        }
528
529        unlink $ofile;
530        $ofile = new_ofilename();
531
532        open $ofh, '>', $ofile or die "Can't write file '$ofile': $!\n";
533        binmode $ofh;
534        ok(crypt_file($ifile, $ofh, CRYPT_MODE_ENCRYPTED()),
535           'crypt_file($file, $fh, CRYPT_MODE_ENCRYPTED) returned OK') or
536           diag("\$ErrStr = '$ErrStr'");
537        close $ofh;
538
539        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
540        $contents = do { local $/; <$fh> };
541        close $fh;
542        is($contents, $prog, '... and left input file unencrypted');
543        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
544        $contents = do { local $/; <$fh> };
545        close $fh;
546        like($contents, $qrhead, '... and encrypted output file OK');
547
548        SKIP: {
549            skip 'Decrypt component not built', 1 unless $have_decrypt;
550            chomp($line = qx{$perl $ofile});
551            is($line, $str, '... and encrypted output file runs OK');
552        }
553
554        unlink $ifile;
555        $ifile = new_ifilename();
556
557        ok(crypt_file($ofile, $ifile, CRYPT_MODE_DECRYPT()),
558           'crypt_file($file1, $file2, CRYPT_MODE_DECRYPT) returned OK') or
559           diag("\$ErrStr = '$ErrStr'");
560        close $ofh;
561
562        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
563        $contents = do { local $/; <$fh> };
564        close $fh;
565        like($contents, $qrhead, '... and left input file encrypted');
566        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
567        $contents = do { local $/; <$fh> };
568        close $fh;
569        is($contents, $prog, '... and decrypted output file OK');
570
571        chomp($line = qx{$perl $ifile});
572        is($line, $str, '... and decrypted output file runs OK');
573
574        unlink $ifile;
575        $ifile = new_ifilename();
576
577        ok(crypt_file($ofile, $ifile, CRYPT_MODE_DECRYPTED()),
578           'crypt_file($file1, $file2, CRYPT_MODE_DECRYPTED) returned OK') or
579           diag("\$ErrStr = '$ErrStr'");
580        close $ofh;
581
582        open $fh, '<', $ofile or die "Can't read file '$ofile': $!\n";
583        $contents = do { local $/; <$fh> };
584        close $fh;
585        like($contents, $qrhead, '... and left input file encrypted');
586        open $fh, '<', $ifile or die "Can't read file '$ifile': $!\n";
587        $contents = do { local $/; <$fh> };
588        close $fh;
589        is($contents, $prog, '... and decrypted output file OK');
590
591        SKIP: {
592            skip 'Decrypt component not built', 1 unless $have_decrypt;
593            chomp($line = qx{$perl $ifile});
594            is($line, $str, '... and decrypted output file runs OK');
595        }
596
597        unlink $ifile;
598        unlink $ofile;
599    }
600
601    {
602        $iofile = new_ifilename();
603
604        $prog =~ s/\n$//o;
605        open $fh, '>', $iofile or die "Can't create file '$iofile': $!\n";
606        print $fh $prog;
607        close $fh;
608
609        ok(crypt_file($iofile), 'file without newline at EOF: OK') or
610           diag("\$ErrStr = '$ErrStr'");
611
612        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
613        $contents = do { local $/; <$fh> };
614        close $fh;
615        like($contents, $qrhead, '... and file encrypted OK');
616
617        SKIP: {
618            skip 'Decrypt component not built', 1 unless $have_decrypt;
619            chomp($line = qx{$perl $iofile});
620            is($line, $str, '... and encrypted file runs OK');
621        }
622
623        for ($i = 1; $i <= 16; $i++) {
624            open $fh, '>', $iofile or die "Can't create file '$iofile': $!\n";
625            binmode $fh;
626            print $fh +(';' x ($i - 1)) . "\n";
627            close $fh;
628
629            ok(crypt_file($iofile), "$i byte file with newline at EOF: OK") or
630                diag("\$ErrStr = '$ErrStr'");
631
632            open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
633            $contents = do { local $/; <$fh> };
634            close $fh;
635            like($contents, $qrhead, '... and file encrypted OK');
636
637            SKIP: {
638                skip 'Decrypt component not built', 1 unless $have_decrypt;
639                chomp($line = qx{$perl $iofile});
640                is($line, '', '... and encrypted file runs OK');
641            }
642        }
643
644        for ($i = 1; $i <= 16; $i++) {
645            open $fh, '>', $iofile or die "Can't create file '$iofile': $!\n";
646            print $fh ';' x $i;
647            close $fh;
648
649            ok(crypt_file($iofile), "$i byte file without newline at EOF: OK") or
650                diag("\$ErrStr = '$ErrStr'");
651
652            open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
653            $contents = do { local $/; <$fh> };
654            close $fh;
655            like($contents, $qrhead, '... and file encrypted OK');
656
657            SKIP: {
658                skip 'Decrypt component not built', 1 unless $have_decrypt;
659                chomp($line = qx{$perl $iofile});
660                is($line, '', '... and encrypted file runs OK');
661            }
662        }
663
664        for ($i = 1; $i <= 16; $i++) {
665            $buf = ';' x $i;
666            open $fh, '>', $iofile or die "Can't create file '$iofile': $!\n";
667            print $fh qq[print "$buf";\n];
668            close $fh;
669
670            $n = -s $iofile;
671            ok(crypt_file($iofile), "$n byte file with newline at EOF: OK") or
672                diag("\$ErrStr = '$ErrStr'");
673
674            open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
675            $contents = do { local $/; <$fh> };
676            close $fh;
677            like($contents, $qrhead, '... and file encrypted OK');
678
679            SKIP: {
680                skip 'Decrypt component not built', 1 unless $have_decrypt;
681                chomp($line = qx{$perl $iofile});
682                is($line, $buf, '... and encrypted file runs OK');
683            }
684        }
685
686        for ($i = 1; $i <= 16; $i++) {
687            $buf = ';' x $i;
688            open $fh, '>', $iofile or die "Can't create file '$iofile': $!\n";
689            print $fh qq[print "$buf";];
690            close $fh;
691
692            $n = -s $iofile;
693            ok(crypt_file($iofile), "$n byte file without newline at EOF: OK") or
694                diag("\$ErrStr = '$ErrStr'");
695
696            open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
697            $contents = do { local $/; <$fh> };
698            close $fh;
699            like($contents, $qrhead, '... and file encrypted OK');
700
701                SKIP: {
702                skip 'Decrypt component not built', 1 unless $have_decrypt;
703                chomp($line = qx{$perl $iofile});
704                is($line, $buf, '... and encrypted file runs OK');
705            }
706        }
707
708        $buf = ';' x 4096;
709        open $fh, '>', $iofile or die "Can't create file '$iofile': $!\n";
710        print $fh qq[print "$buf";\n];
711        close $fh;
712
713        $n = -s $iofile;
714        ok(crypt_file($iofile), "$n byte file with newline at EOF: OK") or
715            diag("\$ErrStr = '$ErrStr'");
716
717        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
718        $contents = do { local $/; <$fh> };
719        close $fh;
720        like($contents, $qrhead, '... and file encrypted OK');
721
722        SKIP: {
723            skip 'Decrypt component not built', 1 unless $have_decrypt;
724            chomp($line = qx{$perl $iofile});
725            is($line, $buf, '... and encrypted file runs OK');
726        }
727
728        $buf = ';' x 4096;
729        open $fh, '>', $iofile or die "Can't create file '$iofile': $!\n";
730        print $fh qq[print "$buf";];
731        close $fh;
732
733        $n = -s $iofile;
734        ok(crypt_file($iofile), "$n byte file without newline at EOF: OK") or
735            diag("\$ErrStr = '$ErrStr'");
736
737        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
738        $contents = do { local $/; <$fh> };
739        close $fh;
740        like($contents, $qrhead, '... and file encrypted OK');
741
742        SKIP: {
743            skip 'Decrypt component not built', 1 unless $have_decrypt;
744            chomp($line = qx{$perl $iofile});
745            is($line, $buf, '... and encrypted file runs OK');
746        }
747
748        open $fh, '>', $iofile or die "Can't create file '$iofile': $!\n";
749        print $fh $prog;
750        close $fh;
751
752        ok(crypt_file($iofile), 'crypt_file($file) returned OK') or
753            diag("\$ErrStr = '$ErrStr'");
754
755        open $fh, '<', $iofile or die "Can't read file '$iofile': $!\n";
756        $contents = do { local $/; <$fh> };
757        close $fh;
758        like($contents, $qrhead, '... and file encrypted OK');
759
760        SKIP: {
761            skip 'Decrypt component not built', 1 unless $have_decrypt;
762            chomp($line = qx{$perl -MCarp $iofile});
763            is($line, $str, '... and encrypted file runs OK with Carp loaded');
764        }
765
766        unlink $iofile;
767    }
768
769    {
770        open $fh, '>', $script or die "Can't create file '$script': $!\n";
771        print $fh $scrsrc;
772        close $fh;
773
774        eval { mkpath($moddir) } or die "Can't create directory '$moddir': $@\n";
775        open $fh, '>', $module or die "Can't create file '$module': $!\n";
776        print $fh $modsrc;
777        close $fh;
778
779        ok(crypt_file($module), 'crypt_file($file) returned OK') or
780            diag("\$ErrStr = '$ErrStr'");
781
782        open $fh, '<', $module or die "Can't read file '$module': $!\n";
783        $contents = do { local $/; <$fh> };
784        close $fh;
785        like($contents, $qrhead, '... and module encrypted OK');
786
787        SKIP: {
788            skip 'Decrypt component not built', 1 unless $have_decrypt;
789            chomp($line = qx{$perl -I. $script});
790            is($line, $str, '... and encrypted module runs OK with Carp loaded');
791        }
792
793        unlink $script;
794        unlink $module;
795        rmtree($modroot, 0, 1);
796    }
797}
798
799#===============================================================================
800