test-general.c 85.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* -*- 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.
 *
18
 * Copyright 2008 - 2011 Red Hat, Inc.
19 20 21
 *
 */

22
#include "nm-default.h"
23

24 25
#include <dbus/dbus-glib.h>
#include <string.h>
26 27
#include <netinet/ether.h>
#include <linux/if_infiniband.h>
28
#include <sys/wait.h>
29
#include <sys/resource.h>
30

31
#include "nm-utils.h"
Dan Winship's avatar
Dan Winship committed
32
#include "nm-gvaluearray-compat.h"
33

34
#include "nm-setting-private.h"
35 36
#include "nm-setting-connection.h"
#include "nm-setting-vpn.h"
37
#include "nm-setting-gsm.h"
38
#include "nm-setting-cdma.h"
39
#include "nm-setting-wired.h"
40
#include "nm-setting-wireless-security.h"
41
#include "nm-setting-ip6-config.h"
42 43
#include "nm-setting-ip4-config.h"
#include "nm-setting-pppoe.h"
44
#include "nm-setting-serial.h"
45 46 47
#include "nm-setting-vlan.h"
#include "nm-setting-bond.h"
#include "nm-utils.h"
48
#include "nm-dbus-glib-types.h"
49

50
#include "nm-utils/nm-test-utils.h"
51

52 53 54 55
static void
vpn_check_func (const char *key, const char *value, gpointer user_data)
{
	if (!strcmp (key, "foobar1")) {
56
		g_assert_cmpstr (value, ==, "blahblah1");
57 58 59 60
		return;
	}

	if (!strcmp (key, "foobar2")) {
61
		g_assert_cmpstr (value, ==, "blahblah2");
62 63 64 65
		return;
	}

	if (!strcmp (key, "foobar3")) {
66
		g_assert_cmpstr (value, ==, "blahblah3");
67 68 69 70
		return;
	}

	if (!strcmp (key, "foobar4")) {
71
		g_assert_cmpstr (value, ==, "blahblah4");
72 73 74
		return;
	}

75
	g_assert_not_reached ();
76 77 78 79 80
}

static void
vpn_check_empty_func (const char *key, const char *value, gpointer user_data)
{
81
	g_assert_not_reached ();
82 83 84 85 86 87 88 89
}

static void
test_setting_vpn_items (void)
{
	NMSettingVPN *s_vpn;

	s_vpn = (NMSettingVPN *) nm_setting_vpn_new ();
90
	g_assert (s_vpn);
91 92 93 94 95 96 97

	nm_setting_vpn_add_data_item (s_vpn, "foobar1", "blahblah1");
	nm_setting_vpn_add_data_item (s_vpn, "foobar2", "blahblah2");
	nm_setting_vpn_add_data_item (s_vpn, "foobar3", "blahblah3");
	nm_setting_vpn_add_data_item (s_vpn, "foobar4", "blahblah4");

	/* Ensure that added values are all present */
98
	nm_setting_vpn_foreach_data_item (s_vpn, vpn_check_func, NULL);
99 100 101 102 103 104 105 106 107 108 109
	nm_setting_vpn_remove_data_item (s_vpn, "foobar1");
	nm_setting_vpn_remove_data_item (s_vpn, "foobar2");
	nm_setting_vpn_remove_data_item (s_vpn, "foobar3");
	nm_setting_vpn_remove_data_item (s_vpn, "foobar4");

	nm_setting_vpn_add_secret (s_vpn, "foobar1", "blahblah1");
	nm_setting_vpn_add_secret (s_vpn, "foobar2", "blahblah2");
	nm_setting_vpn_add_secret (s_vpn, "foobar3", "blahblah3");
	nm_setting_vpn_add_secret (s_vpn, "foobar4", "blahblah4");

	/* Ensure that added values are all present */
110
	nm_setting_vpn_foreach_secret (s_vpn, vpn_check_func, NULL);
111 112 113 114 115 116
	nm_setting_vpn_remove_secret (s_vpn, "foobar1");
	nm_setting_vpn_remove_secret (s_vpn, "foobar2");
	nm_setting_vpn_remove_secret (s_vpn, "foobar3");
	nm_setting_vpn_remove_secret (s_vpn, "foobar4");

	/* Try to add some blank values and make sure they are rejected */
117
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (key != NULL));
118
	nm_setting_vpn_add_data_item (s_vpn, NULL, NULL);
119 120
	g_test_assert_expected_messages ();

121
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (strlen (key) > 0));
122
	nm_setting_vpn_add_data_item (s_vpn, "", "");
123 124
	g_test_assert_expected_messages ();

125
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (item != NULL));
126
	nm_setting_vpn_add_data_item (s_vpn, "foobar1", NULL);
127 128
	g_test_assert_expected_messages ();

129
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (strlen (item) > 0));
130
	nm_setting_vpn_add_data_item (s_vpn, "foobar1", "");
131 132
	g_test_assert_expected_messages ();

133
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (key != NULL));
134
	nm_setting_vpn_add_data_item (s_vpn, NULL, "blahblah1");
135 136
	g_test_assert_expected_messages ();

137
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (strlen (key) > 0));
138
	nm_setting_vpn_add_data_item (s_vpn, "", "blahblah1");
139
	g_test_assert_expected_messages ();
140

141
	nm_setting_vpn_foreach_data_item (s_vpn, vpn_check_empty_func, NULL);
142 143

	/* Try to add some blank secrets and make sure they are rejected */
144
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (key != NULL));
145
	nm_setting_vpn_add_secret (s_vpn, NULL, NULL);
146 147
	g_test_assert_expected_messages ();

148
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (strlen (key) > 0));
149
	nm_setting_vpn_add_secret (s_vpn, "", "");
150 151
	g_test_assert_expected_messages ();

152
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (secret != NULL));
153
	nm_setting_vpn_add_secret (s_vpn, "foobar1", NULL);
