git: 2c449a4c5a33 - main - Fix test of ses(4) when there is no SES device exists

From: Li-Wen Hsu <lwhsu_at_FreeBSD.org>
Date: Wed, 26 Jan 2022 23:12:04 UTC
The branch main has been updated by lwhsu:

URL: https://cgit.FreeBSD.org/src/commit/?id=2c449a4c5a338d2dd39a1949524e57c06ce7eff6

commit 2c449a4c5a338d2dd39a1949524e57c06ce7eff6
Author:     Li-Wen Hsu <lwhsu@FreeBSD.org>
AuthorDate: 2022-01-26 23:11:17 +0000
Commit:     Li-Wen Hsu <lwhsu@FreeBSD.org>
CommitDate: 2022-01-26 23:11:17 +0000

    Fix test of ses(4) when there is no SES device exists
    
    glob(3) returns GLOB_NOMATCH if GLOB_NOCHECK or GLOB_NOMAGIC flag is not
    passed so ATF_REQUIRE_EQ(r, 0) will cause a precondition check failure if no
    /dev/ses* exists.
    
    Remove calling of atf_tc_skip() in ATF_TC_CLEANUP() because it would let
    the clean up procedure unfinish.
    
    While here, fix a set-but-not-used warning.
    
    Reviewed by:    asomers
    Differential Revision:  https://reviews.freebsd.org/D34056
---
 tests/sys/ses/common.h         | 24 +++++++++++++++++-------
 tests/sys/ses/destructive.c    | 17 ++++++++++++-----
 tests/sys/ses/nondestructive.c | 18 ++++++++++++++++++
 3 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/tests/sys/ses/common.h b/tests/sys/ses/common.h
index c4a74bd8505f..0c6b139a6ab4 100644
--- a/tests/sys/ses/common.h
+++ b/tests/sys/ses/common.h
@@ -34,26 +34,36 @@ for_each_ses_dev(ses_cb cb, int oflags)
 	glob_t g;
 	int r;
 	unsigned i;
-	bool tested = false;
 
 	g.gl_pathc = 0;
 	g.gl_pathv = NULL;
 	g.gl_offs = 0;
 
-	r = glob("/dev/ses*", GLOB_NOSORT, NULL, &g);
+	r = glob("/dev/ses*", GLOB_NOCHECK | GLOB_NOSORT, NULL, &g);
 	ATF_REQUIRE_EQ(r, 0);
+	if (g.gl_matchc == 0)
+		return;
 
-	for(i = 0; i < g.gl_pathc; i++) {
+	for(i = 0; i < g.gl_matchc; i++) {
 		int fd;
 
 		fd = open(g.gl_pathv[i], oflags);
 		ATF_REQUIRE(fd >= 0);
-		tested |= cb(g.gl_pathv[i], fd);
+		cb(g.gl_pathv[i], fd);
 		close(fd);
 	}
 
-	if (!tested)
-		atf_tc_skip("No supported devices found");
-
 	globfree(&g);
 }
+
+static bool
+has_ses()
+{
+	glob_t g;
+	int r;
+
+	r = glob("/dev/ses*", GLOB_NOCHECK | GLOB_NOSORT, NULL, &g);
+	ATF_REQUIRE_EQ(r, 0);
+
+	return (g.gl_matchc != 0);
+}
diff --git a/tests/sys/ses/destructive.c b/tests/sys/ses/destructive.c
index 307b272c447f..9b83009280cf 100644
--- a/tests/sys/ses/destructive.c
+++ b/tests/sys/ses/destructive.c
@@ -53,10 +53,10 @@ for_one_ses_dev(ses_cb cb)
 	g.gl_pathv = NULL;
 	g.gl_offs = 0;
 
-	r = glob("/dev/ses*", GLOB_NOSORT, NULL, &g);
+	r = glob("/dev/ses*", GLOB_NOCHECK | GLOB_NOSORT, NULL, &g);
 	ATF_REQUIRE_EQ(r, 0);
-	if (g.gl_pathc == 0)
-		atf_tc_skip("No ses devices found");
+	if (g.gl_matchc == 0)
+		return;
 
 	fd = open(g.gl_pathv[0], O_RDWR);
 	ATF_REQUIRE(fd >= 0);
