test-ifcfg-rh.c 366 KB
Newer Older
Dan Williams's avatar
Dan Williams committed
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 -*- */
/* NetworkManager system settings service - keyfile plugin
 *
 * 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 of the License, 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 (C) 2008 - 2011 Red Hat, Inc.
Dan Williams's avatar
Dan Williams committed
19 20
 */

21
#include "nm-default.h"
22

Dan Williams's avatar
Dan Williams committed
23 24 25
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
26
#include <linux/pkt_sched.h>
Dan Williams's avatar
Dan Williams committed
27 28 29
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
30 31
#include <sys/types.h>
#include <sys/stat.h>
Dan Williams's avatar
Dan Williams committed
32

33 34 35
#include "nm-utils.h"
#include "nm-setting-connection.h"
#include "nm-setting-wired.h"
36
#include "nm-setting-user.h"
37 38 39 40 41 42 43 44
#include "nm-setting-wireless.h"
#include "nm-setting-wireless-security.h"
#include "nm-setting-ip4-config.h"
#include "nm-setting-ip6-config.h"
#include "nm-setting-8021x.h"
#include "nm-setting-pppoe.h"
#include "nm-setting-ppp.h"
#include "nm-setting-vpn.h"
45
#include "nm-setting-ethtool.h"
46 47 48 49 50
#include "nm-setting-gsm.h"
#include "nm-setting-cdma.h"
#include "nm-setting-serial.h"
#include "nm-setting-vlan.h"
#include "nm-setting-dcb.h"
51
#include "nm-core-internal.h"
52
#include "nm-ethtool-utils.h"
Dan Williams's avatar
Dan Williams committed
53

54
#include "NetworkManagerUtils.h"
Dan Williams's avatar
Dan Williams committed
55

56 57 58 59
#include "settings/plugins/ifcfg-rh/nms-ifcfg-rh-common.h"
#include "settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.h"
#include "settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.h"
#include "settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h"
Dan Williams's avatar
Dan Williams committed
60

61
#include "nm-test-utils-core.h"
62

63 64 65 66 67
#define TEST_IFCFG_DIR          NM_BUILD_SRCDIR"/src/settings/plugins/ifcfg-rh/tests/network-scripts"
#define TEST_SCRATCH_DIR        NM_BUILD_BUILDDIR"/src/settings/plugins/ifcfg-rh/tests/network-scripts"
#define TEST_SCRATCH_DIR_TMP    TEST_SCRATCH_DIR"/tmp"

#define TEST_SCRATCH_ALIAS_BASE TEST_SCRATCH_DIR"/ifcfg-alias0"
68

69 70
/*****************************************************************************/

71 72 73 74 75 76 77 78 79 80 81 82
#define _svOpenFile(testfile) \
	({ \
		shvarFile *_f; \
		GError *_error = NULL; \
		const char *_testfile = (testfile); \
		\
		g_assert (_testfile); \
		_f = svOpenFile (_testfile, &_error); \
		nmtst_assert_success (_f, _error); \
		_f; \
	})

83
#define _svGetValue_check(f, key, expected_value) \
84
	G_STMT_START { \
85
		const char *_val; \
86
		gs_free char *_to_free = NULL; \
87 88 89
		gs_free char *_val_string = NULL; \
		shvarFile *const _f = (f); \
		const char *const _key = (key); \
90
		\
91
		_val_string = svGetValueStr_cp (_f, _key); \
92 93 94 95
		_val = svGetValue (_f, _key, &_to_free); \
		g_assert_cmpstr (_val, ==, (expected_value)); \
		g_assert (   (!_val_string && (!_val || !_val[0])) \
		          || ( _val_string && nm_streq0 (_val, _val_string))); \
96 97
	} G_STMT_END

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
static void
_assert_reread_same (NMConnection *connection, NMConnection *reread)
{
	nmtst_assert_connection_verifies_without_normalization (reread);
	nmtst_assert_connection_equals (connection, TRUE, reread, FALSE);
}

static void
_assert_reread_same_FIXME (NMConnection *connection, NMConnection *reread)
{
	gs_unref_object NMConnection *connection_normalized = NULL;
	gs_unref_hashtable GHashTable *settings = NULL;

	/* FIXME: these assertion failures should not happen as we expect
	 * that re-reading a connection after write yields the same result.
	 *
	 * Needs investation and fixing. */
	nmtst_assert_connection_verifies_without_normalization (reread);

	connection_normalized = nmtst_connection_duplicate_and_normalize (connection);

	g_assert (!nm_connection_compare (connection_normalized, reread, NM_SETTING_COMPARE_FLAG_EXACT));
	g_assert (!nm_connection_diff (connection_normalized, reread, NM_SETTING_COMPARE_FLAG_EXACT, &settings));
}

123 124
/* dummy path for an "expected" file, meaning: don't check for expected
 * written ifcfg file. */
125
static const char NO_EXPECTED[1];
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

