Commit b2bd6a885a for openssl.org
commit b2bd6a885aef5be6c0e7ce548015a6cfb0d769ab
Author: Jakub Zelenka <jakub.zelenka@openssl.foundation>
Date: Sat Aug 29 14:01:25 2026 +0200
apps: cover the genpkey error cases and verbose mode in the test recipe
Cover the bad option failures (missing algorithm, extra arguments,
invalid output format, unknown algorithm, key option, cipher and
password arguments) as well as the failures when loading a
nonexistent or invalid parameters file, verifying the error messages
printed to stderr. Also cover the -verbose progress output and the
-quiet option.
Assisted-by: Claude:claude-fable-5
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.foundation>
Merge-date: Mon Sep 7 14:15:52 2026
Merged-from: https://github.com/openssl/openssl/pull/32592
diff --git a/test/recipes/15-test_genpkey.t b/test/recipes/15-test_genpkey.t
index ddef803ff1..564ca65365 100644
--- a/test/recipes/15-test_genpkey.t
+++ b/test/recipes/15-test_genpkey.t
@@ -9,7 +9,7 @@
use strict;
use warnings;
-use OpenSSL::Test qw/:DEFAULT with/;
+use OpenSSL::Test qw/:DEFAULT with app_fails slurp_file/;
use OpenSSL::Test::Utils;
setup("test_genpkey");
@@ -22,7 +22,7 @@ push @algs, qw(EC) unless disabled("ec");
push @algs, qw(X25519 X448) unless disabled("ecx");
push @algs, qw(SM2) unless disabled("sm2");
-plan tests => scalar(@algs) + 2;
+plan tests => scalar(@algs) + 4;
foreach (@algs) {
my $alg = $_;
@@ -67,3 +67,67 @@ SKIP: {
"Cannot use a cipher with -genparam");
});
}
+
+subtest "genpkey error cases" => sub {
+ plan tests => 20;
+
+ app_fails('genpkey', "no algorithm or parameter file should fail",
+ qr/Use -help for summary/);
+ app_fails('genpkey', "extra positional argument should fail",
+ qr/Extra option: "extra"/,
+ '-algorithm', 'RSA', 'extra');
+ app_fails('genpkey', "invalid output format should fail",
+ qr/Invalid format "BAD" for option -outform/,
+ '-outform', 'BAD', '-algorithm', 'RSA');
+ app_fails('genpkey', "parameter file with -genparam should fail",
+ qr/Use -help for summary/,
+ '-genparam', '-paramfile', 'dsagen.pem');
+ app_fails('genpkey', "unknown algorithm should fail",
+ qr/Error initializing FOO context/,
+ '-algorithm', 'FOO');
+ app_fails('genpkey', "unknown key option should fail",
+ qr/Error setting bad:1 parameter/,
+ '-algorithm', 'RSA', '-pkeyopt', 'bad:1');
+ app_fails('genpkey', "unknown cipher option should fail",
+ qr/Unknown option or cipher: badcipher/,
+ '-algorithm', 'RSA', '-badcipher');
+ app_fails('genpkey', "invalid pass argument should fail",
+ qr/Error getting password/,
+ '-algorithm', 'RSA', '-pass', 'bad:secret');
+ app_fails('genpkey', "nonexistent parameter file should fail",
+ qr/Can't open parameter file/,
+ '-paramfile', 'nonexistent.pem');
+
+ my $garbage = "garbage.pem";
+ open(my $fh, '>', $garbage) or die "Cannot write $garbage: $!";
+ print $fh "not a valid params file\n";
+ close($fh);
+ app_fails('genpkey', "garbage parameter file should fail",
+ qr/Error reading parameter file/,
+ '-paramfile', $garbage);
+};
+
+subtest "genpkey verbose mode" => sub {
+ plan tests => 4;
+
+ my $stderr_file = "genpkey_verbose.txt";
+
+ ok(run(app(['openssl', 'genpkey', '-verbose', '-algorithm', 'RSA',
+ '-pkeyopt', 'rsa_keygen_bits:2048',
+ '-out', 'genpkey-verbose.pem'],
+ stderr => $stderr_file)),
+ "genpkey -verbose generates a key");
+ my $err = slurp_file($stderr_file);
+ ok($err =~ qr/Generating RSA key/,
+ "-verbose reports the key generation");
+
+ ok(run(app(['openssl', 'genpkey', '-quiet', '-algorithm', 'RSA',
+ '-pkeyopt', 'rsa_keygen_bits:2048',
+ '-out', 'genpkey-quiet.pem'],
+ stderr => $stderr_file)),
+ "genpkey -quiet generates a key");
+ $err = slurp_file($stderr_file);
+ ok($err !~ qr/Generating RSA key/,
+ "-quiet does not report the key generation");
+ unlink($stderr_file) if -f $stderr_file;
+};