summary refs log tree commit diff
path: root/src/dhcp/tests/test-dhcp-utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dhcp/tests/test-dhcp-utils.c')
-rw-r--r--src/dhcp/tests/test-dhcp-utils.c140
1 files changed, 76 insertions, 64 deletions
diff --git a/src/dhcp/tests/test-dhcp-utils.c b/src/dhcp/tests/test-dhcp-utils.c
index ffd63493..72f31191 100644
--- a/src/dhcp/tests/test-dhcp-utils.c
+++ b/src/dhcp/tests/test-dhcp-utils.c
@@ -22,7 +22,9 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <string.h>
+#include <linux/rtnetlink.h>
 
+#include "nm-utils/nm-dedup-multi.h"
 #include "nm-utils.h"
 
 #include "dhcp/nm-dhcp-utils.h"
@@ -30,6 +32,20 @@
 
 #include "nm-test-utils-core.h"
 
+static NMIP4Config *
+_ip4_config_from_options (int ifindex,
+                          const char *iface,
+                          GHashTable *options,
+                          guint32 route_metric)
+{
+	nm_auto_unref_dedup_multi_index NMDedupMultiIndex *multi_idx = nm_dedup_multi_index_new ();
+	NMIP4Config *config;
+
+	config = nm_dhcp_utils_ip4_config_from_options (multi_idx, ifindex, iface, options, RT_TABLE_MAIN, route_metric);
+	g_assert (config);
+	return config;
+}
+
 typedef struct {
 	const char *name;
 	const char *value;
@@ -41,7 +57,7 @@ fill_table (const Option *test_options, GHashTable *table)
 	const Option *opt;
 
 	if (!table)
-		table = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
+		table = g_hash_table_new_full (nm_str_hash, g_str_equal, NULL, NULL);
 	for (opt = test_options; opt->name; opt++)
 		g_hash_table_insert (table, (gpointer) opt->name, (gpointer) opt->value);
 	return table;
@@ -86,12 +102,11 @@ test_generic_options (void)
 	const char *expected_route2_gw = "10.1.1.1";
 
 	options = fill_table (generic_options, NULL);
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 
 	/* IP4 address */
 	g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1);
-	address = nm_ip4_config_get_address (ip4_config, 0);
+	address = _nmtst_ip4_config_get_address (ip4_config, 0);
 	g_assert (inet_pton (AF_INET, expected_addr, &tmp) > 0);
 	g_assert (address->address == tmp);
 	g_assert (address->peer_address == tmp);
@@ -99,7 +114,7 @@ test_generic_options (void)
 
 	/* Gateway */
 	g_assert (inet_pton (AF_INET, expected_gw, &tmp) > 0);
-	g_assert (nm_ip4_config_get_gateway (ip4_config) == tmp);
+	g_assert (nmtst_ip4_config_get_gateway (ip4_config) == tmp);
 
 	g_assert_cmpint (nm_ip4_config_get_num_wins (ip4_config), ==, 0);
 
@@ -118,10 +133,10 @@ test_generic_options (void)
 	g_assert (nm_ip4_config_get_nameserver (ip4_config, 1) == tmp);
 
 	/* Routes */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 3);
 
 	/* Route #1 */
-	route = nm_ip4_config_get_route (ip4_config, 0);
+	route = _nmtst_ip4_config_get_route (ip4_config, 0);
 	g_assert (inet_pton (AF_INET, expected_route1_dest, &tmp) > 0);
 	g_assert (route->network == tmp);
 	g_assert (inet_pton (AF_INET, expected_route1_gw, &tmp) > 0);
@@ -130,14 +145,18 @@ test_generic_options (void)
 	g_assert_cmpint (route->metric, ==, 0);
 
 	/* Route #2 */
-	route = nm_ip4_config_get_route (ip4_config, 1);
-	g_assert (inet_pton (AF_INET, expected_route2_dest, &tmp) > 0);
-	g_assert (route->network == tmp);
-	g_assert (inet_pton (AF_INET, expected_route2_gw, &tmp) > 0);
-	g_assert (route->gateway == tmp);
+	route = _nmtst_ip4_config_get_route (ip4_config, 1);
+	g_assert (route->network == nmtst_inet4_from_string (expected_route2_dest));
+	g_assert (route->gateway == nmtst_inet4_from_string (expected_route2_gw));
 	g_assert_cmpint (route->plen, ==, 32);
 	g_assert_cmpint (route->metric, ==, 0);
 
+	route = _nmtst_ip4_config_get_route (ip4_config, 2);
+	g_assert (route->network == nmtst_inet4_from_string ("0.0.0.0"));
+	g_assert (route->gateway == nmtst_inet4_from_string ("192.168.1.1"));
+	g_assert_cmpint (route->plen, ==, 0);
+	g_assert_cmpint (route->metric, ==, 0);
+
 	g_hash_table_destroy (options);
 }
 
