test-sysfs.c 27.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright © 2018 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

#include "config.h"

23
#include "bolt-macros.h"
24
#include "bolt-store.h"
25
#include "bolt-str.h"
26 27 28
#include "bolt-sysfs.h"
#include "bolt-domain.h"

29
#include "bolt-test.h"
30 31
#include "mock-sysfs.h"

32 33
#include "bolt-daemon-resource.h"

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
#include <glib.h>
#include <gio/gio.h>
#include <glib/gprintf.h>
#include <glib/gstdio.h>

#include <libudev.h>
#include <locale.h>

typedef struct udev_device udev_device;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (udev_device, udev_device_unref);

typedef struct
{
  MockSysfs   *sysfs;
  struct udev *udev;
} TestSysfs;


static void
test_sysfs_setup (TestSysfs *tt, gconstpointer data)
{
  tt->sysfs = mock_sysfs_new ();
  tt->udev = udev_new ();
}

static void
test_sysfs_tear_down (TestSysfs *tt, gconstpointer user)
{
  g_clear_object (&tt->sysfs);
  g_clear_pointer (&tt->udev, udev_unref);
}

66 67 68 69 70 71 72 73 74
static void
count_domains (gpointer data,
               gpointer user_data)
{
  int *n = user_data;

  (*n)++;
}

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
static void
test_sysfs_domain_for_device (TestSysfs *tt, gconstpointer user)
{
  g_autoptr(udev_device) udevice = NULL;
  udev_device *dh;
  udev_device *dd;
  const char *domain;
  const char *host;
  const char *dock;
  const char *syspath;
  MockDevId hostid = {
    .vendor_id = 0x42,
    .vendor_name = "GNOME.org",
    .device_id = 0x42,
    .device_name = "Laptop",
    .unique_id = "884c6edd-7118-4b21-b186-b02d396ecca0",
  };
  MockDevId dockid = {
    .vendor_id = 0x42,
    .vendor_name = "GNOME.org",
    .device_id = 0x42,
    .device_name = "Thunderbolt Dock",
    .unique_id = "884c6edd-7118-4b21-b186-b02d396ecca1",
  };

  domain = mock_sysfs_domain_add (tt->sysfs, BOLT_SECURITY_SECURE, NULL);
  g_assert_nonnull (domain);

  host = mock_sysfs_host_add (tt->sysfs, domain, &hostid);
  g_assert_nonnull (host);

  dock = mock_sysfs_device_add (tt->sysfs,
                                host,
                                &dockid,
                                0,
                                NULL,
                                0);

  g_assert_nonnull (dock);

  syspath = mock_sysfs_device_get_syspath (tt->sysfs, dock);
  udevice = udev_device_new_from_syspath (tt->udev, syspath);
  g_assert_nonnull (udevice);

  /* for the dock */
  dd = bolt_sysfs_domain_for_device (udevice, &dh);
  g_assert_nonnull (dd);
  g_assert_nonnull (dh);

  g_assert_cmpstr (udev_device_get_syspath (dd),
                   ==,
                   mock_sysfs_domain_get_syspath (tt->sysfs, domain));

  g_assert_cmpstr (udev_device_get_syspath (dh),
                   ==,
                   mock_sysfs_device_get_syspath (tt->sysfs, host));

  /* for the host itself */
  dd = bolt_sysfs_domain_for_device (dh, &dh);
  g_assert_nonnull (dd);
  g_assert_nonnull (dh);

  g_assert_cmpstr (udev_device_get_syspath (dd),
                   ==,
                   mock_sysfs_domain_get_syspath (tt->sysfs, domain));

  g_assert_cmpstr (udev_device_get_syspath (dh),
                   ==,
                   mock_sysfs_device_get_syspath (tt->sysfs, host));

  mock_sysfs_domain_remove (tt->sysfs, domain);
}

