test_all.c 13.8 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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager system settings service (ifnet)
 *
 * Mu Qiao <qiaomuf@gmail.com>
 *
 * 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.
 *
 * Copyright (C) 1999-2010 Gentoo Foundation, Inc.
 */

#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <arpa/inet.h>
#include <stdlib.h>
28
#include <unistd.h>
29 30 31 32 33 34 35
#include <nm-utils.h>

#include "net_parser.h"
#include "nm-test-helpers.h"
#include "net_utils.h"
#include "wpa_parser.h"
#include "connection_parser.h"
36
#include "nm-config.h"
37

38 39 40 41 42 43 44 45
/* Fake NMConfig handling; the values it returns don't matter, so this
 * is easier than forcing it to read our own config file, etc.
 */
NMConfig *
nm_config_get (void)
{
	return NULL;
}
Dan Williams's avatar
Dan Williams committed
46 47

const char *
48
nm_config_get_dhcp_client (NMConfig *config)
Dan Williams's avatar
Dan Williams committed
49
{
50
	return "dhclient";
Dan Williams's avatar
Dan Williams committed
51 52
}

53 54 55 56
static void
test_getdata ()
{
	ASSERT (ifnet_get_data ("eth1", "config")
Mu Qiao's avatar
Mu Qiao committed
57
		&& strcmp (ifnet_get_data ("eth1", "config"), "( \"dhcp\" )") == 0,
58 59 60 61 62 63 64 65
		"get data", "config_eth1 is not correct");
	ASSERT (ifnet_get_data ("ppp0", "username")
		&& strcmp (ifnet_get_data ("ppp0", "username"), "user") == 0,
		"get data", "config_ppp0 username is not correctly read");
	ASSERT (ifnet_get_data ("ppp0", "password")
		&& strcmp (ifnet_get_data ("ppp0", "password"),
			   "password") == 0, "get data",
		"config_ppp0 password is not correctly read");
66 67 68
	ASSERT (ifnet_get_global_data ("modules")
		&& strcmp ("!wpa_supplicant", ifnet_get_global_data ("modules")) == 0,
		"get data", "config_eth1 is not correct");
69 70 71
}

static void
72
test_read_hostname (const char *base_path)
73
{
74 75 76 77 78 79
	char *hostname_path, *hostname;

	hostname_path = g_build_filename (base_path, "hostname", NULL);
	hostname = read_hostname (hostname_path);

	g_assert_cmpstr (hostname, ==, "gentoo");
80

81
	g_free (hostname);
82
	g_free (hostname_path);
83 84 85
}

static void
86
test_write_hostname (const char *temp_path)
87
{
88 89 90 91 92 93 94 95
	char *hostname_path, *hostname;

	hostname_path = g_build_filename (temp_path, "hostname-test", NULL);
	write_hostname (hostname_path, "gentoo-nm");
	hostname = read_hostname (hostname_path);

	g_assert_cmpstr (hostname, ==, "gentoo-nm");

96
	g_free (hostname);
97 98
	unlink (hostname_path);
	g_free (hostname_path);
99 100 101 102 103 104 105 106 107 108
}

static void
test_is_static ()
{
	ASSERT (is_static_ip4 ("eth1") == FALSE, "is static",
		"a dhcp interface is recognized as static");
	ASSERT (is_static_ip4 ("eth0") == TRUE, "is static",
		"a static interface is recognized as dhcp");
	ASSERT (!is_static_ip6 ("eth0") == TRUE, "is static",
Mu Qiao's avatar
Mu Qiao committed
109
		"a dhcp interface is recognized as static");
110 111 112 113 114
}

static void
test_has_default_route ()
{
115 116 117 118
	ASSERT (has_default_ip4_route ("eth0"),
		"has default route", "eth0 should have a default ipv4 route");
	ASSERT (has_default_ip6_route ("eth4"),
		"has default route", "eth4 should have a default ipv6 route");
119
	ASSERT (!has_default_ip4_route ("eth5")
120 121
		&& !has_default_ip6_route ("eth5"),
		"has default route", "eth5 shouldn't have a default route");
122 123 124 125 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
}

static void
test_has_ip6_address ()
{
	ASSERT (has_ip6_address ("eth2"), "has ip6 address",
		"eth2 should have a ipv6 address");
	ASSERT (!has_ip6_address ("eth0"), "has ip6 address",
		"eth0 shouldn't have a ipv6 address")

}

static void
test_is_ip4_address ()
{
	gchar *address1 = "192.168.4.232/24";
	gchar *address2 = "192.168.100.{1..254}/24";
	gchar *address3 = "192.168.4.2555/24";

	ASSERT (is_ip4_address (address1), "is ip4 address",
		"%s should be a valid address", address1);
	ASSERT (is_ip4_address (address2), "is ip4 address",
		"%s should be a valid address", address2);
	ASSERT (!is_ip4_address (address3), "is ip4 address",
		"%s should be an invalid address", address3);
}

