xref: /freebsd/crypto/openssl/test/recipes/20-test_enc.t (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubertuse strict;
11*e0c4386eSCy Schubertuse warnings;
12*e0c4386eSCy Schubert
13*e0c4386eSCy Schubertuse File::Spec::Functions qw/catfile/;
14*e0c4386eSCy Schubertuse File::Copy;
15*e0c4386eSCy Schubertuse File::Compare qw/compare_text/;
16*e0c4386eSCy Schubertuse File::Basename;
17*e0c4386eSCy Schubertuse OpenSSL::Test qw/:DEFAULT srctop_file bldtop_dir/;
18*e0c4386eSCy Schubertuse OpenSSL::Test::Utils;
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubertsetup("test_enc");
21*e0c4386eSCy Schubertplan skip_all => "Deprecated functions are disabled in this OpenSSL build"
22*e0c4386eSCy Schubert    if disabled("deprecated");
23*e0c4386eSCy Schubert
24*e0c4386eSCy Schubert# We do it this way, because setup() may have moved us around,
25*e0c4386eSCy Schubert# so the directory portion of $0 might not be correct any more.
26*e0c4386eSCy Schubert# However, the name hasn't changed.
27*e0c4386eSCy Schubertmy $testsrc = srctop_file("test","recipes",basename($0));
28*e0c4386eSCy Schubert
29*e0c4386eSCy Schubertmy $test = catfile(".", "p");
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubertmy $cmd = "openssl";
32*e0c4386eSCy Schubertmy $provpath = bldtop_dir("providers");
33*e0c4386eSCy Schubertmy @prov = ("-provider-path", $provpath, "-provider", "default");
34*e0c4386eSCy Schubertpush @prov, ("-provider", "legacy") unless disabled("legacy");
35*e0c4386eSCy Schubertmy $ciphersstatus = undef;
36*e0c4386eSCy Schubertmy @ciphers =
37*e0c4386eSCy Schubert    map { s/^\s+//; s/\s+$//; split /\s+/ }
38*e0c4386eSCy Schubert    run(app([$cmd, "list", "-cipher-commands"]),
39*e0c4386eSCy Schubert        capture => 1, statusvar => \$ciphersstatus);
40*e0c4386eSCy Schubert@ciphers = grep {!/^(bf|cast|des$|des-cbc|des-cfb|des-ecb|des-ofb|desx|idea
41*e0c4386eSCy Schubert                     |rc2|rc4|seed)/x} @ciphers
42*e0c4386eSCy Schubert    if disabled("legacy");
43*e0c4386eSCy Schubert
44*e0c4386eSCy Schubertplan tests => 2 + (scalar @ciphers)*2;
45*e0c4386eSCy Schubert
46*e0c4386eSCy Schubert SKIP: {
47*e0c4386eSCy Schubert     skip "Problems getting ciphers...", 1 + scalar(@ciphers)
48*e0c4386eSCy Schubert         unless ok($ciphersstatus, "Running 'openssl list -cipher-commands'");
49*e0c4386eSCy Schubert     unless (ok(copy($testsrc, $test), "Copying $testsrc to $test")) {
50*e0c4386eSCy Schubert         diag($!);
51*e0c4386eSCy Schubert         skip "Not initialized, skipping...", scalar(@ciphers);
52*e0c4386eSCy Schubert     }
53*e0c4386eSCy Schubert
54*e0c4386eSCy Schubert     foreach my $c (@ciphers) {
55*e0c4386eSCy Schubert         my %variant = ("$c" => [],
56*e0c4386eSCy Schubert                        "$c base64" => [ "-a" ]);
57*e0c4386eSCy Schubert
58*e0c4386eSCy Schubert         foreach my $t (sort keys %variant) {
59*e0c4386eSCy Schubert             my $cipherfile = "$test.$c.cipher";
60*e0c4386eSCy Schubert             my $clearfile = "$test.$c.clear";
61*e0c4386eSCy Schubert             my @e = ( "$c", "-bufsize", "113", @{$variant{$t}}, "-e", "-k", "test" );
62*e0c4386eSCy Schubert             my @d = ( "$c", "-bufsize", "157", @{$variant{$t}}, "-d", "-k", "test" );
63*e0c4386eSCy Schubert             if ($c eq "cat") {
64*e0c4386eSCy Schubert                 $cipherfile = "$test.cipher";
65*e0c4386eSCy Schubert                 $clearfile = "$test.clear";
66*e0c4386eSCy Schubert                 @e = ( "enc", @{$variant{$t}}, "-e" );
67*e0c4386eSCy Schubert                 @d = ( "enc", @{$variant{$t}}, "-d" );
68*e0c4386eSCy Schubert             }
69*e0c4386eSCy Schubert
70*e0c4386eSCy Schubert             ok(run(app([$cmd, @e, @prov, "-in", $test, "-out", $cipherfile]))
71*e0c4386eSCy Schubert                && run(app([$cmd, @d, @prov, "-in", $cipherfile, "-out", $clearfile]))
72*e0c4386eSCy Schubert                && compare_text($test,$clearfile) == 0, $t);
73*e0c4386eSCy Schubert         }
74*e0c4386eSCy Schubert     }
75*e0c4386eSCy Schubert}
76