@@ -157,12 +176,11 @@ test_wins_options (void)
 
 	options = fill_table (generic_options, NULL);
 	options = fill_table (data, options);
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 
 	/* IP4 address */
 	g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1);
-	address = nm_ip4_config_get_address (ip4_config, 0);
+	address = _nmtst_ip4_config_get_address (ip4_config, 0);
 	g_assert (address);
 	g_assert_cmpint (nm_ip4_config_get_num_wins (ip4_config), ==, 2);
 	g_assert (inet_pton (AF_INET, expected_wins1, &tmp) > 0);
@@ -184,16 +202,14 @@ test_vendor_option_metered (void)
 	};
 
 	options = fill_table (generic_options, NULL);
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 	g_assert (nm_ip4_config_get_metered (ip4_config) == FALSE);
 	g_hash_table_destroy (options);
 	g_clear_object (&ip4_config);
 
 	options = fill_table (generic_options, NULL);
 	options = fill_table (data, options);
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 	g_assert (nm_ip4_config_get_metered (ip4_config) == TRUE);
 	g_hash_table_destroy (options);
 }
@@ -210,7 +226,7 @@ ip4_test_route (NMIP4Config *ip4_config,
 
 	g_assert (expected_prefix <= 32);
 
-	route = nm_ip4_config_get_route (ip4_config, route_num);
+	route = _nmtst_ip4_config_get_route (ip4_config, route_num);
 	g_assert (inet_pton (AF_INET, expected_dest, &tmp) > 0);
 	g_assert (route->network == tmp);
 	g_assert (inet_pton (AF_INET, expected_gw, &tmp) > 0);
@@ -226,7 +242,7 @@ ip4_test_gateway (NMIP4Config *ip4_config, const char *expected_gw)
 
 	g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1);
 	g_assert (inet_pton (AF_INET, expected_gw, &tmp) > 0);
-	g_assert (nm_ip4_config_get_gateway (ip4_config) == tmp);
+	g_assert (nmtst_ip4_config_get_gateway (ip4_config) == tmp);
 }
 
 static void
@@ -246,13 +262,13 @@ test_classless_static_routes_1 (void)
 
 	options = fill_table (generic_options, NULL);
 	options = fill_table (data, options);
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 
 	/* IP4 routes */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 3);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24);
 	ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 8);
+	ip4_test_route (ip4_config, 2, "0.0.0.0", "192.168.1.1", 0);
 
 	g_hash_table_destroy (options);
 }
@@ -274,13 +290,13 @@ test_classless_static_routes_2 (void)
 
 	options = fill_table (generic_options, NULL);
 	options = fill_table (data, options);
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 
 	/* IP4 routes */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 3);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24);
 	ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 8);
+	ip4_test_route (ip4_config, 2, "0.0.0.0", expected_route1_gw, 0);
 
 	g_hash_table_destroy (options);
 }
@@ -303,13 +319,13 @@ test_fedora_dhclient_classless_static_routes (void)
 
 	options = fill_table (generic_options, NULL);
 	options = fill_table (data, options);
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 
 	/* IP4 routes */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 3);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 25);
 	ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 7);
+	ip4_test_route (ip4_config, 2, "0.0.0.0", expected_route1_gw, 0);
 
 	/* Gateway */
 	ip4_test_gateway (ip4_config, expected_gateway);
@@ -335,13 +351,13 @@ test_dhclient_invalid_classless_routes_1 (void)
 
 	g_test_expect_message ("NetworkManager", G_LOG_LEVEL_MESSAGE,
 	                       "*ignoring invalid classless static routes*");
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 	g_test_assert_expected_messages ();
 
 	/* IP4 routes */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24);
+	ip4_test_route (ip4_config, 1, "0.0.0.0", expected_route1_gw, 0);
 
 	g_hash_table_destroy (options);
 }
@@ -366,16 +382,16 @@ test_dhcpcd_invalid_classless_routes_1 (void)
 
 	g_test_expect_message ("NetworkManager", G_LOG_LEVEL_MESSAGE,
 	                       "*ignoring invalid classless static routes*");
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 	g_test_assert_expected_messages ();
 
 	/* Test falling back to old-style static routes if the classless static
 	 * routes are invalid.
 	 */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 3);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 32);
 	ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 32);
+	ip4_test_route (ip4_config, 2, "0.0.0.0", "192.168.1.1", 0);
 
 	g_hash_table_destroy (options);
 }
@@ -399,16 +415,16 @@ test_dhclient_invalid_classless_routes_2 (void)
 
 	g_test_expect_message ("NetworkManager", G_LOG_LEVEL_MESSAGE,
 	                       "*ignoring invalid classless static routes*");
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 	g_test_assert_expected_messages ();
 
 	/* Test falling back to old-style static routes if the classless static
 	 * routes are invalid.
 	 */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 3);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 32);
 	ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 32);
