test-general.c 122 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 32 33 34 35 36 37 38 39 40
/* -*- 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 2008 - 2011 Red Hat, Inc.
 *
 */

#include <glib.h>
#include <string.h>

#include <nm-utils.h>

#include "nm-setting-private.h"
#include "nm-setting-connection.h"
#include "nm-setting-vpn.h"
#include "nm-setting-gsm.h"
#include "nm-setting-cdma.h"
#include "nm-setting-wired.h"
#include "nm-setting-wireless-security.h"
#include "nm-setting-ip6-config.h"
#include "nm-setting-ip4-config.h"
#include "nm-setting-pppoe.h"
#include "nm-setting-serial.h"
#include "nm-setting-vlan.h"
#include "nm-setting-bond.h"
#include "nm-utils.h"
41
#include "nm-core-internal.h"
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

#include "nm-test-utils.h"

static void
vpn_check_func (const char *key, const char *value, gpointer user_data)
{
	const char *test = user_data;

	if (!strcmp (key, "foobar1")) {
		ASSERT (strcmp (value, "blahblah1") == 0,
				test, "unexpected vpn item '%s' / '%s'", key, value);
		return;
	}

	if (!strcmp (key, "foobar2")) {
		ASSERT (strcmp (value, "blahblah2") == 0,
				test, "unexpected vpn item '%s' / '%s'", key, value);
		return;
	}

	if (!strcmp (key, "foobar3")) {
		ASSERT (strcmp (value, "blahblah3") == 0,
				test, "unexpected vpn item '%s' / '%s'", key, value);
		return;
	}

	if (!strcmp (key, "foobar4")) {
		ASSERT (strcmp (value, "blahblah4") == 0,
				test, "unexpected vpn item '%s' / '%s'", key, value);
		return;
	}

	ASSERT (FALSE, test, "unexpected vpn item '%s'", key);
}

static void
vpn_check_empty_func (const char *key, const char *value, gpointer user_data)
{
	const char *test = user_data;

	/* We don't expect any values */
	ASSERT (FALSE, test, "unexpected vpn item '%s'", key);
}

static void
test_setting_vpn_items (void)
{
89
	NMSettingVpn *s_vpn;
90

91
	s_vpn = (NMSettingVpn *) nm_setting_vpn_new ();
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	ASSERT (s_vpn != NULL,
	        "vpn-items",
	        "error creating vpn setting");

	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 */
	nm_setting_vpn_foreach_data_item (s_vpn, vpn_check_func, "vpn-data");
	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 */
	nm_setting_vpn_foreach_secret (s_vpn, vpn_check_func, "vpn-secrets");
	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 */
121
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*key != NULL*");
122 123 124
	nm_setting_vpn_add_data_item (s_vpn, NULL, NULL);
	g_test_assert_expected_messages ();

125
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*strlen (key) > 0*");
126 127 128
	nm_setting_vpn_add_data_item (s_vpn, "", "");
	g_test_assert_expected_messages ();

129
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*item != NULL*");
130 131 132
	nm_setting_vpn_add_data_item (s_vpn, "foobar1", NULL);
	g_test_assert_expected_messages ();

133
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*strlen (item) > 0*");
134 135 136
	nm_setting_vpn_add_data_item (s_vpn, "foobar1", "");
	g_test_assert_expected_messages ();

137
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*key != NULL*");
138 139 140
	nm_setting_vpn_add_data_item (s_vpn, NULL, "blahblah1");
	g_test_assert_expected_messages ();

141
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*strlen (key) > 0*");
142 143 144 145 146 147
	nm_setting_vpn_add_data_item (s_vpn, "", "blahblah1");
	g_test_assert_expected_messages ();

	nm_setting_vpn_foreach_data_item (s_vpn, vpn_check_empty_func, "vpn-data-empty");

	/* Try to add some blank secrets and make sure they are rejected */
148
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*key != NULL*");
149 150 151
	nm_setting_vpn_add_secret (s_vpn, NULL, NULL);
	g_test_assert_expected_messages ();

152
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*strlen (key) > 0*");
153 154 155
	nm_setting_vpn_add_secret (s_vpn, "", "");
	g_test_assert_expected_messages ();

156
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*secret != NULL*");
157 158 159
	nm_setting_vpn_add_secret (s_vpn, "foobar1", NULL);
	g_test_assert_expected_messages ();

160
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*strlen (secret) > 0*");
161 162 163
	nm_setting_vpn_add_secret (s_vpn, "foobar1", "");
	g_test_assert_expected_messages ();

164
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*key != NULL*");
165 166 167
	nm_setting_vpn_add_secret (s_vpn, NULL, "blahblah1");
	g_test_assert_expected_messages ();

168
	g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*strlen (key) > 0*");
169 170 171 172 173 174 175 176 177 178 179 180
	nm_setting_vpn_add_secret (s_vpn, "", "blahblah1");
	g_test_assert_expected_messages ();

	nm_setting_vpn_foreach_secret (s_vpn, vpn_check_empty_func, "vpn-secrets-empty");

	g_object_unref (s_vpn);
}