static void
_assert_expected_content (NMConnection *connection, const char *filename, const char *expected)
{
	gs_free char *content_expectd = NULL;
	gs_free char *content_written = NULL;
	GError *error = NULL;
	gsize len_expectd = 0;
	gsize len_written = 0;
	gboolean success;
	const char *uuid = NULL;

	g_assert (NM_IS_CONNECTION (connection));
	g_assert (filename);
	g_assert (g_file_test (filename, G_FILE_TEST_EXISTS));

	g_assert (expected);
	if (expected == NO_EXPECTED)
		return;

	success = g_file_get_contents (filename, &content_written, &len_written, &error);
	nmtst_assert_success (success, error);

	success = g_file_get_contents (expected, &content_expectd, &len_expectd, &error);
	nmtst_assert_success (success, error);

	{
		gsize i, j;

		for (i = 0; i < len_expectd; ) {
			if (content_expectd[i] != '$') {
				i++;
				continue;
			}
			if (g_str_has_prefix (&content_expectd[i], "${UUID}")) {
				GString *str;

				if (!uuid) {
					uuid = nm_connection_get_uuid (connection);
					g_assert (uuid);
				}

				j = strlen (uuid);

				str = g_string_new_len (content_expectd, len_expectd);
				g_string_erase (str, i, NM_STRLEN ("${UUID}"));
				g_string_insert_len (str, i, uuid, j);

				g_free (content_expectd);
				len_expectd = str->len;
				content_expectd = g_string_free (str, FALSE);
				i += j;
				continue;
			}

			/* other '$' is not supported. If need be, support escaping of
			 * '$' via '$$'. */
			g_assert_not_reached ();
		}
	}

	if (   len_expectd != len_written
	    || memcmp (content_expectd, content_written, len_expectd) != 0) {
189 190
		if (   g_getenv ("NMTST_IFCFG_RH_UPDATE_EXPECTED")
		    || nm_streq0 (g_getenv ("NM_TEST_REGENERATE"), "1")) {
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
			if (uuid) {
				gs_free char *search = g_strdup_printf ("UUID=%s\n", uuid);
				const char *s;
				gsize i;
				GString *str;

				s = content_written;
				while (TRUE) {
					s = strstr (s, search);
					g_assert (s);
					if (   s == content_written
					    || s[-1] == '\n')
						break;
					s += strlen (search);
				}

				i = s - content_written;

				str = g_string_new_len (content_written, len_written);
				g_string_erase (str, i, strlen (search));
				g_string_insert (str, i, "UUID=${UUID}\n");

				len_written = str->len;
				content_written = g_string_free (str, FALSE);
			}
			success = g_file_set_contents (expected, content_written, len_written, &error);
			nmtst_assert_success (success, error);
		} else {
			g_error ("The content of \"%s\" (%zu) differs from \"%s\" (%zu). Set NMTST_IFCFG_RH_UPDATE_EXPECTED=yes to update the files inplace\n\n>>>%s<<<\n\n>>>%s<<<\n",
			         filename, len_written,
			         expected, len_expectd,
			         content_written,
			         content_expectd);
		}
	}
}

#define _writer_update_connection_reread(connection, ifcfg_dir, filename, expected, out_reread, out_reread_same) \
229
	G_STMT_START { \
230
		gs_unref_object NMConnection *_connection = nmtst_connection_duplicate_and_normalize (connection); \
231 232
		NMConnection **_out_reread = (out_reread); \
		gboolean *_out_reread_same = (out_reread_same); \
233 234
		const char *_ifcfg_dir = (ifcfg_dir); \
		const char *_filename = (filename); \
235
		const char *_expected = (expected); \
236 237 238 239 240 241
		GError *_error = NULL; \
		gboolean _success; \
		\
		g_assert (_ifcfg_dir && _ifcfg_dir[0]); \
		g_assert (_filename && _filename[0]); \
		\
242
		_success = nms_ifcfg_rh_writer_write_connection (_connection, _ifcfg_dir, _filename, NULL, _out_reread, _out_reread_same, &_error); \
243
		nmtst_assert_success (_success, _error); \
244
		_assert_expected_content (_connection, _filename, _expected); \
245 246
	} G_STMT_END

247
#define _writer_update_connection(connection, ifcfg_dir, filename, expected) \
248 249 250 251 252
	G_STMT_START { \
		gs_unref_object NMConnection *_reread = NULL; \
		NMConnection *_c = (connection); \
		gboolean _reread_same = FALSE; \
		\
253
		_writer_update_connection_reread (_c, ifcfg_dir, filename, expected, &_reread, &_reread_same); \
254 255 256 257
		_assert_reread_same (_c, _reread); \
		g_assert (_reread_same); \
	} G_STMT_END

258 259 260 261 262 263 264 265
static NMConnection *
_connection_from_file (const char *filename,
                       const char *network_file,
                       const char *test_type,
                       char **out_unhandled)
{
	NMConnection *connection;
	GError *error = NULL;
266
	char *unhandled_fallback = NULL;
267 268 269

	g_assert (!out_unhandled || !*out_unhandled);

270 271
	connection = nmtst_connection_from_file (filename, network_file, test_type,
	                                         out_unhandled ?: &unhandled_fallback, &error);
272
	g_assert_no_error (error);
273
	g_assert (!unhandled_fallback);
274 275 276 277 278 279 280 281

	if (out_unhandled && *out_unhandled)
		nmtst_assert_connection_verifies (connection);
	else
		nmtst_assert_connection_verifies_without_normalization (connection);
	return connection;
}

282
static void
283 284 285 286
_connection_from_file_fail (const char *filename,
                            const char *network_file,
                            const char *test_type,
                            GError **error)
287
{
288 289 290
	NMConnection *connection;
	GError *local = NULL;
	char *unhandled = NULL;
291

292
	connection = nmtst_connection_from_file (filename, network_file, test_type, &unhandled, &local);
293 294 295

	g_assert (!connection);
	g_assert (local);
296
	g_assert (!unhandled);
297 298 299 300
	g_propagate_error (error, local);
}

static void
301 302 303
_writer_new_connection_reread (NMConnection *connection,
                               const char *ifcfg_dir,
                               char **out_filename,
304
                               const char *expected,
305 306
                               NMConnection **out_reread,
                               gboolean *out_reread_same)
307 308 309 310
{
	gboolean success;
	GError *error = NULL;
	char *filename = NULL;
311
	gs_unref_object NMConnection *con_verified = NULL;
312 313
	gs_unref_object NMConnection *reread_copy = NULL;
	NMConnection **reread = out_reread ?: ((nmtst_get_rand_int () % 2) ? &reread_copy : NULL);
314 315 316 317

	g_assert (NM_IS_CONNECTION (connection));
	g_assert (ifcfg_dir);

318 319
	con_verified = nmtst_connection_duplicate_and_normalize (connection);

320 321 322 323 324 325 326
	success = nms_ifcfg_rh_writer_write_connection (con_verified,
	                                                ifcfg_dir,
	                                                NULL,
	                                                &filename,
	                                                reread,
	                                                out_reread_same,
	                                                &error);
327 328 329
	nmtst_assert_success (success, error);
	g_assert (filename && filename[0]);

330 331 332
	if (reread)
		nmtst_assert_connection_verifies_without_normalization (*reread);

333 334
	_assert_expected_content (con_verified, filename, expected);

335 336 337 338
	if (out_filename)
		*out_filename = filename;
	else
		g_free (filename);
339

340 341
}

