summaryrefslogtreecommitdiff
path: root/src/platform/tests/test-nmp-object.c
blob: 85042f7d169e7418bb4cf544d8852fdbfb13063c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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
362
363
364
365
366
367
368
369
370
371
372
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
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
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
578
579
580
581
582
583
584
585
586
587
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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2015 - 2017 Red Hat, Inc.
 */

#include "nm-default.h"

#include <libudev.h>
#include <linux/pkt_sched.h>

#include "platform/nmp-object.h"
#include "nm-udev-aux/nm-udev-utils.h"

#include "nm-test-utils-core.h"

struct {
    GList *udev_devices;
} global;

/*****************************************************************************/

static void
test_obj_base(void)
{
    static const union {
        GObject   g;
        NMPObject k;
    } x = {};
    static const union {
        GTypeClass k;
        NMPClass   c;
    } l                        = {};
    static const GObject *   g = &x.g;
    static const GTypeClass *k = &l.k;
    static const NMPObject * o = &x.k;
    static const NMPClass *  c = &l.c;

    NMObjBaseInst * obj;
    gs_unref_object GCancellable *obj_cancellable = g_cancellable_new();
    nm_auto_nmpobj NMPObject *obj_link            = nmp_object_new_link(10);

    g_assert(&g->g_type_instance == (void *) &o->_class);
    g_assert(&g->g_type_instance.g_class == (void *) &o->_class);

    g_assert(sizeof(o->parent.parent) == sizeof(GTypeInstance));

    g_assert(&c->parent == (void *) c);
    g_assert(&c->parent.parent.g_type_class == (void *) c);
    g_assert(&c->parent.parent.g_type == (void *) c);
    g_assert(&c->parent.parent.g_type == &k->g_type);

    g_assert(sizeof(c->parent.parent) == sizeof(GTypeClass));

    g_assert(&o->parent == (void *) o);
    g_assert(&o->parent.klass == (void *) &o->_class);

    obj = (NMObjBaseInst *) obj_cancellable;
    g_assert(!NMP_CLASS_IS_VALID((NMPClass *) obj->klass));
    g_assert(G_TYPE_CHECK_INSTANCE_TYPE(obj, G_TYPE_CANCELLABLE));

    obj = (NMObjBaseInst *) obj_link;
    g_assert(NMP_CLASS_IS_VALID((NMPClass *) obj->klass));
    g_assert(!G_TYPE_CHECK_INSTANCE_TYPE(obj, G_TYPE_CANCELLABLE));
}

/*****************************************************************************/

static gboolean
_nmp_object_id_equal(const NMPObject *a, const NMPObject *b)
{
    gboolean a_b = nmp_object_id_equal(a, b);

    g_assert(NM_IN_SET(a_b, FALSE, TRUE) && a_b == nmp_object_id_equal(b, a));
    return a_b;
}
#define nmp_object_id_equal _nmp_object_id_equal

static gboolean
_nmp_object_equal(const NMPObject *a, const NMPObject *b)
{
    gboolean a_b = nmp_object_equal(a, b);

    g_assert(NM_IN_SET(a_b, FALSE, TRUE) && a_b == nmp_object_equal(b, a));
    return a_b;
}
#define nmp_object_equal _nmp_object_equal

/*****************************************************************************/

static void
_assert_cache_multi_lookup_contains(const NMPCache *             cache,
                                    const NMDedupMultiHeadEntry *head_entry,
                                    const NMPObject *            obj,
                                    gboolean                     visible_only,
                                    gboolean                     contains)
{
    NMDedupMultiIter iter;
    gboolean         found;
    guint            i, len;
    const NMPObject *o;

    g_assert(NMP_OBJECT_IS_VALID(obj));

    g_assert(nmp_cache_lookup_obj(cache, obj) == obj);
    g_assert(!head_entry
             || (head_entry->len > 0
                 && c_list_length(&head_entry->lst_entries_head) == head_entry->len));

    len = head_entry ? head_entry->len : 0;

    found = FALSE;
    i     = 0;
    nmp_cache_iter_for_each (&iter, head_entry, &o) {
        g_assert(NMP_OBJECT_IS_VALID(o));
        if (obj == o) {
            if (!visible_only || nmp_object_is_visible(o)) {
                g_assert(!found);
                found = TRUE;
            }
        }
        i++;
    }

    g_assert(len == i);
    g_assert(!!contains == found);
}