static void
test_setting_vpn_update_secrets (void)
{
	NMConnection *connection;
181
	NMSettingVpn *s_vpn;
182 183
	GVariantBuilder settings_builder, vpn_builder, secrets_builder;
	GVariant *settings;
184 185 186 187 188 189 190 191
	gboolean success;
	GError *error = NULL;
	const char *tmp;
	const char *key1 = "foobar";
	const char *key2 = "blahblah";
	const char *val1 = "value1";
	const char *val2 = "value2";

192
	connection = nm_simple_connection_new ();
193 194 195 196
	ASSERT (connection != NULL,
	        "vpn-update-secrets",
	        "error creating connection");

197
	s_vpn = (NMSettingVpn *) nm_setting_vpn_new ();
198 199 200 201 202
	ASSERT (s_vpn != NULL,
	        "vpn-update-secrets",
	        "error creating vpn setting");
	nm_connection_add_setting (connection, NM_SETTING (s_vpn));

203 204 205
	g_variant_builder_init (&settings_builder, NM_VARIANT_TYPE_CONNECTION);
	g_variant_builder_init (&vpn_builder, NM_VARIANT_TYPE_SETTING);
	g_variant_builder_init (&secrets_builder, G_VARIANT_TYPE ("a{ss}"));
206

207 208
	g_variant_builder_add (&secrets_builder, "{ss}", key1, val1);
	g_variant_builder_add (&secrets_builder, "{ss}", key2, val2);
209

210 211 212 213 214 215 216
	g_variant_builder_add (&vpn_builder, "{sv}",
	                       NM_SETTING_VPN_SECRETS,
	                       g_variant_builder_end (&secrets_builder));
	g_variant_builder_add (&settings_builder, "{sa{sv}}",
	                       NM_SETTING_VPN_SETTING_NAME,
	                       &vpn_builder);
	settings = g_variant_builder_end (&settings_builder);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

	success = nm_connection_update_secrets (connection, NM_SETTING_VPN_SETTING_NAME, settings, &error);
	ASSERT (success == TRUE,
	        "vpn-update-secrets", "failed to update VPN secrets: %s", error->message);

	/* Read the secrets back out */
	tmp = nm_setting_vpn_get_secret (s_vpn, key1);
	ASSERT (tmp != NULL,
	        "vpn-update-secrets", "unexpected failure getting key #1");
	ASSERT (strcmp (tmp, val1) == 0,
	        "vpn-update-secrets", "unexpected key #1 value");

	tmp = nm_setting_vpn_get_secret (s_vpn, key2);
	ASSERT (tmp != NULL,
	        "vpn-update-secrets", "unexpected failure getting key #2");
	ASSERT (strcmp (tmp, val2) == 0,
	        "vpn-update-secrets", "unexpected key #2 value");

235
	g_variant_unref (settings);
236 237 238 239 240
	g_object_unref (connection);
}

#define TO_DEL_NUM 50
typedef struct {
241
	NMSettingVpn *s_vpn;
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
	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)
{
268
	NMSettingVpn *s_vpn;
269 270 271 272
	IterInfo info;
	char *key, *val;
	int i, u = 0;

273
	s_vpn = (NMSettingVpn *) nm_setting_vpn_new ();
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	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);
}

static void
test_setting_ip4_config_labels (void)
{
	NMSettingIP4Config *s_ip4;
	NMIP4Address *addr;
	const char *label;
	GPtrArray *addrs;
315
	char **labels;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
	GError *error = NULL;

	s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
	g_object_set (G_OBJECT (s_ip4),
	              NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
	              NULL);

	/* addr 1 */
	addr = nm_ip4_address_new ();
	nm_ip4_address_set_address (addr, 0x01010101);
	nm_ip4_address_set_prefix (addr, 24);

	nm_setting_ip4_config_add_address (s_ip4, addr);
	nm_ip4_address_unref (addr);
	nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
	g_assert_no_error (error);

333
	label = _nm_setting_ip4_config_get_address_label (s_ip4, 0);
334
	g_assert_cmpstr (label, ==, "");
335 336 337 338 339 340

	/* addr 2 */
	addr = nm_ip4_address_new ();
	nm_ip4_address_set_address (addr, 0x02020202);
	nm_ip4_address_set_prefix (addr, 24);

341
	_nm_setting_ip4_config_add_address_with_label (s_ip4, addr, "eth0:1");
342 343 344 345
	nm_ip4_address_unref (addr);
	nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
	g_assert_no_error (error);

346
	label = _nm_setting_ip4_config_get_address_label (s_ip4, 1);
347 348 349 350 351 352 353
	g_assert_cmpstr (label, ==, "eth0:1");

	/* addr 3 */
	addr = nm_ip4_address_new ();
	nm_ip4_address_set_address (addr, 0x03030303);
	nm_ip4_address_set_prefix (addr, 24);

354
	_nm_setting_ip4_config_add_address_with_label (s_ip4, addr, "");
355 356 357 358
	nm_ip4_address_unref (addr);
	nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
	g_assert_no_error (error);

359
	label = _nm_setting_ip4_config_get_address_label (s_ip4, 2);
360
	g_assert_cmpstr (label, ==, "");
361 362 363 364 365 366 367 368

	/* Remove addr 1 and re-verify remaining addresses */
	nm_setting_ip4_config_remove_address (s_ip4, 0);
	nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
	g_assert_no_error (error);

	addr = nm_setting_ip4_config_get_address (s_ip4, 0);
	g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x02020202);
