This is an automated email from the git hooks/post-receive script. guillem pushed a commit to branch main in repository dpkg.
View the commit online: https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=c0511ef62988d5598b838b226590630feb8452dd commit c0511ef62988d5598b838b226590630feb8452dd Author: Guillem Jover <[email protected]> AuthorDate: Tue Jul 19 13:58:17 2022 +0200 Dpkg::OpenPGP: Refactor is_armored() out from openpgp_sig_to_asc() --- scripts/Dpkg/OpenPGP.pm | 29 +++++++++++++++++------------ scripts/Makefile.am | 2 ++ scripts/t/Dpkg_OpenPGP.t | 11 +++++++++-- scripts/t/Dpkg_OpenPGP/data-file | 5 +++++ scripts/t/Dpkg_OpenPGP/data-file.asc | 8 ++++++++ 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/scripts/Dpkg/OpenPGP.pm b/scripts/Dpkg/OpenPGP.pm index e51e5ac91..5e9e634d9 100644 --- a/scripts/Dpkg/OpenPGP.pm +++ b/scripts/Dpkg/OpenPGP.pm @@ -33,6 +33,22 @@ our @EXPORT = qw( openpgp_sig_to_asc ); +sub is_armored { + my $file = shift; + my $armored = 0; + + open my $fh, '<', $file or syserr(g_('cannot open %s'), $file); + while (<$fh>) { + if (m/^-----BEGIN PGP /) { + $armored = 1; + last; + } + } + close $fh; + + return $armored; +} + sub _gpg_armor { my ($sig, $asc) = @_; @@ -62,18 +78,7 @@ sub openpgp_sig_to_asc my ($sig, $asc) = @_; if (-e $sig) { - my $is_openpgp_ascii_armor = 0; - - open my $fh_sig, '<', $sig or syserr(g_('cannot open %s'), $sig); - while (<$fh_sig>) { - if (m/^-----BEGIN PGP /) { - $is_openpgp_ascii_armor = 1; - last; - } - } - close $fh_sig; - - if ($is_openpgp_ascii_armor) { + if (is_armored($sig)) { notice(g_('signature file is already OpenPGP ASCII armor, copying')); copy($sig, $asc); return $asc; diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 3e7053617..922515a2c 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -282,6 +282,8 @@ test_data = \ t/Dpkg_File/append-me \ t/Dpkg_File/dump-partial \ t/Dpkg_File/slurp-me \ + t/Dpkg_OpenPGP/data-file \ + t/Dpkg_OpenPGP/data-file.asc \ t/Dpkg_OpenPGP/package_1.0.orig.tar \ t/Dpkg_OpenPGP/package_1.0.orig.tar.asc \ t/Dpkg_OpenPGP/package_1.0.orig.tar.sig \ diff --git a/scripts/t/Dpkg_OpenPGP.t b/scripts/t/Dpkg_OpenPGP.t index 164395117..214212f87 100644 --- a/scripts/t/Dpkg_OpenPGP.t +++ b/scripts/t/Dpkg_OpenPGP.t @@ -25,7 +25,7 @@ use Dpkg::ErrorHandling; test_needs_command('gpg'); -plan tests => 6; +plan tests => 8; use_ok('Dpkg::OpenPGP'); @@ -33,7 +33,14 @@ report_options(quiet_warnings => 1); my $datadir = test_get_data_path(); my $tmpdir = test_get_temp_path(); -my $ascfile; + +my ($reffile, $binfile, $ascfile); + +$binfile = "$datadir/data-file"; +$reffile = "$datadir/data-file.asc"; + +ok(!Dpkg::OpenPGP::is_armored($binfile), 'file not ASCII Armored'); +ok(Dpkg::OpenPGP::is_armored($reffile), 'file ASCII Armored'); $ascfile = "$tmpdir/package_1.0.orig.tar.enoent"; is(openpgp_sig_to_asc("$datadir/nonexistent", $ascfile), diff --git a/scripts/t/Dpkg_OpenPGP/data-file b/scripts/t/Dpkg_OpenPGP/data-file new file mode 100644 index 000000000..65e7b36ef --- /dev/null +++ b/scripts/t/Dpkg_OpenPGP/data-file @@ -0,0 +1,5 @@ +This is a data file that will be armored +as part of the dpkg OpenPGP test suite. + +It contains «UTF-8» characters so that +we can check → non-ASCII Armor ☺. diff --git a/scripts/t/Dpkg_OpenPGP/data-file.asc b/scripts/t/Dpkg_OpenPGP/data-file.asc new file mode 100644 index 000000000..614bc2b27 --- /dev/null +++ b/scripts/t/Dpkg_OpenPGP/data-file.asc @@ -0,0 +1,8 @@ +-----BEGIN PGP ARMORED FILE----- + +VGhpcyBpcyBhIGRhdGEgZmlsZSB0aGF0IHdpbGwgYmUgYXJtb3JlZAphcyBwYXJ0 +IG9mIHRoZSBkcGtnIE9wZW5QR1AgdGVzdCBzdWl0ZS4KCkl0IGNvbnRhaW5zIMKr +VVRGLTjCuyBjaGFyYWN0ZXJzIHNvIHRoYXQKd2UgY2FuIGNoZWNrIOKGkiBub24t +QVNDSUkgQXJtb3Ig4pi6Lgo= +=NrdA +-----END PGP ARMORED FILE----- -- Dpkg.Org's dpkg

