File: test-commands.c

package info (click to toggle)
mptcpd 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,612 kB
  • sloc: ansic: 9,472; sh: 5,154; makefile: 467; cpp: 61
file content (974 lines) | stat: -rw-r--r-- 31,506 bytes parent folder | download
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
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
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
903
904
905
906
907
908
909
910
911
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
// SPDX-License-Identifier: BSD-3-Clause
/**
 * @file test-commands.c
 *
 * @brief mptcpd commands API test.
 *
 * Copyright (c) 2019-2022, Intel Corporation
 */

#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <net/if.h>

#include <ell/ell.h>

// Internal Headers
// -----------------
#include <mptcpd/private/configuration.h>
#include "../src/path_manager.h"
#include "../src/commands.h"
// -----------------

#include "test-plugin.h"
#include "test-util.h"

#include <mptcpd/path_manager.h>
#include <mptcpd/addr_info.h>
#include <mptcpd/id_manager.h>

#undef NDEBUG
#include <assert.h>

// -------------------------------------------------------------------

struct test_addr_info
{
        // Test address.
        struct sockaddr *const addr;

        // Network interface on which to bind the test address.
        int const ifindex;

        // CIDR prefix length.
        uint8_t const prefix_len;

        /**
         * @brief String form of the addr.
         *
         * @note Long enough for both IPv4 and IPv6 addresses.
         */
        char ip[INET6_ADDRSTRLEN];

        /// MPTCP connection used in user space PM calls.
        mptcpd_token_t token;

        // MPTCP address ID used for add_addr and dump_addr calls.
        mptcpd_aid_t id;

        // MPTCP address flags used in the endpoints
        mptcpd_flags_t const flags;
};

struct test_info
{
        struct l_netlink *const rtnl;

        /*
          Address information for user space add_addr and remove_addr
          calls.
        */
        struct test_addr_info u_addr;

        /*
          Address information for kernel add_addr and dump_addr
          calls.
        */
        struct test_addr_info k_addr;

        // Mptcpd configuration.
        struct mptcpd_config *config;

        // Mptcpd path manager object.
        struct mptcpd_pm *pm;

        // Number of times dump_addrs call was completed.
        int dump_addrs_complete_count;

        // Were the tests called?
        bool tests_called;
};

// -------------------------------------------------------------------
/*
  Number of addresses to set up for test purposes (2, user space and
  kernel space).
*/
static int const addrs_to_setup_count = 2;

// Number of addresses set up for test purposes.
static int addr_setup_count;

// -------------------------------------------------------------------
// Addresses used for user space PM subflow command tests.
// -------------------------------------------------------------------

static struct sockaddr const *const laddr2 =
        (struct sockaddr const *) &test_laddr_2;

static struct sockaddr const *const raddr2 =
        (struct sockaddr const *) &test_raddr_2;

// -------------------------------------------------------------------

// MPTCP resource limits at test start.
static uint32_t old_max_addrs;
static uint32_t old_max_subflows;

// MPTCP resource limits set by this test.
static uint32_t max_addrs;
static uint32_t max_subflows;

// MPTCP resource limit offsets applied to the old ones.
static uint32_t const absolute_max_limit = 8;  // MPTCP_PM_ADDR_MAX
static uint32_t const max_addrs_offset = 3;
static uint32_t const max_subflows_offset = 5;

// -------------------------------------------------------------------

static void const *get_in_addr(struct sockaddr const *sa)
{
        if (sa->sa_family == AF_INET) {
                struct sockaddr_in const *const addr =
                        (struct sockaddr_in const *) sa;

                return &addr->sin_addr;
        } else if (sa->sa_family == AF_INET6) {
                struct sockaddr_in6 const *const addr =
                        (struct sockaddr_in6 const *) sa;

                return &addr->sin6_addr;
        }

        return NULL;  // Not an internet address. Unlikely.
}

static void dump_addr(char const *description, struct sockaddr const *a)
{
        assert(a != NULL);

        void const *src = get_in_addr(a);

        char addrstr[INET6_ADDRSTRLEN];  // Long enough for both IPv4
                                         // and IPv6 addresses.

        assert(inet_ntop(a->sa_family, src, addrstr, sizeof(addrstr)));

        in_port_t const port = mptcpd_get_port_number(a);

        l_info("%s: %s:<0x%x (%u)>",
               description,
               addrstr,
               port,
               port);
}