369
	label = _nm_setting_ip4_config_get_address_label (s_ip4, 0);
370 371 372 373
	g_assert_cmpstr (label, ==, "eth0:1");

	addr = nm_setting_ip4_config_get_address (s_ip4, 1);
	g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303);
374
	label = _nm_setting_ip4_config_get_address_label (s_ip4, 1);
375
	g_assert_cmpstr (label, ==, "");
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390


	/* Test explicit property assignment */
	g_object_get (G_OBJECT (s_ip4),
	              NM_SETTING_IP4_CONFIG_ADDRESSES, &addrs,
	              "address-labels", &labels,
	              NULL);

	nm_setting_ip4_config_clear_addresses (s_ip4);
	g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 0);

	/* Setting addrs but not labels will result in empty labels */
	g_object_set (G_OBJECT (s_ip4),
	              NM_SETTING_IP4_CONFIG_ADDRESSES, addrs,
	              NULL);
391
	g_ptr_array_unref (addrs);
392 393 394 395 396 397
	nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
	g_assert_no_error (error);
	g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2);

	addr = nm_setting_ip4_config_get_address (s_ip4, 0);
	g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x02020202);
398
	label = _nm_setting_ip4_config_get_address_label (s_ip4, 0);
399
	g_assert_cmpstr (label, ==, "");
400 401 402

	addr = nm_setting_ip4_config_get_address (s_ip4, 1);
	g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303);
403
	label = _nm_setting_ip4_config_get_address_label (s_ip4, 1);
404
	g_assert_cmpstr (label, ==, "");
405 406 407 408 409

	/* Setting labels now will leave addresses untouched */
	g_object_set (G_OBJECT (s_ip4),
	              "address-labels", labels,
	              NULL);
410
	g_strfreev (labels);
411 412 413 414 415 416
	nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
	g_assert_no_error (error);
	g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2);

	addr = nm_setting_ip4_config_get_address (s_ip4, 0);
	g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x02020202);
417
	label = _nm_setting_ip4_config_get_address_label (s_ip4, 0);
418 419 420 421
	g_assert_cmpstr (label, ==, "eth0:1");

	addr = nm_setting_ip4_config_get_address (s_ip4, 1);
	g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303);
422
	label = _nm_setting_ip4_config_get_address_label (s_ip4, 1);
423
	g_assert_cmpstr (label, ==, "");
424 425 426 427

	/* Setting labels to a value that's too short or too long will result in
	 * the setting not verifying.
	 */
428
	labels = g_strsplit ("eth0:2", ",", -1);
429 430 431
	g_object_set (G_OBJECT (s_ip4),
	              "address-labels", labels,
	              NULL);
432
	g_strfreev (labels);
433 434

	nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
435
	g_assert_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY);
436 437 438
	g_assert (g_str_has_prefix (error->message, "ipv4.address-labels:"));
	g_clear_error (&error);

439
	labels = g_strsplit ("eth0:2,eth0:3", ",", -1);
440 441 442
	g_object_set (G_OBJECT (s_ip4),
	              "address-labels", labels,
	              NULL);
443
	g_strfreev (labels);
444 445 446
	nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
	g_assert_no_error (error);

447
	labels = g_strsplit ("eth0:2,eth0:3,eth0:4", ",", -1);
448 449 450
	g_object_set (G_OBJECT (s_ip4),
	              "address-labels", labels,
	              NULL);
451
	g_strfreev (labels);
452
	nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
453
	g_assert_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY);
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 483 484 485 486 487 488 489 490 491 492 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 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	g_assert (g_str_has_prefix (error->message, "ipv4.address-labels:"));
	g_clear_error (&error);

	g_object_unref (s_ip4);
}

static void
test_setting_gsm_apn_spaces (void)
{
	NMSettingGsm *s_gsm;
	const char *tmp;

	s_gsm = (NMSettingGsm *) nm_setting_gsm_new ();
	ASSERT (s_gsm != NULL,
	        "gsm-apn-spaces",
	        "error creating GSM setting");

	/* Trailing space */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "foobar ", NULL);
	tmp = nm_setting_gsm_get_apn (s_gsm);
	ASSERT (tmp != NULL,
	        "gsm-apn-spaces", "empty APN");
	ASSERT (strcmp (tmp, "foobar") == 0,
	        "gsm-apn-spaces", "unexpected APN");

	/* Leading space */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, " foobar", NULL);
	tmp = nm_setting_gsm_get_apn (s_gsm);
	ASSERT (tmp != NULL,
	        "gsm-apn-spaces", "empty APN");
	ASSERT (strcmp (tmp, "foobar") == 0,
	        "gsm-apn-spaces", "unexpected APN");
}

