All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Vorel <pvorel@suse.cz>
To: ltp@lists.linux.it
Cc: Eric Biggers <ebiggers@kernel.org>
Subject: [LTP] [PATCH v3 1/2] tst_af_alg: Moving tst_res(TCONF) to tst_have_alg()
Date: Wed, 22 Dec 2021 20:26:03 +0100	[thread overview]
Message-ID: <20211222192604.16839-1-pvorel@suse.cz> (raw)

+ introduce tst_try_alg() for cases where tst_res(TCONF) cannot be used.

It reduces duplicity for tst_have_alg() use.

Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
New in v3.

 include/tst_af_alg.h               | 16 ++++++++++--
 lib/tst_af_alg.c                   | 41 ++++++++++++++++++++++--------
 testcases/kernel/crypto/af_alg01.c | 14 ++++------
 testcases/kernel/crypto/af_alg03.c |  3 ++-
 testcases/kernel/crypto/af_alg04.c |  6 ++---
 5 files changed, 53 insertions(+), 27 deletions(-)

diff --git a/include/tst_af_alg.h b/include/tst_af_alg.h
index fd2ff06478..93ff5715b7 100644
--- a/include/tst_af_alg.h
+++ b/include/tst_af_alg.h
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright 2019 Google LLC
+ * Copyright (c) Linux Test Project, 2021
  */
 /**
  * @file tst_af_alg.h
@@ -60,8 +61,19 @@ void tst_alg_bind(int algfd, const char *algtype, const char *algname);
  * @param algtype The type of algorithm, such as "hash" or "skcipher"
  * @param algname The name of the algorithm, such as "sha256" or "xts(aes)"
  *
- * Return true if the algorithm is available, or false if unavailable.
- * If another error occurs, tst_brk() is called with TBROK.
+ * Return 0 if the algorithm is available, or errno if unavailable.
+ */
+int tst_try_alg(const char *algtype, const char *algname);
+
+/**
+ * Check for the availability of an algorithm.
+ *
+ * @param algtype The type of algorithm, such as "hash" or "skcipher"
+ * @param algname The name of the algorithm, such as "sha256" or "xts(aes)"
+ *
+ * Return true if the algorithm is available, or false if unavailable
+ * and call tst_res() with TCONF. If another error occurs, tst_brk() is called
+ * with TBROK.
  */
 bool tst_have_alg(const char *algtype, const char *algname);
 
diff --git a/lib/tst_af_alg.c b/lib/tst_af_alg.c
index 05caa63016..d99a9ee2ef 100644
--- a/lib/tst_af_alg.c
+++ b/lib/tst_af_alg.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright 2019 Google LLC
+ * Copyright (c) Linux Test Project, 2019-2021
  */
 
 #include <errno.h>
@@ -64,29 +65,47 @@ void tst_alg_bind(int algfd, const char *algtype, const char *algname)
 	tst_alg_bind_addr(algfd, &addr);
 }
 
-bool tst_have_alg(const char *algtype, const char *algname)
+int tst_try_alg(const char *algtype, const char *algname)
 {
 	long ret;
+	int retval = 0;
 	int algfd;
 	struct sockaddr_alg addr;
-	bool have_alg = true;
 
 	algfd = tst_alg_create();
 
 	init_sockaddr_alg(&addr, algtype, algname);
 
 	ret = bind(algfd, (const struct sockaddr *)&addr, sizeof(addr));
-	if (ret != 0) {
-		if (errno != ENOENT) {
-			tst_brk(TBROK | TERRNO,
-				"unexpected error binding AF_ALG socket to %s algorithm '%s'",
-				algtype, algname);
-		}
-		have_alg = false;
-	}
+
+	if (ret != 0)
+		retval = errno;
 
 	close(algfd);
-	return have_alg;
+	return retval;
+}
+
+bool tst_have_alg(const char *algtype, const char *algname)
+{
+	int ret;
+
+	ret = tst_try_alg(algtype, algname);
+
+	switch (ret) {
+	case 0:
+		return true;
+	case ENOENT:
+		tst_res(TCONF, "kernel doesn't have %s algorithm '%s'",
+			algtype, algname);
+		return false;
+	default:
+		errno = ret;
+		tst_brk(TBROK | TERRNO,
+			"unexpected error binding AF_ALG socket to %s algorithm '%s'",
+			algtype, algname);
+		return false;
+	break;
+	}
 }
 
 void tst_require_alg(const char *algtype, const char *algname)
