test-dispatcher-envp.c 17.1 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 29 30 31
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2011 Red Hat, Inc.
 *
 */

#include <config.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>

#include "nm-connection.h"
#include "nm-setting-connection.h"
#include "nm-dispatcher-utils.h"
#include "nm-dbus-glib-types.h"
32
#include "nm-dispatcher-api.h"
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
#include "nm-utils.h"

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

static void
value_destroy (gpointer data)
{
	GValue *value = (GValue *) data;

	g_value_unset (value);
	g_slice_free (GValue, value);
}

static GHashTable *
value_hash_create (void)
{
	return g_hash_table_new_full (g_str_hash, g_str_equal, g_free, value_destroy);
}

static void
value_hash_add (GHashTable *hash,
				const char *key,
				GValue *value)
{
	g_hash_table_insert (hash, g_strdup (key), value);
}

static void
value_hash_add_string (GHashTable *hash,
					   const char *key,
					   const char *str)
{
	GValue *value;

	value = g_slice_new0 (GValue);
	g_value_init (value, G_TYPE_STRING);
	g_value_set_string (value, str);

	value_hash_add (hash, key, value);
}

static void
value_hash_add_object_path (GHashTable *hash,
							const char *key,
							const char *op)
{
	GValue *value;

	value = g_slice_new0 (GValue);
	g_value_init (value, DBUS_TYPE_G_OBJECT_PATH);
	g_value_set_boxed (value, op);

	value_hash_add (hash, key, value);
}

static void
value_hash_add_uint (GHashTable *hash,
					 const char *key,
					 guint32 val)
{
	GValue *value;

	value = g_slice_new0 (GValue);
	g_value_init (value, G_TYPE_UINT);
	g_value_set_uint (value, val);

	value_hash_add (hash, key, value);
}

static void
value_hash_add_strv (GHashTable *hash,
104 105
                     const char *key,
                     char **strv)
106 107 108 109
{
	GValue *value;

	value = g_slice_new0 (GValue);
110 111
	g_value_init (value, G_TYPE_STRV);
	g_value_take_boxed (value, strv);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	value_hash_add (hash, key, value);
}

static void
value_hash_add_uint_array (GHashTable *hash,
					       const char *key,
					       GArray *array)
{
	GValue *value;

	value = g_slice_new0 (GValue);
	g_value_init (value, DBUS_TYPE_G_UINT_ARRAY);
	g_value_take_boxed (value, array);
	value_hash_add (hash, key, value);
}

static gboolean
parse_main (GKeyFile *kf,
            GHashTable **out_con_hash,
            GHashTable **out_con_props,
132
            char **out_expected_iface,
133
            char **out_action,
134
            char **out_vpn_ip_iface,
135 136 137 138 139 140
            GError **error)
{
	char *uuid, *id;
	NMConnection *connection;
	NMSettingConnection *s_con;

141 142
	*out_expected_iface = g_key_file_get_string (kf, "main", "expected-iface", error);
	if (*out_expected_iface == NULL)
143 144
		return FALSE;

145 146
	*out_vpn_ip_iface = g_key_file_get_string (kf, "main", "vpn-ip-iface", NULL);

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
	*out_action = g_key_file_get_string (kf, "main", "action", error);
	if (*out_action == NULL)
		return FALSE;

	uuid = g_key_file_get_string (kf, "main", "uuid", error);
	if (uuid == NULL)
		return FALSE;
	id = g_key_file_get_string (kf, "main", "id", error);
	if (id == NULL)
		return FALSE;

	connection = nm_connection_new ();
	g_assert (connection);
	s_con = (NMSettingConnection *) nm_setting_connection_new ();
	g_assert (s_con);
	g_object_set (s_con,
	              NM_SETTING_CONNECTION_UUID, uuid,
	              NM_SETTING_CONNECTION_ID, id,
	              NULL);
	g_free (uuid);
	g_free (id);
	nm_connection_add_setting (connection, NM_SETTING (s_con));

	*out_con_hash = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_ALL);
	g_object_unref (connection);

	*out_con_props = value_hash_create ();
	value_hash_add_object_path (*out_con_props, "connection-path", "/org/freedesktop/NetworkManager/Connections/5");

	return TRUE;
}