static void get_addr_callback(struct mptcpd_addr_info const *info,
                              void *user_data)
{
        struct test_info const *const tinfo = user_data;
        struct test_addr_info const *const k_addr = &tinfo->k_addr;

        /**
         * @bug We could have a resource leak in the kernel here if
         *      the below assert()s are triggered since addresses
         *      previously added through @c mptcpd_kpm_add_addr()
         *      would end up not being removed prior to test exit.
         */
        assert(info != NULL);

        l_info("=======================");
        dump_addr("Address   to mptcpd_kpm_add_addr()",
                  k_addr->addr);
        dump_addr("Address from mptcpd_kpm_get_addr()",
                  mptcpd_addr_info_get_addr(info));
        l_info("=======================");

        assert(mptcpd_addr_info_get_id(info) == k_addr->id);
        assert(mptcpd_addr_info_get_index(info) == k_addr->ifindex);
        assert(mptcpd_addr_info_get_flags(info) == k_addr->flags);
        assert(sockaddr_is_equal(k_addr->addr,
                                 mptcpd_addr_info_get_addr(info)));
}

static void dump_addrs_callback(struct mptcpd_addr_info const *info,
                                void *user_data)
{
        struct test_info const *const tinfo = user_data;

        /**
         * @bug We could have a resource leak in the kernel here if
         *      the below assert()s are triggered since addresses
         *      previously added through @c mptcpd_kpm_add_addr()
         *      would end up not being removed prior to test exit.
         */
        assert(info != NULL);

        struct test_addr_info const *const k_addr = &tinfo->k_addr;

        // Other IDs unrelated to this test could already exist.
        if (mptcpd_addr_info_get_id(info) != k_addr->id)
                return;

        assert(mptcpd_addr_info_get_index(info) == k_addr->ifindex);
        assert(mptcpd_addr_info_get_flags(info) == k_addr->flags);
        assert(sockaddr_is_equal(k_addr->addr,
                                 mptcpd_addr_info_get_addr(info)));
}

static void dump_addrs_complete(void *user_data)
{
        struct test_info *const info = user_data;

        info->dump_addrs_complete_count++;
}

static void reset_old_limits(struct mptcpd_pm *pm)
{
        struct mptcpd_limit const limits[] = {
                {
                        .type  = MPTCPD_LIMIT_RCV_ADD_ADDRS,
                        .limit = old_max_addrs
                },
                {
                        .type  = MPTCPD_LIMIT_SUBFLOWS,
                        .limit = old_max_subflows
                }
        };

        int const result = mptcpd_kpm_set_limits(pm,
                                                 limits,
                                                 L_ARRAY_SIZE(limits));

        assert(result == 0 || result == ENOTSUP);
}

static void get_limits_callback(struct mptcpd_limit const *limits,
                                size_t len,
                                void *user_data)
{
        uint32_t addrs_limit = max_addrs;
        uint32_t subflows_limit = max_subflows;

        if (geteuid() != 0) {
                /*
                  If the current user is not root, the previous
                  set_limits() call fails with EPERM, but libell
                  APIs don't allow reporting such error to the caller.
                  Just assume set_limits() has no effect.
                */
                addrs_limit = old_max_addrs;
                subflows_limit = old_max_subflows;
        }

        assert(limits != NULL);

        for (struct mptcpd_limit const *l = limits;
             l != limits + len;
             ++l) {
                if (l->type == MPTCPD_LIMIT_RCV_ADD_ADDRS) {
                        assert(l->limit == addrs_limit);
                } else if (l->type == MPTCPD_LIMIT_SUBFLOWS) {
                        assert(l->limit == subflows_limit);
                } else {
                        /*
                          Unless more MPTCP limit types are added to
                          the kernel path management API this should
                          never be reached.
                        */
                        l_error("Unexpected MPTCP limit type.");
                }
        }

        /*
          Done testing get/set_limits.

          Reset MPTCP limits to their original values.
        */
        struct mptcpd_pm *const pm = user_data;
        reset_old_limits(pm);
}

// -------------------------------------------------------------------

