2019-09-10 11:19:01 +02:00
|
|
|
// SPDX-License-Identifier: LGPL-2.1+
|
2016-05-17 14:04:28 +02:00
|
|
|
/*
|
2019-10-01 09:20:35 +02:00
|
|
|
* Copyright (C) 2014 - 2016 Red Hat, Inc.
|
2016-05-17 14:04:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef __NM_TEST_UTILS_CORE_H__
|
|
|
|
|
#define __NM_TEST_UTILS_CORE_H__
|
|
|
|
|
|
|
|
|
|
#include "NetworkManagerUtils.h"
|
2020-01-02 07:37:59 +01:00
|
|
|
#include "nm-keyfile/nm-keyfile-internal.h"
|
2016-05-17 14:04:28 +02:00
|
|
|
|
|
|
|
|
#define _NMTST_INSIDE_CORE 1
|
|
|
|
|
|
2016-06-15 15:29:10 +02:00
|
|
|
#include "nm-utils/nm-test-utils.h"
|
2016-05-17 14:04:28 +02:00
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2018-01-02 12:13:00 +01:00
|
|
|
#define NMTST_EXPECT_NM(level, msg) NMTST_EXPECT ("NetworkManager", level, msg)
|
|
|
|
|
|
|
|
|
|
#define NMTST_EXPECT_NM_ERROR(msg) NMTST_EXPECT_NM (G_LOG_LEVEL_MESSAGE, "*<error> [*] "msg)
|
|
|
|
|
#define NMTST_EXPECT_NM_WARN(msg) NMTST_EXPECT_NM (G_LOG_LEVEL_MESSAGE, "*<warn> [*] "msg)
|
|
|
|
|
#define NMTST_EXPECT_NM_INFO(msg) NMTST_EXPECT_NM (G_LOG_LEVEL_INFO, "*<info> [*] "msg)
|
|
|
|
|
#define NMTST_EXPECT_NM_DEBUG(msg) NMTST_EXPECT_NM (G_LOG_LEVEL_DEBUG, "*<debug> [*] "msg)
|
|
|
|
|
#define NMTST_EXPECT_NM_TRACE(msg) NMTST_EXPECT_NM (G_LOG_LEVEL_DEBUG, "*<trace> [*] "msg)
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline void
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_init_with_logging (int *argc, char ***argv, const char *log_level, const char *log_domains)
|
|
|
|
|
{
|
|
|
|
|
__nmtst_init (argc, argv, FALSE, log_level, log_domains, NULL);
|
|
|
|
|
}
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline void
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_init_assert_logging (int *argc, char ***argv, const char *log_level, const char *log_domains)
|
|
|
|
|
{
|
|
|
|
|
gboolean set_logging;
|
|
|
|
|
|
|
|
|
|
__nmtst_init (argc, argv, TRUE, NULL, NULL, &set_logging);
|
|
|
|
|
|
|
|
|
|
if (!set_logging) {
|
|
|
|
|
gboolean success;
|
|
|
|
|
|
|
|
|
|
success = nm_logging_setup (log_level, log_domains, NULL, NULL);
|
|
|
|
|
g_assert (success);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifdef __NETWORKMANAGER_PLATFORM_H__
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline NMPlatformIP4Address *
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_platform_ip4_address (const char *address, const char *peer_address, guint plen)
|
|
|
|
|
{
|
|
|
|
|
static NMPlatformIP4Address addr;
|
|
|
|
|
|
|
|
|
|
g_assert (plen <= 32);
|
|
|
|
|
|
|
|
|
|
memset (&addr, 0, sizeof (addr));
|
|
|
|
|
addr.address = nmtst_inet4_from_string (address);
|
|
|
|
|
if (peer_address)
|
|
|
|
|
addr.peer_address = nmtst_inet4_from_string (peer_address);
|
|
|
|
|
else
|
|
|
|
|
addr.peer_address = addr.address;
|
|
|
|
|
addr.plen = plen;
|
|
|
|
|
|
|
|
|
|
return &addr;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline NMPlatformIP4Address *
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_platform_ip4_address_full (const char *address, const char *peer_address, guint plen,
|
|
|
|
|
int ifindex, NMIPConfigSource source, guint32 timestamp,
|
|
|
|
|
guint32 lifetime, guint32 preferred, guint32 flags,
|
|
|
|
|
const char *label)
|
|
|
|
|
{
|
|
|
|
|
NMPlatformIP4Address *addr = nmtst_platform_ip4_address (address, peer_address, plen);
|
|
|
|
|
|
2018-11-12 15:23:36 +01:00
|
|
|
G_STATIC_ASSERT (NMP_IFNAMSIZ == sizeof (addr->label));
|
|
|
|
|
g_assert (!label || strlen (label) < NMP_IFNAMSIZ);
|
2016-05-17 14:04:28 +02:00
|
|
|
|
|
|
|
|
addr->ifindex = ifindex;
|
|
|
|
|
addr->addr_source = source;
|
|
|
|
|
addr->timestamp = timestamp;
|
|
|
|
|
addr->lifetime = lifetime;
|
|
|
|
|
addr->preferred = preferred;
|
|
|
|
|
addr->n_ifa_flags = flags;
|
|
|
|
|
if (label)
|
|
|
|
|
g_strlcpy (addr->label, label, sizeof (addr->label));
|
|
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline NMPlatformIP6Address *
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_platform_ip6_address (const char *address, const char *peer_address, guint plen)
|
|
|
|
|
{
|
|
|
|
|
static NMPlatformIP6Address addr;
|
|
|
|
|
|
|
|
|
|
g_assert (plen <= 128);
|
|
|
|
|
|
|
|
|
|
memset (&addr, 0, sizeof (addr));
|
|
|
|
|
addr.address = *nmtst_inet6_from_string (address);
|
|
|
|
|
addr.peer_address = *nmtst_inet6_from_string (peer_address);
|
|
|
|
|
addr.plen = plen;
|
|
|
|
|
|
|
|
|
|
return &addr;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline NMPlatformIP6Address *
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_platform_ip6_address_full (const char *address, const char *peer_address, guint plen,
|
|
|
|
|
int ifindex, NMIPConfigSource source, guint32 timestamp,
|
|
|
|
|
guint32 lifetime, guint32 preferred, guint32 flags)
|
|
|
|
|
{
|
|
|
|
|
NMPlatformIP6Address *addr = nmtst_platform_ip6_address (address, peer_address, plen);
|
|
|
|
|
|
|
|
|
|
addr->ifindex = ifindex;
|
|
|
|
|
addr->addr_source = source;
|
|
|
|
|
addr->timestamp = timestamp;
|
|
|
|
|
addr->lifetime = lifetime;
|
|
|
|
|
addr->preferred = preferred;
|
|
|
|
|
addr->n_ifa_flags = flags;
|
|
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline NMPlatformIP4Route *
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_platform_ip4_route (const char *network, guint plen, const char *gateway)
|
|
|
|
|
{
|
|
|
|
|
static NMPlatformIP4Route route;
|
|
|
|
|
|
|
|
|
|
g_assert (plen <= 32);
|
|
|
|
|
|
|
|
|
|
memset (&route, 0, sizeof (route));
|
|
|
|
|
route.network = nmtst_inet4_from_string (network);
|
|
|
|
|
route.plen = plen;
|
|
|
|
|
route.gateway = nmtst_inet4_from_string (gateway);
|
|
|
|
|
|
|
|
|
|
return &route;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline NMPlatformIP4Route *
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_platform_ip4_route_full (const char *network, guint plen, const char *gateway,
|
|
|
|
|
int ifindex, NMIPConfigSource source,
|
|
|
|
|
guint metric, guint mss,
|
|
|
|
|
guint8 scope,
|
|
|
|
|
const char *pref_src)
|
|
|
|
|
{
|
|
|
|
|
NMPlatformIP4Route *route = nmtst_platform_ip4_route (network, plen, gateway);
|
|
|
|
|
|
|
|
|
|
route->ifindex = ifindex;
|
|
|
|
|
route->rt_source = source;
|
|
|
|
|
route->metric = metric;
|
|
|
|
|
route->mss = mss;
|
|
|
|
|
route->scope_inv = nm_platform_route_scope_inv (scope);
|
|
|
|
|
route->pref_src = nmtst_inet4_from_string (pref_src);
|
|
|
|
|
|
|
|
|
|
return route;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline NMPlatformIP6Route *
|
2017-02-09 17:26:04 +01:00
|
|
|
nmtst_platform_ip6_route (const char *network, guint plen, const char *gateway, const char *pref_src)
|
2016-05-17 14:04:28 +02:00
|
|
|
{
|
|
|
|
|
static NMPlatformIP6Route route;
|
|
|
|
|
|
|
|
|
|
nm_assert (plen <= 128);
|
|
|
|
|
|
|
|
|
|
memset (&route, 0, sizeof (route));
|
|
|
|
|
route.network = *nmtst_inet6_from_string (network);
|
|
|
|
|
route.plen = plen;
|
|
|
|
|
route.gateway = *nmtst_inet6_from_string (gateway);
|
2017-02-09 17:26:04 +01:00
|
|
|
route.pref_src = *nmtst_inet6_from_string (pref_src);
|
2016-05-17 14:04:28 +02:00
|
|
|
|
|
|
|
|
return &route;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline NMPlatformIP6Route *
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_platform_ip6_route_full (const char *network, guint plen, const char *gateway,
|
|
|
|
|
int ifindex, NMIPConfigSource source,
|
|
|
|
|
guint metric, guint mss)
|
|
|
|
|
{
|
2017-02-09 17:26:04 +01:00
|
|
|
NMPlatformIP6Route *route = nmtst_platform_ip6_route (network, plen, gateway, NULL);
|
2016-05-17 14:04:28 +02:00
|
|
|
|
|
|
|
|
route->ifindex = ifindex;
|
|
|
|
|
route->rt_source = source;
|
|
|
|
|
route->metric = metric;
|
|
|
|
|
route->mss = mss;
|
|
|
|
|
|
|
|
|
|
return route;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline int
|
2016-05-17 14:04:28 +02:00
|
|
|
_nmtst_platform_ip4_routes_equal_sort (gconstpointer a, gconstpointer b, gpointer user_data)
|
|
|
|
|
{
|
2017-07-26 10:50:23 +02:00
|
|
|
return nm_platform_ip4_route_cmp_full ((const NMPlatformIP4Route *) a, (const NMPlatformIP4Route *) b);
|
2016-05-17 14:04:28 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline void
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_platform_ip4_routes_equal (const NMPlatformIP4Route *a, const NMPlatformIP4Route *b, gsize len, gboolean ignore_order)
|
|
|
|
|
{
|
|
|
|
|
gsize i;
|
|
|
|
|
gs_free const NMPlatformIP4Route *c_a = NULL, *c_b = NULL;
|
|
|
|
|
|
|
|
|
|
g_assert (a);
|
|
|
|
|
g_assert (b);
|
|
|
|
|
|
|
|
|
|
if (ignore_order) {
|
2018-09-05 08:08:38 +02:00
|
|
|
a = c_a = nm_memdup (a, sizeof (NMPlatformIP4Route) * len);
|
|
|
|
|
b = c_b = nm_memdup (b, sizeof (NMPlatformIP4Route) * len);
|
2016-05-17 14:04:28 +02:00
|
|
|
g_qsort_with_data (c_a, len, sizeof (NMPlatformIP4Route), _nmtst_platform_ip4_routes_equal_sort, NULL);
|
|
|
|
|
g_qsort_with_data (c_b, len, sizeof (NMPlatformIP4Route), _nmtst_platform_ip4_routes_equal_sort, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2017-07-26 10:50:23 +02:00
|
|
|
if (nm_platform_ip4_route_cmp_full (&a[i], &b[i]) != 0) {
|
2016-05-17 14:04:28 +02:00
|
|
|
char buf[sizeof (_nm_utils_to_string_buffer)];
|
|
|
|
|
|
2017-03-14 11:15:05 +01:00
|
|
|
g_error ("Error comparing IPv4 route[%lu]: %s vs %s", (unsigned long) i,
|
2016-05-17 14:04:28 +02:00
|
|
|
nm_platform_ip4_route_to_string (&a[i], NULL, 0),
|
|
|
|
|
nm_platform_ip4_route_to_string (&b[i], buf, sizeof (buf)));
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-29 15:19:35 +02:00
|
|
|
#ifdef __NMP_OBJECT_H__
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
|
nmtst_platform_ip4_routes_equal_aptr (const NMPObject *const*a, const NMPlatformIP4Route *b, gsize len, gboolean ignore_order)
|
|
|
|
|
{
|
|
|
|
|
gsize i;
|
|
|
|
|
gs_free NMPlatformIP4Route *c_a = NULL;
|
|
|
|
|
|
|
|
|
|
g_assert (len > 0);
|
|
|
|
|
g_assert (a);
|
|
|
|
|
|
|
|
|
|
c_a = g_new (NMPlatformIP4Route, len);
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
c_a[i] = *NMP_OBJECT_CAST_IP4_ROUTE (a[i]);
|
|
|
|
|
nmtst_platform_ip4_routes_equal (c_a, b, len, ignore_order);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline int
|
2016-05-17 14:04:28 +02:00
|
|
|
_nmtst_platform_ip6_routes_equal_sort (gconstpointer a, gconstpointer b, gpointer user_data)
|
|
|
|
|
{
|
2017-07-26 10:50:23 +02:00
|
|
|
return nm_platform_ip6_route_cmp_full ((const NMPlatformIP6Route *) a, (const NMPlatformIP6Route *) b);
|
2016-05-17 14:04:28 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:24:18 +01:00
|
|
|
static inline void
|
2016-05-17 14:04:28 +02:00
|
|
|
nmtst_platform_ip6_routes_equal (const NMPlatformIP6Route *a, const NMPlatformIP6Route *b, gsize len, gboolean ignore_order)
|
|
|
|
|
{
|
|
|
|
|
gsize i;
|
|
|
|
|
gs_free const NMPlatformIP6Route *c_a = NULL, *c_b = NULL;
|
|
|
|
|
|
|
|
|
|
g_assert (a);
|
|
|
|
|
g_assert (b);
|
|
|
|
|
|
|
|
|
|
if (ignore_order) {
|
2018-09-05 08:08:38 +02:00
|
|
|
a = c_a = nm_memdup (a, sizeof (NMPlatformIP6Route) * len);
|
|
|
|
|
b = c_b = nm_memdup (b, sizeof (NMPlatformIP6Route) * len);
|
2016-05-17 14:04:28 +02:00
|
|
|
g_qsort_with_data (c_a, len, sizeof (NMPlatformIP6Route), _nmtst_platform_ip6_routes_equal_sort, NULL);
|
|
|
|
|
g_qsort_with_data (c_b, len, sizeof (NMPlatformIP6Route), _nmtst_platform_ip6_routes_equal_sort, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2017-07-26 10:50:23 +02:00
|
|
|
if (nm_platform_ip6_route_cmp_full (&a[i], &b[i]) != 0) {
|
2016-05-17 14:04:28 +02:00
|
|
|
char buf[sizeof (_nm_utils_to_string_buffer)];
|
|
|
|
|
|
2017-03-14 11:15:05 +01:00
|
|
|
g_error ("Error comparing IPv6 route[%lu]: %s vs %s", (unsigned long) i,
|
2016-05-17 14:04:28 +02:00
|
|
|
nm_platform_ip6_route_to_string (&a[i], NULL, 0),
|
|
|
|
|
nm_platform_ip6_route_to_string (&b[i], buf, sizeof (buf)));
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-29 15:19:35 +02:00
|
|
|
#ifdef __NMP_OBJECT_H__
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
|
nmtst_platform_ip6_routes_equal_aptr (const NMPObject *const*a, const NMPlatformIP6Route *b, gsize len, gboolean ignore_order)
|
|
|
|
|
{
|
|
|
|
|
gsize i;
|
|
|
|
|
gs_free NMPlatformIP6Route *c_a = NULL;
|
|
|
|
|
|
|
|
|
|
g_assert (len > 0);
|
|
|
|
|
g_assert (a);
|
|
|
|
|
|
|
|
|
|
c_a = g_new (NMPlatformIP6Route, len);
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
c_a[i] = *NMP_OBJECT_CAST_IP6_ROUTE (a[i]);
|
|
|
|
|
nmtst_platform_ip6_routes_equal (c_a, b, len, ignore_order);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-05-17 14:04:28 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef __NETWORKMANAGER_IP4_CONFIG_H__
|
|
|
|
|
|
2019-04-15 08:16:00 +02:00
|
|
|
#include "nm-glib-aux/nm-dedup-multi.h"
|
core: pass NMDedupMultiIndex instance to NMIP4Config and other
NMIP4Config, NMIP6Config, and NMPlatform shall share one
NMDedupMultiIndex instance.
For that, pass an NMDedupMultiIndex instance to NMPlatform and NMNetns.
NMNetns than passes it on to NMDevice, NMDhcpClient, NMIP4Config and NMIP6Config.
So currently NMNetns is the access point to the shared NMDedupMultiIndex
instance, and it gets it from it's NMPlatform instance.
The NMDedupMultiIndex instance is really a singleton, we don't want
multiple instances of it. However, for testing, instead of adding a
singleton instance, pass the instance explicitly around.
2017-06-12 08:16:47 +02:00
|
|
|
|
|
|
|
|
static inline NMIP4Config *
|
|
|
|
|
nmtst_ip4_config_new (int ifindex)
|
|
|
|
|
{
|
|
|
|
|
nm_auto_unref_dedup_multi_index NMDedupMultiIndex *multi_idx = nm_dedup_multi_index_new ();
|
|
|
|
|
|
|
|
|
|
return nm_ip4_config_new (multi_idx, ifindex);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-17 14:04:28 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef __NETWORKMANAGER_IP6_CONFIG_H__
|
|
|
|
|
|
2019-04-15 08:16:00 +02:00
|
|
|
#include "nm-glib-aux/nm-dedup-multi.h"
|
core: pass NMDedupMultiIndex instance to NMIP4Config and other
NMIP4Config, NMIP6Config, and NMPlatform shall share one
NMDedupMultiIndex instance.
For that, pass an NMDedupMultiIndex instance to NMPlatform and NMNetns.
NMNetns than passes it on to NMDevice, NMDhcpClient, NMIP4Config and NMIP6Config.
So currently NMNetns is the access point to the shared NMDedupMultiIndex
instance, and it gets it from it's NMPlatform instance.
The NMDedupMultiIndex instance is really a singleton, we don't want
multiple instances of it. However, for testing, instead of adding a
singleton instance, pass the instance explicitly around.
2017-06-12 08:16:47 +02:00
|
|
|
|
|
|
|
|
static inline NMIP6Config *
|
|
|
|
|
nmtst_ip6_config_new (int ifindex)
|
|
|
|
|
{
|
|
|
|
|
nm_auto_unref_dedup_multi_index NMDedupMultiIndex *multi_idx = nm_dedup_multi_index_new ();
|
|
|
|
|
|
|
|
|
|
return nm_ip6_config_new (multi_idx, ifindex);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-17 14:04:28 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif /* __NM_TEST_UTILS_CORE_H__ */
|