static void
test_is_ip6_address ()
{
	gchar *address1 = "4321:0:1:2:3:4:567:89ac/24";

	ASSERT (is_ip6_address (address1), "is ip6 address",
		"%s should be a valid address", address1);
}

static void
check_ip_block (ip_block * iblock, gchar * ip, gchar * netmask, gchar * gateway)
{
	char *str;
	struct in_addr tmp_ip4_addr;

	str = malloc (INET_ADDRSTRLEN);
	tmp_ip4_addr.s_addr = iblock->ip;
	inet_ntop (AF_INET, &tmp_ip4_addr, str, INET_ADDRSTRLEN);
167 168
	ASSERT (strcmp (ip, str) == 0, "check ip",
		"ip expected:%s, find:%s", ip, str);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	tmp_ip4_addr.s_addr = iblock->netmask;
	inet_ntop (AF_INET, &tmp_ip4_addr, str, INET_ADDRSTRLEN);
	ASSERT (strcmp (netmask, str) == 0, "check netmask",
		"netmask expected:%s, find:%s", netmask, str);
	tmp_ip4_addr.s_addr = iblock->gateway;
	inet_ntop (AF_INET, &tmp_ip4_addr, str, INET_ADDRSTRLEN);
	ASSERT (strcmp (gateway, str) == 0, "check gateway",
		"gateway expected:%s, find:%s", gateway, str);
	free (str);
}

static void
test_convert_ipv4_config_block ()
{
	ip_block *iblock = convert_ip4_config_block ("eth0");
	ip_block *tmp = iblock;

	ASSERT (iblock != NULL, "convert ipv4 block",
		"block eth0 should not be NULL");
	check_ip_block (iblock, "202.117.16.121", "255.255.255.0",
			"202.117.16.1");
	iblock = iblock->next;
	destroy_ip_block (tmp);
	ASSERT (iblock != NULL, "convert ipv4 block",
		"block eth0 should have a second IP address");
	check_ip_block (iblock, "192.168.4.121", "255.255.255.0",
			"202.117.16.1");
	destroy_ip_block (iblock);
	iblock = convert_ip4_config_block ("eth2");
	ASSERT (iblock != NULL
199 200
		&& iblock->next == NULL,
		"convert error IPv4 address", "should only get one address");
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	check_ip_block (iblock, "192.168.4.121", "255.255.255.0", "0.0.0.0");
	destroy_ip_block (iblock);
	iblock = convert_ip4_config_block ("eth3");
	ASSERT (iblock == NULL, "convert config_block",
		"convert error configuration");
	destroy_ip_block (iblock);
}

static void
test_convert_ipv4_routes_block ()
{
	ip_block *iblock = convert_ip4_routes_block ("eth0");
	ip_block *tmp = iblock;

	ASSERT (iblock != NULL, "convert ip4 routes", "should get one route");
	check_ip_block (iblock, "192.168.4.0", "255.255.255.0", "192.168.4.1");
	iblock = iblock->next;
	destroy_ip_block (tmp);
	ASSERT (iblock == NULL, "convert ip4 routes",
		"should only get one route");
Mu Qiao's avatar
Mu Qiao committed
221 222 223 224 225 226 227 228 229 230

	iblock = convert_ip4_routes_block ("eth9");
	tmp = iblock;

	ASSERT (iblock != NULL, "convert ip4 routes", "should get one route");
	check_ip_block (iblock, "10.0.0.0", "255.0.0.0", "192.168.0.1");
	iblock = iblock->next;
	destroy_ip_block (tmp);
	ASSERT (iblock == NULL, "convert ip4 routes",
		"should only get one route");
231 232 233 234 235
}

static void
test_wpa_parser ()
{
Dan Williams's avatar
Dan Williams committed
236
	const char *value;
237

238 239 240
	g_assert (exist_ssid ("example"));

	g_assert (exist_ssid ("static-wep-test"));
241
	value = wpa_get_value ("static-wep-test", "key_mgmt");
242
	g_assert_cmpstr (value, ==, "NONE");
243
	value = wpa_get_value ("static-wep-test", "wep_key0");
244 245 246
	g_assert_cmpstr (value, ==, "\"abcde\"");

	g_assert (exist_ssid ("leap-example"));
247 248 249

	value = wpa_get_value ("test-with-hash-in-psk", "psk");
	g_assert_cmpstr (value, ==, "\"xjtudlc3731###asdfasdfasdf\"");
250 251 252 253 254 255 256 257 258 259 260 261
}