static gboolean
parse_device (GKeyFile *kf, GHashTable **out_device_props, GError **error)
{
	char *tmp;
	gint i;

	*out_device_props = value_hash_create ();

	i = g_key_file_get_integer (kf, "device", "state", error);
	if (i == 0)
		return FALSE;
	value_hash_add_uint (*out_device_props, NMD_DEVICE_PROPS_STATE, (guint) i);

	i = g_key_file_get_integer (kf, "device", "type", error);
	if (i == 0)
		return FALSE;
	value_hash_add_uint (*out_device_props, NMD_DEVICE_PROPS_TYPE, (guint) i);

	tmp = g_key_file_get_string (kf, "device", "interface", error);
	if (tmp == NULL)
		return FALSE;
	value_hash_add_string (*out_device_props, NMD_DEVICE_PROPS_INTERFACE, tmp);
	g_free (tmp);

	tmp = g_key_file_get_string (kf, "device", "ip-interface", error);
	if (tmp == NULL)
		return FALSE;
	value_hash_add_string (*out_device_props, NMD_DEVICE_PROPS_IP_INTERFACE, tmp);
	g_free (tmp);

	tmp = g_key_file_get_string (kf, "device", "path", error);
	if (tmp == NULL)
		return FALSE;
	value_hash_add_object_path (*out_device_props, NMD_DEVICE_PROPS_PATH, tmp);
	g_free (tmp);

	return TRUE;
}

static gboolean
add_uint_array (GKeyFile *kf,
                GHashTable *props,
                const char *section,
                const char *key,
                GError **error)
{
	char *tmp;
	char **split, **iter;
	GArray *items;

	tmp = g_key_file_get_string (kf, section, key, error);
	if (tmp == NULL) {
		g_clear_error (error);
		return TRUE;
	}
	split = g_strsplit_set (tmp, " ", -1);
	g_free (tmp);

	if (g_strv_length (split) > 0) {
		items = g_array_sized_new (FALSE, TRUE, sizeof (guint32), g_strv_length (split));
		for (iter = split; iter && *iter; iter++) {
			if (strlen (g_strstrip (*iter))) {
241
				guint32 addr;
242 243

				g_assert_cmpint (inet_pton (AF_INET, *iter, &addr), ==, 1);
244
				g_array_append_val (items, addr);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
			}
		}
		value_hash_add_uint_array (props, key, items);
	}
	g_strfreev (split);
	return TRUE;
}