static void
test_setting_gsm_apn_bad_chars (void)
{
	NMSettingGsm *s_gsm;

	s_gsm = (NMSettingGsm *) nm_setting_gsm_new ();
	ASSERT (s_gsm != NULL,
	        "gsm-apn-bad-chars",
	        "error creating GSM setting");

	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);
	ASSERT (nm_setting_verify (NM_SETTING (s_gsm), NULL, NULL) == TRUE,
	        "gsm-apn-bad-chars", "unexpectedly invalid GSM setting");

	/* Random invalid chars */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "@#%$@#%@#%", NULL);
	ASSERT (nm_setting_verify (NM_SETTING (s_gsm), NULL, NULL) == FALSE,
	        "gsm-apn-bad-chars", "unexpectedly valid GSM setting");

	/* Spaces */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "foobar baz", NULL);
	ASSERT (nm_setting_verify (NM_SETTING (s_gsm), NULL, NULL) == FALSE,
	        "gsm-apn-bad-chars", "unexpectedly valid GSM setting");

	/* 0 characters long */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "", NULL);
	ASSERT (nm_setting_verify (NM_SETTING (s_gsm), NULL, NULL) == FALSE,
	        "gsm-apn-bad-chars", "unexpectedly valid GSM setting");

	/* 65-character long */
	g_object_set (s_gsm, NM_SETTING_GSM_APN, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl1", NULL);
	ASSERT (nm_setting_verify (NM_SETTING (s_gsm), NULL, NULL) == FALSE,
	        "gsm-apn-bad-chars", "unexpectedly valid GSM setting");
}

static void
test_setting_gsm_apn_underscore (void)
{
	NMSettingGsm *s_gsm;
	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);
}

static void
test_setting_gsm_without_number (void)
{
	NMSettingGsm *s_gsm;
	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);
562
	g_assert_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY);
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
	g_error_free (error);
}

static NMSettingWirelessSecurity *
make_test_wsec_setting (const char *detail)
{
	NMSettingWirelessSecurity *s_wsec;

	s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
	ASSERT (s_wsec != NULL, detail, "error creating setting");

	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",
578
	              NM_SETTING_WIRELESS_SECURITY_PSK_FLAGS, NM_SETTING_SECRET_FLAG_NOT_SAVED,
579 580 581 582 583 584
	              NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, "aaaaaaaaaa",
	              NULL);

	return s_wsec;
}

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
#define ASSERT_CONTAINS(vardict, key, test_name, msg) \
	{ \
		GVariant *value; \
		value = g_variant_lookup_value (vardict, key, NULL); \
		ASSERT (value != NULL, test_name, msg); \
		g_variant_unref (value); \
	}

#define ASSERT_NOT_CONTAINS(vardict, key, test_name, msg) \
	{ \
		GVariant *value; \
		value = g_variant_lookup_value (vardict, key, NULL); \
		ASSERT (value == NULL, test_name, msg); \
	}

600
static void
601
test_setting_to_dbus_all (void)
602 603
{
	NMSettingWirelessSecurity *s_wsec;
604
	GVariant *dict;
605

606
	s_wsec = make_test_wsec_setting ("setting-to-dbus-all");
607

608
	dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_ALL);
609 610

	/* Make sure all keys are there */
611
	ASSERT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
612
	        "setting-to-dbus-all", "unexpectedly missing " NM_SETTING_WIRELESS_SECURITY_KEY_MGMT);
613
	ASSERT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME,
614
	        "setting-to-dbus-all", "unexpectedly missing " NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME);
615
	ASSERT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_PSK,
616
	        "setting-to-dbus-all", "unexpectedly missing " NM_SETTING_WIRELESS_SECURITY_PSK);
617
	ASSERT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0,
618
	        "setting-to-dbus-all", "unexpectedly missing " NM_SETTING_WIRELESS_SECURITY_WEP_KEY0);
619

620
	g_variant_unref (dict);
621 622 623 624
	g_object_unref (s_wsec);
}

static void
625
test_setting_to_dbus_no_secrets (void)
626 627
{
	NMSettingWirelessSecurity *s_wsec;
628
	GVariant *dict;
629

630
	s_wsec = make_test_wsec_setting ("setting-to-dbus-no-secrets");
631

632
	dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_NO_SECRETS);
633 634

	/* Make sure non-secret keys are there */
635 636 637 638
	ASSERT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
	                 "setting-to-dbus-no-secrets", "unexpectedly missing " NM_SETTING_WIRELESS_SECURITY_KEY_MGMT);
	ASSERT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME,
	                 "setting-to-dbus-no-secrets", "unexpectedly missing " NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME);
639 640

	/* Make sure secrets are not there */
641 642 643 644
	ASSERT_NOT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_PSK,
	                     "setting-to-dbus-no-secrets", "unexpectedly present " NM_SETTING_WIRELESS_SECURITY_PSK);
	ASSERT_NOT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0,
	                     "setting-to-dbus-no-secrets", "unexpectedly present " NM_SETTING_WIRELESS_SECURITY_WEP_KEY0);
645

646
	g_variant_unref (dict);
647 648 649 650
	g_object_unref (s_wsec);
}

static void
651
test_setting_to_dbus_only_secrets (void)
652 653
{
	NMSettingWirelessSecurity *s_wsec;
654
	GVariant *dict;
655

656
	s_wsec = make_test_wsec_setting ("setting-to-dbus-only-secrets");
657

658
	dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_ONLY_SECRETS);
659

660 661 662 663 664
	/* Make sure non-secret keys are not there */
	ASSERT_NOT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
	                     "setting-to-dbus-only-secrets", "unexpectedly present " NM_SETTING_WIRELESS_SECURITY_KEY_MGMT);
	ASSERT_NOT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME,
	                     "setting-to-dbus-only-secrets", "unexpectedly present " NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME);
665

