init: fix subcontext tests running as non-root.
A recently added subcontext test was failing beause it was running as
non-root, but GTEST_SKIP() didn't work as I expected it to.
In retrospect, all of these tests except for the property one, can
easily run as root, so this changes allows all of these tests to run
as root, while fixing the original issue.
Bug: 144707143
Test: root and nonroot subcontext unit tests
Change-Id: Ia835597701698f6be2101f92d6f4c9450bd3c7dd
diff --git a/init/subcontext.cpp b/init/subcontext.cpp
index e55265b..bebcc77 100644
--- a/init/subcontext.cpp
+++ b/init/subcontext.cpp
@@ -209,8 +209,12 @@
PLOG(FATAL) << "Could not dup child_fd";
}
- if (setexeccon(context_.c_str()) < 0) {
- PLOG(FATAL) << "Could not set execcon for '" << context_ << "'";
+ // We don't switch contexts if we're running the unit tests. We don't use std::optional,
+ // since we still need a real context string to pass to the builtin functions.
+ if (context_ != kTestContext) {
+ if (setexeccon(context_.c_str()) < 0) {
+ PLOG(FATAL) << "Could not set execcon for '" << context_ << "'";
+ }
}
auto init_path = GetExecutablePath();