154 155
	g_test_assert_expected_messages ();

156
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (strlen (secret) > 0));
157
	nm_setting_vpn_add_secret (s_vpn, "foobar1", "");
158 159
	g_test_assert_expected_messages ();

160
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (key != NULL));
161
	nm_setting_vpn_add_secret (s_vpn, NULL, "blahblah1");
162 163
	g_test_assert_expected_messages ();

164
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (strlen (key) > 0));
165
	nm_setting_vpn_add_secret (s_vpn, "", "blahblah1");
166
	g_test_assert_expected_messages ();
167

168
	nm_setting_vpn_foreach_secret (s_vpn, vpn_check_empty_func, NULL);
169 170 171 172

	g_object_unref (s_vpn);
}

173 174 175 176 177 178
static void
test_setting_vpn_update_secrets (void)
{
	NMConnection *connection;
	NMSettingVPN *s_vpn;
	GHashTable *settings, *vpn, *secrets;
179
	GValue val = G_VALUE_INIT;
180 181 182 183 184 185 186 187 188
	gboolean success;
	GError *error = NULL;
	const char *tmp;
	const char *key1 = "foobar";
	const char *key2 = "blahblah";
	const char *val1 = "value1";
	const char *val2 = "value2";

	connection = nm_connection_new ();
189
	g_assert (connection);
190 191

	s_vpn = (NMSettingVPN *) nm_setting_vpn_new ();
192
	g_assert (s_vpn);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	nm_connection_add_setting (connection, NM_SETTING (s_vpn));

	settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_hash_table_destroy);
	vpn = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_value_unset);
	g_hash_table_insert (settings, NM_SETTING_VPN_SETTING_NAME, vpn);

	secrets = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
	g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING);
	g_value_take_boxed (&val, secrets);
	g_hash_table_insert (vpn, NM_SETTING_VPN_SECRETS, &val);

	/* Add some secrets */
	g_hash_table_insert (secrets, (char *) key1, (char *) val1);
	g_hash_table_insert (secrets, (char *) key2, (char *) val2);

	success = nm_connection_update_secrets (connection, NM_SETTING_VPN_SETTING_NAME, settings, &error);
209 210
	g_assert_no_error (error);
	g_assert (success);
211 212 213

	/* Read the secrets back out */
	tmp = nm_setting_vpn_get_secret (s_vpn, key1);
214 215
	g_assert (tmp);
	g_assert_cmpstr (tmp, ==, val1);
216 217

	tmp = nm_setting_vpn_get_secret (s_vpn, key2);
218 219
	g_assert (tmp);
	g_assert_cmpstr (tmp, ==, val2);
220

221
	g_hash_table_destroy (settings);
222 223 224
	g_object_unref (connection);
}

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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
#define TO_DEL_NUM 50
typedef struct {
	NMSettingVPN *s_vpn;
	char *to_del[TO_DEL_NUM];
	guint called;
} IterInfo;

static void
del_iter_func (const char *key, const char *value, gpointer user_data)
{
	IterInfo *info = user_data;
	int i;

	/* Record how many times this function gets called; it should get called
	 * exactly as many times as there are keys in the hash table, regardless
	 * of what keys we delete from the table.
	 */
	info->called++;

	/* During the iteration, remove a bunch of stuff from the table */
	if (info->called == 1) {
		for (i = 0; i < TO_DEL_NUM; i++)
			nm_setting_vpn_remove_data_item (info->s_vpn, info->to_del[i]);
	}
}

static void
test_setting_vpn_modify_during_foreach (void)
{
	NMSettingVPN *s_vpn;
	IterInfo info;
	char *key, *val;
	int i, u = 0;

	s_vpn = (NMSettingVPN *) nm_setting_vpn_new ();
	g_assert (s_vpn);

	for (i = 0; i < TO_DEL_NUM * 2; i++) {
		key = g_strdup_printf ("adsfasdfadf%d", i);
		val = g_strdup_printf ("42263236236awt%d", i);
		nm_setting_vpn_add_data_item (s_vpn, key, val);

		/* Cache some keys to delete */
		if (i % 2)
			info.to_del[u++] = g_strdup (key);

		g_free (key);
		g_free (val);
	}

	/* Iterate over current table keys */
	info.s_vpn = s_vpn;
	info.called = 0;
	nm_setting_vpn_foreach_data_item (s_vpn, del_iter_func, &info);

	/* Make sure all the things we removed during iteration are really gone */
	for (i = 0; i < TO_DEL_NUM; i++) {
		g_assert_cmpstr (nm_setting_vpn_get_data_item (s_vpn, info.to_del[i]), ==, NULL);
		g_free (info.to_del[i]);
	}

	/* And make sure the foreach callback was called the same number of times
	 * as there were keys in the table at the beginning of the foreach.
	 */
	g_assert_cmpint (info.called, ==, TO_DEL_NUM * 2);

	g_object_unref (s_vpn);
}

294 295 296 297 298 299 300
static void
_g_value_array_free (void *ptr)
{
	if (ptr)
		g_value_array_free ((GValueArray *) ptr);
}

301 302 303 304 305 306 307 308 309 310 311 312 313
#define OLD_DBUS_TYPE_G_IP6_ADDRESS (dbus_g_type_get_struct ("GValueArray", DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, G_TYPE_INVALID))
#define OLD_DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS (dbus_g_type_get_collection ("GPtrArray", OLD_DBUS_TYPE_G_IP6_ADDRESS))

/* Test that setting the IPv6 setting's 'addresses' property using the old
 * IPv6 address format still works, i.e. that the GValue transformation function
 * from old->new is working correctly.
 */