666 667 668 669 670
	/* Make sure secrets are there */
	ASSERT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_PSK,
	                 "setting-to-dbus-only-secrets", "unexpectedly missing " NM_SETTING_WIRELESS_SECURITY_PSK);
	ASSERT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0,
	                 "setting-to-dbus-only-secrets", "unexpectedly missing " NM_SETTING_WIRELESS_SECURITY_WEP_KEY0);
671

672
	g_variant_unref (dict);
673 674 675
	g_object_unref (s_wsec);
}

676 677 678 679
static void
test_setting_to_dbus_transform (void)
{
	NMSetting *s_wired;
680
	GVariant *dict, *val;
681
	const char *test_mac_address = "11:22:33:44:55:66";
682 683 684
	const guint8 *dbus_mac_address;
	guint8 cmp_mac_address[ETH_ALEN];
	gsize len;
685 686 687 688 689 690 691 692

	s_wired = nm_setting_wired_new ();
	g_object_set (s_wired,
	              NM_SETTING_WIRED_MAC_ADDRESS, test_mac_address,
	              NULL);

	g_assert_cmpstr (nm_setting_wired_get_mac_address (NM_SETTING_WIRED (s_wired)), ==, test_mac_address);

693 694
	dict = _nm_setting_to_dbus (s_wired, NULL, NM_CONNECTION_SERIALIZE_ALL);
	g_assert (dict != NULL);
695

696
	val = g_variant_lookup_value (dict, NM_SETTING_WIRED_MAC_ADDRESS, G_VARIANT_TYPE_BYTESTRING);
697 698
	g_assert (val != NULL);

699 700
	dbus_mac_address = g_variant_get_fixed_array (val, &len, 1);
	g_assert_cmpint (len, ==, ETH_ALEN);
701

702 703
	nm_utils_hwaddr_aton (test_mac_address, cmp_mac_address, ETH_ALEN);
	g_assert (memcmp (dbus_mac_address, cmp_mac_address, ETH_ALEN) == 0);
704

705 706
	g_variant_unref (val);
	g_variant_unref (dict);
707 708 709
	g_object_unref (s_wired);
}

710 711 712
static void
test_setting_to_dbus_enum (void)
{
713
	NMSetting *s_ip6, *s_wsec, *s_serial;
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
	GVariant *dict, *val;

	/* enum */
	s_ip6 = nm_setting_ip6_config_new ();
	g_object_set (s_ip6,
	              NM_SETTING_IP6_CONFIG_IP6_PRIVACY, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR,
	              NULL);

	dict = _nm_setting_to_dbus (s_ip6, NULL, NM_CONNECTION_SERIALIZE_ALL);
	g_assert (dict != NULL);

	val = g_variant_lookup_value (dict, NM_SETTING_IP6_CONFIG_IP6_PRIVACY, G_VARIANT_TYPE_INT32);
	g_assert (val != NULL);
	g_assert_cmpint (g_variant_get_int32 (val), ==, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR);
	g_variant_unref (val);

	g_variant_unref (dict);
	g_object_unref (s_ip6);

	/* flags (and a transformed enum) */
	s_wsec = nm_setting_wireless_security_new ();
	g_object_set (s_wsec,
	              NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE, NM_WEP_KEY_TYPE_KEY,
	              NM_SETTING_WIRELESS_SECURITY_WEP_KEY_FLAGS, (NM_SETTING_SECRET_FLAG_AGENT_OWNED |
	                                                           NM_SETTING_SECRET_FLAG_NOT_SAVED),
	              NULL);

	dict = _nm_setting_to_dbus (s_wsec, NULL, NM_CONNECTION_SERIALIZE_ALL);
	g_assert (dict != NULL);

	val = g_variant_lookup_value (dict, NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE, G_VARIANT_TYPE_UINT32);
	g_assert (val != NULL);
	g_assert_cmpint (g_variant_get_uint32 (val), ==, NM_WEP_KEY_TYPE_KEY);
	g_variant_unref (val);

	val = g_variant_lookup_value (dict, NM_SETTING_WIRELESS_SECURITY_WEP_KEY_FLAGS, G_VARIANT_TYPE_UINT32);
	g_assert (val != NULL);
	g_assert_cmpint (g_variant_get_uint32 (val), ==, (NM_SETTING_SECRET_FLAG_AGENT_OWNED |
	                                                  NM_SETTING_SECRET_FLAG_NOT_SAVED));
	g_variant_unref (val);

	g_variant_unref (dict);
	g_object_unref (s_wsec);
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

	/* another transformed enum */
	s_serial = nm_setting_serial_new ();
	g_object_set (s_serial,
	              NM_SETTING_SERIAL_PARITY, NM_SETTING_SERIAL_PARITY_ODD,
	              NULL);

	dict = _nm_setting_to_dbus (s_serial, NULL, NM_CONNECTION_SERIALIZE_ALL);
	g_assert (dict != NULL);

	val = g_variant_lookup_value (dict, NM_SETTING_SERIAL_PARITY, G_VARIANT_TYPE_BYTE);
	g_assert (val != NULL);
	g_assert_cmpint (g_variant_get_byte (val), ==, 'o');
	g_variant_unref (val);

	g_variant_unref (dict);
	g_object_unref (s_serial);
774 775
}

