summaryrefslogtreecommitdiffstats
path: root/src/login/test-session-properties.c
blob: b5b5f60eaaac29b82fe1c3a9b323f365f27544bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* SPDX-License-Identifier: LGPL-2.1-or-later */

/* Usage:
 * ./test-session-properties <SESSION-OBJECT-PATH> [<TTY>]
 * e.g.,
 * ./test-session-properties /org/freedesktop/login1/session/_32 /dev/tty2
 */

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include "alloc-util.h"
#include "bus-common-errors.h"
#include "bus-locator.h"
#include "path-util.h"
#include "string-util.h"
#include "terminal-util.h"
#include "tests.h"

static const char *arg_tty = NULL;

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, NULL) >= 0);
        type = mfree(type);
        assert_se(bus_get_property_string(bus, &session, "Type", NULL, &type) >= 0);
        assert_se(streq(type, "tty"));
}

/* Tests org.freedesktop.logind.Session SetDisplay */
TEST(set_display) {
        _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 *display = NULL;

        assert_se(sd_bus_open_system(&bus) >= 0);

        /* Display is unset by default */
        assert_se(bus_get_property_string(bus, &session, "Display", NULL, &display) >= 0);
        assert_se(isempty(display));

        /* Display can only be set by the session controller (which we're not ATM) */
        assert_se(bus_call_method(bus, &session, "SetDisplay", &error, NULL, "s", ":0") < 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);

        /* Display can only be set on a graphical session */
        assert_se(bus_call_method(bus, &session, "SetType", NULL, NULL, "s", "tty") >= 0);
        sd_bus_error_free(&error);
        assert_se(bus_call_method(bus, &session, "SetDisplay", &error, NULL, "s", ":0") < 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);

        /* Non-empty display can be set */
        assert_se(bus_call_method(bus, &session, "SetDisplay", NULL, NULL, "s", ":0") >= 0);
        display = mfree(display);
        assert_se(bus_get_property_string(bus, &session, "Display", NULL, &display) >= 0);
        assert_se(streq(display, ":0"));

        /* Empty display can be set too */
        assert_se(bus_call_method(bus, &session, "SetDisplay", NULL, NULL, "s", "") >= 0);
        display = mfree(display);
        assert_se(bus_get_property_string(bus, &session, "Display", NULL, &display) >= 0);
        assert_se(isempty(display));
}

/* Tests org.freedesktop.logind.Session SetTTY */
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;
        int fd;

        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);

        /* tty can only be set by the session controller (which we're not ATM) */
        assert_se(bus_call_method(bus, &session, "SetTTY", &error, NULL, "h", fd) < 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);

        /* tty can be set */
        assert_se(bus_call_method(bus, &session, "SetTTY", NULL, NULL, "h", fd) >= 0);
        tty = mfree(tty);
        assert_se(bus_get_property_string(bus, &session, "TTY", NULL, &tty) >= 0);
        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;

        session = (BusLocator) {
                .destination = "org.freedesktop.login1",
                .path = saved_argv[1],
                .interface = "org.freedesktop.login1.Session",
        };

        if (saved_argc > 2)
                arg_tty = saved_argv[2];

        return EXIT_SUCCESS;
}

DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);