static void
test_setting_ip6_config_old_address_array (void)
{
	NMSettingIP6Config *s_ip6;
	GPtrArray *addresses, *read_addresses;
	GValueArray *array, *read_array;
314
	GValue element = G_VALUE_INIT, written_value = G_VALUE_INIT, read_value = G_VALUE_INIT;
315 316 317 318 319 320 321 322 323
	GByteArray *ba;
	const guint8 addr[16] = { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
	                          0x11, 0x22, 0x33, 0x44, 0x66, 0x77, 0x88, 0x99 };
	const guint8 gw[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	guint32 prefix = 56;
	GValue *read_addr, *read_prefix, *read_gw;

	s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new ();
324
	g_assert (s_ip6);
325 326 327

	g_value_init (&written_value, OLD_DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS);

328
	addresses = g_ptr_array_new_full (0, _g_value_array_free);
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	array = g_value_array_new (3);

	/* IP address */
	g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY);
	ba = g_byte_array_new ();
	g_byte_array_append (ba, &addr[0], sizeof (addr));
	g_value_take_boxed (&element, ba);
	g_value_array_append (array, &element);
	g_value_unset (&element);

	/* Prefix */
	g_value_init (&element, G_TYPE_UINT);
	g_value_set_uint (&element, prefix);
	g_value_array_append (array, &element);
	g_value_unset (&element);

	g_ptr_array_add (addresses, array);
	g_value_set_boxed (&written_value, addresses);

	/* Set the address array on the object */
	g_object_set_property (G_OBJECT (s_ip6), NM_SETTING_IP6_CONFIG_ADDRESSES, &written_value);

	/* Get it back so we can compare it */
	g_value_init (&read_value, DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS);
	g_object_get_property (G_OBJECT (s_ip6), NM_SETTING_IP6_CONFIG_ADDRESSES, &read_value);

355
	g_assert (G_VALUE_HOLDS (&read_value, DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS));
356 357

	read_addresses = (GPtrArray *) g_value_get_boxed (&read_value);
358 359
	g_assert (read_addresses);
	g_assert_cmpint (read_addresses->len, ==, 1);
360 361 362 363 364

	read_array = (GValueArray *) g_ptr_array_index (read_addresses, 0);

	read_addr = g_value_array_get_nth (read_array, 0);
	ba = g_value_get_boxed (read_addr);
365 366
	g_assert_cmpint (ba->len, ==, sizeof (addr));
	g_assert_cmpmem (ba->data, ba->len, &addr[0], sizeof (addr));
367 368

	read_prefix = g_value_array_get_nth (read_array, 1);
369
	g_assert_cmpint (g_value_get_uint (read_prefix), ==, prefix);
370 371 372 373 374 375

	/* Ensure the gateway is all zeros, which is how the 2-item to 3-item
	 * conversion happens.
	 */
	read_gw = g_value_array_get_nth (read_array, 2);
	ba = g_value_get_boxed (read_gw);
376 377
	g_assert_cmpint (ba->len, ==, sizeof (gw));
	g_assert_cmpmem (ba->data, ba->len, &gw[0], sizeof (gw));
378

379
	g_ptr_array_unref (addresses);
380 381 382 383 384
	g_value_unset (&written_value);
	g_value_unset (&read_value);
	g_object_unref (s_ip6);
}

385 386 387
static void
test_setting_gsm_apn_spaces (void)
{
388
	gs_unref_object NMSettingGsm *s_gsm = NULL;
389 390 391
	const char *tmp;

	s_gsm = (NMSettingGsm *) nm_setting_gsm_new ();
392
	g_assert (s_gsm);
393 394 395 396

	/* Trailing space */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "foobar ", NULL);
	tmp = nm_setting_gsm_get_apn (s_gsm);
397
	g_assert_cmpstr (tmp, ==, "foobar");
398 399 400 401

	/* Leading space */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, " foobar", NULL);
	tmp = nm_setting_gsm_get_apn (s_gsm);
402
	g_assert_cmpstr (tmp, ==, "foobar");
403 404 405 406 407
}

static void
test_setting_gsm_apn_bad_chars (void)
{
408
	gs_unref_object NMSettingGsm *s_gsm = NULL;
409 410

	s_gsm = (NMSettingGsm *) nm_setting_gsm_new ();
411
	g_assert (s_gsm);
412 413 414 415 416

	g_object_set (s_gsm, NM_SETTING_GSM_NUMBER, "*99#", NULL);

	/* Make sure a valid APN works */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "foobar123.-baz", NULL);
417
	g_assert (nm_setting_verify (NM_SETTING (s_gsm), NULL, NULL));
418 419 420

	/* Random invalid chars */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "@#%$@#%@#%", NULL);
421
	g_assert (!nm_setting_verify (NM_SETTING (s_gsm), NULL, NULL));
422 423 424

	/* Spaces */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "foobar baz", NULL);
425
	g_assert (!nm_setting_verify (NM_SETTING (s_gsm), NULL, NULL));
426 427 428

	/* 0 characters long */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "", NULL);
429
	g_assert (!nm_setting_verify (NM_SETTING (s_gsm), NULL, NULL));
430

Dan Williams's avatar
Dan Williams committed
431 432
	/* 65-character long */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl1", NULL);
433
	g_assert (!nm_setting_verify (NM_SETTING (s_gsm), NULL, NULL));
434 435
}

436 437 438
static void
test_setting_gsm_apn_underscore (void)
{
439
	gs_unref_object NMSettingGsm *s_gsm = NULL;
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	GError *error = NULL;
	gboolean success;

	s_gsm = (NMSettingGsm *) nm_setting_gsm_new ();
	g_assert (s_gsm);

	g_object_set (s_gsm, NM_SETTING_GSM_NUMBER, "*99#", NULL);

	/* 65-character long */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "foobar_baz", NULL);
	success = nm_setting_verify (NM_SETTING (s_gsm), NULL, &error);
	g_assert_no_error (error);
	g_assert (success == TRUE);
}