static void
_assert_cache_multi_lookup_contains_link(const NMPCache * cache,
                                         gboolean         visible_only,
                                         const NMPObject *obj,
                                         gboolean         contains)
{
    const NMDedupMultiHeadEntry *head_entry;
    NMPLookup                    lookup;

    g_assert(cache);

    nmp_lookup_init_obj_type(&lookup, NMP_OBJECT_TYPE_LINK);
    head_entry = nmp_cache_lookup(cache, &lookup);
    _assert_cache_multi_lookup_contains(cache, head_entry, obj, visible_only, contains);
}

/*****************************************************************************/

static void
ops_post_check(NMPCache *       cache,
               NMPCacheOpsType  ops_type,
               const NMPObject *obj_old,
               const NMPObject *obj_new,
               const NMPObject *obj_new_expected,
               NMPCacheOpsType  expected_ops_type)
{
    g_assert(cache);

    g_assert_cmpint(expected_ops_type, ==, ops_type);

    switch (ops_type) {
    case NMP_CACHE_OPS_ADDED:
        g_assert(!obj_old);
        g_assert(NMP_OBJECT_IS_VALID(obj_new));
        g_assert(nmp_object_is_alive(obj_new));
        g_assert(nmp_object_id_equal(obj_new_expected, obj_new));
        g_assert(nmp_object_equal(obj_new_expected, obj_new));
        break;
    case NMP_CACHE_OPS_UPDATED:
        g_assert(obj_old != obj_new);
        g_assert(NMP_OBJECT_IS_VALID(obj_old));
        g_assert(NMP_OBJECT_IS_VALID(obj_new));
        g_assert(nmp_object_is_alive(obj_old));
        g_assert(nmp_object_is_alive(obj_new));
        g_assert(nmp_object_id_equal(obj_new_expected, obj_new));
        g_assert(nmp_object_id_equal(obj_new_expected, obj_old));
        g_assert(nmp_object_id_equal(obj_old, obj_new));
        g_assert(nmp_object_equal(obj_new_expected, obj_new));
        g_assert(!nmp_object_equal(obj_new_expected, obj_old));
        g_assert(!nmp_object_equal(obj_old, obj_new));
        break;
    case NMP_CACHE_OPS_REMOVED:
        g_assert(!obj_new);
        g_assert(NMP_OBJECT_IS_VALID(obj_old));
        g_assert(nmp_object_is_alive(obj_old));
        if (obj_new_expected)
            g_assert(nmp_object_id_equal(obj_new_expected, obj_old));
        break;
    case NMP_CACHE_OPS_UNCHANGED:
        g_assert(obj_old == obj_new);
        if (obj_old) {
            g_assert(NMP_OBJECT_IS_VALID(obj_old));
            g_assert(nmp_object_is_alive(obj_old));
            g_assert(nmp_object_equal(obj_old, obj_new));
            g_assert(nmp_object_id_equal(obj_new_expected, obj_new));
        } else
            g_assert(!obj_new_expected);
        break;
    default:
        g_assert_not_reached();
    }
}

static void
_nmp_cache_update_netlink(NMPCache *        cache,
                          NMPObject *       obj,
                          const NMPObject **out_obj_old,
                          const NMPObject **out_obj_new,
                          NMPCacheOpsType   expected_ops_type)
{
    NMPCacheOpsType  ops_type;
    const NMPObject *obj_prev;
    const NMPObject *obj_old;
    const NMPObject *obj_new;
    nm_auto_nmpobj NMPObject *obj_new_expected = NULL;

    g_assert(cache);
    g_assert(NMP_OBJECT_IS_VALID(obj));

    obj_prev         = nmp_cache_lookup_link(cache, NMP_OBJECT_CAST_LINK(obj)->ifindex);
    obj_new_expected = nmp_object_clone(obj, FALSE);
    if (obj_prev && obj_prev->_link.udev.device)
        obj_new_expected->_link.udev.device = udev_device_ref(obj_prev->_link.udev.device);
    _nmp_object_fixup_link_udev_fields(&obj_new_expected, NULL, nmp_cache_use_udev_get(cache));

    ops_type = nmp_cache_update_netlink(cache, obj, FALSE, &obj_old, &obj_new);
    ops_post_check(cache,
                   ops_type,
                   obj_old,
                   obj_new,
                   nmp_object_is_alive(obj_new_expected) ? obj_new_expected : NULL,
                   expected_ops_type);

    if (out_obj_new)
        *out_obj_new = obj_new;
    else
        nmp_object_unref(obj_new);
    if (out_obj_old)
        *out_obj_old = obj_old;
    else
        nmp_object_unref(obj_old);
}