776
static void
777
test_connection_to_dbus_setting_name (void)
778 779 780
{
	NMConnection *connection;
	NMSettingWirelessSecurity *s_wsec;
781
	GVariant *dict;
782

783
	connection = nm_simple_connection_new ();
784
	s_wsec = make_test_wsec_setting ("connection-to-dbus-setting-name");
785 786
	nm_connection_add_setting (connection, NM_SETTING (s_wsec));

787
	dict = nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL);
788

789
	/* Make sure the keys of the first level dict are setting names, not
790 791
	 * the GType name of the setting objects.
	 */
792 793
	ASSERT_CONTAINS (dict, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
	                 "connection-to-dbus-setting-name", "unexpectedly missing " NM_SETTING_WIRELESS_SECURITY_SETTING_NAME);
794

795
	g_variant_unref (dict);
796 797 798
	g_object_unref (connection);
}

799 800 801 802 803
static void
test_connection_to_dbus_deprecated_props (void)
{
	NMConnection *connection;
	NMSetting *s_wireless;
804
	GBytes *ssid;
805
	NMSettingWirelessSecurity *s_wsec;
806
	GVariant *dict, *wireless_dict, *sec_val;
807 808 809 810 811 812 813

	connection = nmtst_create_minimal_connection ("test-connection-to-dbus-deprecated-props",
	                                              NULL,
	                                              NM_SETTING_WIRELESS_SETTING_NAME,
	                                              NULL);

	s_wireless = nm_setting_wireless_new ();
814
	ssid = g_bytes_new ("1234567", 7);
815 816 817
	g_object_set (s_wireless,
	              NM_SETTING_WIRELESS_SSID, ssid,
	              NULL);
818
	g_bytes_unref (ssid);
819 820
	nm_connection_add_setting (connection, s_wireless);

821 822 823
	/* Serialization should not have an 802-11-wireless.security property */
	dict = nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL);
	g_assert (dict != NULL);
824

825 826
	wireless_dict = g_variant_lookup_value (dict, NM_SETTING_WIRELESS_SETTING_NAME, NM_VARIANT_TYPE_SETTING);
	g_assert (wireless_dict != NULL);
827

828
	sec_val = g_variant_lookup_value (wireless_dict, "security", NULL);
829 830
	g_assert (sec_val == NULL);

831 832
	g_variant_unref (wireless_dict);
	g_variant_unref (dict);
833 834 835 836 837

	/* Now add an NMSettingWirelessSecurity and try again */
	s_wsec = make_test_wsec_setting ("test-connection-to-dbus-deprecated-props");
	nm_connection_add_setting (connection, NM_SETTING (s_wsec));

838 839
	dict = nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL);
	g_assert (dict != NULL);
840

841 842
	wireless_dict = g_variant_lookup_value (dict, NM_SETTING_WIRELESS_SETTING_NAME, NM_VARIANT_TYPE_SETTING);
	g_assert (wireless_dict != NULL);
843

844 845 846
	sec_val = g_variant_lookup_value (wireless_dict, "security", NULL);
	g_assert (g_variant_is_of_type (sec_val, G_VARIANT_TYPE_STRING));
	g_assert_cmpstr (g_variant_get_string (sec_val, NULL), ==, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME);
847

848 849 850
	g_variant_unref (sec_val);
	g_variant_unref (wireless_dict);
	g_variant_unref (dict);
851 852 853
	g_object_unref (connection);
}

854
static void
855
test_setting_new_from_dbus (void)
856 857
{
	NMSettingWirelessSecurity *s_wsec;
858
	GVariant *dict;
859

860 861
	s_wsec = make_test_wsec_setting ("setting-new-from-dbus");
	dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_ALL);
862 863
	g_object_unref (s_wsec);

864 865
	s_wsec = (NMSettingWirelessSecurity *) _nm_setting_new_from_dbus (NM_TYPE_SETTING_WIRELESS_SECURITY, dict, NULL, NULL);
	g_variant_unref (dict);
866 867 868 869 870 871 872 873

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

874 875 876 877
static void
test_setting_new_from_dbus_transform (void)
{
	NMSetting *s_wired;
878 879
	GVariant *dict;
	GVariantBuilder builder;
880
	const char *test_mac_address = "11:22:33:44:55:66";
881
	guint8 dbus_mac_address[ETH_ALEN];
882 883
	GError *error = NULL;

884
	nm_utils_hwaddr_aton (test_mac_address, dbus_mac_address, ETH_ALEN);
885

886 887 888 889 890 891
	g_variant_builder_init (&builder, NM_VARIANT_TYPE_SETTING);
	g_variant_builder_add (&builder, "{sv}",
	                       NM_SETTING_WIRED_MAC_ADDRESS,
	                       g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
	                                                  dbus_mac_address, ETH_ALEN, 1));
	dict = g_variant_builder_end (&builder);
892

893
	s_wired = _nm_setting_new_from_dbus (NM_TYPE_SETTING_WIRED, dict, NULL, &error);
894 895 896 897
	g_assert_no_error (error);

	g_assert_cmpstr (nm_setting_wired_get_mac_address (NM_SETTING_WIRED (s_wired)), ==, test_mac_address);

898
	g_variant_unref (dict);
899 900 901
	g_object_unref (s_wired);
}