148 149 150
static void
test_sysfs_domains (TestSysfs *tt, gconstpointer user)
{
151
  g_autoptr(GError) err = NULL;
152
  const char *uid = "884c6edd-7118-4b21-b186-b02d396ecca0";
153 154 155 156 157 158 159 160 161
  const char *ids[5];
  BoltSecurity sl[5] = {BOLT_SECURITY_NONE,
                        BOLT_SECURITY_DPONLY,
                        BOLT_SECURITY_USER,
                        BOLT_SECURITY_SECURE,
                        BOLT_SECURITY_USBONLY};
  BoltDomain *all[5] = {NULL, };
  BoltDomain *domains = NULL;
  BoltDomain *iter;
162 163 164 165 166 167
  int n;

  n = bolt_sysfs_count_domains (tt->udev, &err);

  g_assert_no_error (err);
  g_assert_cmpint (n, ==, 0);
168 169 170 171 172 173 174

  for (gsize i = 0; i < G_N_ELEMENTS (sl); i++)
    {
      g_autoptr(udev_device) udevice = NULL;
      g_autoptr(BoltDomain) dom = NULL; /* the list will own reference */
      const char *syspath;

175
      ids[i] = mock_sysfs_domain_add (tt->sysfs, sl[i], NULL);
176 177 178 179

      syspath = mock_sysfs_domain_get_syspath (tt->sysfs, ids[i]);
      udevice = udev_device_new_from_syspath (tt->udev, syspath);

180 181
      g_assert_nonnull (udevice);

182
      dom = bolt_domain_new_for_udev (udevice, uid, &err);
183 184 185 186
      g_assert_no_error (err);
      g_assert_nonnull (dom);

      g_assert_cmpstr (syspath, ==, bolt_domain_get_syspath (dom));
187
      g_assert_cmpstr (uid, ==, bolt_domain_get_uid (dom));
188 189 190 191 192 193 194 195 196 197 198 199
      g_assert_cmpstr (ids[i], ==, bolt_domain_get_id (dom));

      domains = bolt_domain_insert (domains, dom);
      all[i] = dom;
      g_object_add_weak_pointer (G_OBJECT (dom), (gpointer *) &all[i]);
    }

  g_assert_nonnull (domains);
  g_assert_cmpuint (bolt_domain_count (domains),
                    ==,
                    G_N_ELEMENTS (sl));

200 201 202 203
  g_assert_cmpint (bolt_sysfs_count_domains (tt->udev, NULL),
                   ==,
                   (int) G_N_ELEMENTS (sl));

204 205 206 207
  n = 0;
  bolt_domain_foreach (domains, count_domains, &n);
  g_assert_cmpint (n, ==, (int) G_N_ELEMENTS (sl));

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  iter = domains;
  for (gsize i = 0; i < bolt_domain_count (domains); i++)
    {
      const char *id = bolt_domain_get_id (iter);
      BoltSecurity s = bolt_domain_get_security (iter);

      g_assert_cmpstr (id, ==, ids[i]);
      g_assert_cmpint (s, ==, sl[i]);

      iter = bolt_domain_next (iter);
    }

  iter = bolt_domain_prev (domains);
  g_assert_cmpstr (bolt_domain_get_id (iter),
                   ==,
                   ids[G_N_ELEMENTS (sl) - 1]);

  /* removing of domains: start with the second one */
  iter = bolt_domain_next (domains);
  domains = bolt_domain_remove (domains, iter);

  g_assert_cmpuint (bolt_domain_count (domains),
                    ==,
                    G_N_ELEMENTS (sl) - 1);

  g_assert_cmpstr (bolt_domain_get_id (domains), ==, ids[0]);

  iter = bolt_domain_next (domains);
  /* ids[1] should be gone */
  g_assert_cmpstr (bolt_domain_get_id (iter), ==, ids[2]);

  /* remove of domains: the list head */
  domains = bolt_domain_remove (domains, domains);
  g_assert_cmpuint (bolt_domain_count (domains),
                    ==,
                    G_N_ELEMENTS (sl) - 2);
  /* the head is now ids[2], because 0, 1 got removed */
  g_assert_cmpstr (bolt_domain_get_id (domains), ==, ids[2]);

  /* remove of domains: clear the whole list */
248 249
  bolt_domain_clear (&domains);
  g_assert_null (domains);
250 251 252 253 254 255 256
  g_assert_cmpuint (bolt_domain_count (domains), ==, 0);

  /* check we also got rid of all references */
  for (gsize i = 0; i < G_N_ELEMENTS (all); i++)
    g_assert_null (all[i]);
}

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
typedef struct
{
  MockSysfs   *sysfs;
  struct udev *udev;

  GStrv        acl;
  guint        slots;

  const char  *dom_sysid;
  const char  *dom_uid;

  BoltDomain  *dom;
} TestBootacl;


