nm-test-utils-core.h 10.2 KB
Newer Older
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2014 - 2016 Red Hat, Inc.
 */

#ifndef __NM_TEST_UTILS_CORE_H__
#define __NM_TEST_UTILS_CORE_H__

#include "NetworkManagerUtils.h"
#include "nm-keyfile-internal.h"

#define _NMTST_INSIDE_CORE 1

29
#include "nm-utils/nm-test-utils.h"
30 31 32

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

33 34 35 36 37 38 39 40
#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)

41
static inline void
42 43 44 45
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);
}
46
static inline void
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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__

65
static inline NMPlatformIP4Address *
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
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;
}

83
static inline NMPlatformIP4Address *
84 85 86 87 88 89 90
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);

91 92
	G_STATIC_ASSERT (NMP_IFNAMSIZ == sizeof (addr->label));
	g_assert (!label || strlen (label) < NMP_IFNAMSIZ);
93 94 95 96 97 98 99 100 101 102 103 104 105

	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;
}

106
static inline NMPlatformIP6Address *
107 108 109 110 111 112 113 114 115 116 117 118 119 120
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;
}

121
static inline NMPlatformIP6Address *
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
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;
}

138
static inline NMPlatformIP4Route *
139 140 141 142 143 144 145 146 147 148 149 150 151 152
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;
}

153
static inline NMPlatformIP4Route *
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
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;
}

172
static inline NMPlatformIP6Route *
173
nmtst_platform_ip6_route (const char *network, guint plen, const char *gateway, const char *pref_src)
174 175 176 177 178 179 180 181 182
{
	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);
183
	route.pref_src = *nmtst_inet6_from_string (pref_src);
184 185 186 187

	return &route;
}

188
static inline NMPlatformIP6Route *
189 190 191 192
nmtst_platform_ip6_route_full (const char *network, guint plen, const char *gateway,
                               int ifindex, NMIPConfigSource source,
                               guint metric, guint mss)
{
193
	NMPlatformIP6Route *route = nmtst_platform_ip6_route (network, plen, gateway, NULL);
194 195 196 197 198 199 200 201 202

	route->ifindex = ifindex;
	route->rt_source = source;
	route->metric = metric;
	route->mss = mss;

	return route;
}

203
static inline int
204 205
_nmtst_platform_ip4_routes_equal_sort (gconstpointer a, gconstpointer b, gpointer user_data)
{
206
	return nm_platform_ip4_route_cmp_full ((const NMPlatformIP4Route *) a, (const NMPlatformIP4Route *) b);
207 208
}

209
static inline void
210 211 212 213 214 215 216 217 218
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) {
Thomas Haller's avatar
Thomas Haller committed
219 220
		a = c_a = nm_memdup (a, sizeof (NMPlatformIP4Route) * len);
		b = c_b = nm_memdup (b, sizeof (NMPlatformIP4Route) * len);
221 222 223 224 225
		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++) {
226
		if (nm_platform_ip4_route_cmp_full (&a[i], &b[i]) != 0) {
227 228
			char buf[sizeof (_nm_utils_to_string_buffer)];

229
			g_error ("Error comparing IPv4 route[%lu]: %s vs %s", (unsigned long) i,
230 231 232 233 234 235 236
			         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 ();
		}
	}
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
#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

256
static inline int
257 258
_nmtst_platform_ip6_routes_equal_sort (gconstpointer a, gconstpointer b, gpointer user_data)
{
259
	return nm_platform_ip6_route_cmp_full ((const NMPlatformIP6Route *) a, (const NMPlatformIP6Route *) b);
260 261
}

262
static inline void
263 264 265 266 267 268 269 270 271
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) {
Thomas Haller's avatar
Thomas Haller committed
272 273
		a = c_a = nm_memdup (a, sizeof (NMPlatformIP6Route) * len);
		b = c_b = nm_memdup (b, sizeof (NMPlatformIP6Route) * len);
274 275 276 277 278
		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++) {
279
		if (nm_platform_ip6_route_cmp_full (&a[i], &b[i]) != 0) {
280 281
			char buf[sizeof (_nm_utils_to_string_buffer)];

282
			g_error ("Error comparing IPv6 route[%lu]: %s vs %s", (unsigned long) i,
283 284 285 286 287 288 289
			         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 ();
		}
	}
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
#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

309 310 311 312
#endif

#ifdef __NETWORKMANAGER_IP4_CONFIG_H__

313 314 315 316 317 318 319 320 321 322
#include "nm-utils/nm-dedup-multi.h"

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);
}

323 324 325 326
#endif

#ifdef __NETWORKMANAGER_IP6_CONFIG_H__

327 328 329 330 331 332 333 334 335 336
#include "nm-utils/nm-dedup-multi.h"

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);
}

337 338 339
#endif

#endif /* __NM_TEST_UTILS_CORE_H__ */