455 456 457
static void
test_setting_gsm_without_number (void)
{
458
	gs_unref_object NMSettingGsm *s_gsm = NULL;
459 460 461 462 463 464 465 466 467 468 469 470 471 472
	GError *error = NULL;
	gboolean success;

	s_gsm = (NMSettingGsm *) nm_setting_gsm_new ();
	g_assert (s_gsm);

	g_object_set (s_gsm, NM_SETTING_GSM_NUMBER, NULL, NULL);
	success = nm_setting_verify (NM_SETTING (s_gsm), NULL, &error);
	g_assert_no_error (error);
	g_assert (success == TRUE);

	g_object_set (s_gsm, NM_SETTING_GSM_NUMBER, "", NULL);
	success = nm_setting_verify (NM_SETTING (s_gsm), NULL, &error);
	g_assert_error (error, NM_SETTING_GSM_ERROR, NM_SETTING_GSM_ERROR_INVALID_PROPERTY);
473
	g_error_free (error);
474 475
}

476 477 478 479 480 481
static NMSettingWirelessSecurity *
make_test_wsec_setting (const char *detail)
{
	NMSettingWirelessSecurity *s_wsec;

	s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
482
	g_assert (s_wsec);
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
	g_object_set (s_wsec,
	              NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk",
	              NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, "foobarbaz",
	              NM_SETTING_WIRELESS_SECURITY_PSK, "random psk",
	              NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, "aaaaaaaaaa",
	              NULL);
	return s_wsec;
}

static void
test_setting_to_hash_all (void)
{
	NMSettingWirelessSecurity *s_wsec;
	GHashTable *hash;

	s_wsec = make_test_wsec_setting ("setting-to-hash-all");

	hash = nm_setting_to_hash (NM_SETTING (s_wsec), NM_SETTING_HASH_FLAG_ALL);

	/* Make sure all keys are there */
503 504 505 506
	g_assert (g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT));
	g_assert (g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME));
	g_assert (g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_PSK));
	g_assert (g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0));
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

	g_hash_table_destroy (hash);
	g_object_unref (s_wsec);
}

static void
test_setting_to_hash_no_secrets (void)
{
	NMSettingWirelessSecurity *s_wsec;
	GHashTable *hash;

	s_wsec = make_test_wsec_setting ("setting-to-hash-no-secrets");

	hash = nm_setting_to_hash (NM_SETTING (s_wsec), NM_SETTING_HASH_FLAG_NO_SECRETS);

	/* Make sure non-secret keys are there */
523 524
	g_assert (g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT));
	g_assert (g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME));
525 526

	/* Make sure secrets are not there */
527 528
	g_assert (!g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_PSK));
	g_assert (!g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0));
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

	g_hash_table_destroy (hash);
	g_object_unref (s_wsec);
}

static void
test_setting_to_hash_only_secrets (void)
{
	NMSettingWirelessSecurity *s_wsec;
	GHashTable *hash;

	s_wsec = make_test_wsec_setting ("setting-to-hash-only-secrets");

	hash = nm_setting_to_hash (NM_SETTING (s_wsec), NM_SETTING_HASH_FLAG_ONLY_SECRETS);

	/* Make sure non-secret keys are there */
545 546
	g_assert (!g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT));
	g_assert (!g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME));
547 548

	/* Make sure secrets are not there */
549 550
	g_assert (g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_PSK));
	g_assert (g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0));
551 552 553 554 555

	g_hash_table_destroy (hash);
	g_object_unref (s_wsec);
}

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
static void
test_connection_to_hash_setting_name (void)
{
	NMConnection *connection;
	NMSettingWirelessSecurity *s_wsec;
	GHashTable *hash;

	connection = nm_connection_new ();
	s_wsec = make_test_wsec_setting ("connection-to-hash-setting-name");
	nm_connection_add_setting (connection, NM_SETTING (s_wsec));

	hash = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_ALL);

	/* Make sure the keys of the first level hash are setting names, not
	 * the GType name of the setting objects.
	 */
572
	g_assert (g_hash_table_lookup (hash, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME));
573 574 575 576 577

	g_hash_table_destroy (hash);
	g_object_unref (connection);
}

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
static void
test_setting_new_from_hash (void)
{
	NMSettingWirelessSecurity *s_wsec;
	GHashTable *hash;

	s_wsec = make_test_wsec_setting ("setting-to-hash-all");
	hash = nm_setting_to_hash (NM_SETTING (s_wsec), NM_SETTING_HASH_FLAG_ALL);
	g_object_unref (s_wsec);

	s_wsec = (NMSettingWirelessSecurity *) nm_setting_new_from_hash (NM_TYPE_SETTING_WIRELESS_SECURITY, hash);
	g_hash_table_destroy (hash);

	g_assert (s_wsec);
	g_assert_cmpstr (nm_setting_wireless_security_get_key_mgmt (s_wsec), ==, "wpa-psk");
	g_assert_cmpstr (nm_setting_wireless_security_get_leap_username (s_wsec), ==, "foobarbaz");
	g_assert_cmpstr (nm_setting_wireless_security_get_psk (s_wsec), ==, "random psk");
	g_object_unref (s_wsec);
}

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
static NMConnection *
new_test_connection (void)
{
	NMConnection *connection;
	NMSetting *setting;
	char *uuid;
	guint64 timestamp = time (NULL);

	connection = nm_connection_new ();

	setting = nm_setting_connection_new ();
	uuid = nm_utils_uuid_generate ();
	g_object_set (G_OBJECT (setting),
	              NM_SETTING_CONNECTION_ID, "foobar",
	              NM_SETTING_CONNECTION_UUID, uuid,
	              NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME,
	              NM_SETTING_CONNECTION_TIMESTAMP, timestamp,
	              NULL);
	g_free (uuid);
	nm_connection_add_setting (connection, setting);

	setting = nm_setting_wired_new ();
	g_object_set (G_OBJECT (setting),
	              NM_SETTING_WIRED_MTU, 1592,
	              NULL);
	nm_connection_add_setting (connection, setting);

	setting = nm_setting_ip4_config_new ();
	g_object_set (G_OBJECT (setting),
	              NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
	              NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, "eyeofthetiger",
	              NULL);
	nm_connection_add_setting (connection, setting);

	return connection;
}