static void test_get_port(void const *test_data)
{
        (void) test_data;

        struct sockaddr_in const *const addr = &test_laddr_1;

        // Network byte order port.
        in_port_t const nport = addr->sin_port;

        // Host byte order port.
        struct sockaddr const *const sa = (struct sockaddr const *) addr;
        in_port_t const hport = mptcpd_get_port_number(sa);

        /*
           Verify that mptcpd_get_port_number() returns a host byte
           order port.

           The mptcpd_get_port_number() function is used internally by
           the kernel-specific generic netlink MPTCP path management
           API implementations in mptcpd.  That API expects ports to
           be in host byte order.
        */
        assert(hport == ntohs(nport));
}

// -------------------------------------------------------------------
// In-kernel (server-oriented) path manager commands.
// -------------------------------------------------------------------
static void test_add_addr_kernel(void const *test_data)
{
        struct test_info  *const info = (struct test_info *) test_data;
        struct mptcpd_pm  *const pm   = info->pm;
        struct mptcpd_idm *const idm  = mptcpd_pm_get_idm(pm);

        struct test_addr_info *const k_addr = &info->k_addr;

        k_addr->id = mptcpd_idm_get_id(idm, k_addr->addr);

        int const result = mptcpd_kpm_add_addr(pm,
                                               k_addr->addr,
                                               k_addr->id,
                                               k_addr->flags,
                                               k_addr->ifindex);

        assert(result == 0 || result == ENOTSUP);
}

static void test_remove_addr_kernel(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        struct test_addr_info const *const k_addr = &info->k_addr;

        int const result = mptcpd_kpm_remove_addr(pm, k_addr->id);

        assert(result == 0 || result == ENOTSUP);
}

static void test_get_addr(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        int const result =
                mptcpd_kpm_get_addr(pm,
                                    info->k_addr.id,
                                    get_addr_callback,
                                    info,
                                    NULL);

        assert(result == 0 || result == ENOTSUP);
}

static void test_dump_addrs(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        int const result =
                mptcpd_kpm_dump_addrs(pm,
                                      dump_addrs_callback,
                                      info,
                                      dump_addrs_complete);

        assert(result == 0 || result == ENOTSUP);
}

static void test_flush_addrs(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        int const result = mptcpd_kpm_flush_addrs(pm);

        /**
         * @bug We could have a resource leak in the kernel here if
         *      the below assert()s are triggered since addresses
         *      previously added through @c mptcpd_kpm_add_addr()
         *      would end up not being removed prior to test exit.
         */
        assert(result == 0 || result == ENOTSUP);
}

static void test_set_limits(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        struct mptcpd_limit const limits[] = {
                {
                        .type  = MPTCPD_LIMIT_RCV_ADD_ADDRS,
                        .limit = max_addrs
                },
                {
                        .type  = MPTCPD_LIMIT_SUBFLOWS,
                        .limit = max_subflows
                }
        };

        l_debug("SETTING max addrs to: %u", limits[0].limit);

        /**
         * @note We're potentially overriding previously set MPTCP
         *       limits here but we reset them later on once the
         *       get_limits test is done, assuming the test exits
         *       gracefully.
         */
        int const result = mptcpd_kpm_set_limits(pm,
                                                 limits,
                                                 L_ARRAY_SIZE(limits));

        assert(result == 0 || result == ENOTSUP);
}

static void test_get_limits(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        int const result = mptcpd_kpm_get_limits(pm,
                                                 get_limits_callback,
                                                 pm);

        assert(result == 0 || result == ENOTSUP);
}

static void test_set_flags(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        static mptcpd_flags_t const flags = MPTCPD_ADDR_FLAG_BACKUP;

        int const result =
                mptcpd_kpm_set_flags(pm, info->k_addr.addr, flags);

        assert(result == 0 || result == ENOTSUP);
}

// -------------------------------------------------------------------
// User space (client-oriented) path manager commands.
// -------------------------------------------------------------------
static void test_add_addr_user(void const *test_data)
{
        struct test_info  *const info = (struct test_info *) test_data;
        struct mptcpd_pm  *const pm   = info->pm;

        struct test_addr_info const *const u_addr = &info->u_addr;

        int const result = mptcpd_pm_add_addr(pm,
                                              u_addr->addr,
                                              u_addr->id,
                                              u_addr->token);

        /*
          EADDRNOTAVAIL error will generally occur if the test is run
          without sufficient permissions to set up the test addresses
          by associating them with a network interface.

         */
        assert(result == 0
               || result == ENOTSUP
               || result == EADDRNOTAVAIL);
}