diff --git a/testcases/kernel/crypto/af_alg01.c b/testcases/kernel/crypto/af_alg01.c
index 47292ee328..7cefe59461 100644
--- a/testcases/kernel/crypto/af_alg01.c
+++ b/testcases/kernel/crypto/af_alg01.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright 2019 Google LLC
+ * Copyright (c) Linux Test Project, 2019-2021
  */
 
 /*
@@ -21,20 +22,15 @@ static void test_with_hash_alg(const char *hash_algname)
 	char hmac_algname[64];
 	char key[4096] = { 0 };
 
-	if (!tst_have_alg("hash", hash_algname)) {
-		tst_res(TCONF, "kernel doesn't have hash algorithm '%s'",
-			hash_algname);
+	if (!tst_have_alg("hash", hash_algname))
 		return;
-	}
+
 	sprintf(hmac_algname, "hmac(%s)", hash_algname);
-	if (!tst_have_alg("hash", hmac_algname)) {
-		tst_res(TCONF, "kernel doesn't have hash algorithm '%s'",
-			hmac_algname);
+	if (!tst_have_alg("hash", hmac_algname))
 		return;
-	}
 
 	sprintf(hmac_algname, "hmac(hmac(%s))", hash_algname);
-	if (tst_have_alg("hash", hmac_algname)) {
+	if (tst_try_alg("hash", hmac_algname) != ENOENT) {
 		int algfd;
 
 		tst_res(TFAIL, "instantiated nested hmac algorithm ('%s')!",
diff --git a/testcases/kernel/crypto/af_alg03.c b/testcases/kernel/crypto/af_alg03.c
index 5f214e48ba..bb8d480e28 100644
--- a/testcases/kernel/crypto/af_alg03.c
+++ b/testcases/kernel/crypto/af_alg03.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright 2019 Google LLC
+ * Copyright (c) Linux Test Project, 2019-2021
  */
 
 /*
@@ -17,7 +18,7 @@ static void run(void)
 	tst_require_alg("aead", "rfc7539(chacha20,poly1305)");
 	tst_require_alg("hash", "sha256");
 
-	if (tst_have_alg("aead", "rfc7539(chacha20,sha256)")) {
+	if (tst_try_alg("aead", "rfc7539(chacha20,sha256)") != ENOENT) {
 		tst_res(TFAIL,
 			"instantiated rfc7539 template with wrong digest size");
 	} else {
diff --git a/testcases/kernel/crypto/af_alg04.c b/testcases/kernel/crypto/af_alg04.c
index 112afcd527..7b665f89a3 100644
--- a/testcases/kernel/crypto/af_alg04.c
+++ b/testcases/kernel/crypto/af_alg04.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright 2019 Google LLC
+ * Copyright (c) Linux Test Project, 2019-2021
  */
 
 /*
@@ -28,11 +29,8 @@ static void test_with_symm_enc_algs(const char *symm_enc_algname)
 	sprintf(vmac_algname, "vmac64(%s)", symm_enc_algname);
 	if (!tst_have_alg("hash", vmac_algname)) {
 		sprintf(vmac_algname, "vmac(%s)", symm_enc_algname);
-		if (!tst_have_alg("hash", vmac_algname)) {
-			tst_res(TCONF, "kernel doesn't have hash algorithm '%s'",
-				vmac_algname);
+		if (!tst_have_alg("hash", vmac_algname))
 			return;
-		}
 	}
 	algfd = tst_alg_setup("hash", vmac_algname, NULL, 16);
 
-- 
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

             reply	other threads:[~2021-12-22 19:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-22 19:26 Petr Vorel [this message]
2021-12-22 19:26 ` [LTP] [PATCH v3 2/2] tst_af_alg: TCONF on ciphers disabled by FIPS Petr Vorel
2022-01-05 14:44   ` Cyril Hrubis
2022-01-05 15:04   ` Eric Biggers
2022-01-05 16:52     ` Petr Vorel
2022-01-05 14:41 ` [LTP] [PATCH v3 1/2] tst_af_alg: Moving tst_res(TCONF) to tst_have_alg() Cyril Hrubis
2022-01-05 15:03 ` Eric Biggers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211222192604.16839-1-pvorel@suse.cz \
    --to=pvorel@suse.cz \
    --cc=ebiggers@kernel.org \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.