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

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

#include <arpa/inet.h>
#include <linux/if_ether.h>

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

static void
test_stable_privacy(void)
{
    struct in6_addr addr1;

    inet_pton(AF_INET6, "1234::", &addr1);
    nm_utils_ipv6_addr_set_stable_privacy_with_host_id(NM_UTILS_STABLE_TYPE_UUID,
                                                       &addr1,
                                                       "eth666",
                                                       "6b138152-9f3e-4b97-aaf7-e6e553f2a24e",
                                                       0,
                                                       (guint8 *) "key",
                                                       3);
    nmtst_assert_ip6_address(&addr1, "1234::4ceb:14cd:3d54:793f");

    /* We get an address without the UUID. */
    inet_pton(AF_INET6, "1::", &addr1);
    nm_utils_ipv6_addr_set_stable_privacy_with_host_id(NM_UTILS_STABLE_TYPE_UUID,
                                                       &addr1,
                                                       "eth666",
                                                       "",
                                                       384,
                                                       (guint8 *) "key",
                                                       3);
    nmtst_assert_ip6_address(&addr1, "1::11aa:2530:9144:dafa");

    /* We get a different address in a different network. */
    inet_pton(AF_INET6, "2::", &addr1);
    nm_utils_ipv6_addr_set_stable_privacy_with_host_id(NM_UTILS_STABLE_TYPE_UUID,
                                                       &addr1,
                                                       "eth666",
                                                       "",
                                                       384,
                                                       (guint8 *) "key",
                                                       3);
    nmtst_assert_ip6_address(&addr1, "2::338e:8d:c11:8726");

    inet_pton(AF_INET6, "1234::", &addr1);
    nm_utils_ipv6_addr_set_stable_privacy_with_host_id(NM_UTILS_STABLE_TYPE_STABLE_ID,
                                                       &addr1,
                                                       "eth666",
                                                       "6b138152-9f3e-4b97-aaf7-e6e553f2a24e",
                                                       0,
                                                       (guint8 *) "key",
                                                       3);
    nmtst_assert_ip6_address(&addr1, "1234::ad4c:ae44:3d30:af1e");

    inet_pton(AF_INET6, "1234::", &addr1);
    nm_utils_ipv6_addr_set_stable_privacy_with_host_id(NM_UTILS_STABLE_TYPE_STABLE_ID,
                                                       &addr1,
                                                       "eth666",
                                                       "stable-id-1",
                                                       0,
                                                       (guint8 *) "key",
                                                       3);
    nmtst_assert_ip6_address(&addr1, "1234::4944:67b0:7a6c:1cf");
}

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

static void
_do_test_hw_addr(NMUtilsStableType  stable_type,
                 const char        *stable_id,
                 const guint8      *secret_key,
                 gsize              key_len,
                 const char        *ifname,
                 const char        *current_mac_address,
                 const char        *generate_mac_address_mask,
                 const char *const *expected)
{
    gs_free char      *generated = NULL;
    const char *const *e;
    gboolean           found = FALSE;

    for (e = expected; *e; e++) {
        g_assert(*e);
        g_assert(nm_utils_hwaddr_valid(*e, ETH_ALEN));
    }

    generated = nm_utils_hw_addr_gen_stable_eth_impl(stable_type,
                                                     stable_id,
                                                     secret_key,
                                                     key_len,
                                                     ifname,
                                                     current_mac_address,
                                                     generate_mac_address_mask);

    g_assert(generated);
    g_assert(nm_utils_hwaddr_valid(generated, ETH_ALEN));
    for (e = expected; *e; e++) {
        if (!nm_utils_hwaddr_matches(generated, -1, *e, -1))
            continue;
        g_assert(!found);
        found = TRUE;
        g_assert_cmpstr(generated, ==, *e);
    }
    g_assert(found);
}
#define do_test_hw_addr(stable_type,                    \
                        stable_id,                      \
                        secret_key,                     \
                        ifname,                         \
                        current_mac_address,            \
                        generate_mac_address_mask,      \
                        ...)                            \
    _do_test_hw_addr((stable_type),                     \
                     (stable_id),                       \
                     (const guint8 *) "" secret_key "", \
                     NM_STRLEN(secret_key),             \
                     (ifname),                          \
                     "" current_mac_address "",         \
                     generate_mac_address_mask,         \
                     NM_MAKE_STRV(__VA_ARGS__))