@@ -84,7 +84,6 @@ static bool do_setelmstat(const char *devname __unused, int fd) {
 	for (elm_idx = 0; elm_idx < nobj; elm_idx++) {
 		encioc_elm_status_t elmstat;
 		struct ses_ctrl_dev_slot *cslot;
-		struct ses_status_dev_slot *sslot;
 
 		if (last_elm_type != map[elm_idx].elm_type) {
 			/* skip overall elements */
@@ -99,7 +98,6 @@ static bool do_setelmstat(const char *devname __unused, int fd) {
 			ATF_REQUIRE_EQ(r, 0);
 
 			cslot = (struct ses_ctrl_dev_slot*)&elmstat.cstat[0];
-			sslot = (struct ses_status_dev_slot*)&elmstat.cstat[0];
 
 			ses_ctrl_common_set_select(&cslot->common, 1);
 			ses_ctrl_dev_slot_set_rqst_ident(cslot, 1);
@@ -203,10 +201,16 @@ ATF_TC_HEAD(setelmstat, tc)
 }
 ATF_TC_BODY(setelmstat, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
+
 	for_one_ses_dev(do_setelmstat);
 }
 ATF_TC_CLEANUP(setelmstat, tc)
 {
+	if (!has_ses())
+		return;
+
 	for_one_ses_dev(do_setelmstat_cleanup);
 }
 
@@ -262,6 +266,9 @@ ATF_TC_HEAD(setencstat, tc)
 }
 ATF_TC_BODY(setencstat, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
+
 	for_each_ses_dev(do_setencstat, O_RDWR);
 }
 ATF_TC_CLEANUP(setencstat, tc)
diff --git a/tests/sys/ses/nondestructive.c b/tests/sys/ses/nondestructive.c
index e52a38a97862..47c7e5067239 100644
--- a/tests/sys/ses/nondestructive.c
+++ b/tests/sys/ses/nondestructive.c
@@ -120,6 +120,8 @@ ATF_TC_HEAD(getelmdesc, tc)
 }
 ATF_TC_BODY(getelmdesc, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
 	for_each_ses_dev(do_getelmdesc, O_RDONLY);
 }
 
@@ -221,6 +223,8 @@ ATF_TC_HEAD(getelmdevnames, tc)
 }
 ATF_TC_BODY(getelmdevnames, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
 	for_each_ses_dev(do_getelmdevnames, O_RDONLY);
 }
 
@@ -311,6 +315,8 @@ ATF_TC_HEAD(getelmmap, tc)
 }
 ATF_TC_BODY(getelmmap, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
 	for_each_ses_dev(do_getelmmap, O_RDONLY);
 }
 
@@ -380,6 +386,8 @@ ATF_TC_HEAD(getelmstat, tc)
 }
 ATF_TC_BODY(getelmstat, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
 	for_each_ses_dev(do_getelmstat, O_RDONLY);
 }
 
@@ -428,6 +436,8 @@ ATF_TC_HEAD(getencid, tc)
 }
 ATF_TC_BODY(getencid, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
 	for_each_ses_dev(do_getencid, O_RDONLY);
 }
 
@@ -472,6 +482,8 @@ ATF_TC_HEAD(getencname, tc)
 }
 ATF_TC_BODY(getencname, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
 	for_each_ses_dev(do_getencname, O_RDONLY);
 }
 
@@ -513,6 +525,8 @@ ATF_TC_HEAD(getencstat, tc)
 }
 ATF_TC_BODY(getencstat, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
 	for_each_ses_dev(do_getencstat, O_RDONLY);
 }
 
@@ -559,6 +573,8 @@ ATF_TC_HEAD(getnelm, tc)
 }
 ATF_TC_BODY(getnelm, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
 	for_each_ses_dev(do_getnelm, O_RDONLY);
 }
 
@@ -609,6 +625,8 @@ ATF_TC_HEAD(getstring, tc)
 }
 ATF_TC_BODY(getstring, tc)
 {
+	if (!has_ses())
+		atf_tc_skip("No ses devices found");
 	atf_tc_expect_fail("Bug 258188 ENCIO_GETSTRING does not set the string's returned size");
 	for_each_ses_dev(do_getstring, O_RDWR);
 }