Loading init/subcontext.cpp +6 −2 Original line number Diff line number Diff line Loading @@ -209,9 +209,13 @@ void Subcontext::Fork() { PLOG(FATAL) << "Could not dup child_fd"; } // 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(); auto child_fd_string = std::to_string(child_fd); Loading init/subcontext.h +1 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,7 @@ namespace init { static constexpr const char kInitContext[] = "u:r:init:s0"; static constexpr const char kVendorContext[] = "u:r:vendor_init:s0"; static constexpr const char kTestContext[] = "test-test-test"; class Subcontext { public: Loading init/subcontext_test.cpp +16 −23 Original line number Diff line number Diff line Loading @@ -39,24 +39,12 @@ using android::base::WaitForProperty; namespace android { namespace init { // I would use test fixtures, but I cannot skip the test if not root with them, so instead we have // this test runner. template <typename F> void RunTest(F&& test_function) { if (getuid() != 0) { GTEST_SKIP() << "Skipping test, must be run as root."; return; } char* context; ASSERT_EQ(0, getcon(&context)); auto context_string = std::string(context); free(context); auto subcontext = Subcontext({"dummy_path"}, context_string); auto subcontext = Subcontext({"dummy_path"}, kTestContext); ASSERT_NE(0, subcontext.pid()); test_function(subcontext, context_string); test_function(subcontext); if (subcontext.pid() > 0) { kill(subcontext.pid(), SIGTERM); Loading @@ -65,7 +53,7 @@ void RunTest(F&& test_function) { } TEST(subcontext, CheckDifferentPid) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"}); ASSERT_FALSE(result); Loading @@ -78,7 +66,12 @@ TEST(subcontext, CheckDifferentPid) { } TEST(subcontext, SetProp) { RunTest([](auto& subcontext, auto& context_string) { if (getuid() != 0) { GTEST_SKIP() << "Skipping test, must be run as root."; return; } RunTest([](auto& subcontext) { SetProperty("init.test.subcontext", "fail"); WaitForProperty("init.test.subcontext", "fail"); Loading @@ -95,7 +88,7 @@ TEST(subcontext, SetProp) { } TEST(subcontext, MultipleCommands) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto first_pid = subcontext.pid(); auto expected_words = std::vector<std::string>{ Loading @@ -122,7 +115,7 @@ TEST(subcontext, MultipleCommands) { } TEST(subcontext, RecoverAfterAbort) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto first_pid = subcontext.pid(); auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"}); Loading @@ -136,10 +129,10 @@ TEST(subcontext, RecoverAfterAbort) { } TEST(subcontext, ContextString) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"}); ASSERT_FALSE(result); ASSERT_EQ(context_string, result.error().message()); ASSERT_EQ(kTestContext, result.error().message()); }); } Loading @@ -147,7 +140,7 @@ TEST(subcontext, TriggerShutdown) { static constexpr const char kTestShutdownCommand[] = "reboot,test-shutdown-command"; static std::string trigger_shutdown_command; trigger_shutdown = [](const std::string& command) { trigger_shutdown_command = command; }; RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto result = subcontext.Execute( std::vector<std::string>{"trigger_shutdown", kTestShutdownCommand}); ASSERT_TRUE(result); Loading @@ -156,7 +149,7 @@ TEST(subcontext, TriggerShutdown) { } TEST(subcontext, ExpandArgs) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto args = std::vector<std::string>{ "first", "${ro.hardware}", Loading @@ -172,7 +165,7 @@ TEST(subcontext, ExpandArgs) { } TEST(subcontext, ExpandArgsFailure) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto args = std::vector<std::string>{ "first", "${", Loading Loading
init/subcontext.cpp +6 −2 Original line number Diff line number Diff line Loading @@ -209,9 +209,13 @@ void Subcontext::Fork() { PLOG(FATAL) << "Could not dup child_fd"; } // 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(); auto child_fd_string = std::to_string(child_fd); Loading
init/subcontext.h +1 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,7 @@ namespace init { static constexpr const char kInitContext[] = "u:r:init:s0"; static constexpr const char kVendorContext[] = "u:r:vendor_init:s0"; static constexpr const char kTestContext[] = "test-test-test"; class Subcontext { public: Loading
init/subcontext_test.cpp +16 −23 Original line number Diff line number Diff line Loading @@ -39,24 +39,12 @@ using android::base::WaitForProperty; namespace android { namespace init { // I would use test fixtures, but I cannot skip the test if not root with them, so instead we have // this test runner. template <typename F> void RunTest(F&& test_function) { if (getuid() != 0) { GTEST_SKIP() << "Skipping test, must be run as root."; return; } char* context; ASSERT_EQ(0, getcon(&context)); auto context_string = std::string(context); free(context); auto subcontext = Subcontext({"dummy_path"}, context_string); auto subcontext = Subcontext({"dummy_path"}, kTestContext); ASSERT_NE(0, subcontext.pid()); test_function(subcontext, context_string); test_function(subcontext); if (subcontext.pid() > 0) { kill(subcontext.pid(), SIGTERM); Loading @@ -65,7 +53,7 @@ void RunTest(F&& test_function) { } TEST(subcontext, CheckDifferentPid) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"}); ASSERT_FALSE(result); Loading @@ -78,7 +66,12 @@ TEST(subcontext, CheckDifferentPid) { } TEST(subcontext, SetProp) { RunTest([](auto& subcontext, auto& context_string) { if (getuid() != 0) { GTEST_SKIP() << "Skipping test, must be run as root."; return; } RunTest([](auto& subcontext) { SetProperty("init.test.subcontext", "fail"); WaitForProperty("init.test.subcontext", "fail"); Loading @@ -95,7 +88,7 @@ TEST(subcontext, SetProp) { } TEST(subcontext, MultipleCommands) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto first_pid = subcontext.pid(); auto expected_words = std::vector<std::string>{ Loading @@ -122,7 +115,7 @@ TEST(subcontext, MultipleCommands) { } TEST(subcontext, RecoverAfterAbort) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto first_pid = subcontext.pid(); auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"}); Loading @@ -136,10 +129,10 @@ TEST(subcontext, RecoverAfterAbort) { } TEST(subcontext, ContextString) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"}); ASSERT_FALSE(result); ASSERT_EQ(context_string, result.error().message()); ASSERT_EQ(kTestContext, result.error().message()); }); } Loading @@ -147,7 +140,7 @@ TEST(subcontext, TriggerShutdown) { static constexpr const char kTestShutdownCommand[] = "reboot,test-shutdown-command"; static std::string trigger_shutdown_command; trigger_shutdown = [](const std::string& command) { trigger_shutdown_command = command; }; RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto result = subcontext.Execute( std::vector<std::string>{"trigger_shutdown", kTestShutdownCommand}); ASSERT_TRUE(result); Loading @@ -156,7 +149,7 @@ TEST(subcontext, TriggerShutdown) { } TEST(subcontext, ExpandArgs) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto args = std::vector<std::string>{ "first", "${ro.hardware}", Loading @@ -172,7 +165,7 @@ TEST(subcontext, ExpandArgs) { } TEST(subcontext, ExpandArgsFailure) { RunTest([](auto& subcontext, auto& context_string) { RunTest([](auto& subcontext) { auto args = std::vector<std::string>{ "first", "${", Loading