902 903 904 905 906
static void
test_setting_new_from_dbus_enum (void)
{
	NMSettingIP6Config *s_ip6;
	NMSettingWirelessSecurity *s_wsec;
907
	NMSettingSerial *s_serial;
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
	GVariant *dict;
	GVariantBuilder builder;
	GError *error = NULL;

	/* enum */
	g_variant_builder_init (&builder, NM_VARIANT_TYPE_SETTING);
	g_variant_builder_add (&builder, "{sv}",
	                       NM_SETTING_IP6_CONFIG_IP6_PRIVACY,
	                       g_variant_new_int32 (NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR));
	dict = g_variant_builder_end (&builder);

	s_ip6 = (NMSettingIP6Config *) _nm_setting_new_from_dbus (NM_TYPE_SETTING_IP6_CONFIG, dict, NULL, &error);
	g_assert_no_error (error);

	g_assert_cmpint (nm_setting_ip6_config_get_ip6_privacy (s_ip6), ==, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR);

	g_variant_unref (dict);
	g_object_unref (s_ip6);

	/* flags (and a transformed enum) */
	g_variant_builder_init (&builder, NM_VARIANT_TYPE_SETTING);
	g_variant_builder_add (&builder, "{sv}",
	                       NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE,
	                       g_variant_new_uint32 (NM_WEP_KEY_TYPE_KEY));
	g_variant_builder_add (&builder, "{sv}",
	                       NM_SETTING_WIRELESS_SECURITY_WEP_KEY_FLAGS,
	                       g_variant_new_uint32 (NM_SETTING_SECRET_FLAG_AGENT_OWNED |
	                                             NM_SETTING_SECRET_FLAG_NOT_SAVED));
	dict = g_variant_builder_end (&builder);

	s_wsec = (NMSettingWirelessSecurity *) _nm_setting_new_from_dbus (NM_TYPE_SETTING_WIRELESS_SECURITY, dict, NULL, &error);
	g_assert_no_error (error);

	g_assert_cmpint (nm_setting_wireless_security_get_wep_key_type (s_wsec), ==, NM_WEP_KEY_TYPE_KEY);
	g_assert_cmpint (nm_setting_wireless_security_get_wep_key_flags (s_wsec), ==, (NM_SETTING_SECRET_FLAG_AGENT_OWNED |
	                                                                               NM_SETTING_SECRET_FLAG_NOT_SAVED));

	g_variant_unref (dict);
	g_object_unref (s_wsec);
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

	/* another transformed enum */
	g_variant_builder_init (&builder, NM_VARIANT_TYPE_SETTING);
	g_variant_builder_add (&builder, "{sv}",
	                       NM_SETTING_SERIAL_PARITY,
	                       g_variant_new_byte ('E'));
	dict = g_variant_builder_end (&builder);

	s_serial = (NMSettingSerial *) _nm_setting_new_from_dbus (NM_TYPE_SETTING_SERIAL, dict, NULL, &error);
	g_assert_no_error (error);

	g_assert_cmpint (nm_setting_serial_get_parity (s_serial), ==, NM_SETTING_SERIAL_PARITY_EVEN);

	g_variant_unref (dict);
	g_object_unref (s_serial);
962 963
}

964 965 966 967 968 969 970 971
static NMConnection *
new_test_connection (void)
{
	NMConnection *connection;
	NMSetting *setting;
	char *uuid;
	guint64 timestamp = time (NULL);

972
	connection = nm_simple_connection_new ();
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000

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

1001 1002
static GVariant *
new_connection_dict (char **out_uuid,
1003 1004 1005
                     const char **out_expected_id,
                     const char **out_expected_ip6_method)
{
1006
	GVariantBuilder conn_builder, setting_builder;
1007

1008
	g_variant_builder_init (&conn_builder, NM_VARIANT_TYPE_CONNECTION);
1009 1010 1011 1012 1013 1014

	*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 */
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
	g_variant_builder_init (&setting_builder, NM_VARIANT_TYPE_SETTING);
	g_variant_builder_add (&setting_builder, "{sv}",
	                       NM_SETTING_NAME,
	                       g_variant_new_string (NM_SETTING_CONNECTION_SETTING_NAME));
	g_variant_builder_add (&setting_builder, "{sv}",
	                       NM_SETTING_CONNECTION_ID,
	                       g_variant_new_string (*out_expected_id));
	g_variant_builder_add (&setting_builder, "{sv}",
	                       NM_SETTING_CONNECTION_UUID,
	                       g_variant_new_string (*out_uuid));
	g_variant_builder_add (&setting_builder, "{sv}",
	                       NM_SETTING_CONNECTION_TYPE,
	                       g_variant_new_string (NM_SETTING_WIRED_SETTING_NAME));

	g_variant_builder_add (&conn_builder, "{sa{sv}}",
	                       NM_SETTING_CONNECTION_SETTING_NAME,
	                       &setting_builder);
1032 1033

	/* Wired setting */
1034 1035 1036 1037
	g_variant_builder_init (&setting_builder, NM_VARIANT_TYPE_SETTING);
	g_variant_builder_add (&conn_builder, "{sa{sv}}",
	                       NM_SETTING_WIRED_SETTING_NAME,
	                       &setting_builder);
1038 1039

	/* IP6 */
1040 1041 1042 1043 1044 1045 1046
	g_variant_builder_init (&setting_builder, NM_VARIANT_TYPE_SETTING);
	g_variant_builder_add (&setting_builder, "{sv}",
	                       NM_SETTING_IP6_CONFIG_METHOD,
	                       g_variant_new_string (*out_expected_ip6_method));
	g_variant_builder_add (&conn_builder, "{sa{sv}}",
	                       NM_SETTING_IP6_CONFIG_SETTING_NAME,
	                       &setting_builder);
1047

1048
	return g_variant_builder_end (&conn_builder);
1049 1050 1051 1052 1053 1054
}

static void
test_connection_replace_settings ()
{
	NMConnection *connection;
1055
	GVariant *new_settings;
1056 1057 1058 1059 1060 1061 1062 1063 1064
	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 ();

1065
	new_settings = new_connection_dict (&uuid, &expected_id, &expected_method);
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
	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);
1086
	g_variant_unref (new_settings);
1087 1088 1089 1090 1091 1092 1093 1094 1095
	g_object_unref (connection);
}