342
static void
343
_writer_new_connec_exp (NMConnection *connection,
344
                        const char *ifcfg_dir,
345
                        const char *expected,
346 347 348 349 350
                        char **out_filename)
{
	gs_unref_object NMConnection *reread = NULL;
	gboolean reread_same = FALSE;

351
	_writer_new_connection_reread (connection, ifcfg_dir, out_filename, expected, &reread, &reread_same);
352 353 354 355
	_assert_reread_same (connection, reread);
	g_assert (reread_same);
}

356 357 358 359 360 361 362 363
static void
_writer_new_connection (NMConnection *connection,
                        const char *ifcfg_dir,
                        char **out_filename)
{
	_writer_new_connec_exp (connection, ifcfg_dir, NO_EXPECTED, out_filename);
}

364 365 366 367 368 369 370 371 372 373
static void
_writer_new_connection_FIXME (NMConnection *connection,
                              const char *ifcfg_dir,
                              char **out_filename)
{
	gs_unref_object NMConnection *reread = NULL;
	gboolean reread_same = FALSE;

	/* FIXME: this should not happen. Fix it to use _writer_new_connection() instead. */

374
	_writer_new_connection_reread (connection, ifcfg_dir, out_filename, NO_EXPECTED, &reread, &reread_same);
375 376 377 378
	_assert_reread_same_FIXME (connection, reread);
	g_assert (!reread_same);
}

379 380 381 382 383
static void
_writer_new_connection_fail (NMConnection *connection,
                             const char *ifcfg_dir,
                             GError **error)
{
384
	gs_unref_object NMConnection *connection_normalized = NULL;
385
	gs_unref_object NMConnection *reread = NULL;
386 387 388 389 390 391 392
	gboolean success;
	GError *local = NULL;
	char *filename = NULL;

	g_assert (NM_IS_CONNECTION (connection));
	g_assert (ifcfg_dir);

393 394
	connection_normalized = nmtst_connection_duplicate_and_normalize (connection);

395 396 397 398 399 400 401
	success = nms_ifcfg_rh_writer_write_connection (connection_normalized,
	                                                ifcfg_dir,
	                                                NULL,
	                                                &filename,
	                                                &reread,
	                                                NULL,
	                                                &local);
402 403
	nmtst_assert_no_success (success, local);
	g_assert (!filename);
404
	g_assert (!reread);
405 406

	g_propagate_error (error, local);
407
}
408 409

/*****************************************************************************/
410

411 412 413 414 415 416 417 418 419
static void
test_read_netmask_1 (void)
{
	nmtst_auto_unlinkfile char *testfile = NULL;
	gs_unref_object NMConnection *connection = NULL;
	gs_free char *content = NULL;
	NMSettingConnection *s_con;
	NMSettingIPConfig *s_ip4;
	NMIPAddress *ip4_addr;
420
	const char *FILENAME = TEST_IFCFG_DIR"/ifcfg-netmask-1";
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

	connection = _connection_from_file (FILENAME, NULL, TYPE_ETHERNET, NULL);

	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "System netmask-1");

	s_ip4 = nm_connection_get_setting_ip4_config (connection);
	g_assert (s_ip4);
	g_assert_cmpuint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 1);
	ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0);
	g_assert (ip4_addr);
	g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "102.0.2.2");
	g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 15);

	nmtst_assert_connection_verifies_without_normalization (connection);

	content = nmtst_file_get_contents (FILENAME);

440
	testfile = g_strdup (TEST_SCRATCH_DIR"/ifcfg-netmask-1.copy");
441 442 443 444

	nmtst_file_set_contents (testfile, content);

	_writer_update_connection (connection,
445
	                           TEST_SCRATCH_DIR,
446
	                           testfile,
447
	                           TEST_IFCFG_DIR"/ifcfg-netmask-1.cexpected");
448 449 450 451
}

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

452
static gboolean
453
verify_cert_or_key (NMSetting8021x *s_compare,
454 455
                    const char *file,
                    const char *privkey_password,
456
                    const char *property)