static void
test_hw_addr_gen_stable_eth(void)
{
    do_test_hw_addr(NM_UTILS_STABLE_TYPE_UUID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "01:23:45:67:89:ab",
                    NULL,
                    "06:0D:CD:0C:9E:2C");
    do_test_hw_addr(NM_UTILS_STABLE_TYPE_STABLE_ID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "01:23:45:67:89:ab",
                    NULL,
                    "C6:AE:A9:9A:76:09");

    do_test_hw_addr(NM_UTILS_STABLE_TYPE_UUID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "01:23:45:67:89:ab",
                    "FF:FF:FF:00:00:00",
                    "00:23:45:0C:9E:2C");
    do_test_hw_addr(NM_UTILS_STABLE_TYPE_UUID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "03:23:45:67:89:ab",
                    "FF:FF:FF:00:00:00",
                    "02:23:45:0C:9E:2C");

    do_test_hw_addr(NM_UTILS_STABLE_TYPE_UUID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "01:23:45:67:89:ab",
                    "00:00:00:00:00:00",
                    "06:0D:CD:0C:9E:2C");
    do_test_hw_addr(NM_UTILS_STABLE_TYPE_UUID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "01:23:45:67:89:ab",
                    "02:00:00:00:00:00",
                    "04:0D:CD:0C:9E:2C");
    do_test_hw_addr(NM_UTILS_STABLE_TYPE_UUID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "01:23:45:67:89:ab",
                    "02:00:00:00:00:00",
                    "04:0D:CD:0C:9E:2C");

    do_test_hw_addr(NM_UTILS_STABLE_TYPE_UUID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "01:23:45:67:89:ab",
                    "02:00:00:00:00:00 00:00:00:00:00:00",
                    "04:0D:CD:0C:9E:2C");
    do_test_hw_addr(NM_UTILS_STABLE_TYPE_UUID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "01:23:45:67:89:ab",
                    "02:00:00:00:00:00 02:00:00:00:00:00",
                    "06:0D:CD:0C:9E:2C");

    do_test_hw_addr(NM_UTILS_STABLE_TYPE_UUID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "01:23:45:67:89:ab",
                    "00:00:00:00:00:00 E9:60:CE:F5:ED:2F",
                    "06:0D:CD:0C:9E:2C");

    do_test_hw_addr(NM_UTILS_STABLE_TYPE_UUID,
                    "stable-1",
                    "key1",
                    "eth0",
                    "01:23:45:67:89:ab",
                    "02:00:00:00:00:00 00:00:00:00:00:00 02:00:00:00:00:00",
                    "06:0D:CD:0C:9E:2C",
                    "04:0D:CD:0C:9E:2C");
}

static void
test_shorten_hostname(void)
{
    gs_free char *maxhost = NULL;
    char         *hostname;

#define do_test_shorten_hostname(_host, _exp_res, _exp_short) \
    G_STMT_START                                              \
    {                                                         \
        gboolean      _res;                                   \
        gs_free char *_short = NULL;                          \
                                                              \
        _res = nm_utils_shorten_hostname((_host), &_short);   \
        g_assert_cmpint((_res), ==, (_exp_res));              \
        g_assert_cmpstr(_short, ==, (_exp_short));            \
    }                                                         \
    G_STMT_END

    /* 'maxhost' is the longest allowed hostname according to
     * system configuration (`getconf HOST_NAME_MAX`). On Linux
     * it's typically 64 characters, but POSIX allows up to
     * 255 characters.
     *
     * We use our own define NM_HOST_NAME_MAX, which is always 64.
     */
    maxhost = g_strnfill(NM_HOST_NAME_MAX, 'a');

    do_test_shorten_hostname("name1", TRUE, NULL);

    do_test_shorten_hostname("name1.example.com", TRUE, NULL);

    do_test_shorten_hostname(maxhost, TRUE, NULL);

    hostname = g_strdup_printf("%sbbb", maxhost);
    do_test_shorten_hostname(hostname, TRUE, maxhost);
    nm_clear_g_free(&hostname);

    hostname = g_strdup_printf("%s.com", maxhost);
    do_test_shorten_hostname(hostname, TRUE, maxhost);
    nm_clear_g_free(&hostname);

    hostname = g_strdup_printf("name1.%s.com", maxhost);
    do_test_shorten_hostname(hostname, TRUE, "name1");
    nm_clear_g_free(&hostname);

    do_test_shorten_hostname(".name1", FALSE, NULL);
}

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

NMTST_DEFINE();

int
main(int argc, char **argv)
{
    nmtst_init_with_logging(&argc, &argv, NULL, "ALL");

    g_test_add_func("/utils/stable_privacy", test_stable_privacy);
    g_test_add_func("/utils/hw_addr_gen_stable_eth", test_hw_addr_gen_stable_eth);
    g_test_add_func("/utils/shorten-hostname", test_shorten_hostname);

    return g_test_run();
}