static void
test_strip_string ()
{
	gchar *str = "( \"default via     202.117.16.1\" )";
	gchar *result = g_strdup (str);
	gchar *result_b = result;

	result = strip_string (result, '(');
	result = strip_string (result, ')');
	result = strip_string (result, '"');
262 263 264
	ASSERT (strcmp (result, "default via     202.117.16.1") ==
		0, "strip_string",
		"string isn't stripped, result is: %s", result);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	g_free (result_b);
}

static void
test_is_unmanaged ()
{
	ASSERT (is_managed ("eth0"), "test_is_unmanaged",
		"eth0 should be managed");
	ASSERT (!is_managed ("eth4"), "test_is_unmanaged",
		"eth4 should be unmanaged");
}

static void
test_new_connection ()
{
Jiří Klimeš's avatar
Jiří Klimeš committed
280
	GError *error = NULL;
281 282
	NMConnection *connection;

283
	connection = ifnet_update_connection_from_config_block ("eth2", NULL, &error);
284 285
	ASSERT (connection != NULL, "new connection",
		"new connection failed: %s",
Jiří Klimeš's avatar
Jiří Klimeš committed
286
		error ? error->message : "None");
287
	g_object_unref (connection);
Jiří Klimeš's avatar
Jiří Klimeš committed
288

289
	connection = ifnet_update_connection_from_config_block ("qiaomuf", NULL, &error);
290
	ASSERT (connection != NULL, "new connection",
Jiří Klimeš's avatar
Jiří Klimeš committed
291 292
		"new connection failed: %s",
		error ? error->message : "NONE");
293
	g_object_unref (connection);
Jiří Klimeš's avatar
Jiří Klimeš committed
294

295
	connection = ifnet_update_connection_from_config_block ("myxjtu2", NULL, &error);
296
	ASSERT (connection != NULL, "new connection",
Jiří Klimeš's avatar
Jiří Klimeš committed
297 298
		"new connection failed: %s",
		error ? error->message : "NONE");
299
	g_object_unref (connection);
Mu Qiao's avatar
Mu Qiao committed
300

301
	connection = ifnet_update_connection_from_config_block ("eth9", NULL, &error);
Mu Qiao's avatar
Mu Qiao committed
302 303 304 305 306
	ASSERT (connection != NULL, "new connection",
		"new connection(eth9) failed: %s",
		error ? error->message : "NONE");
	g_object_unref (connection);

307
	connection = ifnet_update_connection_from_config_block ("eth10", NULL, &error);
Mu Qiao's avatar
Mu Qiao committed
308 309 310 311
	ASSERT (connection != NULL, "new connection",
		"new connection(eth10) failed: %s",
		error ? error->message : "NONE");
	g_object_unref (connection);
312 313
}

314 315 316 317 318 319 320 321 322 323
static void
kill_backup (char **path)
{
	if (path) {
		unlink (*path);
		g_free (*path);
		*path = NULL;
	}
}

324 325 326
#define NET_GEN_NAME "net.generate"
#define SUP_GEN_NAME "wpa_supplicant.conf.generate"

327
static void
328
test_update_connection (const char *basepath)
329
{
Jiří Klimeš's avatar
Jiří Klimeš committed
330
	GError *error = NULL;
331
	NMConnection *connection;
Dan Williams's avatar
Dan Williams committed
332
	gboolean success;
333
	char *backup = NULL;
334

335
	connection = ifnet_update_connection_from_config_block ("eth0", basepath, &error);
336 337
	ASSERT (connection != NULL, "get connection",
		"get connection failed: %s",
Jiří Klimeš's avatar
Jiří Klimeš committed
338
		error ? error->message : "None");
Dan Williams's avatar
Dan Williams committed
339 340

	success = ifnet_update_parsers_by_connection (connection, "eth0",
341 342
	                                              NET_GEN_NAME,
	                                              SUP_GEN_NAME,
Dan Williams's avatar
Dan Williams committed
343
	                                              NULL,
344
	                                              &backup,
Jiří Klimeš's avatar
Jiří Klimeš committed
345
	                                              &error);
346
	kill_backup (&backup);
Dan Williams's avatar
Dan Williams committed
347
	ASSERT (success, "update connection", "update connection failed %s", "eth0");
348
	g_object_unref (connection);
Dan Williams's avatar
Dan Williams committed
349

350
	connection = ifnet_update_connection_from_config_block ("0xab3ace", basepath, &error);
351
	ASSERT (connection != NULL, "get connection", "get connection failed: %s",
Jiří Klimeš's avatar
Jiří Klimeš committed
352
		error ? error->message : "None");
Dan Williams's avatar
Dan Williams committed
353 354

	success = ifnet_update_parsers_by_connection (connection, "0xab3ace",
355 356
	                                              NET_GEN_NAME,
	                                              SUP_GEN_NAME,
Dan Williams's avatar
Dan Williams committed
357
	                                              NULL,
358
	                                              &backup,
Jiří Klimeš's avatar
Jiří Klimeš committed
359
	                                              &error);
360
	kill_backup (&backup);
361
	ASSERT (success, "update connection", "update connection failed %s", "0xab3ace");
362
	g_object_unref (connection);
363 364 365

	unlink (NET_GEN_NAME);
	unlink (SUP_GEN_NAME);
366 367 368
}