static GValue *
string_to_gvalue (const char *str)
{
	GValue *val;

	val = g_slice_new0 (GValue);
	g_value_init (val, G_TYPE_STRING);
	g_value_set_string (val, str);
	return val;
}

static void
destroy_gvalue (gpointer data)
{
	g_value_unset ((GValue *) data);
	g_slice_free (GValue, data);
}

static GHashTable *
new_connection_hash (char **out_uuid,
                     const char **out_expected_id,
                     const char **out_expected_ip6_method)
{
	GHashTable *hash;
	GHashTable *setting;

	hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy);

	*out_uuid = nm_utils_uuid_generate ();
	*out_expected_id = "My happy connection";
	*out_expected_ip6_method = NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL;

	/* Connection setting */
	setting = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, destroy_gvalue);
	g_hash_table_insert (setting,
	                     g_strdup (NM_SETTING_NAME),
	                     string_to_gvalue (NM_SETTING_CONNECTION_SETTING_NAME));
	g_hash_table_insert (setting,
	                     g_strdup (NM_SETTING_CONNECTION_ID),
	                     string_to_gvalue (*out_expected_id));
	g_hash_table_insert (setting,
	                     g_strdup (NM_SETTING_CONNECTION_UUID),
	                     string_to_gvalue (*out_uuid));
	g_hash_table_insert (setting,
	                     g_strdup (NM_SETTING_CONNECTION_TYPE),
	                     string_to_gvalue (NM_SETTING_WIRED_SETTING_NAME));
	g_hash_table_insert (hash, g_strdup (NM_SETTING_CONNECTION_SETTING_NAME), setting);

	/* Wired setting */
	setting = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, destroy_gvalue);
	g_hash_table_insert (hash, g_strdup (NM_SETTING_WIRED_SETTING_NAME), setting);

	/* IP6 */
	setting = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, destroy_gvalue);
	g_hash_table_insert (setting,
	                     g_strdup (NM_SETTING_IP6_CONFIG_METHOD),
	                     string_to_gvalue (*out_expected_ip6_method));
	g_hash_table_insert (hash, g_strdup (NM_SETTING_IP6_CONFIG_SETTING_NAME), setting);

	return hash;
}

static void
698
test_connection_replace_settings (void)
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
{
	NMConnection *connection;
	GHashTable *new_settings;
	GError *error = NULL;
	gboolean success;
	NMSettingConnection *s_con;
	NMSettingIP6Config *s_ip6;
	char *uuid = NULL;
	const char *expected_id = NULL, *expected_method = NULL;

	connection = new_test_connection ();

	new_settings = new_connection_hash (&uuid, &expected_id, &expected_method);
	g_assert (new_settings);

	/* Replace settings and test */
	success = nm_connection_replace_settings (connection, new_settings, &error);
	g_assert_no_error (error);
	g_assert (success);

	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, expected_id);
	g_assert_cmpstr (nm_setting_connection_get_uuid (s_con), ==, uuid);

	g_assert (nm_connection_get_setting_wired (connection));
	g_assert (!nm_connection_get_setting_ip4_config (connection));

	s_ip6 = nm_connection_get_setting_ip6_config (connection);
	g_assert (s_ip6);
	g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, expected_method);

	g_free (uuid);
	g_hash_table_destroy (new_settings);
	g_object_unref (connection);
}

736
static void
737
test_connection_replace_settings_from_connection (void)
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
{
	NMConnection *connection, *replacement;
	GError *error = NULL;
	gboolean success;
	NMSettingConnection *s_con;
	NMSetting *setting;
	GByteArray *ssid;
	char *uuid = NULL;
	const char *expected_id = "Awesome connection";

	connection = new_test_connection ();
	g_assert (connection);

	replacement = nm_connection_new ();
	g_assert (replacement);

	/* New connection setting */
	setting = nm_setting_connection_new ();
	g_assert (setting);

	uuid = nm_utils_uuid_generate ();
	g_object_set (setting,
	              NM_SETTING_CONNECTION_ID, expected_id,
	              NM_SETTING_CONNECTION_UUID, uuid,
	              NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME,
	              NULL);
	nm_connection_add_setting (replacement, setting);

	/* New wifi setting */
	setting = nm_setting_wireless_new ();
	g_assert (setting);

	ssid = g_byte_array_new ();
	g_byte_array_append (ssid, (const guint8 *) "1234567", 7);
	g_object_set (setting,
	              NM_SETTING_WIRELESS_SSID, ssid,
	              NM_SETTING_WIRELESS_MODE, "infrastructure",
	              NULL);
	g_byte_array_free (ssid, TRUE);
	nm_connection_add_setting (replacement, setting);

	/* Replace settings and test */
	success = nm_connection_replace_settings_from_connection (connection, replacement, &error);
	g_assert_no_error (error);
	g_assert (success);

	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, expected_id);
	g_assert_cmpstr (nm_setting_connection_get_uuid (s_con), ==, uuid);

	g_assert (!nm_connection_get_setting_wired (connection));
	g_assert (!nm_connection_get_setting_ip6_config (connection));
	g_assert (nm_connection_get_setting_wireless (connection));

	g_free (uuid);
	g_object_unref (replacement);
	g_object_unref (connection);
}