+	ip4_test_route (ip4_config, 2, "0.0.0.0", "192.168.1.1", 0);
 
 	g_hash_table_destroy (options);
 }
@@ -432,8 +448,7 @@ test_dhcpcd_invalid_classless_routes_2 (void)
 
 	g_test_expect_message ("NetworkManager", G_LOG_LEVEL_MESSAGE,
 	                       "*ignoring invalid classless static routes*");
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 	g_test_assert_expected_messages ();
 
 	/* Test falling back to old-style static routes if the classless static
@@ -441,9 +456,10 @@ test_dhcpcd_invalid_classless_routes_2 (void)
 	 */
 
 	/* Routes */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 3);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 32);
 	ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 32);
+	ip4_test_route (ip4_config, 2, "0.0.0.0", "192.168.1.1", 0);
 
 	g_hash_table_destroy (options);
 }
@@ -465,13 +481,13 @@ test_dhclient_invalid_classless_routes_3 (void)
 
 	g_test_expect_message ("NetworkManager", G_LOG_LEVEL_MESSAGE,
 	                       "*ignoring invalid classless static routes*");
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 	g_test_assert_expected_messages ();
 
 	/* IP4 routes */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24);
+	ip4_test_route (ip4_config, 1, "0.0.0.0", expected_route1_gw, 0);
 
 	g_hash_table_destroy (options);
 }
@@ -493,13 +509,13 @@ test_dhcpcd_invalid_classless_routes_3 (void)
 
 	g_test_expect_message ("NetworkManager", G_LOG_LEVEL_MESSAGE,
 	                       "*DHCP provided invalid classless static route*");
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 	g_test_assert_expected_messages ();
 
 	/* IP4 routes */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24);
+	ip4_test_route (ip4_config, 1, "0.0.0.0", expected_route1_gw, 0);
 
 	g_hash_table_destroy (options);
 }
@@ -519,12 +535,12 @@ test_dhclient_gw_in_classless_routes (void)
 
 	options = fill_table (generic_options, NULL);
 	options = fill_table (data, options);
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 
 	/* IP4 routes */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24);
+	ip4_test_route (ip4_config, 1, "0.0.0.0", "192.2.3.4", 0);
 
 	/* Gateway */
 	ip4_test_gateway (ip4_config, expected_gateway);
@@ -547,12 +563,12 @@ test_dhcpcd_gw_in_classless_routes (void)
 
 	options = fill_table (generic_options, NULL);
 	options = fill_table (data, options);
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 
 	/* IP4 routes */
-	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1);
+	g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2);
 	ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24);
+	ip4_test_route (ip4_config, 1, "0.0.0.0", "192.2.3.4", 0);
 
 	/* Gateway */
 	ip4_test_gateway (ip4_config, expected_gateway);
@@ -575,8 +591,7 @@ test_escaped_domain_searches (void)
 
 	options = fill_table (generic_options, NULL);
 	options = fill_table (data, options);
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 
 	/* domain searches */
 	g_assert_cmpint (nm_ip4_config_get_num_searches (ip4_config), ==, 3);
@@ -602,8 +617,7 @@ test_invalid_escaped_domain_searches (void)
 
 	g_test_expect_message ("NetworkManager", G_LOG_LEVEL_MESSAGE,
 	                       "*invalid domain search*");
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 	g_test_assert_expected_messages ();
 
 	/* domain searches */
@@ -623,11 +637,10 @@ test_ip4_missing_prefix (const char *ip, guint32 expected_prefix)
 	g_hash_table_insert (options, "ip_address", (gpointer) ip);
 	g_hash_table_remove (options, "subnet_mask");
 
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 
 	g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1);
-	address = nm_ip4_config_get_address (ip4_config, 0);
+	address = _nmtst_ip4_config_get_address (ip4_config, 0);
 	g_assert (address);
 	g_assert_cmpint (address->plen, ==, expected_prefix);
 
@@ -668,11 +681,10 @@ test_ip4_prefix_classless (void)
 	g_hash_table_insert (options, "ip_address", "172.16.54.22");
 	g_hash_table_insert (options, "subnet_mask", "255.255.252.0");
 
-	ip4_config = nm_dhcp_utils_ip4_config_from_options (1, "eth0", options, 0);
-	g_assert (ip4_config);
+	ip4_config = _ip4_config_from_options (1, "eth0", options, 0);
 
 	g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1);
-	address = nm_ip4_config_get_address (ip4_config, 0);
+	address = _nmtst_ip4_config_get_address (ip4_config, 0);
 	g_assert (address);
 	g_assert_cmpint (address->plen, ==, 22);