272
static void
273
test_bootacl_setup (TestBootacl *tt, gconstpointer data)
274 275
{
  g_autoptr(GError) err = NULL;
276 277
  g_autoptr(udev_device) udevice = NULL;
  g_auto(GStrv) have = NULL;
278
  g_autofree char *str = NULL;
279
  const char *syspath;
280 281 282 283 284 285
  static const char *uid = "884c6edd-7118-4b21-b186-b02d396ecca0";

  tt->sysfs = mock_sysfs_new ();
  tt->udev = udev_new ();

  tt->slots = 16;
286

287 288
  str = g_strnfill (tt->slots - 1, ',');
  tt->acl = g_strsplit (str, ",", 1024);
289

290
  tt->dom_sysid = mock_sysfs_domain_add (tt->sysfs, BOLT_SECURITY_USER, tt->acl);
291

292
  syspath = mock_sysfs_domain_get_syspath (tt->sysfs, tt->dom_sysid);
293 294
  udevice = udev_device_new_from_syspath (tt->udev, syspath);

295 296
  tt->dom_uid = uid;
  tt->dom = bolt_domain_new_for_udev (udevice, tt->dom_uid, &err);
297
  g_assert_no_error (err);
298
  g_assert_nonnull (tt->dom);
299

300 301 302 303
  g_assert_cmpstr (bolt_domain_get_uid (tt->dom), ==, uid);

  g_assert_true (bolt_domain_supports_bootacl (tt->dom));
  g_object_get (tt->dom, "bootacl", &have, NULL);
304 305
  g_assert_nonnull (have);

306 307 308 309 310 311
  bolt_assert_strv_equal (have, tt->acl, -1);
}

static void
test_bootacl_tear_down (TestBootacl *tt, gconstpointer user)
{
312

313 314 315 316 317 318 319 320
  g_clear_pointer (&tt->acl, g_free);
  g_clear_object (&tt->dom);

  g_clear_object (&tt->sysfs);
  g_clear_pointer (&tt->udev, udev_unref);
}


321 322 323
static void
dump_strv (GStrv strv, const char *prefix)
{
324 325 326 327 328 329 330 331 332 333 334
  if (strv == NULL)
    {
      g_print ("%s is NULL\n", prefix);
      return;
    }
  else if (*strv == NULL)
    {
      g_print ("%s is EMPTY\n", prefix);
      return;
    }

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  for (guint i = 0; strv[i]; i++)
    g_print ("%s[%u] %s\n", prefix, i, strv[i]);
}

static void
test_bootacl_connect_domain (TestBootacl *tt, BoltDomain *dom)
{
  g_autoptr(udev_device) udevice = NULL;
  const char *syspath;

  syspath = mock_sysfs_domain_get_syspath (tt->sysfs, tt->dom_sysid);
  udevice = udev_device_new_from_syspath (tt->udev, syspath);
  bolt_domain_connected (dom, udevice);
}

static void
test_bootacl_read_acl (TestBootacl *tt, GStrv *acl)
{
  g_autoptr(GError) err = NULL;
  g_clear_pointer (acl, g_strfreev);

  *acl = mock_sysfs_domain_bootacl_get (tt->sysfs, tt->dom_sysid, &err);

  g_assert_no_error (err);
  g_assert_nonnull (*acl);
}

362 363 364 365 366 367 368 369 370 371 372
static void
test_bootacl_write_acl (TestBootacl *tt, GStrv acl)
{
  g_autoptr(GError) err = NULL;
  gboolean ok;

  ok = mock_sysfs_domain_bootacl_set (tt->sysfs, tt->dom_sysid, acl, &err);
  g_assert_no_error (err);
  g_assert_true (ok);
}

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
static void
test_bootacl_connect_and_verify (TestBootacl *tt,
                                 BoltDomain  *dom,
                                 GStrv       *acl)
{
  g_auto(GStrv) sysacl = NULL;
  GStrv have = NULL;

  test_bootacl_connect_domain (tt, dom);
  test_bootacl_read_acl (tt, &sysacl);

  dump_strv (sysacl, "sysacl ");
  dump_strv (tt->acl, "acl ");

  /* the domain and sysfs */
  have = bolt_domain_get_bootacl (dom);
  bolt_assert_strv_equal (have, sysacl, -1);

  /* the domain and what we expect */
  bolt_assert_strv_equal (have, tt->acl, -1);

  if (acl != NULL)
    bolt_swap (*acl, sysacl);
}

static void test_bootacl_add_uuid (TestBootacl *tt,
                                   BoltDomain  *dom,
                                   int          slot,
                                   const char  *uuidfmt,
                                   ...) G_GNUC_PRINTF (4, 5);