static void test_remove_addr_user(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        struct test_addr_info const *const u_addr = &info->u_addr;

        int const result = mptcpd_pm_remove_addr(pm,
                                                 u_addr->addr,
                                                 u_addr->id,
                                                 u_addr->token);

        assert(result == 0 || result == ENOTSUP);
}

static void test_add_subflow(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        struct test_addr_info const *const u_addr = &info->u_addr;

        int const result = mptcpd_pm_add_subflow(pm,
                                                 u_addr->token,
                                                 test_laddr_id_2,
                                                 test_raddr_id_2,
                                                 laddr2,
                                                 raddr2,
                                                 test_backup_2);

        assert(result == 0 || result == ENOTSUP);
}

void test_set_backup(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        struct test_addr_info const *const u_addr = &info->u_addr;

        int const result = mptcpd_pm_set_backup(pm,
                                                u_addr->token,
                                                laddr2,
                                                raddr2,
                                                !test_backup_2);

        assert(result == 0 || result == ENOTSUP);
}

void test_remove_subflow(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        struct test_addr_info const *const u_addr = &info->u_addr;

        int const result = mptcpd_pm_remove_subflow(pm,
                                                    u_addr->token,
                                                    laddr2,
                                                    raddr2);

        assert(result == 0 || result == ENOTSUP);
}

// -------------------------------------------------------------------


void test_get_nm(void const *test_data)
{
        struct test_info *const info = (struct test_info *) test_data;
        struct mptcpd_pm *const pm   = info->pm;

        assert(mptcpd_pm_get_nm(pm) != NULL);
}

// -------------------------------------------------------------------

static void handle_rtm_newaddr(int errnum,
                               uint16_t type,
                               void const *data,
                               uint32_t len,
                               void *user_data)
{
        /*
          No data expected in underlying RTM_NEWADDR command reply
          message.
        */
        assert(type == 0);
        assert(data == NULL);
        assert(len == 0);

        (void) user_data;  // Pointer to struct test_info.

        if (errnum != 0) {

                static int const status = 0;  // Do not exit on error.

                error(status,
                      errnum,
                      "bind of test address to interface failed");
        }
}

static void handle_rtm_deladdr(int errnum,
                               uint16_t type,
                               void const *data,
                               uint32_t len,
                               void *user_data)
{
        /*
          No data expected in underlying RTM_DELADDR command reply
          message.
        */
        assert(type == 0);
        assert(data == NULL);
        assert(len == 0);

        if (errnum != 0) {
                static int const status = 0;  // Do not exit on error.

                struct test_addr_info *const info = user_data;

                error(status,
                      errnum,
                      "unbind of test address %s from "
                      "interface %d failed",
                      info->ip,
                      info->ifindex);
        }
}

// -------------------------------------------------------------------

static void test_add(const char *name,
                     l_test_func_t function,
                     void *test_data)
{
        l_info("Test: %s", name);
        function(test_data);
}

static void exec_tests(void *user_data)
{
        struct test_info *const info = user_data;

        // Non-command tests.
        test_add("get_port", test_get_port, NULL);
        test_add("get_nm",   test_get_nm,   info);

        // In-kernel path manager tests.
        test_add("add_addr - kernel",    test_add_addr_kernel,    info);
        test_add("get_addr",             test_get_addr,           info);
        test_add("dump_addrs",           test_dump_addrs,         info);
        test_add("set_flags",            test_set_flags,          info);
        test_add("remove_addr - kernel", test_remove_addr_kernel, info);
        test_add("set_limits",           test_set_limits,         info);
        test_add("get_limits",           test_get_limits,         info);
        test_add("flush_addrs",          test_flush_addrs,        info);

        // User space path manager tests.
        test_add("add_addr - user",    test_add_addr_user,    info);
        test_add("add_subflow",        test_add_subflow,      info);
        test_add("set_backup",         test_set_backup,       info);
        test_add("remove_subflow",     test_remove_subflow,   info);
        test_add("remove_addr - user", test_remove_addr_user, info);
}

static void run_tests(void *user_data)
{
        struct test_info *const t = user_data;

        exec_tests(user_data);

        t->tests_called = true;
}