static gboolean
parse_ip4 (GKeyFile *kf, GHashTable **out_props, const char *section, GError **error)
{
	char *tmp;
	char **split, **iter;
	GSList *list;
	GValue *val;

	*out_props = value_hash_create ();

	/* search domains */
264 265 266
	/* Use char** for domains. (DBUS_TYPE_G_ARRAY_OF_STRING of NMIP4Config
	 * becomes G_TYPE_STRV when sending the value over D-Bus)
	 */
267 268 269 270 271 272 273
	tmp = g_key_file_get_string (kf, section, "domains", error);
	if (tmp == NULL)
		return FALSE;
	split = g_strsplit_set (tmp, " ", -1);
	g_free (tmp);

	if (g_strv_length (split) > 0) {
274 275 276
		for (iter = split; iter && *iter; iter++)
			g_strstrip (*iter);
		value_hash_add_strv (*out_props, "domains", split);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	}

	/* nameservers */
	if (!add_uint_array (kf, *out_props, "ip4", "nameservers", error))
		return FALSE;
	/* wins-servers */
	if (!add_uint_array (kf, *out_props, "ip4", "wins-servers", error))
		return FALSE;

	/* Addresses */
	tmp = g_key_file_get_string (kf, section, "addresses", error);
	if (tmp == NULL)
		return FALSE;
	split = g_strsplit_set (tmp, ",", -1);
	g_free (tmp);

	if (g_strv_length (split) > 0) {
		list = NULL;
		for (iter = split; iter && *iter; iter++) {
			NMIP4Address *addr;
297
			guint32 a;
298 299 300 301 302 303 304 305 306 307 308 309
			char *p;

			if (strlen (g_strstrip (*iter)) == 0)
				continue;

			addr = nm_ip4_address_new ();

			p = strchr (*iter, '/');
			g_assert (p);
			*p++ = '\0';

			g_assert_cmpint (inet_pton (AF_INET, *iter, &a), ==, 1);
310
			nm_ip4_address_set_address (addr, a);
311 312 313 314 315 316 317
			nm_ip4_address_set_prefix (addr, (guint) atoi (p));

			p = strchr (p, ' ');
			g_assert (p);
			p++;

			g_assert_cmpint (inet_pton (AF_INET, p, &a), ==, 1);
318
			nm_ip4_address_set_gateway (addr, a);
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

			list = g_slist_append (list, addr);
		}

		val = g_slice_new0 (GValue);
		g_value_init (val, DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT);
		nm_utils_ip4_addresses_to_gvalue (list, val);
		value_hash_add (*out_props, "addresses", val);
	}
	g_strfreev (split);

	/* Routes */
	tmp = g_key_file_get_string (kf, section, "routes", error);
	g_clear_error (error);
	if (tmp) {
		split = g_strsplit_set (tmp, ",", -1);
		g_free (tmp);

		if (g_strv_length (split) > 0) {
			list = NULL;
			for (iter = split; iter && *iter; iter++) {
				NMIP4Route *route;
341
				guint32 a;
342 343 344 345 346 347 348 349 350 351 352 353
				char *p;

				if (strlen (g_strstrip (*iter)) == 0)
					continue;

				route = nm_ip4_route_new ();

				p = strchr (*iter, '/');
				g_assert (p);
				*p++ = '\0';

				g_assert_cmpint (inet_pton (AF_INET, *iter, &a), ==, 1);
354
				nm_ip4_route_set_dest (route, a);
355 356 357 358 359 360 361
				nm_ip4_route_set_prefix (route, (guint) atoi (p));

				p = strchr (p, ' ');
				g_assert (p);
				p++;

				g_assert_cmpint (inet_pton (AF_INET, p, &a), ==, 1);
362
				nm_ip4_route_set_next_hop (route, a);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

				p = strchr (p, ' ');
				g_assert (p);
				p++;
				nm_ip4_route_set_metric (route, (guint) atoi (p));

				list = g_slist_append (list, route);
			}

			val = g_slice_new0 (GValue);
			g_value_init (val, DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT);
			nm_utils_ip4_routes_to_gvalue (list, val);
			value_hash_add (*out_props, "routes", val);
		}
		g_strfreev (split);
	}

	return TRUE;
}

static gboolean
parse_dhcp (GKeyFile *kf,
            const char *group_name,
            GHashTable **out_props,
            GError **error)
{
	char **keys, **iter, *val;

	keys = g_key_file_get_keys (kf, group_name, NULL, error);
	if (!keys)
		return FALSE;

	*out_props = value_hash_create ();
	for (iter = keys; iter && *iter; iter++) {
		val = g_key_file_get_string (kf, group_name, *iter, error);
		if (!val)
			return FALSE;
		value_hash_add_string (*out_props, *iter, val);
		g_free (val);
	}

	return TRUE;
}