static void
test_bootacl_add_uuid (TestBootacl *tt,
                       BoltDomain  *dom,
                       int          slot,
                       const char  *uuidfmt,
                       ...)
{
  g_autoptr(GError) err = NULL;
  g_autofree char *uuid = NULL;
  GStrv acl = tt->acl;
  gboolean ok;
  va_list args;

  va_start (args, uuidfmt);

  uuid = g_strdup_vprintf (uuidfmt, args);
  ok = bolt_domain_bootacl_add (dom, uuid, &err);

  va_end (args);

  g_assert_no_error (err);
  g_assert_true (ok);

  g_assert_true (bolt_domain_bootacl_contains (dom, uuid));

  if (slot > -1)
    bolt_swap (acl[slot], uuid);
}

static void
test_bootacl_del_uuid (TestBootacl *tt,
                       BoltDomain  *dom,
                       const char  *uuid)
{
  g_autoptr(GError) err = NULL;
  GStrv acl = tt->acl;
  gboolean ok;
  char **p;

  ok = bolt_domain_bootacl_del (dom, uuid, &err);

  g_assert_no_error (err);
  g_assert_true (ok);

  g_assert_false (bolt_domain_bootacl_contains (dom, uuid));

  p = bolt_strv_contains (acl, uuid);
  if (p != NULL)
    bolt_set_strdup (p, "");
}

/* the bootacl related tests */