798
static void
799
test_connection_new_from_hash (void)
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
{
	NMConnection *connection;
	GHashTable *new_settings;
	GError *error = NULL;
	NMSettingConnection *s_con;
	NMSettingIP6Config *s_ip6;
	char *uuid = NULL;
	const char *expected_id = NULL, *expected_method = NULL;

	new_settings = new_connection_hash (&uuid, &expected_id, &expected_method);
	g_assert (new_settings);

	/* Replace settings and test */
	connection = nm_connection_new_from_hash (new_settings, &error);
	g_assert_no_error (error);
	g_assert (connection);

	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, expected_id);
	g_assert_cmpstr (nm_setting_connection_get_uuid (s_con), ==, uuid);

	g_assert (nm_connection_get_setting_wired (connection));
	g_assert (!nm_connection_get_setting_ip4_config (connection));

	s_ip6 = nm_connection_get_setting_ip6_config (connection);
	g_assert (s_ip6);
	g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, expected_method);

	g_free (uuid);
	g_hash_table_destroy (new_settings);
	g_object_unref (connection);
}

834 835 836
static void
check_permission (NMSettingConnection *s_con,
                  guint32 idx,
837
                  const char *expected_uname)
838 839 840 841 842
{
	gboolean success;
	const char *ptype = NULL, *pitem = NULL, *detail = NULL;

	success = nm_setting_connection_get_permission (s_con, 0, &ptype, &pitem, &detail);
843
	g_assert (success);
844

845
	g_assert_cmpstr (ptype, ==, "user");
846

847 848
	g_assert (pitem);
	g_assert_cmpstr (pitem, ==, expected_uname);
849

850
	g_assert (!detail);
851 852
}

853 854 855
#define TEST_UNAME "asdfasfasdf"

static void
856
test_setting_connection_permissions_helpers (void)
857 858 859
{
	NMSettingConnection *s_con;
	gboolean success;
860 861
	char buf[9] = { 0x61, 0x62, 0x63, 0xff, 0xfe, 0xfd, 0x23, 0x01, 0x00 };
	GSList *list = NULL;
862 863 864 865 866
	const char *expected_perm = "user:" TEST_UNAME ":";

	s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());

	/* Ensure a bad [type] is rejected */
867
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (strcmp (ptype, "user") == 0));
868
	success = nm_setting_connection_add_permission (s_con, "foobar", "blah", NULL);
869
	g_test_assert_expected_messages ();
870
	g_assert (!success);
871 872

	/* Ensure a bad [type] is rejected */
873
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (ptype));
874
	success = nm_setting_connection_add_permission (s_con, NULL, "blah", NULL);
875
	g_test_assert_expected_messages ();
876
	g_assert (!success);
877 878

	/* Ensure a bad [item] is rejected */
879 880
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (uname));
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (p != NULL));
881
	success = nm_setting_connection_add_permission (s_con, "user", NULL, NULL);
882
	g_test_assert_expected_messages ();
883
	g_assert (!success);
884 885

	/* Ensure a bad [item] is rejected */
886 887
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (uname[0] != '\0'));
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (p != NULL));
888
	success = nm_setting_connection_add_permission (s_con, "user", "", NULL);
889
	g_test_assert_expected_messages ();
890
	g_assert (!success);
891 892

	/* Ensure an [item] with ':' is rejected */
893 894
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (strchr (uname, ':') == NULL));
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (p != NULL));
895
	success = nm_setting_connection_add_permission (s_con, "user", "ad:asdf", NULL);
896
	g_test_assert_expected_messages ();
897
	g_assert (!success);
898 899

	/* Ensure a non-UTF-8 [item] is rejected */
900 901
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (g_utf8_validate (uname, -1, NULL) == TRUE));
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (p != NULL));
902
	success = nm_setting_connection_add_permission (s_con, "user", buf, NULL);
903
	g_test_assert_expected_messages ();
904
	g_assert (!success);
905 906

	/* Ensure a non-NULL [detail] is rejected */
907
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (detail == NULL));
908
	success = nm_setting_connection_add_permission (s_con, "user", "dafasdf", "asdf");
909
	g_test_assert_expected_messages ();
910
	g_assert (!success);
911 912 913

	/* Ensure a valid call results in success */
	success = nm_setting_connection_add_permission (s_con, "user", TEST_UNAME, NULL);
914
	g_assert (success);
915

916
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 1);
917

918
	check_permission (s_con, 0, TEST_UNAME);
919 920 921

	/* Check the actual GObject property just to be paranoid */
	g_object_get (G_OBJECT (s_con), NM_SETTING_CONNECTION_PERMISSIONS, &list, NULL);
922 923 924
	g_assert (list);
	g_assert_cmpint (g_slist_length (list), ==, 1);
	g_assert_cmpstr (list->data, ==, expected_perm);
925
	g_slist_free_full (list, g_free);
926 927 928

	/* Now remove that permission and ensure we have 0 permissions */
	nm_setting_connection_remove_permission (s_con, 0);
929
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 0);
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977

	g_object_unref (s_con);
}

static void
add_permission_property (NMSettingConnection *s_con,
                         const char *ptype,
                         const char *pitem,
                         int pitem_len,
                         const char *detail)
{
	GString *str;
	GSList *list = NULL;

	str = g_string_sized_new (50);
	if (ptype)
		g_string_append (str, ptype);
	g_string_append_c (str, ':');

	if (pitem) {
		if (pitem_len >= 0)
			g_string_append_len (str, pitem, pitem_len);
		else
			g_string_append (str, pitem);
	}

	g_string_append_c (str, ':');

	if (detail)
		g_string_append (str, detail);

	list = g_slist_append (list, str->str);
	g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_PERMISSIONS, list, NULL);

	g_string_free (str, TRUE);
	g_slist_free (list);
}

static void
test_setting_connection_permissions_property (void)
{
	NMSettingConnection *s_con;
	gboolean success;
	char buf[9] = { 0x61, 0x62, 0x63, 0xff, 0xfe, 0xfd, 0x23, 0x01, 0x00 };

	s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());

	/* Ensure a bad [type] is rejected */