457 458 459 460
{
	NMSetting8021x *s_8021x;
	GError *error = NULL;
	gboolean success = FALSE;
461
	const char *expected = NULL, *setting = NULL;
462
	gboolean phase2 = FALSE;
463
	NMSetting8021xCKScheme scheme = NM_SETTING_802_1X_CK_SCHEME_UNKNOWN;
464

465
	if (strstr (property, "phase2"))
466
		phase2 = TRUE;
467 468 469

	s_8021x = (NMSetting8021x *) nm_setting_802_1x_new ();

470 471
	/* Load the certificate into an empty setting */
	if (strstr (property, "ca-cert")) {
472
		if (phase2)
473
			success = nm_setting_802_1x_set_phase2_ca_cert (s_8021x, file, NM_SETTING_802_1X_CK_SCHEME_PATH, NULL, &error);
474
		else
475
			success = nm_setting_802_1x_set_ca_cert (s_8021x, file, NM_SETTING_802_1X_CK_SCHEME_PATH, NULL, &error);
476
	} else if (strstr (property, "client-cert")) {
477
		if (phase2)
478
			success = nm_setting_802_1x_set_phase2_client_cert (s_8021x, file, NM_SETTING_802_1X_CK_SCHEME_PATH, NULL, &error);
479
		else
480
			success = nm_setting_802_1x_set_client_cert (s_8021x, file, NM_SETTING_802_1X_CK_SCHEME_PATH, NULL, &error);
481
	} else if (strstr (property, "private-key")) {
482
		if (phase2)
483
			success = nm_setting_802_1x_set_phase2_private_key (s_8021x, file, privkey_password, NM_SETTING_802_1X_CK_SCHEME_PATH, NULL, &error);
484
		else
485
			success = nm_setting_802_1x_set_private_key (s_8021x, file, privkey_password, NM_SETTING_802_1X_CK_SCHEME_PATH, NULL, &error);
486
	}
487 488
	g_assert_no_error (error);
	g_assert_cmpint (success, ==, TRUE);
489

490 491
	/* Ensure it was loaded using the PATH scheme */
	if (strstr (property, "ca-cert")) {
492
		if (phase2)
493
			scheme = nm_setting_802_1x_get_phase2_ca_cert_scheme (s_8021x);
494
		else
495
			scheme = nm_setting_802_1x_get_ca_cert_scheme (s_8021x);
496
	} else if (strstr (property, "client-cert")) {
497
		if (phase2)
498
			scheme = nm_setting_802_1x_get_phase2_client_cert_scheme (s_8021x);
499
		else
500
			scheme = nm_setting_802_1x_get_client_cert_scheme (s_8021x);
501
	} else if (strstr (property, "private-key")) {
502
		if (phase2)
503
			scheme = nm_setting_802_1x_get_phase2_private_key_scheme (s_8021x);
504
		else
505 506
			scheme = nm_setting_802_1x_get_private_key_scheme (s_8021x);
	}
507
	g_assert_cmpint (scheme, ==, NM_SETTING_802_1X_CK_SCHEME_PATH);
508

509 510
	/* Grab the path back out */
	if (strstr (property, "ca-cert")) {
511 512 513 514
		if (phase2)
			expected = nm_setting_802_1x_get_phase2_ca_cert_path (s_8021x);
		else
			expected = nm_setting_802_1x_get_ca_cert_path (s_8021x);
515
	} else if (strstr (property, "client-cert")) {
516 517 518 519
		if (phase2)
			expected = nm_setting_802_1x_get_phase2_client_cert_path (s_8021x);
		else
			expected = nm_setting_802_1x_get_client_cert_path (s_8021x);
520
	} else if (strstr (property, "private-key")) {
521 522 523 524
		if (phase2)
			expected = nm_setting_802_1x_get_phase2_private_key_path (s_8021x);
		else
			expected = nm_setting_802_1x_get_private_key_path (s_8021x);
525
	}
526
	g_assert_cmpstr (expected, ==, file);
527

528 529
	/* Compare the path with the expected path from the real setting */
	if (strstr (property, "ca-cert")) {
530
		if (phase2)
531
			setting = nm_setting_802_1x_get_phase2_ca_cert_path (s_compare);
532
		else
533
			setting = nm_setting_802_1x_get_ca_cert_path (s_compare);
534
	} else if (strstr (property, "client-cert")) {
535
		if (phase2)
536
			setting = nm_setting_802_1x_get_phase2_client_cert_path (s_compare);
537
		else
538
			setting = nm_setting_802_1x_get_client_cert_path (s_compare);
539
	} else if (strstr (property, "private-key")) {
540
		if (phase2)
541
			setting = nm_setting_802_1x_get_phase2_private_key_path (s_compare);
542
		else
543
			setting = nm_setting_802_1x_get_private_key_path (s_compare);
544
	}
545
	g_assert_cmpstr (setting, ==, expected);
546 547 548 549 550

	g_object_unref (s_8021x);
	return TRUE;
}

Dan Williams's avatar
Dan Williams committed
551
static void
552
test_read_basic (void)
Dan Williams's avatar
Dan Williams committed
553 554 555 556
{
	NMConnection *connection;
	NMSettingConnection *s_con;
	NMSettingWired *s_wired;
557 558
	NMSettingIPConfig *s_ip4;
	NMSettingIPConfig *s_ip6;
559
	const char *mac;
Dan Williams's avatar
Dan Williams committed
560 561
	char expected_mac_address[ETH_ALEN] = { 0x00, 0x16, 0x41, 0x11, 0x22, 0x33 };

562
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-minimal",
563
	                                    NULL, TYPE_ETHERNET, NULL);
Dan Williams's avatar
Dan Williams committed
564 565

	/* ===== CONNECTION SETTING ===== */
566
	s_con = nm_connection_get_setting_connection (connection);
567
	g_assert (s_con);
568 569
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "System test-minimal");
	g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, 0);
570
	g_assert (nm_setting_connection_get_autoconnect (s_con));
571
	g_assert_cmpint (nm_setting_connection_get_autoconnect_retries (s_con), ==, -1);
Dan Williams's avatar
Dan Williams committed
572 573 574 575 576 577 578

	/* UUID can't be tested if the ifcfg does not contain the UUID key, because
	 * the UUID is generated on the full path of the ifcfg file, which can change
	 * depending on where the tests are run.
	 */

	/* ===== WIRED SETTING ===== */
579
	s_wired = nm_connection_get_setting_wired (connection);
580 581
	g_assert (s_wired);
	g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 0);
Dan Williams's avatar
Dan Williams committed
582 583

	/* MAC address */
584 585 586
	mac = nm_setting_wired_get_mac_address (s_wired);
	g_assert (mac);
	g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, ETH_ALEN));
Dan Williams's avatar
Dan Williams committed
587 588

	/* ===== IPv4 SETTING ===== */
589
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
590
	g_assert (s_ip4);
591 592
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED);
	g_assert (nm_setting_ip_config_get_never_default (s_ip4) == FALSE);
593

594 595 596
	/* ===== IPv6 SETTING ===== */
	s_ip6 = nm_connection_get_setting_ip6_config (connection);
	g_assert (s_ip6);
597 598
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE);
	g_assert (nm_setting_ip_config_get_never_default (s_ip6) == FALSE);
599

Dan Williams's avatar
Dan Williams committed
600 601 602
	g_object_unref (connection);
}

603 604 605 606 607 608
static void
test_read_miscellaneous_variables (void)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
	NMSettingWired *s_wired;
609
	NMSettingIPConfig *s_ip4;
610
	char *expected_mac_blacklist[3] = { "00:16:41:11:22:88", "00:16:41:11:22:99", "6a:5d:5a:fa:dd:f0" };
611 612 613
	int mac_blacklist_num, i;
	guint64 expected_timestamp = 0;

614
	NMTST_EXPECT_NM_WARN ("*invalid MAC in HWADDR_BLACKLIST 'XX:aa:invalid'*");
615
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-misc-variables",
616
	                                    NULL, TYPE_ETHERNET, NULL);
617
	g_test_assert_expected_messages ();
618 619 620 621 622 623

	/* ===== CONNECTION SETTING ===== */
	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);
	g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, expected_timestamp);
	g_assert (nm_setting_connection_get_autoconnect (s_con));
624
	g_assert_cmpint (nm_setting_connection_get_autoconnect_retries (s_con), ==, 100);
625 626 627 628 629 630 631 632

	/* ===== WIRED SETTING ===== */
	s_wired = nm_connection_get_setting_wired (connection);
	g_assert (s_wired);
	g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 0);

	/* MAC blacklist */
	mac_blacklist_num = nm_setting_wired_get_num_mac_blacklist_items (s_wired);
