diff --git a/src/login/test-session-properties.c b/src/login/test-session-properties.c index 0bfde42213..48aa21abef 100644 --- a/src/login/test-session-properties.c +++ b/src/login/test-session-properties.c @@ -1,9 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ /* Usage: - * ./test-session-properties + * ./test-session-properties [] * e.g., - * ./test-session-properties /org/freedesktop/login1/session/_32 + * ./test-session-properties /org/freedesktop/login1/session/_32 /dev/tty2 */ #include @@ -18,6 +18,8 @@ #include "terminal-util.h" #include "tests.h" +static const char *arg_tty = NULL; + static BusLocator session; /* Tests org.freedesktop.logind.Session SetType */ @@ -105,10 +107,12 @@ TEST(set_tty) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus* bus = NULL; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_free_ char *tty = NULL; - const char *path = "/dev/tty2"; /* testsuite uses tty2 */ int fd; - fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY); + if (!arg_tty) + return; + + fd = open(arg_tty, O_RDWR|O_CLOEXEC|O_NOCTTY); assert_se(fd >= 0); assert_se(sd_bus_open_system(&bus) >= 0); @@ -126,6 +130,50 @@ TEST(set_tty) { assert_se(streq(tty, "tty2")); } +/* Tests org.freedesktop.logind.Session SetIdleHint */ +TEST(set_idle_hint) { + _cleanup_(sd_bus_flush_close_unrefp) sd_bus* bus = NULL; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + int idle_hint; + time_t stamp, idle_since1, idle_since2; + + assert_se(sd_bus_open_system(&bus) >= 0); + + /* Idle hint is not set by default */ + assert_se(bus_get_property_trivial(bus, &session, "IdleHint", NULL, 'b', &idle_hint) >= 0); + assert_se(!idle_hint); + + assert_se(bus_call_method(bus, &session, "TakeControl", NULL, NULL, "b", true) >= 0); + + /* Idle hint can only be set on a graphical session */ + assert_se(bus_call_method(bus, &session, "SetType", NULL, NULL, "s", "tty") >= 0); + assert_se(bus_call_method(bus, &session, "SetIdleHint", &error, NULL, "b", true) < 0); + assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_NOT_SUPPORTED)); + + assert_se(bus_call_method(bus, &session, "SetType", NULL, NULL, "s", "x11") >= 0); + + stamp = now(CLOCK_MONOTONIC); + + /* Idle hint can be set */ + assert_se(bus_call_method(bus, &session, "SetIdleHint", NULL, NULL, "b", true) >= 0); + assert_se(bus_get_property_trivial(bus, &session, "IdleHint", NULL, 'b', &idle_hint) >= 0); + assert_se(idle_hint); + assert_se(bus_get_property_trivial(bus, &session, "IdleSinceHintMonotonic", NULL, 't', &idle_since1) >= 0); + assert_se(idle_since1 >= stamp); + + /* Repeated setting doesn't change anything */ + assert_se(bus_call_method(bus, &session, "SetIdleHint", NULL, NULL, "b", true) >= 0); + assert_se(bus_get_property_trivial(bus, &session, "IdleHint", NULL, 'b', &idle_hint) >= 0); + assert_se(idle_hint); + assert_se(bus_get_property_trivial(bus, &session, "IdleSinceHintMonotonic", NULL, 't', &idle_since2) >= 0); + assert_se(idle_since2 == idle_since1); + + /* Idle hint can be unset */ + assert_se(bus_call_method(bus, &session, "SetIdleHint", NULL, NULL, "b", false) >= 0); + assert_se(bus_get_property_trivial(bus, &session, "IdleHint", NULL, 'b', &idle_hint) >= 0); + assert_se(!idle_hint); +} + static int intro(void) { if (saved_argc <= 1) return EXIT_FAILURE; @@ -136,6 +184,9 @@ static int intro(void) { .interface = "org.freedesktop.login1.Session", }; + if (saved_argc > 2) + arg_tty = saved_argv[2]; + return EXIT_SUCCESS; } diff --git a/test/units/testsuite-35.sh b/test/units/testsuite-35.sh index 78228ab0b2..e9d88e1c83 100755 --- a/test/units/testsuite-35.sh +++ b/test/units/testsuite-35.sh @@ -519,7 +519,7 @@ testcase_session_properties() { create_session s=$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $1 }') - /usr/lib/systemd/tests/unit-tests/manual/test-session-properties "/org/freedesktop/login1/session/_3${s?}" + /usr/lib/systemd/tests/unit-tests/manual/test-session-properties "/org/freedesktop/login1/session/_3${s?}" /dev/tty2 } testcase_list_users_sessions() {