commit: ef80524385258d796a09700e181ea60292a29b96
parent bd6c19ef913c397e39e091e201c0cef65f0e20bf
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 12 Mar 2022 03:58:24 +0100
test-bin/id: Add test case for missing /etc/group
Diffstat:
2 files changed, 34 insertions(+), 5 deletions(-)
diff --git a/bin/id.c b/bin/id.c
@@ -87,7 +87,7 @@ list_groups(struct passwd *pw)
 
 	// getgrouplist(3) BSD extension might not be in all Unixes
 	int grls = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
-	if(grls >= 0)
+	if(grls > 0)
 	{
 		printf(" groups=");
 
@@ -338,7 +338,7 @@ main(int argc, char *argv[])
 		ret = print_uid(" euid", epw, euid);
 		if(ret < 0)
 		{
-			return 1;
+			goto failure;
 		}
 	}
 
@@ -347,7 +347,7 @@ main(int argc, char *argv[])
 	ret = print_gid(" gid", gr, gid);
 	if(ret < 0)
 	{
-		return 1;
+		goto failure;
 	}
 
 	struct group *egr = getgrgid(egid);
@@ -357,7 +357,7 @@ main(int argc, char *argv[])
 
 		if(ret < 0)
 		{
-			return 1;
+			goto failure;
 		}
 	}
 
@@ -365,7 +365,7 @@ main(int argc, char *argv[])
 	{
 		if(list_groups(pw) != 0)
 		{
-			return 1;
+			goto failure;
 		}
 	}
 
@@ -376,4 +376,8 @@ main(int argc, char *argv[])
 	}
 
 	return 0;
+
+failure:
+	printf("\n");
+	return 1;
 }
diff --git a/test-bin/id b/test-bin/id
@@ -79,6 +79,30 @@ noetc_body() {
 	atf_check -o "inline:$(id -gr)\n" -- bwrap ${bwrap_args} ../bin/id -gr
 }
 
+atf_test_case nogroup
+nogroup_body() {
+	bwrap_args="--bind / / --bind /dev/null /etc/group"
+
+	command -v bwrap >/dev/null 2>/dev/null || atf_skip "'bwrap' command not found"
+
+	set -f
+
+	# shellcheck disable=SC2086
+	atf_check -o "inline:uid=$(id -u)($(id -un)) gid=$(id -g) groups=$(id -g)\n" -- bwrap ${bwrap_args} ../bin/id
+	# shellcheck disable=SC2086
+	atf_check -o "inline:uid=$(id -ur)($(id -unr)) gid=$(id -gr) groups=$(id -gr)\n" -- bwrap ${bwrap_args} ../bin/id
+
+	# shellcheck disable=SC2086
+	atf_check -s exit:1 -o "inline:uid=$(id -un)\n" -- bwrap ${bwrap_args} ../bin/id -n
+	# shellcheck disable=SC2086
+	atf_check -s exit:1 -o "inline:uid=$(id -unr)\n" -- bwrap ${bwrap_args} ../bin/id -nr
+
+	# shellcheck disable=SC2086
+	atf_check -o "inline:$(id -g)\n" -- bwrap ${bwrap_args} ../bin/id -g
+	# shellcheck disable=SC2086
+	atf_check -o "inline:$(id -gr)\n" -- bwrap ${bwrap_args} ../bin/id -gr
+}
+
 atf_init_test_cases() {
 	cd "$(atf_get_srcdir)" || exit 1
 
@@ -93,4 +117,5 @@ atf_init_test_cases() {
 	atf_add_test_case groups
 
 	atf_add_test_case noetc
+	atf_add_test_case nogroup
 }