633
	g_assert_cmpint (mac_blacklist_num, ==, 3);
634 635 636 637 638 639
	for (i = 0; i < mac_blacklist_num; i++)
		g_assert (nm_utils_hwaddr_matches (nm_setting_wired_get_mac_blacklist_item (s_wired, i), -1, expected_mac_blacklist[i], -1));

	/* ===== IPv4 SETTING ===== */
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
	g_assert (s_ip4);
640 641
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED);
	g_assert (nm_setting_ip_config_get_never_default (s_ip4) == FALSE);
642 643 644 645

	g_object_unref (connection);
}

646
static void
647
test_read_variables_corner_cases (void)
648 649 650 651
{
	NMConnection *connection;
	NMSettingConnection *s_con;
	NMSettingWired *s_wired;
652
	NMSettingIPConfig *s_ip4;
653
	const char *mac;
654 655
	char expected_mac_address[ETH_ALEN] = { 0x00, 0x16, 0x41, 0x11, 0x22, 0x33 };

656
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-variables-corner-cases-1",
657
	                                    NULL, TYPE_ETHERNET, NULL);
658 659 660

	/* ===== CONNECTION SETTING ===== */
	s_con = nm_connection_get_setting_connection (connection);
661
	g_assert (s_con);
662 663
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "System test-variables-corner-cases-1");
	g_assert_cmpstr (nm_setting_connection_get_zone (s_con), ==, NULL);
664
	g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, 0);
665
	g_assert (nm_setting_connection_get_autoconnect (s_con));
666 667 668

	/* ===== WIRED SETTING ===== */
	s_wired = nm_connection_get_setting_wired (connection);
669 670
	g_assert (s_wired);
	g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 0);
671 672

	/* MAC address */
673 674 675
	mac = nm_setting_wired_get_mac_address (s_wired);
	g_assert (mac);
	g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, ETH_ALEN));
676 677 678

	/* ===== IPv4 SETTING ===== */
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
679
	g_assert (s_ip4);
680 681
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED);
	g_assert (nm_setting_ip_config_get_never_default (s_ip4) == FALSE);
682 683 684 685

	g_object_unref (connection);
}

Dan Williams's avatar
Dan Williams committed
686 687 688 689 690
static void
test_read_unmanaged (void)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
691
	char *unhandled_spec = NULL;
Dan Williams's avatar
Dan Williams committed
692 693
	guint64 expected_timestamp = 0;

694
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-nm-controlled",
695 696
	                                    NULL, TYPE_ETHERNET,
	                                    &unhandled_spec);
697
	g_assert_cmpstr (unhandled_spec, ==, "unmanaged:mac:00:11:22:33:f8:9f");
Dan Williams's avatar
Dan Williams committed
698 699

	/* ===== CONNECTION SETTING ===== */
700
	s_con = nm_connection_get_setting_connection (connection);
701
	g_assert (s_con);
702
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "System test-nm-controlled");
703 704
	g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, expected_timestamp);
	g_assert (nm_setting_connection_get_autoconnect (s_con));
Dan Williams's avatar
Dan Williams committed
705

706
	g_free (unhandled_spec);
707 708
	g_object_unref (connection);
}
Dan Williams's avatar
Dan Williams committed
709

710 711 712 713 714
static void
test_read_unmanaged_unrecognized (void)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
715
	gs_free char *unhandled_spec = NULL;
716
	guint64 expected_timestamp = 0;
Dan Williams's avatar
Dan Williams committed
717

718
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-nm-controlled-unrecognized",
719 720
	                                    NULL, NULL,
	                                    &unhandled_spec);
721 722 723 724 725
	g_assert_cmpstr (unhandled_spec, ==, "unmanaged:interface-name:ipoac0");

	/* ===== CONNECTION SETTING ===== */
	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);
726
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "PigeonNet");
727 728 729 730 731 732 733 734 735 736
	g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, expected_timestamp);

	g_object_unref (connection);
}

static void
test_read_unrecognized (void)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
737
	gs_free char *unhandled_spec = NULL;
738 739
	guint64 expected_timestamp = 0;

740
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-unrecognized",
741 742
	                                    NULL, NULL,
	                                    &unhandled_spec);
743
	g_assert_cmpstr (unhandled_spec, ==, "unrecognized:mac:00:11:22:33");
744 745 746 747

	/* ===== CONNECTION SETTING ===== */
	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);
748
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "System test-unrecognized");
749
	g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, expected_timestamp);
Dan Williams's avatar
Dan Williams committed
750 751 752 753 754

	g_object_unref (connection);
}

static void
755
test_read_wired_static (gconstpointer test_data)
Dan Williams's avatar
Dan Williams committed
756 757 758 759
{
	NMConnection *connection;
	NMSettingConnection *s_con;
	NMSettingWired *s_wired;
760 761
	NMSettingIPConfig *s_ip4;
	NMSettingIPConfig *s_ip6;
762
	char *unmanaged = NULL;
763
	const char *mac;
Dan Williams's avatar
Dan Williams committed
764
	char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0xee };
765 766
	NMIPAddress *ip4_addr;
	NMIPAddress *ip6_addr;
767 768 769 770
	const char *file, *expected_id;
	gpointer expect_ip6_p;

	nmtst_test_data_unpack (test_data, &file, &expected_id, &expect_ip6_p);
Dan Williams's avatar
Dan Williams committed
771

772 773 774
	g_assert (expected_id);

	connection = _connection_from_file (file, NULL, TYPE_ETHERNET,
775
	                                    &unmanaged);
776
	g_assert_cmpstr (unmanaged, ==, NULL);
Dan Williams's avatar
Dan Williams committed
777 778

	/* ===== CONNECTION SETTING ===== */
779
	s_con = nm_connection_get_setting_connection (connection);
780
	g_assert (s_con);
781
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, expected_id);
782 783
	g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, 0);
	g_assert (nm_setting_connection_get_autoconnect (s_con));
Dan Williams's avatar
Dan Williams committed
784 785

	/* ===== WIRED SETTING ===== */
786
	s_wired = nm_connection_get_setting_wired (connection);
787 788
	g_assert (s_wired);
	g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 1492);
