git: dda15b669d02 - main - tests/sys/capsicum: restore test coverage with root user

From: Enji Cooper <ngie_at_FreeBSD.org>
Date: Tue, 10 Nov 2026 01:33:00 UTC
The branch main has been updated by ngie:

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

commit dda15b669d021346682009f38acc6db2639fb99f
Author:     Enji Cooper <ngie@FreeBSD.org>
AuthorDate: 2026-11-10 01:32:45 +0000
Commit:     Enji Cooper <ngie@FreeBSD.org>
CommitDate: 2026-11-10 01:32:45 +0000

    tests/sys/capsicum: restore test coverage with root user
    
    This change builds capsicum_test and installs/runs it as an unprivileged
    user ("tests" in vanilla FreeBSD) and root.
    
    This required adjusting logic in the Makefile in order to build two
    binaries with the same sources and running them as separate binaries.
    
    Moreover, in order to restore coverage and ensure that some tests which
    require `other_uid` (typically specified via `-u` on the CLI with the
    test program) don't fail, become the "tests" user when run as root. This
    technically doesn't honor unprivileged_user property in kyua.conf: the
    property needs to be passed via a command-line parameter to the test
    program.
    
    MFC after:      1 week
    Reported by:    markj
    Fixes:          3fb03fb35b20 ("[tests] switch GoogleTest tests ...")
    
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D61066
---
 tests/sys/capsicum/Makefile              | 27 ++++++++++++++++++---------
 tests/sys/capsicum/capsicum-test-main.cc | 24 ++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/tests/sys/capsicum/Makefile b/tests/sys/capsicum/Makefile
index 9a88c9cf0ec1..daf508ba7f30 100644
--- a/tests/sys/capsicum/Makefile
+++ b/tests/sys/capsicum/Makefile
@@ -9,14 +9,11 @@ CFLAGS+=	-I${SRCTOP}/tests
 
 .if ${MK_GOOGLETEST} != no
 
-GTESTS+=	capsicum-test
-# This test script runs the same test suite twice, once as root and once as an
-# unprivileged user.  Serialize them since some tests access global namespaces,
-# e.g., mqueuefs, and can trample on each other.
-TEST_METADATA.functional+=	is_exclusive="true"
-TEST_METADATA.functional+=	required_kmods="mqueuefs"
+GTESTS+=	capsicum-test_privileged
+GTESTS+=	capsicum-test_unprivileged
 
-SRCS.capsicum-test+=	\
+# SRCS required for `capsicum-test*`.
+SRCS.capsicum-test=	\
 	capsicum-test-main.cc \
 	capsicum-test.cc \
 	capability-fd.cc \
@@ -36,8 +33,20 @@ SRCS.capsicum-test+=	\
 	overhead.cc \
 	rename.cc
 
-LIBADD.capsicum-test+=	gtest pthread procstat
-TEST_METADATA.capsicum-test=	required_user="unprivileged"
+.for gtest in ${GTESTS}
+SRCS.${gtest}=			${SRCS.capsicum-test}
+
+LIBADD.${gtest}+=		gtest pthread procstat
+
+# This test script runs the same test suite twice, once as root and once as an
+# unprivileged user.  Serialize them since some tests access global namespaces,
+# e.g., mqueuefs, and can trample on each other.
+TEST_METADATA.${gtest}+=	is_exclusive="true"
+TEST_METADATA.${gtest}+=	required_kmods="mqueuefs"
+.endfor
+
+TEST_METADATA.capsicum-test_privileged+=	required_user="root"
+TEST_METADATA.capsicum-test_unprivileged+=	required_user="unprivileged"
 
 .for p in mini-me mini-me.noexec mini-me.setuid
 PROGS+=		$p
diff --git a/tests/sys/capsicum/capsicum-test-main.cc b/tests/sys/capsicum/capsicum-test-main.cc
index 2a2c75000039..308c121a0281 100644
--- a/tests/sys/capsicum/capsicum-test-main.cc
+++ b/tests/sys/capsicum/capsicum-test-main.cc
@@ -136,6 +136,30 @@ int main(int argc, char* argv[]) {
     }
   }
 
+  // Last ditch effort to get an unprivileged user on the fly from /etc/passwd
+  // for testcases that need it.
+  //
+  // This must be done this way because the typical way of handling this (via
+  // the `-u` argument) isn't supported yet with kyua:
+  // https://github.com/freebsd/kyua/issues/354 .
+  //
+  // IMPORTANT:
+  // - this doesn't honor `unprivileged_user` in `kyua.conf`.
+  // - this doesn't fail because doing that could break all testing on systems
+  //   where the `tests` user isn't available.
+  if (other_uid == 0) {
+    const char *test_user = "tests";
+
+    struct passwd *test_user_entry = getpwnam(test_user);
+
+    if (test_user_entry) {
+       other_uid = test_user_entry->pw_uid;
+    } else {
+       std::cerr << "Failed to get entry for " << test_user << ", errno=" << errno << "; some tests may fail" << std::endl;
+    }
+
+  }
+
   testing::AddGlobalTestEnvironment(new SetupEnvironment());
   return RUN_ALL_TESTS();
 }