978
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (strncmp (str, PERM_USER_PREFIX, strlen (PERM_USER_PREFIX)) == 0));
979
	add_permission_property (s_con, "foobar", "blah", -1, NULL);
980
	g_test_assert_expected_messages ();
981
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 0);
982 983

	/* Ensure a bad [type] is rejected */
984
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (strncmp (str, PERM_USER_PREFIX, strlen (PERM_USER_PREFIX)) == 0));
985
	add_permission_property (s_con, NULL, "blah", -1, NULL);
986
	g_test_assert_expected_messages ();
987
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 0);
988 989

	/* Ensure a bad [item] is rejected */
990
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (last_colon > str));
991
	add_permission_property (s_con, "user", NULL, -1, NULL);
992
	g_test_assert_expected_messages ();
993
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 0);
994

995
	/* Ensure a bad [item] is rejected */
996
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (last_colon > str));
997
	add_permission_property (s_con, "user", "", -1, NULL);
998
	g_test_assert_expected_messages ();
999
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 0);
1000 1001

	/* Ensure an [item] with ':' in the middle is rejected */
1002
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (str[i] != ':'));
1003
	add_permission_property (s_con, "user", "ad:asdf", -1, NULL);
1004
	g_test_assert_expected_messages ();
1005
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 0);
1006 1007

	/* Ensure an [item] with ':' at the end is rejected */
1008
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (str[i] != ':'));
1009
	add_permission_property (s_con, "user", "adasdfaf:", -1, NULL);
1010
	g_test_assert_expected_messages ();
1011
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 0);
1012 1013

	/* Ensure a non-UTF-8 [item] is rejected */
1014
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (g_utf8_validate (str, -1, NULL) == TRUE));
1015
	add_permission_property (s_con, "user", buf, (int) sizeof (buf), NULL);
1016
	g_test_assert_expected_messages ();
1017
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 0);
1018 1019

	/* Ensure a non-NULL [detail] is rejected */
1020
	NMTST_EXPECT_LIBNM_U_CRITICAL (NMTST_G_RETURN_MSG (*(last_colon + 1) == '\0'));
1021
	add_permission_property (s_con, "user", "dafasdf", -1, "asdf");
1022
	g_test_assert_expected_messages ();
1023
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 0);
1024 1025 1026

	/* Ensure a valid call results in success */
	success = nm_setting_connection_add_permission (s_con, "user", TEST_UNAME, NULL);
1027
	g_assert (success);
1028
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 1);
1029

1030
	check_permission (s_con, 0, TEST_UNAME);
1031 1032 1033

	/* Now remove that permission and ensure we have 0 permissions */
	nm_setting_connection_remove_permission (s_con, 0);
1034
	g_assert_cmpint (nm_setting_connection_get_num_permissions (s_con), ==, 0);
1035 1036 1037 1038

	g_object_unref (s_con);
}

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
static void
test_connection_compare_same (void)
{
	NMConnection *a, *b;

	a = new_test_connection ();
	b = nm_connection_duplicate (a);
	g_assert (nm_connection_compare (a, b, NM_SETTING_COMPARE_FLAG_EXACT));
	g_object_unref (a);
	g_object_unref (b);
}

static void
test_connection_compare_key_only_in_a (void)
{
	NMConnection *a, *b;
	NMSettingConnection *s_con;

	a = new_test_connection ();
	b = nm_connection_duplicate (a);
	s_con = (NMSettingConnection *) nm_connection_get_setting (b, NM_TYPE_SETTING_CONNECTION);
	g_assert (s_con);
1061
	g_object_set (s_con, NM_SETTING_CONNECTION_TIMESTAMP, (guint64) 0, NULL);
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

	g_assert (!nm_connection_compare (a, b, NM_SETTING_COMPARE_FLAG_EXACT));
	g_object_unref (a);
	g_object_unref (b);
}

static void
test_connection_compare_setting_only_in_a (void)
{
	NMConnection *a, *b;

	a = new_test_connection ();
	b = nm_connection_duplicate (a);
	nm_connection_remove_setting (b, NM_TYPE_SETTING_IP4_CONFIG);
	g_assert (!nm_connection_compare (a, b, NM_SETTING_COMPARE_FLAG_EXACT));
	g_object_unref (a);
	g_object_unref (b);
}

static void
test_connection_compare_key_only_in_b (void)
{
	NMConnection *a, *b;
	NMSettingConnection *s_con;

	a = new_test_connection ();
	b = nm_connection_duplicate (a);
	s_con = (NMSettingConnection *) nm_connection_get_setting (b, NM_TYPE_SETTING_CONNECTION);
	g_assert (s_con);
1091
	g_object_set (s_con, NM_SETTING_CONNECTION_TIMESTAMP, (guint64) 0, NULL);
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

	g_assert (!nm_connection_compare (a, b, NM_SETTING_COMPARE_FLAG_EXACT));
	g_object_unref (a);
	g_object_unref (b);
}

static void
test_connection_compare_setting_only_in_b (void)
{
	NMConnection *a, *b;

	a = new_test_connection ();
	b = nm_connection_duplicate (a);
	nm_connection_remove_setting (a, NM_TYPE_SETTING_IP4_CONFIG);
	g_assert (!nm_connection_compare (a, b, NM_SETTING_COMPARE_FLAG_EXACT));
	g_object_unref (a);
	g_object_unref (b);
}

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
typedef struct {
	const char *key_name;
	guint32 result;
} DiffKey;

typedef struct {
	const char *name;
	DiffKey keys[30];
} DiffSetting;

#define ARRAY_LEN(a)  (sizeof (a) / sizeof (a[0]))