Dan Williams's avatar
Dan Williams committed
789 790

	/* MAC address */
791 792 793
	mac = nm_setting_wired_get_mac_address (s_wired);
	g_assert (mac);
	g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, ETH_ALEN));
Dan Williams's avatar
Dan Williams committed
794 795

	/* ===== IPv4 SETTING ===== */
796
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
797
	g_assert (s_ip4);
798 799
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL);
	g_assert (nm_setting_ip_config_get_may_fail (s_ip4));
800

801 802 803
	g_assert (nm_setting_ip_config_has_dns_options (s_ip4));
	g_assert_cmpint (nm_setting_ip_config_get_num_dns_options (s_ip4), ==, 0);

Dan Williams's avatar
Dan Williams committed
804
	/* DNS Addresses */
805 806 807
	g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 2);
	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 0), ==, "4.2.2.1");
	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 1), ==, "4.2.2.2");
Dan Williams's avatar
Dan Williams committed
808

809
	/* IP addresses */
810 811
	g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1);
	ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0);
812
	g_assert (ip4_addr);
813 814
	g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24);
	g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5");
815 816 817

	/* Gateway */
	g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "192.168.1.1");
Dan Williams's avatar
Dan Williams committed
818

819 820 821
	/* ===== IPv6 SETTING ===== */
	s_ip6 = nm_connection_get_setting_ip6_config (connection);
	g_assert (s_ip6);
822
	if (GPOINTER_TO_INT (expect_ip6_p)) {
823 824
		g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_MANUAL);
		g_assert (nm_setting_ip_config_get_may_fail (s_ip6));
Dan Williams's avatar
Dan Williams committed
825

826 827 828
		g_assert (nm_setting_ip_config_has_dns_options (s_ip6));
		g_assert_cmpint (nm_setting_ip_config_get_num_dns_options (s_ip6), ==, 0);

829
		/* DNS Addresses */
830 831 832
		g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip6), ==, 2);
		g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip6, 0), ==, "1:2:3:4::a");
		g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip6, 1), ==, "1:2:3:4::b");
Dan Williams's avatar
Dan Williams committed
833

834
		/* IP addresses */
835
		g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip6), ==, 2);
836

837
		ip6_addr = nm_setting_ip_config_get_address (s_ip6, 0);
838
		g_assert (ip6_addr);
839 840
		g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64);
		g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "dead:beaf::1");
841

842
		ip6_addr = nm_setting_ip_config_get_address (s_ip6, 1);
843
		g_assert (ip6_addr);
844 845
		g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56);
		g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "dead:beaf::2");
846
	} else {
847
		g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE);
848
		g_assert (!nm_setting_ip_config_has_dns_options (s_ip6));
849 850
	}

Dan Williams's avatar
Dan Williams committed
851 852 853
	g_object_unref (connection);
}

854
static void
855
test_read_wired_static_no_prefix (gconstpointer user_data)
856
{
857
	guint32 expected_prefix = GPOINTER_TO_UINT (user_data);
858 859
	NMConnection *connection;
	NMSettingConnection *s_con;
860
	NMSettingIPConfig *s_ip4;
861
	NMIPAddress *ip4_addr;
862 863
	char *file, *expected_id;

864
	file = g_strdup_printf (TEST_IFCFG_DIR"/ifcfg-test-wired-static-no-prefix-%u", expected_prefix);
865 866
	expected_id = g_strdup_printf ("System test-wired-static-no-prefix-%u", expected_prefix);

867
	NMTST_EXPECT_NM_WARN ("*missing PREFIX, assuming*");
868
	connection = _connection_from_file (file, NULL, TYPE_ETHERNET, NULL);
869
	g_test_assert_expected_messages ();
870 871

	/* ===== CONNECTION SETTING ===== */
872
	s_con = nm_connection_get_setting_connection (connection);
873 874
	g_assert (s_con);
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, expected_id);
875 876

	/* ===== IPv4 SETTING ===== */
877
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
878
	g_assert (s_ip4);
879
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL);
880

881 882 883
	g_assert (!nm_setting_ip_config_has_dns_options (s_ip4));
	g_assert_cmpint (nm_setting_ip_config_get_num_dns_options (s_ip4), ==, 0);

884 885
	g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1);
	ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0);
886
	g_assert (ip4_addr);
887
	g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, expected_prefix);
888 889

	g_free (file);
890
	g_free (expected_id);
891 892 893
	g_object_unref (connection);
}

Dan Williams's avatar
Dan Williams committed
894 895 896 897 898 899
static void
test_read_wired_dhcp (void)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
	NMSettingWired *s_wired;
900
	NMSettingIPConfig *s_ip4;
901
	char *unmanaged = NULL;
Dan Williams's avatar
Dan Williams committed
902
	char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0xee };
903
	const char *mac;
Dan Williams's avatar
Dan Williams committed
904

905
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-wired-dhcp",
906 907
	                                    NULL, TYPE_ETHERNET,
	                                    &unmanaged);
908
	g_assert (unmanaged == NULL);
Dan Williams's avatar
Dan Williams committed
909 910

	/* ===== CONNECTION SETTING ===== */
911
	s_con = nm_connection_get_setting_connection (connection);
912 913 914 915
	g_assert (s_con);
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "System test-wired-dhcp");
	g_assert_cmpuint (nm_setting_connection_get_timestamp (s_con), ==, 0);
	g_assert (nm_setting_connection_get_autoconnect (s_con));
Dan Williams's avatar
Dan Williams committed
916 917

	/* ===== WIRED SETTING ===== */
918
	s_wired = nm_connection_get_setting_wired (connection);
919
	g_assert (s_wired);
Dan Williams's avatar
Dan Williams committed
920 921

	/* MAC address */
922
	mac = nm_setting_wired_get_mac_address (s_wired);
923 924
	g_assert (mac);
	g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, sizeof (expected_mac_address)));
Dan Williams's avatar
Dan Williams committed
925 926

	/* ===== IPv4 SETTING ===== */
927
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
928 929 930 931 932 933 934
	g_assert (s_ip4);
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
	g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip4), ==, "foobar");
	g_assert (nm_setting_ip_config_get_ignore_auto_dns (s_ip4));
	g_assert_cmpuint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 2);
	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 0), ==, "4.2.2.1");
	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 1), ==, "4.2.2.2");
