diff options
Diffstat (limited to 'src/core/dhcp/tests')
| -rw-r--r-- | src/core/dhcp/tests/meson.build | 22 | ||||
| -rw-r--r-- | src/core/dhcp/tests/test-dhclient-commented-duid.leases | 2 | ||||
| -rw-r--r-- | src/core/dhcp/tests/test-dhclient-duid.leases | 2 | ||||
| -rw-r--r-- | src/core/dhcp/tests/test-dhcp-dhclient.c | 1341 | ||||
| -rw-r--r-- | src/core/dhcp/tests/test-dhcp-utils.c | 826 |
5 files changed, 2193 insertions, 0 deletions
diff --git a/src/core/dhcp/tests/meson.build b/src/core/dhcp/tests/meson.build new file mode 100644 index 00000000..e43c8cab --- /dev/null +++ b/src/core/dhcp/tests/meson.build @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later + +test_units = [ + 'test-dhcp-dhclient', + 'test-dhcp-utils', +] + +foreach test_unit: test_units + exe = executable( + test_unit, + test_unit + '.c', + dependencies: libNetworkManagerTest_dep, + c_args: test_c_flags, + ) + + test( + 'dhcp/' + test_unit, + test_script, + args: test_args + [exe.full_path()], + timeout: default_test_timeout, + ) +endforeach diff --git a/src/core/dhcp/tests/test-dhclient-commented-duid.leases b/src/core/dhcp/tests/test-dhclient-commented-duid.leases new file mode 100644 index 00000000..3e46ae7d --- /dev/null +++ b/src/core/dhcp/tests/test-dhclient-commented-duid.leases @@ -0,0 +1,2 @@ +#default-duid "\000\001\000\001\030y\246\023`g \354Lp"; + diff --git a/src/core/dhcp/tests/test-dhclient-duid.leases b/src/core/dhcp/tests/test-dhclient-duid.leases new file mode 100644 index 00000000..229331d4 --- /dev/null +++ b/src/core/dhcp/tests/test-dhclient-duid.leases @@ -0,0 +1,2 @@ +default-duid "\000\001\000\001\030y\246\023`g \354Lp"; + diff --git a/src/core/dhcp/tests/test-dhcp-dhclient.c b/src/core/dhcp/tests/test-dhcp-dhclient.c new file mode 100644 index 00000000..77626f69 --- /dev/null +++ b/src/core/dhcp/tests/test-dhcp-dhclient.c @@ -0,0 +1,1341 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2010 Red Hat, Inc. + */ + +#include "src/core/nm-default-daemon.h" + +#include <unistd.h> +#include <arpa/inet.h> +#include <linux/rtnetlink.h> + +#include "nm-glib-aux/nm-dedup-multi.h" + +#include "NetworkManagerUtils.h" +#include "dhcp/nm-dhcp-dhclient-utils.h" +#include "dhcp/nm-dhcp-utils.h" +#include "nm-utils.h" +#include "nm-ip4-config.h" +#include "platform/nm-platform.h" + +#include "nm-test-utils-core.h" + +#define TEST_DIR NM_BUILD_SRCDIR "/src/core/dhcp/tests" +#define TEST_MUDURL "https://example.com/mud.json" + +static void +test_config(const char * orig, + const char * expected, + int addr_family, + const char * hostname, + guint32 timeout, + gboolean use_fqdn, + NMDhcpHostnameFlags hostname_flags, + const char * dhcp_client_id, + GBytes * expected_new_client_id, + const char * iface, + const char * anycast_addr, + const char * mud_url) +{ + gs_free char *new = NULL; + gs_unref_bytes GBytes *client_id = NULL; + gs_unref_bytes GBytes *new_client_id = NULL; + + if (dhcp_client_id) { + client_id = nm_dhcp_utils_client_id_string_to_bytes(dhcp_client_id); + g_assert(client_id); + } + + new = nm_dhcp_dhclient_create_config(iface, + addr_family, + client_id, + anycast_addr, + hostname, + timeout, + use_fqdn, + hostname_flags, + mud_url, + NULL, + "/path/to/dhclient.conf", + orig, + &new_client_id); + g_assert(new != NULL); + + if (!nm_streq(new, expected)) { + g_message("\n* OLD ---------------------------------\n" + "%s" + "\n- NEW -----------------------------------\n" + "%s" + "\n+ EXPECTED ++++++++++++++++++++++++++++++\n" + "%s" + "\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", + orig, + new, + expected); + } + g_assert_cmpstr(new, ==, expected); + + if (expected_new_client_id) { + g_assert(new_client_id); + g_assert(g_bytes_equal(new_client_id, expected_new_client_id)); + } else + g_assert(new_client_id == NULL); +} + +/*****************************************************************************/ + +static const char *orig_missing_expected = + "# Created by NetworkManager\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +test_orig_missing(void) +{ + test_config(NULL, + orig_missing_expected, + AF_INET, + NULL, + 0, + FALSE, + NM_DHCP_HOSTNAME_FLAG_NONE, + NULL, + NULL, + "eth0", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *orig_missing_add_mud_url_expected = + "# Created by NetworkManager\n" + "\n" + "option mudurl code 161 = text;\n" + "send mudurl \"https://example.com/mud.json\";\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +test_orig_missing_add_mud_url(void) +{ + test_config(NULL, + orig_missing_add_mud_url_expected, + AF_INET, + NULL, + 0, + FALSE, + NM_DHCP_HOSTNAME_FLAG_NONE, + NULL, + NULL, + "eth0", + NULL, + TEST_MUDURL); +} + +/*****************************************************************************/ + +static const char *override_client_id_orig = "send dhcp-client-identifier 00:30:04:20:7A:08;\n"; + +static const char *override_client_id_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send dhcp-client-identifier 11:22:33:44:55:66; # added by NetworkManager\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *quote_client_id_expected = + "# Created by NetworkManager\n" + "\n" + "send dhcp-client-identifier \"\\x00abcd\"; # added by NetworkManager\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *quote_client_id_expected_2 = + "# Created by NetworkManager\n" + "\n" + "send dhcp-client-identifier 00:61:5c:62:63; # added by NetworkManager\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *hex_zero_client_id_expected = + "# Created by NetworkManager\n" + "\n" + "send dhcp-client-identifier 00:11:22:33; # added by NetworkManager\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *ascii_client_id_expected = + "# Created by NetworkManager\n" + "\n" + "send dhcp-client-identifier \"\\x00qb:cd:ef:12:34:56\"; # added by NetworkManager\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *hex_single_client_id_expected = + "# Created by NetworkManager\n" + "\n" + "send dhcp-client-identifier ab:cd:0e:12:34:56; # added by NetworkManager\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *existing_hex_client_id_orig = "send dhcp-client-identifier 10:30:04:20:7A:08;\n"; + +static const char *existing_hex_client_id_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send dhcp-client-identifier 10:30:04:20:7A:08;\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +test_existing_hex_client_id(void) +{ + gs_unref_bytes GBytes *new_client_id = NULL; + const guint8 bytes[] = {0x10, 0x30, 0x04, 0x20, 0x7A, 0x08}; + + 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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *existing_escaped_client_id_orig = + "send dhcp-client-identifier \"\\044test\\xfe\";\n"; + +static const char *existing_escaped_client_id_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send dhcp-client-identifier \"\\044test\\xfe\";\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +test_existing_escaped_client_id(void) +{ + gs_unref_bytes GBytes *new_client_id = NULL; + + 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", + NULL, + NULL); +} + +/*****************************************************************************/ + +#define EACID "qb:cd:ef:12:34:56" + +static const char *existing_ascii_client_id_orig = + "send dhcp-client-identifier \"\\x00" EACID "\";\n"; + +static const char *existing_ascii_client_id_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send dhcp-client-identifier \"\\x00" EACID "\";\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +test_existing_ascii_client_id(void) +{ + gs_unref_bytes GBytes *new_client_id = NULL; + char buf[NM_STRLEN(EACID) + 1] = {0}; + + memcpy(buf + 1, EACID, NM_STRLEN(EACID)); + 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", + NULL, + NULL); +} +/*****************************************************************************/ + +static const char *fqdn_expected = + "# Created by NetworkManager\n" + "\n" + "send fqdn.fqdn \"foo.bar.com\"; # added by NetworkManager\n" + "send fqdn.encoded 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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n\n"; + +static void +test_fqdn(void) +{ + test_config(NULL, + fqdn_expected, + AF_INET, + "foo.bar.com", + 0, + TRUE, + NM_DHCP_HOSTNAME_FLAG_FQDN_ENCODED | NM_DHCP_HOSTNAME_FLAG_FQDN_NO_UPDATE, + NULL, + NULL, + "eth0", + NULL, + NULL); +} + +static const char *fqdn_options_override_orig = + "\n" + "send fqdn.fqdn \"foobar.com\"\n" /* NM must ignore this ... */ + "send fqdn.encoded off;\n" /* ... and honor these */ + "send fqdn.server-update off;\n"; + +static const char *fqdn_options_override_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send fqdn.fqdn \"example2.com\"; # added by NetworkManager\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n" + "# FQDN options from /path/to/dhclient.conf\n" + "send fqdn.encoded off;\n" + "send fqdn.server-update off;\n\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *override_hostname_orig = "send host-name \"foobar\";\n"; + +static const char *override_hostname_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send host-name \"blahblah\"; # added by NetworkManager\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" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *override_hostname6_orig = "send fqdn.fqdn \"foobar\";\n"; + +static const char *override_hostname6_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send fqdn.fqdn \"blahblah.local\"; # added by NetworkManager\n" + "send fqdn.server-update on;\n" + "\n" + "also request dhcp6.name-servers;\n" + "also request dhcp6.domain-search;\n" + "also request dhcp6.client-id;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *nonfqdn_hostname6_expected = + "# Created by NetworkManager\n" + "\n" + "send fqdn.fqdn \"blahblah\"; # added by NetworkManager\n" + "send fqdn.no-client-update on;\n" + "\n" + "also request dhcp6.name-servers;\n" + "also request dhcp6.domain-search;\n" + "also request dhcp6.client-id;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *existing_alsoreq_orig = "also request something;\n" + "also request another-thing;\n"; + +static const char *existing_alsoreq_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\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" + "option wpad code 252 = string;\n" + "\n" + "also request something;\n" + "also request another-thing;\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *existing_req_orig = "request something;\n" + "also request some-other-thing;\n" + "request another-thing;\n" + "also request yet-another-thing;\n"; + +static const char *existing_req_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\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" + "option wpad code 252 = string;\n" + "\n" + "request; # override dhclient defaults\n" + "also request another-thing;\n" + "also request yet-another-thing;\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *existing_multiline_alsoreq_orig = + "also request something another-thing yet-another-thing\n" + " foobar baz blah;\n"; + +static const char *existing_multiline_alsoreq_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\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" + "option wpad code 252 = string;\n" + "\n" + "also request something;\n" + "also request another-thing;\n" + "also request yet-another-thing;\n" + "also request foobar;\n" + "also request baz;\n" + "also request blah;\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +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", + NULL, + NULL); +} + +/*****************************************************************************/ + +static void +test_one_duid(const char *escaped, const guint8 *unescaped, guint len) +{ + gs_unref_bytes GBytes *t1 = NULL; + gs_unref_bytes GBytes *t2 = NULL; + gs_free char * w = NULL; + + t1 = nm_dhcp_dhclient_unescape_duid(escaped); + g_assert(t1); + g_assert(nm_utils_gbytes_equal_mem(t1, unescaped, len)); + + t2 = g_bytes_new(unescaped, len); + w = nm_dhcp_dhclient_escape_duid(t2); + g_assert(w); + g_assert_cmpstr(escaped, ==, w); +} + +static void +test_duids(void) +{ + const guint8 test1_u[] = + {0x00, 0x01, 0x00, 0x01, 0x13, 0x6f, 0x13, 0x6e, 0x00, 0x22, 0xfa, 0x8c, 0xd6, 0xc2}; + const char *test1_s = "\\000\\001\\000\\001\\023o\\023n\\000\\\"\\372\\214\\326\\302"; + + const guint8 test2_u[] = + {0x00, 0x01, 0x00, 0x01, 0x17, 0x57, 0xee, 0x39, 0x00, 0x23, 0x15, 0x08, 0x7E, 0xac}; + const char *test2_s = "\\000\\001\\000\\001\\027W\\3569\\000#\\025\\010~\\254"; + + const guint8 test3_u[] = + {0x00, 0x01, 0x00, 0x01, 0x17, 0x58, 0xe8, 0x58, 0x00, 0x23, 0x15, 0x08, 0x7e, 0xac}; + const char *test3_s = "\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254"; + + const guint8 test4_u[] = + {0x00, 0x01, 0x00, 0x01, 0x15, 0xd5, 0x31, 0x97, 0x00, 0x16, 0xeb, 0x04, 0x45, 0x18}; + const char *test4_s = "\\000\\001\\000\\001\\025\\3251\\227\\000\\026\\353\\004E\\030"; + + const char *bad_s = "\\000\\001\\000\\001\\425\\3251\\227\\000\\026\\353\\004E\\030"; + + test_one_duid(test1_s, test1_u, sizeof(test1_u)); + test_one_duid(test2_s, test2_u, sizeof(test2_u)); + test_one_duid(test3_s, test3_u, sizeof(test3_u)); + test_one_duid(test4_s, test4_u, sizeof(test4_u)); + + /* Invalid octal digit */ + g_assert(nm_dhcp_dhclient_unescape_duid(bad_s) == NULL); +} + +static void +test_read_duid_from_leasefile(void) +{ + const guint8 expected[] = + {0x00, 0x01, 0x00, 0x01, 0x18, 0x79, 0xa6, 0x13, 0x60, 0x67, 0x20, 0xec, 0x4c, 0x70}; + gs_unref_bytes GBytes *duid = NULL; + GError * error = NULL; + + duid = nm_dhcp_dhclient_read_duid(TEST_DIR "/test-dhclient-duid.leases", &error); + nmtst_assert_success(duid, error); + + g_assert(nm_utils_gbytes_equal_mem(duid, expected, G_N_ELEMENTS(expected))); +} + +static void +test_read_commented_duid_from_leasefile(void) +{ + GBytes *duid; + GError *error = NULL; + + duid = nm_dhcp_dhclient_read_duid(TEST_DIR "/test-dhclient-commented-duid.leases", &error); + g_assert_no_error(error); + g_assert(duid == NULL); +} + +/*****************************************************************************/ + +static void +_save_duid(const char *path, const guint8 *duid_bin, gsize duid_len) +{ + gs_unref_bytes GBytes *duid = NULL; + GError * error = NULL; + gboolean success; + + g_assert(path); + g_assert(duid_bin); + g_assert(duid_len > 0); + + duid = g_bytes_new(duid_bin, duid_len); + success = nm_dhcp_dhclient_save_duid(path, duid, &error); + nmtst_assert_success(success, error); +} + +static void +test_write_duid(void) +{ + const guint8 duid[] = {000, 001, 000, 001, 027, 'X', 0350, 'X', 0, '#', 025, 010, '~', 0254}; + const char * expected_contents = + "default-duid \"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n"; + GError * error = NULL; + gs_free char *contents = NULL; + gboolean success; + const char * path = "test-dhclient-write-duid.leases"; + + _save_duid(path, duid, G_N_ELEMENTS(duid)); + + success = g_file_get_contents(path, &contents, NULL, &error); + nmtst_assert_success(success, error); + + unlink(path); + + g_assert_cmpstr(expected_contents, ==, contents); +} + +static void +test_write_existing_duid(void) +{ + const guint8 duid[] = + {000, 001, 000, 001, 023, 'o', 023, 'n', 000, '"', 0372, 0214, 0326, 0302}; + const char *original_contents = + "default-duid \"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n"; + const char *expected_contents = + "default-duid \"\\000\\001\\000\\001\\023o\\023n\\000\\\"\\372\\214\\326\\302\";\n"; + GError * error = NULL; + gs_free char *contents = NULL; + gboolean success; + const char * path = "test-dhclient-write-existing-duid.leases"; + + success = g_file_set_contents(path, original_contents, -1, &error); + nmtst_assert_success(success, error); + + /* Save other DUID; should be overwritten */ + _save_duid(path, duid, G_N_ELEMENTS(duid)); + + /* reread original contents */ + success = g_file_get_contents(path, &contents, NULL, &error); + nmtst_assert_success(success, error); + + unlink(path); + g_assert_cmpstr(expected_contents, ==, contents); +} + +static const guint8 DUID_BIN[] = + {000, 001, 000, 001, 023, 'o', 023, 'n', 000, '"', 0372, 0214, 0326, 0302}; +#define DUID "\\000\\001\\000\\001\\023o\\023n\\000\\\"\\372\\214\\326\\302" + +static void +test_write_existing_commented_duid(void) +{ +#define ORIG_CONTENTS "#default-duid \"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n" + const char * expected_contents = "default-duid \"" DUID "\";\n" ORIG_CONTENTS; + GError * error = NULL; + gs_free char *contents = NULL; + gboolean success; + const char * path = "test-dhclient-write-existing-commented-duid.leases"; + + success = g_file_set_contents(path, ORIG_CONTENTS, -1, &error); + nmtst_assert_success(success, error); + + /* Save other DUID; should be saved on top */ + _save_duid(path, DUID_BIN, G_N_ELEMENTS(DUID_BIN)); + + /* reread original contents */ + success = g_file_get_contents(path, &contents, NULL, &error); + nmtst_assert_success(success, error); + + unlink(path); + g_assert_cmpstr(expected_contents, ==, contents); +#undef ORIG_CONTENTS +} + +static void +test_write_existing_multiline_duid(void) +{ +#define ORIG_CONTENTS \ + "### Commented old DUID ###\n" \ + "#default-duid \"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n" + const char * expected_contents = "default-duid \"" DUID "\";\n" ORIG_CONTENTS; + GError * error = NULL; + gs_free char * contents = NULL; + gboolean success; + nmtst_auto_unlinkfile char *path = + g_strdup("test-dhclient-write-existing-multiline-duid.leases"); + + success = g_file_set_contents(path, ORIG_CONTENTS, -1, &error); + nmtst_assert_success(success, error); + + _save_duid(path, DUID_BIN, G_N_ELEMENTS(DUID_BIN)); + + success = g_file_get_contents(path, &contents, NULL, &error); + nmtst_assert_success(success, error); + + g_assert_cmpstr(expected_contents, ==, contents); +#undef ORIG_CONTENTS +} + +/*****************************************************************************/ + +static const char *interface1_orig = "interface \"eth0\" {\n" + "\talso request my-option;\n" + "\tinitial-delay 5;\n" + "}\n" + "interface \"eth1\" {\n" + "\talso request another-option;\n" + "\tinitial-delay 0;\n" + "}\n" + "\n" + "also request yet-another-option;\n"; + +static const char *interface1_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "initial-delay 5;\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" + "option wpad code 252 = string;\n" + "\n" + "also request my-option;\n" + "also request yet-another-option;\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +test_interface1(void) +{ + test_config(interface1_orig, + interface1_expected, + AF_INET, + NULL, + 0, + FALSE, + NM_DHCP_HOSTNAME_FLAG_NONE, + NULL, + NULL, + "eth0", + NULL, + NULL); +} + +/*****************************************************************************/ + +static const char *interface2_orig = "interface eth0 {\n" + "\talso request my-option;\n" + "\tinitial-delay 5;\n" + " }\n" + "interface eth1 {\n" + "\tinitial-delay 0;\n" + "\trequest another-option;\n" + " } \n" + "\n" + "also request yet-another-option;\n"; + +static const char *interface2_expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "initial-delay 0;\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" + "option wpad code 252 = string;\n" + "\n" + "request; # override dhclient defaults\n" + "also request another-option;\n" + "also request yet-another-option;\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + +static void +test_interface2(void) +{ + test_config(interface2_orig, + interface2_expected, + AF_INET, + NULL, + 0, + FALSE, + NM_DHCP_HOSTNAME_FLAG_NONE, + NULL, + NULL, + "eth1", + NULL, + NULL); +} + +static void +test_structured(void) +{ + gs_unref_bytes GBytes *new_client_id = NULL; + const guint8 bytes[] = "sad-and-useless"; + + static const char *const orig = + "interface \"eth0\" { \n" + " send host-name \"useless.example.com\";\n" + " hardware ethernet de:ad:80:86:ba:be;\n" + " send dhcp-client-identifier \"sad-and-useless\";\n" + " script \"/bin/useless\";\n" + " send dhcp-lease-time 8086;\n" + " request subnet-mask, broadcast-address, time-offset, routers,\n" + " domain-search, domain-name, host-name;\n" + " require subnet-mask;\n" + "} \n" + "\n" + " interface \"eth1\" { \n" + " send host-name \"sad.example.com\";\n" + " hardware ethernet de:ca:f6:66:ca:fe;\n" + " send dhcp-client-identifier \"useless-and-miserable\";\n" + " script \"/bin/miserable\";\n" + " send dhcp-lease-time 1337;\n" + " 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" + " send dhcp-client-identifier \"sad-useless-and-secondary\";\n" + " script \"/bin/secondary\";\n" + " send host-name \"secondary.useless.example.com\";\n" + " send dhcp-lease-time 666;\n" + " request routers;\n" + " require routers;\n" + " } \n" + "\n" + " pseudo \"tertiary\" \"eth0\" { \n" + " send dhcp-client-identifier \"sad-useless-and-tertiary\";\n" + " script \"/bin/tertiary\";\n" + " send host-name \"tertiary.useless.example.com\";\n" + "} \n" + "\n" + " alias{ \n" + " interface \"eth0\";\n" + " fixed-address 192.0.2.1;\n" + " option subnet-mask 255.255.255.0;\n" + " } \n" + " lease { \n" + " interface \"eth0\";\n" + " fixed-address 192.0.2.2;\n" + " option subnet-mask 255.255.255.0;\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" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send host-name \"useless.example.com\";\n" + "hardware ethernet de:ad:80:86:ba:be;\n" + "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" + "option wpad code 252 = string;\n" + "\n" + "request; # override dhclient defaults\n" + "also request subnet-mask;\n" + "also request broadcast-address;\n" + "also request time-offset;\n" + "also request routers;\n" + "also request domain-search;\n" + "also request domain-name;\n" + "also request host-name;\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "also request root-path;\n" + "\n"; + + 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", + NULL, + NULL); +} + +static void +test_config_req_intf(void) +{ + static const char *const orig = "request subnet-mask, broadcast-address, routers,\n" + "\trfc3442-classless-static-routes,\n" + "\tinterface-mtu, host-name, domain-name, domain-search,\n" + "\tdomain-name-servers, nis-domain, nis-servers,\n" + "\tnds-context, nds-servers, nds-tree-name,\n" + "\tnetbios-name-servers, netbios-dd-server,\n" + "\tnetbios-node-type, netbios-scope, ntp-servers;\n" + ""; + static const char *const expected = + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\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" + "option wpad code 252 = string;\n" + "\n" + "request; # override dhclient defaults\n" + "also request subnet-mask;\n" + "also request broadcast-address;\n" + "also request routers;\n" + "also request rfc3442-classless-static-routes;\n" + "also request interface-mtu;\n" + "also request host-name;\n" + "also request domain-name;\n" + "also request domain-search;\n" + "also request domain-name-servers;\n" + "also request nis-domain;\n" + "also request nis-servers;\n" + "also request nds-context;\n" + "also request nds-servers;\n" + "also request nds-tree-name;\n" + "also request netbios-name-servers;\n" + "also request netbios-dd-server;\n" + "also request netbios-node-type;\n" + "also request netbios-scope;\n" + "also request ntp-servers;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request root-path;\n" + "\n"; + + test_config(orig, + expected, + AF_INET, + NULL, + 0, + FALSE, + NM_DHCP_HOSTNAME_FLAG_NONE, + NULL, + NULL, + "eth0", + NULL, + NULL); +} + +/*****************************************************************************/ + +NMTST_DEFINE(); + +int +main(int argc, char **argv) +{ + nmtst_init_with_logging(&argc, &argv, NULL, "DEFAULT"); + + g_test_add_func("/dhcp/dhclient/orig_missing", test_orig_missing); + g_test_add_func("/dhcp/dhclient/orig_missing_add_mud_url", test_orig_missing_add_mud_url); + g_test_add_func("/dhcp/dhclient/override_client_id", test_override_client_id); + g_test_add_func("/dhcp/dhclient/quote_client_id/1", test_quote_client_id); + g_test_add_func("/dhcp/dhclient/quote_client_id/2", test_quote_client_id_2); + g_test_add_func("/dhcp/dhclient/hex_zero_client_id", test_hex_zero_client_id); + g_test_add_func("/dhcp/dhclient/ascii_client_id", test_ascii_client_id); + g_test_add_func("/dhcp/dhclient/hex_single_client_id", test_hex_single_client_id); + g_test_add_func("/dhcp/dhclient/existing-hex-client-id", test_existing_hex_client_id); + g_test_add_func("/dhcp/dhclient/existing-client-id", test_existing_escaped_client_id); + g_test_add_func("/dhcp/dhclient/existing-ascii-client-id", test_existing_ascii_client_id); + g_test_add_func("/dhcp/dhclient/fqdn", test_fqdn); + g_test_add_func("/dhcp/dhclient/fqdn_options_override", test_fqdn_options_override); + g_test_add_func("/dhcp/dhclient/override_hostname", test_override_hostname); + g_test_add_func("/dhcp/dhclient/override_hostname6", test_override_hostname6); + g_test_add_func("/dhcp/dhclient/nonfqdn_hostname6", test_nonfqdn_hostname6); + g_test_add_func("/dhcp/dhclient/existing_req", test_existing_req); + g_test_add_func("/dhcp/dhclient/existing_alsoreq", test_existing_alsoreq); + g_test_add_func("/dhcp/dhclient/existing_multiline_alsoreq", test_existing_multiline_alsoreq); + g_test_add_func("/dhcp/dhclient/duids", test_duids); + g_test_add_func("/dhcp/dhclient/interface/1", test_interface1); + g_test_add_func("/dhcp/dhclient/interface/2", test_interface2); + g_test_add_func("/dhcp/dhclient/config/req_intf", test_config_req_intf); + g_test_add_func("/dhcp/dhclient/structured", test_structured); + + g_test_add_func("/dhcp/dhclient/read_duid_from_leasefile", test_read_duid_from_leasefile); + g_test_add_func("/dhcp/dhclient/read_commented_duid_from_leasefile", + test_read_commented_duid_from_leasefile); + + g_test_add_func("/dhcp/dhclient/write_duid", test_write_duid); + g_test_add_func("/dhcp/dhclient/write_existing_duid", test_write_existing_duid); + g_test_add_func("/dhcp/dhclient/write_existing_commented_duid", + test_write_existing_commented_duid); + g_test_add_func("/dhcp/dhclient/write_existing_multiline_duid", + test_write_existing_multiline_duid); + + return g_test_run(); +} diff --git a/src/core/dhcp/tests/test-dhcp-utils.c b/src/core/dhcp/tests/test-dhcp-utils.c new file mode 100644 index 00000000..9b54e2cd --- /dev/null +++ b/src/core/dhcp/tests/test-dhcp-utils.c @@ -0,0 +1,826 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2008 - 2014 Red Hat, Inc. + */ + +#include "src/core/nm-default-daemon.h" + +#include <netinet/in.h> +#include <arpa/inet.h> +#include <linux/rtnetlink.h> + +#include "nm-glib-aux/nm-dedup-multi.h" +#include "nm-utils.h" + +#include "dhcp/nm-dhcp-utils.h" +#include "dhcp/nm-dhcp-options.h" +#include "platform/nm-platform.h" + +#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; +} Option; + +static GHashTable * +fill_table(const Option *test_options, GHashTable *table) +{ + const Option *opt; + + if (!table) + 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; +} + +static const Option generic_options[] = { + {"subnet_mask", "255.255.255.0"}, + {"ip_address", "192.168.1.106"}, + {"network_number", "192.168.1.0"}, + {"expiry", "1232324877"}, + {"dhcp_lease_time", "3600"}, + {"dhcp_server_identifier", "192.168.1.1"}, + {"routers", "192.168.1.1"}, + {"domain_name_servers", "216.254.95.2 216.231.41.2"}, + {"dhcp_message_type", "5"}, + {"broadcast_address", "192.168.1.255"}, + {"domain_search", "foobar.com blah.foobar.com"}, + {"host_name", "nmreallywhipsthe"}, + {"domain_name", "lamasass.com"}, + {"interface_mtu", "987"}, + {"static_routes", "10.1.1.5 10.1.1.1 100.99.88.56 10.1.1.1"}, + {NULL, NULL}}; + +static void +test_generic_options(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const NMPlatformIP4Address * address; + const NMPlatformIP4Route * route; + guint32 tmp; + const char * expected_addr = "192.168.1.106"; + const char * expected_gw = "192.168.1.1"; + const char * expected_dns1 = "216.254.95.2"; + const char * expected_dns2 = "216.231.41.2"; + const char * expected_search1 = "foobar.com"; + const char * expected_search2 = "blah.foobar.com"; + const char * expected_route1_dest = "10.1.1.5"; + const char * expected_route1_gw = "10.1.1.1"; + const char * expected_route2_dest = "100.99.88.56"; + const char * expected_route2_gw = "10.1.1.1"; + + options = fill_table(generic_options, NULL); + 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 = _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); + g_assert_cmpint(address->plen, ==, 24); + + /* Gateway */ + g_assert(inet_pton(AF_INET, expected_gw, &tmp) > 0); + g_assert(nmtst_ip4_config_get_gateway(ip4_config) == tmp); + + g_assert_cmpint(nm_ip4_config_get_num_wins(ip4_config), ==, 0); + + g_assert_cmpint(nm_ip4_config_get_mtu(ip4_config), ==, 987); + + /* Domain searches */ + g_assert_cmpint(nm_ip4_config_get_num_searches(ip4_config), ==, 2); + g_assert_cmpstr(nm_ip4_config_get_search(ip4_config, 0), ==, expected_search1); + g_assert_cmpstr(nm_ip4_config_get_search(ip4_config, 1), ==, expected_search2); + + /* DNS servers */ + g_assert_cmpint(nm_ip4_config_get_num_nameservers(ip4_config), ==, 2); + g_assert(inet_pton(AF_INET, expected_dns1, &tmp) > 0); + g_assert(nm_ip4_config_get_nameserver(ip4_config, 0) == tmp); + g_assert(inet_pton(AF_INET, expected_dns2, &tmp) > 0); + g_assert(nm_ip4_config_get_nameserver(ip4_config, 1) == tmp); + + /* Routes */ + g_assert_cmpint(nm_ip4_config_get_num_routes(ip4_config), ==, 3); + + /* Route #1 */ + 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); + g_assert(route->gateway == tmp); + g_assert_cmpint(route->plen, ==, 32); + g_assert_cmpint(route->metric, ==, 0); + + /* Route #2 */ + 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); +} + +static void +test_wins_options(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const NMPlatformIP4Address * address; + guint32 tmp; + const char * expected_wins1 = "63.12.199.5"; + const char * expected_wins2 = "150.4.88.120"; + static const Option data[] = {{"netbios_name_servers", "63.12.199.5 150.4.88.120"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + 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 = _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); + g_assert(nm_ip4_config_get_wins(ip4_config, 0) == tmp); + g_assert(inet_pton(AF_INET, expected_wins2, &tmp) > 0); + g_assert(nm_ip4_config_get_wins(ip4_config, 1) == tmp); + + g_hash_table_destroy(options); +} + +static void +test_vendor_option_metered(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + static const Option data[] = {{"vendor_encapsulated_options", "ANDROID_METERED"}, {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + 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 = _ip4_config_from_options(1, "eth0", options, 0); + g_assert(nm_ip4_config_get_metered(ip4_config) == TRUE); + g_hash_table_destroy(options); +} + +static void +test_parse_search_list(void) +{ + guint8 *data; + char ** domains; + + data = (guint8[]){0x05, 'l', 'o', 'c', 'a', 'l', 0x00}; + domains = nm_dhcp_lease_data_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_lease_data_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_lease_data_parse_search_list(data, 4); + g_assert(!domains); + + data = (guint8[]){ + 0x04, + 'o', + 'k', + 'a', + 'y', + 0x00, + 0x40, + 'b', + 'a', + 'd', + }; + domains = nm_dhcp_lease_data_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, + const char * expected_gw, + guint expected_prefix) +{ + const NMPlatformIP4Route *route; + guint32 tmp; + + g_assert(expected_prefix <= 32); + + 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); + g_assert(route->gateway == tmp); + g_assert_cmpint(route->plen, ==, expected_prefix); + g_assert_cmpint(route->metric, ==, 0); +} + +static void +ip4_test_gateway(NMIP4Config *ip4_config, const char *expected_gw) +{ + guint32 tmp; + + g_assert_cmpint(nm_ip4_config_get_num_addresses(ip4_config), ==, 1); + g_assert(inet_pton(AF_INET, expected_gw, &tmp) > 0); + g_assert(nmtst_ip4_config_get_gateway(ip4_config) == tmp); +} + +static void +test_classless_static_routes_1(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "192.168.10.0"; + const char * expected_route1_gw = "192.168.1.1"; + const char * expected_route2_dest = "10.0.0.0"; + const char * expected_route2_gw = "10.17.66.41"; + static const Option data[] = { + /* dhclient custom format */ + {"rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 8 10 10 17 66 41"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + ip4_config = _ip4_config_from_options(1, "eth0", options, 0); + + /* IP4 routes */ + 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); +} + +static void +test_classless_static_routes_2(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "192.168.10.0"; + const char * expected_route1_gw = "192.168.1.1"; + const char * expected_route2_dest = "10.0.0.0"; + const char * expected_route2_gw = "10.17.66.41"; + static const Option data[] = { + /* dhcpcd format */ + {"classless_static_routes", "192.168.10.0/24 192.168.1.1 10.0.0.0/8 10.17.66.41"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + ip4_config = _ip4_config_from_options(1, "eth0", options, 0); + + /* IP4 routes */ + 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); +} + +static void +test_fedora_dhclient_classless_static_routes(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "129.210.177.128"; + const char * expected_route1_gw = "192.168.0.113"; + const char * expected_route2_dest = "2.0.0.0"; + const char * expected_route2_gw = "10.34.255.6"; + const char * expected_gateway = "192.168.0.113"; + static const Option data[] = { + /* Fedora dhclient format */ + {"classless_static_routes", + "0 192.168.0.113 25.129.210.177.132 192.168.0.113 7.2 10.34.255.6"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + ip4_config = _ip4_config_from_options(1, "eth0", options, 0); + + /* IP4 routes */ + 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); + + g_hash_table_destroy(options); +} + +static void +test_dhclient_invalid_classless_routes_1(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "192.168.10.0"; + const char * expected_route1_gw = "192.168.1.1"; + static const Option data[] = { + /* dhclient format */ + {"rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 45 10 17 66 41"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + + NMTST_EXPECT_NM_WARN("*ignoring invalid classless static routes*"); + 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), ==, 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); +} + +static void +test_dhcpcd_invalid_classless_routes_1(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "10.1.1.5"; + const char * expected_route1_gw = "10.1.1.1"; + const char * expected_route2_dest = "100.99.88.56"; + const char * expected_route2_gw = "10.1.1.1"; + static const Option data[] = { + /* dhcpcd format */ + {"classless_static_routes", "192.168.10.0/24 192.168.1.1 10.0.adfadf/44 10.17.66.41"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + + NMTST_EXPECT_NM_WARN("*ignoring invalid classless static routes*"); + 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), ==, 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); +} + +static void +test_dhclient_invalid_classless_routes_2(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "10.1.1.5"; + const char * expected_route1_gw = "10.1.1.1"; + const char * expected_route2_dest = "100.99.88.56"; + const char * expected_route2_gw = "10.1.1.1"; + static const Option data[] = { + {"rfc3442_classless_static_routes", "45 10 17 66 41 24 192 168 10 192 168 1 1"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + + NMTST_EXPECT_NM_WARN("*ignoring invalid classless static routes*"); + 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), ==, 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); +} + +static void +test_dhcpcd_invalid_classless_routes_2(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "10.1.1.5"; + const char * expected_route1_gw = "10.1.1.1"; + const char * expected_route2_dest = "100.99.88.56"; + const char * expected_route2_gw = "10.1.1.1"; + static const Option data[] = { + {"classless_static_routes", "10.0.adfadf/44 10.17.66.41 192.168.10.0/24 192.168.1.1"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + + NMTST_EXPECT_NM_WARN("*ignoring invalid classless static routes*"); + 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. + */ + + /* Routes */ + 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); +} + +static void +test_dhclient_invalid_classless_routes_3(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "192.168.10.0"; + const char * expected_route1_gw = "192.168.1.1"; + static const Option data[] = { + {"rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 32 128 10 17 66 41"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + + NMTST_EXPECT_NM_WARN("*ignoring invalid classless static routes*"); + 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), ==, 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); +} + +static void +test_dhcpcd_invalid_classless_routes_3(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "192.168.10.0"; + const char * expected_route1_gw = "192.168.1.1"; + static Option data[] = { + {"classless_static_routes", "192.168.10.0/24 192.168.1.1 128/32 10.17.66.41"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + + NMTST_EXPECT_NM_WARN("*DHCP provided invalid classless static route*"); + 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), ==, 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); +} + +static void +test_dhclient_gw_in_classless_routes(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "192.168.10.0"; + const char * expected_route1_gw = "192.168.1.1"; + const char * expected_gateway = "192.2.3.4"; + static Option data[] = { + {"rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 0 192 2 3 4"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + ip4_config = _ip4_config_from_options(1, "eth0", options, 0); + + /* IP4 routes */ + 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); + + g_hash_table_destroy(options); +} + +static void +test_dhcpcd_gw_in_classless_routes(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_route1_dest = "192.168.10.0"; + const char * expected_route1_gw = "192.168.1.1"; + const char * expected_gateway = "192.2.3.4"; + static Option data[] = { + {"classless_static_routes", "192.168.10.0/24 192.168.1.1 0.0.0.0/0 192.2.3.4"}, + {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + ip4_config = _ip4_config_from_options(1, "eth0", options, 0); + + /* IP4 routes */ + 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); + + g_hash_table_destroy(options); +} + +static void +test_escaped_domain_searches(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const char * expected_search0 = "host1"; + const char * expected_search1 = "host2"; + const char * expected_search2 = "host3"; + static const Option data[] = {{"domain_search", "host1\\032host2\\032host3"}, {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + ip4_config = _ip4_config_from_options(1, "eth0", options, 0); + + /* domain searches */ + g_assert_cmpint(nm_ip4_config_get_num_searches(ip4_config), ==, 3); + g_assert_cmpstr(nm_ip4_config_get_search(ip4_config, 0), ==, expected_search0); + g_assert_cmpstr(nm_ip4_config_get_search(ip4_config, 1), ==, expected_search1); + g_assert_cmpstr(nm_ip4_config_get_search(ip4_config, 2), ==, expected_search2); + + g_hash_table_destroy(options); +} + +static void +test_invalid_escaped_domain_searches(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + static const Option data[] = {{"domain_search", "host1\\aahost2\\032host3"}, {NULL, NULL}}; + + options = fill_table(generic_options, NULL); + options = fill_table(data, options); + + NMTST_EXPECT_NM_WARN("*invalid domain search*"); + ip4_config = _ip4_config_from_options(1, "eth0", options, 0); + g_test_assert_expected_messages(); + + /* domain searches */ + g_assert_cmpint(nm_ip4_config_get_num_searches(ip4_config), ==, 0); + + g_hash_table_destroy(options); +} + +static void +test_ip4_missing_prefix(const char *ip, guint32 expected_prefix) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const NMPlatformIP4Address * address; + + options = fill_table(generic_options, NULL); + g_hash_table_insert(options, "ip_address", (gpointer) ip); + g_hash_table_remove(options, "subnet_mask"); + + ip4_config = _ip4_config_from_options(1, "eth0", options, 0); + + g_assert_cmpint(nm_ip4_config_get_num_addresses(ip4_config), ==, 1); + address = _nmtst_ip4_config_get_address(ip4_config, 0); + g_assert(address); + g_assert_cmpint(address->plen, ==, expected_prefix); + + g_hash_table_destroy(options); +} + +static void +test_ip4_missing_prefix_24(void) +{ + test_ip4_missing_prefix("192.168.1.10", 24); +} + +static void +test_ip4_missing_prefix_16(void) +{ + test_ip4_missing_prefix("172.16.54.50", 16); +} + +static void +test_ip4_missing_prefix_8(void) +{ + test_ip4_missing_prefix("10.1.2.3", 8); +} + +static void +test_ip4_prefix_classless(void) +{ + GHashTable * options; + gs_unref_object NMIP4Config *ip4_config = NULL; + const NMPlatformIP4Address * address; + + /* Ensure that the missing-subnet-mask handler doesn't mangle classless + * subnet masks at all. The handler should trigger only if the server + * doesn't send the subnet mask. + */ + + options = fill_table(generic_options, NULL); + g_hash_table_insert(options, "ip_address", "172.16.54.22"); + g_hash_table_insert(options, "subnet_mask", "255.255.252.0"); + + ip4_config = _ip4_config_from_options(1, "eth0", options, 0); + + g_assert_cmpint(nm_ip4_config_get_num_addresses(ip4_config), ==, 1); + address = _nmtst_ip4_config_get_address(ip4_config, 0); + g_assert(address); + g_assert_cmpint(address->plen, ==, 22); + + g_hash_table_destroy(options); +} + +#define COMPARE_ID(src, is_str, expected, expected_len) \ + G_STMT_START \ + { \ + gs_unref_bytes GBytes *b = NULL; \ + const char * p; \ + gsize l; \ + \ + b = nm_dhcp_utils_client_id_string_to_bytes(src); \ + g_assert(b); \ + p = g_bytes_get_data(b, &l); \ + if (is_str) { \ + g_assert_cmpint(l, ==, expected_len + 1); \ + g_assert_cmpint(((const char *) p)[0], ==, 0); \ + g_assert(memcmp(p + 1, expected, expected_len) == 0); \ + } else { \ + g_assert_cmpint(l, ==, expected_len); \ + g_assert(memcmp(p, expected, expected_len) == 0); \ + } \ + } \ + G_STMT_END + +static void +test_client_id_from_string(void) +{ + const char * nothex = "asdfasdfasdfasdfasdfasdfasdf"; + const char * allhex = "00:11:22:33:4:55:66:77:88"; + const guint8 allhex_bin[] = {0x00, 0x11, 0x22, 0x33, 0x04, 0x55, 0x66, 0x77, 0x88}; + const char * somehex = "00:11:22:33:44:55:asdfasdfasdf:99:10"; + const char * nocolons = "0011223344559910"; + const char * endcolon = "00:11:22:33:44:55:"; + + COMPARE_ID(nothex, TRUE, nothex, strlen(nothex)); + COMPARE_ID(allhex, FALSE, allhex_bin, sizeof(allhex_bin)); + COMPARE_ID(somehex, TRUE, somehex, strlen(somehex)); + COMPARE_ID(nocolons, TRUE, nocolons, strlen(nocolons)); + COMPARE_ID(endcolon, TRUE, endcolon, strlen(endcolon)); +} + +/*****************************************************************************/ + +static void +test_dhcp_opt_list(gconstpointer test_data) +{ + const gboolean IS_IPv4 = (GPOINTER_TO_INT(test_data) == 0); + const int addr_family = IS_IPv4 ? AF_INET : AF_INET6; + const NMDhcpOption *const options = + IS_IPv4 ? _nm_dhcp_option_dhcp4_options : _nm_dhcp_option_dhcp6_options; + const guint n = (IS_IPv4 ? G_N_ELEMENTS(_nm_dhcp_option_dhcp4_options) + : G_N_ELEMENTS(_nm_dhcp_option_dhcp6_options)); + guint i; + guint j; + + g_assert(options); + g_assert(n > 0); + + for (i = 0; i < n; i++) { + const NMDhcpOption *const opt = &options[i]; + + g_assert_cmpstr(opt->name, !=, NULL); + g_assert(NM_STR_HAS_PREFIX(opt->name, NM_DHCP_OPTION_REQPREFIX)); + + for (j = 0; j < i; j++) { + const NMDhcpOption *const opt2 = &options[j]; + + g_assert_cmpstr(opt->name, !=, opt2->name); + g_assert_cmpint(opt->option_num, !=, opt2->option_num); + } + } + + for (i = 0; i < n; i++) { + const NMDhcpOption *const opt = &options[i]; + + g_assert(opt == nm_dhcp_option_find(addr_family, opt->option_num)); + } +} + +/*****************************************************************************/ + +NMTST_DEFINE(); + +int +main(int argc, char **argv) +{ + nmtst_init_assert_logging(&argc, &argv, "WARN", "DEFAULT"); + + g_test_add_func("/dhcp/generic-options", test_generic_options); + g_test_add_func("/dhcp/wins-options", test_wins_options); + g_test_add_func("/dhcp/classless-static-routes-1", test_classless_static_routes_1); + g_test_add_func("/dhcp/classless-static-routes-2", test_classless_static_routes_2); + g_test_add_func("/dhcp/fedora-dhclient-classless-static-routes", + test_fedora_dhclient_classless_static_routes); + g_test_add_func("/dhcp/dhclient-invalid-classless-routes-1", + test_dhclient_invalid_classless_routes_1); + g_test_add_func("/dhcp/dhcpcd-invalid-classless-routes-1", + test_dhcpcd_invalid_classless_routes_1); + g_test_add_func("/dhcp/dhclient-invalid-classless-routes-2", + test_dhclient_invalid_classless_routes_2); + g_test_add_func("/dhcp/dhcpcd-invalid-classless-routes-2", + test_dhcpcd_invalid_classless_routes_2); + g_test_add_func("/dhcp/dhclient-invalid-classless-routes-3", + test_dhclient_invalid_classless_routes_3); + g_test_add_func("/dhcp/dhcpcd-invalid-classless-routes-3", + test_dhcpcd_invalid_classless_routes_3); + g_test_add_func("/dhcp/dhclient-gw-in-classless-routes", test_dhclient_gw_in_classless_routes); + g_test_add_func("/dhcp/dhcpcd-gw-in-classless-routes", test_dhcpcd_gw_in_classless_routes); + g_test_add_func("/dhcp/escaped-domain-searches", test_escaped_domain_searches); + g_test_add_func("/dhcp/invalid-escaped-domain-searches", test_invalid_escaped_domain_searches); + g_test_add_func("/dhcp/ip4-missing-prefix-24", test_ip4_missing_prefix_24); + g_test_add_func("/dhcp/ip4-missing-prefix-16", test_ip4_missing_prefix_16); + g_test_add_func("/dhcp/ip4-missing-prefix-8", test_ip4_missing_prefix_8); + 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); + g_test_add_data_func("/dhcp/test_dhcp_opt_list/IPv4", GINT_TO_POINTER(0), test_dhcp_opt_list); + g_test_add_data_func("/dhcp/test_dhcp_opt_list/IPv6", GINT_TO_POINTER(1), test_dhcp_opt_list); + + return g_test_run(); +} |