static void
test_connection_replace_settings_from_connection ()
{
	NMConnection *connection, *replacement;
	NMSettingConnection *s_con;
	NMSetting *setting;
1096
	GBytes *ssid;
1097 1098 1099 1100 1101 1102
	char *uuid = NULL;
	const char *expected_id = "Awesome connection";

	connection = new_test_connection ();
	g_assert (connection);

1103
	replacement = nm_simple_connection_new ();
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	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);

1122
	ssid = g_bytes_new ("1234567", 7);
1123 1124 1125 1126
	g_object_set (setting,
	              NM_SETTING_WIRELESS_SSID, ssid,
	              NM_SETTING_WIRELESS_MODE, "infrastructure",
	              NULL);
1127
	g_bytes_unref (ssid);
1128 1129 1130
	nm_connection_add_setting (replacement, setting);

	/* Replace settings and test */
1131
	nm_connection_replace_settings_from_connection (connection, replacement);
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146

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

1147 1148 1149
static void
test_connection_replace_settings_bad (void)
{
1150
	NMConnection *connection, *new_connection;
1151 1152
	GVariant *new_settings;
	GVariantBuilder builder, setting_builder;
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
	GError *error = NULL;
	gboolean success;
	NMSettingConnection *s_con;

	new_connection = new_test_connection ();
	g_assert (nm_connection_verify (new_connection, NULL));
	s_con = nm_connection_get_setting_connection (new_connection);
	g_object_set (s_con,
	              NM_SETTING_CONNECTION_UUID, NULL,
	              NM_SETTING_CONNECTION_ID, "bad-connection",
	              NULL);
	g_assert (!nm_connection_verify (new_connection, NULL));

1166 1167 1168 1169 1170 1171
	/* nm_connection_replace_settings_from_connection() should succeed */
	connection = new_test_connection ();
	nm_connection_replace_settings_from_connection (connection, new_connection);
	g_assert_cmpstr (nm_connection_get_id (connection), ==, "bad-connection");
	g_assert (!nm_connection_verify (connection, NULL));
	g_object_unref (connection);
1172

1173
	/* nm_connection_replace_settings() should succeed */
1174 1175 1176
	new_settings = nm_connection_to_dbus (new_connection, NM_CONNECTION_SERIALIZE_ALL);
	g_assert (new_settings != NULL);

1177 1178 1179 1180 1181 1182 1183 1184
	connection = new_test_connection ();
	success = nm_connection_replace_settings (connection, new_settings, &error);
	g_assert_no_error (error);
	g_assert (success);

	g_assert_cmpstr (nm_connection_get_id (connection), ==, "bad-connection");
	g_assert (!nm_connection_verify (connection, NULL));
	g_object_unref (connection);
1185
	g_variant_unref (new_settings);
1186

1187 1188 1189 1190 1191 1192 1193
	/* But given an invalid dict, it should fail */
	g_variant_builder_init (&builder, NM_VARIANT_TYPE_CONNECTION);
	g_variant_builder_init (&setting_builder, NM_VARIANT_TYPE_SETTING);
	g_variant_builder_add (&builder, "{sa{sv}}",
	                       "ip-over-avian-carrier",
	                       &setting_builder);
	new_settings = g_variant_builder_end (&builder);
1194 1195

	connection = new_test_connection ();
1196
	success = nm_connection_replace_settings (connection, new_settings, &error);
1197
	g_assert_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_SETTING);
1198 1199
	g_assert (!success);

1200 1201
	g_assert (nm_connection_verify (connection, NULL));
	g_object_unref (connection);
1202

1203
	g_variant_unref (new_settings);
1204 1205 1206
	g_object_unref (new_connection);
}

1207
static void
1208
test_connection_new_from_dbus ()
1209 1210
{
	NMConnection *connection;
1211
	GVariant *new_settings;
1212 1213 1214 1215 1216 1217
	GError *error = NULL;
	NMSettingConnection *s_con;
	NMSettingIP6Config *s_ip6;
	char *uuid = NULL;
	const char *expected_id = NULL, *expected_method = NULL;

1218
	new_settings = new_connection_dict (&uuid, &expected_id, &expected_method);
1219 1220 1221
	g_assert (new_settings);

	/* Replace settings and test */
1222
	connection = nm_simple_connection_new_from_dbus (new_settings, &error);
1223 1224 1225 1226 1227 1228