Dan Williams's avatar
Dan Williams committed
935 936 937 938

	g_object_unref (connection);
}

939 940 941 942
static void
test_read_wired_dhcp_plus_ip (void)
{
	NMConnection *connection;
943 944
	NMSettingIPConfig *s_ip4;
	NMSettingIPConfig *s_ip6;
945 946
	NMIPAddress *ip4_addr;
	NMIPAddress *ip6_addr;
947

948
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-wired-dhcp-plus-ip",
949
	                                    NULL, TYPE_ETHERNET, NULL);
950 951 952 953

	/* ===== IPv4 SETTING ===== */
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
	g_assert (s_ip4);
954 955
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
	g_assert (nm_setting_ip_config_get_may_fail (s_ip4));
956 957

	/* DNS Addresses */
958 959 960
	g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 2);
	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 0), ==, "4.2.2.1");
	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 1), ==, "4.2.2.2");
961 962

	/* IP addresses */
963 964
	g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 2);
	ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0);
965
	g_assert (ip4_addr);
966 967
	g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24);
	g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "1.2.3.4");
968 969 970

	/* Gateway */
	g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "1.1.1.1");
971

972
	ip4_addr = nm_setting_ip_config_get_address (s_ip4, 1);
973
	g_assert (ip4_addr);
974 975
	g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 16);
	g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "9.8.7.6");
976 977 978 979

	/* ===== IPv6 SETTING ===== */
	s_ip6 = nm_connection_get_setting_ip6_config (connection);
	g_assert (s_ip6);
980 981
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_AUTO);
	g_assert (nm_setting_ip_config_get_may_fail (s_ip6));
982 983

	/* DNS Addresses */
984 985 986
	g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip6), ==, 2);
	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip6, 0), ==, "1:2:3:4::a");
	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip6, 1), ==, "1:2:3:4::b");
987 988

	/* IP addresses */
989 990
	g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip6), ==, 3);
	ip6_addr = nm_setting_ip_config_get_address (s_ip6, 0);
991
	g_assert (ip6_addr);
992 993
	g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56);
	g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "1001:abba::1234");
994

995
	ip6_addr = nm_setting_ip_config_get_address (s_ip6, 1);
996
	g_assert (ip6_addr);
997 998
	g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64);
	g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "2001:abba::2234");
999

1000
	ip6_addr = nm_setting_ip_config_get_address (s_ip6, 2);
1001
	g_assert (ip6_addr);
1002 1003
	g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 96);
	g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "3001:abba::3234");
1004 1005 1006 1007

	g_object_unref (connection);
}

1008 1009 1010 1011 1012 1013 1014
static void
test_read_wired_shared_plus_ip (void)
{
	NMConnection *connection;
	NMSettingIPConfig *s_ip4;
	NMIPAddress *ip4_addr;

1015
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-wired-shared-plus-ip",
1016
	                                    NULL, TYPE_ETHERNET, NULL);
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

	/* ===== IPv4 SETTING ===== */
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
	g_assert (s_ip4);
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_SHARED);
	g_assert (nm_setting_ip_config_get_may_fail (s_ip4));

	/* IP addresses */
	g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1);
	ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0);
	g_assert (ip4_addr);
	g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24);
	g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "10.20.30.5");

	/* Gateway */
	g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "1.1.1.1");

	g_object_unref (connection);
}

Dan Williams's avatar
Dan Williams committed
1037 1038 1039 1040 1041 1042
static void
test_read_wired_global_gateway (void)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
	NMSettingWired *s_wired;
1043
	NMSettingIPConfig *s_ip4;
1044
	NMIPAddress *ip4_addr;
1045
	char *unmanaged = NULL;
Dan Williams's avatar
Dan Williams committed
1046

1047 1048
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-wired-global-gateway",
	                                    TEST_IFCFG_DIR"/network-test-wired-global-gateway",
1049
	                                    TYPE_ETHERNET, &unmanaged);
1050
	g_assert (unmanaged == NULL);
Dan Williams's avatar
Dan Williams committed
1051 1052

	/* ===== CONNECTION SETTING ===== */
1053
	s_con = nm_connection_get_setting_connection (connection);
1054 1055
	g_assert (s_con);
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "System test-wired-global-gateway");
Dan Williams's avatar
Dan Williams committed
1056 1057

	/* ===== WIRED SETTING ===== */
1058
	s_wired = nm_connection_get_setting_wired (connection);
1059
	g_assert (s_wired);
Dan Williams's avatar
Dan Williams committed
1060 1061

	/* ===== IPv4 SETTING ===== */
1062
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
1063
	g_assert (s_ip4);
1064
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL);
Dan Williams's avatar
Dan Williams committed
1065 1066

	/* Address #1 */
1067
	ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0);
1068
	g_assert (ip4_addr);
1069 1070
	g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24);
	g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5");
1071 1072 1073

	/* Gateway */
	g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "192.168.1.2");
Dan Williams's avatar
Dan Williams committed
1074 1075 1076 1077

	g_object_unref (connection);
}

1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
/* Ignore GATEWAY from /etc/sysconfig/network for automatic connections */
static void
test_read_wired_global_gateway_ignore (void)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
	NMSettingWired *s_wired;
	NMSettingIPConfig *s_ip4;
	char *unmanaged = NULL;

1088
	NMTST_EXPECT_NM_WARN ("*ignoring GATEWAY (/etc/sysconfig/network) for * because the connection has no static addresses");
1089 1090
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-wired-global-gateway-ignore",
	                                    TEST_IFCFG_DIR"/network-test-wired-global-gateway-ignore",
1091 1092
	                                    TYPE_ETHERNET, &unmanaged);
	g_test_assert_expected_messages ();
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	g_assert (unmanaged == NULL);

	/* ===== CONNECTION SETTING ===== */
	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "System test-wired-global-gateway-ignore");

	/* ===== WIRED SETTING ===== */
	s_wired = nm_connection_get_setting_wired (connection);
	g_assert (s_wired);

	/* ===== IPv4 SETTING ===== */
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
	g_assert (s_ip4);
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);

	/* Addresses */
	g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 0);

	/* Gateway */
	g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, NULL);

	g_object_unref (connection);
}

