summary refs log tree commit diff
path: root/src/core/tests/test-utils.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2021-02-11 18:11:46 +0100
committerMichael Biebl <biebl@debian.org>2021-02-11 18:11:46 +0100
commit80ec1decc49c72efec2a8b87c06245c92c0ab807 (patch)
treee3b229aa94e8dcf0590f2317664176e7b8f7607b /src/core/tests/test-utils.c
parent65f86e8f56267192d42f2b629fc6b0c99fb9cd0c (diff)
New upstream version 1.29.90 upstream/1.29.90
Diffstat (limited to 'src/core/tests/test-utils.c')
-rw-r--r--src/core/tests/test-utils.c233
1 files changed, 233 insertions, 0 deletions
diff --git a/src/core/tests/test-utils.c b/src/core/tests/test-utils.c
new file mode 100644
index 00000000..1d562bde
--- /dev/null
+++ b/src/core/tests/test-utils.c
@@ -0,0 +1,233 @@
+/* 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_impl(NM_UTILS_STABLE_TYPE_UUID,
+                                               &addr1,
+                                               "eth666",
+                                               "6b138152-9f3e-4b97-aaf7-e6e553f2a24e",
+                                               0,
+                                               (guint8 *) "key",
+                                               3,
+                                               NULL);
+    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_impl(NM_UTILS_STABLE_TYPE_UUID,
+                                               &addr1,
+                                               "eth666",
+                                               "",
+                                               384,
+                                               (guint8 *) "key",
+                                               3,
+                                               NULL);
+    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_impl(NM_UTILS_STABLE_TYPE_UUID,
+                                               &addr1,
+                                               "eth666",
+                                               "",
+                                               384,
+                                               (guint8 *) "key",
+                                               3,
+                                               NULL);
+    nmtst_assert_ip6_address(&addr1, "2::338e:8d:c11:8726");
+
+    inet_pton(AF_INET6, "1234::", &addr1);
+    nm_utils_ipv6_addr_set_stable_privacy_impl(NM_UTILS_STABLE_TYPE_STABLE_ID,
+                                               &addr1,
+                                               "eth666",
+                                               "6b138152-9f3e-4b97-aaf7-e6e553f2a24e",
+                                               0,
+                                               (guint8 *) "key",
+                                               3,
+                                               NULL);
+    nmtst_assert_ip6_address(&addr1, "1234::ad4c:ae44:3d30:af1e");
+
+    inet_pton(AF_INET6, "1234::", &addr1);
+    nm_utils_ipv6_addr_set_stable_privacy_impl(NM_UTILS_STABLE_TYPE_STABLE_ID,
+                                               &addr1,
+                                               "eth666",
+                                               "stable-id-1",
+                                               0,
+                                               (guint8 *) "key",
+                                               3,
+                                               NULL);
+    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");
+}
+
+/*****************************************************************************/
+
+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);
+
+    return g_test_run();
+}