static const NMPlatformLink pl_link_2 = {
    .ifindex = 2,
    .name    = "eth0",
    .type    = NM_LINK_TYPE_ETHERNET,
};

static const NMPlatformLink pl_link_3 = {
    .ifindex = 3,
    .name    = "wlan0",
    .type    = NM_LINK_TYPE_WIFI,
};

static void
test_cache_link(void)
{
    NMPCache *                      cache;
    NMPObject *                     objm1;
    const NMPObject *               obj_old, *obj_new;
    NMPObject                       objs1;
    struct udev_device *            udev_device_2 = g_list_nth_data(global.udev_devices, 0);
    struct udev_device *            udev_device_3 = g_list_nth_data(global.udev_devices, 0);
    NMPCacheOpsType                 ops_type;
    nm_auto_unref_dedup_multi_index NMDedupMultiIndex *multi_idx = NULL;
    gboolean                                           use_udev  = nmtst_get_rand_uint32() % 2;

    multi_idx = nm_dedup_multi_index_new();

    cache = nmp_cache_new(multi_idx, use_udev);

    /* if we have a link, and don't set is_in_netlink, adding it has no effect. */
    objm1 = nmp_object_new(NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
    g_assert(NMP_OBJECT_UP_CAST(&objm1->object) == objm1);
    g_assert(!nmp_object_is_alive(objm1));
    _nmp_cache_update_netlink(cache, objm1, &obj_old, &obj_new, NMP_CACHE_OPS_UNCHANGED);
    ASSERT_nmp_cache_is_consistent(cache);
    g_assert(!obj_old);
    g_assert(!obj_new);
    g_assert(!nmp_cache_lookup_obj(cache, objm1));
    g_assert(!nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_2.ifindex)));
    nmp_object_unref(objm1);

    /* Only when setting @is_in_netlink the link is added. */
    objm1 = nmp_object_new(NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
    objm1->_link.netlink.is_in_netlink = TRUE;
    g_assert(nmp_object_is_alive(objm1));
    _nmp_cache_update_netlink(cache, objm1, &obj_old, &obj_new, NMP_CACHE_OPS_ADDED);
    ASSERT_nmp_cache_is_consistent(cache);
    g_assert(!obj_old);
    g_assert(obj_new);
    g_assert(objm1 == obj_new);
    g_assert(nmp_object_equal(objm1, obj_new));
    g_assert(nmp_cache_lookup_obj(cache, objm1) == obj_new);
    g_assert(nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_2.ifindex))
             == obj_new);
    g_assert(nmp_object_is_visible(obj_new));
    _assert_cache_multi_lookup_contains_link(cache, FALSE, obj_new, TRUE);
    _assert_cache_multi_lookup_contains_link(cache, TRUE, obj_new, TRUE);
    nmp_object_unref(objm1);
    nmp_object_unref(obj_new);

    /* updating the same link with identical value, has no effect. */
    objm1 = nmp_object_new(NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
    objm1->_link.netlink.is_in_netlink = TRUE;
    g_assert(nmp_object_is_alive(objm1));
    _nmp_cache_update_netlink(cache, objm1, &obj_old, &obj_new, NMP_CACHE_OPS_UNCHANGED);
    ASSERT_nmp_cache_is_consistent(cache);
    g_assert(obj_old);
    g_assert(obj_new);
    g_assert(obj_new != objm1);
    g_assert(nmp_object_equal(objm1, obj_new));
    g_assert(nmp_cache_lookup_obj(cache, objm1) == obj_new);
    g_assert(nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_2.ifindex))
             == obj_new);
    nmp_object_unref(objm1);
    nmp_object_unref(obj_new);
    nmp_object_unref(obj_new);

    /* remove the link from netlink */
    objm1 = nmp_object_new(NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
    g_assert(!nmp_object_is_alive(objm1));
    _nmp_cache_update_netlink(cache, objm1, &obj_old, &obj_new, NMP_CACHE_OPS_REMOVED);
    ASSERT_nmp_cache_is_consistent(cache);
    g_assert(obj_old);
    g_assert(!obj_new);
    g_assert(!nmp_cache_lookup_obj(cache, objm1));
    g_assert(!nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_2.ifindex)));
    nmp_object_unref(objm1);
    nmp_object_unref(obj_old);
    nmp_object_unref(obj_new);

    if (udev_device_2) {
        /* now add the link only with aspect UDEV. */
        ops_type =
            nmp_cache_update_link_udev(cache, pl_link_2.ifindex, udev_device_2, &obj_old, &obj_new);
        ASSERT_nmp_cache_is_consistent(cache);
        g_assert_cmpint(ops_type, ==, NMP_CACHE_OPS_ADDED);
        g_assert(!obj_old);
        g_assert(obj_new);
        g_assert(
            nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_2.ifindex))
            == obj_new);
        g_assert(!nmp_object_is_visible(obj_new));
        _assert_cache_multi_lookup_contains_link(cache, TRUE, obj_new, FALSE);
        _assert_cache_multi_lookup_contains_link(cache, FALSE, obj_new, TRUE);
        nmp_object_unref(obj_new);
    }

    /* add it in netlink too. */
    objm1 = nmp_object_new(NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
    objm1->_link.netlink.is_in_netlink = TRUE;
    g_assert(nmp_object_is_alive(objm1));
    _nmp_cache_update_netlink(cache,
                              objm1,
                              &obj_old,
                              &obj_new,
                              udev_device_2 ? NMP_CACHE_OPS_UPDATED : NMP_CACHE_OPS_ADDED);
    ASSERT_nmp_cache_is_consistent(cache);
    if (udev_device_2) {
        g_assert(obj_old);
        g_assert(!nmp_object_is_visible(obj_old));
    } else
        g_assert(!obj_old);
    g_assert(nmp_object_equal(objm1, obj_new));
    g_assert(nmp_cache_lookup_obj(cache, objm1) == obj_new);
    g_assert(nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_2.ifindex))
             == obj_new);
    g_assert(nmp_object_is_visible(obj_new));
    _assert_cache_multi_lookup_contains_link(cache, TRUE, obj_new, TRUE);
    _assert_cache_multi_lookup_contains_link(cache, FALSE, obj_new, TRUE);
    nmp_object_unref(objm1);
    nmp_object_unref(obj_old);
    nmp_object_unref(obj_new);

    /* remove again from netlink. */
    objm1 = nmp_object_new(NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
    objm1->_link.netlink.is_in_netlink = FALSE;
    g_assert(!nmp_object_is_alive(objm1));
    _nmp_cache_update_netlink(cache,
                              objm1,
                              &obj_old,
                              &obj_new,
                              udev_device_2 ? NMP_CACHE_OPS_UPDATED : NMP_CACHE_OPS_REMOVED);
    ASSERT_nmp_cache_is_consistent(cache);
    if (udev_device_2)
        g_assert(obj_new == objm1);
    else
        g_assert(!obj_new);
    g_assert(obj_old);
    g_assert(nmp_object_is_alive(obj_old));
    if (udev_device_2) {
        g_assert(nmp_cache_lookup_obj(cache, objm1) == obj_new);
        g_assert(
            nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_2.ifindex))
            == obj_new);
        g_assert(!nmp_object_is_visible(obj_new));
        _assert_cache_multi_lookup_contains_link(cache, TRUE, obj_new, FALSE);
        _assert_cache_multi_lookup_contains_link(cache, FALSE, obj_new, TRUE);
    } else {
        g_assert(nmp_cache_lookup_obj(cache, objm1) == NULL);
        g_assert(
            nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_2.ifindex))
            == NULL);
        g_assert(!nmp_object_is_alive(obj_new));
        g_assert(!nmp_object_is_visible(obj_new));
    }
    nmp_object_unref(objm1);
    nmp_object_unref(obj_old);
    nmp_object_unref(obj_new);

    /* now another link only with aspect UDEV. */
    if (udev_device_3) {
        /* now add the link only with aspect UDEV. */
        ops_type =
            nmp_cache_update_link_udev(cache, pl_link_3.ifindex, udev_device_3, &obj_old, &obj_new);
        g_assert_cmpint(ops_type, ==, NMP_CACHE_OPS_ADDED);
        ASSERT_nmp_cache_is_consistent(cache);
        g_assert(NMP_OBJECT_IS_VALID(obj_new));
        g_assert(!obj_old);
        g_assert(!nmp_object_is_visible(obj_new));
        g_assert(
            nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_3.ifindex))
            == obj_new);
        _assert_cache_multi_lookup_contains_link(cache, TRUE, obj_new, FALSE);
        _assert_cache_multi_lookup_contains_link(cache, FALSE, obj_new, TRUE);
        g_assert_cmpint(obj_new->_link.netlink.is_in_netlink, ==, FALSE);
        g_assert_cmpint(obj_new->link.initialized, ==, FALSE);
        nmp_object_unref(obj_new);

        /* add it in netlink too. */
        objm1 = nmp_object_new(NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_3);
        objm1->_link.netlink.is_in_netlink = TRUE;
        g_assert(nmp_object_is_alive(objm1));
        _nmp_cache_update_netlink(cache, objm1, &obj_old, &obj_new, NMP_CACHE_OPS_UPDATED);
        ASSERT_nmp_cache_is_consistent(cache);
        g_assert(obj_old);
        g_assert(obj_new == objm1);
        g_assert(nmp_object_equal(objm1, obj_new));
        g_assert(!obj_old || !nmp_object_is_visible(obj_old));
        g_assert(nmp_cache_lookup_obj(cache, objm1) == obj_new);
        g_assert(
            nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_3.ifindex))
            == obj_new);
        g_assert(nmp_object_is_visible(obj_new));
        _assert_cache_multi_lookup_contains_link(cache, TRUE, obj_new, TRUE);
        _assert_cache_multi_lookup_contains_link(cache, FALSE, obj_new, TRUE);
        g_assert_cmpint(obj_new->_link.netlink.is_in_netlink, ==, TRUE);
        g_assert_cmpint(obj_new->link.initialized, ==, TRUE);
        nmp_object_unref(objm1);
        nmp_object_unref(obj_old);
        nmp_object_unref(obj_new);

        /* remove UDEV. */
        ops_type = nmp_cache_update_link_udev(cache, pl_link_3.ifindex, NULL, &obj_old, &obj_new);
        g_assert_cmpint(ops_type, ==, NMP_CACHE_OPS_UPDATED);
        ASSERT_nmp_cache_is_consistent(cache);
        g_assert(obj_old && nmp_object_is_visible(obj_old));
        g_assert(
            nmp_cache_lookup_obj(cache, nmp_object_stackinit_id_link(&objs1, pl_link_3.ifindex))
            == obj_new);
        g_assert(nmp_object_is_visible(obj_new));
        _assert_cache_multi_lookup_contains_link(cache, TRUE, obj_new, TRUE);
        _assert_cache_multi_lookup_contains_link(cache, FALSE, obj_new, TRUE);
        g_assert_cmpint(obj_new->_link.netlink.is_in_netlink, ==, TRUE);
        g_assert_cmpint(obj_new->link.initialized, ==, !nmp_cache_use_udev_get(cache));
        nmp_object_unref(obj_new);
        nmp_object_unref(obj_old);
    }

    nmp_cache_free(cache);
}

