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 <bvanassche@google.com>
This commit is contained in:
Bart Van Assche 2022-11-14 09:30:51 -08:00
parent a6844e4c62
commit 5d18891e60

View file

@ -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) {