summaryrefslogtreecommitdiff
path: root/clients/common/tests/test-libnm-core-aux.c
blob: a0bfea25d2d259e04046da86a0fb60fb5a4d857f (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019 Red Hat, Inc.
 */

#include "libnm/nm-default-client.h"

#include "libnm-core-aux-extern/nm-libnm-core-aux.h"
#include "libnm-core-aux-intern/nm-libnm-core-utils.h"

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

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

static NMTeamLinkWatcher *
_team_link_watcher_from_string_impl(const char *str, gsize nextra, const char *const *vextra)
{
    NMTeamLinkWatcher *watcher;
    gs_free char *     str1_free = NULL;
    gs_free_error GError *error  = NULL;
    gsize                 i;

    g_assert(str);

    watcher = nm_utils_team_link_watcher_from_string(str, &error);
    nmtst_assert_success(watcher, error);

    for (i = 0; i < 1 + nextra; i++) {
        nm_auto_unref_team_link_watcher NMTeamLinkWatcher *watcher1 = NULL;
        const char *                                       str1;

        if (i == 0) {
            str1_free = nm_utils_team_link_watcher_to_string(watcher);
            g_assert(str1_free);
            str1 = str1_free;
            g_assert_cmpstr(str, ==, str1);
        } else
            str1 = vextra[i - 1];

        watcher1 = nm_utils_team_link_watcher_from_string(str1, &error);
        nmtst_assert_success(watcher1, error);
        if (!nm_team_link_watcher_equal(watcher, watcher1)) {
            gs_free char *ss1 = NULL;
            gs_free char *ss2 = NULL;

            g_print(">>> watcher differs: \"%s\" vs. \"%s\"",
                    (ss1 = nm_utils_team_link_watcher_to_string(watcher)),
                    (ss2 = nm_utils_team_link_watcher_to_string(watcher1)));
            g_print(">>> ORIG: \"%s\" vs. \"%s\"", str, str1);
            g_assert_not_reached();
        }
        g_assert(nm_team_link_watcher_equal(watcher1, watcher));
    }

    return watcher;
}
#define _team_link_watcher_from_string(str, ...) \
    _team_link_watcher_from_string_impl((str), NM_NARG(__VA_ARGS__), NM_MAKE_STRV(__VA_ARGS__))

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

static void
test_team_link_watcher_tofro_string(void)
{
    nm_auto_unref_team_link_watcher NMTeamLinkWatcher *w = NULL;

#define _team_link_watcher_cmp(watcher,                                                   \
                               name,                                                      \
                               delay_down,                                                \
                               delay_up,                                                  \
                               init_wait,                                                 \
                               interval,                                                  \
                               missed_max,                                                \
                               target_host,                                               \
                               source_host,                                               \
                               vlanid,                                                    \
                               arping_flags)                                              \
    G_STMT_START                                                                          \
    {                                                                                     \
        nm_auto_unref_team_link_watcher NMTeamLinkWatcher *_w = g_steal_pointer(watcher); \
                                                                                          \
        g_assert_cmpstr((name), ==, nm_team_link_watcher_get_name(_w));                   \
        g_assert_cmpint((delay_down), ==, nm_team_link_watcher_get_delay_down(_w));       \
        g_assert_cmpint((delay_up), ==, nm_team_link_watcher_get_delay_up(_w));           \
        g_assert_cmpint((init_wait), ==, nm_team_link_watcher_get_init_wait(_w));         \
        g_assert_cmpint((interval), ==, nm_team_link_watcher_get_interval(_w));           \
        g_assert_cmpint((missed_max), ==, nm_team_link_watcher_get_missed_max(_w));       \
        g_assert_cmpstr((target_host), ==, nm_team_link_watcher_get_target_host(_w));     \
        g_assert_cmpstr((source_host), ==, nm_team_link_watcher_get_source_host(_w));     \
        g_assert_cmpint((vlanid), ==, nm_team_link_watcher_get_vlanid(_w));               \
        g_assert_cmpint((arping_flags), ==, nm_team_link_watcher_get_flags(_w));          \
    }                                                                                     \
    G_STMT_END

    w = _team_link_watcher_from_string("name=ethtool",
                                       "delay-up=0   name=ethtool",
                                       "  delay-down=0   name=ethtool   ");
    _team_link_watcher_cmp(&w,
                           "ethtool",
                           0,
                           0,
                           -1,
                           -1,
                           -1,
                           NULL,
                           NULL,
                           -1,
                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);

    w = _team_link_watcher_from_string("name=ethtool delay-up=10",
                                       "   delay-down=0  delay-up=10   name=ethtool");
    _team_link_watcher_cmp(&w,
                           "ethtool",
                           0,
                           10,
                           -1,
                           -1,
                           -1,
                           NULL,
                           NULL,
                           -1,
                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);

    w = _team_link_watcher_from_string("name=ethtool delay-up=10 delay-down=11",
                                       "   delay-down=11  delay-up=10   name=ethtool");
    _team_link_watcher_cmp(&w,
                           "ethtool",
                           11,
                           10,
                           -1,
                           -1,
                           -1,
                           NULL,
                           NULL,
                           -1,
                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);

    w = _team_link_watcher_from_string(
        "name=nsna_ping target-host=xxx",
        "name=nsna_ping target-host=xxx",
        "  missed-max=3    target-host=xxx        name=nsna_ping   ");
    _team_link_watcher_cmp(&w,
                           "nsna_ping",
                           -1,
                           -1,
                           0,
                           0,
                           3,
                           "xxx",
                           NULL,
                           -1,
                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);

    w = _team_link_watcher_from_string("name=arp_ping target-host=xxx source-host=yzd",
                                       "  source-host=yzd target-host=xxx        name=arp_ping   ");
    _team_link_watcher_cmp(&w,
                           "arp_ping",
                           -1,
                           -1,
                           0,
                           0,
                           3,
                           "xxx",
                           "yzd",
                           -1,
                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);

    w = _team_link_watcher_from_string(
        "name=arp_ping missed-max=0 target-host=xxx vlanid=0 source-host=yzd");
    _team_link_watcher_cmp(&w,
                           "arp_ping",
                           -1,
                           -1,
                           0,
                           0,
                           0,
                           "xxx",
                           "yzd",
                           0,
                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);

    w = _team_link_watcher_from_string(
        "name=arp_ping target-host=xxx source-host=yzd validate-active=true",
        "source-host=yzd send-always=false name=arp_ping validate-active=true "
        "validate-inactive=false target-host=xxx",
        "  source-host=yzd target-host=xxx   validate-active=true      name=arp_ping   ");
    _team_link_watcher_cmp(&w,
                           "arp_ping",
                           -1,
                           -1,
                           0,
                           0,
                           3,
                           "xxx",
                           "yzd",
                           -1,
                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_VALIDATE_ACTIVE);

    w = _team_link_watcher_from_string(
        "name=arp_ping target-host=xxx source-host=yzd validate-active=true validate-inactive=true "
        "send-always=true",
        "source-host=yzd send-always=true name=arp_ping validate-active=true "
        "validate-inactive=true target-host=xxx",
        "source-host=yzd send-always=true name=arp_ping validate-active=1 validate-inactive=yes "
        "target-host=xxx",
        "  source-host=yzd target-host=xxx   validate-inactive=true send-always=true    "
        "validate-active=true      name=arp_ping   ");
    _team_link_watcher_cmp(&w,
                           "arp_ping",
                           -1,
                           -1,
                           0,
                           0,
                           3,
                           "xxx",
                           "yzd",
                           -1,
                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_VALIDATE_ACTIVE
                               | NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_VALIDATE_INACTIVE
                               | NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_SEND_ALWAYS);

    w = _team_link_watcher_from_string(
        "name=arp_ping missed-max=0 target-host=xxx vlanid=0 source-host=yzd");
    _team_link_watcher_cmp(&w,
                           "arp_ping",
                           -1,
                           -1,
                           0,
                           0,
                           0,
                           "xxx",
                           "yzd",
                           0,
                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);
}

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

NMTST_DEFINE();

int
main(int argc, char **argv)
{
    nmtst_init(&argc, &argv, TRUE);

    g_test_add_func("/libnm-core-aux/test_team_link_watcher_tofro_string",
                    test_team_link_watcher_tofro_string);

    return g_test_run();
}