diff options
author | David Tardon <dtardon@redhat.com> | 2022-06-27 08:37:22 +0200 |
---|---|---|
committer | David Tardon <dtardon@redhat.com> | 2022-07-13 13:53:13 +0200 |
commit | 9ac0855b614b068e699092be7ec8cb39559ff88f (patch) | |
tree | 856bb1fe4e425d5cb10a5fc98d908157a687d865 | |
parent | bd681a7eb04fd6d1fb9631e277bef93ceb06b321 (diff) | |
download | systemd-9ac0855b614b068e699092be7ec8cb39559ff88f.tar.gz |
test: add test for org.freedesktop.login1.Session SetType
-rw-r--r-- | src/login/meson.build | 3 | ||||
-rw-r--r-- | src/login/test-session-properties.c | 71 | ||||
-rwxr-xr-x | test/units/testsuite-35.sh | 16 |
3 files changed, 90 insertions, 0 deletions
diff --git a/src/login/meson.build b/src/login/meson.build index 8be238254a..1245c050d9 100644 --- a/src/login/meson.build +++ b/src/login/meson.build @@ -103,4 +103,7 @@ tests += [ [liblogind_core, libshared], [threads]], + + [files('test-session-properties.c'), + [], [], [], '', 'manual'], ] diff --git a/src/login/test-session-properties.c b/src/login/test-session-properties.c new file mode 100644 index 0000000000..8406147a2c --- /dev/null +++ b/src/login/test-session-properties.c @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +/* Usage: + * ./test-session-properties <SESSION-OBJECT-PATH> + * e.g., + * ./test-session-properties /org/freedesktop/login1/session/_32 + */ + +#include "alloc-util.h" +#include "bus-common-errors.h" +#include "bus-locator.h" +#include "string-util.h" +#include "tests.h" + +static BusLocator session; + +/* Tests org.freedesktop.logind.Session SetType */ +TEST(set_type) { + _cleanup_(sd_bus_flush_close_unrefp) sd_bus* bus = NULL; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + const char* types[] = {"tty", "x11", "wayland", "mir", "web"}; + _cleanup_free_ char *type = NULL, *type2 = NULL; + + assert_se(sd_bus_open_system(&bus) >= 0); + + /* Default type is set */ + assert_se(bus_get_property_string(bus, &session, "Type", NULL, &type) >= 0); + assert_se(streq(type, "tty")); + + /* Type can only be set by the session controller (which we're not ATM) */ + assert_se(bus_call_method(bus, &session, "SetType", &error, NULL, "s", "x11") < 0); + assert_se(sd_bus_error_has_name(&error, BUS_ERROR_NOT_IN_CONTROL)); + + assert_se(bus_call_method(bus, &session, "TakeControl", NULL, NULL, "b", true) >= 0); + + /* All defined session types can be set */ + for (size_t i = 0; i < ELEMENTSOF(types); i++) { + type = mfree(type); + assert_se(bus_call_method(bus, &session, "SetType", NULL, NULL, "s", types[i]) >= 0); + assert_se(bus_get_property_string(bus, &session, "Type", NULL, &type) >= 0); + assert_se(streq(type, types[i])); + } + + /* An unknown type is rejected */ + sd_bus_error_free(&error); + assert_se(bus_call_method(bus, &session, "SetType", &error, NULL, "s", "hello") < 0); + assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)); + assert_se(bus_get_property_string(bus, &session, "Type", NULL, &type2) >= 0); + + /* Type is reset to the original value when we release control of the session */ + assert_se(!streq(type, "tty")); + assert_se(bus_call_method(bus, &session, "ReleaseControl", NULL, NULL, "") >= 0); + type = mfree(type); + assert_se(bus_get_property_string(bus, &session, "Type", NULL, &type) >= 0); + assert_se(streq(type, "tty")); +} + +static int intro(void) { + if (saved_argc <= 1) + return EXIT_FAILURE; + + session = (BusLocator) { + .destination = "org.freedesktop.login1", + .path = saved_argv[1], + .interface = "org.freedesktop.login1.Session", + }; + + return EXIT_SUCCESS; +} + +DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); diff --git a/test/units/testsuite-35.sh b/test/units/testsuite-35.sh index a1b7ee2687..33b5fbf1ee 100755 --- a/test/units/testsuite-35.sh +++ b/test/units/testsuite-35.sh @@ -474,6 +474,21 @@ EOF assert_eq "$(systemctl --property SubState --value show user@"$(id -u logind-test-user)".service)" "dead" } +test_session_properties() { + local s + + if [[ ! -c /dev/tty2 ]]; then + echo "/dev/tty2 does not exist, skipping test ${FUNCNAME[0]}." + return + fi + + trap cleanup_session RETURN + create_session + + s=$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $1 }') + /usr/lib/systemd/tests/manual/test-session-properties "/org/freedesktop/login1/session/_3${s?}" +} + : >/failed setup_test_user @@ -485,6 +500,7 @@ test_shutdown test_session test_lock_idle_action test_no_user_instance_for_cron +test_session_properties touch /testok rm /failed |