doc: Move Perl version baseline as the first perl coding style subsection
[dpkg.git] / scripts / t / Dpkg_OpenPGP.t
blobc3cd724824cd4d287e61c633c6a4cbb0d29f5ed1
1 #!/usr/bin/perl
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 use strict;
17 use warnings;
19 use Test::More;
20 use Test::Dpkg qw(:paths :needs);
22 use File::Compare;
24 use Dpkg::ErrorHandling;
25 use Dpkg::Path qw(find_command);
26 use Dpkg::OpenPGP::KeyHandle;
28 my %backend_cmd = (
29 auto => 'auto',
30 gpg => 'gpg',
31 sq => 'sq',
32 sqop => 'sop',
33 'pgpainless-cli' => 'sop',
35 my @cmds = test_needs_openpgp_backend();
36 unshift @cmds, 'auto';
38 plan tests => 2 + 15 * scalar @cmds;
40 use_ok('Dpkg::OpenPGP');
41 use_ok('Dpkg::OpenPGP::ErrorCodes');
43 report_options(quiet_warnings => 1);
45 sub test_diff
47 my ($exp_file, $gen_file, $desc) = @_;
49 my $res = compare($exp_file, $gen_file);
50 if ($res) {
51 system "diff -u '$exp_file' '$gen_file' >&2";
53 ok($res == 0, "$desc ($exp_file vs $gen_file)");
56 foreach my $cmd (@cmds) {
57 my $datadir = test_get_data_path();
58 my $tempdir = test_get_temp_path();
60 my $backend = $backend_cmd{$cmd};
61 my $openpgp = Dpkg::OpenPGP->new(
62 backend => $backend,
63 cmd => $cmd,
66 ok($openpgp->dearmor('PUBLIC KEY BLOCK', "$datadir/dpkg-test-pub.asc", "$tempdir/dpkg-test-pub.pgp") == OPENPGP_OK(),
67 "($backend:$cmd) dearmoring OpenPGP ASCII Armored certificate");
68 ok($openpgp->armor('PUBLIC KEY BLOCK', "$tempdir/dpkg-test-pub.pgp", "$tempdir/dpkg-test-pub.asc") == OPENPGP_OK(),
69 "($backend:$cmd) armoring OpenPGP binary certificate");
70 test_diff("$datadir/dpkg-test-pub.asc", "$tempdir/dpkg-test-pub.asc",
71 "($backend:$cmd) OpenPGP certificate dearmor/armor round-trip correctly");
73 ok($openpgp->armor('SIGNATURE', "$datadir/sign-file.sig", "$tempdir/sign-file.asc") == OPENPGP_OK(),
74 "($backend:$cmd) armoring OpenPGP binary signature succeeded");
75 ok(compare("$datadir/sign-file.sig", "$tempdir/sign-file.asc") != 0,
76 "($backend:$cmd) armoring OpenPGP ASCII Armor changed the file");
77 ok($openpgp->armor('SIGNATURE', "$datadir/sign-file.asc", "$tempdir/sign-file-rearmor.asc") == OPENPGP_OK(),
78 "($backend:$cmd) armoring OpenPGP armored signature succeeded");
79 test_diff("$datadir/sign-file.asc", "$tempdir/sign-file-rearmor.asc",
80 "($backend:$cmd) rearmoring OpenPGP ASCII Armor changed the file");
82 ok($openpgp->dearmor('SIGNATURE', "$tempdir/sign-file.asc", "$tempdir/sign-file.sig") == OPENPGP_OK(),
83 "($backend:$cmd) dearmoring OpenPGP armored signature succeeded");
84 test_diff("$datadir/sign-file.sig", "$tempdir/sign-file.sig",
85 "($backend:$cmd) dearmored OpenPGP ASCII Armor signature matches");
87 my $cert = "$datadir/dpkg-test-pub.asc";
89 ok($openpgp->inline_verify("$datadir/sign-file-inline.asc", undef, $cert) == OPENPGP_OK(),
90 "($backend:$cmd) verify OpenPGP ASCII Armor inline signature");
91 ok($openpgp->inline_verify("$datadir/sign-file-inline.sig", undef, $cert) == OPENPGP_OK(),
92 "($backend:$cmd) verify OpenPGP binary inline signature");
94 ok($openpgp->verify("$datadir/sign-file", "$datadir/sign-file.asc", $cert) == OPENPGP_OK(),
95 "($backend:$cmd) verify OpenPGP ASCII Armor detached signature");
96 ok($openpgp->verify("$datadir/sign-file", "$datadir/sign-file.sig", $cert) == OPENPGP_OK(),
97 "($backend:$cmd) verify OpenPGP binary detached signature");
99 my $key = Dpkg::OpenPGP::KeyHandle->new(
100 type => 'keyfile',
101 handle => "$datadir/dpkg-test-sec.asc",
104 SKIP: {
105 skip 'cannot use secrets', 2 unless $openpgp->can_use_secrets($key);
107 ok($openpgp->inline_sign("$datadir/sign-file", "$tempdir/sign-file-inline.asc", $key) == OPENPGP_OK(),
108 "($backend:$cmd) inline OpenPGP sign");
109 ok($openpgp->inline_verify("$tempdir/sign-file-inline.asc", undef, $cert) == OPENPGP_OK(),
110 "($backend:$cmd) verify generated inline OpenPGP signature");
113 # TODO: Add more test cases.