about summary refs log tree commit diff
path: root/src/dhcp/tests
diff options
context:
space:
mode:
authorSebastien Bacher <seb128@ubuntu.com>2020-01-07 16:43:07 +0100
committerSebastien Bacher <seb128@ubuntu.com>2020-01-07 16:43:07 +0100
commita524db35ac5c2099671d16318524e277e88631be (patch)
tree54b29c42a317b9a13c3049ff85b14f7dce734cf6 /src/dhcp/tests
parent6917e28d2d16138d6d497dc96efafe573b4b0979 (diff)
parent724bcc5c7be3aed3646ed2b2989c34d438217176 (diff)
Merge branch 'upstream/latest' of https://salsa.debian.org/utopia-team/network-manager into upstream
Diffstat (limited to 'src/dhcp/tests')
-rw-r--r--src/dhcp/tests/meson.build3
-rw-r--r--src/dhcp/tests/test-dhcp-dhclient.c76
-rw-r--r--src/dhcp/tests/test-dhcp-utils.c72
3 files changed, 114 insertions, 37 deletions
diff --git a/src/dhcp/tests/meson.build b/src/dhcp/tests/meson.build
index 43b33951..031e2efd 100644
--- a/src/dhcp/tests/meson.build
+++ b/src/dhcp/tests/meson.build
@@ -7,7 +7,8 @@ foreach test_unit: test_units
   exe = executable(
     test_unit,
     test_unit + '.c',
-    dependencies: test_nm_dep,
+    dependencies: libnetwork_manager_test_dep,
+    c_args: test_c_flags,
   )
 
   test(
diff --git a/src/dhcp/tests/test-dhcp-dhclient.c b/src/dhcp/tests/test-dhcp-dhclient.c
index 2cc7e5ca..761f9cdb 100644
--- a/src/dhcp/tests/test-dhcp-dhclient.c
+++ b/src/dhcp/tests/test-dhcp-dhclient.c
@@ -1,20 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
- * 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 (C) 2010 Red Hat, Inc.
- *
  */
 
 #include "nm-default.h"
@@ -43,6 +29,7 @@ test_config (const char *orig,
              const char *hostname,
              guint32 timeout,
              gboolean use_fqdn,
+             NMDhcpHostnameFlags hostname_flags,
              const char *dhcp_client_id,
              GBytes *expected_new_client_id,
              const char *iface,
@@ -64,6 +51,7 @@ test_config (const char *orig,
 	                                      hostname,
 	                                      timeout,
 	                                      use_fqdn,
+	                                      hostname_flags,
 	                                      "/path/to/dhclient.conf",
 	                                      orig,
 	                                      &new_client_id);
@@ -108,7 +96,11 @@ static const char *orig_missing_expected = \
 static void
 test_orig_missing (void)
 {
-	test_config (NULL, orig_missing_expected, AF_INET, NULL, 0, FALSE, NULL, NULL, "eth0", NULL);
+	test_config (NULL,
+	             orig_missing_expected,
+	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
+	             NULL, NULL, "eth0", NULL);
 }
 
 /*****************************************************************************/
@@ -139,6 +131,7 @@ test_override_client_id (void)
 {
 	test_config (override_client_id_orig, override_client_id_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             "11:22:33:44:55:66",
 	             NULL,
 	             "eth0",
@@ -169,6 +162,7 @@ test_quote_client_id (void)
 {
 	test_config (NULL, quote_client_id_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             "abcd",
 	             NULL,
 	             "eth0",
@@ -199,6 +193,7 @@ test_quote_client_id_2 (void)
 {
 	test_config (NULL, quote_client_id_expected_2,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             "a\\bc",
 	             NULL,
 	             "eth0",
@@ -229,6 +224,7 @@ test_hex_zero_client_id (void)
 {
 	test_config (NULL, hex_zero_client_id_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             "00:11:22:33",
 	             NULL,
 	             "eth0",
@@ -259,6 +255,7 @@ test_ascii_client_id (void)
 {
 	test_config (NULL, ascii_client_id_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             "qb:cd:ef:12:34:56",
 	             NULL,
 	             "eth0",
@@ -289,6 +286,7 @@ test_hex_single_client_id (void)
 {
 	test_config (NULL, hex_single_client_id_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             "ab:cd:e:12:34:56",
 	             NULL,
 	             "eth0",
@@ -327,6 +325,7 @@ test_existing_hex_client_id (void)
 	new_client_id = g_bytes_new (bytes, sizeof (bytes));
 	test_config (existing_hex_client_id_orig, existing_hex_client_id_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             new_client_id,
 	             "eth0",
@@ -364,6 +363,7 @@ test_existing_escaped_client_id (void)
 	new_client_id = g_bytes_new ("$test\xfe", 6);
 	test_config (existing_escaped_client_id_orig, existing_escaped_client_id_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             new_client_id,
 	             "eth0",
@@ -405,6 +405,7 @@ test_existing_ascii_client_id (void)
 	new_client_id = g_bytes_new (buf, sizeof (buf));
 	test_config (existing_ascii_client_id_orig, existing_ascii_client_id_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             new_client_id,
 	             "eth0",
@@ -417,7 +418,8 @@ static const char *fqdn_expected = \
 	"\n"
 	"send fqdn.fqdn \"foo.bar.com\"; # added by NetworkManager\n"
 	"send fqdn.encoded on;\n"
-	"send fqdn.server-update on;\n"
+	"send fqdn.server-update off;\n"
+	"send fqdn.no-client-update on;\n"
 	"\n"
 	"option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n"
 	"option ms-classless-static-routes code 249 = array of unsigned integer 8;\n"
@@ -435,7 +437,10 @@ test_fqdn (void)
 {
 	test_config (NULL, fqdn_expected,
 	             AF_INET, "foo.bar.com", 0,
-	             TRUE, NULL,
+	             TRUE,
+	               NM_DHCP_HOSTNAME_FLAG_FQDN_ENCODED
+	             | NM_DHCP_HOSTNAME_FLAG_FQDN_NO_UPDATE,
+	             NULL,
 	             NULL,
 	             "eth0",
 	             NULL);
@@ -452,8 +457,9 @@ static const char *fqdn_options_override_expected = \
 	"# Merged from /path/to/dhclient.conf\n"
 	"\n"
 	"send fqdn.fqdn \"example2.com\"; # added by NetworkManager\n"
-	"send fqdn.encoded on;\n"
+	"send fqdn.encoded off;\n"
 	"send fqdn.server-update on;\n"
+	"send fqdn.no-client-update off;\n"
 	"\n"
 	"option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n"
 	"option ms-classless-static-routes code 249 = array of unsigned integer 8;\n"
@@ -476,6 +482,7 @@ test_fqdn_options_override (void)
 	test_config (fqdn_options_override_orig,
 	             fqdn_options_override_expected,
 	             AF_INET, "example2.com", 0,
+	             NM_DHCP_HOSTNAME_FLAG_FQDN_SERV_UPDATE,
 	             TRUE, NULL,
 	             NULL,
 	             "eth0",
@@ -510,6 +517,7 @@ test_override_hostname (void)
 {
 	test_config (override_hostname_orig, override_hostname_expected,
 	             AF_INET, "blahblah", 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             NULL,
 	             "eth0",
@@ -538,6 +546,7 @@ test_override_hostname6 (void)
 {
 	test_config (override_hostname6_orig, override_hostname6_expected,
 	             AF_INET6, "blahblah.local", 0, TRUE,
+	             NM_DHCP_HOSTNAME_FLAG_FQDN_SERV_UPDATE,
 	             NULL,
 	             NULL,
 	             "eth0",
@@ -550,7 +559,7 @@ static const char *nonfqdn_hostname6_expected = \
 	"# Created by NetworkManager\n"
 	"\n"
 	"send fqdn.fqdn \"blahblah\"; # added by NetworkManager\n"
-	"send fqdn.server-update on;\n"
+	"send fqdn.no-client-update on;\n"
 	"\n"
 	"also request dhcp6.name-servers;\n"
 	"also request dhcp6.domain-search;\n"
@@ -563,6 +572,7 @@ test_nonfqdn_hostname6 (void)
 	/* Non-FQDN hostname can now be used with dhclient */
 	test_config (NULL, nonfqdn_hostname6_expected,
 	             AF_INET6, "blahblah", 0, TRUE,
+	             NM_DHCP_HOSTNAME_FLAG_FQDN_NO_UPDATE,
 	             NULL,
 	             NULL,
 	             "eth0",
@@ -599,6 +609,7 @@ test_existing_alsoreq (void)
 {
 	test_config (existing_alsoreq_orig, existing_alsoreq_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             NULL,
 	             "eth0",
@@ -638,6 +649,7 @@ test_existing_req (void)
 {
 	test_config (existing_req_orig, existing_req_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             NULL,
 	             "eth0",
@@ -678,6 +690,7 @@ test_existing_multiline_alsoreq (void)
 {
 	test_config (existing_multiline_alsoreq_orig, existing_multiline_alsoreq_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             NULL,
 	             "eth0",
@@ -917,6 +930,7 @@ test_interface1 (void)
 {
 	test_config (interface1_orig, interface1_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             NULL,
 	             "eth0",
@@ -963,6 +977,7 @@ test_interface2 (void)
 {
 	test_config (interface2_orig, interface2_expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             NULL,
 	             "eth1",
@@ -996,6 +1011,11 @@ test_structured (void)
 		"    request subnet-mask, broadcast-address, time-offset, routers,\n"
 		"        domain-search, domain-name, domain-name-servers, host-name;\n"
 		"    require subnet-mask, domain-name-servers;\n"
+		"    if not option domain-name = \"example.org\" {\n"
+		"        prepend domain-name-servers 127.0.0.1;\n"
+		"    } else {\n"
+		"        prepend domain-name-servers 127.0.0.2;\n"
+		"    }  \n"
 		"    }  \n"
 		"\n"
 		"pseudo \"secondary\" \"eth0\"   {  \n"
@@ -1022,7 +1042,13 @@ test_structured (void)
 		"    interface \"eth0\";\n"
 		"    fixed-address 192.0.2.2;\n"
 		"    option subnet-mask 255.255.255.0;\n"
-		"  }  \n";
+		"  }  \n"
+		"if not option domain-name = \"example.org\" {\n"
+		"  prepend domain-name-servers 127.0.0.1;\n"
+		"  if not option domain-name = \"useless.example.com\" {\n"
+		"    prepend domain-name-servers 127.0.0.2;\n"
+		"  }\n"
+		"}\n";
 
 	static const char *const expected = \
 		"# Created by NetworkManager\n"
@@ -1033,6 +1059,12 @@ test_structured (void)
 		"send dhcp-client-identifier \"sad-and-useless\";\n"
 		"send dhcp-lease-time 8086;\n"
 		"require subnet-mask;\n"
+		"if not option domain-name = \"example.org\" {\n"
+		"prepend domain-name-servers 127.0.0.1;\n"
+		"if not option domain-name = \"useless.example.com\" {\n"
+		"prepend domain-name-servers 127.0.0.2;\n"
+		"}\n"
+		"}\n"
 		"\n"
 		"option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n"
 		"option ms-classless-static-routes code 249 = array of unsigned integer 8;\n"
@@ -1057,6 +1089,7 @@ test_structured (void)
 	new_client_id = g_bytes_new (bytes, sizeof (bytes) - 1);
 	test_config (orig, expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             new_client_id,
 	             "eth0",
@@ -1112,6 +1145,7 @@ test_config_req_intf (void)
 
 	test_config (orig, expected,
 	             AF_INET, NULL, 0, FALSE,
+	             NM_DHCP_HOSTNAME_FLAG_NONE,
 	             NULL,
 	             NULL,
 	             "eth0",
diff --git a/src/dhcp/tests/test-dhcp-utils.c b/src/dhcp/tests/test-dhcp-utils.c
index e4d4c348..e601534e 100644
--- a/src/dhcp/tests/test-dhcp-utils.c
+++ b/src/dhcp/tests/test-dhcp-utils.c
@@ -1,19 +1,6 @@
-/* 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.
- *
+// SPDX-License-Identifier: GPL-2.0+
+/*
  * Copyright (C) 2008 - 2014 Red Hat, Inc.
- *
  */
 
 #include "nm-default.h"
@@ -213,6 +200,60 @@ test_vendor_option_metered (void)
 }
 
 static void
+test_parse_search_list (void)
+{
+	guint8 *data;
+	char **domains;
+
+	data = (guint8 []) {
+		0x05, 'l', 'o', 'c', 'a', 'l',
+		0x00
+	};
+	domains = nm_dhcp_parse_search_list (data, 7);
+	g_assert (domains);
+	g_assert_cmpint (g_strv_length (domains), ==, 1);
+	g_assert_cmpstr (domains[0], ==, "local");
+	g_strfreev (domains);
+
+	data = (guint8 []) {
+		0x04, 't', 'e', 's', 't',
+		0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
+		0x03, 'c', 'o', 'm',
+		0x00,
+		0xc0, 0x05,
+		0x03, 'a', 'b', 'c',
+		0xc0, 0x0d,
+		0x06, 'f', 'o', 'o', 'b', 'a', 'r',
+		0x00
+	};
+	domains = nm_dhcp_parse_search_list (data, 34);
+	g_assert (domains);
+	g_assert_cmpint (g_strv_length (domains), ==, 4);
+	g_assert_cmpstr (domains[0], ==, "test.example.com");
+	g_assert_cmpstr (domains[1], ==, "example.com");
+	g_assert_cmpstr (domains[2], ==, "abc.com");
+	g_assert_cmpstr (domains[3], ==, "foobar");
+	g_strfreev (domains);
+
+	data = (guint8 []) {
+		0x40, 'b', 'a', 'd',
+	};
+	domains = nm_dhcp_parse_search_list (data, 4);
+	g_assert (!domains);
+
+	data = (guint8 []) {
+		0x04, 'o', 'k', 'a', 'y',
+		0x00,
+		0x40, 'b', 'a', 'd',
+	};
+	domains = nm_dhcp_parse_search_list (data, 10);
+	g_assert (domains);
+	g_assert_cmpint (g_strv_length (domains), ==, 1);
+	g_assert_cmpstr (domains[0], ==, "okay");
+	g_strfreev (domains);
+}
+
+static void
 ip4_test_route (NMIP4Config *ip4_config,
                 guint route_num,
                 const char *expected_dest,
@@ -745,6 +786,7 @@ int main (int argc, char **argv)
 	g_test_add_func ("/dhcp/ip4-prefix-classless", test_ip4_prefix_classless);
 	g_test_add_func ("/dhcp/client-id-from-string", test_client_id_from_string);
 	g_test_add_func ("/dhcp/vendor-option-metered", test_vendor_option_metered);
+	g_test_add_func ("/dhcp/parse-search-list", test_parse_search_list);
 
 	return g_test_run ();
 }