static gboolean
get_dispatcher_file (const char *file,
                     GHashTable **out_con_hash,
                     GHashTable **out_con_props,
                     GHashTable **out_device_props,
                     GHashTable **out_device_ip4_props,
                     GHashTable **out_device_ip6_props,
                     GHashTable **out_device_dhcp4_props,
                     GHashTable **out_device_dhcp6_props,
416
                     char **out_vpn_ip_iface,
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
                     GHashTable **out_vpn_ip4_props,
                     GHashTable **out_vpn_ip6_props,
                     char **out_expected_iface,
                     char **out_action,
                     GHashTable **out_env,
                     GError **error)
{
	GKeyFile *kf;
	gboolean success = FALSE;
	char **keys, **iter, *val;

	kf = g_key_file_new ();
	if (!g_key_file_load_from_file (kf, file, G_KEY_FILE_NONE, error))
		return FALSE;

432 433 434 435 436 437 438
	if (!parse_main (kf,
	                 out_con_hash,
	                 out_con_props,
	                 out_expected_iface,
	                 out_action,
	                 out_vpn_ip_iface,
	                 error))
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
		goto out;

	if (!parse_device (kf, out_device_props, error))
		goto out;

	if (g_key_file_has_group (kf, "ip4")) {
		if (!parse_ip4 (kf, out_device_ip4_props, "ip4", error))
			goto out;
	}

	if (g_key_file_has_group (kf, "dhcp4")) {
		if (!parse_dhcp (kf, "dhcp4", out_device_dhcp4_props, error))
			goto out;
	}

	if (g_key_file_has_group (kf, "dhcp6")) {
		if (!parse_dhcp (kf, "dhcp6", out_device_dhcp4_props, error))
			goto out;
	}

	g_assert (g_key_file_has_group (kf, "env"));
	keys = g_key_file_get_keys (kf, "env", NULL, error);
	*out_env = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	for (iter = keys; iter && *iter; iter++) {
		val = g_key_file_get_string (kf, "env", *iter, error);
		if (!val)
			goto out;
		g_hash_table_insert (*out_env,
		                     g_strdup_printf ("%s=%s", *iter, val),
		                     GUINT_TO_POINTER (1));
		g_free (val);
	}
	g_strfreev (keys);

	success = TRUE;

out:
	g_key_file_free (kf);
	return success;
}

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

static void
483
test_generic (const char *path, const char *file, const char *override_vpn_ip_iface)
484 485 486 487 488 489 490 491
{
	GHashTable *con_hash = NULL;
	GHashTable *con_props = NULL;
	GHashTable *device_props = NULL;
	GHashTable *device_ip4_props = NULL;
	GHashTable *device_ip6_props = NULL;
	GHashTable *device_dhcp4_props = NULL;
	GHashTable *device_dhcp6_props = NULL;
492
	char *vpn_ip_iface = NULL;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	GHashTable *vpn_ip4_props = NULL;
	GHashTable *vpn_ip6_props = NULL;
	char *expected_iface = NULL;
	char *action = NULL;
	char *out_iface = NULL;
	GHashTable *expected_env = NULL;
	GError *error = NULL;
	gboolean success;
	char *p;
	char **denv, **iter;

	/* Read in the test file */
	p = g_strdup_printf ("%s/%s", path, file);
	success = get_dispatcher_file (p,
	                               &con_hash,
	                               &con_props,
	                               &device_props,
	                               &device_ip4_props,
	                               &device_ip6_props,
	                               &device_dhcp4_props,
	                               &device_dhcp6_props,
	                               &vpn_ip_iface,
	                               &vpn_ip4_props,
	                               &vpn_ip6_props,
	                               &expected_iface,
	                               &action,
	                               &expected_env,
	                               &error);
	g_free (p);
	g_assert_no_error (error);
	g_assert (success);

	/* Get the environment from the dispatcher code */
	denv = nm_dispatcher_utils_construct_envp (action,
	                                           con_hash,
	                                           con_props,
	                                           device_props,
	                                           device_ip4_props,
	                                           device_ip6_props,
	                                           device_dhcp4_props,
	                                           device_dhcp6_props,
534
	                                           override_vpn_ip_iface ? override_vpn_ip_iface : vpn_ip_iface,
535 536 537 538 539
	                                           vpn_ip4_props,
	                                           vpn_ip6_props,
	                                           &out_iface);

	/* Print out environment for now */
540 541
#ifdef DEBUG
	g_message ("\n******* Generated environment:");
542 543
	for (iter = denv; iter && *iter; iter++)
		g_message ("   %s", *iter);
544
#endif
545

546
#ifdef DEBUG
547 548 549
	{
		GHashTableIter k;
		const char *key;
550 551

		g_message ("\n******* Expected environment:");
552 553 554 555
		g_hash_table_iter_init (&k, expected_env);
		while (g_hash_table_iter_next (&k, (gpointer) &key, NULL))
			g_message ("   %s", key);
	}
556 557 558
#endif

	g_assert_cmpint (g_strv_length (denv), ==, g_hash_table_size (expected_env));
559 560 561 562

	/* Compare dispatcher generated env and expected env */
	for (iter = denv; iter && *iter; iter++) {
		gpointer foo;
563
		const char *i_value = *iter;
564

565 566 567 568 569 570 571 572
		if (strstr (i_value, "PATH=") == i_value) {
			g_assert_cmpstr (&i_value[strlen("PATH=")], ==, g_getenv ("PATH"));

			/* The path is constructed dynamically. Ignore the actual value. */
			i_value = "PATH=";
		}

		foo = g_hash_table_lookup (expected_env, i_value);
573
		if (!foo)
574
			g_warning ("Failed to find %s in environment", i_value);
575 576 577 578
		g_assert (foo);
	}

	g_assert_cmpstr (expected_iface, ==, out_iface);
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599

	g_free (out_iface);
	g_free (vpn_ip_iface);
	g_free (expected_iface);
	g_free (action);
	g_hash_table_destroy (con_hash);
	g_hash_table_destroy (con_props);
	g_hash_table_destroy (device_props);
	if (device_ip4_props)
		g_hash_table_destroy (device_ip4_props);
	if (device_ip6_props)
		g_hash_table_destroy (device_ip6_props);
	if (device_dhcp4_props)
		g_hash_table_destroy (device_dhcp4_props);
	if (device_dhcp6_props)
		g_hash_table_destroy (device_dhcp6_props);
	if (vpn_ip4_props)
		g_hash_table_destroy (vpn_ip4_props);
	if (vpn_ip6_props)
		g_hash_table_destroy (vpn_ip6_props);
	g_hash_table_destroy (expected_env);
600 601 602 603 604 605 606
}

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