static void set_new_limit(uint32_t *limit,
                          uint32_t old_limit,
                          uint32_t offset)
{
        assert(limit != NULL);
        assert(offset > 0);
        assert(old_limit <= absolute_max_limit);

        // Do not exceed kernel hard-coded max.
        if (absolute_max_limit - old_limit >= offset)
                *limit = old_limit + offset;
        else
                *limit = absolute_max_limit;
}

static void get_old_limits_callback(struct mptcpd_limit const *limits,
                                    size_t len,
                                    void *user_data)
{
        for (struct mptcpd_limit const *l = limits;
             l != limits + len;
             ++l) {
                if (l->type == MPTCPD_LIMIT_RCV_ADD_ADDRS) {
                        old_max_addrs = l->limit;
                } else if (l->type == MPTCPD_LIMIT_SUBFLOWS) {
                        old_max_subflows = l->limit;
                } else {
                        /*
                          Unless more MPTCP limit types are added to
                          the kernel path management API this should
                          never be reached.
                        */
                        l_warn("Unexpected MPTCP limit type: %u",
                               l->type);
                }
        }

        set_new_limit(&max_addrs, old_max_addrs, max_addrs_offset);
        set_new_limit(&max_subflows,
                      old_max_subflows,
                      max_subflows_offset);

        run_tests(user_data);
}

static void complete_setup(struct mptcpd_pm *pm, void *user_data)
{
        /**
         * @note There is a chicken-and-egg problem here since we're
         *       relying on mptcpd_kpm_get_limits() to get the MPTCP
         *       limits at test start even though we're actually
         *       testing this function in one of the test cases.
         */
        int const result = mptcpd_kpm_get_limits(pm,
                                                 get_old_limits_callback,
                                                 user_data);

        assert(result == 0 || result == ENOTSUP);
}

static struct mptcpd_pm_ops const pm_ops ={ .ready = complete_setup };

static void setup_tests (void *user_data)
{
        struct test_info *const info = user_data;

        static char *argv[] = {
                "test-commands",
                "--plugin-dir",
                TEST_PLUGIN_DIR
        };

        static int argc = L_ARRAY_SIZE(argv);

        info->config = mptcpd_config_create(argc, argv);
        assert(info->config);

        info->pm = mptcpd_pm_create(info->config);
        assert(info->pm);

        bool const registered =
                mptcpd_pm_register_ops(info->pm, &pm_ops, info);

        assert(registered);
}

static void complete_address_setup(void *user_data)
{
        if (++addr_setup_count == addrs_to_setup_count) {
                // Run tests after address setup is complete.
                bool const result =
                        l_idle_oneshot(setup_tests, user_data, NULL);

                assert(result);
        }
}

static void complete_address_teardown(void *user_data)
{
        (void) user_data;

        if (--addr_setup_count == 0)
                l_main_quit();
}

static void setup_test_address(struct test_info *data,
                               struct test_addr_info *info)
{
        sa_family_t const sa_family = info->addr->sa_family;

        int id = 0;

        if (sa_family == AF_INET) {
                id = l_rtnl_ifaddr4_add(data->rtnl,
                                        info->ifindex,
                                        info->prefix_len,
                                        info->ip,
                                        NULL, // broadcast
                                        handle_rtm_newaddr,
                                        data,
                                        complete_address_setup);
        } else if (sa_family == AF_INET6) {
                id = l_rtnl_ifaddr6_add(data->rtnl,
                                        info->ifindex,
                                        info->prefix_len,
                                        info->ip,
                                        handle_rtm_newaddr,
                                        data,
                                        complete_address_setup);
        }

        assert(id != 0);
}

static void setup_test_addresses(struct test_info *info)
{
        // Address used for user space PM advertising tests.
        setup_test_address(info, &info->u_addr);

        // Address used for kernel space PM tests.
        setup_test_address(info, &info->k_addr);
}

static void teardown_test_address(struct l_netlink *rtnl,
                                  struct test_addr_info *info)
{
        sa_family_t const sa_family = info->addr->sa_family;

        int id = 0;

