summaryrefslogtreecommitdiff
path: root/src/core/devices/tests/test-acd.c
blob: b4b6516549df72755a945bfa4eddc50bc55c0ef3 (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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2015 Red Hat, Inc.
 */

#include "src/core/nm-default-daemon.h"

#include "n-acd/src/n-acd.h"

#include <linux/if_ether.h>

#include "devices/nm-acd-manager.h"
#include "platform/tests/test-common.h"

#define IFACE_VETH0 "nm-test-veth0"
#define IFACE_VETH1 "nm-test-veth1"

#define ADDR1 0x01010101
#define ADDR2 0x02020202
#define ADDR3 0x03030303
#define ADDR4 0x04040404

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

static gboolean
_skip_acd_test_check(void)
{
    NAcd *       acd;
    NAcdConfig * config;
    const guint8 hwaddr[ETH_ALEN] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
    int          r;
    static int   skip = -1;

    if (skip == -1) {
        r = n_acd_config_new(&config);
        g_assert(r == 0);

        n_acd_config_set_ifindex(config, 1);
        n_acd_config_set_transport(config, N_ACD_TRANSPORT_ETHERNET);
        n_acd_config_set_mac(config, hwaddr, sizeof(hwaddr));

        r = n_acd_new(&acd, config);
        n_acd_config_free(config);
        if (r == 0)
            n_acd_unref(acd);

        skip = (r != 0);
    }
    return skip;
}

#define _skip_acd_test()                                               \
    ({                                                                 \
        gboolean _skip = _skip_acd_test_check();                       \
                                                                       \
        if (_skip)                                                     \
            g_test_skip("Cannot create NAcd. Running under valgind?"); \
        _skip;                                                         \
    })

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

typedef struct {
    int           ifindex0;
    int           ifindex1;
    const guint8 *hwaddr0;
    const guint8 *hwaddr1;
    size_t        hwaddr0_len;
    size_t        hwaddr1_len;
} test_fixture;

static void
fixture_setup(test_fixture *fixture, gconstpointer user_data)
{
    /* create veth pair. */
    fixture->ifindex0 =
        nmtstp_link_veth_add(NM_PLATFORM_GET, -1, IFACE_VETH0, IFACE_VETH1)->ifindex;
    fixture->ifindex1 =
        nmtstp_link_get_typed(NM_PLATFORM_GET, -1, IFACE_VETH1, NM_LINK_TYPE_VETH)->ifindex;

    g_assert(nm_platform_link_change_flags(NM_PLATFORM_GET, fixture->ifindex0, IFF_UP, TRUE) >= 0);
    g_assert(nm_platform_link_change_flags(NM_PLATFORM_GET, fixture->ifindex1, IFF_UP, TRUE) >= 0);

    fixture->hwaddr0 =
        nm_platform_link_get_address(NM_PLATFORM_GET, fixture->ifindex0, &fixture->hwaddr0_len);
    fixture->hwaddr1 =
        nm_platform_link_get_address(NM_PLATFORM_GET, fixture->ifindex1, &fixture->hwaddr1_len);
}

typedef struct {
    in_addr_t addresses[8];
    in_addr_t peer_addresses[8];
    gboolean  expected_result[8];
} TestInfo;

static void
acd_manager_probe_terminated(NMAcdManager *acd_manager, gpointer user_data)
{
    g_main_loop_quit(user_data);
}

static void
test_acd_common(test_fixture *fixture, TestInfo *info)
{
    nm_auto_free_acdmgr NMAcdManager *manager = NULL;
    nm_auto_unref_gmainloop GMainLoop *loop   = NULL;
    int                                i;
    const guint                        WAIT_TIME_OPTIMISTIC = 50;
    guint                              wait_time;
    static const NMAcdCallbacks        callbacks = {
        .probe_terminated_callback = acd_manager_probe_terminated,
        .user_data_destroy         = (GDestroyNotify) g_main_loop_unref,
    };
    int r;

    if (_skip_acd_test())
        return;

    /* first, try with a short waittime. We hope that this is long enough
     * to successfully complete the test. Only if that's not the case, we
     * assume the computer is currently busy (high load) and we retry with
     * a longer timeout. */
    wait_time = WAIT_TIME_OPTIMISTIC;
again:

    nm_clear_pointer(&loop, g_main_loop_unref);
    loop = g_main_loop_new(NULL, FALSE);

    nm_clear_pointer(&manager, nm_acd_manager_free);
    manager = nm_acd_manager_new(fixture->ifindex0,
                                 fixture->hwaddr0,
                                 fixture->hwaddr0_len,
                                 &callbacks,
                                 g_main_loop_ref(loop));
    g_assert(manager != NULL);

    for (i = 0; info->addresses[i]; i++)
        g_assert(nm_acd_manager_add_address(manager, info->addresses[i]));

    for (i = 0; info->peer_addresses[i]; i++) {
        nmtstp_ip4_address_add(NULL,
                               FALSE,
                               fixture->ifindex1,
                               info->peer_addresses[i],
                               24,
                               0,
                               3600,
                               1800,
                               0,
                               NULL);
    }

    r = nm_acd_manager_start_probe(manager, wait_time);
    g_assert_cmpint(r, ==, 0);

    g_assert(nmtst_main_loop_run(loop, 2000));

    for (i = 0; info->addresses[i]; i++) {
        gboolean val;
        char     sbuf[NM_UTILS_INET_ADDRSTRLEN];

        val = nm_acd_manager_check_address(manager, info->addresses[i]);
        if (val == info->expected_result[i])
            continue;

        if (wait_time == WAIT_TIME_OPTIMISTIC) {
            /* probably we just had a glitch and the system took longer than
             * expected. Re-verify with a large timeout this time. */
            wait_time = 1000;
            goto again;
        }

        g_error("expected check for address #%d (%s) to %s, but it didn't",
                i,
                _nm_utils_inet4_ntop(info->addresses[i], sbuf),
                info->expected_result[i] ? "detect no duplicated" : "detect a duplicate");
    }
}

static void
test_acd_probe_1(test_fixture *fixture, gconstpointer user_data)
{
    TestInfo info = {.addresses       = {ADDR1, ADDR2, ADDR3},
                     .peer_addresses  = {ADDR4},
                     .expected_result = {TRUE, TRUE, TRUE}};

    test_acd_common(fixture, &info);
}

static void
test_acd_probe_2(test_fixture *fixture, gconstpointer user_data)
{
    TestInfo info = {.addresses       = {ADDR1, ADDR2, ADDR3, ADDR4},
                     .peer_addresses  = {ADDR3, ADDR2},
                     .expected_result = {TRUE, FALSE, FALSE, TRUE}};

    test_acd_common(fixture, &info);
}

static void
test_acd_announce(test_fixture *fixture, gconstpointer user_data)
{
    nm_auto_free_acdmgr NMAcdManager *manager = NULL;
    nm_auto_unref_gmainloop GMainLoop *loop   = NULL;
    int                                r;

    if (_skip_acd_test())
        return;

    manager =
        nm_acd_manager_new(fixture->ifindex0, fixture->hwaddr0, fixture->hwaddr0_len, NULL, NULL);
    g_assert(manager != NULL);

    g_assert(nm_acd_manager_add_address(manager, ADDR1));
    g_assert(nm_acd_manager_add_address(manager, ADDR2));

    loop = g_main_loop_new(NULL, FALSE);
    r    = nm_acd_manager_announce_addresses(manager);
    g_assert_cmpint(r, ==, 0);
    g_assert(!nmtst_main_loop_run(loop, 200));
}

static void
fixture_teardown(test_fixture *fixture, gconstpointer user_data)
{
    nm_platform_link_delete(NM_PLATFORM_GET, fixture->ifindex0);
    nm_platform_link_delete(NM_PLATFORM_GET, fixture->ifindex1);
}

NMTstpSetupFunc const _nmtstp_setup_platform_func = nm_linux_platform_setup;

void
_nmtstp_init_tests(int *argc, char ***argv)
{
    nmtst_init_with_logging(argc, argv, NULL, "ALL");
}

void
_nmtstp_setup_tests(void)
{
    g_test_add("/acd/probe/1",
               test_fixture,
               NULL,
               fixture_setup,
               test_acd_probe_1,
               fixture_teardown);
    g_test_add("/acd/probe/2",
               test_fixture,
               NULL,
               fixture_setup,
               test_acd_probe_2,
               fixture_teardown);
    g_test_add("/acd/announce",
               test_fixture,
               NULL,
               fixture_setup,
               test_acd_announce,
               fixture_teardown);
}