static void
ensure_diffs (GHashTable *diffs, const DiffSetting *check, gsize n_check)
{
	guint i;

	g_assert (g_hash_table_size (diffs) == n_check);

	/* Loop through the settings */
	for (i = 0; i < n_check; i++) {
		GHashTable *setting_hash;
		guint z = 0;

		setting_hash = g_hash_table_lookup (diffs, check[i].name);
		g_assert (setting_hash);

		/* Get the number of keys to check */
		while (check[i].keys[z].key_name)
			z++;
		g_assert (g_hash_table_size (setting_hash) == z);

		/* Now compare the actual keys */
		for (z = 0; check[i].keys[z].key_name; z++) {
			NMSettingDiffResult result;

			result = GPOINTER_TO_UINT (g_hash_table_lookup (setting_hash, check[i].keys[z].key_name));
			g_assert (result == check[i].keys[z].result);
		}
	}
}

static void
test_connection_diff_a_only (void)
{
	NMConnection *connection;
	GHashTable *out_diffs = NULL;
	gboolean same;
	const DiffSetting settings[] = {
		{ NM_SETTING_CONNECTION_SETTING_NAME, {
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
			{ NM_SETTING_CONNECTION_ID,                   NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_UUID,                 NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_INTERFACE_NAME,       NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_TYPE,                 NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_TIMESTAMP,            NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_AUTOCONNECT,          NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_READ_ONLY,            NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_PERMISSIONS,          NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_ZONE,                 NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_MASTER,               NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_SLAVE_TYPE,           NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_SECONDARIES,          NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_CONNECTION_GATEWAY_PING_TIMEOUT, NM_SETTING_DIFF_RESULT_IN_A },
1174 1175 1176
			{ NULL, NM_SETTING_DIFF_RESULT_UNKNOWN }
		} },
		{ NM_SETTING_WIRED_SETTING_NAME, {
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
			{ NM_SETTING_WIRED_PORT,                  NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_WIRED_SPEED,                 NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_WIRED_DUPLEX,                NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_WIRED_AUTO_NEGOTIATE,        NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_WIRED_MAC_ADDRESS,           NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_WIRED_CLONED_MAC_ADDRESS,    NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_WIRED_MAC_ADDRESS_BLACKLIST, NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_WIRED_MTU,                   NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_WIRED_S390_SUBCHANNELS,      NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_WIRED_S390_NETTYPE,          NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_WIRED_S390_OPTIONS,          NM_SETTING_DIFF_RESULT_IN_A },
1188 1189 1190 1191 1192 1193 1194 1195
			{ NULL, NM_SETTING_DIFF_RESULT_UNKNOWN },
		} },
		{ NM_SETTING_IP4_CONFIG_SETTING_NAME, {
			{ NM_SETTING_IP4_CONFIG_METHOD,             NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_IP4_CONFIG_DNS,                NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_IP4_CONFIG_DNS_SEARCH,         NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_IP4_CONFIG_ADDRESSES,          NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_IP4_CONFIG_ROUTES,             NM_SETTING_DIFF_RESULT_IN_A },
1196
			{ NM_SETTING_IP4_CONFIG_ROUTE_METRIC,       NM_SETTING_DIFF_RESULT_IN_A },
1197 1198 1199
			{ NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS,    NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID,     NM_SETTING_DIFF_RESULT_IN_A },
1200
			{ NM_SETTING_IP4_CONFIG_DHCP_TIMEOUT,       NM_SETTING_DIFF_RESULT_IN_A },
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
			{ NM_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME, NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME,      NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_IP4_CONFIG_NEVER_DEFAULT,      NM_SETTING_DIFF_RESULT_IN_A },
			{ NM_SETTING_IP4_CONFIG_MAY_FAIL,           NM_SETTING_DIFF_RESULT_IN_A },
			{ NULL, NM_SETTING_DIFF_RESULT_UNKNOWN },
		} },
	};

	connection = new_test_connection ();

	same = nm_connection_diff (connection, NULL, NM_SETTING_COMPARE_FLAG_EXACT, &out_diffs);
	g_assert (same == FALSE);
	g_assert (out_diffs != NULL);
	g_assert (g_hash_table_size (out_diffs) > 0);

	ensure_diffs (out_diffs, settings, ARRAY_LEN (settings));

1218
	g_hash_table_destroy (out_diffs);
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	g_object_unref (connection);
}

static void
test_connection_diff_same (void)
{
	NMConnection *a, *b;
	GHashTable *out_diffs = NULL;
	gboolean same;

	a = new_test_connection ();
	b = nm_connection_duplicate (a);

	same = nm_connection_diff (a, b, NM_SETTING_COMPARE_FLAG_EXACT, &out_diffs);
	g_assert (same == TRUE);
	g_assert (out_diffs == NULL);
	g_object_unref (a);
	g_object_unref (b);
}

static void
test_connection_diff_different (void)
{
	NMConnection *a, *b;
	GHashTable *out_diffs = NULL;
1244
	NMSettingIP4Config *s_ip4;
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	gboolean same;
	const DiffSetting settings[] = {
		{ NM_SETTING_IP4_CONFIG_SETTING_NAME, {
			{ NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_DIFF_RESULT_IN_A | NM_SETTING_DIFF_RESULT_IN_B },
			{ NULL, NM_SETTING_DIFF_RESULT_UNKNOWN },
		} },
	};

	a = new_test_connection ();
	b = nm_connection_duplicate (a);
1255
	s_ip4 = nm_connection_get_setting_ip4_config (a);
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	g_assert (s_ip4);
	g_object_set (G_OBJECT (s_ip4),
	              NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
	              NULL);

	same = nm_connection_diff (a, b, NM_SETTING_COMPARE_FLAG_EXACT, &out_diffs);
	g_assert (same == FALSE);
	g_assert (out_diffs != NULL);
	g_assert (g_hash_table_size (out_diffs) > 0);

	ensure_diffs (out_diffs, settings, ARRAY_LEN (settings));

1268
	g_hash_table_destroy (out_diffs);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287