        if (sa_family == AF_INET) {
                id = l_rtnl_ifaddr4_delete(rtnl,
                                           info->ifindex,
                                           info->prefix_len,
                                           info->ip,
                                           NULL, // broadcast
                                           handle_rtm_deladdr,
                                           info,
                                           complete_address_teardown);
        } else if (sa_family == AF_INET6) {
                id = l_rtnl_ifaddr6_delete(rtnl,
                                           info->ifindex,
                                           info->prefix_len,
                                           info->ip,
                                           handle_rtm_deladdr,
                                           info,
                                           complete_address_teardown);
        }

        assert(id != 0);
}

static void teardown_test_addresses(struct test_info *info)
{
        // Address used for user space PM advertising tests.
        teardown_test_address(info->rtnl, &info->u_addr);

        // Address used for kernel space PM tests.
        teardown_test_address(info->rtnl, &info->k_addr);
}

// -------------------------------------------------------------------

static void idle_callback(struct l_idle *idle, void *user_data)
{
        (void) idle;

        /*
          Number of ELL event loop iterations to go through before
          exiting.

          This gives the mptcpd path manager enough time to process
          replies from commands like get_addr, dump_addrs, and get_limits.
        */
        static int const trigger_count = 40;

        /*
          Maximum number of ELL event loop iterations.

          Stop the ELL event loop after this number of iterations.
         */
        static int const max_count = trigger_count * 2;

        /* ELL event loop iteration count. */
        static int count = 0;

        assert(max_count > trigger_count);  // Sanity check.

        if (++count > max_count)
                teardown_test_addresses(user_data); // Done running tests.
}

static uint8_t get_prefix_len(struct sockaddr const *sa)
{
        // One IP address
        return sa->sa_family == AF_INET ? 32 : 128;
}

// -------------------------------------------------------------------

static void test_commands(void const *data)
{
        (void) data;

        assert(l_main_init());

        l_log_set_stderr();
        l_debug_enable("*");

        struct l_netlink *const rtnl = l_netlink_new(NETLINK_ROUTE);
        assert(rtnl != NULL);

        // Bind test IP addresses to loopback interface.
        static char const loopback[] = "lo";

        // Mutable sockaddr copies.
        struct sockaddr_in user_addr   = test_laddr_4;
        struct sockaddr_in kernel_addr = test_laddr_1;

        /*
          Set the test port to zero make the kernel choose a random
          (ephemeral) unused port to prevent potential reuse of an
          existing address.
        */
        user_addr.sin_port = 0;

        struct test_info info = {
                .rtnl    = rtnl,
                .u_addr = {
                        .addr        = (struct sockaddr *) &user_addr,
                        .ifindex     = if_nametoindex(loopback),
                        .prefix_len  = get_prefix_len(info.u_addr.addr),
                        .token       = test_token_4,
                        .id          = test_laddr_id_4
                },
                .k_addr = {
                        .addr        = (struct sockaddr *) &kernel_addr,
                        .ifindex     = if_nametoindex(loopback),
                        .prefix_len  = get_prefix_len(info.k_addr.addr),
                        .flags       = MPTCPD_ADDR_FLAG_SUBFLOW
                }
        };

        inet_ntop(info.u_addr.addr->sa_family,
                  get_in_addr(info.u_addr.addr),
                  info.u_addr.ip,
                  sizeof(info.u_addr.ip));

        inet_ntop(info.k_addr.addr->sa_family,
                  get_in_addr(info.k_addr.addr),
                  info.k_addr.ip,
                  sizeof(info.k_addr.ip));

        setup_test_addresses(&info);

        struct l_idle *const idle =
                l_idle_create(idle_callback, &info, NULL);

        (void) l_main_run();

        /*
          The tests will have run only if the MPTCP generic netlink
          family appeared.
         */
        assert(info.tests_called);

#ifdef HAVE_UPSTREAM_KERNEL
        assert(info.dump_addrs_complete_count == 1);
#endif

        l_idle_remove(idle);
        l_netlink_destroy(rtnl);
        mptcpd_pm_destroy(info.pm);
        mptcpd_config_destroy(info.config);

        assert(l_main_exit());
}

int main(int argc, char *argv[])
{
        // Skip this test if the kernel is not MPTCP capable.
        tests_skip_if_no_mptcp();

        l_test_init(&argc, &argv);

        l_test_add("commands", test_commands, NULL);

        return l_test_run();
}


/*
  Local Variables:
  c-file-style: "linux"
  End:
*/