static void
test_old_up (const char *path)
{
607
	test_generic (path, "dispatcher-old-up", NULL);
608 609 610 611 612
}

static void
test_old_down (const char *path)
{
613
	test_generic (path, "dispatcher-old-down", NULL);
614 615 616 617 618
}

static void
test_old_vpn_up (const char *path)
{
619
	test_generic (path, "dispatcher-old-vpn-up", NULL);
620 621 622 623 624
}

static void
test_old_vpn_down (const char *path)
{
625 626 627 628 629 630 631 632 633 634
	test_generic (path, "dispatcher-old-vpn-down", NULL);
}

static void
test_up_empty_vpn_iface (const char *path)
{
	/* Test that an empty VPN iface variable, like is passed through D-Bus
	 * from NM, is ignored by the dispatcher environment construction code.
	 */
	test_generic (path, "dispatcher-old-up", "");
635 636 637 638
}

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

Dan Winship's avatar
Dan Winship committed
639 640
int
main (int argc, char **argv)
641 642 643 644
{
	g_assert (argc > 1);

	g_test_init (&argc, &argv, NULL);
645 646

#if !GLIB_CHECK_VERSION (2, 35, 0)
647
	g_type_init ();
648
#endif
649

Dan Winship's avatar
Dan Winship committed
650 651 652 653
	g_test_add_data_func ("/dispatcher/old_up", argv[1], (GTestDataFunc) test_old_up);
	g_test_add_data_func ("/dispatcher/old_down", argv[1], (GTestDataFunc) test_old_down);
	g_test_add_data_func ("/dispatcher/old_vpn_up", argv[1], (GTestDataFunc) test_old_vpn_up);
	g_test_add_data_func ("/dispatcher/old_vpn_down", argv[1], (GTestDataFunc) test_old_vpn_down);
654

Dan Winship's avatar
Dan Winship committed
655
	g_test_add_data_func ("/dispatcher/up_empty_vpn_iface", argv[1], (GTestDataFunc) test_up_empty_vpn_iface);
656

657 658 659
	return g_test_run ();
}