457 458 459
static void
test_bootacl_basic (TestBootacl *tt, gconstpointer user)
{
460
  g_auto(GStrv) sysacl = NULL;
461 462 463 464
  BoltDomain *dom = tt->dom;
  const char **used;
  guint slots = tt->slots;
  guint n, n_free, n_used;
465

466 467
  g_assert_true (bolt_domain_supports_bootacl (dom));

468 469 470 471 472 473 474
  n = bolt_domain_bootacl_slots (dom, &n_free);
  g_assert_cmpuint (n, ==, slots);
  g_assert_cmpuint (n_free, ==, slots);

  used = bolt_domain_bootacl_get_used (dom, &n_used);
  g_assert_cmpuint (g_strv_length ((GStrv) used), ==, 0);
  g_assert_cmpuint (n_used, ==, 0);
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

  /* disconnect and reconnect */
  bolt_domain_disconnected (dom);
  test_bootacl_connect_and_verify (tt, dom, &sysacl);

  /* simulate some pathological cases that should not
   * actually happen, but should still be handled
   */

  /* after we connect, the slot list is empty */
  bolt_domain_disconnected (dom);
  g_strfreev (tt->acl);
  tt->acl = g_strsplit ("", ",", -1);

  test_bootacl_write_acl (tt, tt->acl);

  test_bootacl_connect_and_verify (tt, dom, &sysacl);
  g_assert_false (bolt_domain_supports_bootacl (dom));

  n = bolt_domain_bootacl_slots (dom, &n_free);
  g_assert_cmpuint (n, ==, 0);
  g_assert_cmpuint (n_free, ==, 0);

  /* after we connect, the slot list changed */
  bolt_domain_disconnected (dom);
  g_strfreev (tt->acl);
  tt->acl = g_strsplit (",", ",", -1); /* two slots */

  test_bootacl_write_acl (tt, tt->acl);

  test_bootacl_connect_and_verify (tt, dom, &sysacl);
  g_assert_true (bolt_domain_supports_bootacl (dom));

  n = bolt_domain_bootacl_slots (dom, &n_free);
  g_assert_cmpuint (n, ==, 2);
  g_assert_cmpuint (n_free, ==, 2);
511 512
}

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
static void
test_bootacl_errors (TestBootacl *tt, gconstpointer user)
{
  g_autoptr(udev_device) udevice = NULL;
  g_autoptr(BoltDomain) dom2 = NULL;
  g_autoptr(GError) err = NULL;
  g_auto(GStrv) tmp = NULL;
  g_autofree char *str = NULL;
  BoltDomain *dom = tt->dom;
  const char *noacl_dom;
  const char *syspath;
  GStrv acl = tt->acl;
  gboolean ok;

  /* adding an existing uuid */
  test_bootacl_add_uuid (tt, dom, 0, "deadbab%x-0200-0100-ffff-ffffffffffff", 0U);
  ok = bolt_domain_bootacl_add (dom, acl[0], &err);
  g_assert_error (err, G_IO_ERROR, G_IO_ERROR_EXISTS);
  g_assert_false (ok);
  g_clear_pointer (&err, g_error_free);

  /* removing an unknown uuid */
  ok = bolt_domain_bootacl_del (dom, "deadbabe-0200-ffff-ffff-ffffffffffff", &err);
  g_assert_error (err, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  g_assert_false (ok);
  g_clear_pointer (&err, g_error_free);

  /* number of slots mismatch */
  str = g_strnfill (tt->slots * 2, ',');
  tmp = g_strsplit (str, ",", 1024);

  ok = bolt_domain_bootacl_set (dom, tmp, &err);
  g_assert_error (err, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
  g_assert_false (ok);
  g_clear_pointer (&err, g_error_free);

  /* domain without bootacl support */
  noacl_dom = mock_sysfs_domain_add (tt->sysfs, BOLT_SECURITY_SECURE, NULL);
  g_assert_nonnull (noacl_dom);

  syspath = mock_sysfs_domain_get_syspath (tt->sysfs, noacl_dom);
  udevice = udev_device_new_from_syspath (tt->udev, syspath);

  dom2 = bolt_domain_new_for_udev (udevice, tt->dom_uid, &err);
  g_assert_no_error (err);
  g_assert_nonnull (dom2);
  g_assert_false (bolt_domain_supports_bootacl (dom2));

  ok = bolt_domain_bootacl_add (dom2, acl[0], &err);
  g_assert_error (err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
  g_assert_false (ok);
  g_clear_pointer (&err, g_error_free);

  ok = bolt_domain_bootacl_del (dom2, acl[0], &err);
  g_assert_error (err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
  g_assert_false (ok);
  g_clear_pointer (&err, g_error_free);

  ok = bolt_domain_bootacl_set (dom2, tmp, &err);
  g_assert_error (err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
  g_assert_false (ok);
  g_clear_pointer (&err, g_error_free);

}

578 579 580 581 582 583 584 585 586 587
static void
on_bootacl_notify (GObject    *object,
                   GParamSpec *pspec,
                   gpointer    user_data)
{
  guint *n_signals = user_data;

  (*n_signals)++;
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
typedef struct
{
  gboolean    changed;
  GHashTable *changes;
  gboolean    fired;
} AclChangeSet;

static void
acl_change_set_verify (AclChangeSet *cs,
                       int           n_changes,
                       ...)
{
  guint sz;
  va_list ap;

  if (n_changes == -1)
    {
      g_assert_false (cs->fired);
      return;
    }

  g_assert_true (cs->fired);
  g_assert_true (cs->changed);

  sz = g_hash_table_size (cs->changes);
  g_assert_cmpint (sz, ==, n_changes);

  va_start (ap, n_changes);

  for (int i = 0; i < n_changes; i++)
    {
      const char *uuid = va_arg (ap, const char *);
      int op = va_arg (ap, int);
      char *key;

      g_assert_nonnull (uuid);
      key = g_hash_table_lookup (cs->changes, uuid);

      g_assert_nonnull (key);
      g_assert_cmpint (GPOINTER_TO_INT (key), ==, op);
    }

  va_end (ap);
}

static void
on_bootacl_changed (BoltDomain *dom,
                    gboolean    changed,
                    GHashTable *changes,
                    gpointer    user_data)
{
  AclChangeSet *changeset = user_data;

  g_clear_pointer (&changeset->changes, g_hash_table_unref);

  changeset->changed = changed;
  changeset->changes = g_hash_table_ref (changes);
  changeset->fired = TRUE;
}

648
static void
649
test_bootacl_update_udev (TestBootacl *tt, gconstpointer user)
650
{
651 652 653
  GStrv acl = tt->acl;
  BoltDomain *dom = tt->dom;
  guint slots = tt->slots;
654
  const char *syspath;
655
  guint n_free, n_used;
656
  guint n_signals = 0;
657
  AclChangeSet changeset = {FALSE, NULL, FALSE, };
658

659
  syspath = mock_sysfs_domain_get_syspath (tt->sysfs, tt->dom_sysid);
660

661
  /* fill in some uuids via sysfs */
662 663 664 665
  g_signal_connect (dom, "notify::bootacl",
                    G_CALLBACK (on_bootacl_notify),
                    &n_signals);

666 667 668 669
  g_signal_connect (dom, "bootacl-changed",
                    G_CALLBACK (on_bootacl_changed),
                    &changeset);

670 671 672 673 674 675 676 677 678 679 680 681
  for (guint i = 0; i < 8; i++)
    {
      g_autoptr(udev_device) ud = NULL;
      GStrv have = NULL;
      g_autofree const char **used = NULL;
      char *uuid = NULL;
      struct udev *udev;
      guint n;

      uuid = g_strdup_printf ("deadbab%x-0200-0100-ffff-ffffffffffff", i);
      bolt_set_str (&acl[i], uuid);

682
      test_bootacl_write_acl (tt, acl);
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

      udev = udev_new ();
      ud = udev_device_new_from_syspath (udev, syspath);
      bolt_domain_update_from_udev (dom, ud);

      g_assert_cmpuint (n_signals, ==, i + 1);
      g_assert_true (bolt_domain_bootacl_contains (dom, acl[i]));

      n = bolt_domain_bootacl_slots (dom, &n_free);
      g_assert_cmpuint (n, ==, slots);
      g_assert_cmpuint (n_free, ==, slots - (i + 1));

      used = bolt_domain_bootacl_get_used (dom, &n_used);
      g_assert_cmpuint (n_used, ==, i + 1);

      have = bolt_domain_get_bootacl (dom);
      bolt_assert_strv_equal (have, acl, -1);

701 702 703 704
      /* we verify the bootacl-changed signal */
      acl_change_set_verify (&changeset, 1, uuid, '+');
      changeset.fired = FALSE;

705 706 707 708
      udev_unref (udev);
    }
}

709 710 711 712 713 714 715 716 717
static void
test_bootacl_update_online (TestBootacl *tt, gconstpointer user)
{
  g_autoptr(GError) err = NULL;
  g_auto(GStrv) sysacl = NULL;
  GStrv acl = tt->acl;
  BoltDomain *dom = tt->dom;
  gboolean ok;
  guint slots;
718 719 720 721 722
  AclChangeSet changeset = {FALSE, NULL, FALSE, };

  g_signal_connect (dom, "bootacl-changed",
                    G_CALLBACK (on_bootacl_changed),
                    &changeset);
723 724 725

  for (guint i = 0; i < tt->slots; i++)
    {
726 727
      test_bootacl_add_uuid (tt, dom, i, "deadbab%x-0200-0100-ffff-ffffffffffff", i);
      test_bootacl_read_acl (tt, &sysacl);
728

729
      g_assert_nonnull (bolt_strv_contains (sysacl, acl[i]));
730

731
      acl_change_set_verify (&changeset, 1, acl[i], '+');
732 733
      changeset.fired = FALSE;

734
      bolt_assert_strv_equal (acl, sysacl, -1);
735 736 737
    }

  /* verify with what we have in mock sysfs */
738
  test_bootacl_read_acl (tt, &sysacl);
739
  slots = bolt_strv_length (acl);
740
  dump_strv (sysacl, "sysacl ");
741 742 743 744 745 746 747 748 749 750 751 752

  /* NB: acl was verified to be in sync with domain's acl */
  bolt_assert_strv_equal (acl, sysacl, -1);

  /* lets overwrite all the bootacl entries bit by bit
   *  and also verify we honor FIFO when replacing them
   */
  for (guint i = 0; i < tt->slots; i++)
    {
      g_auto(GStrv) have = NULL;

      /* NB: different uuid pattern from above (0200-0100) */
753 754
      test_bootacl_add_uuid (tt, dom, i, "deadbab%x-0200-aaaa-ffff-ffffffffffff", i);
      test_bootacl_read_acl (tt, &have);
755

756 757
      g_assert_nonnull (bolt_strv_contains (have, acl[i]));
      g_assert_cmpstr (have[slots - 1], ==, acl[i]);
758 759 760 761

      /* check the bootacl-changed signal emission:
       *  add for the new one, remove for the overwritten one
       */
762
      acl_change_set_verify (&changeset, 2, acl[i], '+', sysacl[i], '-');
763
      changeset.fired = FALSE;
764 765 766
    }

  /* remove all the entries */
767
  test_bootacl_read_acl (tt, &sysacl);
768 769
  slots = bolt_strv_length (acl);

770 771
  dump_strv (sysacl, "sysacl ");

772 773 774 775 776 777

  for (guint i = 0; i < slots; i++)
    {
      g_auto(GStrv) have = NULL;
      char *uuid = sysacl[i];

778
      changeset.fired = FALSE;
779

780 781
      test_bootacl_del_uuid (tt, dom, uuid);
      test_bootacl_read_acl (tt, &have);
782 783

      g_assert_null (bolt_strv_contains (have, uuid));
784 785 786 787

      /* check the bootacl-changed signal emission */
      acl_change_set_verify (&changeset, 1, uuid, '-');
      changeset.fired = FALSE;
788
    }
789 790 791

  /* now we set a bunch in one-go */
  for (guint i = 0; i < tt->slots; i++)
792
    bolt_set_strdup_printf (&acl[i], "deadbab%x-cccc-0100-ffff-ffffffffffff", i);
793

794
  changeset.fired = FALSE;
795 796 797 798
  ok = bolt_domain_bootacl_set (dom, acl, &err);
  g_assert_no_error (err);
  g_assert_true (ok);

799 800 801 802 803 804
  /* check we got removed signals for all of them */
  g_assert_true (changeset.fired);
  g_assert_true (changeset.changed);
  g_assert_cmpuint (g_hash_table_size (changeset.changes), ==, slots);
  changeset.fired = FALSE;

805 806
  dump_strv (sysacl, "sysacl ");
  test_bootacl_read_acl (tt, &sysacl);
807 808 809 810
  bolt_assert_strv_equal (acl, sysacl, -1);

  /* check that if we set the same bootacl as
   * we already have, we get FALSE but no error */
811
  changeset.fired = FALSE;
812 813 814
  ok = bolt_domain_bootacl_set (dom, acl, &err);
  g_assert_no_error (err);
  g_assert_false (ok);
815
  g_assert_false (changeset.fired);
816 817
}

818 819 820 821 822 823 824 825 826
static void
test_bootacl_update_offline (TestBootacl *tt, gconstpointer user)
{
  g_autoptr(GError) err = NULL;
  g_autoptr(BoltStore) store = NULL;
  g_auto(GStrv) sysacl = NULL;
  g_auto(BoltTmpDir) dir = NULL;
  BoltDomain *dom = tt->dom;
  gboolean ok;
827 828 829
  GStrv have;
  GStrv acl = tt->acl;
  guint k;
830 831 832 833 834 835 836 837 838 839

  dir = bolt_tmp_dir_make ("bolt.sysfs.XXXXXX", NULL);
  store = bolt_store_new (dir);

  ok = bolt_store_put_domain (store, dom, &err);
  g_assert_no_error (err);
  g_assert_true (ok);
  g_assert_true (bolt_domain_is_stored (dom));
  g_assert_true (bolt_domain_supports_bootacl (dom));

840 841
  /* 1. disconnect and add uuids that will get added to the journal */
  g_debug ("1. adding uuids offline");
842 843
  bolt_domain_disconnected (dom);

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
  for (guint i = 0; i < tt->slots / 2; i++)
    test_bootacl_add_uuid (tt, dom, i, "deadbab%x-0200-0100-ffff-ffffffffffff", i);

  have = bolt_domain_get_bootacl (dom);
  bolt_assert_strv_equal (have, acl, -1);

  /*   connect, and make sure we have sync */
  test_bootacl_connect_and_verify (tt, dom, &sysacl);

  /* 2. disconnect and remove uuids so they will end up in the journal */
  /*    remove the first quarter of uuids */
  g_debug ("2. remove uuids offline");
  bolt_domain_disconnected (dom);

  for (guint i = 0; i < tt->slots / 4; i++)
    test_bootacl_del_uuid (tt, dom, acl[i]);

  /*    simulate external changes: uuids added at the end */
  for (guint i = tt->slots / 2 + 1; i < tt->slots; i++)
    {
      bolt_set_strdup_printf (&sysacl[i], "deadbab%x-0200-0100-ffff-ffffffffffff", i);
      bolt_set_strdup_printf (&acl[i], "deadbab%x-0200-0100-ffff-ffffffffffff", i);
    }
  /*    write the external modifications */
868
  test_bootacl_write_acl (tt, sysacl);
869

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
  /*   connect, and make sure we have sync */
  test_bootacl_connect_and_verify (tt, dom, &sysacl);

  /* 3. simulate external modifications on top of journaled changes */
  g_debug ("3. external updates and offline changes");

  bolt_domain_disconnected (dom);
  dump_strv (bolt_domain_get_bootacl (dom), "domain ");

  /*    current state: [0,  N/4]: empty
   *                   [N/4,  N]: filled */
  test_bootacl_read_acl (tt, &sysacl);

  /*    [ 0 ] externally added and added in the journal (duplicated) */
  k = 0;
  bolt_set_strdup_printf (&sysacl[k], "deadbab%x-0200-0100-ffff-ffffffffffff", k);
  test_bootacl_add_uuid (tt, dom, k, "deadbab%x-0200-0100-ffff-ffffffffffff", k);

  /*    [ 1 ] added via the journal */
  k = 1;
  test_bootacl_add_uuid (tt, dom, k, "deadbab%x-0200-0100-ffff-ffffffffffff", k);
  bolt_set_strdup_printf (&acl[k], "deadbab%x-0200-0100-ffff-ffffffffffff", k);

  /*    [N/2+1] removed externally and via the journal */
  k = tt->slots / 2 + 1;
  bolt_set_strdup (&sysacl[k], "");
  test_bootacl_del_uuid (tt, dom, acl[k]);

  /*    [N/2+2] removed via the journal */
  k = tt->slots / 2 + 2;
  test_bootacl_del_uuid (tt, dom, acl[k]);

  /*    write the external modifications */
903
  test_bootacl_write_acl (tt, sysacl);
904 905 906 907 908 909 910 911

  /*   connect, and make sure we have sync */
  test_bootacl_connect_and_verify (tt, dom, &sysacl);

  /* 4. we pretend we got disconnected and reconnected with no change */
  g_debug ("4. no change reconnect");
  bolt_domain_disconnected (dom);
  test_bootacl_read_acl (tt, &sysacl);
912 913
}

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
static gboolean
bootacl_allocator (BoltDomain *domain,
                   GStrv       bootacl,
                   const char *uid,
                   gint       *slot,
                   gpointer    data)
{
  g_assert_nonnull (bootacl);
  g_assert_nonnull (uid);
  g_assert_nonnull (slot);
  g_assert_cmpint (*slot, >, -1);

  *slot = 0;
  return TRUE;
}

static void
test_bootacl_allocate (TestBootacl *tt, gconstpointer user)
{
  GStrv acl = tt->acl;
  BoltDomain *dom = tt->dom;

  g_signal_connect (dom, "bootacl-alloc",
                    G_CALLBACK (bootacl_allocator),
                    NULL);

  for (guint i = 0; i < tt->slots; i++)
    {
      g_auto(GStrv) have = NULL;
      GStrv domacl;

945 946
      test_bootacl_add_uuid (tt, dom, 0, "deadbab%x-0200-0100-ffff-ffffffffffff", i);
      test_bootacl_read_acl (tt, &have);
947

948
      g_assert_cmpstr (have[0], ==, acl[0]);
949 950 951 952 953 954 955
      bolt_assert_strv_equal (acl, have, -1);

      domacl = bolt_domain_get_bootacl (dom);
      bolt_assert_strv_equal (domacl, have, -1);
    }
}

956 957 958 959 960 961 962 963
int
main (int argc, char **argv)
{

  setlocale (LC_ALL, "");

  g_test_init (&argc, &argv, NULL);

964 965
  g_resources_register (bolt_daemon_get_resource ());

966 967 968 969 970 971 972
  g_test_add ("/sysfs/domain_for_device",
              TestSysfs,
              NULL,
              test_sysfs_setup,
              test_sysfs_domain_for_device,
              test_sysfs_tear_down);

973 974 975 976 977 978 979
  g_test_add ("/sysfs/domain/basic",
              TestSysfs,
              NULL,
              test_sysfs_setup,
              test_sysfs_domains,
              test_sysfs_tear_down);

980
  g_test_add ("/sysfs/domain/bootacl/basic",
981
              TestBootacl,
982
              NULL,
983 984 985
              test_bootacl_setup,
              test_bootacl_basic,
              test_bootacl_tear_down);
986

987 988 989 990 991 992 993
  g_test_add ("/sysfs/domain/bootacl/errors",
              TestBootacl,
              NULL,
              test_bootacl_setup,
              test_bootacl_errors,
              test_bootacl_tear_down);

994
  g_test_add ("/sysfs/domain/bootacl/update_udev",
995
              TestBootacl,
996
              NULL,
997 998 999
              test_bootacl_setup,
              test_bootacl_update_udev,
              test_bootacl_tear_down);
1000

1001 1002 1003 1004 1005 1006 1007
  g_test_add ("/sysfs/domain/bootacl/update_online",
              TestBootacl,
              NULL,
              test_bootacl_setup,
              test_bootacl_update_online,
              test_bootacl_tear_down);

1008 1009 1010 1011 1012 1013 1014
  g_test_add ("/sysfs/domain/bootacl/update_offline",
              TestBootacl,
              NULL,
              test_bootacl_setup,
              test_bootacl_update_offline,
              test_bootacl_tear_down);

1015 1016 1017 1018 1019 1020 1021
  g_test_add ("/sysfs/domain/bootacl/allocate",
              TestBootacl,
              NULL,
              test_bootacl_setup,
              test_bootacl_allocate,
              test_bootacl_tear_down);

1022 1023
  return g_test_run ();
}