const char noqueue[]  = "noqueue";
const char fq_codel[] = "fq_codel";
const char ingress[]  = "ingress";

static const NMPlatformQdisc pl_qdisc_1a = {
    .ifindex     = 1,
    .kind        = noqueue,
    .addr_family = AF_UNSPEC,
    .handle      = 0,
    .parent      = TC_H_ROOT,
    .info        = 0,
};

static const NMPlatformQdisc pl_qdisc_1b = {
    .ifindex     = 1,
    .kind        = fq_codel,
    .addr_family = AF_UNSPEC,
    .handle      = 0,
    .parent      = TC_H_ROOT,
    .info        = 0,
};

static const NMPlatformQdisc pl_qdisc_1c = {
    .ifindex     = 1,
    .kind        = ingress,
    .addr_family = AF_UNSPEC,
    .handle      = TC_H_MAKE(TC_H_INGRESS, 0),
    .parent      = TC_H_INGRESS,
    .info        = 0,
};

static const NMPlatformQdisc pl_qdisc_2 = {
    .ifindex     = 2,
    .kind        = fq_codel,
    .addr_family = AF_UNSPEC,
    .handle      = 0,
    .parent      = TC_H_ROOT,
    .info        = 0,
};

static void
test_cache_qdisc(void)
{
    NMPCache *                      cache;
    nm_auto_unref_dedup_multi_index NMDedupMultiIndex *multi_idx = NULL;
    NMPLookup                                          lookup;
    const NMDedupMultiHeadEntry *                      head_entry;
    nm_auto_nmpobj NMPObject *obj1a =
        nmp_object_new(NMP_OBJECT_TYPE_QDISC, (NMPlatformObject *) &pl_qdisc_1a);
    nm_auto_nmpobj NMPObject *obj1b =
        nmp_object_new(NMP_OBJECT_TYPE_QDISC, (NMPlatformObject *) &pl_qdisc_1b);
    nm_auto_nmpobj NMPObject *obj1c =
        nmp_object_new(NMP_OBJECT_TYPE_QDISC, (NMPlatformObject *) &pl_qdisc_1c);
    nm_auto_nmpobj NMPObject *obj2 =
        nmp_object_new(NMP_OBJECT_TYPE_QDISC, (NMPlatformObject *) &pl_qdisc_2);

    multi_idx = nm_dedup_multi_index_new();
    cache     = nmp_cache_new(multi_idx, nmtst_get_rand_uint32() % 2);

    g_assert(nmp_cache_lookup_obj(cache, obj1a) == NULL);

    g_assert(nmp_cache_update_netlink(cache, obj1a, FALSE, NULL, NULL) == NMP_CACHE_OPS_ADDED);
    g_assert(nmp_cache_lookup_obj(cache, obj1a) == obj1a);
    g_assert(nmp_cache_lookup_obj(cache, obj1b) == obj1a);
    g_assert(nmp_cache_lookup_obj(cache, obj2) == NULL);

    g_assert(nmp_cache_update_netlink(cache, obj1b, FALSE, NULL, NULL) == NMP_CACHE_OPS_UPDATED);
    g_assert(nmp_cache_lookup_obj(cache, obj1a) == obj1b);
    g_assert(nmp_cache_lookup_obj(cache, obj1b) == obj1b);
    g_assert(nmp_cache_lookup_obj(cache, obj2) == NULL);

    g_assert(nmp_cache_update_netlink(cache, obj1c, FALSE, NULL, NULL) == NMP_CACHE_OPS_ADDED);
    g_assert(nmp_cache_lookup_obj(cache, obj1a) == obj1b);
    g_assert(nmp_cache_lookup_obj(cache, obj1b) == obj1b);
    g_assert(nmp_cache_lookup_obj(cache, obj1c) == obj1c);
    g_assert(nmp_cache_lookup_obj(cache, obj2) == NULL);

    g_assert(nmp_cache_update_netlink(cache, obj2, FALSE, NULL, NULL) == NMP_CACHE_OPS_ADDED);
    g_assert(nmp_cache_lookup_obj(cache, obj1a) == obj1b);
    g_assert(nmp_cache_lookup_obj(cache, obj1b) == obj1b);
    g_assert(nmp_cache_lookup_obj(cache, obj2) == obj2);

    head_entry = nmp_cache_lookup(cache, nmp_lookup_init_object(&lookup, NMP_OBJECT_TYPE_QDISC, 1));
    g_assert(head_entry->len == 2);

    nmp_cache_free(cache);
}

