From 5d18891e60c3190313e827fa07304e700498648c Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Mon, 14 Nov 2022 09:30:51 -0800 Subject: [PATCH] init: Add a unit test for the "start console" action Treehugger starts Cuttlefish with the console disabled. Add a test that enables the console. The purpose of this test is to trigger the code paths in Service::Start() that are unique to processes associated with a console. Bug: 213617178 Change-Id: I834632ce6ec5c237c9c2c3f5b1aa7bc98c3ef260 Signed-off-by: Bart Van Assche --- init/init_test.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/init/init_test.cpp b/init/init_test.cpp index 5c1e9efb0..0ca791ebc 100644 --- a/init/init_test.cpp +++ b/init/init_test.cpp @@ -193,6 +193,32 @@ service A something EXPECT_TRUE(service->is_override()); } +TEST(init, StartConsole) { + std::string init_script = R"init( +service console /system/bin/sh + class core + console console + disabled + user root + group root shell log readproc + seclabel u:r:su:s0 + setenv HOSTNAME console +)init"; + + ActionManager action_manager; + ServiceList service_list; + TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list); + ASSERT_EQ(std::distance(service_list.begin(), service_list.end()), 1); + + auto service = service_list.begin()->get(); + ASSERT_NE(service, nullptr); + ASSERT_RESULT_OK(service->Start()); + const pid_t pid = service->pid(); + ASSERT_GT(pid, 0); + EXPECT_EQ(getsid(pid), pid); + service->Stop(); +} + static std::string GetSecurityContext() { char* ctx; if (getcon(&ctx) == -1) {