From 55d9ccca8eec8ec57ab85175dea908f8b76fd4e1 Mon Sep 17 00:00:00 2001
From: Richard Levitte <levitte@openssl.org>
Date: Wed, 17 Mar 2021 19:17:32 +0100
Subject: [PATCH] TEST: Clarify and adjust test/recipes/30-test_evp.t

There are a few test cases at the end of test/recipes/30-test_evp.t,
which are designed to check that loading DSA keys when DSA is disabled,
or SM2 keys when SM2 is disables fail in an understandable way.  These
needed a small adjustment.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14314)
---
 test/recipes/30-test_evp.t | 52 ++++++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 19 deletions(-)

diff --git a/test/recipes/30-test_evp.t b/test/recipes/30-test_evp.t
index fde0f658ee..6a1bf664a4 100644
--- a/test/recipes/30-test_evp.t
+++ b/test/recipes/30-test_evp.t
@@ -130,19 +130,29 @@ foreach my $f ( @defltfiles ) {
        "running evp_test -config $conf $f");
 }
 
+# test_errors OPTIONS
+#
+# OPTIONS may include:
+#
+# key      => "filename"        # expected to be found in $SRCDIR/test/certs
+# out      => "filename"        # file to write error strings to
+# args     => [ ... extra openssl pkey args ... ]
+# expected => regexps to match error lines against
 sub test_errors { # actually tests diagnostics of OSSL_STORE
-    my ($expected, $key, @opts) = @_;
-    my $infile = srctop_file('test', 'certs', $key);
-    my @args = qw(openssl pkey -in);
-    push(@args, $infile, @opts);
-    my $tmpfile = 'out.txt';
-    my $res = !run(app([@args], stderr => $tmpfile));
-    my $found = 0;
-    open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
-    while(<$in>) {
-        print; # this may help debugging
-        $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
-        $found = 1 if m/$expected/; # output must include $expected
+    my %opts = @_;
+    my $infile = srctop_file('test', 'certs', $opts{key});
+    my @args = ( qw(openssl pkey -in), $infile, @{$opts{args} // []} );
+    my $res = !run(app([@args], stderr => $opts{out}));
+    my $found = !exists $opts{expected};
+    open(my $in, '<', $opts{out}) or die "Could not open file $opts{out}";
+    while(my $errline = <$in>) {
+        print $errline; # this may help debugging
+
+        # output must not include ASN.1 parse errors
+        $res &&= $errline !~ m/asn1 encoding/;
+        # output must include what is expressed in $opts{$expected}
+        $found = 1
+            if exists $opts{expected} && $errline =~ m/$opts{expected}/;
     }
     close $in;
     # $tmpfile is kept to help with investigation in case of failure
@@ -152,15 +162,19 @@ sub test_errors { # actually tests diagnostics of OSSL_STORE
 SKIP: {
     skip "DSA not disabled", 2 if !disabled("dsa");
 
-    ok(test_errors("unsupported algorithm", "server-dsa-key.pem"),
-       "error loading unsupported dsa private key");
-    ok(test_errors("unsupported algorithm", "server-dsa-pubkey.pem", "-pubin"),
-       "error loading unsupported dsa public key");
+    ok(test_errors(key => 'server-dsa-key.pem',
+                   out => 'server-dsa-key.err'),
+       "expected error loading unsupported dsa private key");
+    ok(test_errors(key => 'server-dsa-pubkey.pem',
+                   out => 'server-dsa-pubkey.err',
+                   args => [ '-pubin' ],
+                   expected => 'unsupported algorithm'),
+       "expected error loading unsupported dsa public key");
 }
 
 SKIP: {
-    skip "sm2 not disabled", 1 if !disabled("sm2");
+    skip "SM2 not disabled", 1 if !disabled("sm2");
 
-    ok(test_errors("unknown group|unsupported algorithm", "sm2.key"),
-       "error loading unsupported sm2 private key");
+    ok(test_errors(key => 'sm2.key', out => 'sm2.err'),
+       "expected error loading unsupported sm2 private key");
 }