/*****************************************************************************/

NMTST_DEFINE();

int
main(int argc, char **argv)
{
    int           result;
    NMUdevClient *udev_client;

    nmtst_init_assert_logging(&argc, &argv, "INFO", "DEFAULT");

    udev_client = nm_udev_client_new((const char *[]){"net", NULL}, NULL, NULL);
    {
        struct udev_enumerate * enumerator;
        struct udev_list_entry *devices, *l;

        enumerator = nm_udev_client_enumerate_new(udev_client);

        /* Demand that the device is initialized (udev rules ran,
		 * device has a stable name now) in case udev is running
		 * (not in a container). */
        if (access("/sys", W_OK) == 0)
            udev_enumerate_add_match_is_initialized(enumerator);

        udev_enumerate_scan_devices(enumerator);

        devices = udev_enumerate_get_list_entry(enumerator);
        for (l = devices; l != NULL; l = udev_list_entry_get_next(l)) {
            struct udev_device *udevice;

            udevice = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerator),
                                                   udev_list_entry_get_name(l));
            if (udevice == NULL)
                continue;

            global.udev_devices = g_list_prepend(global.udev_devices, udevice);
        }
        global.udev_devices = g_list_reverse(global.udev_devices);

        udev_enumerate_unref(enumerator);
    }

    g_test_add_func("/nmp-object/obj-base", test_obj_base);
    g_test_add_func("/nmp-object/cache_link", test_cache_link);
    g_test_add_func("/nmp-object/cache_qdisc", test_cache_qdisc);

    result = g_test_run();

    while (global.udev_devices) {
        udev_device_unref(global.udev_devices->data);
        global.udev_devices = g_list_delete_link(global.udev_devices, global.udev_devices);
    }

    nm_udev_client_unref(udev_client);

    return result;
}