Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 : : *
3 : : * Copyright © 2019 Endless Mobile, Inc.
4 : : * Copyright 2025 GNOME Foundation, Inc.
5 : : *
6 : : * SPDX-License-Identifier: LGPL-2.1-or-later
7 : : *
8 : : * This library is free software; you can redistribute it and/or
9 : : * modify it under the terms of the GNU Lesser General Public
10 : : * License as published by the Free Software Foundation; either
11 : : * version 2.1 of the License, or (at your option) any later version.
12 : : *
13 : : * This library is distributed in the hope that it will be useful,
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : : * Lesser General Public License for more details.
17 : : *
18 : : * You should have received a copy of the GNU Lesser General Public
19 : : * License along with this library; if not, write to the Free Software
20 : : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 : : *
22 : : * Authors:
23 : : * - Philip Withnall <withnall@endlessm.com>
24 : : */
25 : :
26 : : #include "config.h"
27 : :
28 : : #include <glib.h>
29 : : #include <gio/gio.h>
30 : : #include <libmalcontent/session-limits.h>
31 : : #include <libmalcontent/manager.h>
32 : : #include <libglib-testing/dbus-queue.h>
33 : : #include <locale.h>
34 : : #include <string.h>
35 : : #include "accounts-service-iface.h"
36 : : #include "accounts-service-extension-iface.h"
37 : :
38 : :
39 : : /* Test that the #GType definitions for various types work. */
40 : : static void
41 : 2 : test_session_limits_types (void)
42 : : {
43 : 2 : g_type_ensure (mct_session_limits_get_type ());
44 : 2 : g_type_ensure (mct_session_limits_builder_get_type ());
45 : 2 : }
46 : :
47 : : /* Test that ref() and unref() work on an #MctSessionLimits. */
48 : : static void
49 : 2 : test_session_limits_refs (void)
50 : : {
51 : 2 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
52 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
53 : 4 : g_autoptr(GDateTime) dummy_dt = g_date_time_new_from_unix_utc (0);
54 : :
55 : : /* Use an empty #MctSessionLimits. */
56 : 2 : limits = mct_session_limits_builder_end (&builder);
57 : :
58 : 2 : g_assert_nonnull (limits);
59 : :
60 : : /* Call check_time_remaining() to check that the limits object hasn’t been
61 : : * finalised. */
62 : 2 : g_assert_true (mct_session_limits_check_time_remaining (limits, dummy_dt, 0, NULL, NULL));
63 : 2 : mct_session_limits_ref (limits);
64 : 2 : g_assert_true (mct_session_limits_check_time_remaining (limits, dummy_dt, 0, NULL, NULL));
65 : 2 : mct_session_limits_unref (limits);
66 : 2 : g_assert_true (mct_session_limits_check_time_remaining (limits, dummy_dt, 0, NULL, NULL));
67 : :
68 : : /* Final ref is dropped by g_autoptr(). */
69 : 2 : }
70 : :
71 : : /* Check error handling when passing an invalid time for @now_secs to
72 : : * mct_session_limits_check_time_remaining(). */
73 : : static void
74 : 2 : test_session_limits_check_time_remaining_invalid_time (void)
75 : : {
76 : 2 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
77 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
78 : : guint64 time_remaining_secs;
79 : : gboolean time_limit_enabled;
80 : :
81 : : /* Use an empty #MctSessionLimits. */
82 : 2 : limits = mct_session_limits_builder_end (&builder);
83 : :
84 : : /* Pass an invalid time to mct_session_limits_check_time_remaining(). */
85 : 4 : g_autoptr(GDateTime) invalid_dt = g_date_time_new_from_unix_utc (G_MAXUINT64);
86 : 2 : g_assert_false (mct_session_limits_check_time_remaining (limits, invalid_dt, 0, &time_remaining_secs, &time_limit_enabled));
87 : 2 : g_assert_cmpuint (time_remaining_secs, ==, 0);
88 : 2 : g_assert_true (time_limit_enabled);
89 : 2 : }
90 : :
91 : : /* Check calculation of time remaining via
92 : : * mct_session_limits_check_time_remaining() with a daily schedule limit. */
93 : : static void
94 : 2 : test_session_limits_check_time_remaining_daily_schedule (void)
95 : : {
96 : 2 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
97 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
98 : :
99 : 2 : mct_session_limits_builder_set_daily_schedule (&builder, 100, 1000);
100 : 2 : limits = mct_session_limits_builder_end (&builder);
101 : :
102 : : /* Try different calls to mct_session_limits_check_time_remaining(). */
103 : : const struct
104 : : {
105 : : const char *now_iso8601;
106 : : gboolean expected_has_time_remaining;
107 : : gboolean expected_time_remaining_secs;
108 : : }
109 : 2 : vectors[] =
110 : : {
111 : : { "2025-07-12T00:00:00Z", FALSE, 0 },
112 : : { "2025-07-12T00:01:39Z", FALSE, 0 },
113 : : { "2025-07-12T00:01:40Z", TRUE, 900 },
114 : : { "2025-07-12T00:16:39Z", TRUE, 1 },
115 : : { "2025-07-12T00:16:40Z", FALSE, 0 },
116 : : { "2025-07-12T00:17:00Z", FALSE, 0 },
117 : : };
118 : :
119 [ + + ]: 14 : for (size_t i = 0; i < G_N_ELEMENTS (vectors); i++)
120 : : {
121 : : uint64_t time_remaining_secs;
122 : : gboolean has_time_remaining, time_limit_enabled;
123 : 24 : g_autoptr(GDateTime) now_dt = g_date_time_new_from_iso8601 (vectors[i].now_iso8601, NULL);
124 : :
125 : 12 : g_test_message ("Vector %zu: %s", i, vectors[i].now_iso8601);
126 : :
127 : 12 : has_time_remaining = mct_session_limits_check_time_remaining (limits, now_dt, 0, &time_remaining_secs, &time_limit_enabled);
128 : 12 : g_assert_true (has_time_remaining == vectors[i].expected_has_time_remaining);
129 : 12 : g_assert_cmpuint (time_remaining_secs, ==, vectors[i].expected_time_remaining_secs);
130 : 12 : g_assert_true (time_limit_enabled);
131 : : }
132 : 2 : }
133 : :
134 : : /* Check calculation of time remaining via
135 : : * mct_session_limits_check_time_remaining() with a daily schedule limit. */
136 : : static void
137 : 3 : test_session_limits_check_time_remaining_daily_schedule_timezone (void)
138 : : {
139 : : /* Change the timezone to one with a non-zero UTC offset (any time of year).
140 : : * This needs to be done in a subprocess so we’re not changing environment
141 : : * variables at runtime. */
142 [ + + ]: 3 : if (!g_test_subprocess ())
143 : : {
144 : 4 : g_auto(GStrv) envp = g_get_environ ();
145 : 2 : envp = g_environ_setenv (g_steal_pointer (&envp), "TZ", "Europe/Warsaw", TRUE);
146 : 2 : g_test_trap_subprocess_with_envp (NULL, (const char * const *) envp, 0, G_TEST_SUBPROCESS_DEFAULT);
147 : 2 : g_test_trap_assert_passed ();
148 : : }
149 : : else
150 : : {
151 : 1 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
152 : 1 : g_autoptr(MctSessionLimits) limits = NULL;
153 : : guint64 time_remaining_secs;
154 : : gboolean time_limit_enabled;
155 : 2 : g_autoptr(GTimeZone) local_tz = g_time_zone_new_local ();
156 : 1 : g_autoptr(GDateTime) now_dt = NULL;
157 : :
158 : : /* Check the timezone loaded properly. */
159 : 1 : g_assert_cmpstr (g_time_zone_get_identifier (local_tz), ==, "Europe/Warsaw");
160 : :
161 : 1 : mct_session_limits_builder_set_daily_schedule (&builder, 8 * 60 * 60, 20 * 60 * 60);
162 : 1 : limits = mct_session_limits_builder_end (&builder);
163 : :
164 : 1 : now_dt = g_date_time_new_from_iso8601 ("2025-08-21T16:00:00", local_tz);
165 : 1 : g_assert_true (g_date_time_get_timezone (now_dt) == local_tz);
166 : 1 : g_assert_true (mct_session_limits_check_time_remaining (limits, now_dt, 0, &time_remaining_secs, &time_limit_enabled));
167 : 1 : g_assert_cmpuint (time_remaining_secs, ==, 4 * 60 * 60);
168 : 1 : g_assert_true (time_limit_enabled);
169 : : }
170 : 3 : }
171 : :
172 : : /* Check calculation of time remaining via
173 : : * mct_session_limits_check_time_remaining() with a daily limit. */
174 : : static void
175 : 2 : test_session_limits_check_time_remaining_daily_limit (void)
176 : : {
177 : 2 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
178 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
179 : :
180 : 2 : mct_session_limits_builder_set_daily_limit (&builder, 100);
181 : 2 : limits = mct_session_limits_builder_end (&builder);
182 : :
183 : : /* Try different calls to mct_session_limits_check_time_remaining(). */
184 : : const struct
185 : : {
186 : : const char *now_iso8601;
187 : : uint64_t active_session_time_today_secs;
188 : : gboolean expected_has_time_remaining;
189 : : gboolean expected_time_remaining_secs;
190 : : }
191 : 2 : vectors[] =
192 : : {
193 : : { "2025-07-12T00:00:00Z", 0, TRUE, 100 },
194 : : { "2025-07-12T01:00:00Z", 0, TRUE, 100 },
195 : : { "2025-07-12T01:00:50Z", 50, TRUE, 50 },
196 : : { "2025-07-12T01:01:39Z", 99, TRUE, 1 },
197 : : { "2025-07-12T01:01:40Z", 100, FALSE, 0 },
198 : : { "2025-07-12T02:00:00Z", 100, FALSE, 0 },
199 : : };
200 : :
201 [ + + ]: 14 : for (size_t i = 0; i < G_N_ELEMENTS (vectors); i++)
202 : : {
203 : : uint64_t time_remaining_secs;
204 : : gboolean has_time_remaining, time_limit_enabled;
205 : 24 : g_autoptr(GDateTime) now_dt = g_date_time_new_from_iso8601 (vectors[i].now_iso8601, NULL);
206 : :
207 : 12 : g_test_message ("Vector %zu: %s", i, vectors[i].now_iso8601);
208 : :
209 : 12 : has_time_remaining = mct_session_limits_check_time_remaining (limits, now_dt, vectors[i].active_session_time_today_secs, &time_remaining_secs, &time_limit_enabled);
210 : 12 : g_assert_true (has_time_remaining == vectors[i].expected_has_time_remaining);
211 : 12 : g_assert_cmpuint (time_remaining_secs, ==, vectors[i].expected_time_remaining_secs);
212 : 12 : g_assert_true (time_limit_enabled);
213 : : }
214 : 2 : }
215 : :
216 : : /* Check calculation of time remaining via
217 : : * mct_session_limits_check_time_remaining() with multiple types of limit. */
218 : : static void
219 : 2 : test_session_limits_check_time_remaining_multiple (void)
220 : : {
221 : 2 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
222 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
223 : :
224 : : /* Session limits here are: 4h of computer use between 08:00 and 18:00. */
225 : 2 : mct_session_limits_builder_set_daily_schedule (&builder, 8 * 60 * 60, 18 * 60 * 60);
226 : 2 : mct_session_limits_builder_set_daily_limit (&builder, 4 * 60 * 60);
227 : 2 : limits = mct_session_limits_builder_end (&builder);
228 : :
229 : : /* Try different calls to mct_session_limits_check_time_remaining(). */
230 : : const struct
231 : : {
232 : : const char *now_iso8601;
233 : : uint64_t active_session_time_today_secs;
234 : : gboolean expected_has_time_remaining;
235 : : gboolean expected_time_remaining_secs;
236 : : }
237 : 2 : vectors[] =
238 : : {
239 : : { "2025-07-12T00:00:00Z", 0, FALSE, 0 },
240 : : { "2025-07-12T08:00:00Z", 0, TRUE, 4 * 60 * 60 },
241 : : { "2025-07-12T12:00:00Z", 2 * 60 * 60, TRUE, 2 * 60 * 60 },
242 : : { "2025-07-12T17:00:00Z", 2 * 60 * 60, TRUE, 1 * 60 * 60 },
243 : : { "2025-07-12T18:00:00Z", 3 * 60 * 60, FALSE, 0 },
244 : : };
245 : :
246 [ + + ]: 12 : for (size_t i = 0; i < G_N_ELEMENTS (vectors); i++)
247 : : {
248 : : uint64_t time_remaining_secs;
249 : : gboolean has_time_remaining, time_limit_enabled;
250 : 20 : g_autoptr(GDateTime) now_dt = g_date_time_new_from_iso8601 (vectors[i].now_iso8601, NULL);
251 : :
252 : 10 : g_test_message ("Vector %zu: %s", i, vectors[i].now_iso8601);
253 : :
254 : 10 : has_time_remaining = mct_session_limits_check_time_remaining (limits, now_dt, vectors[i].active_session_time_today_secs, &time_remaining_secs, &time_limit_enabled);
255 : 10 : g_assert_true (has_time_remaining == vectors[i].expected_has_time_remaining);
256 : 10 : g_assert_cmpuint (time_remaining_secs, ==, vectors[i].expected_time_remaining_secs);
257 : 10 : g_assert_true (time_limit_enabled);
258 : : }
259 : 2 : }
260 : :
261 : : /* Basic test of mct_session_limits_serialize() on session limits. */
262 : : static void
263 : 2 : test_session_limits_serialize (void)
264 : : {
265 : 2 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
266 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
267 : 2 : g_autoptr(GVariant) serialized = NULL;
268 : :
269 : : /* Use an empty #MctSessionLimits. */
270 : 2 : limits = mct_session_limits_builder_end (&builder);
271 : :
272 : : /* We can’t assert anything about the serialisation format, since it’s opaque. */
273 : 2 : serialized = mct_session_limits_serialize (limits);
274 : 2 : g_assert_nonnull (serialized);
275 : 2 : }
276 : :
277 : : /* Basic test of mct_session_limits_deserialize() on various current and historic
278 : : * serialised app filter variants. */
279 : : static void
280 : 2 : test_session_limits_deserialize (void)
281 : : {
282 : : /* These are all opaque. Older versions should be kept around to test
283 : : * backwards compatibility. */
284 : 2 : const gchar *valid_session_limits[] =
285 : : {
286 : : "@a{sv} {}",
287 : : "{ 'LimitType': <@u 0> }",
288 : : "{ 'LimitType': <@u 1>, 'DailySchedule': <(@u 0, @u 100)> }",
289 : : "{ 'DailySchedule': <(@u 0, @u 100)> }",
290 : : "{ 'LimitType': <@u 2>, 'DailyLimit': <@u 100> }",
291 : : "{ 'DailyLimit': <@u 100> }",
292 : : };
293 : :
294 [ + + ]: 14 : for (gsize i = 0; i < G_N_ELEMENTS (valid_session_limits); i++)
295 : : {
296 : 12 : g_autoptr(GVariant) serialized = NULL;
297 : 12 : g_autoptr(MctSessionLimits) limits = NULL;
298 : 12 : g_autoptr(GError) local_error = NULL;
299 : :
300 : 12 : g_test_message ("%" G_GSIZE_FORMAT ": %s", i, valid_session_limits[i]);
301 : :
302 : 12 : serialized = g_variant_parse (NULL, valid_session_limits[i], NULL, NULL, NULL);
303 : 12 : g_assert (serialized != NULL);
304 : :
305 : 12 : limits = mct_session_limits_deserialize (serialized, 1, &local_error);
306 : 12 : g_assert_no_error (local_error);
307 : 12 : g_assert_nonnull (limits);
308 : : }
309 : 2 : }
310 : :
311 : : /* Test of mct_session_limits_deserialize() on various invalid variants. */
312 : : static void
313 : 2 : test_session_limits_deserialize_invalid (void)
314 : : {
315 : 2 : const gchar *invalid_session_limits[] =
316 : : {
317 : : "false",
318 : : "()",
319 : : "{ 'LimitType': <@u 100> }",
320 : : "{ 'DailySchedule': <(@u 100, @u 0)> }",
321 : : "{ 'DailySchedule': <(@u 0, @u 4294967295)> }",
322 : : "{ 'DailyLimit': <@u 4294967295> }",
323 : : };
324 : :
325 [ + + ]: 14 : for (gsize i = 0; i < G_N_ELEMENTS (invalid_session_limits); i++)
326 : : {
327 : 12 : g_autoptr(GVariant) serialized = NULL;
328 : 12 : g_autoptr(MctSessionLimits) limits = NULL;
329 : 12 : g_autoptr(GError) local_error = NULL;
330 : :
331 : 12 : g_test_message ("%" G_GSIZE_FORMAT ": %s", i, invalid_session_limits[i]);
332 : :
333 : 12 : serialized = g_variant_parse (NULL, invalid_session_limits[i], NULL, NULL, NULL);
334 : 12 : g_assert (serialized != NULL);
335 : :
336 : 12 : limits = mct_session_limits_deserialize (serialized, 1, &local_error);
337 : 12 : g_assert_error (local_error, MCT_MANAGER_ERROR, MCT_MANAGER_ERROR_INVALID_DATA);
338 : 12 : g_assert_null (limits);
339 : : }
340 : 2 : }
341 : :
342 : : /* Test that mct_session_limits_equal() returns the correct results on various
343 : : * session limits. */
344 : : static void
345 : 2 : test_session_limits_equal (void)
346 : : {
347 : 2 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
348 : : MctSessionLimits *equal_filters[2];
349 : 2 : const char *unequal_filters_serialised[] =
350 : : {
351 : : "@a{sv} {}",
352 : : "{ 'LimitType': <@u 1>, 'DailySchedule': <(@u 0, @u 100)> }",
353 : : "{ 'LimitType': <@u 1>, 'DailySchedule': <(@u 0, @u 101)> }",
354 : : "{ 'DailySchedule': <(@u 0, @u 100)> }",
355 : : "{ 'DailySchedule': <(@u 0, @u 101)> }",
356 : : "{ 'LimitType': <@u 2>, 'DailyLimit': <@u 100> }",
357 : : "{ 'DailyLimit': <@u 100> }",
358 : : };
359 : : MctSessionLimits *unequal_filters[G_N_ELEMENTS (unequal_filters_serialised)];
360 : :
361 : : /* Build a couple of filters which are identical. */
362 : 2 : equal_filters[0] = mct_session_limits_builder_end (&builder);
363 : :
364 : 2 : mct_session_limits_builder_init (&builder);
365 : 2 : equal_filters[1] = mct_session_limits_builder_end (&builder);
366 : :
367 : : /* And a load of filters which are not. */
368 [ + + ]: 16 : for (size_t i = 0; i < G_N_ELEMENTS (unequal_filters_serialised); i++)
369 : : {
370 : 14 : g_autoptr(GVariant) serialized = NULL;
371 : :
372 : 14 : serialized = g_variant_parse (NULL, unequal_filters_serialised[i], NULL, NULL, NULL);
373 : 14 : g_assert (serialized != NULL);
374 : :
375 : 14 : unequal_filters[i] = mct_session_limits_deserialize (serialized, 1, NULL);
376 : 14 : g_assert (unequal_filters[i] != NULL);
377 : : }
378 : :
379 : : /* Test the equality checks on them all. */
380 [ + + ]: 6 : for (size_t i = 0; i < G_N_ELEMENTS (equal_filters); i++)
381 [ + + ]: 12 : for (size_t j = 0; j < G_N_ELEMENTS (equal_filters); j++)
382 : 8 : g_assert_true (mct_session_limits_equal (equal_filters[i], equal_filters[j]));
383 : :
384 [ + + ]: 16 : for (size_t i = 0; i < G_N_ELEMENTS (unequal_filters); i++)
385 : : {
386 [ + + ]: 42 : for (size_t j = 0; j < G_N_ELEMENTS (equal_filters); j++)
387 : 28 : g_assert_false (mct_session_limits_equal (unequal_filters[i], equal_filters[j]));
388 [ + + ]: 112 : for (size_t j = 0; j < G_N_ELEMENTS (unequal_filters); j++)
389 : : {
390 [ + + ]: 98 : if (i != j)
391 : 84 : g_assert_false (mct_session_limits_equal (unequal_filters[i], unequal_filters[j]));
392 : : else
393 : 14 : g_assert_true (mct_session_limits_equal (unequal_filters[i], unequal_filters[j]));
394 : : }
395 : : }
396 : :
397 [ + + ]: 6 : for (size_t i = 0; i < G_N_ELEMENTS (equal_filters); i++)
398 : 4 : mct_session_limits_unref (equal_filters[i]);
399 [ + + ]: 16 : for (size_t i = 0; i < G_N_ELEMENTS (unequal_filters); i++)
400 : 14 : mct_session_limits_unref (unequal_filters[i]);
401 : 2 : }
402 : :
403 : : /* Fixture for tests which use an #MctSessionLimitsBuilder. The builder can
404 : : * either be heap- or stack-allocated. @builder will always be a valid pointer
405 : : * to it.
406 : : */
407 : : typedef struct
408 : : {
409 : : MctSessionLimitsBuilder *builder;
410 : : MctSessionLimitsBuilder stack_builder;
411 : : } BuilderFixture;
412 : :
413 : : static void
414 : 4 : builder_set_up_stack (BuilderFixture *fixture,
415 : : gconstpointer test_data)
416 : : {
417 : 4 : mct_session_limits_builder_init (&fixture->stack_builder);
418 : 4 : fixture->builder = &fixture->stack_builder;
419 : 4 : }
420 : :
421 : : static void
422 : 4 : builder_tear_down_stack (BuilderFixture *fixture,
423 : : gconstpointer test_data)
424 : : {
425 : 4 : mct_session_limits_builder_clear (&fixture->stack_builder);
426 : 4 : fixture->builder = NULL;
427 : 4 : }
428 : :
429 : : static void
430 : 4 : builder_set_up_stack2 (BuilderFixture *fixture,
431 : : gconstpointer test_data)
432 : : {
433 : 4 : MctSessionLimitsBuilder local_builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
434 : 4 : memcpy (&fixture->stack_builder, &local_builder, sizeof (local_builder));
435 : 4 : fixture->builder = &fixture->stack_builder;
436 : 4 : }
437 : :
438 : : static void
439 : 4 : builder_tear_down_stack2 (BuilderFixture *fixture,
440 : : gconstpointer test_data)
441 : : {
442 : 4 : mct_session_limits_builder_clear (&fixture->stack_builder);
443 : 4 : fixture->builder = NULL;
444 : 4 : }
445 : :
446 : : static void
447 : 4 : builder_set_up_heap (BuilderFixture *fixture,
448 : : gconstpointer test_data)
449 : : {
450 : 4 : fixture->builder = mct_session_limits_builder_new ();
451 : 4 : }
452 : :
453 : : static void
454 : 4 : builder_tear_down_heap (BuilderFixture *fixture,
455 : : gconstpointer test_data)
456 : : {
457 [ + - ]: 4 : g_clear_pointer (&fixture->builder, mct_session_limits_builder_free);
458 : 4 : }
459 : :
460 : : static void
461 : 56 : assert_session_limits_has_time_remaining (MctSessionLimits *limits,
462 : : uint64_t now_secs_local)
463 : : {
464 : : /* Force the user’s timezone to be UTC so we can simply work in seconds since
465 : : * midnight on the Unix epoch; this isn’t how the API is meant to be used, but
466 : : * we’re testing MctSessionLimitsBuilder here */
467 : 112 : g_autoptr(GDateTime) now_dt_local = g_date_time_new_from_unix_utc (now_secs_local);
468 : 56 : g_assert_true (mct_session_limits_check_time_remaining (limits, now_dt_local, 0, NULL, NULL));
469 : 56 : }
470 : :
471 : : static void
472 : 34 : assert_session_limits_has_no_time_remaining (MctSessionLimits *limits,
473 : : uint64_t now_secs_local)
474 : : {
475 : : /* Force the user’s timezone to be UTC so we can simply work in seconds since
476 : : * midnight on the Unix epoch; this isn’t how the API is meant to be used, but
477 : : * we’re testing MctSessionLimitsBuilder here */
478 : 68 : g_autoptr(GDateTime) now_dt_local = g_date_time_new_from_unix_utc (now_secs_local);
479 : 34 : g_assert_false (mct_session_limits_check_time_remaining (limits, now_dt_local, 0, NULL, NULL));
480 : 34 : }
481 : :
482 : : /* Test building a non-empty #MctSessionLimits using an
483 : : * #MctSessionLimitsBuilder. */
484 : : static void
485 : 6 : test_session_limits_builder_non_empty (BuilderFixture *fixture,
486 : : gconstpointer test_data)
487 : : {
488 : 6 : g_autoptr(MctSessionLimits) limits = NULL;
489 : 6 : g_autofree const gchar **sections = NULL;
490 : : unsigned int start, end;
491 : :
492 : 6 : mct_session_limits_builder_set_daily_schedule (fixture->builder, 100, 8 * 60 * 60);
493 : :
494 : 6 : limits = mct_session_limits_builder_end (fixture->builder);
495 : :
496 : 6 : assert_session_limits_has_no_time_remaining (limits, 0);
497 : 6 : assert_session_limits_has_no_time_remaining (limits, 99);
498 : 6 : assert_session_limits_has_time_remaining (limits, 100);
499 : 6 : assert_session_limits_has_time_remaining (limits, 8 * 60 * 60 - 1);
500 : 6 : assert_session_limits_has_no_time_remaining (limits, 8 * 60 * 60);
501 : :
502 : 6 : g_assert_true (mct_session_limits_get_daily_schedule (limits, &start, &end));
503 : 6 : g_assert_cmpuint (start, ==, 100);
504 : 6 : g_assert_cmpuint (end, ==, 8 * 60 * 60);
505 : 6 : }
506 : :
507 : : /* Test building an empty #MctSessionLimits using an #MctSessionLimitsBuilder. */
508 : : static void
509 : 6 : test_session_limits_builder_empty (BuilderFixture *fixture,
510 : : gconstpointer test_data)
511 : : {
512 : 6 : g_autoptr(MctSessionLimits) limits = NULL;
513 : 6 : g_autofree const gchar **sections = NULL;
514 : :
515 : 6 : limits = mct_session_limits_builder_end (fixture->builder);
516 : :
517 : 6 : assert_session_limits_has_time_remaining (limits, 0);
518 : 6 : assert_session_limits_has_time_remaining (limits, 99);
519 : 6 : assert_session_limits_has_time_remaining (limits, 100);
520 : 6 : assert_session_limits_has_time_remaining (limits, 8 * 60 * 60 - 1);
521 : 6 : assert_session_limits_has_time_remaining (limits, 8 * 60 * 60);
522 : 6 : }
523 : :
524 : : /* Check that copying a cleared #MctSessionLimitsBuilder works, and the copy can
525 : : * then be initialised and used to build a limits object. */
526 : : static void
527 : 2 : test_session_limits_builder_copy_empty (void)
528 : : {
529 : 4 : g_autoptr(MctSessionLimitsBuilder) builder = mct_session_limits_builder_new ();
530 : 2 : g_autoptr(MctSessionLimitsBuilder) builder_copy = NULL;
531 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
532 : : unsigned int start, end;
533 : :
534 : 2 : mct_session_limits_builder_clear (builder);
535 : 2 : builder_copy = mct_session_limits_builder_copy (builder);
536 : :
537 : 2 : mct_session_limits_builder_init (builder_copy);
538 : 2 : mct_session_limits_builder_set_daily_schedule (builder_copy, 100, 8 * 60 * 60);
539 : 2 : limits = mct_session_limits_builder_end (builder_copy);
540 : :
541 : 2 : assert_session_limits_has_no_time_remaining (limits, 0);
542 : 2 : assert_session_limits_has_no_time_remaining (limits, 99);
543 : 2 : assert_session_limits_has_time_remaining (limits, 100);
544 : 2 : assert_session_limits_has_time_remaining (limits, 8 * 60 * 60 - 1);
545 : 2 : assert_session_limits_has_no_time_remaining (limits, 8 * 60 * 60);
546 : :
547 : 2 : g_assert_true (mct_session_limits_get_daily_schedule (limits, &start, &end));
548 : 2 : g_assert_cmpuint (start, ==, 100);
549 : 2 : g_assert_cmpuint (end, ==, 8 * 60 * 60);
550 : 2 : }
551 : :
552 : : /* Check that copying a filled #MctSessionLimitsBuilder works, and the copy can
553 : : * be used to build a limits object. */
554 : : static void
555 : 2 : test_session_limits_builder_copy_full (void)
556 : : {
557 : 4 : g_autoptr(MctSessionLimitsBuilder) builder = mct_session_limits_builder_new ();
558 : 2 : g_autoptr(MctSessionLimitsBuilder) builder_copy = NULL;
559 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
560 : : unsigned int start, end;
561 : :
562 : 2 : mct_session_limits_builder_set_daily_schedule (builder, 100, 8 * 60 * 60);
563 : 2 : builder_copy = mct_session_limits_builder_copy (builder);
564 : 2 : limits = mct_session_limits_builder_end (builder_copy);
565 : :
566 : 2 : assert_session_limits_has_no_time_remaining (limits, 0);
567 : 2 : assert_session_limits_has_no_time_remaining (limits, 99);
568 : 2 : assert_session_limits_has_time_remaining (limits, 100);
569 : 2 : assert_session_limits_has_time_remaining (limits, 8 * 60 * 60 - 1);
570 : 2 : assert_session_limits_has_no_time_remaining (limits, 8 * 60 * 60);
571 : :
572 : 2 : g_assert_true (mct_session_limits_get_daily_schedule (limits, &start, &end));
573 : 2 : g_assert_cmpuint (start, ==, 100);
574 : 2 : g_assert_cmpuint (end, ==, 8 * 60 * 60);
575 : 2 : }
576 : :
577 : : /* Check that overriding an already-set limit in a #MctSessionLimitsBuilder
578 : : * removes all trace of it. In this test, override with a ‘none’ limit. */
579 : : static void
580 : 2 : test_session_limits_builder_override_none (void)
581 : : {
582 : 4 : g_autoptr(MctSessionLimitsBuilder) builder = mct_session_limits_builder_new ();
583 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
584 : : unsigned int start, end;
585 : :
586 : : /* Set up some schedule. */
587 : 2 : mct_session_limits_builder_set_daily_schedule (builder, 100, 8 * 60 * 60);
588 : :
589 : : /* Override it. */
590 : 2 : mct_session_limits_builder_set_none (builder);
591 : 2 : limits = mct_session_limits_builder_end (builder);
592 : :
593 : 2 : assert_session_limits_has_time_remaining (limits, 0);
594 : :
595 : 2 : g_assert_false (mct_session_limits_get_daily_schedule (limits, &start, &end));
596 : 2 : g_assert_cmpuint (start, ==, 0);
597 : 2 : g_assert_cmpuint (end, ==, 0);
598 : 2 : }
599 : :
600 : : /* Check that overriding an already-set limit in a #MctSessionLimitsBuilder
601 : : * removes all trace of it. In this test, override with a ‘daily schedule’
602 : : * limit. */
603 : : static void
604 : 2 : test_session_limits_builder_override_daily_schedule (void)
605 : : {
606 : 4 : g_autoptr(MctSessionLimitsBuilder) builder = mct_session_limits_builder_new ();
607 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
608 : : unsigned int start, end;
609 : :
610 : : /* Set up some schedule. */
611 : 2 : mct_session_limits_builder_set_daily_schedule (builder, 100, 8 * 60 * 60);
612 : :
613 : : /* Override it. */
614 : 2 : mct_session_limits_builder_set_daily_schedule (builder, 200, 7 * 60 * 60);
615 : 2 : limits = mct_session_limits_builder_end (builder);
616 : :
617 : 2 : assert_session_limits_has_no_time_remaining (limits, 150);
618 : 2 : assert_session_limits_has_time_remaining (limits, 4 * 60 * 60);
619 : 2 : assert_session_limits_has_no_time_remaining (limits, 7 * 60 * 60 + 30 * 60);
620 : :
621 : 2 : g_assert_true (mct_session_limits_get_daily_schedule (limits, &start, &end));
622 : 2 : g_assert_cmpuint (start, ==, 200);
623 : 2 : g_assert_cmpuint (end, ==, 7 * 60 * 60);
624 : 2 : }
625 : :
626 : : /* Check that overriding an already-set limit in a #MctSessionLimitsBuilder
627 : : * removes all trace of it. In this test, override with a ‘daily limit’
628 : : * limit. */
629 : : static void
630 : 2 : test_session_limits_builder_override_daily_limit (void)
631 : : {
632 : 4 : g_autoptr(MctSessionLimitsBuilder) builder = mct_session_limits_builder_new ();
633 : 2 : g_autoptr(MctSessionLimits) limits = NULL;
634 : : unsigned int limit;
635 : :
636 : : /* Set up some schedule. */
637 : 2 : mct_session_limits_builder_set_daily_schedule (builder, 100, 8 * 60 * 60);
638 : :
639 : : /* Override it. */
640 : 2 : mct_session_limits_builder_set_daily_limit (builder, 1000);
641 : 2 : limits = mct_session_limits_builder_end (builder);
642 : :
643 : 2 : assert_session_limits_has_time_remaining (limits, 150);
644 : :
645 : : /* actually test with UTC as in assert_session_limits_has_time_remaining(): */
646 : 4 : g_autoptr(GDateTime) now_dt_local1 = g_date_time_new_from_unix_utc (150);
647 : 2 : g_assert_true (mct_session_limits_check_time_remaining (limits, now_dt_local1, 150, NULL, NULL));
648 : 4 : g_autoptr(GDateTime) now_dt_local2 = g_date_time_new_from_unix_utc (1001);
649 : 2 : g_assert_false (mct_session_limits_check_time_remaining (limits, now_dt_local2, 1001, NULL, NULL));
650 : :
651 : 2 : g_assert_true (mct_session_limits_get_daily_limit (limits, &limit));
652 : 2 : g_assert_cmpuint (limit, ==, 1000);
653 : 2 : }
654 : :
655 : : /* Fixture for tests which interact with the accountsservice over D-Bus. The
656 : : * D-Bus service is mocked up using @queue, which allows us to reply to D-Bus
657 : : * calls from the code under test from within the test process.
658 : : *
659 : : * It exports one user object (for UID 500) and the manager object. The method
660 : : * return values from UID 500 are up to the test in question, so it could be an
661 : : * administrator, or non-administrator, have a restrictive or permissive app
662 : : * limits, etc.
663 : : */
664 : : typedef struct
665 : : {
666 : : GtDBusQueue *queue; /* (owned) */
667 : : uid_t valid_uid;
668 : : uid_t missing_uid;
669 : : MctManager *manager; /* (owned) */
670 : : } BusFixture;
671 : :
672 : : static void
673 : 17 : bus_set_up (BusFixture *fixture,
674 : : gconstpointer test_data)
675 : : {
676 : 16 : g_autoptr(GError) local_error = NULL;
677 : 33 : g_autofree gchar *object_path = NULL;
678 : :
679 : 17 : fixture->valid_uid = 500; /* arbitrarily chosen */
680 : 17 : fixture->missing_uid = 501; /* must be different from valid_uid and not exported */
681 : 17 : fixture->queue = gt_dbus_queue_new ();
682 : :
683 : 17 : gt_dbus_queue_connect (fixture->queue, &local_error);
684 : 16 : g_assert_no_error (local_error);
685 : :
686 : 16 : gt_dbus_queue_own_name (fixture->queue, "org.freedesktop.Accounts");
687 : :
688 : 16 : object_path = g_strdup_printf ("/org/freedesktop/Accounts/User%u", fixture->valid_uid);
689 : 16 : gt_dbus_queue_export_object (fixture->queue,
690 : : object_path,
691 : : (GDBusInterfaceInfo *) &com_endlessm_parental_controls_session_limits_interface,
692 : : &local_error);
693 : 16 : g_assert_no_error (local_error);
694 : :
695 : 16 : gt_dbus_queue_export_object (fixture->queue,
696 : : "/org/freedesktop/Accounts",
697 : : (GDBusInterfaceInfo *) &org_freedesktop_accounts_interface,
698 : : &local_error);
699 : 16 : g_assert_no_error (local_error);
700 : :
701 : 16 : fixture->manager = mct_manager_new (gt_dbus_queue_get_client_connection (fixture->queue));
702 : 16 : }
703 : :
704 : : static void
705 : 16 : bus_tear_down (BusFixture *fixture,
706 : : gconstpointer test_data)
707 : : {
708 [ + - ]: 16 : g_clear_object (&fixture->manager);
709 : 16 : gt_dbus_queue_disconnect (fixture->queue, TRUE);
710 [ + - ]: 16 : g_clear_pointer (&fixture->queue, gt_dbus_queue_free);
711 : 16 : }
712 : :
713 : : /* Helper #GAsyncReadyCallback which returns the #GAsyncResult in its @user_data. */
714 : : static void
715 : 9 : async_result_cb (GObject *obj,
716 : : GAsyncResult *result,
717 : : gpointer user_data)
718 : : {
719 : 9 : GAsyncResult **result_out = (GAsyncResult **) user_data;
720 : :
721 : 9 : g_assert_null (*result_out);
722 : 9 : *result_out = g_object_ref (result);
723 : 9 : }
724 : :
725 : : /* Generic mock accountsservice implementation which returns the properties
726 : : * given in #GetSessionLimitsData.properties if queried for a UID matching
727 : : * #GetSessionLimitsData.expected_uid. Intended to be used for writing
728 : : * ‘successful’ mct_manager_get_session_limits() tests returning a variety of
729 : : * values. */
730 : : typedef struct
731 : : {
732 : : uid_t expected_uid;
733 : : const gchar *properties;
734 : : } GetSessionLimitsData;
735 : :
736 : : /* This is run in a worker thread. */
737 : : static void
738 : 3 : get_session_limits_server_cb (GtDBusQueue *queue,
739 : : gpointer user_data)
740 : : {
741 : 3 : const GetSessionLimitsData *data = user_data;
742 : 3 : g_autoptr(GDBusMethodInvocation) invocation1 = NULL;
743 : 3 : g_autoptr(GDBusMethodInvocation) invocation2 = NULL;
744 : 3 : g_autofree gchar *object_path = NULL;
745 : 3 : g_autoptr(GVariant) properties_variant = NULL;
746 : :
747 : : /* Handle the FindUserById() call. */
748 : : gint64 user_id;
749 : 3 : invocation1 =
750 : 3 : gt_dbus_queue_assert_pop_message (queue,
751 : : "/org/freedesktop/Accounts",
752 : : "org.freedesktop.Accounts",
753 : : "FindUserById", "(x)", &user_id);
754 : 3 : g_assert_cmpint (user_id, ==, data->expected_uid);
755 : :
756 : 3 : object_path = g_strdup_printf ("/org/freedesktop/Accounts/User%u", (uid_t) user_id);
757 : 3 : g_dbus_method_invocation_return_value (invocation1, g_variant_new ("(o)", object_path));
758 : :
759 : : /* Handle the Properties.GetAll() call and return some arbitrary, valid values
760 : : * for the given user. */
761 : : const gchar *property_interface;
762 : 3 : invocation2 =
763 : 3 : gt_dbus_queue_assert_pop_message (queue,
764 : : object_path,
765 : : "org.freedesktop.DBus.Properties",
766 : : "GetAll", "(&s)", &property_interface);
767 : 3 : g_assert_cmpstr (property_interface, ==, "com.endlessm.ParentalControls.SessionLimits");
768 : :
769 : 3 : properties_variant = g_variant_ref_sink (g_variant_new_parsed (data->properties));
770 : 3 : g_dbus_method_invocation_return_value (invocation2,
771 : : g_variant_new_tuple (&properties_variant, 1));
772 : 3 : }
773 : :
774 : : /* Test that getting an #MctSessionLimits from the mock D-Bus service works. The
775 : : * @test_data is a boolean value indicating whether to do the call
776 : : * synchronously (%FALSE) or asynchronously (%TRUE).
777 : : *
778 : : * The mock D-Bus replies are generated in get_session_limits_server_cb(), which
779 : : * is used for both synchronous and asynchronous calls. */
780 : : static void
781 : 2 : test_session_limits_bus_get (BusFixture *fixture,
782 : : gconstpointer test_data)
783 : : {
784 : 2 : g_autoptr(MctSessionLimits) session_limits = NULL;
785 : 2 : g_autoptr(GError) local_error = NULL;
786 : : guint64 time_remaining_secs;
787 : : gboolean time_limit_enabled;
788 : 2 : gboolean test_async = GPOINTER_TO_UINT (test_data);
789 : 2 : const GetSessionLimitsData get_session_limits_data =
790 : : {
791 : 2 : .expected_uid = fixture->valid_uid,
792 : : .properties = "{"
793 : : "'LimitType': <@u 1>,"
794 : : "'DailySchedule': <(@u 100, @u 8000)>"
795 : : "}"
796 : : };
797 : :
798 : 2 : gt_dbus_queue_set_server_func (fixture->queue, get_session_limits_server_cb,
799 : : (gpointer) &get_session_limits_data);
800 : :
801 [ + + ]: 2 : if (test_async)
802 : : {
803 : 2 : g_autoptr(GAsyncResult) result = NULL;
804 : :
805 : 1 : mct_manager_get_session_limits_async (fixture->manager,
806 : : fixture->valid_uid,
807 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE, NULL,
808 : : async_result_cb, &result);
809 : :
810 [ + + ]: 4 : while (result == NULL)
811 : 3 : g_main_context_iteration (NULL, TRUE);
812 : 1 : session_limits = mct_manager_get_session_limits_finish (fixture->manager, result, &local_error);
813 : : }
814 : : else
815 : : {
816 : 1 : session_limits = mct_manager_get_session_limits (fixture->manager,
817 : : fixture->valid_uid,
818 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE, NULL,
819 : : &local_error);
820 : : }
821 : :
822 : 2 : g_assert_no_error (local_error);
823 : 2 : g_assert_nonnull (session_limits);
824 : :
825 : : /* Check the session limits properties. */
826 : 2 : g_assert_cmpuint (mct_session_limits_get_user_id (session_limits), ==, fixture->valid_uid);
827 : 2 : g_assert_true (mct_session_limits_is_enabled (session_limits));
828 : 4 : g_autoptr(GDateTime) now_dt_local1 = g_date_time_new_from_unix_utc (0);
829 : 2 : g_assert_false (mct_session_limits_check_time_remaining (session_limits, now_dt_local1, 0,
830 : : &time_remaining_secs, &time_limit_enabled));
831 : 2 : g_assert_true (time_limit_enabled);
832 : :
833 : 4 : g_autoptr(GDateTime) now_dt_local2 = g_date_time_new_from_unix_utc (2000);
834 : 2 : g_assert_true (mct_session_limits_check_time_remaining (session_limits, now_dt_local2, 0,
835 : : &time_remaining_secs, &time_limit_enabled));
836 : 2 : g_assert_cmpuint (time_remaining_secs, ==, 8000 - 2000);
837 : 2 : g_assert_true (time_limit_enabled);
838 : 2 : }
839 : :
840 : : /* Test that getting an #MctSessionLimits from the mock D-Bus service works. The
841 : : * @test_data is a boolean value indicating whether to do the call
842 : : * synchronously (%FALSE) or asynchronously (%TRUE).
843 : : *
844 : : * The mock D-Bus replies are generated in get_session_limits_server_cb(), which
845 : : * is used for both synchronous and asynchronous calls. */
846 : : static void
847 : 1 : test_session_limits_bus_get_none (BusFixture *fixture,
848 : : gconstpointer test_data)
849 : : {
850 : 1 : g_autoptr(MctSessionLimits) session_limits = NULL;
851 : 1 : g_autoptr(GError) local_error = NULL;
852 : : guint64 time_remaining_secs;
853 : : gboolean time_limit_enabled;
854 : 1 : gboolean test_async = GPOINTER_TO_UINT (test_data);
855 : 1 : const GetSessionLimitsData get_session_limits_data =
856 : : {
857 : 1 : .expected_uid = fixture->valid_uid,
858 : : .properties = "{"
859 : : "'LimitType': <@u 0>,"
860 : : "'DailySchedule': <(@u 0, @u 86400)>"
861 : : "}"
862 : : };
863 : :
864 : 1 : gt_dbus_queue_set_server_func (fixture->queue, get_session_limits_server_cb,
865 : : (gpointer) &get_session_limits_data);
866 : :
867 [ - + ]: 1 : if (test_async)
868 : : {
869 : 0 : g_autoptr(GAsyncResult) result = NULL;
870 : :
871 : 0 : mct_manager_get_session_limits_async (fixture->manager,
872 : : fixture->valid_uid,
873 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE, NULL,
874 : : async_result_cb, &result);
875 : :
876 [ # # ]: 0 : while (result == NULL)
877 : 0 : g_main_context_iteration (NULL, TRUE);
878 : 0 : session_limits = mct_manager_get_session_limits_finish (fixture->manager, result, &local_error);
879 : : }
880 : : else
881 : : {
882 : 1 : session_limits = mct_manager_get_session_limits (fixture->manager,
883 : : fixture->valid_uid,
884 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE, NULL,
885 : : &local_error);
886 : : }
887 : :
888 : 1 : g_assert_no_error (local_error);
889 : 1 : g_assert_nonnull (session_limits);
890 : :
891 : : /* Check the session limits properties. */
892 : 1 : g_assert_cmpuint (mct_session_limits_get_user_id (session_limits), ==, fixture->valid_uid);
893 : 1 : g_assert_false (mct_session_limits_is_enabled (session_limits));
894 : :
895 : 2 : g_autoptr(GDateTime) now_dt_local1 = g_date_time_new_from_unix_utc (0);
896 : 1 : g_assert_true (mct_session_limits_check_time_remaining (session_limits, now_dt_local1, 0,
897 : : &time_remaining_secs, &time_limit_enabled));
898 : 1 : g_assert_false (time_limit_enabled);
899 : :
900 : 2 : g_autoptr(GDateTime) now_dt_local2 = g_date_time_new_from_unix_utc (2000);
901 : 1 : g_assert_true (mct_session_limits_check_time_remaining (session_limits, now_dt_local2, 0,
902 : : &time_remaining_secs, &time_limit_enabled));
903 : 1 : g_assert_false (time_limit_enabled);
904 : 1 : }
905 : :
906 : : /* Test that mct_manager_get_session_limits() returns an appropriate error if the
907 : : * mock D-Bus service reports that the given user cannot be found.
908 : : *
909 : : * The mock D-Bus replies are generated inline. */
910 : : static void
911 : 1 : test_session_limits_bus_get_error_invalid_user (BusFixture *fixture,
912 : : gconstpointer test_data)
913 : : {
914 : 1 : g_autoptr(GAsyncResult) result = NULL;
915 : 1 : g_autoptr(GError) local_error = NULL;
916 : 1 : g_autoptr(GDBusMethodInvocation) invocation = NULL;
917 : 1 : g_autofree gchar *error_message = NULL;
918 : 1 : g_autoptr(MctSessionLimits) session_limits = NULL;
919 : :
920 : 1 : mct_manager_get_session_limits_async (fixture->manager,
921 : : fixture->missing_uid,
922 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE, NULL,
923 : : async_result_cb, &result);
924 : :
925 : : /* Handle the FindUserById() call and claim the user doesn’t exist. */
926 : : gint64 user_id;
927 : 1 : invocation =
928 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
929 : : "/org/freedesktop/Accounts",
930 : : "org.freedesktop.Accounts",
931 : : "FindUserById", "(x)", &user_id);
932 : 1 : g_assert_cmpint (user_id, ==, fixture->missing_uid);
933 : :
934 : 1 : error_message = g_strdup_printf ("Failed to look up user with uid %u.", fixture->missing_uid);
935 : 1 : g_dbus_method_invocation_return_dbus_error (invocation,
936 : : "org.freedesktop.Accounts.Error.Failed",
937 : : error_message);
938 : :
939 : : /* Get the get_session_limits() result. */
940 [ + + ]: 2 : while (result == NULL)
941 : 1 : g_main_context_iteration (NULL, TRUE);
942 : 1 : session_limits = mct_manager_get_session_limits_finish (fixture->manager, result,
943 : : &local_error);
944 : :
945 : 1 : g_assert_error (local_error,
946 : : MCT_MANAGER_ERROR, MCT_MANAGER_ERROR_INVALID_USER);
947 : 1 : g_assert_null (session_limits);
948 : 1 : }
949 : :
950 : : /* Test that mct_manager_get_session_limits() returns an appropriate error if the
951 : : * mock D-Bus service reports that the properties of the given user can’t be
952 : : * accessed due to permissions.
953 : : *
954 : : * The mock D-Bus replies are generated inline. */
955 : : static void
956 : 1 : test_session_limits_bus_get_error_permission_denied (BusFixture *fixture,
957 : : gconstpointer test_data)
958 : : {
959 : 1 : g_autoptr(GAsyncResult) result = NULL;
960 : 1 : g_autoptr(GError) local_error = NULL;
961 : 1 : g_autoptr(GDBusMethodInvocation) invocation1 = NULL;
962 : 1 : g_autoptr(GDBusMethodInvocation) invocation2 = NULL;
963 : 1 : g_autofree gchar *object_path = NULL;
964 : 1 : g_autoptr(MctSessionLimits) session_limits = NULL;
965 : :
966 : 1 : mct_manager_get_session_limits_async (fixture->manager,
967 : : fixture->valid_uid,
968 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE, NULL,
969 : : async_result_cb, &result);
970 : :
971 : : /* Handle the FindUserById() call. */
972 : : gint64 user_id;
973 : 1 : invocation1 =
974 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
975 : : "/org/freedesktop/Accounts",
976 : : "org.freedesktop.Accounts",
977 : : "FindUserById", "(x)", &user_id);
978 : 1 : g_assert_cmpint (user_id, ==, fixture->valid_uid);
979 : :
980 : 1 : object_path = g_strdup_printf ("/org/freedesktop/Accounts/User%u", (uid_t) user_id);
981 : 1 : g_dbus_method_invocation_return_value (invocation1, g_variant_new ("(o)", object_path));
982 : :
983 : : /* Handle the Properties.GetAll() call and return a permission denied error. */
984 : : const gchar *property_interface;
985 : 1 : invocation2 =
986 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
987 : : object_path,
988 : : "org.freedesktop.DBus.Properties",
989 : : "GetAll", "(&s)", &property_interface);
990 : 1 : g_assert_cmpstr (property_interface, ==, "com.endlessm.ParentalControls.SessionLimits");
991 : :
992 : 1 : g_dbus_method_invocation_return_dbus_error (invocation2,
993 : : "org.freedesktop.Accounts.Error.PermissionDenied",
994 : : "Not authorized");
995 : :
996 : : /* Get the get_session_limits() result. */
997 [ + + ]: 2 : while (result == NULL)
998 : 1 : g_main_context_iteration (NULL, TRUE);
999 : 1 : session_limits = mct_manager_get_session_limits_finish (fixture->manager, result,
1000 : : &local_error);
1001 : :
1002 : 1 : g_assert_error (local_error,
1003 : : MCT_MANAGER_ERROR, MCT_MANAGER_ERROR_PERMISSION_DENIED);
1004 : 1 : g_assert_null (session_limits);
1005 : 1 : }
1006 : :
1007 : : /* Test that mct_manager_get_session_limits() returns an appropriate error if
1008 : : * the mock D-Bus service replies with no session limits properties (implying
1009 : : * that it hasn’t sent the property values because of permissions).
1010 : : *
1011 : : * The mock D-Bus replies are generated inline. */
1012 : : static void
1013 : 1 : test_session_limits_bus_get_error_permission_denied_missing (BusFixture *fixture,
1014 : : gconstpointer test_data)
1015 : : {
1016 : 1 : g_autoptr(GAsyncResult) result = NULL;
1017 : 1 : g_autoptr(GError) local_error = NULL;
1018 : 1 : g_autoptr(GDBusMethodInvocation) invocation1 = NULL;
1019 : 1 : g_autoptr(GDBusMethodInvocation) invocation2 = NULL;
1020 : 1 : g_autofree gchar *object_path = NULL;
1021 : 1 : g_autoptr(MctSessionLimits) session_limits = NULL;
1022 : :
1023 : 1 : mct_manager_get_session_limits_async (fixture->manager,
1024 : : fixture->valid_uid,
1025 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE, NULL,
1026 : : async_result_cb, &result);
1027 : :
1028 : : /* Handle the FindUserById() call. */
1029 : : gint64 user_id;
1030 : 1 : invocation1 =
1031 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
1032 : : "/org/freedesktop/Accounts",
1033 : : "org.freedesktop.Accounts",
1034 : : "FindUserById", "(x)", &user_id);
1035 : 1 : g_assert_cmpint (user_id, ==, fixture->valid_uid);
1036 : :
1037 : 1 : object_path = g_strdup_printf ("/org/freedesktop/Accounts/User%u", (uid_t) user_id);
1038 : 1 : g_dbus_method_invocation_return_value (invocation1, g_variant_new ("(o)", object_path));
1039 : :
1040 : : /* Handle the Properties.GetAll() call and return an empty array due to not
1041 : : * having permission to access the properties. The code actually keys off the
1042 : : * presence of the LimitType property, since that was the first one to be
1043 : : * added. */
1044 : : const gchar *property_interface;
1045 : 1 : invocation2 =
1046 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
1047 : : object_path,
1048 : : "org.freedesktop.DBus.Properties",
1049 : : "GetAll", "(&s)", &property_interface);
1050 : 1 : g_assert_cmpstr (property_interface, ==, "com.endlessm.ParentalControls.SessionLimits");
1051 : :
1052 : 1 : g_dbus_method_invocation_return_value (invocation2, g_variant_new ("(a{sv})", NULL));
1053 : :
1054 : : /* Get the get_session_limits() result. */
1055 [ + + ]: 2 : while (result == NULL)
1056 : 1 : g_main_context_iteration (NULL, TRUE);
1057 : 1 : session_limits = mct_manager_get_session_limits_finish (fixture->manager, result,
1058 : : &local_error);
1059 : :
1060 : 1 : g_assert_error (local_error,
1061 : : MCT_MANAGER_ERROR, MCT_MANAGER_ERROR_PERMISSION_DENIED);
1062 : 1 : g_assert_null (session_limits);
1063 : 1 : }
1064 : :
1065 : : /* Test that mct_manager_get_session_limits() returns an error if the mock D-Bus
1066 : : * service reports an unrecognised error.
1067 : : *
1068 : : * The mock D-Bus replies are generated inline. */
1069 : : static void
1070 : 1 : test_session_limits_bus_get_error_unknown (BusFixture *fixture,
1071 : : gconstpointer test_data)
1072 : : {
1073 : 1 : g_autoptr(GAsyncResult) result = NULL;
1074 : 1 : g_autoptr(GError) local_error = NULL;
1075 : 1 : g_autoptr(GDBusMethodInvocation) invocation = NULL;
1076 : 1 : g_autoptr(MctSessionLimits) session_limits = NULL;
1077 : :
1078 : 1 : mct_manager_get_session_limits_async (fixture->manager,
1079 : : fixture->valid_uid,
1080 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE, NULL,
1081 : : async_result_cb, &result);
1082 : :
1083 : : /* Handle the FindUserById() call and return a bogus error. */
1084 : : gint64 user_id;
1085 : 1 : invocation =
1086 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
1087 : : "/org/freedesktop/Accounts",
1088 : : "org.freedesktop.Accounts",
1089 : : "FindUserById", "(x)", &user_id);
1090 : 1 : g_assert_cmpint (user_id, ==, fixture->valid_uid);
1091 : :
1092 : 1 : g_dbus_method_invocation_return_dbus_error (invocation,
1093 : : "org.freedesktop.Accounts.Error.NewAndInterestingError",
1094 : : "This is a fake error message "
1095 : : "which libmalcontent "
1096 : : "will never have seen before, "
1097 : : "but must still handle correctly");
1098 : :
1099 : : /* Get the get_session_limits() result. */
1100 [ + + ]: 2 : while (result == NULL)
1101 : 1 : g_main_context_iteration (NULL, TRUE);
1102 : 1 : session_limits = mct_manager_get_session_limits_finish (fixture->manager, result,
1103 : : &local_error);
1104 : :
1105 : : /* We don’t actually care what error is actually used here. */
1106 : 1 : g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
1107 : 1 : g_assert_null (session_limits);
1108 : 1 : }
1109 : :
1110 : : /* Test that mct_manager_get_session_limits() returns an error if the mock D-Bus
1111 : : * service reports an unknown interface, which means that parental controls are
1112 : : * not installed properly.
1113 : : *
1114 : : * The mock D-Bus replies are generated inline. */
1115 : : static void
1116 : 1 : test_session_limits_bus_get_error_disabled (BusFixture *fixture,
1117 : : gconstpointer test_data)
1118 : : {
1119 : 1 : g_autoptr(GAsyncResult) result = NULL;
1120 : 1 : g_autoptr(GError) local_error = NULL;
1121 : 1 : g_autoptr(GDBusMethodInvocation) invocation1 = NULL;
1122 : 1 : g_autoptr(GDBusMethodInvocation) invocation2 = NULL;
1123 : 1 : g_autofree gchar *object_path = NULL;
1124 : 1 : g_autoptr(MctSessionLimits) session_limits = NULL;
1125 : :
1126 : 1 : mct_manager_get_session_limits_async (fixture->manager,
1127 : : fixture->valid_uid,
1128 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE, NULL,
1129 : : async_result_cb, &result);
1130 : :
1131 : : /* Handle the FindUserById() call. */
1132 : : gint64 user_id;
1133 : 1 : invocation1 =
1134 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
1135 : : "/org/freedesktop/Accounts",
1136 : : "org.freedesktop.Accounts",
1137 : : "FindUserById", "(x)", &user_id);
1138 : 1 : g_assert_cmpint (user_id, ==, fixture->valid_uid);
1139 : :
1140 : 1 : object_path = g_strdup_printf ("/org/freedesktop/Accounts/User%u", (uid_t) user_id);
1141 : 1 : g_dbus_method_invocation_return_value (invocation1, g_variant_new ("(o)", object_path));
1142 : :
1143 : : /* Handle the Properties.GetAll() call and return an InvalidArgs error. */
1144 : : const gchar *property_interface;
1145 : 1 : invocation2 =
1146 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
1147 : : object_path,
1148 : : "org.freedesktop.DBus.Properties",
1149 : : "GetAll", "(&s)", &property_interface);
1150 : 1 : g_assert_cmpstr (property_interface, ==, "com.endlessm.ParentalControls.SessionLimits");
1151 : :
1152 : 1 : g_dbus_method_invocation_return_dbus_error (invocation2,
1153 : : "org.freedesktop.DBus.Error.InvalidArgs",
1154 : : "No such interface "
1155 : : "“com.endlessm.ParentalControls.SessionLimits”");
1156 : :
1157 : : /* Get the get_session_limits() result. */
1158 [ + + ]: 2 : while (result == NULL)
1159 : 1 : g_main_context_iteration (NULL, TRUE);
1160 : 1 : session_limits = mct_manager_get_session_limits_finish (fixture->manager, result,
1161 : : &local_error);
1162 : :
1163 : 1 : g_assert_error (local_error,
1164 : : MCT_MANAGER_ERROR, MCT_MANAGER_ERROR_DISABLED);
1165 : 1 : g_assert_null (session_limits);
1166 : 1 : }
1167 : :
1168 : : /* Test that mct_manager_get_session_limits() returns an error if the mock D-Bus
1169 : : * service reports an unknown interface (via a different kind of error), which
1170 : : * means that parental controls are not installed properly.
1171 : : *
1172 : : * The mock D-Bus replies are generated inline. */
1173 : : static void
1174 : 1 : test_session_limits_bus_get_error_disabled2 (BusFixture *fixture,
1175 : : gconstpointer test_data)
1176 : : {
1177 : 1 : g_autoptr(GAsyncResult) result = NULL;
1178 : 1 : g_autoptr(GError) local_error = NULL;
1179 : 1 : g_autoptr(GDBusMethodInvocation) invocation1 = NULL;
1180 : 1 : g_autoptr(GDBusMethodInvocation) invocation2 = NULL;
1181 : 1 : g_autofree gchar *object_path = NULL;
1182 : 1 : g_autoptr(MctSessionLimits) session_limits = NULL;
1183 : :
1184 : 1 : mct_manager_get_session_limits_async (fixture->manager,
1185 : : fixture->valid_uid,
1186 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE, NULL,
1187 : : async_result_cb, &result);
1188 : :
1189 : : /* Handle the FindUserById() call. */
1190 : : gint64 user_id;
1191 : 1 : invocation1 =
1192 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
1193 : : "/org/freedesktop/Accounts",
1194 : : "org.freedesktop.Accounts",
1195 : : "FindUserById", "(x)", &user_id);
1196 : 1 : g_assert_cmpint (user_id, ==, fixture->valid_uid);
1197 : :
1198 : 1 : object_path = g_strdup_printf ("/org/freedesktop/Accounts/User%u", (uid_t) user_id);
1199 : 1 : g_dbus_method_invocation_return_value (invocation1, g_variant_new ("(o)", object_path));
1200 : :
1201 : : /* Handle the Properties.GetAll() call and return an InvalidArgs error. */
1202 : : const gchar *property_interface;
1203 : 1 : invocation2 =
1204 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
1205 : : object_path,
1206 : : "org.freedesktop.DBus.Properties",
1207 : : "GetAll", "(&s)", &property_interface);
1208 : 1 : g_assert_cmpstr (property_interface, ==, "com.endlessm.ParentalControls.SessionLimits");
1209 : :
1210 : 1 : g_dbus_method_invocation_return_dbus_error (invocation2,
1211 : : "org.freedesktop.Accounts.User.UnknownInterface",
1212 : : "no such interface com.endlessm.ParentalControls.SessionLimits");
1213 : :
1214 : : /* Get the get_session_limits() result. */
1215 [ + + ]: 2 : while (result == NULL)
1216 : 1 : g_main_context_iteration (NULL, TRUE);
1217 : 1 : session_limits = mct_manager_get_session_limits_finish (fixture->manager, result,
1218 : : &local_error);
1219 : :
1220 : 1 : g_assert_error (local_error,
1221 : : MCT_MANAGER_ERROR, MCT_MANAGER_ERROR_DISABLED);
1222 : 1 : g_assert_null (session_limits);
1223 : 1 : }
1224 : :
1225 : : /* Generic mock accountsservice implementation which handles properties being
1226 : : * set on a mock User object, and compares their values to the given
1227 : : * `expected_*` ones.
1228 : : *
1229 : : * If @error_index is non-negative, it gives the index of a Set() call to return
1230 : : * the given @dbus_error_name and @dbus_error_message from, rather than
1231 : : * accepting the property value from the caller. If @error_index is negative,
1232 : : * all Set() calls will be accepted. */
1233 : : typedef struct
1234 : : {
1235 : : uid_t expected_uid;
1236 : :
1237 : : const gchar * const *expected_properties;
1238 : :
1239 : : /* All GVariants in text format: */
1240 : : const gchar *expected_limit_type_value; /* (nullable) */
1241 : : const gchar *expected_daily_schedule_value; /* (nullable) */
1242 : :
1243 : : gint error_index; /* -1 to return no error */
1244 : : const gchar *dbus_error_name; /* NULL to return no error */
1245 : : const gchar *dbus_error_message; /* NULL to return no error */
1246 : : } SetSessionLimitsData;
1247 : :
1248 : : static const gchar *
1249 : 5 : set_session_limits_data_get_expected_property_value (const SetSessionLimitsData *data,
1250 : : const gchar *property_name)
1251 : : {
1252 [ + + ]: 5 : if (g_str_equal (property_name, "LimitType"))
1253 : 2 : return data->expected_limit_type_value;
1254 [ + - ]: 3 : else if (g_str_equal (property_name, "DailySchedule"))
1255 : 3 : return data->expected_daily_schedule_value;
1256 : : else
1257 : : g_assert_not_reached ();
1258 : : }
1259 : :
1260 : : /* This is run in a worker thread. */
1261 : : static void
1262 : 6 : set_session_limits_server_cb (GtDBusQueue *queue,
1263 : : gpointer user_data)
1264 : : {
1265 : 6 : const SetSessionLimitsData *data = user_data;
1266 : 6 : g_autoptr(GDBusMethodInvocation) find_invocation = NULL;
1267 : 6 : g_autofree gchar *object_path = NULL;
1268 : :
1269 : 6 : g_assert ((data->error_index == -1) == (data->dbus_error_name == NULL));
1270 : 6 : g_assert ((data->dbus_error_name == NULL) == (data->dbus_error_message == NULL));
1271 : :
1272 : : /* Handle the FindUserById() call. */
1273 : : gint64 user_id;
1274 : 6 : find_invocation =
1275 : 6 : gt_dbus_queue_assert_pop_message (queue,
1276 : : "/org/freedesktop/Accounts",
1277 : : "org.freedesktop.Accounts",
1278 : : "FindUserById", "(x)", &user_id);
1279 : 6 : g_assert_cmpint (user_id, ==, data->expected_uid);
1280 : :
1281 : 6 : object_path = g_strdup_printf ("/org/freedesktop/Accounts/User%u", (uid_t) user_id);
1282 : 6 : g_dbus_method_invocation_return_value (find_invocation, g_variant_new ("(o)", object_path));
1283 : :
1284 : : /* Handle the Properties.Set() calls. */
1285 : : gsize i;
1286 : :
1287 [ + + ]: 11 : for (i = 0; data->expected_properties[i] != NULL; i++)
1288 : : {
1289 : : const gchar *property_interface;
1290 : : const gchar *property_name;
1291 [ + + ]: 9 : g_autoptr(GVariant) property_value = NULL;
1292 [ + + ]: 9 : g_autoptr(GDBusMethodInvocation) property_invocation = NULL;
1293 [ + + ]: 9 : g_autoptr(GVariant) expected_property_value = NULL;
1294 : :
1295 : 9 : property_invocation =
1296 : 9 : gt_dbus_queue_assert_pop_message (queue,
1297 : : object_path,
1298 : : "org.freedesktop.DBus.Properties",
1299 : : "Set", "(&s&sv)", &property_interface,
1300 : : &property_name, &property_value);
1301 : 9 : g_assert_cmpstr (property_interface, ==, "com.endlessm.ParentalControls.SessionLimits");
1302 : 9 : g_assert_cmpstr (property_name, ==, data->expected_properties[i]);
1303 : :
1304 [ + + + + ]: 9 : if (data->error_index >= 0 && (gsize) data->error_index == i)
1305 : : {
1306 : 4 : g_dbus_method_invocation_return_dbus_error (property_invocation,
1307 : 4 : data->dbus_error_name,
1308 : 4 : data->dbus_error_message);
1309 : 4 : break;
1310 : : }
1311 : : else
1312 : : {
1313 : 5 : expected_property_value = g_variant_new_parsed (set_session_limits_data_get_expected_property_value (data, property_name));
1314 : 5 : g_assert_cmpvariant (property_value, expected_property_value);
1315 : :
1316 : 5 : g_dbus_method_invocation_return_value (property_invocation, NULL);
1317 : : }
1318 : : }
1319 : 6 : }
1320 : :
1321 : : /* Test that setting an #MctSessionLimits on the mock D-Bus service works. The
1322 : : * @test_data is a boolean value indicating whether to do the call
1323 : : * synchronously (%FALSE) or asynchronously (%TRUE).
1324 : : *
1325 : : * The mock D-Bus replies are generated in set_session_limits_server_cb(), which
1326 : : * is used for both synchronous and asynchronous calls. */
1327 : : static void
1328 : 2 : test_session_limits_bus_set (BusFixture *fixture,
1329 : : gconstpointer test_data)
1330 : : {
1331 : : gboolean success;
1332 : 2 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
1333 : 2 : g_autoptr(MctSessionLimits) session_limits = NULL;
1334 : 2 : g_autoptr(GError) local_error = NULL;
1335 : 2 : gboolean test_async = GPOINTER_TO_UINT (test_data);
1336 : 2 : const gchar *expected_properties[] =
1337 : : {
1338 : : "DailySchedule",
1339 : : "LimitType",
1340 : : NULL
1341 : : };
1342 : 2 : const SetSessionLimitsData set_session_limits_data =
1343 : : {
1344 : 2 : .expected_uid = fixture->valid_uid,
1345 : : .expected_properties = expected_properties,
1346 : : .expected_limit_type_value = "@u 1",
1347 : : .expected_daily_schedule_value = "(@u 100, @u 4000)",
1348 : : .error_index = -1,
1349 : : };
1350 : :
1351 : : /* Build a session limits object. */
1352 : 2 : mct_session_limits_builder_set_daily_schedule (&builder, 100, 4000);
1353 : :
1354 : 2 : session_limits = mct_session_limits_builder_end (&builder);
1355 : :
1356 : : /* Set the mock service function and set the limits. */
1357 : 2 : gt_dbus_queue_set_server_func (fixture->queue, set_session_limits_server_cb,
1358 : : (gpointer) &set_session_limits_data);
1359 : :
1360 [ + + ]: 2 : if (test_async)
1361 : : {
1362 : 1 : g_autoptr(GAsyncResult) result = NULL;
1363 : :
1364 : 1 : mct_manager_set_session_limits_async (fixture->manager,
1365 : : fixture->valid_uid, session_limits,
1366 : : MCT_MANAGER_SET_VALUE_FLAGS_NONE, NULL,
1367 : : async_result_cb, &result);
1368 : :
1369 [ + + ]: 6 : while (result == NULL)
1370 : 5 : g_main_context_iteration (NULL, TRUE);
1371 : 1 : success = mct_manager_set_session_limits_finish (fixture->manager, result,
1372 : : &local_error);
1373 : : }
1374 : : else
1375 : : {
1376 : 1 : success = mct_manager_set_session_limits (fixture->manager,
1377 : : fixture->valid_uid, session_limits,
1378 : : MCT_MANAGER_SET_VALUE_FLAGS_NONE, NULL,
1379 : : &local_error);
1380 : : }
1381 : :
1382 : 2 : g_assert_no_error (local_error);
1383 : 2 : g_assert_true (success);
1384 : 2 : }
1385 : :
1386 : : /* Test that mct_manager_set_session_limits() returns an appropriate error if
1387 : : * the mock D-Bus service reports that the given user cannot be found.
1388 : : *
1389 : : * The mock D-Bus replies are generated inline. */
1390 : : static void
1391 : 1 : test_session_limits_bus_set_error_invalid_user (BusFixture *fixture,
1392 : : gconstpointer test_data)
1393 : : {
1394 : : gboolean success;
1395 : 1 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
1396 : 1 : g_autoptr(MctSessionLimits) session_limits = NULL;
1397 : 1 : g_autoptr(GAsyncResult) result = NULL;
1398 : 1 : g_autoptr(GError) local_error = NULL;
1399 : 1 : g_autoptr(GDBusMethodInvocation) invocation = NULL;
1400 : 1 : g_autofree gchar *error_message = NULL;
1401 : :
1402 : : /* Use the default session limits. */
1403 : 1 : session_limits = mct_session_limits_builder_end (&builder);
1404 : :
1405 : 1 : mct_manager_set_session_limits_async (fixture->manager,
1406 : : fixture->missing_uid, session_limits,
1407 : : MCT_MANAGER_SET_VALUE_FLAGS_NONE, NULL,
1408 : : async_result_cb, &result);
1409 : :
1410 : : /* Handle the FindUserById() call and claim the user doesn’t exist. */
1411 : : gint64 user_id;
1412 : 1 : invocation =
1413 : 1 : gt_dbus_queue_assert_pop_message (fixture->queue,
1414 : : "/org/freedesktop/Accounts",
1415 : : "org.freedesktop.Accounts",
1416 : : "FindUserById", "(x)", &user_id);
1417 : 1 : g_assert_cmpint (user_id, ==, fixture->missing_uid);
1418 : :
1419 : 1 : error_message = g_strdup_printf ("Failed to look up user with uid %u.", fixture->missing_uid);
1420 : 1 : g_dbus_method_invocation_return_dbus_error (invocation,
1421 : : "org.freedesktop.Accounts.Error.Failed",
1422 : : error_message);
1423 : :
1424 : : /* Get the set_session_limits() result. */
1425 [ + + ]: 2 : while (result == NULL)
1426 : 1 : g_main_context_iteration (NULL, TRUE);
1427 : 1 : success = mct_manager_set_session_limits_finish (fixture->manager, result,
1428 : : &local_error);
1429 : :
1430 : 1 : g_assert_error (local_error,
1431 : : MCT_MANAGER_ERROR, MCT_MANAGER_ERROR_INVALID_USER);
1432 : 1 : g_assert_false (success);
1433 : 1 : }
1434 : :
1435 : : /* Test that mct_manager_set_session_limits() returns an appropriate error if the
1436 : : * mock D-Bus service replies with a permission denied error when setting
1437 : : * properties.
1438 : : *
1439 : : * The mock D-Bus replies are generated in set_session_limits_server_cb(). */
1440 : : static void
1441 : 1 : test_session_limits_bus_set_error_permission_denied (BusFixture *fixture,
1442 : : gconstpointer test_data)
1443 : : {
1444 : : gboolean success;
1445 : 1 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
1446 : 1 : g_autoptr(MctSessionLimits) session_limits = NULL;
1447 : 1 : g_autoptr(GError) local_error = NULL;
1448 : 1 : const gchar *expected_properties[] =
1449 : : {
1450 : : "LimitType",
1451 : : NULL
1452 : : };
1453 : 1 : const SetSessionLimitsData set_session_limits_data =
1454 : : {
1455 : 1 : .expected_uid = fixture->valid_uid,
1456 : : .expected_properties = expected_properties,
1457 : : .error_index = 0,
1458 : : .dbus_error_name = "org.freedesktop.Accounts.Error.PermissionDenied",
1459 : : .dbus_error_message = "Not authorized",
1460 : : };
1461 : :
1462 : : /* Use the default session limits. */
1463 : 1 : session_limits = mct_session_limits_builder_end (&builder);
1464 : :
1465 : 1 : gt_dbus_queue_set_server_func (fixture->queue, set_session_limits_server_cb,
1466 : : (gpointer) &set_session_limits_data);
1467 : :
1468 : 1 : success = mct_manager_set_session_limits (fixture->manager,
1469 : : fixture->valid_uid, session_limits,
1470 : : MCT_MANAGER_SET_VALUE_FLAGS_NONE, NULL,
1471 : : &local_error);
1472 : :
1473 : 1 : g_assert_error (local_error,
1474 : : MCT_MANAGER_ERROR, MCT_MANAGER_ERROR_PERMISSION_DENIED);
1475 : 1 : g_assert_false (success);
1476 : 1 : }
1477 : :
1478 : : /* Test that mct_manager_set_session_limits() returns an error if the mock D-Bus
1479 : : * service reports an unrecognised error.
1480 : : *
1481 : : * The mock D-Bus replies are generated in set_session_limits_server_cb(). */
1482 : : static void
1483 : 1 : test_session_limits_bus_set_error_unknown (BusFixture *fixture,
1484 : : gconstpointer test_data)
1485 : : {
1486 : : gboolean success;
1487 : 1 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
1488 : 1 : g_autoptr(MctSessionLimits) session_limits = NULL;
1489 : 1 : g_autoptr(GError) local_error = NULL;
1490 : 1 : const gchar *expected_properties[] =
1491 : : {
1492 : : "LimitType",
1493 : : NULL
1494 : : };
1495 : 1 : const SetSessionLimitsData set_session_limits_data =
1496 : : {
1497 : 1 : .expected_uid = fixture->valid_uid,
1498 : : .expected_properties = expected_properties,
1499 : : .error_index = 0,
1500 : : .dbus_error_name = "org.freedesktop.Accounts.Error.NewAndInterestingError",
1501 : : .dbus_error_message = "This is a fake error message which "
1502 : : "libmalcontent will never have seen "
1503 : : "before, but must still handle correctly",
1504 : : };
1505 : :
1506 : : /* Use the default session limits. */
1507 : 1 : session_limits = mct_session_limits_builder_end (&builder);
1508 : :
1509 : 1 : gt_dbus_queue_set_server_func (fixture->queue, set_session_limits_server_cb,
1510 : : (gpointer) &set_session_limits_data);
1511 : :
1512 : 1 : success = mct_manager_set_session_limits (fixture->manager,
1513 : : fixture->valid_uid, session_limits,
1514 : : MCT_MANAGER_SET_VALUE_FLAGS_NONE, NULL,
1515 : : &local_error);
1516 : :
1517 : 1 : g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
1518 : 1 : g_assert_false (success);
1519 : 1 : }
1520 : :
1521 : : /* Test that mct_manager_set_session_limits() returns an error if the mock D-Bus
1522 : : * service reports an InvalidArgs error with a given one of its Set() calls.
1523 : : *
1524 : : * @test_data contains a property index encoded with GINT_TO_POINTER(),
1525 : : * indicating which Set() call to return the error on, since the calls are made
1526 : : * in series.
1527 : : *
1528 : : * The mock D-Bus replies are generated in set_session_limits_server_cb(). */
1529 : : static void
1530 : 2 : test_session_limits_bus_set_error_invalid_property (BusFixture *fixture,
1531 : : gconstpointer test_data)
1532 : : {
1533 : : gboolean success;
1534 : 2 : g_auto(MctSessionLimitsBuilder) builder = MCT_SESSION_LIMITS_BUILDER_INIT ();
1535 : 2 : g_autoptr(MctSessionLimits) session_limits = NULL;
1536 : 2 : g_autoptr(GError) local_error = NULL;
1537 : 2 : const gchar *expected_properties[] =
1538 : : {
1539 : : "DailySchedule",
1540 : : "LimitType",
1541 : : NULL
1542 : : };
1543 : 2 : const SetSessionLimitsData set_session_limits_data =
1544 : : {
1545 : 2 : .expected_uid = fixture->valid_uid,
1546 : : .expected_properties = expected_properties,
1547 : : .expected_limit_type_value = "@u 1",
1548 : : .expected_daily_schedule_value = "(@u 100, @u 3000)",
1549 : 2 : .error_index = GPOINTER_TO_INT (test_data),
1550 : : .dbus_error_name = "org.freedesktop.DBus.Error.InvalidArgs",
1551 : : .dbus_error_message = "Mumble mumble something wrong with the limits value",
1552 : : };
1553 : :
1554 : : /* Build a session limits object. */
1555 : 2 : mct_session_limits_builder_set_daily_schedule (&builder, 100, 3000);
1556 : :
1557 : 2 : session_limits = mct_session_limits_builder_end (&builder);
1558 : :
1559 : 2 : gt_dbus_queue_set_server_func (fixture->queue, set_session_limits_server_cb,
1560 : : (gpointer) &set_session_limits_data);
1561 : :
1562 : 2 : success = mct_manager_set_session_limits (fixture->manager,
1563 : : fixture->valid_uid, session_limits,
1564 : : MCT_MANAGER_SET_VALUE_FLAGS_NONE, NULL,
1565 : : &local_error);
1566 : :
1567 : 2 : g_assert_error (local_error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
1568 : 2 : g_assert_false (success);
1569 : 2 : }
1570 : :
1571 : : int
1572 : 3 : main (int argc,
1573 : : char **argv)
1574 : : {
1575 : 3 : setlocale (LC_ALL, "");
1576 : 3 : g_test_init (&argc, &argv, NULL);
1577 : :
1578 : 3 : g_test_add_func ("/session-limits/types", test_session_limits_types);
1579 : 3 : g_test_add_func ("/session-limits/refs", test_session_limits_refs);
1580 : 3 : g_test_add_func ("/session-limits/check-time-remaining/invalid-time",
1581 : : test_session_limits_check_time_remaining_invalid_time);
1582 : 3 : g_test_add_func ("/session-limits/check-time-remaining/daily-schedule",
1583 : : test_session_limits_check_time_remaining_daily_schedule);
1584 : 3 : g_test_add_func ("/session-limits/check-time-remaining/daily-schedule/timezone",
1585 : : test_session_limits_check_time_remaining_daily_schedule_timezone);
1586 : 3 : g_test_add_func ("/session-limits/check-time-remaining/daily-limit",
1587 : : test_session_limits_check_time_remaining_daily_limit);
1588 : 3 : g_test_add_func ("/session-limits/check-time-remaining/multiple",
1589 : : test_session_limits_check_time_remaining_multiple);
1590 : :
1591 : 3 : g_test_add_func ("/session-limits/serialize", test_session_limits_serialize);
1592 : 3 : g_test_add_func ("/session-limits/deserialize", test_session_limits_deserialize);
1593 : 3 : g_test_add_func ("/session-limits/deserialize/invalid", test_session_limits_deserialize_invalid);
1594 : :
1595 : 3 : g_test_add_func ("/session-limits/equal", test_session_limits_equal);
1596 : :
1597 : 3 : g_test_add ("/session-limits/builder/stack/non-empty", BuilderFixture, NULL,
1598 : : builder_set_up_stack, test_session_limits_builder_non_empty,
1599 : : builder_tear_down_stack);
1600 : 3 : g_test_add ("/session-limits/builder/stack/empty", BuilderFixture, NULL,
1601 : : builder_set_up_stack, test_session_limits_builder_empty,
1602 : : builder_tear_down_stack);
1603 : 3 : g_test_add ("/session-limits/builder/stack2/non-empty", BuilderFixture, NULL,
1604 : : builder_set_up_stack2, test_session_limits_builder_non_empty,
1605 : : builder_tear_down_stack2);
1606 : 3 : g_test_add ("/session-limits/builder/stack2/empty", BuilderFixture, NULL,
1607 : : builder_set_up_stack2, test_session_limits_builder_empty,
1608 : : builder_tear_down_stack2);
1609 : 3 : g_test_add ("/session-limits/builder/heap/non-empty", BuilderFixture, NULL,
1610 : : builder_set_up_heap, test_session_limits_builder_non_empty,
1611 : : builder_tear_down_heap);
1612 : 3 : g_test_add ("/session-limits/builder/heap/empty", BuilderFixture, NULL,
1613 : : builder_set_up_heap, test_session_limits_builder_empty,
1614 : : builder_tear_down_heap);
1615 : 3 : g_test_add_func ("/session-limits/builder/copy/empty",
1616 : : test_session_limits_builder_copy_empty);
1617 : 3 : g_test_add_func ("/session-limits/builder/copy/full",
1618 : : test_session_limits_builder_copy_full);
1619 : 3 : g_test_add_func ("/session-limits/builder/override/none",
1620 : : test_session_limits_builder_override_none);
1621 : 3 : g_test_add_func ("/session-limits/builder/override/daily-schedule",
1622 : : test_session_limits_builder_override_daily_schedule);
1623 : 3 : g_test_add_func ("/session-limits/builder/override/daily-limit",
1624 : : test_session_limits_builder_override_daily_limit);
1625 : :
1626 : 3 : g_test_add ("/session-limits/bus/get/async", BusFixture, GUINT_TO_POINTER (TRUE),
1627 : : bus_set_up, test_session_limits_bus_get, bus_tear_down);
1628 : 3 : g_test_add ("/session-limits/bus/get/sync", BusFixture, GUINT_TO_POINTER (FALSE),
1629 : : bus_set_up, test_session_limits_bus_get, bus_tear_down);
1630 : 3 : g_test_add ("/session-limits/bus/get/none", BusFixture, NULL,
1631 : : bus_set_up, test_session_limits_bus_get_none, bus_tear_down);
1632 : :
1633 : 3 : g_test_add ("/session-limits/bus/get/error/invalid-user", BusFixture, NULL,
1634 : : bus_set_up, test_session_limits_bus_get_error_invalid_user, bus_tear_down);
1635 : 3 : g_test_add ("/session-limits/bus/get/error/permission-denied", BusFixture, NULL,
1636 : : bus_set_up, test_session_limits_bus_get_error_permission_denied, bus_tear_down);
1637 : 3 : g_test_add ("/session-limits/bus/get/error/permission-denied-missing", BusFixture, NULL,
1638 : : bus_set_up, test_session_limits_bus_get_error_permission_denied_missing, bus_tear_down);
1639 : 3 : g_test_add ("/session-limits/bus/get/error/unknown", BusFixture, NULL,
1640 : : bus_set_up, test_session_limits_bus_get_error_unknown, bus_tear_down);
1641 : 3 : g_test_add ("/session-limits/bus/get/error/disabled", BusFixture, NULL,
1642 : : bus_set_up, test_session_limits_bus_get_error_disabled, bus_tear_down);
1643 : 3 : g_test_add ("/session-limits/bus/get/error/disabled2", BusFixture, NULL,
1644 : : bus_set_up, test_session_limits_bus_get_error_disabled2, bus_tear_down);
1645 : :
1646 : 3 : g_test_add ("/session-limits/bus/set/async", BusFixture, GUINT_TO_POINTER (TRUE),
1647 : : bus_set_up, test_session_limits_bus_set, bus_tear_down);
1648 : 3 : g_test_add ("/session-limits/bus/set/sync", BusFixture, GUINT_TO_POINTER (FALSE),
1649 : : bus_set_up, test_session_limits_bus_set, bus_tear_down);
1650 : :
1651 : 3 : g_test_add ("/session-limits/bus/set/error/invalid-user", BusFixture, NULL,
1652 : : bus_set_up, test_session_limits_bus_set_error_invalid_user, bus_tear_down);
1653 : 3 : g_test_add ("/session-limits/bus/set/error/permission-denied", BusFixture, NULL,
1654 : : bus_set_up, test_session_limits_bus_set_error_permission_denied, bus_tear_down);
1655 : 3 : g_test_add ("/session-limits/bus/set/error/unknown", BusFixture, NULL,
1656 : : bus_set_up, test_session_limits_bus_set_error_unknown, bus_tear_down);
1657 : 3 : g_test_add ("/session-limits/bus/set/error/invalid-property/daily-schedule",
1658 : : BusFixture, GINT_TO_POINTER (0), bus_set_up,
1659 : : test_session_limits_bus_set_error_invalid_property, bus_tear_down);
1660 : 3 : g_test_add ("/session-limits/bus/set/error/invalid-property/limit-type",
1661 : : BusFixture, GINT_TO_POINTER (1), bus_set_up,
1662 : : test_session_limits_bus_set_error_invalid_property, bus_tear_down);
1663 : :
1664 : 3 : return g_test_run ();
1665 : : }
|