1118 1119 1120 1121 1122 1123 1124
static void
test_read_wired_obsolete_gateway_n (void)
{
	NMConnection *connection;
	NMSettingIPConfig *s_ip4;
	NMIPAddress *ip4_addr;

1125
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-wired-obsolete-gateway-n",
1126
	                                    NULL, TYPE_ETHERNET, NULL);
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145

	/* ===== IPv4 SETTING ===== */
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
	g_assert (s_ip4);
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL);

	/* IP addresses */
	g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1);
	ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0);
	g_assert (ip4_addr);
	g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24);
	g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "1.2.3.4");

	/* Gateway */
	g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "1.1.1.1");

	g_object_unref (connection);
}

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
static void
test_user_1 (void)
{
	nmtst_auto_unlinkfile char *testfile = NULL;
	gs_unref_object NMConnection *connection = NULL;
	gs_unref_object NMConnection *reread = NULL;
	NMSettingUser *s_user;

	connection = nmtst_create_minimal_connection ("Test User 1", NULL, NM_SETTING_WIRED_SETTING_NAME, NULL);
	s_user = NM_SETTING_USER (nm_setting_user_new ());

#define _USER_SET_DATA(s_user, key, val) \
	G_STMT_START { \
		GError *_error = NULL; \
		gboolean _success; \
		\
		_success = nm_setting_user_set_data ((s_user), (key), (val), &_error); \
		nmtst_assert_success (_success, _error); \
	} G_STMT_END

#define _USER_SET_DATA_X(s_user, key) \
	_USER_SET_DATA (s_user, key, "val="key"")

	_USER_SET_DATA (s_user, "my.val1", "");
	_USER_SET_DATA_X (s_user, "my.val2");
	_USER_SET_DATA_X (s_user, "my.v__al3");
	_USER_SET_DATA_X (s_user, "my._v");
	_USER_SET_DATA_X (s_user, "my.v+");
	_USER_SET_DATA_X (s_user, "my.Av");
	_USER_SET_DATA_X (s_user, "MY.AV");
	_USER_SET_DATA_X (s_user, "MY.8V");
	_USER_SET_DATA_X (s_user, "MY.8-V");
	_USER_SET_DATA_X (s_user, "MY.8_V");
	_USER_SET_DATA_X (s_user, "MY.8+V");
	_USER_SET_DATA_X (s_user, "MY.8/V");
	_USER_SET_DATA_X (s_user, "MY.8=V");
	_USER_SET_DATA_X (s_user, "MY.-");
	_USER_SET_DATA_X (s_user, "MY._");
	_USER_SET_DATA_X (s_user, "MY.+");
	_USER_SET_DATA_X (s_user, "MY./");
	_USER_SET_DATA_X (s_user, "MY.=");
	_USER_SET_DATA_X (s_user, "my.keys.1");
	_USER_SET_DATA_X (s_user, "my.other.KEY.42");

	nm_connection_add_setting (connection, NM_SETTING (s_user));

	_writer_new_connec_exp (connection,
1193 1194
	                        TEST_SCRATCH_DIR,
	                        TEST_IFCFG_DIR"/ifcfg-Test_User_1.cexpected",
1195 1196 1197 1198 1199 1200 1201
	                        &testfile);

	reread = _connection_from_file (testfile, NULL, TYPE_ETHERNET, NULL);

	nmtst_assert_connection_equals (connection, TRUE, reread, FALSE);
}

Dan Williams's avatar
Dan Williams committed
1202 1203 1204 1205
static void
test_read_wired_never_default (void)
{
	NMConnection *connection;
1206 1207
	NMSettingIPConfig *s_ip4;
	NMSettingIPConfig *s_ip6;
Dan Williams's avatar
Dan Williams committed
1208

1209 1210
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-wired-never-default",
	                                    TEST_IFCFG_DIR"/network-test-wired-never-default",
1211
	                                    TYPE_ETHERNET, NULL);
Dan Williams's avatar
Dan Williams committed
1212 1213

	/* ===== WIRED SETTING ===== */
1214
	g_assert (nm_connection_get_setting_wired (connection));
Dan Williams's avatar
Dan Williams committed
1215 1216

	/* ===== IPv4 SETTING ===== */
1217
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
1218
	g_assert (s_ip4);
1219 1220 1221
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
	g_assert (nm_setting_ip_config_get_never_default (s_ip4));
	g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 0);
Dan Williams's avatar
Dan Williams committed
1222

1223
	/* ===== IPv6 SETTING ===== */
1224
	s_ip6 = nm_connection_get_setting_ip6_config (connection);
1225
	g_assert (s_ip6);
1226 1227
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_AUTO);
	g_assert (nm_setting_ip_config_get_never_default (s_ip6));
1228

Dan Williams's avatar
Dan Williams committed
1229 1230 1231
	g_object_unref (connection);
}

1232 1233 1234 1235 1236
static void
test_read_wired_defroute_no (void)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
1237 1238
	NMSettingIPConfig *s_ip4;
	NMSettingIPConfig *s_ip6;
1239 1240
	char *unmanaged = NULL;

1241
	connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-wired-defroute-no",
1242
	                                    NULL, TYPE_ETHERNET, &unmanaged);
1243
	g_assert (unmanaged == NULL);
1244

1245
	s_con = nm_connection_get_setting_connection (connection);
1246 1247
	g_assert (s_con);
	g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "System test-wired-defroute-no");
1248

1249
	g_assert (nm_connection_get_setting_wired (connection));
1250

1251
	s_ip4 = nm_connection_get_setting_ip4_config (connection);
1252 1253 1254
	g_assert (s_ip4);
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
	g_assert (nm_setting_ip_config_get_never_default (s_ip4));
1255

1256
	s_ip6 = nm_connection_get_setting_ip6_config (connection);
1257 1258 1259
	g_assert (s_ip6);
	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_AUTO);
	g_assert (nm_setting_ip_config_get_never_default (s_ip6));
1260

1261 1262 1263 1264 1265 1266 1267 1268
	g_object_unref (connection);
}

static void
test_read_wired_defroute_no_gatewaydev_yes (void)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
1269 1270
	NMSettingIPConfig *s_ip4;
	NMSettingIPConfig *s_ip6;
1271 1272