static void
369
test_add_connection (const char *basepath)
370 371
{
	NMConnection *connection;
372
	char *backup = NULL;
373

374
	connection = ifnet_update_connection_from_config_block ("eth0", basepath, NULL);
375
	ASSERT (ifnet_add_new_connection (connection, NET_GEN_NAME, SUP_GEN_NAME, NULL, &backup, NULL),
376
	        "add connection", "add connection failed: %s", "eth0");
377
	kill_backup (&backup);
378
	g_object_unref (connection);
379 380

	connection = ifnet_update_connection_from_config_block ("myxjtu2", basepath, NULL);
381
	ASSERT (ifnet_add_new_connection (connection, NET_GEN_NAME, SUP_GEN_NAME, NULL, &backup, NULL),
382
	        "add connection", "add connection failed: %s", "myxjtu2");
383
	kill_backup (&backup);
384
	g_object_unref (connection);
385 386 387

	unlink (NET_GEN_NAME);
	unlink (SUP_GEN_NAME);
388 389 390 391 392
}

static void
test_delete_connection ()
{
393
	GError *error = NULL;
394
	NMConnection *connection;
395
	char *backup = NULL;
396

397
	connection = ifnet_update_connection_from_config_block ("eth7", NULL, &error);
398
	ASSERT (connection != NULL, "get connection",
399 400
	        "get connection failed: %s",
	        error ? error->message : "None");
401
	ASSERT (ifnet_delete_connection_in_parsers ("eth7", NET_GEN_NAME, SUP_GEN_NAME, &backup),
402
	        "delete connection", "delete connection failed: %s", "eth7");
403
	kill_backup (&backup);
404
	g_object_unref (connection);
405

406
	connection = ifnet_update_connection_from_config_block ("qiaomuf", NULL, &error);
407
	ASSERT (connection != NULL, "get connection",
408 409
	        "get connection failed: %s",
	        error ? error->message : "None");
410
	ASSERT (ifnet_delete_connection_in_parsers ("qiaomuf", NET_GEN_NAME, SUP_GEN_NAME, &backup),
411
	        "delete connection", "delete connection failed: %s", "qiaomuf");
412
	kill_backup (&backup);
413
	g_object_unref (connection);
414 415 416

	unlink (NET_GEN_NAME);
	unlink (SUP_GEN_NAME);
417 418
}

419 420 421 422 423 424
static void
test_missing_config ()
{
	GError *error = NULL;
	NMConnection *connection;

425
	connection = ifnet_update_connection_from_config_block ("eth8", NULL, &error);
426 427 428 429
	ASSERT (connection == NULL && error != NULL, "get connection",
	        "get connection should fail with 'Unknown config for eth8'");
}

Dan Williams's avatar
Dan Williams committed
430 431
int
main (int argc, char **argv)
432
{
Dan Williams's avatar
Dan Williams committed
433 434 435 436 437 438 439 440 441 442 443 444
	char *f;

	g_type_init ();

	f = g_build_filename (argv[1], "net", NULL);
	ifnet_init (f);
	g_free (f);

	f = g_build_filename (argv[1], "wpa_supplicant.conf", NULL);
	wpa_parser_init (f);
	g_free (f);

445 446 447 448 449
	test_strip_string ();
	test_is_static ();
	test_has_ip6_address ();
	test_has_default_route ();
	test_getdata ();
Dan Williams's avatar
Dan Williams committed
450 451
	test_read_hostname (argv[1]);
	test_write_hostname (argv[2]);
452 453 454 455 456 457 458 459
	test_is_ip4_address ();
	test_is_ip6_address ();
	test_convert_ipv4_config_block ();
	test_convert_ipv4_routes_block ();
	test_is_unmanaged ();
	test_wpa_parser ();
	test_convert_ipv4_routes_block ();
	test_new_connection ();
460
	test_update_connection (argv[1]);
461
	test_add_connection (argv[1]);
462 463 464
	test_delete_connection ();
	test_missing_config ();

465 466
	ifnet_destroy ();
	wpa_parser_destroy ();
Dan Williams's avatar
Dan Williams committed
467 468 469 470

	f = g_path_get_basename (argv[0]);
	fprintf (stdout, "%s: SUCCESS\n", f);
	g_free (f);
471 472
	return 0;
}